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);
}

View File

@@ -10,9 +10,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
@@ -25,8 +23,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly bool showModTab;
readonly bool showEngineTab;
bool isShowingModTab;
readonly IEnumerable<string> modLines;
readonly IEnumerable<string> engineLines;
readonly string modLines;
readonly string engineLines;
[ObjectCreator.UseCtor]
public CreditsLogic(Widget widget, ModData modData, Action onExit)
@@ -83,21 +81,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic
scrollPanel.RemoveChildren();
var font = Game.Renderer.Fonts[template.Font];
foreach (var line in modCredits ? modLines : engineLines)
{
var label = (LabelWidget)template.Clone();
label.GetText = () => line;
var wrappedLine = line;
if (label.WordWrap)
wrappedLine = WidgetUtils.WrapText(line, label.Bounds.Width, font);
label.Bounds.Height = Math.Max(label.Bounds.Height, font.Measure(wrappedLine).Y);
scrollPanel.AddChild(label);
}
var lines = modCredits ? modLines : engineLines;
var label = (LabelWidget)template.Clone();
label.GetText = () => lines;
label.IncreaseHeightToFitCurrentText();
scrollPanel.AddChild(label);
}
static IEnumerable<string> ParseLines(Stream file)
static string ParseLines(Stream file)
{
return file.ReadAllLines().Select(l => l.Replace("\t", " ").Replace("*", "\u2022")).ToList();
return file.ReadAllText().Replace(Environment.NewLine, "\n").Replace("\t", " ").Replace("*", "\u2022");
}
}
}