]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Show missing materials in lathes tooltip (#26795)
authorCrotalus <Crotalus@users.noreply.github.com>
Mon, 8 Apr 2024 03:16:11 +0000 (05:16 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Apr 2024 03:16:11 +0000 (23:16 -0400)
* Lathes: Show missing materials amount in tooltip

* Use AppendLine and remove the last newline at the end

Content.Client/Lathe/UI/LatheMenu.xaml.cs
Content.Client/Lathe/UI/RecipeControl.xaml.cs
Resources/Locale/en-US/lathe/ui/lathe-menu.ftl

index 93d80dbf9c5e36c35ce1982a71174ac7d3482192..21897681c132eacb298ce57ab3fd8abb07665d35 100644 (file)
@@ -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<MaterialPrototype>(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<MaterialPrototype>(id, out var proto))
+                continue;
+
+            var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, _entityManager.GetComponent<LatheComponent>(_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<ProtoId<LatheCategoryPrototype>>();
index 451a988765f8db72a40a9e14981ee3196b561e1f..bf85ff7d938c87dd604a7d2215da407e5f636b47 100644 (file)
@@ -11,17 +11,16 @@ namespace Content.Client.Lathe.UI;
 public sealed partial class RecipeControl : Control
 {
     public Action<string>? OnButtonPressed;
+    public Func<string> TooltipTextSupplier;
 
-    public string TooltipText;
-
-    public RecipeControl(LatheRecipePrototype recipe, string tooltip, bool canProduce, Texture? texture = null)
+    public RecipeControl(LatheRecipePrototype recipe, Func<string> 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());
     }
 }
index 92f313086cb3f9170b7a4e3ed4d1daa90d3d0197..72b836e69c22df6864764351e6b4539a1855e91b 100644 (file)
@@ -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...