Refactors Vending Menu to XAML-UI (#4614)

* Initial

* Some web-edit reviews

* Keep VV

* Applied Reviews
This commit is contained in:
Swept
2021-09-19 10:21:05 -07:00
committed by GitHub
parent b84930cc31
commit 8e802b4305
3 changed files with 20 additions and 22 deletions

View File

@@ -0,0 +1,6 @@
<SS14Window xmlns="https://spacestation14.io">
<ItemList Name="VendingContents"
SizeFlagsStretchRatio="8"
VerticalExpand="True">
</ItemList>
</SS14Window>

View File

@@ -1,65 +1,57 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.ResourceManagement; using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using static Content.Shared.VendingMachines.SharedVendingMachineComponent; using static Content.Shared.VendingMachines.SharedVendingMachineComponent;
namespace Content.Client.VendingMachines.UI namespace Content.Client.VendingMachines.UI
{ {
class VendingMachineMenu : SS14Window [GenerateTypedNameReferences]
public partial class VendingMachineMenu : SS14Window
{ {
[Dependency] private readonly IResourceCache _resourceCache = default!; [Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly ItemList _items; private VendingMachineBoundUserInterface Owner { get; }
private List<VendingMachineInventoryEntry> _cachedInventory = new();
public VendingMachineBoundUserInterface Owner { get; } private List<VendingMachineInventoryEntry> _cachedInventory = new();
public VendingMachineMenu(VendingMachineBoundUserInterface owner) public VendingMachineMenu(VendingMachineBoundUserInterface owner)
{ {
SetSize = (300, 450);
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
Owner = owner; Owner = owner;
VendingContents.OnItemSelected += ItemSelected;
_items = new ItemList()
{
SizeFlagsStretchRatio = 8,
VerticalExpand = true,
};
_items.OnItemSelected += ItemSelected;
Contents.AddChild(_items);
} }
public void Populate(List<VendingMachineInventoryEntry> inventory) public void Populate(List<VendingMachineInventoryEntry> inventory)
{ {
_items.Clear(); VendingContents.Clear();
_cachedInventory = inventory; _cachedInventory = inventory;
var longestEntry = ""; var longestEntry = "";
foreach (VendingMachineInventoryEntry entry in inventory) foreach (VendingMachineInventoryEntry entry in inventory)
{ {
var itemName = _prototypeManager.Index<EntityPrototype>(entry.ID).Name; var itemName = _prototypeManager.Index<EntityPrototype>(entry.ID).Name;
if (itemName.Length > longestEntry.Length) if (itemName.Length > longestEntry.Length)
{
longestEntry = itemName; longestEntry = itemName;
}
Texture? icon = null; Texture? icon = null;
if(_prototypeManager.TryIndex(entry.ID, out EntityPrototype? prototype)) if(_prototypeManager.TryIndex(entry.ID, out EntityPrototype? prototype))
{
icon = SpriteComponent.GetPrototypeIcon(prototype, _resourceCache).Default; icon = SpriteComponent.GetPrototypeIcon(prototype, _resourceCache).Default;
}
_items.AddItem($"{itemName} ({entry.Amount} left)", icon); VendingContents.AddItem($"{itemName} [{entry.Amount}]", icon);
} }
SetSize = ((longestEntry.Length + 8) * 12, _items.Count * 40 + 50); SetSize = (Math.Clamp((longestEntry.Length + 2) * 12, 250, 300),
Math.Clamp(VendingContents.Count * 30, 150, 350));
} }
public void ItemSelected(ItemList.ItemListSelectedEventArgs args) public void ItemSelected(ItemList.ItemListSelectedEventArgs args)

View File

@@ -7,7 +7,7 @@ using static Content.Shared.VendingMachines.SharedVendingMachineComponent;
namespace Content.Client.VendingMachines namespace Content.Client.VendingMachines
{ {
class VendingMachineBoundUserInterface : BoundUserInterface public class VendingMachineBoundUserInterface : BoundUserInterface
{ {
[ViewVariables] private VendingMachineMenu? _menu; [ViewVariables] private VendingMachineMenu? _menu;