mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +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:
58
src/ui/label.cpp
Normal file
58
src/ui/label.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "Cubed/ui/label.hpp"
|
||||
|
||||
#include "Cubed/render/renderer.hpp"
|
||||
#include "Cubed/tools/font.hpp"
|
||||
namespace Cubed {
|
||||
Label::Label(const std::string& id) : Widget(id) {}
|
||||
|
||||
Label& Label::set_text(std::string_view text) {
|
||||
m_text.text = text;
|
||||
m_dirty = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Label& Label::set_position(const glm::vec2& pos) {
|
||||
return set_position(pos.x, pos.y);
|
||||
}
|
||||
|
||||
Label& Label::set_position(float x, float y) {
|
||||
m_pos.x = x;
|
||||
m_pos.y = y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Label& Label::set_scale(float scale) {
|
||||
m_scale = scale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Label& Label::set_color(Color color) {
|
||||
m_text.color = color;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Label::update(float dt) { on_update(dt); }
|
||||
|
||||
void Label::on_update(float dt) {
|
||||
(void)dt;
|
||||
if (m_dirty) {
|
||||
update_vertices();
|
||||
m_dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Label::render(Renderer& renderer) { on_render(renderer); }
|
||||
|
||||
void Label::on_render(Renderer& renderer) { renderer.render_lable(*this); }
|
||||
|
||||
void Label::update_vertices() {
|
||||
m_data.m_vertices = Font::vertices(m_text.text);
|
||||
m_data.update_sum();
|
||||
m_data.upload();
|
||||
}
|
||||
|
||||
const UIVertexData& Label::data() const { return m_data; }
|
||||
const glm::vec2& Label::pos() const { return m_pos; }
|
||||
const TextStyle& Label::text_style() const { return m_text; }
|
||||
float Label::scale() const { return m_scale; }
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user