CatStation/Content.Client/VendingMachines/UI/VendingMachineMenu.xaml.cs
AndrewFenriz e934829cb3
VendingMachine refactor (systems. UI, components) (#44896)
* better

* VendingMachineComponent upd

* move components

* small fix

* move some fields to VendingMachineEjectComponent

* move some fields to VendingMachineVisualsComponent

* fix

* move some fields to VendingMachineDispenseOnHitComponent

* move NextEmpEject to VendingMachineEjectComponent

* upd

* split shared system to eject

* no more dummies for name

* fix contraband inventory

* small fix

* and again

* move VendingMachineDispenseOnHitComponent to server

* upd dispense

* upd

* move visuals to client

* fix

* move canshoot to server

* fix client

* fix

* upd

* fix merge

* fix

* upd1

* afterhandlestate

* upd

* more docs and protoid

* upd visuals

* upd components

* reorganize

* Entity<>

* revert AfterAutoHandleState
2026-08-01 15:44:49 +00:00

184 lines
6.1 KiB
C#

using System.Numerics;
using Content.Client.Stylesheets;
using Content.Shared.Labels.Components;
using Content.Shared.VendingMachines;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;
using Robust.Client.UserInterface;
using Content.Client.UserInterface.Controls;
using Robust.Shared.Utility;
namespace Content.Client.VendingMachines.UI;
[GenerateTypedNameReferences]
public sealed partial class VendingMachineMenu : FancyWindow
{
[Dependency] private IComponentFactory _componentFactory = default!;
[Dependency] private ILocalizationManager _loc = default!;
[Dependency] private IPrototypeManager _prototypeManager = default!;
private readonly Dictionary<VendorItemKey, (ListContainerButton Button, VendingMachineItem Item)> _listItems = new();
private readonly Dictionary<VendorItemKey, uint> _amounts = new();
/// <summary>
/// Whether the vending machine is able to be interacted with or not.
/// </summary>
private bool _enabled;
public event Action<GUIBoundKeyEventArgs, ListData>? OnItemSelected;
public VendingMachineMenu()
{
MinSize = SetSize = new Vector2(250, 150);
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
VendingContents.SearchBar = SearchBar;
VendingContents.DataFilterCondition += DataFilterCondition;
VendingContents.GenerateItem += GenerateButton;
VendingContents.ItemKeyBindDown += (args, data) => OnItemSelected?.Invoke(args, data);
}
private bool DataFilterCondition(string filter, ListData data)
{
if (data is not VendorItemsListData { ItemText: var text })
return false;
return string.IsNullOrEmpty(filter)
|| text.Contains(filter, StringComparison.CurrentCultureIgnoreCase);
}
private void GenerateButton(ListData data, ListContainerButton button)
{
if (data is not VendorItemsListData { ItemType: var type, ItemProtoID: var protoID, ItemText: var text })
return;
var item = new VendingMachineItem(protoID, text);
var key = new VendorItemKey(type, protoID);
_listItems[key] = (button, item);
button.AddChild(item);
button.AddStyleClass(StyleClass.ButtonSquare);
button.Disabled =
!_enabled ||
!_amounts.TryGetValue(key, out var amount) ||
amount == 0;
}
/// <summary>
/// Populates the list of available items on the vending machine interface
/// and sets icons based on their prototypes
/// </summary>
public void Populate(List<VendingMachineInventoryEntry> inventory, bool enabled)
{
_enabled = enabled;
_listItems.Clear();
_amounts.Clear();
if (inventory.Count == 0 && VendingContents.Visible)
{
SearchBar.Visible = false;
VendingContents.Visible = false;
var outOfStockLabel = new Label
{
Text = Loc.GetString("vending-machine-component-try-eject-out-of-stock"),
Margin = new Thickness(4, 4),
HorizontalExpand = true,
VerticalAlignment = VAlignment.Stretch,
HorizontalAlignment = HAlignment.Center
};
MainContainer.AddChild(outOfStockLabel);
SetSizeAfterUpdate(outOfStockLabel.Text.Length, 0);
return;
}
var longestEntry = string.Empty;
var listData = new List<VendorItemsListData>();
for (var i = 0; i < inventory.Count; i++)
{
var entry = inventory[i];
if (!_prototypeManager.Resolve(entry.ID, out var prototype))
{
_amounts[new VendorItemKey(entry.Type, entry.ID)] = 0;
continue;
}
var itemText = GetItemText(prototype, entry.Amount);
var key = new VendorItemKey(entry.Type, prototype.ID);
_amounts[key] = entry.Amount;
if (itemText.Length > longestEntry.Length)
longestEntry = itemText;
listData.Add(new VendorItemsListData(entry.Type, prototype.ID, i)
{
ItemText = itemText,
});
}
VendingContents.PopulateList(listData);
SetSizeAfterUpdate(longestEntry.Length, inventory.Count);
}
/// <summary>
/// Updates text entries for vending data in place without modifying the list controls.
/// </summary>
public void UpdateAmounts(List<VendingMachineInventoryEntry> cachedInventory, bool enabled)
{
_enabled = enabled;
foreach (var (key, button) in _listItems)
{
if (!cachedInventory.TryFirstOrDefault(o => o.Type == key.Type && o.ID == key.Prototype, out var entry))
continue;
if (!_prototypeManager.Resolve(key.Prototype, out var prototype))
continue;
var amount = entry.Amount;
_amounts[key] = amount;
var text = GetItemText(prototype, amount);
button.Item.SetText(text);
button.Button.Disabled = !enabled || amount == 0;
}
}
private string GetItemText(EntityPrototype prototype, uint amount)
{
var itemName = prototype.Name;
if (prototype.TryComp<LabelComponent>(out var label, _componentFactory) &&
label.LocalizedLabel is { } locId)
{
itemName = _loc.GetString("comp-label-format",
("baseName", itemName),
("label", _loc.GetString(locId)));
}
return $"[{amount}] {itemName}";
}
private void SetSizeAfterUpdate(int longestEntryLength, int contentCount)
{
SetSize = new Vector2(Math.Clamp((longestEntryLength + 2) * 12, 250, 400),
Math.Clamp(contentCount * 50, 150, 350));
}
}
public record VendorItemsListData(InventoryType ItemType, EntProtoId ItemProtoID, int ItemIndex) : ListData
{
public string ItemText = string.Empty;
}
public readonly record struct VendorItemKey(InventoryType Type, EntProtoId Prototype);