refactor: add framebuffer resize event and separate UI management

This commit is contained in:
2026-07-12 18:38:42 +08:00
parent 5712ba0600
commit cb1eb25e7b
20 changed files with 331 additions and 132 deletions

View File

@@ -25,6 +25,8 @@ public:
static void window_focus_callback(GLFWwindow* window, int focused);
static void window_reshape_callback(GLFWwindow* window, int new_width,
int new_height);
static void framebuffer_size_callback(GLFWwindow* window, int new_width,
int new_height);
static void mouse_scroll_callback(GLFWwindow* window, double xoffset,
double yoffset);
static void cursor_enter_callback(GLFWwindow* window, int entered);

View File

@@ -41,8 +41,17 @@ struct TextInputEvent {
TextInputEvent(std::string t) : text(std::move(t)) {}
};
using Event = std::variant<MouseMoveEvent, MouseButtonEvent, MouseWheelEvent,
KeyEvent, TextInputEvent>;
struct WindowResizeEvent {
int width;
int height;
};
struct FrameBufferResizeEvent {
int width;
int height;
};
using Event =
std::variant<MouseMoveEvent, MouseButtonEvent, MouseWheelEvent, KeyEvent,
TextInputEvent, WindowResizeEvent, FrameBufferResizeEvent>;
template <class... T> struct Overloaded : T... {
using T::operator()...;

View File

@@ -2,6 +2,7 @@
#include "Cubed/config.hpp"
#include "Cubed/constants.hpp"
#include "Cubed/input/event.hpp"
#include "Cubed/primitive_data.hpp"
#include "Cubed/render/player_renderer.hpp"
#include "Cubed/render/shader_manager.hpp"
@@ -29,15 +30,16 @@ public:
const Shader& get_shader(const std::string& name) const;
void begin_frame();
void end_frame();
void render();
void render_world(ClientWorld& world);
void render_lable(const Label& label);
void render_image(const Image& image);
void begin_render_ui();
void end_render_ui();
void update(float delta_time);
void update_fov(float fov);
void update_proj_matrix(float aspect, float width, float height);
void updata_framebuffer(int width, int height);
float& ambient_strength();
bool& discard_transparent();
@@ -65,13 +67,17 @@ public:
const TextureManager& texture_mamger() const;
float delta_time() const;
float height() const;
float width() const;
float window_height() const;
float window_width() const;
float frame_height() const;
float frame_width() const;
const glm::mat4& p_mat() const;
const std::vector<VertexArray>& vao() const;
void render_dev_panel(DevPanel& dev_panel);
bool handle_event(const Event& e);
private:
TextureManager& m_texture_manager;
@@ -82,8 +88,11 @@ private:
float m_delta_time = 0.0f;
float m_width = 0.0f;
float m_height = 0.0f;
float m_frame_width = 0.0f;
float m_frame_height = 0.0f;
float m_window_width = 0.0f;
float m_window_height = 0.0f;
glm::mat4 m_world_proj_matrix;
@@ -94,8 +103,6 @@ private:
std::unique_ptr<VertexBuffer> m_player_vbo;
std::unique_ptr<VertexBuffer> m_quad_vbo;
std::unique_ptr<Image> m_crosshair_image;
glm::mat4 m_ui_proj_matrix;
ShaderManager m_shaders;
@@ -111,11 +118,12 @@ private:
WorldRenderer m_world_renderer;
Config& m_config;
bool handle_window_resize_event(const WindowResizeEvent& e);
bool handle_frame_buffer_resize_event(const FrameBufferResizeEvent& e);
void updata_framebuffer(int width, int height);
void init_quad();
void init_text();
void render_ui();
void render_crosshair();
};
} // namespace Cubed

View File

@@ -4,6 +4,7 @@
#include "Cubed/dev_panel.hpp"
#include "Cubed/gameplay/client_world.hpp"
#include "Cubed/scene/scene.hpp"
#include "Cubed/ui/world_ui_manager.hpp"
namespace Cubed {
class SceneManager;
class WorldScene : public Scene {
@@ -32,7 +33,7 @@ private:
Camera m_camera;
std::shared_ptr<NetworkClient> m_client;
ClientWorld m_client_world;
WorldUIManager m_ui_manager;
const Argument& m_argument;
};
} // namespace Cubed

View File

@@ -1,13 +0,0 @@
#pragma once
namespace Cubed {
class UIManager {
public:
UIManager(const UIManager&) = delete;
UIManager(UIManager&&) = delete;
UIManager& operator=(const UIManager&) = delete;
UIManager& operator=(UIManager&&) = delete;
private:
};
} // namespace Cubed

View File

@@ -0,0 +1,34 @@
#pragma once
#include "Cubed/input/event.hpp"
#include "Cubed/ui/widget.hpp"
#include <string>
#include <unordered_map>
namespace Cubed {
class WorldScene;
class WorldUIManager {
public:
WorldUIManager(const WorldUIManager&) = delete;
WorldUIManager(WorldUIManager&&) = delete;
WorldUIManager& operator=(const WorldUIManager&) = delete;
WorldUIManager& operator=(WorldUIManager&&) = delete;
WorldUIManager(WorldScene& scene);
~WorldUIManager();
void init();
void update(float dt);
void render(Renderer& renderer);
bool handle_event(const Event& e);
private:
WorldScene& m_scene;
std::unordered_map<std::string, std::unique_ptr<Widget>> m_widgets;
bool handle_mouse_button_event(const MouseButtonEvent& e);
bool handle_window_resize_event(const WindowResizeEvent& e);
bool handle_mouse_wheel_event(const MouseWheelEvent& e);
bool handle_key_event(const KeyEvent& e);
};
} // namespace Cubed

View File

@@ -10,7 +10,7 @@ class Camera;
class Renderer;
class Window {
public:
Window(Renderer& renderer, Config& config);
Window(Config& config);
~Window();
bool is_mouse_enable() const;
@@ -18,7 +18,7 @@ public:
GLFWwindow* get_glfw_window();
void init();
void imgui_init();
void update_viewport();
// end of frame to reload!
bool handle_event(const Event& e);
@@ -33,14 +33,13 @@ public:
private:
bool m_mouse_enable = false;
bool m_imgui_init = false;
float m_aspect;
GLFWwindow* m_window;
int m_width;
int m_height;
Renderer& m_renderer;
int m_window_width;
int m_window_height;
Config& m_config;
Camera* m_camera = nullptr;
bool handle_key_event(const KeyEvent& e);
bool handle_window_resize_event(const WindowResizeEvent& e);
bool handle_mouse_button_event(const MouseButtonEvent& e);
};