]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Adds a search bar to vending machines (#20326)
authorike709 <ike709@users.noreply.github.com>
Wed, 20 Sep 2023 19:40:41 +0000 (12:40 -0700)
committerGitHub <noreply@github.com>
Wed, 20 Sep 2023 19:40:41 +0000 (15:40 -0400)
* Adds a search bar to vending machines

* fix indentation

* whitespace again

---------

Co-authored-by: ike709 <ike709@github.com>
Content.Client/VendingMachines/UI/VendingMachineMenu.xaml
Content.Client/VendingMachines/UI/VendingMachineMenu.xaml.cs
Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs
Resources/Locale/en-US/vending-machines/vending-machine-component.ftl

index 221baa36bbc4787544627680d9dd7f5930c00a98..dcd6ed91a7eedbbc55d9dd52de6180ad7fc57d9f 100644 (file)
@@ -1,6 +1,9 @@
 <DefaultWindow xmlns="https://spacestation14.io">
-    <ItemList Name="VendingContents"
-              SizeFlagsStretchRatio="8"
-              VerticalExpand="True">
-    </ItemList>
+    <BoxContainer Orientation="Vertical">
+        <LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True"  Margin ="0 4"/>
+        <ItemList Name="VendingContents"
+                  SizeFlagsStretchRatio="8"
+                  VerticalExpand="True">
+        </ItemList>
+    </BoxContainer>
 </DefaultWindow>
index 3f878d1383f2ec94608037164741415ba7484b86..b436a9d2342df2f2a2a65049112ba9932318c605 100644 (file)
@@ -17,6 +17,7 @@ namespace Content.Client.VendingMachines.UI
         [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
 
         public event Action<ItemList.ItemListSelectedEventArgs>? OnItemSelected;
+        public event Action<string>? OnSearchChanged;
 
         public VendingMachineMenu()
         {
@@ -24,8 +25,14 @@ namespace Content.Client.VendingMachines.UI
             RobustXamlLoader.Load(this);
             IoCManager.InjectDependencies(this);
 
+            SearchBar.OnTextChanged += _ =>
+            {
+                OnSearchChanged?.Invoke(SearchBar.Text);
+            };
+
             VendingContents.OnItemSelected += args =>
             {
+                SearchBar.Text = string.Empty;
                 OnItemSelected?.Invoke(args);
             };
         }
@@ -34,14 +41,14 @@ 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)
+        public void Populate(List<VendingMachineInventoryEntry> inventory, string? filter = null)
         {
             if (inventory.Count == 0)
             {
                 VendingContents.Clear();
                 var outOfStockText = Loc.GetString("vending-machine-component-try-eject-out-of-stock");
                 VendingContents.AddItem(outOfStockText);
-                SetSizeAfterUpdate(outOfStockText.Length);
+                SetSizeAfterUpdate(outOfStockText.Length, VendingContents.Count);
                 return;
             }
 
@@ -56,10 +63,11 @@ namespace Content.Client.VendingMachines.UI
             var longestEntry = string.Empty;
             var spriteSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SpriteSystem>();
 
+            var filterCount = 0;
             for (var i = 0; i < inventory.Count; i++)
             {
                 var entry = inventory[i];
-                var vendingItem = VendingContents[i];
+                var vendingItem = VendingContents[i - filterCount];
                 vendingItem.Text = string.Empty;
                 vendingItem.Icon = null;
 
@@ -71,6 +79,15 @@ namespace Content.Client.VendingMachines.UI
                     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;
 
@@ -78,13 +95,13 @@ namespace Content.Client.VendingMachines.UI
                 vendingItem.Icon = icon;
             }
 
-            SetSizeAfterUpdate(longestEntry.Length);
+            SetSizeAfterUpdate(longestEntry.Length, inventory.Count);
         }
 
-        private void SetSizeAfterUpdate(int longestEntryLength)
+        private void SetSizeAfterUpdate(int longestEntryLength, int contentCount)
         {
             SetSize = new Vector2(Math.Clamp((longestEntryLength + 2) * 12, 250, 300),
-                Math.Clamp(VendingContents.Count * 50, 150, 350));
+                Math.Clamp(contentCount * 50, 150, 350));
         }
     }
 }
index 7a68907871bb20020001c0100acd5c70a5624034..ab310144d5396a18d7af9df698012dbb4d2fa44f 100644 (file)
@@ -30,6 +30,7 @@ namespace Content.Client.VendingMachines
 
             _menu.OnClose += Close;
             _menu.OnItemSelected += OnItemSelected;
+            _menu.OnSearchChanged += OnSearchChanged;
 
             _menu.Populate(_cachedInventory);
 
@@ -74,5 +75,10 @@ namespace Content.Client.VendingMachines
             _menu.OnClose -= Close;
             _menu.Dispose();
         }
+
+        private void OnSearchChanged(string? filter)
+        {
+            _menu?.Populate(_cachedInventory, filter);
+        }
     }
 }
index 855becf16fa348ce239be1a6d2d935f5974127bb..c00586a920eab7d2abf78ff8191f63b486ee646b 100644 (file)
@@ -3,3 +3,4 @@
 vending-machine-component-try-eject-invalid-item = Invalid item
 vending-machine-component-try-eject-out-of-stock = Out of stock
 vending-machine-component-try-eject-access-denied = Access denied
+vending-machine-component-search-filter = Search...