From e7dcbb3c2d1a4a54a9747d00d80777e6fa69df0f Mon Sep 17 00:00:00 2001 From: Ivaylo Draganov Date: Tue, 13 Sep 2022 14:26:09 +0300 Subject: [PATCH] Improve translation of power/silo usage tooltip - Fix an instance where "silo-usage" translation was used without arguments - Use the same translation reference for the "Power usage" - Make the ResourceBarWidget accept a cached transform with the tooltip text so it won't have to build the string itself - Display an infinity symbol when the infinite power cheat is used - Removes a magic number that is no longer used (>1000000 to check for unlimited power) --- .../Logic/Ingame/IngameCashCounterLogic.cs | 4 +-- .../Logic/Ingame/IngamePowerBarLogic.cs | 20 +++++++++++--- .../Logic/Ingame/IngamePowerCounterLogic.cs | 27 +++++++++++++++---- .../Logic/Ingame/IngameSiloBarLogic.cs | 9 +++++-- .../Widgets/ResourceBarWidget.cs | 4 +-- mods/common/languages/en.ftl | 5 ++-- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs index efc24508b0..e8fca59e7a 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs @@ -32,7 +32,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic string siloUsageTooltip = ""; - [TranslationReference("resources", "capacity")] + [TranslationReference("usage", "capacity")] static readonly string SiloUsage = "silo-usage"; [ObjectCreator.UseCtor] @@ -44,7 +44,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic displayResources = playerResources.Cash + playerResources.Resources; siloUsageTooltipCache = new CachedTransform<(int Resources, int Capacity), string>(x => - modData.Translation.GetString(SiloUsage, Translation.Arguments("resources", x.Resources, "capacity", x.Capacity))); + modData.Translation.GetString(SiloUsage, Translation.Arguments("usage", x.Resources, "capacity", x.Capacity))); cashLabel = widget.Get("CASH"); cashLabel.GetTooltipText = () => siloUsageTooltip; diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerBarLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerBarLogic.cs index a6b358152b..8000d51295 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerBarLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerBarLogic.cs @@ -17,18 +17,32 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class IngamePowerBarLogic : ChromeLogic { - [TranslationReference] + [TranslationReference("usage", "capacity")] static readonly string PowerUsage = "power-usage"; + [TranslationReference] + static readonly string Infinite = "infinite-power"; + [ObjectCreator.UseCtor] public IngamePowerBarLogic(Widget widget, ModData modData, World world) { + var developerMode = world.LocalPlayer.PlayerActor.Trait(); var powerManager = world.LocalPlayer.PlayerActor.Trait(); var powerBar = widget.Get("POWERBAR"); - powerBar.GetProvided = () => powerManager.PowerProvided; + powerBar.GetProvided = () => developerMode.UnlimitedPower ? -1 : powerManager.PowerProvided; powerBar.GetUsed = () => powerManager.PowerDrained; - powerBar.TooltipFormat = modData.Translation.GetString(PowerUsage) + ": {0}/{1}"; + powerBar.TooltipTextCached = new CachedTransform<(float Current, float Capacity), string>(usage => + { + var capacity = developerMode.UnlimitedPower ? + modData.Translation.GetString(Infinite) : + powerManager.PowerProvided.ToString(); + + return modData.Translation.GetString( + PowerUsage, + Translation.Arguments("usage", usage.Current, "capacity", capacity)); + }); + powerBar.GetBarColor = () => { if (powerManager.PowerState == PowerState.Critical) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerCounterLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerCounterLogic.cs index 7e07d624ed..c31caf5f49 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerCounterLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngamePowerCounterLogic.cs @@ -17,22 +17,39 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class IngamePowerCounterLogic : ChromeLogic { - [TranslationReference] + [TranslationReference("usage", "capacity")] static readonly string PowerUsage = "power-usage"; + [TranslationReference] + static readonly string Infinite = "infinite-power"; + [ObjectCreator.UseCtor] public IngamePowerCounterLogic(Widget widget, ModData modData, World world) { + var developerMode = world.LocalPlayer.PlayerActor.Trait(); + var powerManager = world.LocalPlayer.PlayerActor.Trait(); var power = widget.Get("POWER"); var powerIcon = widget.Get("POWER_ICON"); - var powerUsage = modData.Translation.GetString(PowerUsage); + var unlimitedCapacity = modData.Translation.GetString(Infinite); powerIcon.GetImageName = () => powerManager.ExcessPower < 0 ? "power-critical" : "power-normal"; power.GetColor = () => powerManager.ExcessPower < 0 ? Color.Red : Color.White; - power.GetText = () => powerManager.PowerProvided == 1000000 ? "∞" : powerManager.ExcessPower.ToString(); - power.GetTooltipText = () => powerUsage + ": " + powerManager.PowerDrained.ToString() + - (powerManager.PowerProvided != 1000000 ? "/" + powerManager.PowerProvided.ToString() : ""); + power.GetText = () => developerMode.UnlimitedPower ? unlimitedCapacity : powerManager.ExcessPower.ToString(); + + var tooltipTextCached = new CachedTransform<(string, string), string>(((string usage, string capacity) args) => + { + return modData.Translation.GetString( + PowerUsage, + Translation.Arguments("usage", args.usage, "capacity", args.capacity)); + }); + + power.GetTooltipText = () => + { + var capacity = developerMode.UnlimitedPower ? unlimitedCapacity : powerManager.PowerProvided.ToString(); + + return tooltipTextCached.Update((powerManager.PowerDrained.ToString(), capacity)); + }; } } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameSiloBarLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameSiloBarLogic.cs index 252d4332af..65483a1b91 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameSiloBarLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameSiloBarLogic.cs @@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class IngameSiloBarLogic : ChromeLogic { - [TranslationReference] + [TranslationReference("usage", "capacity")] static readonly string SiloUsage = "silo-usage"; [ObjectCreator.UseCtor] @@ -28,7 +28,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic siloBar.GetProvided = () => playerResources.ResourceCapacity; siloBar.GetUsed = () => playerResources.Resources; - siloBar.TooltipFormat = modData.Translation.GetString(SiloUsage) + ": {0}/{1}"; + siloBar.TooltipTextCached = new CachedTransform<(float Current, float Capacity), string>(usage => + { + return modData.Translation.GetString( + SiloUsage, + Translation.Arguments("usage", usage.Current, "capacity", usage.Capacity)); + }); siloBar.GetBarColor = () => { if (playerResources.Resources == playerResources.ResourceCapacity) diff --git a/OpenRA.Mods.Common/Widgets/ResourceBarWidget.cs b/OpenRA.Mods.Common/Widgets/ResourceBarWidget.cs index 9ddc1115c1..b6d2b1672a 100644 --- a/OpenRA.Mods.Common/Widgets/ResourceBarWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ResourceBarWidget.cs @@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Widgets public readonly string TooltipContainer; readonly Lazy tooltipContainer; - public string TooltipFormat = ""; + public CachedTransform<(float, float), string> TooltipTextCached; public ResourceBarOrientation Orientation = ResourceBarOrientation.Vertical; public string IndicatorCollection = "sidebar-bits"; public string IndicatorImage = "indicator"; @@ -56,7 +56,7 @@ namespace OpenRA.Mods.Common.Widgets if (TooltipContainer == null) return; - Func getText = () => TooltipFormat.F(GetUsed(), GetProvided()); + Func getText = () => TooltipTextCached.Update((GetUsed(), GetProvided())); tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() { { "getText", getText }, { "world", world } }); } diff --git a/mods/common/languages/en.ftl b/mods/common/languages/en.ftl index 1321fd28ac..66e3b53718 100644 --- a/mods/common/languages/en.ftl +++ b/mods/common/languages/en.ftl @@ -202,11 +202,12 @@ exit-map-editor-confirm = Exit ## IngamePowerBarLogic ## IngamePowerCounterLogic -power-usage = Power Usage +power-usage = Power Usage: { $usage }/{ $capacity } +infinite-power = Infinite ## IngameSiloBarLogic ## IngameCashCounterLogic -silo-usage = Silo Usage: { $resources }/{ $capacity } +silo-usage = Silo Usage: { $usage }/{ $capacity } ## ObserverShroudSelectorLogic camera-option-all-players = All Players