mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
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:
35
src/app.cpp
35
src/app.cpp
@@ -634,7 +634,8 @@ void App::update() {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -643,32 +644,53 @@ void App::handle_sdl_event(SDL_Event& e) {
|
||||
m_running = false;
|
||||
break;
|
||||
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);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_KEY_UP:
|
||||
if (!imgui_enable) {
|
||||
handle_sdl_key(e);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
if (!imgui_enable) {
|
||||
handle_sdl_mouse_button(e);
|
||||
}
|
||||
|
||||
break;
|
||||
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;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
|
||||
handle_window_resize(e.window.data1, e.window.data2);
|
||||
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
|
||||
|
||||
handle_framebuffer_resize(e.window.data1, e.window.data2);
|
||||
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_WHEEL: {
|
||||
case SDL_EVENT_MOUSE_WHEEL:
|
||||
if (!imgui_enable) {
|
||||
float scroll_x = e.wheel.x;
|
||||
float scroll_y = e.wheel.y;
|
||||
if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
|
||||
scroll_y = -scroll_y;
|
||||
}
|
||||
handle_mouse_scroll(scroll_x, scroll_y);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
||||
handle_window_focus(true);
|
||||
break;
|
||||
@@ -676,7 +698,10 @@ void App::handle_sdl_event(SDL_Event& e) {
|
||||
handle_window_focus(false);
|
||||
break;
|
||||
case SDL_EVENT_TEXT_INPUT:
|
||||
if (!imgui_enable) {
|
||||
handle_text_input(e.text.text);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,10 +76,8 @@ bool Window::handle_key_event(const KeyEvent& e) {
|
||||
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
|
||||
if (m_game_running) {
|
||||
if (m_mouse_enable) {
|
||||
disable_mouse();
|
||||
set_imgui_enabled(false);
|
||||
} else {
|
||||
enable_mouse();
|
||||
set_imgui_enabled(true);
|
||||
}
|
||||
return true;
|
||||
@@ -225,7 +223,14 @@ void Window::should_close_window() {
|
||||
|
||||
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) {
|
||||
if (!SDL_GL_SetSwapInterval(static_cast<int>(enable))) {
|
||||
|
||||
Reference in New Issue
Block a user