HandsUIController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using Content.Client.Gameplay;
  2. using Content.Client.Hands.Systems;
  3. using Content.Client.UserInterface.Controls;
  4. using Content.Client.UserInterface.Systems.Hands.Controls;
  5. using Content.Client.UserInterface.Systems.Hotbar.Widgets;
  6. using Content.Shared.Hands.Components;
  7. using Content.Shared.Input;
  8. using Content.Shared.Inventory.VirtualItem;
  9. using Content.Shared.Timing;
  10. using Robust.Client.Player;
  11. using Robust.Client.UserInterface;
  12. using Robust.Client.UserInterface.Controllers;
  13. using Robust.Shared.Input;
  14. using Robust.Shared.Timing;
  15. using Robust.Shared.Utility;
  16. namespace Content.Client.UserInterface.Systems.Hands;
  17. public sealed class HandsUIController : UIController, IOnStateEntered<GameplayState>, IOnSystemChanged<HandsSystem>
  18. {
  19. [Dependency] private readonly IEntityManager _entities = default!;
  20. [Dependency] private readonly IPlayerManager _player = default!;
  21. [UISystemDependency] private readonly HandsSystem _handsSystem = default!;
  22. [UISystemDependency] private readonly UseDelaySystem _useDelay = default!;
  23. private readonly List<HandsContainer> _handsContainers = new();
  24. private readonly Dictionary<string, int> _handContainerIndices = new();
  25. private readonly Dictionary<string, HandButton> _handLookup = new();
  26. private HandsComponent? _playerHandsComponent;
  27. private HandButton? _activeHand = null;
  28. // We only have two item status controls (left and right hand),
  29. // but we may have more than two hands.
  30. // We handle this by having the item status be the *last active* hand of that side.
  31. // These variables store which that is.
  32. // ("middle" hands are hardcoded as right, whatever)
  33. private HandButton? _statusHandLeft;
  34. private HandButton? _statusHandRight;
  35. private int _backupSuffix = 0; //this is used when autogenerating container names if they don't have names
  36. private HotbarGui? HandsGui => UIManager.GetActiveUIWidgetOrNull<HotbarGui>();
  37. public void OnSystemLoaded(HandsSystem system)
  38. {
  39. _handsSystem.OnPlayerAddHand += OnAddHand;
  40. _handsSystem.OnPlayerItemAdded += OnItemAdded;
  41. _handsSystem.OnPlayerItemRemoved += OnItemRemoved;
  42. _handsSystem.OnPlayerSetActiveHand += SetActiveHand;
  43. _handsSystem.OnPlayerRemoveHand += RemoveHand;
  44. _handsSystem.OnPlayerHandsAdded += LoadPlayerHands;
  45. _handsSystem.OnPlayerHandsRemoved += UnloadPlayerHands;
  46. _handsSystem.OnPlayerHandBlocked += HandBlocked;
  47. _handsSystem.OnPlayerHandUnblocked += HandUnblocked;
  48. }
  49. public void OnSystemUnloaded(HandsSystem system)
  50. {
  51. _handsSystem.OnPlayerAddHand -= OnAddHand;
  52. _handsSystem.OnPlayerItemAdded -= OnItemAdded;
  53. _handsSystem.OnPlayerItemRemoved -= OnItemRemoved;
  54. _handsSystem.OnPlayerSetActiveHand -= SetActiveHand;
  55. _handsSystem.OnPlayerRemoveHand -= RemoveHand;
  56. _handsSystem.OnPlayerHandsAdded -= LoadPlayerHands;
  57. _handsSystem.OnPlayerHandsRemoved -= UnloadPlayerHands;
  58. _handsSystem.OnPlayerHandBlocked -= HandBlocked;
  59. _handsSystem.OnPlayerHandUnblocked -= HandUnblocked;
  60. }
  61. private void OnAddHand(string name, HandLocation location)
  62. {
  63. AddHand(name, location);
  64. }
  65. private void HandPressed(GUIBoundKeyEventArgs args, SlotControl hand)
  66. {
  67. if (_playerHandsComponent == null)
  68. {
  69. return;
  70. }
  71. if (args.Function == EngineKeyFunctions.UIClick)
  72. {
  73. _handsSystem.UIHandClick(_playerHandsComponent, hand.SlotName);
  74. args.Handle();
  75. }
  76. else if (args.Function == EngineKeyFunctions.UseSecondary)
  77. {
  78. _handsSystem.UIHandOpenContextMenu(hand.SlotName);
  79. args.Handle();
  80. }
  81. else if (args.Function == ContentKeyFunctions.ActivateItemInWorld)
  82. {
  83. _handsSystem.UIHandActivate(hand.SlotName);
  84. args.Handle();
  85. }
  86. else if (args.Function == ContentKeyFunctions.AltActivateItemInWorld)
  87. {
  88. _handsSystem.UIHandAltActivateItem(hand.SlotName);
  89. args.Handle();
  90. }
  91. else if (args.Function == ContentKeyFunctions.ExamineEntity)
  92. {
  93. _handsSystem.UIInventoryExamine(hand.SlotName);
  94. args.Handle();
  95. }
  96. }
  97. private void UnloadPlayerHands()
  98. {
  99. if (HandsGui != null)
  100. HandsGui.Visible = false;
  101. _handContainerIndices.Clear();
  102. _handLookup.Clear();
  103. _playerHandsComponent = null;
  104. foreach (var container in _handsContainers)
  105. {
  106. container.Clear();
  107. }
  108. }
  109. private void LoadPlayerHands(HandsComponent handsComp)
  110. {
  111. DebugTools.Assert(_playerHandsComponent == null);
  112. if (HandsGui != null)
  113. HandsGui.Visible = true;
  114. _playerHandsComponent = handsComp;
  115. foreach (var (name, hand) in handsComp.Hands)
  116. {
  117. var handButton = AddHand(name, hand.Location);
  118. if (_entities.TryGetComponent(hand.HeldEntity, out VirtualItemComponent? virt))
  119. {
  120. handButton.SetEntity(virt.BlockingEntity);
  121. handButton.Blocked = true;
  122. }
  123. else
  124. {
  125. handButton.SetEntity(hand.HeldEntity);
  126. handButton.Blocked = false;
  127. }
  128. }
  129. var activeHand = handsComp.ActiveHand;
  130. if (activeHand == null)
  131. return;
  132. SetActiveHand(activeHand.Name);
  133. }
  134. private void HandBlocked(string handName)
  135. {
  136. if (!_handLookup.TryGetValue(handName, out var hand))
  137. {
  138. return;
  139. }
  140. hand.Blocked = true;
  141. }
  142. private void HandUnblocked(string handName)
  143. {
  144. if (!_handLookup.TryGetValue(handName, out var hand))
  145. {
  146. return;
  147. }
  148. hand.Blocked = false;
  149. }
  150. private int GetHandContainerIndex(string containerName)
  151. {
  152. if (!_handContainerIndices.TryGetValue(containerName, out var result))
  153. return -1;
  154. return result;
  155. }
  156. private void OnItemAdded(string name, EntityUid entity)
  157. {
  158. var hand = GetHand(name);
  159. if (hand == null)
  160. return;
  161. if (_entities.TryGetComponent(entity, out VirtualItemComponent? virt))
  162. {
  163. hand.SetEntity(virt.BlockingEntity);
  164. hand.Blocked = true;
  165. }
  166. else
  167. {
  168. hand.SetEntity(entity);
  169. hand.Blocked = false;
  170. }
  171. UpdateHandStatus(hand, entity);
  172. }
  173. private void OnItemRemoved(string name, EntityUid entity)
  174. {
  175. var hand = GetHand(name);
  176. if (hand == null)
  177. return;
  178. hand.SetEntity(null);
  179. UpdateHandStatus(hand, null);
  180. }
  181. private HandsContainer GetFirstAvailableContainer()
  182. {
  183. if (_handsContainers.Count == 0)
  184. throw new Exception("Could not find an attached hand hud container");
  185. foreach (var container in _handsContainers)
  186. {
  187. if (container.IsFull)
  188. continue;
  189. return container;
  190. }
  191. throw new Exception("All attached hand hud containers were full!");
  192. }
  193. public bool TryGetHandContainer(string containerName, out HandsContainer? container)
  194. {
  195. container = null;
  196. var containerIndex = GetHandContainerIndex(containerName);
  197. if (containerIndex == -1)
  198. return false;
  199. container = _handsContainers[containerIndex];
  200. return true;
  201. }
  202. //propagate hand activation to the hand system.
  203. private void StorageActivate(GUIBoundKeyEventArgs args, SlotControl handControl)
  204. {
  205. _handsSystem.UIHandActivate(handControl.SlotName);
  206. }
  207. private void SetActiveHand(string? handName)
  208. {
  209. if (handName == null)
  210. {
  211. if (_activeHand != null)
  212. _activeHand.Highlight = false;
  213. return;
  214. }
  215. if (!_handLookup.TryGetValue(handName, out var handControl) || handControl == _activeHand)
  216. return;
  217. if (_activeHand != null)
  218. _activeHand.Highlight = false;
  219. handControl.Highlight = true;
  220. _activeHand = handControl;
  221. if (HandsGui != null &&
  222. _playerHandsComponent != null &&
  223. _player.LocalSession?.AttachedEntity is { } playerEntity &&
  224. _handsSystem.TryGetHand(playerEntity, handName, out var hand, _playerHandsComponent))
  225. {
  226. var foldedLocation = hand.Location.GetUILocation();
  227. if (foldedLocation == HandUILocation.Left)
  228. {
  229. _statusHandLeft = handControl;
  230. HandsGui.UpdatePanelEntityLeft(hand.HeldEntity);
  231. }
  232. else
  233. {
  234. // Middle or right
  235. _statusHandRight = handControl;
  236. HandsGui.UpdatePanelEntityRight(hand.HeldEntity);
  237. }
  238. HandsGui.SetHighlightHand(foldedLocation);
  239. }
  240. }
  241. private HandButton? GetHand(string handName)
  242. {
  243. _handLookup.TryGetValue(handName, out var handControl);
  244. return handControl;
  245. }
  246. private HandButton AddHand(string handName, HandLocation location)
  247. {
  248. var button = new HandButton(handName, location);
  249. button.StoragePressed += StorageActivate;
  250. button.Pressed += HandPressed;
  251. if (!_handLookup.TryAdd(handName, button))
  252. throw new Exception("Tried to add hand with duplicate name to UI. Name:" + handName);
  253. if (HandsGui != null)
  254. {
  255. HandsGui.HandContainer.AddButton(button);
  256. }
  257. else
  258. {
  259. GetFirstAvailableContainer().AddButton(button);
  260. }
  261. // If we don't have a status for this hand type yet, set it.
  262. // This means we have status filled by default in most scenarios,
  263. // otherwise the user'd need to switch hands to "activate" the hands the first time.
  264. if (location.GetUILocation() == HandUILocation.Left)
  265. _statusHandLeft ??= button;
  266. else
  267. _statusHandRight ??= button;
  268. UpdateVisibleStatusPanels();
  269. return button;
  270. }
  271. /// <summary>
  272. /// Reload all hands.
  273. /// </summary>
  274. public void ReloadHands()
  275. {
  276. UnloadPlayerHands();
  277. _handsSystem.ReloadHandButtons();
  278. }
  279. /// <summary>
  280. /// Swap hands from one container to the other.
  281. /// </summary>
  282. /// <param name="other"></param>
  283. /// <param name="source"></param>
  284. public void SwapHands(HandsContainer other, HandsContainer? source = null)
  285. {
  286. if (HandsGui == null && source == null)
  287. {
  288. throw new ArgumentException("Cannot swap hands if no source hand container exists!");
  289. }
  290. source ??= HandsGui!.HandContainer;
  291. var transfer = new List<Control>();
  292. foreach (var child in source.Children)
  293. {
  294. if (child is not HandButton)
  295. {
  296. continue;
  297. }
  298. transfer.Add(child);
  299. }
  300. foreach (var control in transfer)
  301. {
  302. source.RemoveChild(control);
  303. other.AddChild(control);
  304. }
  305. }
  306. private void RemoveHand(string handName)
  307. {
  308. RemoveHand(handName, out _);
  309. }
  310. private bool RemoveHand(string handName, out HandButton? handButton)
  311. {
  312. if (!_handLookup.TryGetValue(handName, out handButton))
  313. return false;
  314. if (handButton.Parent is HandsContainer handContainer)
  315. {
  316. handContainer.RemoveButton(handButton);
  317. }
  318. if (_statusHandLeft == handButton)
  319. _statusHandLeft = null;
  320. if (_statusHandRight == handButton)
  321. _statusHandRight = null;
  322. _handLookup.Remove(handName);
  323. handButton.Dispose();
  324. UpdateVisibleStatusPanels();
  325. return true;
  326. }
  327. private void UpdateVisibleStatusPanels()
  328. {
  329. var leftVisible = false;
  330. var rightVisible = false;
  331. foreach (var hand in _handLookup.Values)
  332. {
  333. if (hand.HandLocation.GetUILocation() == HandUILocation.Left)
  334. {
  335. leftVisible = true;
  336. }
  337. else
  338. {
  339. rightVisible = true;
  340. }
  341. }
  342. HandsGui?.UpdateStatusVisibility(leftVisible, rightVisible);
  343. }
  344. public string RegisterHandContainer(HandsContainer handContainer)
  345. {
  346. var name = "HandContainer_" + _backupSuffix;
  347. if (handContainer.Indexer == null)
  348. {
  349. handContainer.Indexer = name;
  350. _backupSuffix++;
  351. }
  352. else
  353. {
  354. name = handContainer.Indexer;
  355. }
  356. _handContainerIndices.Add(name, _handsContainers.Count);
  357. _handsContainers.Add(handContainer);
  358. return name;
  359. }
  360. public bool RemoveHandContainer(string handContainerName)
  361. {
  362. var index = GetHandContainerIndex(handContainerName);
  363. if (index == -1)
  364. return false;
  365. _handContainerIndices.Remove(handContainerName);
  366. _handsContainers.RemoveAt(index);
  367. return true;
  368. }
  369. public bool RemoveHandContainer(string handContainerName, out HandsContainer? container)
  370. {
  371. var success = _handContainerIndices.TryGetValue(handContainerName, out var index);
  372. container = _handsContainers[index];
  373. _handContainerIndices.Remove(handContainerName);
  374. _handsContainers.RemoveAt(index);
  375. return success;
  376. }
  377. public void OnStateEntered(GameplayState state)
  378. {
  379. if (HandsGui != null)
  380. HandsGui.Visible = _playerHandsComponent != null;
  381. }
  382. public override void FrameUpdate(FrameEventArgs args)
  383. {
  384. base.FrameUpdate(args);
  385. // TODO this should be event based but 2 systems modify the same component differently for some reason
  386. foreach (var container in _handsContainers)
  387. {
  388. foreach (var hand in container.GetButtons())
  389. {
  390. if (!_entities.TryGetComponent(hand.Entity, out UseDelayComponent? useDelay))
  391. {
  392. hand.CooldownDisplay.Visible = false;
  393. continue;
  394. }
  395. var delay = _useDelay.GetLastEndingDelay((hand.Entity.Value, useDelay));
  396. hand.CooldownDisplay.Visible = true;
  397. hand.CooldownDisplay.FromTime(delay.StartTime, delay.EndTime);
  398. }
  399. }
  400. }
  401. private void UpdateHandStatus(HandButton hand, EntityUid? entity)
  402. {
  403. if (hand == _statusHandLeft)
  404. HandsGui?.UpdatePanelEntityLeft(entity);
  405. if (hand == _statusHandRight)
  406. HandsGui?.UpdatePanelEntityRight(entity);
  407. }
  408. }