From ad01e2e28eefaf2a877339eccd74cca8de4ddf50 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 24 Oct 2020 00:12:16 +1100 Subject: [PATCH] Android: Fix if you touched on a text input widget to move the caret to somewhere in the text, then press backspace multiple times, the game crashed The soft keyboard actually ignores the caret you see in the text input widget, but that's another story --- src/LWidgets.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/LWidgets.c b/src/LWidgets.c index 5926261d9..061aa6449 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -501,6 +501,11 @@ void LInput_Init(struct LScreen* s, struct LInput* w, int width, const char* hin s->widgets[s->numWidgets++] = (struct LWidget*)w; } +/* If caret position is now beyond end of text, resets to -1 */ +static CC_INLINE void LInput_ClampCaret(struct LInput* w) { + if (w->caretPos >= w->text.length) w->caretPos = -1; +} + void LInput_SetText(struct LInput* w, const cc_string* text_) { cc_string text; char textBuffer[STRING_SIZE]; struct DrawTextArgs args; @@ -514,6 +519,7 @@ void LInput_SetText(struct LInput* w, const cc_string* text_) { textWidth = Drawer2D_TextWidth(&args); w->width = max(w->minWidth, textWidth + inputExpand); w->_textHeight = Drawer2D_TextHeight(&args); + LInput_ClampCaret(w); } static CC_NOINLINE cc_bool LInput_AppendRaw(struct LInput* w, char c) { @@ -557,7 +563,7 @@ void LInput_Backspace(struct LInput* w) { } if (w->TextChanged) w->TextChanged(w); - if (w->caretPos >= w->text.length) w->caretPos = -1; + LInput_ClampCaret(w); LWidget_Redraw(w); } @@ -568,7 +574,7 @@ void LInput_Delete(struct LInput* w) { if (w->caretPos == -1) w->caretPos = 0; if (w->TextChanged) w->TextChanged(w); - if (w->caretPos >= w->text.length) w->caretPos = -1; + LInput_ClampCaret(w); LWidget_Redraw(w); }