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

@@ -42,6 +42,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
[Dependency] private readonly IOverlayManager _overlays = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IEntityManager _entMan = default!;
[UISystemDependency] private readonly ActionsSystem? _actionsSystem = default;
[UISystemDependency] private readonly InteractionOutlineSystem? _interactionOutline = default;
@@ -113,12 +114,11 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
{
_actionsSystem.OnActionAdded += OnActionAdded;
_actionsSystem.OnActionRemoved += OnActionRemoved;
_actionsSystem.ActionReplaced += OnActionReplaced;
_actionsSystem.ActionsUpdated += OnActionsUpdated;
}
UpdateFilterLabel();
SearchAndDisplay();
QueueWindowUpdate();
_dragShadow.Orphan();
UIManager.PopupRoot.AddChild(_dragShadow);
@@ -307,6 +307,8 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
{
if (ActionButton != null)
ActionButton.Pressed = true;
SearchAndDisplay();
}
private void OnWindowClosed()
@@ -321,7 +323,6 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
{
_actionsSystem.OnActionAdded -= OnActionAdded;
_actionsSystem.OnActionRemoved -= OnActionRemoved;
_actionsSystem.ActionReplaced -= OnActionReplaced;
_actionsSystem.ActionsUpdated -= OnActionsUpdated;
}
@@ -345,6 +346,9 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private void ChangePage(int index)
{
if (_actionsSystem == null)
return;
var lastPage = _pages.Count - 1;
if (index < 0)
{
@@ -357,7 +361,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
_currentPageIndex = index;
var page = _pages[_currentPageIndex];
_container?.SetActionData(page);
_container?.SetActionData(_actionsSystem, page);
ActionsBar!.PageButtons.Label.Text = $"{_currentPageIndex + 1}";
}
@@ -424,7 +428,6 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
}
AppendAction(actionId);
SearchAndDisplay();
}
private void OnActionRemoved(EntityUid actionId)
@@ -454,24 +457,11 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
}
}
}
SearchAndDisplay();
}
private void OnActionReplaced(EntityUid actionId)
{
if (_container == null)
return;
foreach (var button in _container.GetButtons())
{
if (button.ActionId == actionId)
button.UpdateData(actionId);
}
}
private void OnActionsUpdated()
{
QueueWindowUpdate();
if (_container == null)
return;
@@ -538,27 +528,56 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private void PopulateActions(IEnumerable<(EntityUid Id, BaseActionComponent Comp)> actions)
{
if (_window == null)
if (_window is not { Disposed: false, IsOpen: true })
return;
ClearList();
if (_actionsSystem == null)
return;
_window.UpdateNeeded = false;
List<ActionButton> existing = new(_window.ResultsGrid.ChildCount);
foreach (var child in _window.ResultsGrid.Children)
{
if (child is ActionButton button)
existing.Add(button);
}
int i = 0;
foreach (var action in actions)
{
var button = new ActionButton {Locked = true};
if (i < existing.Count)
{
existing[i++].UpdateData(action.Id, _actionsSystem);
continue;
}
button.UpdateData(action.Id);
var button = new ActionButton(_entMan, _spriteSystem, this) {Locked = true};
button.ActionPressed += OnWindowActionPressed;
button.ActionUnpressed += OnWindowActionUnPressed;
button.ActionFocusExited += OnWindowActionFocusExisted;
button.UpdateData(action.Id, _actionsSystem);
_window.ResultsGrid.AddChild(button);
}
for (; i < existing.Count; i++)
{
existing[i].Dispose();
}
}
public void QueueWindowUpdate()
{
if (_window != null)
_window.UpdateNeeded = true;
}
private void SearchAndDisplay()
{
if (_window is not { Disposed: false } || _actionsSystem == null)
if (_window is not { Disposed: false, IsOpen: true })
return;
if (_actionsSystem == null)
return;
if (_playerManager.LocalPlayer?.ControlledEntity is not { } player)
@@ -598,6 +617,9 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private void SetAction(ActionButton button, EntityUid? actionId)
{
if (_actionsSystem == null)
return;
int position;
if (actionId == null)
@@ -611,7 +633,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
return;
}
if (button.TryReplaceWith(actionId.Value) &&
if (button.TryReplaceWith(actionId.Value, _actionsSystem) &&
_container != null &&
_container.TryGetButtonIndex(button, out position))
{
@@ -648,18 +670,18 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
_window.SearchBar.Clear();
_window.FilterButton.DeselectAll();
UpdateFilterLabel();
SearchAndDisplay();
QueueWindowUpdate();
}
private void OnSearchChanged(LineEditEventArgs args)
{
SearchAndDisplay();
QueueWindowUpdate();
}
private void OnFilterSelected(ItemPressedEventArgs args)
{
UpdateFilterLabel();
SearchAndDisplay();
QueueWindowUpdate();
}
private void OnWindowActionPressed(GUIBoundKeyEventArgs args, ActionButton action)
@@ -849,12 +871,15 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private void AssignSlots(List<SlotAssignment> assignments)
{
if (_actionsSystem == null)
return;
foreach (ref var assignment in CollectionsMarshal.AsSpan(assignments))
{
_pages[assignment.Hotbar][assignment.Slot] = assignment.ActionId;
}
_container?.SetActionData(_pages[_currentPageIndex]);
_container?.SetActionData(_actionsSystem, _pages[_currentPageIndex]);
}
public void RemoveActionContainer()
@@ -881,19 +906,24 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
public override void FrameUpdate(FrameEventArgs args)
{
_menuDragHelper.Update(args.DeltaSeconds);
if (_window is {UpdateNeeded: true})
SearchAndDisplay();
}
private void OnComponentLinked(ActionsComponent component)
{
if (_actionsSystem == null)
return;
LoadDefaultActions(component);
_container?.SetActionData(_pages[DefaultPageIndex]);
SearchAndDisplay();
_container?.SetActionData(_actionsSystem, _pages[DefaultPageIndex]);
QueueWindowUpdate();
}
private void OnComponentUnlinked()
{
_container?.ClearActionData();
SearchAndDisplay();
QueueWindowUpdate();
StopTargeting();
}