From b7773d2b4c2642345b9da2acff90521f0bbcd03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 29 Aug 2023 18:01:09 +0200 Subject: [PATCH] Fix lineinput selection getting desynchronized from cursor When the text cursor/selection mode is set to calculate, values of `-1` are used when the selection is empty. These values were not being handled anymore due to a regression from #7028. This was causing the selection to be set to the last position instead, which was causing text to subsequently be inserted there instead of at the cursor position. An assertion is added to ensure that the selection cannot be desynchronized from the cursor position anymore. Closes #7099. --- src/game/client/lineinput.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/game/client/lineinput.cpp b/src/game/client/lineinput.cpp index 9f7d3e050..ac44c8574 100644 --- a/src/game/client/lineinput.cpp +++ b/src/game/client/lineinput.cpp @@ -159,6 +159,7 @@ void CLineInput::SetCursorOffset(size_t Offset) void CLineInput::SetSelection(size_t Start, size_t End) { + dbg_assert(m_CursorPos == Start || m_CursorPos == End, "Selection and cursor offset got desynchronized"); if(Start > End) std::swap(Start, End); m_SelectionStart = clamp(Start, 0, m_Len); @@ -472,12 +473,12 @@ STextBoundingBox CLineInput::Render(const CUIRect *pRect, float FontSize, int Al TextRender()->TextEx(&Cursor, pDisplayStr); } - if(Cursor.m_CursorMode == TEXT_CURSOR_CURSOR_MODE_CALCULATE) + if(Cursor.m_CursorMode == TEXT_CURSOR_CURSOR_MODE_CALCULATE && Cursor.m_CursorCharacter >= 0) { const size_t NewCursorOffset = str_utf8_offset_chars_to_bytes(pDisplayStr, Cursor.m_CursorCharacter); SetCursorOffset(OffsetFromDisplayToActual(NewCursorOffset)); } - if(Cursor.m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_CALCULATE) + if(Cursor.m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_CALCULATE && Cursor.m_SelectionStart >= 0 && Cursor.m_SelectionEnd >= 0) { const size_t NewSelectionStart = str_utf8_offset_chars_to_bytes(pDisplayStr, Cursor.m_SelectionStart); const size_t NewSelectionEnd = str_utf8_offset_chars_to_bytes(pDisplayStr, Cursor.m_SelectionEnd);