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.
This commit is contained in:
Robert Müller 2023-08-29 18:01:09 +02:00
parent 812231449b
commit b7773d2b4c

View file

@ -159,6 +159,7 @@ void CLineInput::SetCursorOffset(size_t Offset)
void CLineInput::SetSelection(size_t Start, size_t End) 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) if(Start > End)
std::swap(Start, End); std::swap(Start, End);
m_SelectionStart = clamp<size_t>(Start, 0, m_Len); m_SelectionStart = clamp<size_t>(Start, 0, m_Len);
@ -472,12 +473,12 @@ STextBoundingBox CLineInput::Render(const CUIRect *pRect, float FontSize, int Al
TextRender()->TextEx(&Cursor, pDisplayStr); 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); const size_t NewCursorOffset = str_utf8_offset_chars_to_bytes(pDisplayStr, Cursor.m_CursorCharacter);
SetCursorOffset(OffsetFromDisplayToActual(NewCursorOffset)); 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 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); const size_t NewSelectionEnd = str_utf8_offset_chars_to_bytes(pDisplayStr, Cursor.m_SelectionEnd);