Reuse lathe queue instead of redrawing (#39886)
* init * init * PUSH!!! * // * Me when the when the me when the * review --------- Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
This commit is contained in:
@@ -11,6 +11,7 @@ using Robust.Client.UserInterface.Controls;
|
|||||||
using Robust.Client.UserInterface.CustomControls;
|
using Robust.Client.UserInterface.CustomControls;
|
||||||
using Robust.Client.UserInterface.XAML;
|
using Robust.Client.UserInterface.XAML;
|
||||||
using Robust.Shared.Prototypes;
|
using Robust.Shared.Prototypes;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Client.Lathe.UI;
|
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));
|
RecipeCount.Text = Loc.GetString("lathe-menu-recipe-count", ("count", recipesToShow.Count));
|
||||||
|
|
||||||
var sortedRecipesToShow = recipesToShow.OrderBy(_lathe.GetRecipeName);
|
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);
|
_entityManager.TryGetComponent(Entity, out LatheComponent? lathe);
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
foreach (var prototype in sortedRecipesToShow)
|
foreach (var prototype in sortedRecipesToShow)
|
||||||
{
|
{
|
||||||
var canProduce = _lathe.CanProduce(Entity, prototype, quantity, component: lathe);
|
var canProduce = _lathe.CanProduce(Entity, prototype, quantity, component: lathe);
|
||||||
|
var tooltipFunction = () => GenerateTooltipText(prototype);
|
||||||
|
|
||||||
var control = new RecipeControl(_lathe, prototype, () => GenerateTooltipText(prototype), canProduce, GetRecipeDisplayControl(prototype));
|
if (idx >= oldChildCount)
|
||||||
control.OnButtonPressed += s =>
|
|
||||||
{
|
{
|
||||||
if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
|
var control = new RecipeControl(_lathe, prototype, tooltipFunction, canProduce, GetRecipeDisplayControl(prototype));
|
||||||
amount = 1;
|
control.OnButtonPressed += s =>
|
||||||
RecipeQueueAction?.Invoke(s, amount);
|
{
|
||||||
};
|
if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
|
||||||
RecipeList.AddChild(control);
|
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
|
|||||||
/// <param name="queue"></param>
|
/// <param name="queue"></param>
|
||||||
public void PopulateQueueList(IReadOnlyCollection<LatheRecipeBatch> queue)
|
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)
|
foreach (var batch in queue)
|
||||||
{
|
{
|
||||||
var recipe = _prototypeManager.Index(batch.Recipe);
|
var recipe = _prototypeManager.Index(batch.Recipe);
|
||||||
@@ -248,18 +279,40 @@ public sealed partial class LatheMenu : DefaultWindow
|
|||||||
var itemName = _lathe.GetRecipeName(batch.Recipe);
|
var itemName = _lathe.GetRecipeName(batch.Recipe);
|
||||||
string displayText;
|
string displayText;
|
||||||
if (batch.ItemsRequested > 1)
|
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
|
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));
|
if (idx >= oldChildCount)
|
||||||
queuedRecipeBox.OnDeletePressed += s => QueueDeleteAction?.Invoke(s);
|
{
|
||||||
queuedRecipeBox.OnMoveUpPressed += s => QueueMoveUpAction?.Invoke(s);
|
var queuedRecipeBox = new QueuedRecipeControl(displayText, idx, GetRecipeDisplayControl(recipe));
|
||||||
queuedRecipeBox.OnMoveDownPressed += s => QueueMoveDownAction?.Invoke(s);
|
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++;
|
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)
|
public void SetQueueInfo(ProtoId<LatheRecipePrototype>? recipeProto)
|
||||||
|
|||||||
@@ -11,26 +11,46 @@ public sealed partial class QueuedRecipeControl : Control
|
|||||||
public Action<int>? OnMoveUpPressed;
|
public Action<int>? OnMoveUpPressed;
|
||||||
public Action<int>? OnMoveDownPressed;
|
public Action<int>? OnMoveDownPressed;
|
||||||
|
|
||||||
|
private int _index;
|
||||||
|
|
||||||
public QueuedRecipeControl(string displayText, int index, Control displayControl)
|
public QueuedRecipeControl(string displayText, int index, Control displayControl)
|
||||||
{
|
{
|
||||||
RobustXamlLoader.Load(this);
|
RobustXamlLoader.Load(this);
|
||||||
|
|
||||||
RecipeName.Text = displayText;
|
SetDisplayText(displayText);
|
||||||
RecipeDisplayContainer.AddChild(displayControl);
|
SetDisplayControl(displayControl);
|
||||||
|
SetIndex(index);
|
||||||
|
_index = index;
|
||||||
|
|
||||||
MoveUp.OnPressed += (_) =>
|
MoveUp.OnPressed += (_) =>
|
||||||
{
|
{
|
||||||
OnMoveUpPressed?.Invoke(index);
|
OnMoveUpPressed?.Invoke(_index);
|
||||||
};
|
};
|
||||||
|
|
||||||
MoveDown.OnPressed += (_) =>
|
MoveDown.OnPressed += (_) =>
|
||||||
{
|
{
|
||||||
OnMoveDownPressed?.Invoke(index);
|
OnMoveDownPressed?.Invoke(_index);
|
||||||
};
|
};
|
||||||
|
|
||||||
Delete.OnPressed += (_) =>
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using Content.Shared.Research.Prototypes;
|
|||||||
using Robust.Client.AutoGenerated;
|
using Robust.Client.AutoGenerated;
|
||||||
using Robust.Client.UserInterface;
|
using Robust.Client.UserInterface;
|
||||||
using Robust.Client.UserInterface.XAML;
|
using Robust.Client.UserInterface.XAML;
|
||||||
|
using Robust.Shared.Prototypes;
|
||||||
|
|
||||||
namespace Content.Client.Lathe.UI;
|
namespace Content.Client.Lathe.UI;
|
||||||
|
|
||||||
@@ -11,20 +12,47 @@ public sealed partial class RecipeControl : Control
|
|||||||
public Action<string>? OnButtonPressed;
|
public Action<string>? OnButtonPressed;
|
||||||
public Func<string> TooltipTextSupplier;
|
public Func<string> TooltipTextSupplier;
|
||||||
|
|
||||||
|
private ProtoId<LatheRecipePrototype> _recipeId;
|
||||||
|
private LatheSystem _latheSystem;
|
||||||
|
|
||||||
public RecipeControl(LatheSystem latheSystem, LatheRecipePrototype recipe, Func<string> tooltipTextSupplier, bool canProduce, Control displayControl)
|
public RecipeControl(LatheSystem latheSystem, LatheRecipePrototype recipe, Func<string> tooltipTextSupplier, bool canProduce, Control displayControl)
|
||||||
{
|
{
|
||||||
RobustXamlLoader.Load(this);
|
RobustXamlLoader.Load(this);
|
||||||
|
|
||||||
RecipeName.Text = latheSystem.GetRecipeName(recipe);
|
_latheSystem = latheSystem;
|
||||||
RecipeDisplayContainer.AddChild(displayControl);
|
_recipeId = recipe.ID;
|
||||||
Button.Disabled = !canProduce;
|
|
||||||
TooltipTextSupplier = tooltipTextSupplier;
|
TooltipTextSupplier = tooltipTextSupplier;
|
||||||
Button.TooltipSupplier = SupplyTooltip;
|
SetRecipe(recipe);
|
||||||
|
SetCanProduce(canProduce);
|
||||||
|
SetDisplayControl(displayControl);
|
||||||
|
|
||||||
Button.OnPressed += (_) =>
|
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)
|
private Control? SupplyTooltip(Control sender)
|
||||||
|
|||||||
Reference in New Issue
Block a user