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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 143 B

After

Width:  |  Height:  |  Size: 215 B

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:

View File

@@ -1,7 +1,13 @@
#include "Cubed/ui/button.hpp"
namespace Cubed {
Button::Button(Widget* parent) : Widget(parent) {}
Button::Button(Widget* parent) : Widget(parent) {
m_background = std::make_unique<Image>(this);
m_background->set_fill(true);
m_background->set_anchor(Anchor::TOP_LEFT);
m_foreground = std::make_unique<Label>(this);
m_foreground->set_anchor(Anchor::CENTER);
}
void Button::on_update(float dt) {
if (m_background) {
@@ -22,9 +28,9 @@ void Button::on_render(Renderer& renderer) {
}
bool Button::handle_mouse_move_event(const MouseMoveEvent& e) {
auto pos = m_background->pos();
if (e.xpos >= pos.x && e.xpos <= pos.x + width() && e.ypos >= pos.y &&
e.ypos <= pos.y + height()) {
auto p = pos();
if (e.xpos >= p.x && e.xpos <= p.x + width() && e.ypos >= p.y &&
e.ypos <= p.y + height()) {
m_hovered = true;
return true;
}
@@ -45,28 +51,53 @@ bool Button::handle_mouse_button_event(const MouseButtonEvent& e) {
}
Button& Button::set_scale(float scale) {
m_background->set_scale(scale);
m_scale = scale;
update_text_scale();
return *this;
}
void Button::set_window_size(int width, int height) {
m_background->set_window_size(width, height);
Widget::set_window_size(width, height);
}
Widget& Button::set_anchor(Anchor anchor) {
m_background->set_anchor(anchor);
float Button::scale() const { return m_scale; }
float Button::width() const { return m_width * m_scale; }
float Button::height() const { return m_height * m_scale; }
Button& Button::set_width(float width) {
m_width = width;
update_text_scale();
return *this;
}
Widget& Button::set_offset(glm::ivec2 offset) {
m_background->set_offset(offset);
Button& Button::set_height(float height) {
m_height = height;
update_text_scale();
return *this;
}
glm::vec2 Button::pos() const { return m_background->pos(); }
float Button::scale() const { return m_background->scale(); }
Button& Button::set_background_image(const std::string& path,
TextureManager& texture_manager) {
m_background->set_image(path, texture_manager);
return *this;
}
Button& Button::set_text(const std::string& text) {
m_foreground->set_text(text);
update_text_scale();
return *this;
}
float Button::width() const { return m_background->width(); }
float Button::height() const { return m_background->height(); }
void Button::update_text_scale() {
float text_w = m_foreground->real_width();
float text_h = m_foreground->real_height();
if (text_w <= 0.0f || text_h <= 0.0f)
return;
float available_w = std::max(0.0f, width() - 2.0f * PADDING * m_scale);
float available_h = std::max(0.0f, height() - 2.0f * PADDING * m_scale);
float scale_x = available_w / text_w;
float scale_y = available_h / text_h;
float scale = std::max(0.0f, std::min(scale_x, scale_y));
scale = std::clamp(scale, 0.01f, 100.0f);
m_foreground->set_scale(scale);
}
} // namespace Cubed

View File

@@ -5,8 +5,8 @@ ColumnLayout::ColumnLayout(Widget* parent) : Widget(parent) {}
ColumnLayout::~ColumnLayout() {}
void ColumnLayout::update(float dt) {
Widget::update(dt);
layout();
Widget::update(dt);
}
float ColumnLayout::width() const {

View File

@@ -26,14 +26,12 @@ void CreditsUI::init() {
{
auto& button = rect.add_child<Button>();
auto& back = button.set_background<Image>();
back.set_image("texture/ui/button001.png",
button.set_background_image(
"texture/ui/button001.png",
m_scene.scene_manager().app().texture_manager());
auto& fore = button.set_foreground<Label>();
fore.set_scale(0.5f).set_text("Return");
button.set_scale(4.0f)
.set_anchor(Anchor::BOTTOM_CENTER)
.set_offset({0, -20});
button.set_text("Return");
button.set_anchor(Anchor::BOTTOM_CENTER).set_offset({0, -20});
button.set_clicked([this]() { m_scene.scene_manager().request_pop(); });
}
{

View File

@@ -41,7 +41,10 @@ const UIVertexData& Label::data() const { return m_data; }
const TextStyle& Label::text_style() const { return m_text; }
float Label::width() const { return m_real_width * m_scale; }
float Label::height() const { return m_real_height * m_scale; }
float Label::real_width() const { return m_real_width; }
float Label::real_height() const { return m_real_height; }
float Label::offset_x() const { return m_offset_x; }
float Label::offset_y() const { return m_offset_y; }
float Label::scale() const { return m_scale; }
const std::string& Label::text() const { return m_text.text; }
} // namespace Cubed

View File

@@ -31,39 +31,32 @@ void MainMenuUIManager::init() {
{
auto& start_game_button = layout.add_child<Button>();
auto& back = start_game_button.set_background<Image>();
back.set_image("texture/ui/button001.png",
start_game_button.set_background_image(
"texture/ui/button001.png",
m_scene.scene_manager().app().texture_manager());
auto& fore = start_game_button.set_foreground<Label>();
fore.set_text("Start Game");
fore.set_scale(0.6f);
start_game_button.set_text("Start Game");
start_game_button.set_clicked([this]() {
m_scene.scene_manager().request_push(SceneType::WORLD);
});
start_game_button.set_scale(4.0f);
}
{
auto& button = layout.add_child<Button>();
auto& back = button.set_background<Image>();
back.set_image("texture/ui/button001.png",
button.set_background_image(
"texture/ui/button001.png",
m_scene.scene_manager().app().texture_manager());
auto& fore = button.set_foreground<Label>();
fore.set_text("Credits");
fore.set_scale(0.6f);
button.set_text("Credits");
button.set_clicked([this]() {
m_scene.scene_manager().request_push(SceneType::CREDITS);
});
button.set_scale(4.0f);
}
{
auto& exit_game = layout.add_child<Button>();
auto& back = exit_game.set_background<Image>();
back.set_image("texture/ui/button001.png",
exit_game.set_background_image(
"texture/ui/button001.png",
m_scene.scene_manager().app().texture_manager());
auto& fore = exit_game.set_foreground<Label>();
fore.set_scale(0.6f).set_text("Exit");
exit_game.set_scale(4.0f);
exit_game.set_text("Exit");
exit_game.set_clicked([this]() {
m_scene.scene_manager().app().window().should_close_window();
});

View File

@@ -18,13 +18,10 @@ void PauseMenuUIManager::init() {
rect->set_window_size(renderer.window_width(), renderer.window_height());
auto& back_main = rect->add_child<Button>();
auto& bg = back_main.set_background<Image>();
bg.set_image("texture/ui/button001.png",
back_main.set_background_image(
"texture/ui/button001.png",
m_scene.scene_manager().app().texture_manager());
auto& fg = back_main.set_foreground<Label>();
fg.set_text("return to main menu");
fg.set_scale(0.5f);
back_main.set_scale(5.0f);
back_main.set_text("return to main menu");
back_main.set_anchor(Anchor::CENTER);
back_main.set_clicked([this]() { m_scene.scene_manager().request_pop(); });
m_root_widget = std::move(rect);