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:
parent
63751d6f91
commit
fd508e4a68
@ -3,6 +3,7 @@
|
||||
#include "../Widgets/UIButton.h"
|
||||
#include "../Widgets/UISlider.h"
|
||||
#include "../Widgets/UICheckbox.h"
|
||||
#include "../Widgets/UITextInput.h"
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace grove {
|
||||
@ -52,6 +53,12 @@ UIWidget* hitTest(UIWidget* widget, float x, float y) {
|
||||
return widget;
|
||||
}
|
||||
}
|
||||
else if (type == "textinput") {
|
||||
UITextInput* textInput = static_cast<UITextInput*>(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<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;
|
||||
}
|
||||
|
||||
@ -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<UITextInput*>(clickedWidget);
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "../Rendering/UIRenderer.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -494,14 +494,24 @@ public:
|
||||
m_gameIO->publish("input:mouse:wheel", std::move(msg));
|
||||
}
|
||||
else if (e.type == SDL_KEYDOWN) {
|
||||
// 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<JsonDataNode>("key");
|
||||
msg->setInt("keyCode", e.key.keysym.sym);
|
||||
msg->setInt("scancode", e.key.keysym.scancode);
|
||||
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<JsonDataNode>("key");
|
||||
msg->setInt("keyCode", 0);
|
||||
msg->setBool("pressed", true);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user