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
This commit is contained in:
UnknownShadow200 2020-10-24 00:12:16 +11:00
parent 64da5c6909
commit ad01e2e28e

View File

@ -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; 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_) { void LInput_SetText(struct LInput* w, const cc_string* text_) {
cc_string text; char textBuffer[STRING_SIZE]; cc_string text; char textBuffer[STRING_SIZE];
struct DrawTextArgs args; struct DrawTextArgs args;
@ -514,6 +519,7 @@ void LInput_SetText(struct LInput* w, const cc_string* text_) {
textWidth = Drawer2D_TextWidth(&args); textWidth = Drawer2D_TextWidth(&args);
w->width = max(w->minWidth, textWidth + inputExpand); w->width = max(w->minWidth, textWidth + inputExpand);
w->_textHeight = Drawer2D_TextHeight(&args); w->_textHeight = Drawer2D_TextHeight(&args);
LInput_ClampCaret(w);
} }
static CC_NOINLINE cc_bool LInput_AppendRaw(struct LInput* w, char c) { 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->TextChanged) w->TextChanged(w);
if (w->caretPos >= w->text.length) w->caretPos = -1; LInput_ClampCaret(w);
LWidget_Redraw(w); LWidget_Redraw(w);
} }
@ -568,7 +574,7 @@ void LInput_Delete(struct LInput* w) {
if (w->caretPos == -1) w->caretPos = 0; if (w->caretPos == -1) w->caretPos = 0;
if (w->TextChanged) w->TextChanged(w); if (w->TextChanged) w->TextChanged(w);
if (w->caretPos >= w->text.length) w->caretPos = -1; LInput_ClampCaret(w);
LWidget_Redraw(w); LWidget_Redraw(w);
} }