diff --git a/Content.Client/Lathe/UI/LatheMenu.xaml.cs b/Content.Client/Lathe/UI/LatheMenu.xaml.cs index ce190464d2..a0dc241c29 100644 --- a/Content.Client/Lathe/UI/LatheMenu.xaml.cs +++ b/Content.Client/Lathe/UI/LatheMenu.xaml.cs @@ -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) { - if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0) - amount = 1; - RecipeQueueAction?.Invoke(s, amount); - }; - RecipeList.AddChild(control); + 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 + { + 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 /// public void PopulateQueueList(IReadOnlyCollection 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? recipeProto) diff --git a/Content.Client/Lathe/UI/QueuedRecipeControl.xaml.cs b/Content.Client/Lathe/UI/QueuedRecipeControl.xaml.cs index c4ba9803b0..69c8da6d7b 100644 --- a/Content.Client/Lathe/UI/QueuedRecipeControl.xaml.cs +++ b/Content.Client/Lathe/UI/QueuedRecipeControl.xaml.cs @@ -11,26 +11,46 @@ public sealed partial class QueuedRecipeControl : Control public Action? OnMoveUpPressed; public Action? 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; + } } diff --git a/Content.Client/Lathe/UI/RecipeControl.xaml.cs b/Content.Client/Lathe/UI/RecipeControl.xaml.cs index 4f438c8a8e..277fe12c04 100644 --- a/Content.Client/Lathe/UI/RecipeControl.xaml.cs +++ b/Content.Client/Lathe/UI/RecipeControl.xaml.cs @@ -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? OnButtonPressed; public Func TooltipTextSupplier; + private ProtoId _recipeId; + private LatheSystem _latheSystem; + public RecipeControl(LatheSystem latheSystem, LatheRecipePrototype recipe, Func 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 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)