]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
fix searching on vending machines (#21233)
authorNemanja <98561806+EmoGarbage404@users.noreply.github.com>
Wed, 25 Oct 2023 13:01:16 +0000 (09:01 -0400)
committerGitHub <noreply@github.com>
Wed, 25 Oct 2023 13:01:16 +0000 (00:01 +1100)
Content.Client/VendingMachines/UI/VendingMachineMenu.xaml
Content.Client/VendingMachines/UI/VendingMachineMenu.xaml.cs
Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs

index dcd6ed91a7eedbbc55d9dd52de6180ad7fc57d9f..abc7252e40946fa40d10fb56aa101d182f570917 100644 (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">
index b436a9d2342df2f2a2a65049112ba9932318c605..a2741cc17abfa8360a1fb000988bc90454b8e4d5 100644 (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);
index ab310144d5396a18d7af9df698012dbb4d2fa44f..6f28ddb31f030cea0d0080766f7126a6bec27658 100644 (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);
         }
     }
 }