]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Reuse lathe queue instead of redrawing (#39886)
authorScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Thu, 4 Sep 2025 19:37:15 +0000 (21:37 +0200)
committerGitHub <noreply@github.com>
Thu, 4 Sep 2025 19:37:15 +0000 (21:37 +0200)
* init

* init

* PUSH!!!

* //

* Me when the when the me when the

* review

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Content.Client/Lathe/UI/LatheMenu.xaml.cs
Content.Client/Lathe/UI/QueuedRecipeControl.xaml.cs
Content.Client/Lathe/UI/RecipeControl.xaml.cs

index ce190464d2c2cf87170a1087fa05809a14458de1..a0dc241c29b711f0ee94a36dd6c094ccab51ad36 100644 (file)
@@ -11,6 +11,7 @@ using Robust.Client.UserInterface.Controls;
 using Robust.Client.UserInterface.CustomControls;
 using Robust.Client.UserInterface.XAML;
 using Robust.Shared.Prototypes;
+using Robust.Shared.Utility;
 
 namespace Content.Client.Lathe.UI;
 
@@ -128,21 +129,50 @@ public sealed partial class LatheMenu : DefaultWindow
         RecipeCount.Text = Loc.GetString("lathe-menu-recipe-count", ("count", recipesToShow.Count));
 
         var sortedRecipesToShow = recipesToShow.OrderBy(_lathe.GetRecipeName);
-        RecipeList.Children.Clear();
+
+        // Get the existing list of queue controls
+        var oldChildCount = RecipeList.ChildCount;
         _entityManager.TryGetComponent(Entity, out LatheComponent? lathe);
 
+        int idx = 0;
         foreach (var prototype in sortedRecipesToShow)
         {
             var canProduce = _lathe.CanProduce(Entity, prototype, quantity, component: lathe);
+            var tooltipFunction = () => GenerateTooltipText(prototype);
 
-            var control = new RecipeControl(_lathe, prototype, () => GenerateTooltipText(prototype), canProduce, GetRecipeDisplayControl(prototype));
-            control.OnButtonPressed += s =>
+            if (idx >= oldChildCount)
+            {
+                var control = new RecipeControl(_lathe, prototype, tooltipFunction, canProduce, GetRecipeDisplayControl(prototype));
+                control.OnButtonPressed += s =>
+                {
+                    if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
+                        amount = 1;
+                    RecipeQueueAction?.Invoke(s, amount);
+                };
+                RecipeList.AddChild(control);
+            }
+            else
             {
-                if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
-                    amount = 1;
-                RecipeQueueAction?.Invoke(s, amount);
-            };
-            RecipeList.AddChild(control);
+                var child = RecipeList.GetChild(idx) as RecipeControl;
+
+                if (child == null)
+                {
+                    DebugTools.Assert($"Lathe menu recipe control at {idx} is not of type RecipeControl"); // Something's gone terribly wrong.
+                    continue;
+                }
+
+                child.SetRecipe(prototype);
+                child.SetTooltipSupplier(tooltipFunction);
+                child.SetCanProduce(canProduce);
+                child.SetDisplayControl(GetRecipeDisplayControl(prototype));
+            }
+            idx++;
+        }
+
+        // Shrink list if new list is shorter than old list.
+        for (var childIdx = oldChildCount - 1; idx <= childIdx; childIdx--)
+        {
+            RecipeList.RemoveChild(childIdx);
         }
     }
 
@@ -238,9 +268,10 @@ public sealed partial class LatheMenu : DefaultWindow
     /// <param name="queue"></param>
     public void PopulateQueueList(IReadOnlyCollection<LatheRecipeBatch> queue)
     {
-        QueueList.DisposeAllChildren();
+        // Get the existing list of queue controls
+        var oldChildCount = QueueList.ChildCount;
 
-        var idx = 1;
+        var idx = 0;
         foreach (var batch in queue)
         {
             var recipe = _prototypeManager.Index(batch.Recipe);
@@ -248,18 +279,40 @@ public sealed partial class LatheMenu : DefaultWindow
             var itemName = _lathe.GetRecipeName(batch.Recipe);
             string displayText;
             if (batch.ItemsRequested > 1)
-                displayText = Loc.GetString("lathe-menu-item-batch", ("index", idx), ("name", itemName), ("printed", batch.ItemsPrinted), ("total", batch.ItemsRequested));
+                displayText = Loc.GetString("lathe-menu-item-batch", ("index", idx + 1), ("name", itemName), ("printed", batch.ItemsPrinted), ("total", batch.ItemsRequested));
             else
-                displayText = Loc.GetString("lathe-menu-item-single", ("index", idx), ("name", itemName));
+                displayText = Loc.GetString("lathe-menu-item-single", ("index", idx + 1), ("name", itemName));
 
-            var queuedRecipeBox = new QueuedRecipeControl(displayText, idx - 1, GetRecipeDisplayControl(recipe));
-            queuedRecipeBox.OnDeletePressed += s => QueueDeleteAction?.Invoke(s);
-            queuedRecipeBox.OnMoveUpPressed += s => QueueMoveUpAction?.Invoke(s);
-            queuedRecipeBox.OnMoveDownPressed += s => QueueMoveDownAction?.Invoke(s);
+            if (idx >= oldChildCount)
+            {
+                var queuedRecipeBox = new QueuedRecipeControl(displayText, idx, GetRecipeDisplayControl(recipe));
+                queuedRecipeBox.OnDeletePressed += s => QueueDeleteAction?.Invoke(s);
+                queuedRecipeBox.OnMoveUpPressed += s => QueueMoveUpAction?.Invoke(s);
+                queuedRecipeBox.OnMoveDownPressed += s => QueueMoveDownAction?.Invoke(s);
+                QueueList.AddChild(queuedRecipeBox);
+            }
+            else
+            {
+                var child = QueueList.GetChild(idx) as QueuedRecipeControl;
 
-            QueueList.AddChild(queuedRecipeBox);
+                if (child == null)
+                {
+                    DebugTools.Assert($"Lathe menu queued recipe control at {idx} is not of type QueuedRecipeControl"); // Something's gone terribly wrong.
+                    continue;
+                }
+
+                child.SetDisplayText(displayText);
+                child.SetIndex(idx);
+                child.SetDisplayControl(GetRecipeDisplayControl(recipe));
+            }
             idx++;
         }
+
+        // Shrink list if new list is shorter than old list.
+        for (var childIdx = oldChildCount - 1; idx <= childIdx; childIdx--)
+        {
+            QueueList.RemoveChild(childIdx);
+        }
     }
 
     public void SetQueueInfo(ProtoId<LatheRecipePrototype>? recipeProto)
