From 38bc79b4322cfdbfb3166810f96c6f4f0f2b6cc8 Mon Sep 17 00:00:00 2001 From: segcore <44293604+segcore@users.noreply.github.com> Date: Mon, 23 Sep 2024 02:25:21 +0800 Subject: [PATCH] GuiTextBox(): Do not get previous code point when at start of string (#420) Co-authored-by: segcore --- src/raygui.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/raygui.h b/src/raygui.h index 5e66896..24a34d2 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -2611,16 +2611,17 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode) if (IsKeyPressed(KEY_BACKSPACE) || (autoCursorDelayCounter%RAYGUI_TEXTBOX_AUTO_CURSOR_DELAY) == 0) // Delay every movement some frames { int prevCodepointSize = 0; - GetCodepointPrevious(text + textBoxCursorIndex, &prevCodepointSize); - - // Move backward text from cursor position - for (int i = (textBoxCursorIndex - prevCodepointSize); i < textLength; i++) text[i] = text[i + prevCodepointSize]; - - // TODO Check: >= cursor+codepointsize and <= length-codepointsize // Prevent cursor index from decrementing past 0 if (textBoxCursorIndex > 0) { + GetCodepointPrevious(text + textBoxCursorIndex, &prevCodepointSize); + + // Move backward text from cursor position + for (int i = (textBoxCursorIndex - prevCodepointSize); i < textLength; i++) text[i] = text[i + prevCodepointSize]; + + // TODO Check: >= cursor+codepointsize and <= length-codepointsize + textBoxCursorIndex -= codepointSize; textLength -= codepointSize; } @@ -2638,7 +2639,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode) if (IsKeyPressed(KEY_LEFT) || (autoCursorDelayCounter%RAYGUI_TEXTBOX_AUTO_CURSOR_DELAY) == 0) // Delay every movement some frames { int prevCodepointSize = 0; - GetCodepointPrevious(text + textBoxCursorIndex, &prevCodepointSize); + if (textBoxCursorIndex > 0) GetCodepointPrevious(text + textBoxCursorIndex, &prevCodepointSize); if (textBoxCursorIndex >= prevCodepointSize) textBoxCursorIndex -= prevCodepointSize; }