refactor(ui): simplify Button API with property setters

This commit is contained in:
2026-07-15 11:06:49 +08:00
parent 4653ae9f06
commit 8706723d53
11 changed files with 100 additions and 76 deletions

View File

@@ -18,41 +18,35 @@ public:
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
void set_window_size(int width, int height) override;
Button& set_scale(float scale);
Widget& set_anchor(Anchor anchor) override;
Widget& set_offset(glm::ivec2 offset) override;
glm::vec2 pos() const override;
float width() const override;
float height() const override;
Button& set_width(float width);
Button& set_height(float height);
Button& set_background_image(const std::string& path,
TextureManager& texture_manager);
Button& set_text(const std::string& text);
float scale() const;
template <typename F> Button& set_clicked(F&& f) {
m_clicked = std::forward<F>(f);
return *this;
}
template <typename T, typename... Args> T& set_background(Args&&... args) {
auto w = std::make_unique<T>(std::forward<Args>(args)..., m_parent);
T& ref = *w;
m_background = std::move(w);
return ref;
}
template <typename T, typename... Args> T& set_foreground(Args&&... args) {
auto w = std::make_unique<T>(std::forward<Args>(args)..., this);
T& ref = *w;
m_foreground = std::move(w);
m_foreground->set_anchor(Anchor::CENTER);
return ref;
}
private:
static constexpr float PADDING = 5.0f;
static constexpr float DEFAULT_SCALE = 3.0f;
std::function<void()> m_clicked;
std::unique_ptr<Image> m_background;
std::unique_ptr<Label> m_foreground;
bool m_hovered = false;
float m_width = NORMAL_BUTTON_WIDTH;
float m_height = NORMAL_BUTTON_HEIGHT;
float m_scale = DEFAULT_SCALE;
void on_render(Renderer& renderer) override;
void on_update(float dt) override;
void update_text_scale();
};
} // namespace Cubed

View File

@@ -25,9 +25,12 @@ public:
float width() const override;
float height() const override;
float real_width() const;
float real_height() const;
float offset_x() const;
float offset_y() const;
float scale() const;
const std::string& text() const;
protected:
virtual void on_update(float dt) override;

View File

@@ -38,8 +38,8 @@ private:
std::unique_ptr<Image> m_track;
std::unique_ptr<Image> m_thumb;
float m_scale = 1.0f;
float m_width = 0.0f;
float m_height = 0.0f;
float m_width = NORMAL_SLIDER_WIDTH;
float m_height = NORMAL_SLIDER_HEIGHT;
float m_xpos = 0.0f;
bool m_dragging = false;
bool m_inside = false;

View File

@@ -8,6 +8,11 @@
namespace Cubed {
class Renderer;
constexpr float NORMAL_BUTTON_WIDTH = 225.0f;
constexpr float NORMAL_BUTTON_HEIGHT = 20.0f;
constexpr float NORMAL_SLIDER_WIDTH = 225.0f;
constexpr float NORMAL_SLIDER_HEIGHT = 20.0f;
class Widget {
public: