ItemSlotUIContainer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Client.UserInterface.Controls;
  3. using Robust.Client.UserInterface.Controls;
  4. namespace Content.Client.UserInterface.Systems.Inventory.Controls;
  5. public interface IItemslotUIContainer
  6. {
  7. public bool TryRegisterButton(SlotControl control, string newSlotName);
  8. public bool TryAddButton(SlotControl control);
  9. }
  10. [Virtual]
  11. public abstract class ItemSlotUIContainer<T> : GridContainer, IItemslotUIContainer where T : SlotControl
  12. {
  13. protected readonly Dictionary<string, T> Buttons = new();
  14. private int? _maxColumns;
  15. public int? MaxColumns
  16. {
  17. get => _maxColumns;
  18. set => _maxColumns = value;
  19. }
  20. public virtual bool TryAddButton(T newButton, out T button)
  21. {
  22. var tempButton = AddButton(newButton);
  23. if (tempButton == null)
  24. {
  25. button = newButton;
  26. return false;
  27. }
  28. button = newButton;
  29. return true;
  30. }
  31. public void ClearButtons()
  32. {
  33. foreach (var button in Buttons.Values)
  34. {
  35. button.Dispose();
  36. }
  37. Buttons.Clear();
  38. }
  39. public bool TryRegisterButton(SlotControl control, string newSlotName)
  40. {
  41. if (newSlotName == "")
  42. return false;
  43. if (!(control is T slotButton))
  44. return false;
  45. if (Buttons.TryGetValue(newSlotName, out var foundButton))
  46. {
  47. if (control == foundButton)
  48. return true; //if the slotName is already set do nothing
  49. throw new Exception("Could not update button to slot:" + newSlotName + " slot already assigned!");
  50. }
  51. Buttons.Remove(slotButton.SlotName);
  52. AddButton(slotButton);
  53. return true;
  54. }
  55. public bool TryAddButton(SlotControl control)
  56. {
  57. if (control is not T newButton)
  58. return false;
  59. return AddButton(newButton) != null;
  60. }
  61. public virtual T? AddButton(T newButton)
  62. {
  63. if (!Children.Contains(newButton) && newButton.Parent == null && newButton.SlotName != "")
  64. AddChild(newButton);
  65. Columns = _maxColumns ?? ChildCount;
  66. return AddButtonToDict(newButton);
  67. }
  68. protected virtual T? AddButtonToDict(T newButton)
  69. {
  70. if (newButton.SlotName == "")
  71. {
  72. Logger.Warning("Could not add button " + newButton.Name + "No slotname");
  73. }
  74. return !Buttons.TryAdd(newButton.SlotName, newButton) ? null : newButton;
  75. }
  76. public virtual void RemoveButton(string slotName)
  77. {
  78. if (!Buttons.TryGetValue(slotName, out var button))
  79. return;
  80. RemoveButton(button);
  81. }
  82. public virtual void RemoveButtons(params string[] slotNames)
  83. {
  84. foreach (var slotName in slotNames)
  85. {
  86. RemoveButton(slotName);
  87. }
  88. }
  89. public virtual void RemoveButtons(params T?[] buttons)
  90. {
  91. foreach (var button in buttons)
  92. {
  93. if (button != null)
  94. RemoveButton(button);
  95. }
  96. }
  97. protected virtual void RemoveButtonFromDict(T button)
  98. {
  99. Buttons.Remove(button.SlotName);
  100. }
  101. public virtual void RemoveButton(T button)
  102. {
  103. RemoveButtonFromDict(button);
  104. Children.Remove(button);
  105. button.Dispose();
  106. }
  107. public virtual T? GetButton(string slotName)
  108. {
  109. return !Buttons.TryGetValue(slotName, out var button) ? null : button;
  110. }
  111. public virtual bool TryGetButton(string slotName, [NotNullWhen(true)] out T? button)
  112. {
  113. return (button = GetButton(slotName)) != null;
  114. }
  115. }