fix searching on vending machines (#21233)

This commit is contained in:
Nemanja
2023-10-25 09:01:16 -04:00
committed by GitHub
parent 88a29450d9
commit 0253270a9a
3 changed files with 12 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
<DefaultWindow xmlns="https://spacestation14.io"> <DefaultWindow xmlns="https://spacestation14.io">
<BoxContainer Orientation="Vertical"> <BoxContainer Orientation="Vertical">
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True" Margin ="0 4"/> <LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True" Margin ="0 4" Access="Public"/>
<ItemList Name="VendingContents" <ItemList Name="VendingContents"
SizeFlagsStretchRatio="8" SizeFlagsStretchRatio="8"
VerticalExpand="True"> VerticalExpand="True">

View File

@@ -6,7 +6,6 @@ using Robust.Client.Graphics;
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.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Client.VendingMachines.UI namespace Content.Client.VendingMachines.UI
@@ -32,7 +31,6 @@ namespace Content.Client.VendingMachines.UI
VendingContents.OnItemSelected += args => VendingContents.OnItemSelected += args =>
{ {
SearchBar.Text = string.Empty;
OnItemSelected?.Invoke(args); OnItemSelected?.Invoke(args);
}; };
} }
@@ -41,8 +39,10 @@ namespace Content.Client.VendingMachines.UI
/// Populates the list of available items on the vending machine interface /// Populates the list of available items on the vending machine interface
/// and sets icons based on their prototypes /// and sets icons based on their prototypes
/// </summary> /// </summary>
public void Populate(List<VendingMachineInventoryEntry> inventory, string? filter = null) public void Populate(List<VendingMachineInventoryEntry> inventory, out List<int> filteredInventory, string? filter = null)
{ {
filteredInventory = new();
if (inventory.Count == 0) if (inventory.Count == 0)
{ {
VendingContents.Clear(); VendingContents.Clear();
@@ -93,6 +93,7 @@ namespace Content.Client.VendingMachines.UI
vendingItem.Text = $"{itemName} [{entry.Amount}]"; vendingItem.Text = $"{itemName} [{entry.Amount}]";
vendingItem.Icon = icon; vendingItem.Icon = icon;
filteredInventory.Add(i);
} }
SetSizeAfterUpdate(longestEntry.Length, inventory.Count); SetSizeAfterUpdate(longestEntry.Length, inventory.Count);

View File

@@ -1,6 +1,5 @@
using Content.Client.VendingMachines.UI; using Content.Client.VendingMachines.UI;
using Content.Shared.VendingMachines; using Content.Shared.VendingMachines;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using System.Linq; using System.Linq;
@@ -14,6 +13,9 @@ namespace Content.Client.VendingMachines
[ViewVariables] [ViewVariables]
private List<VendingMachineInventoryEntry> _cachedInventory = new(); private List<VendingMachineInventoryEntry> _cachedInventory = new();
[ViewVariables]
private List<int> _cachedFilteredIndex = new();
public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey) public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{ {
} }
@@ -32,7 +34,7 @@ namespace Content.Client.VendingMachines
_menu.OnItemSelected += OnItemSelected; _menu.OnItemSelected += OnItemSelected;
_menu.OnSearchChanged += OnSearchChanged; _menu.OnSearchChanged += OnSearchChanged;
_menu.Populate(_cachedInventory); _menu.Populate(_cachedInventory, out _cachedFilteredIndex);
_menu.OpenCentered(); _menu.OpenCentered();
} }
@@ -46,7 +48,7 @@ namespace Content.Client.VendingMachines
_cachedInventory = newState.Inventory; _cachedInventory = newState.Inventory;
_menu?.Populate(_cachedInventory); _menu?.Populate(_cachedInventory, out _cachedFilteredIndex, _menu.SearchBar.Text);
} }
private void OnItemSelected(ItemList.ItemListSelectedEventArgs args) private void OnItemSelected(ItemList.ItemListSelectedEventArgs args)
@@ -54,7 +56,7 @@ namespace Content.Client.VendingMachines
if (_cachedInventory.Count == 0) if (_cachedInventory.Count == 0)
return; return;
var selectedItem = _cachedInventory.ElementAtOrDefault(args.ItemIndex); var selectedItem = _cachedInventory.ElementAtOrDefault(_cachedFilteredIndex.ElementAtOrDefault(args.ItemIndex));
if (selectedItem == null) if (selectedItem == null)
return; return;
@@ -78,7 +80,7 @@ namespace Content.Client.VendingMachines
private void OnSearchChanged(string? filter) private void OnSearchChanged(string? filter)
{ {
_menu?.Populate(_cachedInventory, filter); _menu?.Populate(_cachedInventory, out _cachedFilteredIndex, filter);
} }
} }
} }