refactor(font): return TextMesh from Font::vertices to include bounding box

Replace the plain vertex vector with a TextMesh struct that carries width, height, and min coordinates. Update Label to compute and store these values immediately on text change (removing lazy dirty update). Fix Button's width/height to factor in scale and use concrete widget types for background and foreground.
This commit is contained in:
2026-07-13 11:32:52 +08:00
parent 576a2f8072
commit b5e0bb9290
6 changed files with 62 additions and 21 deletions

View File

@@ -7,7 +7,7 @@ Label::Label(const std::string& id) : Widget(id) {}
Label::Label() {}
Label& Label::set_text(std::string_view text) {
m_text.text = text;
m_dirty = true;
update_vertices();
return *this;
}
@@ -18,25 +18,30 @@ Label& Label::set_color(Color color) {
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::on_update(float dt) { (void)dt; }
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);
auto textmesh = Font::vertices(m_text.text);
m_data.m_vertices = std::move(textmesh.vertices);
m_offset_x = textmesh.min_x;
m_offset_y = textmesh.min_y;
m_width = textmesh.width;
m_height = textmesh.height;
m_data.update_sum();
m_data.upload();
}
const UIVertexData& Label::data() const { return m_data; }
const TextStyle& Label::text_style() const { return m_text; }
float Label::width() const { return m_width; }
float Label::height() const { return m_height; }
float Label::offset_x() const { return m_offset_x; }
float Label::offset_y() const { return m_offset_y; }
} // namespace Cubed