CatStation/Content.Client/VendingMachines/UI/VendingMachineMenu.xaml.cs
Morb0 e21ee44f9e Merge remote-tracking branch 'upstream/master' into upstream-sync
# Conflicts:
#	.github/CODEOWNERS
#	Content.IntegrationTests/Tests/Preferences/ServerDbSqliteTests.cs
#	Content.Server/VoiceMask/VoiceMaskSystem.cs
#	Resources/Prototypes/Datasets/verbs.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml
#	Resources/Prototypes/Entities/Mobs/Player/admin_ghost.yml
#	Resources/Prototypes/Maps/aspid.yml
#	Resources/Prototypes/Maps/barratry.yml
#	Resources/Prototypes/Maps/cluster.yml
#	Resources/Prototypes/Maps/kettle.yml
#	Resources/Prototypes/Maps/origin.yml
#	Resources/ServerInfo/Guidebook/Cargo/Salvage.xml
#	Resources/Textures/Structures/Furniture/chairs.rsi/meta.json
2023-09-13 16:51:52 +03:00

91 lines
3.2 KiB
C#

using System.Numerics;
using Content.Shared.VendingMachines;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
using Robust.Shared.Prototypes;
namespace Content.Client.VendingMachines.UI
{
[GenerateTypedNameReferences]
public sealed partial class VendingMachineMenu : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public event Action<ItemList.ItemListSelectedEventArgs>? OnItemSelected;
public VendingMachineMenu()
{
MinSize = new Vector2(250, 150); // Corvax-Resize
SetSize = new Vector2(450, 150); // Corvax-Resize
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
VendingContents.OnItemSelected += args =>
{
OnItemSelected?.Invoke(args);
};
}
/// <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)
{
if (inventory.Count == 0)
{
VendingContents.Clear();
var outOfStockText = Loc.GetString("vending-machine-component-try-eject-out-of-stock");
VendingContents.AddItem(outOfStockText);
SetSizeAfterUpdate(outOfStockText.Length);
return;
}
while (inventory.Count != VendingContents.Count)
{
if (inventory.Count > VendingContents.Count)
VendingContents.AddItem(string.Empty);
else
VendingContents.RemoveAt(VendingContents.Count - 1);
}
var longestEntry = string.Empty;
var spriteSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SpriteSystem>();
for (var i = 0; i < inventory.Count; i++)
{
var entry = inventory[i];
var vendingItem = VendingContents[i];
vendingItem.Text = string.Empty;
vendingItem.Icon = null;
var itemName = entry.ID;
Texture? icon = null;
if (_prototypeManager.TryIndex<EntityPrototype>(entry.ID, out var prototype))
{
itemName = prototype.Name;
icon = spriteSystem.GetPrototypeIcon(prototype).Default;
}
if (itemName.Length > longestEntry.Length)
longestEntry = itemName;
vendingItem.Text = $"{itemName} [{entry.Amount}]";
vendingItem.Icon = icon;
}
SetSizeAfterUpdate(longestEntry.Length);
}
private void SetSizeAfterUpdate(int longestEntryLength)
{
SetSize = new Vector2(Math.Clamp((longestEntryLength + 2) * 12, 250, 300),
Math.Clamp(VendingContents.Count * 50, 150, 350));
}
}
}