1
0

HotbarUIController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Content.Client.UserInterface.Systems.Gameplay;
  2. using Content.Client.UserInterface.Systems.Hands;
  3. using Content.Client.UserInterface.Systems.Hands.Controls;
  4. using Content.Client.UserInterface.Systems.Hotbar.Widgets;
  5. using Content.Client.UserInterface.Systems.Inventory;
  6. using Content.Client.UserInterface.Systems.Inventory.Controls;
  7. using Content.Client.UserInterface.Systems.Inventory.Widgets;
  8. using Content.Client.UserInterface.Systems.Storage;
  9. using Content.Client.UserInterface.Systems.Storage.Controls;
  10. using Robust.Client.UserInterface;
  11. using Robust.Client.UserInterface.Controllers;
  12. namespace Content.Client.UserInterface.Systems.Hotbar;
  13. public sealed class HotbarUIController : UIController
  14. {
  15. private InventoryUIController? _inventory;
  16. private HandsUIController? _hands;
  17. private StorageUIController? _storage;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. var gameplayStateLoad = UIManager.GetUIController<GameplayStateLoadController>();
  22. gameplayStateLoad.OnScreenLoad += OnScreenLoad;
  23. }
  24. private void OnScreenLoad()
  25. {
  26. ReloadHotbar();
  27. }
  28. public void Setup(HandsContainer handsContainer)
  29. {
  30. _inventory = UIManager.GetUIController<InventoryUIController>();
  31. _hands = UIManager.GetUIController<HandsUIController>();
  32. _storage = UIManager.GetUIController<StorageUIController>();
  33. _hands.RegisterHandContainer(handsContainer);
  34. }
  35. public void ReloadHotbar()
  36. {
  37. if (UIManager.ActiveScreen == null)
  38. {
  39. return;
  40. }
  41. if (UIManager.ActiveScreen.GetWidget<HotbarGui>() is { } hotbar)
  42. {
  43. foreach (var container in GetAllItemSlotContainers(hotbar))
  44. {
  45. // Yes, this is dirty.
  46. container.SlotGroup = container.SlotGroup;
  47. }
  48. }
  49. _hands?.ReloadHands();
  50. _inventory?.ReloadSlots();
  51. //todo move this over to its own hellhole
  52. var inventory = UIManager.ActiveScreen.GetWidget<InventoryGui>();
  53. if (inventory == null)
  54. {
  55. return;
  56. }
  57. foreach (var container in GetAllItemSlotContainers(inventory))
  58. {
  59. // Yes, this is dirty.
  60. container.SlotGroup = container.SlotGroup;
  61. }
  62. _inventory?.RegisterInventoryBarContainer(inventory.InventoryHotbar);
  63. }
  64. private static IEnumerable<ItemSlotButtonContainer> GetAllItemSlotContainers(Control gui)
  65. {
  66. var result = new List<ItemSlotButtonContainer>();
  67. foreach (var child in gui.Children)
  68. {
  69. if (child is ItemSlotButtonContainer container)
  70. {
  71. result.Add(container);
  72. }
  73. result.AddRange(GetAllItemSlotContainers(child));
  74. }
  75. return result;
  76. }
  77. }