From d1583e85877e1ad2416a3308d16bcf3eb7771f7a Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 23 Sep 2024 19:53:30 +0100 Subject: [PATCH] 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. --- OpenRA.Mods.Common/Widgets/LabelWidget.cs | 17 +++++++++--- .../Widgets/Logic/CreditsLogic.cs | 26 +++++++------------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/LabelWidget.cs b/OpenRA.Mods.Common/Widgets/LabelWidget.cs index 0d74011d05..49b0c2ae3f 100644 --- a/OpenRA.Mods.Common/Widgets/LabelWidget.cs +++ b/OpenRA.Mods.Common/Widgets/LabelWidget.cs @@ -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); } diff --git a/OpenRA.Mods.Common/Widgets/Logic/CreditsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/CreditsLogic.cs index d1467891a4..1dcdfcb02f 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/CreditsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/CreditsLogic.cs @@ -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 modLines; - readonly IEnumerable 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 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"); } } }