fix(camera): reset camera on resize events and fix input handling on imgui toggle

- Add Camera::player() accessor.
- Handle WindowResizeEvent and FrameBufferResizeEvent to reset camera.
- Fix escape key handling to disable mouse before toggling imgui.
- Fix left alt handling to toggle mouse state correctly.
- Reset player key status when toggling imgui.
This commit is contained in:
2026-07-17 12:39:30 +08:00
parent 15a9a749da
commit ec9e89886e
4 changed files with 39 additions and 28 deletions

View File

@@ -49,6 +49,7 @@ public:
void change_perspective();
bool is_first_person() const;
bool handle_event(const Event& e);
ClientPlayer* player();
};
} // namespace Cubed

View File

@@ -645,9 +645,10 @@ void App::handle_sdl_event(SDL_Event& e) {
break;
case SDL_EVENT_KEY_DOWN:
if (imgui_enable && e.key.key == SDLK_ESCAPE) {
m_window.disable_mouse();
m_window.set_imgui_enabled(false);
} else {
}
if (!imgui_enable) {
handle_sdl_key(e);
}
break;

View File

@@ -107,10 +107,9 @@ void Camera::change_perspective() {
bool Camera::is_first_person() const {
return m_perspective == Perspective::FIRST_PERSON;
}
ClientPlayer* Camera::player() { return m_player; }
bool Camera::handle_event(const Event& e) {
return std::visit(
Overloaded{[this](const MouseMoveEvent& e) {
return std::visit(Overloaded{[this](const MouseMoveEvent& e) {
if (handle_mouse_move_event(e)) {
return true;
}
@@ -126,8 +125,14 @@ bool Camera::handle_event(const Event& e) {
return false;
},
[](const TextInputEvent&) { return false; },
[](const WindowResizeEvent&) { return false; },
[](const FrameBufferResizeEvent&) { return false; }
[this](const WindowResizeEvent&) {
reset_camera();
return false;
},
[this](const FrameBufferResizeEvent&) {
reset_camera();
return false;
}
} // namespace Cubed
,

View File

@@ -1,6 +1,7 @@
#include "Cubed/window.hpp"
#include "Cubed/camera.hpp"
#include "Cubed/gameplay/client_player.hpp"
#include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/font.hpp"
#include "Cubed/tools/log.hpp"
@@ -76,8 +77,10 @@ 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,10 +228,11 @@ bool Window::is_enable_imgui() const { return m_imgui_enable; }
void Window::set_imgui_enabled(bool enable) {
m_imgui_enable = enable;
if (!enable) {
disable_mouse();
} else {
enable_mouse();
if (m_camera) {
auto player = m_camera->player();
if (player) {
player->reset_key_status();
}
}
}