Slightly improve action ui performance (#20799)

This commit is contained in:
Leon Friedrich
2023-10-09 07:37:09 +11:00
committed by GitHub
parent 35f41742a7
commit 1a5cf370e7
6 changed files with 120 additions and 78 deletions

View File

@@ -8,8 +8,6 @@ using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.Utility;
using Robust.Shared.Graphics;
using Robust.Shared.Input;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
@@ -21,11 +19,9 @@ namespace Content.Client.UserInterface.Systems.Actions.Controls;
public sealed class ActionButton : Control, IEntityControl
{
private IEntityManager? _entities;
private ActionUIController Controller => UserInterfaceManager.GetUIController<ActionUIController>();
private IEntityManager Entities => _entities ??= IoCManager.Resolve<IEntityManager>();
private ActionsSystem Actions => Entities.System<ActionsSystem>();
private IEntityManager _entities;
private SpriteSystem? _spriteSys;
private ActionUIController? _controller;
private bool _beingHovered;
private bool _depressed;
private bool _toggled;
@@ -54,14 +50,21 @@ public sealed class ActionButton : Control, IEntityControl
private readonly SpriteView _bigItemSpriteView;
public EntityUid? ActionId { get; private set; }
private BaseActionComponent? _action;
public bool Locked { get; set; }
public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionPressed;
public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionUnpressed;
public event Action<ActionButton>? ActionFocusExited;
public ActionButton()
public ActionButton(IEntityManager entities, SpriteSystem? spriteSys = null, ActionUIController? controller = null)
{
// TODO why is this constructor so slooooow. The rest of the code is fine
_entities = entities;
_spriteSys = spriteSys;
_controller = controller;
MouseFilter = MouseFilterMode.Pass;
Button = new TextureRect
{
@@ -180,7 +183,7 @@ public sealed class ActionButton : Control, IEntityControl
private Control? SupplyTooltip(Control sender)
{
if (!Entities.TryGetComponent(ActionId, out MetaDataComponent? metadata))
if (!_entities.TryGetComponent(ActionId, out MetaDataComponent? metadata))
return null;
var name = FormattedMessage.FromMarkupPermissive(Loc.GetString(metadata.EntityName));
@@ -196,9 +199,8 @@ public sealed class ActionButton : Control, IEntityControl
private void UpdateItemIcon()
{
if (!Actions.TryGetActionData(ActionId, out var action) ||
action is not {EntityIcon: { } entity} ||
!Entities.HasComponent<SpriteComponent>(entity))
if (_action is not {EntityIcon: { } entity} ||
!_entities.HasComponent<SpriteComponent>(entity))
{
_bigItemSpriteView.Visible = false;
_bigItemSpriteView.SetEntity(null);
@@ -207,7 +209,7 @@ public sealed class ActionButton : Control, IEntityControl
}
else
{
switch (action.ItemIconStyle)
switch (_action.ItemIconStyle)
{
case ItemActionIconStyle.BigItem:
_bigItemSpriteView.Visible = true;
@@ -233,17 +235,17 @@ public sealed class ActionButton : Control, IEntityControl
private void SetActionIcon(Texture? texture)
{
if (!Actions.TryGetActionData(ActionId, out var action) || texture == null)
if (_action == null || texture == null)
{
_bigActionIcon.Texture = null;
_bigActionIcon.Visible = false;
_smallActionIcon.Texture = null;
_smallActionIcon.Visible = false;
}
else if (action.EntityIcon != null && action.ItemIconStyle == ItemActionIconStyle.BigItem)
else if (_action.EntityIcon != null && _action.ItemIconStyle == ItemActionIconStyle.BigItem)
{
_smallActionIcon.Texture = texture;
_smallActionIcon.Modulate = action.IconColor;
_smallActionIcon.Modulate = _action.IconColor;
_smallActionIcon.Visible = true;
_bigActionIcon.Texture = null;
_bigActionIcon.Visible = false;
@@ -251,7 +253,7 @@ public sealed class ActionButton : Control, IEntityControl
else
{
_bigActionIcon.Texture = texture;
_bigActionIcon.Modulate = action.IconColor;
_bigActionIcon.Modulate = _action.IconColor;
_bigActionIcon.Visible = true;
_smallActionIcon.Texture = null;
_smallActionIcon.Visible = false;
@@ -262,39 +264,43 @@ public sealed class ActionButton : Control, IEntityControl
{
UpdateItemIcon();
if (!Actions.TryGetActionData(ActionId, out var action))
if (_action == null)
{
SetActionIcon(null);
return;
}
if ((Controller.SelectingTargetFor == ActionId || action.Toggled) && action.IconOn != null)
SetActionIcon(action.IconOn.Frame0());
_controller ??= UserInterfaceManager.GetUIController<ActionUIController>();
_spriteSys ??= _entities.System<SpriteSystem>();
if ((_controller.SelectingTargetFor == ActionId || _action.Toggled) && _action.IconOn != null)
SetActionIcon(_spriteSys.Frame0(_action.IconOn));
else
SetActionIcon(action.Icon?.Frame0());
SetActionIcon(_action.Icon != null ? _spriteSys.Frame0(_action.Icon) : null);
}
public bool TryReplaceWith(EntityUid actionId)
public bool TryReplaceWith(EntityUid actionId, ActionsSystem system)
{
if (Locked)
{
return false;
}
UpdateData(actionId);
UpdateData(actionId, system);
return true;
}
public void UpdateData(EntityUid actionId)
public void UpdateData(EntityUid? actionId, ActionsSystem system)
{
ActionId = actionId;
Label.Visible = true;
system.TryGetActionData(actionId, out _action);
Label.Visible = actionId != null;
UpdateIcons();
}
public void ClearData()
{
ActionId = null;
_action = null;
Cooldown.Visible = false;
Cooldown.Progress = 1;
Label.Visible = false;
@@ -305,19 +311,17 @@ public sealed class ActionButton : Control, IEntityControl
{
base.FrameUpdate(args);
if (!Actions.TryGetActionData(ActionId, out var action))
{
if (_action == null)
return;
if (_action.Cooldown != null)
{
Cooldown.FromTime(_action.Cooldown.Value.Start, _action.Cooldown.Value.End);
}
if (action.Cooldown != null)
if (ActionId != null && _toggled != _action.Toggled)
{
Cooldown.FromTime(action.Cooldown.Value.Start, action.Cooldown.Value.End);
}
if (ActionId != null && _toggled != action.Toggled)
{
_toggled = action.Toggled;
_toggled = _action.Toggled;
}
}
@@ -344,7 +348,7 @@ public sealed class ActionButton : Control, IEntityControl
public void Depress(GUIBoundKeyEventArgs args, bool depress)
{
// action can still be toggled if it's allowed to stay selected
if (!Actions.TryGetActionData(ActionId, out var action) || action is not {Enabled: true})
if (_action is not {Enabled: true})
return;
if (_depressed && !depress)
@@ -362,14 +366,15 @@ public sealed class ActionButton : Control, IEntityControl
HighlightRect.Visible = _beingHovered;
// always show the normal empty button style if no action in this slot
if (!Actions.TryGetActionData(ActionId, out var action))
if (_action == null)
{
SetOnlyStylePseudoClass(ContainerButton.StylePseudoClassNormal);
return;
}
// show a hover only if the action is usable or another action is being dragged on top of this
if (_beingHovered && (Controller.IsDragging || action.Enabled))
_controller ??= UserInterfaceManager.GetUIController<ActionUIController>();
if (_beingHovered && (_controller.IsDragging || _action.Enabled))
{
SetOnlyStylePseudoClass(ContainerButton.StylePseudoClassHover);
}
@@ -384,16 +389,16 @@ public sealed class ActionButton : Control, IEntityControl
}
// if it's toggled on, always show the toggled on style (currently same as depressed style)
if (action.Toggled || Controller.SelectingTargetFor == ActionId)
if (_action.Toggled || _controller.SelectingTargetFor == ActionId)
{
// when there's a toggle sprite, we're showing that sprite instead of highlighting this slot
SetOnlyStylePseudoClass(action.IconOn != null
SetOnlyStylePseudoClass(_action.IconOn != null
? ContainerButton.StylePseudoClassNormal
: ContainerButton.StylePseudoClassPressed);
return;
}
if (!action.Enabled)
if (!_action.Enabled)
{
SetOnlyStylePseudoClass(ContainerButton.StylePseudoClassDisabled);
return;