VendingMachineBoundUserInterface.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Content.Client.UserInterface.Controls;
  2. using Content.Client.VendingMachines.UI;
  3. using Content.Shared.VendingMachines;
  4. using Robust.Client.UserInterface;
  5. using Robust.Shared.Input;
  6. using System.Linq;
  7. namespace Content.Client.VendingMachines
  8. {
  9. public sealed class VendingMachineBoundUserInterface : BoundUserInterface
  10. {
  11. [ViewVariables]
  12. private VendingMachineMenu? _menu;
  13. [ViewVariables]
  14. private List<VendingMachineInventoryEntry> _cachedInventory = new();
  15. public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  16. {
  17. }
  18. protected override void Open()
  19. {
  20. base.Open();
  21. _menu = this.CreateWindowCenteredLeft<VendingMachineMenu>();
  22. _menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
  23. _menu.OnItemSelected += OnItemSelected;
  24. Refresh();
  25. }
  26. public void Refresh()
  27. {
  28. var enabled = EntMan.TryGetComponent(Owner, out VendingMachineComponent? bendy) && !bendy.Ejecting;
  29. var system = EntMan.System<VendingMachineSystem>();
  30. _cachedInventory = system.GetAllInventory(Owner);
  31. _menu?.Populate(_cachedInventory, enabled);
  32. }
  33. public void UpdateAmounts()
  34. {
  35. var enabled = EntMan.TryGetComponent(Owner, out VendingMachineComponent? bendy) && !bendy.Ejecting;
  36. var system = EntMan.System<VendingMachineSystem>();
  37. _cachedInventory = system.GetAllInventory(Owner);
  38. _menu?.UpdateAmounts(_cachedInventory, enabled);
  39. }
  40. private void OnItemSelected(GUIBoundKeyEventArgs args, ListData data)
  41. {
  42. if (args.Function != EngineKeyFunctions.UIClick)
  43. return;
  44. if (data is not VendorItemsListData { ItemIndex: var itemIndex })
  45. return;
  46. if (_cachedInventory.Count == 0)
  47. return;
  48. var selectedItem = _cachedInventory.ElementAtOrDefault(itemIndex);
  49. if (selectedItem == null)
  50. return;
  51. SendPredictedMessage(new VendingMachineEjectMessage(selectedItem.Type, selectedItem.ID));
  52. }
  53. protected override void Dispose(bool disposing)
  54. {
  55. base.Dispose(disposing);
  56. if (!disposing)
  57. return;
  58. if (_menu == null)
  59. return;
  60. _menu.OnItemSelected -= OnItemSelected;
  61. _menu.OnClose -= Close;
  62. _menu.Dispose();
  63. }
  64. }
  65. }