refactor(core): add event system and scene management

- Introduce Event variant and Overloaded helper for input handling
- Move game state (camera, client_world, dev_panel) from App to WorldScene
- Add SceneManager with push/pop/change operations for scene stack
- Refactor input callbacks to dispatch events through scene hierarchy
- Extract Argument struct to separate header
- Update Renderer and WorldRenderer to accept world reference
- Replace global input state with event-driven processing in ClientPlayer, Camera, etc.
This commit is contained in:
2026-07-12 16:05:59 +08:00
parent 2d28488126
commit 5712ba0600
38 changed files with 1383 additions and 346 deletions

View File

@@ -1,5 +1,6 @@
#include "Cubed/window.hpp"
#include "Cubed/camera.hpp"
#include "Cubed/render/renderer.hpp"
#include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/font.hpp"
@@ -51,6 +52,52 @@ void Window::update_viewport() {
m_config.set("window.height", windowed_height);
}
bool Window::handle_event(const Event& e) {
return std::visit(Overloaded{[](const MouseMoveEvent& e) { return false; },
[this](const MouseButtonEvent& e) {
if (handle_mouse_button_event(e)) {
return true;
}
return false;
},
[](const MouseWheelEvent& e) { return false; },
[this](const KeyEvent& e) {
if (handle_key_event(e)) {
return true;
}
return false;
},
[](const TextInputEvent& e) { return false; }},
e);
}
bool Window::handle_key_event(const KeyEvent& e) {
if (e.key == Key::F11 && e.action == KeyAction::PRESS) {
toggle_fullscreen();
return true;
}
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
glfwSetWindowShouldClose(m_window, GLFW_TRUE);
return true;
}
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
toggle_mouse_able();
return true;
}
return false;
}
bool Window::handle_mouse_button_event(const MouseButtonEvent& e) {
if (e.key == MouseKey::LEFT_BUTTON && e.action == KeyAction::PRESS) {
if (is_mouse_enable()) {
toggle_mouse_able();
return true;
}
}
return false;
}
void Window::init() {
if (!glfwInit()) {
Logger::error("glfw init fail");
@@ -166,8 +213,13 @@ void Window::toggle_mouse_able() {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
m_mouse_enable = true;
}
}
if (m_camera) {
m_camera->reset_camera();
}
}
void Window::set_camera(Camera* camera) { m_camera = camera; }
Camera* Window::camera() { return m_camera; }
void Window::imgui_init() {
float dpi_scale_x, dpi_scale_y;
glfwGetWindowContentScale(m_window, &dpi_scale_x, &dpi_scale_y);