2024-08-22 17:40:39 +03:00
|
|
|
using Content.Client.UserInterface.Controls;
|
2022-01-09 20:10:36 -08:00
|
|
|
using Content.Client.VendingMachines.UI;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.VendingMachines;
|
2024-08-22 17:40:39 +03:00
|
|
|
using Robust.Client.UserInterface;
|
|
|
|
|
using Robust.Shared.Input;
|
2022-08-31 14:12:09 +02:00
|
|
|
using System.Linq;
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.VendingMachines
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class VendingMachineBoundUserInterface : BoundUserInterface
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2022-08-31 14:12:09 +02:00
|
|
|
[ViewVariables]
|
|
|
|
|
private VendingMachineMenu? _menu;
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2023-07-08 09:02:17 -07:00
|
|
|
[ViewVariables]
|
2022-08-31 14:12:09 +02:00
|
|
|
private List<VendingMachineInventoryEntry> _cachedInventory = new();
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2023-07-08 09:02:17 -07:00
|
|
|
public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Open()
|
|
|
|
|
{
|
|
|
|
|
base.Open();
|
|
|
|
|
|
2024-07-21 14:48:13 +10:00
|
|
|
_menu = this.CreateWindow<VendingMachineMenu>();
|
|
|
|
|
_menu.OpenCenteredLeft();
|
|
|
|
|
_menu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
|
2022-08-31 14:12:09 +02:00
|
|
|
_menu.OnItemSelected += OnItemSelected;
|
2024-09-23 12:10:22 +10:00
|
|
|
Refresh();
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-23 12:10:22 +10:00
|
|
|
public void Refresh()
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2024-09-23 12:10:22 +10:00
|
|
|
var system = EntMan.System<VendingMachineSystem>();
|
|
|
|
|
_cachedInventory = system.GetAllInventory(Owner);
|
2022-08-31 14:12:09 +02:00
|
|
|
|
2024-08-22 17:40:39 +03:00
|
|
|
_menu?.Populate(_cachedInventory);
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:40:39 +03:00
|
|
|
private void OnItemSelected(GUIBoundKeyEventArgs args, ListData data)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2024-08-22 17:40:39 +03:00
|
|
|
if (args.Function != EngineKeyFunctions.UIClick)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (data is not VendorItemsListData { ItemIndex: var itemIndex })
|
|
|
|
|
return;
|
|
|
|
|
|
2022-10-04 14:24:19 +11:00
|
|
|
if (_cachedInventory.Count == 0)
|
2022-08-31 14:12:09 +02:00
|
|
|
return;
|
|
|
|
|
|
2024-08-22 17:40:39 +03:00
|
|
|
var selectedItem = _cachedInventory.ElementAtOrDefault(itemIndex);
|
2022-08-31 14:12:09 +02:00
|
|
|
|
|
|
|
|
if (selectedItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SendMessage(new VendingMachineEjectMessage(selectedItem.Type, selectedItem.ID));
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
2020-10-06 10:16:42 +02:00
|
|
|
if (!disposing)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-08-31 14:12:09 +02:00
|
|
|
if (_menu == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_menu.OnItemSelected -= OnItemSelected;
|
|
|
|
|
_menu.OnClose -= Close;
|
|
|
|
|
_menu.Dispose();
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|