index c4ba9803b0d121cac1dfe1cfcf54ab39b205aeed..69c8da6d7b713ed4f1cda978b2b468bbc05a320b 100644 (file)
@@ -11,26 +11,46 @@ public sealed partial class QueuedRecipeControl : Control
     public Action<int>? OnMoveUpPressed;
     public Action<int>? OnMoveDownPressed;
 
+    private int _index;
+
     public QueuedRecipeControl(string displayText, int index, Control displayControl)
     {
         RobustXamlLoader.Load(this);
 
-        RecipeName.Text = displayText;
-        RecipeDisplayContainer.AddChild(displayControl);
+        SetDisplayText(displayText);
+        SetDisplayControl(displayControl);
+        SetIndex(index);
+        _index = index;
 
         MoveUp.OnPressed += (_) =>
         {
-            OnMoveUpPressed?.Invoke(index);
+            OnMoveUpPressed?.Invoke(_index);
         };
 
         MoveDown.OnPressed += (_) =>
         {
-            OnMoveDownPressed?.Invoke(index);
+            OnMoveDownPressed?.Invoke(_index);
         };
 
         Delete.OnPressed += (_) =>
         {
-            OnDeletePressed?.Invoke(index);
+            OnDeletePressed?.Invoke(_index);
         };
     }
+
+    public void SetDisplayText(string displayText)
+    {
+        RecipeName.Text = displayText;
+    }
+
+    public void SetDisplayControl(Control displayControl)
+    {
+        RecipeDisplayContainer.Children.Clear();
+        RecipeDisplayContainer.AddChild(displayControl);
+    }
+
+    public void SetIndex(int index)
+    {
+        _index = index;
+    }
 }
index 4f438c8a8e54c9379260c5c1212395a63321747d..277fe12c046f863bc275d8f75e1b77e98b6f68c8 100644 (file)
@@ -2,6 +2,7 @@ using Content.Shared.Research.Prototypes;
 using Robust.Client.AutoGenerated;
 using Robust.Client.UserInterface;
 using Robust.Client.UserInterface.XAML;
+using Robust.Shared.Prototypes;
 
 namespace Content.Client.Lathe.UI;
 
@@ -11,20 +12,47 @@ public sealed partial class RecipeControl : Control
     public Action<string>? OnButtonPressed;
     public Func<string> TooltipTextSupplier;
 
+    private ProtoId<LatheRecipePrototype> _recipeId;
+    private LatheSystem _latheSystem;
+
     public RecipeControl(LatheSystem latheSystem, LatheRecipePrototype recipe, Func<string> tooltipTextSupplier, bool canProduce, Control displayControl)
     {
         RobustXamlLoader.Load(this);
 
-        RecipeName.Text = latheSystem.GetRecipeName(recipe);
-        RecipeDisplayContainer.AddChild(displayControl);
-        Button.Disabled = !canProduce;
+        _latheSystem = latheSystem;
+        _recipeId = recipe.ID;
         TooltipTextSupplier = tooltipTextSupplier;
-        Button.TooltipSupplier = SupplyTooltip;
+        SetRecipe(recipe);
+        SetCanProduce(canProduce);
+        SetDisplayControl(displayControl);
 
         Button.OnPressed += (_) =>
         {
-            OnButtonPressed?.Invoke(recipe.ID);
+            OnButtonPressed?.Invoke(_recipeId);
         };
+        Button.TooltipSupplier = SupplyTooltip;
+    }
+
+    public void SetRecipe(LatheRecipePrototype recipe)
+    {
+        RecipeName.Text = _latheSystem.GetRecipeName(recipe);
+        _recipeId = recipe.ID;
+    }
+
+    public void SetTooltipSupplier(Func<string> tooltipTextSupplier)
+    {
+        TooltipTextSupplier = tooltipTextSupplier;
+    }
+
+    public void SetCanProduce(bool canProduce)
+    {
+        Button.Disabled = !canProduce;
+    }
+
+    public void SetDisplayControl(Control displayControl)
+    {
+        RecipeDisplayContainer.Children.Clear();
+        RecipeDisplayContainer.AddChild(displayControl);
     }
 
     private Control? SupplyTooltip(Control sender)