show non-sheet material units in lathe (#19896)

* locale for material units

* use material units in lathe ui

* give units to non-sheet materials

* :trollface:

* use volume properly

* :trollface:

* review

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-09-08 03:11:10 +01:00
committed by GitHub
parent 1c6d723515
commit db2a4478b0
8 changed files with 81 additions and 28 deletions

View File

@@ -26,6 +26,11 @@ public sealed partial class LatheMenu : DefaultWindow
public event Action<string, int>? OnEjectPressed;
public List<string> Recipes = new();
/// <summary>
/// Default volume for a sheet if the material's entity prototype has no material composition.
/// </summary>
private const int DEFAULT_SHEET_VOLUME = 100;
public LatheMenu(LatheBoundUserInterface owner)
{
RobustXamlLoader.Load(this);
@@ -71,24 +76,16 @@ public sealed partial class LatheMenu : DefaultWindow
if (!_prototypeManager.TryIndex(materialId, out MaterialPrototype? material))
continue;
var sheetVolume = SheetVolume(material);
var sheets = (float) volume / sheetVolume;
var maxEjectableSheets = (int) MathF.Floor(sheets);
var unit = Loc.GetString(material.Unit);
var amountText = Loc.GetString("lathe-menu-material-amount", ("amount", sheets), ("unit", unit));
var name = Loc.GetString(material.Name);
var mat = Loc.GetString("lathe-menu-material-display",
("material", name), ("amount", volume / 100));
var volumePerSheet = 0;
var maxEjectableSheets = 0;
var mat = Loc.GetString("lathe-menu-material-display", ("material", name), ("amount", amountText));
if (material.StackEntity != null)
{
var proto = _prototypeManager.Index<EntityPrototype>(material.StackEntity);
if (proto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
{
volumePerSheet = composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == materialId).Value;
maxEjectableSheets = (int) MathF.Floor((float) volume / volumePerSheet);
}
}
var row = new LatheMaterialEjector(materialId, OnEjectPressed, volumePerSheet, maxEjectableSheets)
var row = new LatheMaterialEjector(materialId, OnEjectPressed, sheetVolume, maxEjectableSheets)
{
Icon = { Texture = _spriteSystem.Frame0(material.Icon) },
ProductName = { Text = mat }
@@ -151,10 +148,14 @@ public sealed partial class LatheMenu : DefaultWindow
sb.Append('\n');
var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, component.MaterialUseMultiplier);
var sheetVolume = SheetVolume(proto);
sb.Append(Loc.GetString("lathe-menu-tooltip-display",
("amount", MathF.Round(adjustedAmount / 100f, 2)),
("material", Loc.GetString(proto.Name))));
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)));
}
var icon = prototype.Icon == null
@@ -201,4 +202,17 @@ public sealed partial class LatheMenu : DefaultWindow
: _spriteSystem.Frame0(recipe.Icon);
NameLabel.Text = $"{recipe.Name}";
}
private int SheetVolume(MaterialPrototype material)
{
if (material.StackEntity == null)
return DEFAULT_SHEET_VOLUME;
var proto = _prototypeManager.Index<EntityPrototype>(material.StackEntity);
if (!proto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
return DEFAULT_SHEET_VOLUME;
return composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == material.ID).Value;
}
}