refactor(event-handling): guard input events with imgui state

Move mouse enable/disable logic into set_imgui_enabled and check imgui_enable before forwarding non-ImGui events to handlers. This prevents duplicate processing and ensures proper mouse state when toggling ImGui.
This commit is contained in:
2026-07-17 12:00:38 +08:00
parent 7461dd766e
commit 15a9a749da
2 changed files with 49 additions and 19 deletions

View File

@@ -634,7 +634,8 @@ void App::update() {
} }
void App::handle_sdl_event(SDL_Event& e) { void App::handle_sdl_event(SDL_Event& e) {
if (m_window.is_enable_imgui()) { bool imgui_enable = m_window.is_enable_imgui();
if (imgui_enable) {
ImGui_ImplSDL3_ProcessEvent(&e); ImGui_ImplSDL3_ProcessEvent(&e);
} }
@@ -643,32 +644,53 @@ void App::handle_sdl_event(SDL_Event& e) {
m_running = false; m_running = false;
break; break;
case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP: if (imgui_enable && e.key.key == SDLK_ESCAPE) {
m_window.set_imgui_enabled(false);
} else {
handle_sdl_key(e); handle_sdl_key(e);
}
break;
case SDL_EVENT_KEY_UP:
if (!imgui_enable) {
handle_sdl_key(e);
}
break; break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP: case SDL_EVENT_MOUSE_BUTTON_UP:
if (!imgui_enable) {
handle_sdl_mouse_button(e); handle_sdl_mouse_button(e);
}
break; break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel); if (!imgui_enable) {
handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel,
e.motion.yrel);
}
break; break;
case SDL_EVENT_WINDOW_RESIZED: case SDL_EVENT_WINDOW_RESIZED:
handle_window_resize(e.window.data1, e.window.data2); handle_window_resize(e.window.data1, e.window.data2);
break; break;
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
handle_framebuffer_resize(e.window.data1, e.window.data2); handle_framebuffer_resize(e.window.data1, e.window.data2);
break; break;
case SDL_EVENT_MOUSE_WHEEL: { case SDL_EVENT_MOUSE_WHEEL:
if (!imgui_enable) {
float scroll_x = e.wheel.x; float scroll_x = e.wheel.x;
float scroll_y = e.wheel.y; float scroll_y = e.wheel.y;
if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) { if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
scroll_y = -scroll_y; scroll_y = -scroll_y;
} }
handle_mouse_scroll(scroll_x, scroll_y); handle_mouse_scroll(scroll_x, scroll_y);
break;
} }
break;
case SDL_EVENT_WINDOW_FOCUS_GAINED: case SDL_EVENT_WINDOW_FOCUS_GAINED:
handle_window_focus(true); handle_window_focus(true);
break; break;
@@ -676,7 +698,10 @@ void App::handle_sdl_event(SDL_Event& e) {
handle_window_focus(false); handle_window_focus(false);
break; break;
case SDL_EVENT_TEXT_INPUT: case SDL_EVENT_TEXT_INPUT:
if (!imgui_enable) {
handle_text_input(e.text.text); handle_text_input(e.text.text);
}
break; break;
} }
} }

View File

@@ -76,10 +76,8 @@ bool Window::handle_key_event(const KeyEvent& e) {
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) { if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
if (m_game_running) { if (m_game_running) {
if (m_mouse_enable) { if (m_mouse_enable) {
disable_mouse();
set_imgui_enabled(false); set_imgui_enabled(false);
} else { } else {
enable_mouse();
set_imgui_enabled(true); set_imgui_enabled(true);
} }
return true; return true;
@@ -225,7 +223,14 @@ void Window::should_close_window() {
bool Window::is_enable_imgui() const { return m_imgui_enable; } bool Window::is_enable_imgui() const { return m_imgui_enable; }
void Window::set_imgui_enabled(bool enable) { m_imgui_enable = enable; } void Window::set_imgui_enabled(bool enable) {
m_imgui_enable = enable;
if (!enable) {
disable_mouse();
} else {
enable_mouse();
}
}
void Window::set_vsync(bool enable) { void Window::set_vsync(bool enable) {
if (!SDL_GL_SetSwapInterval(static_cast<int>(enable))) { if (!SDL_GL_SetSwapInterval(static_cast<int>(enable))) {