refactor(ui): move scale property to subclasses and fix resize event handling

Remove scale from base Widget class and add per-type set_scale/scale methods to Button, Image, and Label. Dispatch separate WindowResizeEvent alongside existing FrameBufferResizeEvent. Correct centering calculations from `+` to `-` in main menu and world UI managers.
This commit is contained in:
2026-07-13 13:06:09 +08:00
parent 1fa3b9cce8
commit e8157ab549
12 changed files with 59 additions and 39 deletions

View File

@@ -24,10 +24,10 @@ public:
Widget& set_position(const glm::vec2& pos) override;
Widget& set_position(float x, float y) override;
Widget& set_scale(float scale) override;
Button& set_scale(float scale);
float width() const override;
float height() const override;
float scale() const;
template <typename F> Button& set_clicked(F&& f) {
m_clicked = std::forward<F>(f);
return *this;

View File

@@ -16,6 +16,8 @@ public:
float width() const override;
float height() const override;
const Texture* texture() const;
Image& set_scale(float scale);
float scale() const;
private:
const Texture* m_texture = nullptr;

View File

@@ -17,6 +17,7 @@ public:
Label& set_text(std::string_view text);
Label& set_color(Color color);
Label& set_scale(float scale);
virtual void update(float dt) override;
virtual void render(Renderer& renderer) override;
@@ -28,6 +29,7 @@ public:
float height() const override;
float offset_x() const;
float offset_y() const;
float scale() const;
protected:
virtual void on_update(float dt) override;
@@ -40,6 +42,7 @@ private:
float m_real_height = 0.0f;
float m_offset_x = 0.0f;
float m_offset_y = 0.0f;
float m_scale = 1.0f;
void update_vertices();
};
} // namespace Cubed

View File

@@ -18,11 +18,10 @@ public:
virtual const std::string& id() const;
virtual Widget& set_position(const glm::vec2& pos);
virtual Widget& set_position(float x, float y);
virtual Widget& set_scale(float scale);
// Returns the final display size
virtual float width() const;
virtual float height() const;
virtual const glm::vec2& pos() const;
virtual float scale() const;
virtual bool handle_key_event(const KeyEvent& e);
virtual bool handle_mouse_button_event(const MouseButtonEvent& e);
@@ -41,7 +40,6 @@ protected:
virtual void on_update(float dt);
virtual void on_render(Renderer& renderer);
std::string m_id;
float m_scale = 1.0f;
// Center is at the top-left corner, position is at the top-left corner
glm::vec2 m_pos{0.0f, 0.0f};