mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(ui): simplify Button API with property setters
This commit is contained in:
@@ -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
|
||||
@@ -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 {
|
||||
|
||||
@@ -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",
|
||||
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_background_image(
|
||||
"texture/ui/button001.png",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
|
||||
button.set_text("Return");
|
||||
button.set_anchor(Anchor::BOTTOM_CENTER).set_offset({0, -20});
|
||||
button.set_clicked([this]() { m_scene.scene_manager().request_pop(); });
|
||||
}
|
||||
{
|
||||
|
||||
@@ -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
|
||||
@@ -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",
|
||||
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_background_image(
|
||||
"texture/ui/button001.png",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
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",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
auto& fore = button.set_foreground<Label>();
|
||||
fore.set_text("Credits");
|
||||
fore.set_scale(0.6f);
|
||||
button.set_background_image(
|
||||
"texture/ui/button001.png",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
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",
|
||||
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_background_image(
|
||||
"texture/ui/button001.png",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
exit_game.set_text("Exit");
|
||||
|
||||
exit_game.set_clicked([this]() {
|
||||
m_scene.scene_manager().app().window().should_close_window();
|
||||
});
|
||||
|
||||
@@ -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",
|
||||
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_background_image(
|
||||
"texture/ui/button001.png",
|
||||
m_scene.scene_manager().app().texture_manager());
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user