From ef066560ad289c921794e01d14298e0f936f5d47 Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Fri, 25 Apr 2014 18:11:44 +0300 Subject: [PATCH] Fix text-wrapping special case If a line of text contained a whole word that was longer than the allotted space, it would fail to wrap that line completely, even if it was possible to wrap at other locations. Fixing this uncovered a second issue, where it would drop the last line if the input had more than one lines and one of the first ones was wider than the specified width. --- OpenRA.Game/Exts.cs | 2 +- OpenRA.Game/Widgets/WidgetUtils.cs | 46 +++++++++++------------------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/OpenRA.Game/Exts.cs b/OpenRA.Game/Exts.cs index 8ee1b34341..41d98389b0 100755 --- a/OpenRA.Game/Exts.cs +++ b/OpenRA.Game/Exts.cs @@ -149,7 +149,7 @@ namespace OpenRA public static string JoinWith(this IEnumerable ts, string j) { - return string.Join(j, ts.Select(t => t.ToString()).ToArray()); + return string.Join(j, ts); } public static IEnumerable Append(this IEnumerable ts, params T[] moreTs) diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index 6465b4f3ea..7ab7270407 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -170,52 +170,38 @@ namespace OpenRA.Widgets var textSize = font.Measure(text); if (textSize.X > width) { - var lines = text.Split('\n'); - var newLines = new List(); - var i = 0; - var line = lines[i++]; + var lines = text.Split('\n').ToList(); - for(;;) + for (int i=0; i width) { - if (-1 == (spaceIndex = line.LastIndexOf(' ', start))) + int spaceIndex = line.LastIndexOf(' ', start); + if (spaceIndex == -1) break; + bestSpaceIndex = spaceIndex; + start = spaceIndex - 1; m = font.Measure(line.Substring(0, spaceIndex)); } - if (spaceIndex != -1) + if (bestSpaceIndex != -1) { - newLines.RemoveAt(newLines.Count - 1); - newLines.Add(line.Substring(0, spaceIndex)); - line = line.Substring(spaceIndex + 1); + lines[i] = line.Substring(0, bestSpaceIndex); + lines.Insert(i + 1, line.Substring(bestSpaceIndex + 1)); } - else if (i < lines.Length - 1) - { - line = lines[i++]; - continue; - } - else - break; } - return newLines.JoinWith("\n"); + + return string.Join("\n", lines); } return text; }