diff --git a/src/Screens.c b/src/Screens.c index 0fc1fb24f..ce1e130ea 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -405,6 +405,10 @@ static void ChatScreen_UpdateChatYOffsets(struct ChatScreen* s) { Widget_Layout(&s->chat); } +static void ChatScreen_OnInputTextChanged(void* elem) { + ChatScreen_UpdateChatYOffsets(Gui_Chat); +} + static String ChatScreen_GetChat(void* obj, int i) { i += ChatScreen_Instance.chatIndex; @@ -749,7 +753,6 @@ static int ChatScreen_KeyPress(void* screen, char keyChar) { } InputWidget_Append(&s->input.base, keyChar); - ChatScreen_UpdateChatYOffsets(s); return true; } @@ -759,7 +762,6 @@ static int ChatScreen_TextChanged(void* screen, const String* str) { if (!s->grabsInput) return false; InputWidget_SetText(&s->input.base, str); - ChatScreen_UpdateChatYOffsets(s); #endif return true; } @@ -794,7 +796,6 @@ static int ChatScreen_KeyDown(void* screen, int key) { ChatScreen_ScrollChatBy(s, +Gui_Chatlines); } else { Elem_HandlesKeyDown(&s->input.base, key); - ChatScreen_UpdateChatYOffsets(s); } return key < KEY_F1 || key > KEY_F35; } @@ -903,6 +904,7 @@ static int ChatScreen_PointerDown(void* screen, int id, int x, int y) { static void ChatScreen_Init(void* screen) { struct ChatScreen* s = (struct ChatScreen*)screen; ChatInputWidget_Create(&s->input); + s->input.base.OnTextChanged = ChatScreen_OnInputTextChanged; SpecialInputWidget_Create(&s->altText, &s->chatFont, &s->input.base); TextGroupWidget_Create(&s->status, CHAT_MAX_STATUS, @@ -1011,7 +1013,6 @@ void ChatScreen_OpenInput(const String* text) { void ChatScreen_AppendInput(const String* text) { struct ChatScreen* s = &ChatScreen_Instance; InputWidget_AppendText(&s->input.base, text); - ChatScreen_UpdateChatYOffsets(s); } void ChatScreen_SetChatlines(int lines) { diff --git a/src/Widgets.c b/src/Widgets.c index cf4d825d7..503d3d3c0 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -892,8 +892,9 @@ void TableWidget_OnInventoryChanged(struct TableWidget* w) { *#########################################################################################################################*/ static void InputWidget_Reset(struct InputWidget* w) { Widget_Reset(w); - w->caretPos = -1; - w->caretOffset = Display_ScaleY(2); + w->caretPos = -1; + w->caretOffset = Display_ScaleY(2); + w->OnTextChanged = NULL; } static void InputWidget_FormatLine(struct InputWidget* w, int i, String* line) { @@ -1025,6 +1026,7 @@ void InputWidget_Clear(struct InputWidget* w) { w->caretPos = -1; Gfx_DeleteTexture(&w->inputTex.ID); + /* TODO: Maybe call w->OnTextChanged */ } static cc_bool InputWidget_AllowedChar(void* widget, char c) { @@ -1186,12 +1188,14 @@ void InputWidget_UpdateText(struct InputWidget* w) { w->RemakeTexture(w); InputWidget_UpdateCaret(w); Window_SetKeyboardText(&w->text); + if (w->OnTextChanged) w->OnTextChanged(w); } void InputWidget_SetText(struct InputWidget* w, const String* str) { InputWidget_Clear(w); InputWidget_AppendText(w, str); /* If text is empty, InputWidget_UpdateText won't have been called */ + /* TODO: InputWidet_UpdateText should probably always be called... */ if (!w->text.length) Window_SetKeyboardText(&w->text); } diff --git a/src/Widgets.h b/src/Widgets.h index e2c4f1ed6..b23989800 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -107,11 +107,12 @@ struct InputWidget { void (*RemakeTexture)(void* elem); /* Remakes the raw texture containing all the chat lines. Also updates dimensions. */ void (*OnPressedEnter)(void* elem); /* Invoked when the user presses enter. */ cc_bool (*AllowedChar)(void* elem, char c); + void (*OnTextChanged)(void* elem); /* Callback invoked whenever text changes. */ - String text; - String lines[INPUTWIDGET_MAX_LINES]; /* raw text of each line */ - int lineWidths[INPUTWIDGET_MAX_LINES]; /* Width of each line in pixels */ - int lineHeight; + String text; /* The actual raw text */ + String lines[INPUTWIDGET_MAX_LINES]; /* text of each line after word wrapping */ + int lineWidths[INPUTWIDGET_MAX_LINES]; /* Width of each line in pixels */ + int lineHeight; /* Height of a line in pixels */ struct Texture inputTex; int prefixWidth; cc_bool convertPercents;