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 <noreply@anthropic.com>
This commit is contained in:
StillHammer 2026-01-14 14:36:49 +07:00
parent 63751d6f91
commit fd508e4a68
4 changed files with 44 additions and 6 deletions

View File

@ -3,6 +3,7 @@
#include "../Widgets/UIButton.h" #include "../Widgets/UIButton.h"
#include "../Widgets/UISlider.h" #include "../Widgets/UISlider.h"
#include "../Widgets/UICheckbox.h" #include "../Widgets/UICheckbox.h"
#include "../Widgets/UITextInput.h"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
namespace grove { namespace grove {
@ -52,6 +53,12 @@ UIWidget* hitTest(UIWidget* widget, float x, float y) {
return widget; return widget;
} }
} }
else if (type == "textinput") {
UITextInput* textInput = static_cast<UITextInput*>(widget);
if (textInput->containsPoint(x, y)) {
return widget;
}
}
return nullptr; return nullptr;
} }
@ -135,6 +142,14 @@ UIWidget* dispatchMouseButton(UIWidget* widget, UIContext& ctx, int button, bool
return target; // Return for value_changed publishing return target; // Return for value_changed publishing
} }
} }
else if (type == "textinput") {
UITextInput* textInput = static_cast<UITextInput*>(target);
handled = textInput->onMouseButton(button, pressed, ctx.mouseX, ctx.mouseY);
if (handled) {
return target; // Return for focus handling in UIModule
}
}
return handled ? target : nullptr; return handled ? target : nullptr;
} }

View File

@ -208,6 +208,9 @@ void UIModule::updateUI(float deltaTime) {
// Publish type-specific events // Publish type-specific events
std::string widgetType = clickedWidget->getType(); std::string widgetType = clickedWidget->getType();
m_logger->info("🖱️ Widget clicked: id='{}', type='{}', mousePressed={}",
clickedWidget->id, widgetType, m_context->mousePressed);
// Handle focus for text inputs // Handle focus for text inputs
if (widgetType == "textinput" && m_context->mousePressed) { if (widgetType == "textinput" && m_context->mousePressed) {
UITextInput* textInput = static_cast<UITextInput*>(clickedWidget); UITextInput* textInput = static_cast<UITextInput*>(clickedWidget);

View File

@ -3,6 +3,7 @@
#include "../Rendering/UIRenderer.h" #include "../Rendering/UIRenderer.h"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <spdlog/spdlog.h>
namespace grove { namespace grove {
@ -63,6 +64,14 @@ void UITextInput::render(UIRenderer& renderer) {
// Render border // Render border
int borderLayer = renderer.nextLayer(); int borderLayer = renderer.nextLayer();
uint32_t borderColor = isFocused ? style.focusBorderColor : style.borderColor; 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, renderer.updateRect(m_borderRenderId, absX, absY + height - style.borderWidth,
width, style.borderWidth, borderColor, borderLayer); width, style.borderWidth, borderColor, borderLayer);
@ -199,6 +208,7 @@ void UITextInput::gainFocus() {
isFocused = true; isFocused = true;
cursorBlinkTimer = 0.0f; cursorBlinkTimer = 0.0f;
cursorVisible = true; cursorVisible = true;
spdlog::info("🎯 UITextInput '{}' gainFocus() called - isFocused={}", id, isFocused);
} }
} }

View File

@ -494,14 +494,24 @@ public:
m_gameIO->publish("input:mouse:wheel", std::move(msg)); m_gameIO->publish("input:mouse:wheel", std::move(msg));
} }
else if (e.type == SDL_KEYDOWN) { else if (e.type == SDL_KEYDOWN) {
auto msg = std::make_unique<JsonDataNode>("key"); // Only publish special keys (non-printable), printable chars come from SDL_TEXTINPUT
msg->setInt("keyCode", e.key.keysym.sym); int keyCode = e.key.keysym.sym;
msg->setInt("scancode", e.key.keysym.scancode); bool isSpecialKey = (keyCode == SDLK_BACKSPACE || keyCode == SDLK_DELETE ||
msg->setBool("pressed", true); keyCode == SDLK_RETURN || keyCode == SDLK_LEFT ||
msg->setInt("char", 0); keyCode == SDLK_RIGHT || keyCode == SDLK_HOME ||
m_gameIO->publish("input:keyboard", std::move(msg)); keyCode == SDLK_END || keyCode == SDLK_UP ||
keyCode == SDLK_DOWN || keyCode == SDLK_TAB);
if (isSpecialKey) {
auto msg = std::make_unique<JsonDataNode>("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) { else if (e.type == SDL_TEXTINPUT) {
// Printable characters come here
auto msg = std::make_unique<JsonDataNode>("key"); auto msg = std::make_unique<JsonDataNode>("key");
msg->setInt("keyCode", 0); msg->setInt("keyCode", 0);
msg->setBool("pressed", true); msg->setBool("pressed", true);