From a17875498056ff746abb9ec1d4a40017c99ad7cc Mon Sep 17 00:00:00 2001 From: Crotalus Date: Mon, 8 Apr 2024 05:16:11 +0200 Subject: [PATCH] Show missing materials in lathes tooltip (#26795) * Lathes: Show missing materials amount in tooltip * Use AppendLine and remove the last newline at the end --- Content.Client/Lathe/UI/LatheMenu.xaml.cs | 75 +++++++++++-------- Content.Client/Lathe/UI/RecipeControl.xaml.cs | 9 +-- .../Locale/en-US/lathe/ui/lathe-menu.ftl | 8 +- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/Content.Client/Lathe/UI/LatheMenu.xaml.cs b/Content.Client/Lathe/UI/LatheMenu.xaml.cs index 93d80dbf9c..21897681c1 100644 --- a/Content.Client/Lathe/UI/LatheMenu.xaml.cs +++ b/Content.Client/Lathe/UI/LatheMenu.xaml.cs @@ -104,41 +104,12 @@ public sealed partial class LatheMenu : DefaultWindow RecipeList.Children.Clear(); foreach (var prototype in sortedRecipesToShow) { - StringBuilder sb = new(); - var first = true; - foreach (var (id, amount) in prototype.RequiredMaterials) - { - if (!_prototypeManager.TryIndex(id, out var proto)) - continue; - - if (first) - first = false; - else - sb.Append('\n'); - - var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, component.MaterialUseMultiplier); - var sheetVolume = _materialStorage.GetSheetVolume(proto); - - var unit = Loc.GetString(proto.Unit); - // rounded in locale not here - var sheets = adjustedAmount / (float) sheetVolume; - var amountText = Loc.GetString("lathe-menu-material-amount", ("amount", sheets), ("unit", unit)); - var name = Loc.GetString(proto.Name); - sb.Append(Loc.GetString("lathe-menu-tooltip-display", ("material", name), ("amount", amountText))); - } - - if (!string.IsNullOrWhiteSpace(prototype.Description)) - { - sb.Append('\n'); - sb.Append(Loc.GetString("lathe-menu-description-display", ("description", prototype.Description))); - } - var icon = prototype.Icon == null ? _spriteSystem.GetPrototypeIcon(prototype.Result).Default : _spriteSystem.Frame0(prototype.Icon); var canProduce = _lathe.CanProduce(_owner, prototype, quantity); - var control = new RecipeControl(prototype, sb.ToString(), canProduce, icon); + var control = new RecipeControl(prototype, () => GenerateTooltipText(prototype), canProduce, icon); control.OnButtonPressed += s => { if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0) @@ -149,6 +120,50 @@ public sealed partial class LatheMenu : DefaultWindow } } + private string GenerateTooltipText(LatheRecipePrototype prototype) + { + StringBuilder sb = new(); + + foreach (var (id, amount) in prototype.RequiredMaterials) + { + if (!_prototypeManager.TryIndex(id, out var proto)) + continue; + + var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, _entityManager.GetComponent(_owner).MaterialUseMultiplier); + var sheetVolume = _materialStorage.GetSheetVolume(proto); + + var unit = Loc.GetString(proto.Unit); + var sheets = adjustedAmount / (float) sheetVolume; + + var availableAmount = _materialStorage.GetMaterialAmount(_owner, id); + var missingAmount = Math.Max(0, adjustedAmount - availableAmount); + var missingSheets = missingAmount / (float) sheetVolume; + + var name = Loc.GetString(proto.Name); + + string amountText; + if (missingSheets > 0) + { + amountText = Loc.GetString("lathe-menu-material-amount-missing", ("amount", sheets), ("missingAmount", missingSheets), ("unit", unit), ("material", name)); + } + else + { + amountText = Loc.GetString("lathe-menu-material-amount", ("amount", sheets), ("unit", unit), ("material", name)); + } + + sb.AppendLine(amountText); + } + + if (!string.IsNullOrWhiteSpace(prototype.Description)) + sb.AppendLine(Loc.GetString("lathe-menu-description-display", ("description", prototype.Description))); + + // Remove last newline + if (sb.Length > 0) + sb.Remove(sb.Length - 1, 1); + + return sb.ToString(); + } + public void UpdateCategories() { var currentCategories = new List>(); diff --git a/Content.Client/Lathe/UI/RecipeControl.xaml.cs b/Content.Client/Lathe/UI/RecipeControl.xaml.cs index 451a988765..bf85ff7d93 100644 --- a/Content.Client/Lathe/UI/RecipeControl.xaml.cs +++ b/Content.Client/Lathe/UI/RecipeControl.xaml.cs @@ -11,17 +11,16 @@ namespace Content.Client.Lathe.UI; public sealed partial class RecipeControl : Control { public Action? OnButtonPressed; + public Func TooltipTextSupplier; - public string TooltipText; - - public RecipeControl(LatheRecipePrototype recipe, string tooltip, bool canProduce, Texture? texture = null) + public RecipeControl(LatheRecipePrototype recipe, Func tooltipTextSupplier, bool canProduce, Texture? texture = null) { RobustXamlLoader.Load(this); RecipeName.Text = recipe.Name; RecipeTexture.Texture = texture; Button.Disabled = !canProduce; - TooltipText = tooltip; + TooltipTextSupplier = tooltipTextSupplier; Button.TooltipSupplier = SupplyTooltip; Button.OnPressed += (_) => @@ -32,6 +31,6 @@ public sealed partial class RecipeControl : Control private Control? SupplyTooltip(Control sender) { - return new RecipeTooltip(TooltipText); + return new RecipeTooltip(TooltipTextSupplier()); } } diff --git a/Resources/Locale/en-US/lathe/ui/lathe-menu.ftl b/Resources/Locale/en-US/lathe/ui/lathe-menu.ftl index 92f313086c..72b836e69c 100644 --- a/Resources/Locale/en-US/lathe/ui/lathe-menu.ftl +++ b/Resources/Locale/en-US/lathe/ui/lathe-menu.ftl @@ -10,8 +10,12 @@ lathe-menu-material-display = {$material} ({$amount}) lathe-menu-tooltip-display = {$amount} of {$material} lathe-menu-description-display = [italic]{$description}[/italic] lathe-menu-material-amount = { $amount -> - [1] {NATURALFIXED($amount, 2)} {$unit} - *[other] {NATURALFIXED($amount, 2)} {MAKEPLURAL($unit)} + [1] {NATURALFIXED($amount, 2)} {$unit} of {$material} + *[other] {NATURALFIXED($amount, 2)} {MAKEPLURAL($unit)} of {$material} +} +lathe-menu-material-amount-missing = { $amount -> + [1] {NATURALFIXED($amount, 2)} {$unit} of {$material} ([color=red]{NATURALFIXED($missingAmount, 2)} {$unit} missing[/color]) + *[other] {NATURALFIXED($amount, 2)} {MAKEPLURAL($unit)} of {$material} ([color=red]{NATURALFIXED($missingAmount, 2)} {MAKEPLURAL($unit)} missing[/color]) } lathe-menu-no-materials-message = No materials loaded. lathe-menu-fabricating-message = Fabricating...