Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make vending machine use EntityPrototypeView #30064

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Content.Client/VendingMachines/UI/VendingMachineItem.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<PanelContainer xmlns="https://spacestation14.io"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
Name="BackgroundColorPanel">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat
BackgroundColor="#464966"
/>
</PanelContainer.PanelOverride>
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"
SeparationOverride="4">
<EntityPrototypeView
Name="ItemPrototype"
Margin="4 4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MinSize="32 32"
/>
<Label Name="NameLabel"
SizeFlagsStretchRatio="3"
HorizontalExpand="True"
ClipText="True"/>
</BoxContainer>
</PanelContainer>
19 changes: 19 additions & 0 deletions Content.Client/VendingMachines/UI/VendingMachineItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;

namespace Content.Client.VendingMachines.UI;

[GenerateTypedNameReferences]
public sealed partial class VendingMachineItem : PanelContainer
{
public VendingMachineItem(EntityUid entity, string name)
{
RobustXamlLoader.Load(this);

ItemPrototype.SetEntity(entity);

NameLabel.Text = name;
}
}
15 changes: 4 additions & 11 deletions Content.Client/VendingMachines/UI/VendingMachineMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:style="clr-namespace:Content.Client.Stylesheets">
<BoxContainer Orientation="Vertical">
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True" Margin ="4 4" Access="Public"/>
<ItemList Name="VendingContents"
SizeFlagsStretchRatio="8"
VerticalExpand="True"
ItemSeparation="2"
Margin="4 0"
SelectMode="Button"
StyleClasses="transparentBackgroundItemList">
</ItemList>
xmlns:co="clr-namespace:Content.Client.UserInterface.Controls">
<BoxContainer Name="MainContainer" Orientation="Vertical">
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True" Margin ="4 4"/>
<co:SearchListContainer Name="VendingContents" VerticalExpand="True" Margin="4 0"/>
<!-- Footer -->
<BoxContainer Orientation="Vertical">
<PanelContainer StyleClasses="LowDivider" />
Expand Down
102 changes: 52 additions & 50 deletions Content.Client/VendingMachines/UI/VendingMachineMenu.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,52 @@
using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;
using Content.Shared.IdentityManagement;
using Robust.Shared.Timing;
using Robust.Client.UserInterface;
using Content.Client.UserInterface.Controls;

