Files
crystall-punk-14/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs

87 lines
2.5 KiB
C#
Raw Normal View History

using Content.Client.VendingMachines.UI;
2021-06-09 22:19:39 +02:00
using Content.Shared.VendingMachines;
using Robust.Client.UserInterface.Controls;
using System.Linq;
2021-06-09 22:19:39 +02:00
namespace Content.Client.VendingMachines
{
public sealed class VendingMachineBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private VendingMachineMenu? _menu;
2023-07-08 09:02:17 -07:00
[ViewVariables]
private List<VendingMachineInventoryEntry> _cachedInventory = new();
[ViewVariables]
private List<int> _cachedFilteredIndex = new();
2023-07-08 09:02:17 -07:00
public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
2023-07-08 09:02:17 -07:00
var vendingMachineSys = EntMan.System<VendingMachineSystem>();
2023-07-08 09:02:17 -07:00
_cachedInventory = vendingMachineSys.GetAllInventory(Owner);
2023-07-08 09:02:17 -07:00
_menu = new VendingMachineMenu { Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName };
_menu.OnClose += Close;
_menu.OnItemSelected += OnItemSelected;
_menu.OnSearchChanged += OnSearchChanged;
_menu.Populate(_cachedInventory, out _cachedFilteredIndex);
_menu.OpenCenteredLeft();
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not VendingMachineInterfaceState newState)
return;
_cachedInventory = newState.Inventory;
_menu?.Populate(_cachedInventory, out _cachedFilteredIndex, _menu.SearchBar.Text);
}
private void OnItemSelected(ItemList.ItemListSelectedEventArgs args)
{
2022-10-04 14:24:19 +11:00
if (_cachedInventory.Count == 0)
return;
var selectedItem = _cachedInventory.ElementAtOrDefault(_cachedFilteredIndex.ElementAtOrDefault(args.ItemIndex));
if (selectedItem == null)
return;
SendMessage(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();
}
private void OnSearchChanged(string? filter)
{
_menu?.Populate(_cachedInventory, out _cachedFilteredIndex, filter);
}
}
}