ActionButtonContainer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Linq;
  2. using Content.Client.Actions;
  3. using Content.Shared.Input;
  4. using Robust.Client.Input;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Shared.Utility;
  8. namespace Content.Client.UserInterface.Systems.Actions.Controls;
  9. [Virtual]
  10. public class ActionButtonContainer : GridContainer
  11. {
  12. [Dependency] private readonly IEntityManager _entity = default!;
  13. [Dependency] private readonly IInputManager _input = default!;
  14. public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionPressed;
  15. public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionUnpressed;
  16. public event Action<ActionButton>? ActionFocusExited;
  17. public ActionButtonContainer()
  18. {
  19. IoCManager.InjectDependencies(this);
  20. }
  21. public ActionButton this[int index]
  22. {
  23. get => (ActionButton) GetChild(index);
  24. }
  25. public void SetActionData(ActionsSystem system, params EntityUid?[] actionTypes)
  26. {
  27. var uniqueCount = Math.Min(system.GetClientActions().Count(), actionTypes.Length + 1);
  28. var keys = ContentKeyFunctions.GetHotbarBoundKeys();
  29. for (var i = 0; i < uniqueCount; i++)
  30. {
  31. if (i >= ChildCount)
  32. {
  33. AddChild(MakeButton(i));
  34. }
  35. if (!actionTypes.TryGetValue(i, out var action))
  36. action = null;
  37. ((ActionButton) GetChild(i)).UpdateData(action, system);
  38. }
  39. for (var i = ChildCount - 1; i >= uniqueCount; i--)
  40. {
  41. RemoveChild(GetChild(i));
  42. }
  43. ActionButton MakeButton(int index)
  44. {
  45. var button = new ActionButton(_entity);
  46. if (!keys.TryGetValue(index, out var boundKey))
  47. return button;
  48. button.KeyBind = boundKey;
  49. if (_input.TryGetKeyBinding(boundKey, out var binding))
  50. {
  51. button.Label.Text = binding.GetKeyString();
  52. }
  53. return button;
  54. }
  55. }
  56. public void ClearActionData()
  57. {
  58. foreach (var button in Children)
  59. {
  60. ((ActionButton) button).ClearData();
  61. }
  62. }
  63. protected override void ChildAdded(Control newChild)
  64. {
  65. base.ChildAdded(newChild);
  66. if (newChild is not ActionButton button)
  67. return;
  68. button.ActionPressed += ActionPressed;
  69. button.ActionUnpressed += ActionUnpressed;
  70. button.ActionFocusExited += ActionFocusExited;
  71. }
  72. protected override void ChildRemoved(Control newChild)
  73. {
  74. if (newChild is not ActionButton button)
  75. return;
  76. button.ActionPressed -= ActionPressed;
  77. button.ActionUnpressed -= ActionUnpressed;
  78. button.ActionFocusExited -= ActionFocusExited;
  79. }
  80. public bool TryGetButtonIndex(ActionButton button, out int position)
  81. {
  82. if (button.Parent != this)
  83. {
  84. position = 0;
  85. return false;
  86. }
  87. position = button.GetPositionInParent();
  88. return true;
  89. }
  90. public IEnumerable<ActionButton> GetButtons()
  91. {
  92. foreach (var control in Children)
  93. {
  94. if (control is ActionButton button)
  95. yield return button;
  96. }
  97. }
  98. }