| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using Content.Client.UserInterface.Controls;
- using Content.Client.VendingMachines.UI;
- using Content.Shared.VendingMachines;
- using Robust.Client.UserInterface;
- using Robust.Shared.Input;
- using System.Linq;
- namespace Content.Client.VendingMachines
- {
- public sealed class VendingMachineBoundUserInterface : BoundUserInterface
- {
- [ViewVariables]
- private VendingMachineMenu? _menu;
- [ViewVariables]
- private List<VendingMachineInventoryEntry> _cachedInventory = new();
- public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
- {
- }
- protected override void Open()
- {
- base.Open();
- _menu = this.CreateWindowCenteredLeft<VendingMachineMenu>();
- _menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
- _menu.OnItemSelected += OnItemSelected;
- Refresh();
- }
- public void Refresh()
- {
- var enabled = EntMan.TryGetComponent(Owner, out VendingMachineComponent? bendy) && !bendy.Ejecting;
- var system = EntMan.System<VendingMachineSystem>();
- _cachedInventory = system.GetAllInventory(Owner);
- _menu?.Populate(_cachedInventory, enabled);
- }
- public void UpdateAmounts()
- {
- var enabled = EntMan.TryGetComponent(Owner, out VendingMachineComponent? bendy) && !bendy.Ejecting;
- var system = EntMan.System<VendingMachineSystem>();
- _cachedInventory = system.GetAllInventory(Owner);
- _menu?.UpdateAmounts(_cachedInventory, enabled);
- }
- private void OnItemSelected(GUIBoundKeyEventArgs args, ListData data)
- {
- if (args.Function != EngineKeyFunctions.UIClick)
- return;
- if (data is not VendorItemsListData { ItemIndex: var itemIndex })
- return;
- if (_cachedInventory.Count == 0)
- return;
- var selectedItem = _cachedInventory.ElementAtOrDefault(itemIndex);
- if (selectedItem == null)
- return;
- SendPredictedMessage(new VendingMachineEjectMessage(selectedItem.Type, selectedItem.ID));
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (!disposing)
- return;
- if (_menu == null)
- return;
- _menu.OnItemSelected -= OnItemSelected;
- _menu.OnClose -= Close;
- _menu.Dispose();
- }
- }
- }
|