HandsContainer.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Linq;
  2. using Content.Client.UserInterface.Systems.Inventory.Controls;
  3. using Robust.Client.UserInterface.Controls;
  4. namespace Content.Client.UserInterface.Systems.Hands.Controls;
  5. public sealed class HandsContainer : ItemSlotUIContainer<HandButton>
  6. {
  7. private readonly GridContainer _grid;
  8. public int ColumnLimit { get => _grid.Columns; set => _grid.Columns = value; }
  9. public int MaxButtonCount { get; set; } = 0;
  10. public int MaxButtonsPerRow { get; set; }= 6;
  11. /// <summary>
  12. /// Indexer. This is used to reference a HandsContainer from the
  13. /// controller.
  14. /// </summary>
  15. public string? Indexer { get; set; }
  16. public HandsContainer()
  17. {
  18. AddChild(_grid = new GridContainer());
  19. _grid.ExpandBackwards = true;
  20. }
  21. public override HandButton? AddButton(HandButton newButton)
  22. {
  23. if (MaxButtonCount > 0)
  24. {
  25. if (ButtonCount >= MaxButtonCount)
  26. return null;
  27. _grid.AddChild(newButton);
  28. }
  29. else
  30. {
  31. _grid.AddChild(newButton);
  32. }
  33. _grid.Columns = Math.Min(_grid.ChildCount, MaxButtonsPerRow);
  34. return base.AddButton(newButton);
  35. }
  36. public override void RemoveButton(string handName)
  37. {
  38. var button = GetButton(handName);
  39. if (button == null)
  40. return;
  41. base.RemoveButton(button);
  42. _grid.RemoveChild(button);
  43. }
  44. public bool TryGetLastButton(out HandButton? control)
  45. {
  46. if (Buttons.Count == 0)
  47. {
  48. control = null;
  49. return false;
  50. }
  51. control = Buttons.Values.Last();
  52. return true;
  53. }
  54. public bool TryRemoveLastHand(out HandButton? control)
  55. {
  56. var success = TryGetLastButton(out control);
  57. if (control != null)
  58. RemoveButton(control);
  59. return success;
  60. }
  61. public void Clear()
  62. {
  63. ClearButtons();
  64. _grid.DisposeAllChildren();
  65. }
  66. public IEnumerable<HandButton> GetButtons()
  67. {
  68. foreach (var child in _grid.Children)
  69. {
  70. if (child is HandButton hand)
  71. yield return hand;
  72. }
  73. }
  74. public bool IsFull => (MaxButtonCount != 0 && ButtonCount >= MaxButtonCount);
  75. public int ButtonCount => _grid.ChildCount;
  76. }