Fix uses of LabelWidget.Text and ButtonWidget.Text to use GetText instead.
The Text element of these widgets was changed from display text to a translation key as part of adding translation support. Functions interested in the display text need to invoke GetText instead. Lots of functions have not been updated, resulting in symptoms such as measuring the font size of the translation key rather than the display text and resizing a widget to the wrong size. Update all callers to use GetText when getting or setting display text. This ensure their existing functionality that was intended to work in terms of the display text and not the translation key works as expected.
This commit is contained in:
@@ -41,12 +41,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var name = tooltip != null ? TranslationProvider.GetString(tooltip.Name) : armyUnit.ActorInfo.Name;
|
||||
var buildable = armyUnit.BuildableInfo;
|
||||
|
||||
nameLabel.Text = name;
|
||||
|
||||
nameLabel.GetText = () => name;
|
||||
var nameSize = font.Measure(name);
|
||||
|
||||
var desc = string.IsNullOrEmpty(buildable.Description) ? "" : TranslationProvider.GetString(buildable.Description);
|
||||
descLabel.Text = desc;
|
||||
descLabel.GetText = () => desc;
|
||||
var descSize = descFont.Measure(desc);
|
||||
descLabel.Bounds.Width = descSize.X;
|
||||
descLabel.Bounds.Height = descSize.Y + descLabelPadding;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
if (missionData != null)
|
||||
{
|
||||
var text = WidgetUtils.WrapText(missionData.Briefing?.Replace("\\n", "\n"), mapDescription.Bounds.Width, mapFont);
|
||||
mapDescription.Text = text;
|
||||
mapDescription.GetText = () => text;
|
||||
mapDescription.Bounds.Height = mapFont.Measure(text).Y;
|
||||
mapDescriptionPanel.ScrollToTop();
|
||||
mapDescriptionPanel.Layout.AdjustChildren();
|
||||
|
||||
@@ -108,7 +108,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
if (tabButton != null)
|
||||
{
|
||||
tabButton.Text = TranslationProvider.GetString(label);
|
||||
var tabButtonText = TranslationProvider.GetString(label);
|
||||
tabButton.GetText = () => tabButtonText;
|
||||
tabButton.OnClick = () =>
|
||||
{
|
||||
if (activePanel == IngameInfoPanel.Chat)
|
||||
|
||||
@@ -22,7 +22,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
var versionLabel = widget.GetOrNull<LabelWidget>("VERSION_LABEL");
|
||||
if (versionLabel != null)
|
||||
versionLabel.Text = modData.Manifest.Metadata.Version;
|
||||
{
|
||||
var versionText = modData.Manifest.Metadata.Version;
|
||||
versionLabel.GetText = () => versionText;
|
||||
}
|
||||
|
||||
var keyhandler = widget.Get<LogicKeyListenerWidget>("CANCEL_HANDLER");
|
||||
keyhandler.AddHandler(e =>
|
||||
|
||||
@@ -78,7 +78,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
}
|
||||
|
||||
siloUsageTooltip = siloUsageTooltipCache.Update((playerResources.Resources, playerResources.ResourceCapacity));
|
||||
cashLabel.Text = displayResources.ToString(CultureInfo.CurrentCulture);
|
||||
var displayResourcesText = displayResources.ToString(CultureInfo.CurrentCulture);
|
||||
cashLabel.GetText = () => displayResourcesText;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +187,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
mpe = world.WorldActor.TraitOrDefault<MenuPostProcessEffect>();
|
||||
mpe?.Fade(mpe.Info.MenuEffect);
|
||||
|
||||
menu.Get<LabelWidget>("VERSION_LABEL").Text = modData.Manifest.Metadata.Version;
|
||||
var versionText = modData.Manifest.Metadata.Version;
|
||||
menu.Get<LabelWidget>("VERSION_LABEL").GetText = () => versionText;
|
||||
|
||||
buttonContainer = menu.Get("MENU_BUTTONS");
|
||||
buttonTemplate = buttonContainer.Get<ButtonWidget>("BUTTON_TEMPLATE");
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
cost = valued.Cost;
|
||||
}
|
||||
|
||||
nameLabel.Text = name;
|
||||
nameLabel.GetText = () => name;
|
||||
|
||||
var nameSize = font.Measure(name);
|
||||
var hotkeyWidth = 0;
|
||||
@@ -93,18 +93,21 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var hotkeyText = $"({hotkey.DisplayString()})";
|
||||
|
||||
hotkeyWidth = font.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X;
|
||||
hotkeyLabel.Text = hotkeyText;
|
||||
hotkeyLabel.GetText = () => hotkeyText;
|
||||
hotkeyLabel.Bounds.X = nameSize.X + 2 * nameLabel.Bounds.X;
|
||||
}
|
||||
|
||||
var prereqs = buildable.Prerequisites.Select(a => ActorName(mapRules, a))
|
||||
.Where(s => !s.StartsWith('~') && !s.StartsWith('!'));
|
||||
var prereqs = buildable.Prerequisites
|
||||
.Select(a => ActorName(mapRules, a))
|
||||
.Where(s => !s.StartsWith('~') && !s.StartsWith('!'))
|
||||
.ToList();
|
||||
|
||||
var requiresSize = int2.Zero;
|
||||
if (prereqs.Any())
|
||||
if (prereqs.Count > 0)
|
||||
{
|
||||
requiresLabel.Text = TranslationProvider.GetString(Requires, Translation.Arguments("prequisites", prereqs.JoinWith(", ")));
|
||||
requiresSize = requiresFont.Measure(requiresLabel.Text);
|
||||
var requiresText = TranslationProvider.GetString(Requires, Translation.Arguments("prequisites", prereqs.JoinWith(", ")));
|
||||
requiresLabel.GetText = () => requiresText;
|
||||
requiresSize = requiresFont.Measure(requiresText);
|
||||
requiresLabel.Visible = true;
|
||||
descLabel.Bounds.Y = descLabelY + requiresLabel.Bounds.Height;
|
||||
}
|
||||
@@ -118,27 +121,30 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
if (pm != null)
|
||||
{
|
||||
var power = actor.TraitInfos<PowerInfo>().Where(i => i.EnabledByDefault).Sum(i => i.Amount);
|
||||
powerLabel.Text = power.ToString(NumberFormatInfo.CurrentInfo);
|
||||
var powerText = power.ToString(NumberFormatInfo.CurrentInfo);
|
||||
powerLabel.GetText = () => powerText;
|
||||
powerLabel.GetColor = () => (pm.PowerProvided - pm.PowerDrained >= -power || power > 0)
|
||||
? Color.White : Color.Red;
|
||||
powerLabel.Visible = power != 0;
|
||||
powerIcon.Visible = power != 0;
|
||||
powerSize = font.Measure(powerLabel.Text);
|
||||
powerSize = font.Measure(powerText);
|
||||
}
|
||||
|
||||
var buildTime = tooltipIcon.ProductionQueue?.GetBuildTime(actor, buildable) ?? 0;
|
||||
var timeModifier = pm != null && pm.PowerState != PowerState.Normal ? tooltipIcon.ProductionQueue.Info.LowPowerModifier : 100;
|
||||
|
||||
timeLabel.Text = formatBuildTime.Update(buildTime * timeModifier / 100);
|
||||
var timeText = formatBuildTime.Update(buildTime * timeModifier / 100);
|
||||
timeLabel.GetText = () => timeText;
|
||||
timeLabel.TextColor = (pm != null && pm.PowerState != PowerState.Normal && tooltipIcon.ProductionQueue.Info.LowPowerModifier > 100) ? Color.Red : Color.White;
|
||||
var timeSize = font.Measure(timeLabel.Text);
|
||||
var timeSize = font.Measure(timeText);
|
||||
|
||||
costLabel.Text = cost.ToString(NumberFormatInfo.CurrentInfo);
|
||||
var costText = cost.ToString(NumberFormatInfo.CurrentInfo);
|
||||
costLabel.GetText = () => costText;
|
||||
costLabel.GetColor = () => pr.GetCashAndResources() >= cost ? Color.White : Color.Red;
|
||||
var costSize = font.Measure(costLabel.Text);
|
||||
var costSize = font.Measure(costText);
|
||||
|
||||
var desc = string.IsNullOrEmpty(buildable.Description) ? "" : TranslationProvider.GetString(buildable.Description);
|
||||
descLabel.Text = desc;
|
||||
descLabel.GetText = () => desc;
|
||||
var descSize = descFont.Measure(desc);
|
||||
descLabel.Bounds.Width = descSize.X;
|
||||
descLabel.Bounds.Height = descSize.Y + descLabelPadding;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var errorMessage = luaScript.Context.ErrorMessage.Replace("\r\n", "\n");
|
||||
|
||||
var text = WidgetUtils.WrapText(errorMessage, label.Bounds.Width, font);
|
||||
label.Text = text;
|
||||
label.GetText = () => text;
|
||||
label.Bounds.Height = font.Measure(text).Y;
|
||||
panel.ScrollToTop();
|
||||
panel.Layout.AdjustChildren();
|
||||
|
||||
@@ -53,23 +53,24 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
if (sp == lastPower && hotkey == lastHotkey && lastRemainingSeconds == remainingSeconds)
|
||||
return;
|
||||
|
||||
nameLabel.Text = sp.Info.Name;
|
||||
var nameSize = nameFont.Measure(nameLabel.Text);
|
||||
var nameText = sp.Info.Name;
|
||||
nameLabel.GetText = () => nameText;
|
||||
var nameSize = nameFont.Measure(nameText);
|
||||
|
||||
descLabel.Text = sp.Info.Description.Replace("\\n", "\n");
|
||||
var descSize = descFont.Measure(descLabel.Text);
|
||||
var descText = sp.Info.Description.Replace("\\n", "\n");
|
||||
descLabel.GetText = () => descText;
|
||||
var descSize = descFont.Measure(descText);
|
||||
|
||||
var customLabel = sp.TooltipTimeTextOverride();
|
||||
if (customLabel == null)
|
||||
var timeText = sp.TooltipTimeTextOverride();
|
||||
if (timeText == null)
|
||||
{
|
||||
var remaining = WidgetUtils.FormatTime(sp.RemainingTicks, world.Timestep);
|
||||
var total = WidgetUtils.FormatTime(sp.Info.ChargeInterval, world.Timestep);
|
||||
timeLabel.Text = $"{remaining} / {total}";
|
||||
timeText = $"{remaining} / {total}";
|
||||
}
|
||||
else
|
||||
timeLabel.Text = customLabel;
|
||||
|
||||
var timeSize = timeFont.Measure(timeLabel.Text);
|
||||
timeLabel.GetText = () => timeText;
|
||||
var timeSize = timeFont.Measure(timeText);
|
||||
var hotkeyWidth = 0;
|
||||
hotkeyLabel.Visible = hotkey.IsValid();
|
||||
if (hotkeyLabel.Visible)
|
||||
@@ -77,7 +78,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var hotkeyText = $"({hotkey.DisplayString()})";
|
||||
|
||||
hotkeyWidth = hotkeyFont.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X;
|
||||
hotkeyLabel.Text = hotkeyText;
|
||||
hotkeyLabel.GetText = () => hotkeyText;
|
||||
hotkeyLabel.Bounds.X = nameSize.X + 2 * nameLabel.Bounds.X;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user