mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor: add framebuffer resize event and separate UI management
This commit is contained in:
@@ -1,7 +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"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
@@ -15,8 +14,7 @@ namespace Cubed {
|
||||
static int windowed_xpos = 0, windowed_ypos = 0;
|
||||
static int windowed_width = 800, windowed_height = 600;
|
||||
|
||||
Window::Window(Renderer& renderer, Config& config)
|
||||
: m_renderer(renderer), m_config(config) {}
|
||||
Window::Window(Config& config) : m_config(config) {}
|
||||
|
||||
Window::~Window() {
|
||||
if (m_imgui_init) {
|
||||
@@ -42,33 +40,31 @@ const GLFWwindow* Window::get_glfw_window() const { return m_window; }
|
||||
|
||||
GLFWwindow* Window::get_glfw_window() { return m_window; }
|
||||
|
||||
void Window::update_viewport() {
|
||||
glfwGetFramebufferSize(m_window, &m_width, &m_height);
|
||||
m_aspect = (float)m_width / (float)m_height;
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
m_renderer.update_proj_matrix(m_aspect, m_width, m_height);
|
||||
m_renderer.updata_framebuffer(m_width, m_height);
|
||||
m_config.set("window.width", windowed_width);
|
||||
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);
|
||||
return std::visit(
|
||||
Overloaded{[](const MouseMoveEvent&) { return false; },
|
||||
[this](const MouseButtonEvent& e) {
|
||||
if (handle_mouse_button_event(e)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
[](const MouseWheelEvent&) { return false; },
|
||||
[this](const KeyEvent& e) {
|
||||
if (handle_key_event(e)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
[](const TextInputEvent&) { return false; },
|
||||
[this](const WindowResizeEvent& e) {
|
||||
handle_window_resize_event(e);
|
||||
return false;
|
||||
},
|
||||
[](const FrameBufferResizeEvent&) { return false; }
|
||||
|
||||
},
|
||||
e);
|
||||
}
|
||||
|
||||
bool Window::handle_key_event(const KeyEvent& e) {
|
||||
@@ -88,6 +84,12 @@ bool Window::handle_key_event(const KeyEvent& e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Window::handle_window_resize_event(const WindowResizeEvent& e) {
|
||||
m_window_height = e.height;
|
||||
m_window_width = e.width;
|
||||
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()) {
|
||||
@@ -107,15 +109,18 @@ void Window::init() {
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
|
||||
m_width = m_config.get("window.width", 800);
|
||||
m_height = m_config.get("window.height", 600);
|
||||
|
||||
m_window_width = m_config.get("window.width", 800);
|
||||
m_window_height = m_config.get("window.height", 600);
|
||||
|
||||
if (m_config.get("window.fullscreen", false)) {
|
||||
GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor();
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(primary_monitor);
|
||||
m_window = glfwCreateWindow(mode->width, mode->height, "Cubed",
|
||||
primary_monitor, NULL);
|
||||
} else {
|
||||
m_window = glfwCreateWindow(m_width, m_height, "Cubed", NULL, NULL);
|
||||
m_window = glfwCreateWindow(m_window_width, m_window_height, "Cubed",
|
||||
NULL, NULL);
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(m_window);
|
||||
@@ -168,12 +173,13 @@ void Window::hot_reload() {
|
||||
Logger::error("Can't Find Monitor");
|
||||
}
|
||||
}
|
||||
update_viewport();
|
||||
if (!m_mouse_enable) {
|
||||
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
} else {
|
||||
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
}
|
||||
m_config.set("window.width", windowed_width);
|
||||
m_config.set("window.height", windowed_height);
|
||||
}
|
||||
|
||||
void Window::toggle_fullscreen() {
|
||||
@@ -195,7 +201,8 @@ void Window::toggle_fullscreen() {
|
||||
GL_DONT_CARE);
|
||||
m_config.set("window.fullscreen", true);
|
||||
}
|
||||
update_viewport();
|
||||
m_config.set("window.width", windowed_width);
|
||||
m_config.set("window.height", windowed_height);
|
||||
}
|
||||
|
||||
void Window::toggle_mouse_able() {
|
||||
|
||||
Reference in New Issue
Block a user