namespace Content.Client.VendingMachines.UI
{
[GenerateTypedNameReferences]
public sealed partial class VendingMachineMenu : FancyWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private readonly Dictionary<EntProtoId, EntityUid> _dummies = [];

public event Action<ItemList.ItemListSelectedEventArgs>? OnItemSelected;
public event Action<string>? OnSearchChanged;
public event Action<GUIBoundKeyEventArgs, ListData>? OnItemSelected;

public VendingMachineMenu()
{
MinSize = SetSize = new Vector2(250, 150);
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

SearchBar.OnTextChanged += _ =>
{
OnSearchChanged?.Invoke(SearchBar.Text);
};
VendingContents.SearchBar = SearchBar;
VendingContents.DataFilterCondition += DataFilterCondition;
VendingContents.GenerateItem += GenerateButton;
VendingContents.ItemKeyBindDown += (args, data) => OnItemSelected?.Invoke(args, data);
}

VendingContents.OnItemSelected += args =>
{
OnItemSelected?.Invoke(args);
};
private bool DataFilterCondition(string filter, ListData data)
{
if (data is not VendorItemsListData { ItemName: var itemName })
return false;

if (string.IsNullOrEmpty(filter))
return true;

return itemName.Contains(filter, StringComparison.CurrentCultureIgnoreCase);
}

private void GenerateButton(ListData data, ListContainerButton button)
{
if (data is not VendorItemsListData { Dummy: var dummy, ItemName: var itemName })
return;

var entry = new VendingMachineItem(dummy, itemName);
button.ToolTip = itemName;

button.AddChild(entry);
}

protected override void Dispose(bool disposing)
Expand All @@ -61,37 +77,35 @@ protected override void Dispose(bool disposing)
/// 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, out List<int> filteredInventory, string? filter = null)
public void Populate(List<VendingMachineInventoryEntry> inventory)
{
filteredInventory = new();

if (inventory.Count == 0)
if (inventory.Count == 0 && VendingContents.Visible)
{
VendingContents.Clear();
var outOfStockText = Loc.GetString("vending-machine-component-try-eject-out-of-stock");
VendingContents.AddItem(outOfStockText);
SetSizeAfterUpdate(outOfStockText.Length, VendingContents.Count);
return;
}
SearchBar.Visible = false;
VendingContents.Visible = false;

while (inventory.Count != VendingContents.Count)
{
if (inventory.Count > VendingContents.Count)
VendingContents.AddItem(string.Empty);
else
VendingContents.RemoveAt(VendingContents.Count - 1);
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 spriteSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SpriteSystem>();
var listData = new List<VendorItemsListData>();

var filterCount = 0;
for (var i = 0; i < inventory.Count; i++)
{
var entry = inventory[i];
var vendingItem = VendingContents[i - filterCount];
vendingItem.Text = string.Empty;
vendingItem.Icon = null;

if (!_dummies.TryGetValue(entry.ID, out var dummy))
{
Expand All @@ -100,29 +114,15 @@ public void Populate(List<VendingMachineInventoryEntry> inventory, out List<int>
}

var itemName = Identity.Name(dummy, _entityManager);
Texture? icon = null;
if (_prototypeManager.TryIndex<EntityPrototype>(entry.ID, out var prototype))
{
icon = spriteSystem.GetPrototypeIcon(prototype).Default;
}

// search filter
if (!string.IsNullOrEmpty(filter) &&
!itemName.ToLowerInvariant().Contains(filter.Trim().ToLowerInvariant()))
{
VendingContents.Remove(vendingItem);
filterCount++;
continue;
}

if (itemName.Length > longestEntry.Length)
longestEntry = itemName;

vendingItem.Text = $"{itemName} [{entry.Amount}]";
vendingItem.Icon = icon;
filteredInventory.Add(i);
listData.Add(new VendorItemsListData(dummy, $"{itemName} [{entry.Amount}]", i));
}

VendingContents.PopulateList(listData);

SetSizeAfterUpdate(longestEntry.Length, inventory.Count);
}

Expand All @@ -133,3 +133,5 @@ private void SetSizeAfterUpdate(int longestEntryLength, int contentCount)
}
}
}

public record VendorItemsListData(EntityUid Dummy, string ItemName, int ItemIndex) : ListData;
26 changes: 13 additions & 13 deletions Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Content.Client.UserInterface.Controls;
using Content.Client.VendingMachines.UI;
using Content.Shared.VendingMachines;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using System.Linq;

namespace Content.Client.VendingMachines
Expand All @@ -13,9 +16,6 @@ public sealed class VendingMachineBoundUserInterface : BoundUserInterface
[ViewVariables]
private List<VendingMachineInventoryEntry> _cachedInventory = new();

[ViewVariables]
private List<int> _cachedFilteredIndex = new();

public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
Expand All @@ -32,9 +32,8 @@ protected override void Open()

_menu.OnClose += Close;
_menu.OnItemSelected += OnItemSelected;
_menu.OnSearchChanged += OnSearchChanged;

_menu.Populate(_cachedInventory, out _cachedFilteredIndex);
_menu.Populate(_cachedInventory);

_menu.OpenCenteredLeft();
}
Expand All @@ -48,15 +47,21 @@ protected override void UpdateState(BoundUserInterfaceState state)

_cachedInventory = newState.Inventory;

_menu?.Populate(_cachedInventory, out _cachedFilteredIndex, _menu.SearchBar.Text);
_menu?.Populate(_cachedInventory);
}

private void OnItemSelected(ItemList.ItemListSelectedEventArgs args)
private void OnItemSelected(GUIBoundKeyEventArgs args, ListData data)
{
if (args.Function != EngineKeyFunctions.UIClick)
return;

if (data is not VendorItemsListData { ItemIndex: var itemIndex })
return;

if (_cachedInventory.Count == 0)
return;

var selectedItem = _cachedInventory.ElementAtOrDefault(_cachedFilteredIndex.ElementAtOrDefault(args.ItemIndex));
var selectedItem = _cachedInventory.ElementAtOrDefault(itemIndex);

if (selectedItem == null)
return;
Expand All @@ -77,10 +82,5 @@ protected override void Dispose(bool disposing)
_menu.OnClose -= Close;
_menu.Dispose();
}

private void OnSearchChanged(string? filter)
{
_menu?.Populate(_cachedInventory, out _cachedFilteredIndex, filter);
}
}
}
Loading