Fix LabelWidget positioning of text when WordWrap is true.

If WordWrap is enabled, the wrapping must be applied before any TextAlign is applied, or the final position will be incorrect.

Introduce a IncreaseHeightToFitCurrentText to make such WordWrap labels easier to use, and apply to CreditsLogic.
This commit is contained in:
RoosterDragon
2024-09-23 19:53:30 +01:00
committed by Gustas
parent 86b9227577
commit d1583e8587
2 changed files with 24 additions and 19 deletions

View File

@@ -68,6 +68,17 @@ namespace OpenRA.Mods.Common.Widgets
GetContrastColorLight = other.GetContrastColorLight;
}
public void IncreaseHeightToFitCurrentText()
{
if (!Game.Renderer.Fonts.TryGetValue(Font, out var font))
throw new ArgumentException($"Requested font '{Font}' was not found.");
var line = GetText();
if (WordWrap)
line = WidgetUtils.WrapText(line, Bounds.Width, font);
Bounds.Height = Math.Max(Bounds.Height, font.Measure(line).Y);
}
public override void Draw()
{
if (!Game.Renderer.Fonts.TryGetValue(Font, out var font))
@@ -77,6 +88,9 @@ namespace OpenRA.Mods.Common.Widgets
if (text == null)
return;
if (WordWrap)
text = WidgetUtils.WrapText(text, Bounds.Width, font);
var textSize = font.Measure(text);
var position = RenderOrigin;
var offset = font.TopOffset;
@@ -96,9 +110,6 @@ namespace OpenRA.Mods.Common.Widgets
if (Align == TextAlign.Right)
position += new int2(Bounds.Width - textSize.X, 0);
if (WordWrap)
text = WidgetUtils.WrapText(text, Bounds.Width, font);
DrawInner(text, font, GetColor(), position);
}