Skip to content

Commit a7e79b0

Browse files
Merge pull request #10 from CSGALS/fix-newlines
Fixed newlines in strings causing rendering bugs
2 parents 3098389 + 9134f96 commit a7e79b0

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

src/SharpModMenu/PlayerMenuState.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ public bool DrawActiveMenu()
410410
BackgroundSb.Clear();
411411

412412
bool firstLine = true;
413-
int linesWrote = 0;
414413
void writeLine(string text, TextStyling style, int? selectionIndex)
415414
{
416415
if (firstLine)
@@ -423,23 +422,30 @@ void writeLine(string text, TextStyling style, int? selectionIndex)
423422
BackgroundSb.AppendLine();
424423
}
425424

426-
StringBuilder sb = style.Foreground ? ForegroundTextSb : BackgroundTextSb;
425+
int newlineCount = 0;
426+
for (int i = 0; i < text.Length; i++)
427+
if (text[i] == '\n')
428+
newlineCount++;
429+
var syncNewlines = newlineCount == 0 ? string.Empty : new string('\n', newlineCount);
427430

428-
if (selectionIndex.HasValue)
429-
{
430-
sb.Append($"{selectionIndex}. ");
431-
BackgroundSb.Append($"{selectionIndex}. ");
432-
433-
if (style.Highlight)
434-
HighlightTextSb.Append($"{selectionIndex}. ");
435-
}
436-
sb.Append(text);
437-
BackgroundSb.Append(text);
431+
var sb = style.Foreground ? ForegroundTextSb : BackgroundTextSb;
432+
var formattedLine = text;
438433

434+
if (selectionIndex.HasValue)
435+
formattedLine = $"{selectionIndex}. {formattedLine}";
436+
437+
sb.Append(formattedLine);
438+
BackgroundSb.Append(formattedLine);
439439
if (style.Highlight)
440-
HighlightTextSb.Append(text);
441-
442-
linesWrote++;
440+
HighlightTextSb.Append(formattedLine);
441+
442+
// keep newlines in sync with the other entities
443+
if (sb == ForegroundTextSb)
444+
BackgroundTextSb.Append(syncNewlines);
445+
else if (sb == BackgroundTextSb)
446+
ForegroundTextSb.Append(syncNewlines);
447+
if (!style.Highlight)
448+
HighlightTextSb.Append(syncNewlines);
443449
}
444450

445451
BuildMenuStrings(CurrentMenu, writeLine);
@@ -491,6 +497,9 @@ void writeLine(string text, TextStyling style, int? selectIndex)
491497
if (string.IsNullOrEmpty(text))
492498
return;
493499

500+
if (text.IndexOf('\n') >= 0)
501+
text = text.Replace("\n", "<br>");
502+
494503
if (firstLine)
495504
firstLine = false;
496505
else

0 commit comments

Comments
 (0)