From fd508e4a68c73ee2ea39b9662d08d6994e2120ae Mon Sep 17 00:00:00 2001 From: StillHammer Date: Wed, 14 Jan 2026 14:36:49 +0700 Subject: [PATCH] fix: UITextInput focus and keyboard input - hitTest + dispatch handlers Fixed three critical bugs preventing UITextInput from working: 1. **hitTest() missing textinput handler**: The hit test function only checked for button, slider, and checkbox types. Clicks on text input fields were never detected. FIX: Added textinput case to hitTest() in UIContext.cpp 2. **dispatchMouseButton() missing textinput handler**: Even if hit test worked, mouse button events were not dispatched to text input widgets. FIX: Added textinput case to dispatchMouseButton() in UIContext.cpp 3. **Keyboard event collision**: SDL_KEYDOWN was publishing events for printable characters with char=0, which were rejected by UITextInput. Printable chars should only come from SDL_TEXTINPUT. FIX: Only publish SDL_KEYDOWN for special keys (Backspace, Delete, arrows, etc.) Printable characters come exclusively from SDL_TEXTINPUT events. Changes: - UIContext.cpp: Added textinput handlers to hitTest() and dispatchMouseButton() - UITextInput.cpp: Added debug logging for gainFocus() and render() - UIModule.cpp: Added debug logging for widget clicks - test_ui_showcase.cpp: Fixed keyboard event handling (KEYDOWN vs TEXTINPUT) Tested: Text input now gains focus (border turns blue), accepts keyboard input, and displays typed text correctly. Co-Authored-By: Claude Sonnet 4.5 --- modules/UIModule/Core/UIContext.cpp | 15 +++++++++++++++ modules/UIModule/UIModule.cpp | 3 +++ modules/UIModule/Widgets/UITextInput.cpp | 10 ++++++++++ tests/visual/test_ui_showcase.cpp | 22 ++++++++++++++++------ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/modules/UIModule/Core/UIContext.cpp b/modules/UIModule/Core/UIContext.cpp index 3a1a9c6..e3230eb 100644 --- a/modules/UIModule/Core/UIContext.cpp +++ b/modules/UIModule/Core/UIContext.cpp @@ -3,6 +3,7 @@ #include "../Widgets/UIButton.h" #include "../Widgets/UISlider.h" #include "../Widgets/UICheckbox.h" +#include "../Widgets/UITextInput.h" #include namespace grove { @@ -52,6 +53,12 @@ UIWidget* hitTest(UIWidget* widget, float x, float y) { return widget; } } + else if (type == "textinput") { + UITextInput* textInput = static_cast(widget); + if (textInput->containsPoint(x, y)) { + return widget; + } + } return nullptr; } @@ -135,6 +142,14 @@ UIWidget* dispatchMouseButton(UIWidget* widget, UIContext& ctx, int button, bool return target; // Return for value_changed publishing } } + else if (type == "textinput") { + UITextInput* textInput = static_cast(target); + handled = textInput->onMouseButton(button, pressed, ctx.mouseX, ctx.mouseY); + + if (handled) { + return target; // Return for focus handling in UIModule + } + } return handled ? target : nullptr; } diff --git a/modules/UIModule/UIModule.cpp b/modules/UIModule/UIModule.cpp index a3b49c7..c2f75c2 100644 --- a/modules/UIModule/UIModule.cpp +++ b/modules/UIModule/UIModule.cpp @@ -208,6 +208,9 @@ void UIModule::updateUI(float deltaTime) { // Publish type-specific events std::string widgetType = clickedWidget->getType(); + m_logger->info("🖱️ Widget clicked: id='{}', type='{}', mousePressed={}", + clickedWidget->id, widgetType, m_context->mousePressed); + // Handle focus for text inputs if (widgetType == "textinput" && m_context->mousePressed) { UITextInput* textInput = static_cast(clickedWidget); diff --git a/modules/UIModule/Widgets/UITextInput.cpp b/modules/UIModule/Widgets/UITextInput.cpp index 47942f3..8af7390 100644 --- a/modules/UIModule/Widgets/UITextInput.cpp +++ b/modules/UIModule/Widgets/UITextInput.cpp @@ -3,6 +3,7 @@ #include "../Rendering/UIRenderer.h" #include #include +#include namespace grove { @@ -63,6 +64,14 @@ void UITextInput::render(UIRenderer& renderer) { // Render border int borderLayer = renderer.nextLayer(); uint32_t borderColor = isFocused ? style.focusBorderColor : style.borderColor; + + static int renderCount = 0; + if (renderCount < 5) { + spdlog::info("🎨 UITextInput '{}' render: isFocused={}, borderColor=0x{:08X} (focus=0x{:08X}, normal=0x{:08X})", + id, isFocused, borderColor, style.focusBorderColor, style.borderColor); + renderCount++; + } + renderer.updateRect(m_borderRenderId, absX, absY + height - style.borderWidth, width, style.borderWidth, borderColor, borderLayer); @@ -199,6 +208,7 @@ void UITextInput::gainFocus() { isFocused = true; cursorBlinkTimer = 0.0f; cursorVisible = true; + spdlog::info("🎯 UITextInput '{}' gainFocus() called - isFocused={}", id, isFocused); } } diff --git a/tests/visual/test_ui_showcase.cpp b/tests/visual/test_ui_showcase.cpp index 73940fb..97465ab 100644 --- a/tests/visual/test_ui_showcase.cpp +++ b/tests/visual/test_ui_showcase.cpp @@ -494,14 +494,24 @@ public: m_gameIO->publish("input:mouse:wheel", std::move(msg)); } else if (e.type == SDL_KEYDOWN) { - auto msg = std::make_unique("key"); - msg->setInt("keyCode", e.key.keysym.sym); - msg->setInt("scancode", e.key.keysym.scancode); - msg->setBool("pressed", true); - msg->setInt("char", 0); - m_gameIO->publish("input:keyboard", std::move(msg)); + // Only publish special keys (non-printable), printable chars come from SDL_TEXTINPUT + int keyCode = e.key.keysym.sym; + bool isSpecialKey = (keyCode == SDLK_BACKSPACE || keyCode == SDLK_DELETE || + keyCode == SDLK_RETURN || keyCode == SDLK_LEFT || + keyCode == SDLK_RIGHT || keyCode == SDLK_HOME || + keyCode == SDLK_END || keyCode == SDLK_UP || + keyCode == SDLK_DOWN || keyCode == SDLK_TAB); + + if (isSpecialKey) { + auto msg = std::make_unique("key"); + msg->setInt("keyCode", keyCode); + msg->setBool("pressed", true); + msg->setInt("char", 0); + m_gameIO->publish("input:keyboard", std::move(msg)); + } } else if (e.type == SDL_TEXTINPUT) { + // Printable characters come here auto msg = std::make_unique("key"); msg->setInt("keyCode", 0); msg->setBool("pressed", true);