mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(ui): replace Text with Label/Widget component system
Replace the old `Text` class with a new component-based UI system consisting of `Widget` and `Label` classes. Key changes: - Remove `Text` class and its GPU upload logic; introduce `Label` as a `Widget` subclass with dirty-flag vertex update. - Add `UIVertexData` struct to manage per-label vertex buffer/array and memory. - Refactor `DebugCollector` to store labels in a `Widget` tree, removing hash-based lookup. - Simplify `Font::vertices()` signature by removing position/scale parameters; scaling is now applied in the renderer’s model matrix. - Update `Renderer` with `render_label()` and move UI rendering to a `render_ui()` that renders the widget tree. - Add new source files `label.cpp`, `widget.cpp`, `ui_vertex_data.cpp` and update CMakeLists.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/ui/text.hpp"
|
||||
#include "Cubed/ui/label.hpp"
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -11,14 +12,13 @@ public:
|
||||
static DebugCollector& get();
|
||||
DebugCollector();
|
||||
|
||||
std::unordered_map<std::size_t, Text>& all_texts();
|
||||
|
||||
Text& text(std::string_view name);
|
||||
void report(std::string_view name, std::string_view content);
|
||||
void report(const std::string& name, std::string_view content);
|
||||
void init_text();
|
||||
Widget& get_widget();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::size_t, Text> m_texts;
|
||||
Widget m_widget;
|
||||
std::unordered_map<std::string, Label*> m_component;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
#include "Cubed/render/world_renderer.hpp"
|
||||
#include "Cubed/shader.hpp"
|
||||
#include "Cubed/ui/text.hpp"
|
||||
#include "Cubed/ui/label.hpp"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
@@ -31,6 +31,9 @@ public:
|
||||
void init(bool debug_on);
|
||||
const Shader& get_shader(const std::string& name) const;
|
||||
void render();
|
||||
|
||||
void render_lable(const Label& label);
|
||||
|
||||
void update(float delta_time);
|
||||
void update_fov(float fov);
|
||||
void update_proj_matrix(float aspect, float width, float height);
|
||||
@@ -118,9 +121,9 @@ private:
|
||||
void day_night_calculation();
|
||||
|
||||
void render_sky();
|
||||
void render_text();
|
||||
void render_ui();
|
||||
|
||||
void render_ui();
|
||||
void render_crosshair();
|
||||
void render_dev_panel();
|
||||
};
|
||||
|
||||
|
||||
11
include/Cubed/scene/scene.hpp
Normal file
11
include/Cubed/scene/scene.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
namespace Cubed {
|
||||
class Scene {
|
||||
public:
|
||||
Scene() = default;
|
||||
virtual ~Scene() = default;
|
||||
virtual void update(float dt) = 0;
|
||||
virtual void render() = 0;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -29,9 +29,7 @@ public:
|
||||
Font();
|
||||
~Font();
|
||||
|
||||
static std::vector<Vertex2D> vertices(const std::string& text,
|
||||
float x = 0.0f, float y = 0.0f,
|
||||
float scale = 1.0f);
|
||||
static std::vector<Vertex2D> vertices(const std::string& text);
|
||||
static const Texture* text_texture();
|
||||
static const std::string& font_path();
|
||||
|
||||
|
||||
42
include/Cubed/ui/label.hpp
Normal file
42
include/Cubed/ui/label.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "Cubed/ui/text.hpp"
|
||||
#include "Cubed/ui/ui_vertex_data.hpp"
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
#include "glm/ext/vector_float2.hpp"
|
||||
namespace Cubed {
|
||||
class Label : public Widget {
|
||||
public:
|
||||
Label(const Label&) = delete;
|
||||
Label(Label&&) = delete;
|
||||
Label& operator=(const Label&) = delete;
|
||||
Label& operator=(Label&&) = delete;
|
||||
|
||||
Label(const std::string& id);
|
||||
virtual ~Label() = default;
|
||||
|
||||
Label& set_position(const glm::vec2& pos);
|
||||
Label& set_position(float x, float y);
|
||||
Label& set_text(std::string_view text);
|
||||
Label& set_scale(float scale);
|
||||
Label& set_color(Color color);
|
||||
virtual void update(float dt) override;
|
||||
virtual void render(Renderer& renderer) override;
|
||||
|
||||
const UIVertexData& data() const;
|
||||
const glm::vec2& pos() const;
|
||||
const TextStyle& text_style() const;
|
||||
float scale() const;
|
||||
|
||||
protected:
|
||||
virtual void on_update(float dt) override;
|
||||
virtual void on_render(Renderer& renderer) override;
|
||||
|
||||
private:
|
||||
float m_scale = 1.0f;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
TextStyle m_text;
|
||||
UIVertexData m_data;
|
||||
bool m_dirty = false;
|
||||
void update_vertices();
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -1,8 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/render/vertex_array.hpp"
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
#include "Cubed/ui/color.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
@@ -10,46 +7,9 @@
|
||||
#include <string>
|
||||
namespace Cubed {
|
||||
|
||||
class Shader;
|
||||
|
||||
class Text {
|
||||
public:
|
||||
explicit Text(std::string_view name);
|
||||
Text(std::string_view name, std::string_view str,
|
||||
glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
|
||||
~Text();
|
||||
Text(const Text&) = delete;
|
||||
Text(Text&&) noexcept;
|
||||
Text& operator=(const Text&) = delete;
|
||||
Text& operator=(Text&&) noexcept = delete;
|
||||
Text& color(Color color);
|
||||
// Text& color(const glm::vec4& color, int pos);
|
||||
Text& position(float x, float y);
|
||||
Text& scale(float s);
|
||||
Text& text(std::string_view str);
|
||||
|
||||
std::size_t uuid() const;
|
||||
void render(const Shader& shader);
|
||||
|
||||
bool operator==(const Text& other) const;
|
||||
|
||||
private:
|
||||
float m_scale = 1.0f;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
|
||||
const std::string NAME;
|
||||
const std::size_t UUID;
|
||||
|
||||
std::string m_text;
|
||||
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glm::mat4 m_model_matrix;
|
||||
|
||||
std::vector<Vertex2D> m_vertices;
|
||||
std::unique_ptr<VertexBuffer> m_vbo;
|
||||
std::unique_ptr<VertexArray> m_vao;
|
||||
|
||||
void update_vertices();
|
||||
void upload_to_gpu();
|
||||
struct TextStyle {
|
||||
std::string text;
|
||||
Color color = Color::WHITE;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
25
include/Cubed/ui/ui_vertex_data.hpp
Normal file
25
include/Cubed/ui/ui_vertex_data.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/render/vertex_array.hpp"
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
struct UIVertexData {
|
||||
std::vector<Vertex2D> m_vertices;
|
||||
std::unique_ptr<VertexBuffer> m_vbo;
|
||||
std::unique_ptr<VertexArray> m_vao;
|
||||
std::atomic<std::size_t> m_sum{0};
|
||||
UIVertexData();
|
||||
~UIVertexData();
|
||||
UIVertexData(const UIVertexData&) = delete;
|
||||
UIVertexData(UIVertexData&&) noexcept;
|
||||
UIVertexData& operator=(const UIVertexData&) = delete;
|
||||
UIVertexData& operator=(UIVertexData&&) noexcept;
|
||||
|
||||
void upload();
|
||||
void update_sum();
|
||||
};
|
||||
} // namespace Cubed
|
||||
31
include/Cubed/ui/widget.hpp
Normal file
31
include/Cubed/ui/widget.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
class Renderer;
|
||||
class Widget {
|
||||
|
||||
public:
|
||||
Widget(const std::string& id);
|
||||
Widget() = default;
|
||||
virtual ~Widget() = default;
|
||||
virtual void update(float dt);
|
||||
virtual void render(Renderer& renderer);
|
||||
virtual const std::string& id() const;
|
||||
template <typename T, typename... Args> T& add_child(Args&&... args) {
|
||||
auto widget = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
T& ref = *widget;
|
||||
m_children.emplace_back(std::move(widget));
|
||||
return ref;
|
||||
};
|
||||
|
||||
protected:
|
||||
virtual void on_update(float dt);
|
||||
virtual void on_render(Renderer& renderer);
|
||||
std::string m_id;
|
||||
|
||||
private:
|
||||
Widget* m_parent = nullptr;
|
||||
std::vector<std::unique_ptr<Widget>> m_children;
|
||||
};
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user