Skip to content

Commit 0d7fcc1

Browse files
committed
Fix index out-of-range exception in TextBox highlighting
If the text is updated externally or if a wrapping mode/format change results in a layout change, the highlight range could become invalid.
1 parent b4aafb7 commit 0d7fcc1

File tree

1 file changed

+14
-2
lines changed
  • TextEditorExample/Data/Scripts/TextEditorExample/RichHudFramework/Shared/UI/HUD/HudElements/ClickableHudElements

1 file changed

+14
-2
lines changed

TextEditorExample/Data/Scripts/TextEditorExample/RichHudFramework/Shared/UI/HUD/HudElements/ClickableHudElements/TextBox.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,20 @@ private void UpdateHighlight()
760760
highlightStale = false;
761761
highlightList.Clear();
762762

763-
int startLine = Math.Max(Start.X, text.VisibleLineRange.X),
764-
endLine = Math.Min(End.X, text.VisibleLineRange.Y);
763+
// Clamp line range
764+
Vector2I lineRange = text.VisibleLineRange;
765+
Start = new Vector2I(MathHelper.Clamp(Start.X, 0, text.Count - 1), Start.Y);
766+
End = new Vector2I(MathHelper.Clamp(End.X, 0, text.Count - 1), End.Y);
767+
768+
// Clamp char range
769+
if (text.Count > 0)
770+
{
771+
Start = new Vector2I(Start.X, MathHelper.Clamp(Start.Y, 0, text[Start.X].Count - 1));
772+
End = new Vector2I(End.X, MathHelper.Clamp(End.Y, 0, text[End.X].Count - 1));
773+
}
774+
775+
int startLine = Math.Max(Start.X, lineRange.X),
776+
endLine = Math.Min(End.X, lineRange.Y);
765777

766778
// Add start and end
767779
if (Start.X == End.X && Start.X == startLine)

0 commit comments

Comments
 (0)