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">
<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"
SizeFlagsStretchRatio="8"
VerticalExpand="True">

View File

@@ -6,7 +6,6 @@ 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
@@ -32,7 +31,6 @@ namespace Content.Client.VendingMachines.UI
VendingContents.OnItemSelected += args =>
{
SearchBar.Text = string.Empty;
OnItemSelected?.Invoke(args);
};
}
@@ -41,8 +39,10 @@ namespace Content.Client.VendingMachines.UI
/// 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, string? filter = null)
public void Populate(List<VendingMachineInventoryEntry> inventory, out List<int> filteredInventory, string? filter = null)
{
filteredInventory = new();
if (inventory.Count == 0)
{
VendingContents.Clear();
@@ -93,6 +93,7 @@ namespace Content.Client.VendingMachines.UI
vendingItem.Text = $"{itemName} [{entry.Amount}]";
vendingItem.Icon = icon;
filteredInventory.Add(i);
}
SetSizeAfterUpdate(longestEntry.Length, inventory.Count);

View File

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