mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
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:
@@ -22,6 +22,16 @@ struct Character {
|
||||
GLuint advance;
|
||||
};
|
||||
|
||||
struct TextMesh {
|
||||
std::vector<Vertex2D> vertices;
|
||||
|
||||
float width;
|
||||
float height;
|
||||
|
||||
float min_x;
|
||||
float min_y;
|
||||
};
|
||||
|
||||
class Shader;
|
||||
|
||||
class Font {
|
||||
@@ -29,7 +39,7 @@ public:
|
||||
Font();
|
||||
~Font();
|
||||
|
||||
static std::vector<Vertex2D> vertices(const std::string& text);
|
||||
static TextMesh vertices(const std::string& text);
|
||||
static const Texture* text_texture();
|
||||
static const std::string& font_path();
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/ui/image.hpp"
|
||||
#include "Cubed/ui/label.hpp"
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
|
||||
#include <functional>
|
||||
@@ -47,8 +49,8 @@ public:
|
||||
|
||||
private:
|
||||
std::function<void()> m_clicked;
|
||||
std::unique_ptr<Widget> m_background;
|
||||
std::unique_ptr<Widget> m_foreground;
|
||||
std::unique_ptr<Image> m_background;
|
||||
std::unique_ptr<Label> m_foreground;
|
||||
bool m_hovered = false;
|
||||
glm::vec2 m_min_pos;
|
||||
glm::vec2 m_max_pos;
|
||||
|
||||
@@ -24,6 +24,11 @@ public:
|
||||
|
||||
const TextStyle& text_style() const;
|
||||
|
||||
float width() const override;
|
||||
float height() const override;
|
||||
float offset_x() const;
|
||||
float offset_y() const;
|
||||
|
||||
protected:
|
||||
virtual void on_update(float dt) override;
|
||||
virtual void on_render(Renderer& renderer) override;
|
||||
@@ -31,7 +36,10 @@ protected:
|
||||
private:
|
||||
TextStyle m_text;
|
||||
UIVertexData m_data;
|
||||
bool m_dirty = false;
|
||||
float m_width = 0.0f;
|
||||
float m_height = 0.0f;
|
||||
float m_offset_x = 0.0f;
|
||||
float m_offset_y = 0.0f;
|
||||
void update_vertices();
|
||||
};
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user