SlotControl.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System.Numerics;
  2. using Content.Client.Cooldown;
  3. using Content.Client.UserInterface.Systems.Inventory.Controls;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Shared.Input;
  7. namespace Content.Client.UserInterface.Controls
  8. {
  9. [Virtual]
  10. public abstract class SlotControl : Control, IEntityControl
  11. {
  12. public static int DefaultButtonSize = 64;
  13. public TextureRect ButtonRect { get; }
  14. public TextureRect BlockedRect { get; }
  15. public TextureRect HighlightRect { get; }
  16. public SpriteView HoverSpriteView { get; }
  17. public TextureButton StorageButton { get; }
  18. public CooldownGraphic CooldownDisplay { get; }
  19. private SpriteView SpriteView { get; }
  20. public EntityUid? Entity => SpriteView.Entity;
  21. private bool _slotNameSet;
  22. private string _slotName = "";
  23. public string SlotName
  24. {
  25. get => _slotName;
  26. set
  27. {
  28. //this auto registers the button with it's parent container when it's set
  29. if (_slotNameSet)
  30. {
  31. Logger.Warning("Tried to set slotName after init for:" + Name);
  32. return;
  33. }
  34. _slotNameSet = true;
  35. if (Parent is IItemslotUIContainer container)
  36. {
  37. container.TryRegisterButton(this, value);
  38. }
  39. Name = "SlotButton_" + value;
  40. _slotName = value;
  41. }
  42. }
  43. public bool Highlight { get => HighlightRect.Visible; set => HighlightRect.Visible = value;}
  44. public bool Blocked { get => BlockedRect.Visible; set => BlockedRect.Visible = value;}
  45. private string? _blockedTexturePath;
  46. public string? BlockedTexturePath
  47. {
  48. get => _blockedTexturePath;
  49. set
  50. {
  51. _blockedTexturePath = value;
  52. BlockedRect.Texture = Theme.ResolveTextureOrNull(_blockedTexturePath)?.Texture;
  53. }
  54. }
  55. private string? _buttonTexturePath;
  56. public string? ButtonTexturePath
  57. {
  58. get => _buttonTexturePath;
  59. set
  60. {
  61. _buttonTexturePath = value;
  62. UpdateButtonTexture();
  63. }
  64. }
  65. private string? _fullButtonTexturePath;
  66. public string? FullButtonTexturePath
  67. {
  68. get => _fullButtonTexturePath;
  69. set
  70. {
  71. _fullButtonTexturePath = value;
  72. UpdateButtonTexture();
  73. }
  74. }
  75. private string? _storageTexturePath;
  76. public string? StorageTexturePath
  77. {
  78. get => _buttonTexturePath;
  79. set
  80. {
  81. _storageTexturePath = value;
  82. StorageButton.TextureNormal = Theme.ResolveTextureOrNull(_storageTexturePath)?.Texture;
  83. }
  84. }
  85. private string? _highlightTexturePath;
  86. public string? HighlightTexturePath
  87. {
  88. get => _highlightTexturePath;
  89. set
  90. {
  91. _highlightTexturePath = value;
  92. HighlightRect.Texture = Theme.ResolveTextureOrNull(_highlightTexturePath)?.Texture;
  93. }
  94. }
  95. public event Action<GUIBoundKeyEventArgs, SlotControl>? Pressed;
  96. public event Action<GUIBoundKeyEventArgs, SlotControl>? Unpressed;
  97. public event Action<GUIBoundKeyEventArgs, SlotControl>? StoragePressed;
  98. public event Action<GUIMouseHoverEventArgs, SlotControl>? Hover;
  99. public bool EntityHover => HoverSpriteView.Sprite != null;
  100. public bool MouseIsHovering;
  101. public SlotControl()
  102. {
  103. IoCManager.InjectDependencies(this);
  104. Name = "SlotButton_null";
  105. MinSize = new Vector2(DefaultButtonSize, DefaultButtonSize);
  106. AddChild(ButtonRect = new TextureRect
  107. {
  108. TextureScale = new Vector2(2, 2),
  109. MouseFilter = MouseFilterMode.Stop
  110. });
  111. AddChild(HighlightRect = new TextureRect
  112. {
  113. Visible = false,
  114. TextureScale = new Vector2(2, 2),
  115. MouseFilter = MouseFilterMode.Ignore
  116. });
  117. ButtonRect.OnKeyBindDown += OnButtonPressed;
  118. ButtonRect.OnKeyBindUp += OnButtonUnpressed;
  119. AddChild(SpriteView = new SpriteView
  120. {
  121. Scale = new Vector2(2, 2),
  122. SetSize = new Vector2(DefaultButtonSize, DefaultButtonSize),
  123. OverrideDirection = Direction.South
  124. });
  125. AddChild(HoverSpriteView = new SpriteView
  126. {
  127. Scale = new Vector2(2, 2),
  128. SetSize = new Vector2(DefaultButtonSize, DefaultButtonSize),
  129. OverrideDirection = Direction.South
  130. });
  131. AddChild(StorageButton = new TextureButton
  132. {
  133. Scale = new Vector2(0.75f, 0.75f),
  134. HorizontalAlignment = HAlignment.Right,
  135. VerticalAlignment = VAlignment.Bottom,
  136. Visible = false,
  137. });
  138. StorageButton.OnKeyBindDown += args =>
  139. {
  140. if (args.Function != EngineKeyFunctions.UIClick)
  141. {
  142. OnButtonPressed(args);
  143. }
  144. };
  145. StorageButton.OnPressed += OnStorageButtonPressed;
  146. ButtonRect.OnMouseEntered += _ =>
  147. {
  148. MouseIsHovering = true;
  149. };
  150. ButtonRect.OnMouseEntered += OnButtonHover;
  151. ButtonRect.OnMouseExited += _ =>
  152. {
  153. MouseIsHovering = false;
  154. ClearHover();
  155. };
  156. AddChild(CooldownDisplay = new CooldownGraphic
  157. {
  158. Visible = false,
  159. });
  160. AddChild(BlockedRect = new TextureRect
  161. {
  162. TextureScale = new Vector2(2, 2),
  163. MouseFilter = MouseFilterMode.Stop,
  164. Visible = false
  165. });
  166. HighlightTexturePath = "slot_highlight";
  167. BlockedTexturePath = "blocked";
  168. }
  169. public void ClearHover()
  170. {
  171. if (!EntityHover)
  172. return;
  173. var tempQualifier = HoverSpriteView.Entity;
  174. if (tempQualifier != null)
  175. {
  176. IoCManager.Resolve<IEntityManager>().QueueDeleteEntity(tempQualifier);
  177. }
  178. HoverSpriteView.SetEntity(null);
  179. }
  180. public void SetEntity(EntityUid? ent)
  181. {
  182. SpriteView.SetEntity(ent);
  183. UpdateButtonTexture();
  184. }
  185. private void UpdateButtonTexture()
  186. {
  187. var fullTexture = Theme.ResolveTextureOrNull(_fullButtonTexturePath);
  188. var texture = Entity.HasValue && fullTexture != null
  189. ? fullTexture.Texture
  190. : Theme.ResolveTextureOrNull(_buttonTexturePath)?.Texture;
  191. ButtonRect.Texture = texture;
  192. }
  193. private void OnButtonPressed(GUIBoundKeyEventArgs args)
  194. {
  195. Pressed?.Invoke(args, this);
  196. }
  197. private void OnButtonUnpressed(GUIBoundKeyEventArgs args)
  198. {
  199. Unpressed?.Invoke(args, this);
  200. }
  201. private void OnStorageButtonPressed(BaseButton.ButtonEventArgs args)
  202. {
  203. if (args.Event.Function == EngineKeyFunctions.UIClick)
  204. {
  205. StoragePressed?.Invoke(args.Event, this);
  206. }
  207. else
  208. {
  209. Pressed?.Invoke(args.Event, this);
  210. }
  211. }
  212. private void OnButtonHover(GUIMouseHoverEventArgs args)
  213. {
  214. Hover?.Invoke(args, this);
  215. }
  216. protected override void OnThemeUpdated()
  217. {
  218. base.OnThemeUpdated();
  219. StorageButton.TextureNormal = Theme.ResolveTextureOrNull(_storageTexturePath)?.Texture;
  220. HighlightRect.Texture = Theme.ResolveTextureOrNull(_highlightTexturePath)?.Texture;
  221. UpdateButtonTexture();
  222. }
  223. EntityUid? IEntityControl.UiEntity => Entity;
  224. }
  225. }