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(const glm::vec2& pos) override;
Widget& set_position(float x, float y) 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 width() const override;
float height() const override; float height() const override;
float scale() const;
template <typename F> Button& set_clicked(F&& f) { template <typename F> Button& set_clicked(F&& f) {
m_clicked = std::forward<F>(f); m_clicked = std::forward<F>(f);
return *this; return *this;

View File

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

View File

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

View File

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

View File

@@ -677,12 +677,21 @@ void App::run() {
// static Gait player_gait = Gait::WALK; // static Gait player_gait = Gait::WALK;
void App::update() { void App::update() {
glfwPollEvents(); glfwPollEvents();
{
int w, h;
glfwGetFramebufferSize(m_window.get_glfw_window(), &w, &h);
int w, h; if (w != m_renderer.frame_width() || h != m_renderer.frame_height()) {
glfwGetFramebufferSize(m_window.get_glfw_window(), &w, &h); dispatch_event(FrameBufferResizeEvent{w, h});
}
}
{
int w, h;
glfwGetWindowSize(m_window.get_glfw_window(), &w, &h);
if (w != m_renderer.frame_width() || h != m_renderer.frame_height()) { if (w != m_renderer.window_width() || h != m_renderer.window_height()) {
dispatch_event(FrameBufferResizeEvent{w, h}); dispatch_event(WindowResizeEvent{w, h});
}
} }
current_time = glfwGetTime(); current_time = glfwGetTime();

View File

@@ -23,8 +23,8 @@ void DebugCollector::init_text() {
#endif #endif
version_text.set_color(Color::WHITE) version_text.set_color(Color::WHITE)
.set_text(version) .set_text(version)
.set_position(0.0f, 100.0f) .set_scale(0.8f)
.set_scale(0.8f); .set_position(0.0f, 100.0f);
m_component.try_emplace(version_text.id(), &version_text); m_component.try_emplace(version_text.id(), &version_text);
// fps // fps
@@ -35,8 +35,8 @@ void DebugCollector::init_text() {
// player_pos // player_pos
auto& player_pos_text = m_widget.add_child<Label>("player_pos"); auto& player_pos_text = m_widget.add_child<Label>("player_pos");
player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00") player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00")
.set_position(0.0f, 150.0f) .set_scale(0.8f)
.set_scale(0.8f); .set_position(0.0f, 150.0f);
m_component.try_emplace(player_pos_text.id(), &player_pos_text); m_component.try_emplace(player_pos_text.id(), &player_pos_text);
// rendered_chunk // rendered_chunk

View File

@@ -59,14 +59,16 @@ Widget& Button::set_position(const glm::vec2& pos) {
Widget& Button::set_position(float x, float y) { Widget& Button::set_position(float x, float y) {
return set_position(glm::vec2{x, y}); return set_position(glm::vec2{x, y});
} }
Widget& Button::set_scale(float scale) { Button& Button::set_scale(float scale) {
m_scale = scale;
m_background->set_scale(scale); m_background->set_scale(scale);
m_max_pos.x = m_min_pos.x + width(); m_max_pos.x = m_min_pos.x + width();
m_max_pos.y = m_min_pos.y + height(); m_max_pos.y = m_min_pos.y + height();
return *this; return *this;
} }
float Button::width() const { return m_background->width() * m_scale; }
float Button::height() const { return m_background->height() * m_scale; } float Button::scale() const { return m_background->scale(); }
float Button::width() const { return m_background->width(); }
float Button::height() const { return m_background->height(); }
} // namespace Cubed } // namespace Cubed

View File

@@ -13,7 +13,11 @@ Image& Image::set_image(const std::string& path,
m_texture = texture_manager.get_image_texture(path); m_texture = texture_manager.get_image_texture(path);
return *this; return *this;
} }
Image& Image::set_scale(float scale) {
m_scale = scale;
return *this;
}
float Image::scale() const { return m_scale; }
float Image::height() const { float Image::height() const {
if (!m_texture) { if (!m_texture) {
Logger::error("Image id {} not set image!", m_id); Logger::error("Image id {} not set image!", m_id);

View File

@@ -15,7 +15,10 @@ Label& Label::set_color(Color color) {
m_text.color = color; m_text.color = color;
return *this; return *this;
} }
Label& Label::set_scale(float scale) {
m_scale = scale;
return *this;
}
void Label::update(float dt) { on_update(dt); } void Label::update(float dt) { on_update(dt); }
void Label::on_update(float dt) { (void)dt; } void Label::on_update(float dt) { (void)dt; }
@@ -43,5 +46,5 @@ float Label::width() const { return m_real_width * m_scale; }
float Label::height() const { return m_real_height * m_scale; } float Label::height() const { return m_real_height * m_scale; }
float Label::offset_x() const { return m_offset_x; } float Label::offset_x() const { return m_offset_x; }
float Label::offset_y() const { return m_offset_y; } float Label::offset_y() const { return m_offset_y; }
float Label::scale() const { return m_scale; }
} // namespace Cubed } // namespace Cubed

View File

@@ -22,19 +22,22 @@ void MainMenuUIManager::init() {
start_game_button->set_clicked( start_game_button->set_clicked(
[this]() { m_scene.scene_manager().request_push(SceneType::WORLD); }); [this]() { m_scene.scene_manager().request_push(SceneType::WORLD); });
start_game_button->set_position(
m_scene.scene_manager().app().renderer().window_width() / 2.0f +
back.width() / 2.0f,
m_scene.scene_manager().app().renderer().window_width() / 2.0f +
back.height() / 2.0f);
start_game_button->set_scale(3.0f); start_game_button->set_scale(3.0f);
start_game_button->set_position(
m_scene.scene_manager().app().renderer().window_width() / 2.0f -
back.width() / 2.0f,
m_scene.scene_manager().app().renderer().window_height() / 2.0f -
back.height() / 2.0f);
m_widgets.try_emplace("start game", std::move(start_game_button)); m_widgets.try_emplace("start game", std::move(start_game_button));
} }
void MainMenuUIManager::update(float dt) { void MainMenuUIManager::update(float dt) {
for (auto& w : m_widgets) { for (auto& w : m_widgets) {
w.second->update(dt); w.second->update(dt);
} }
} }
void MainMenuUIManager::render(Renderer& renderer) { void MainMenuUIManager::render(Renderer& renderer) {
renderer.begin_render_ui(); renderer.begin_render_ui();
@@ -101,8 +104,8 @@ bool MainMenuUIManager::handle_window_resize_event(const WindowResizeEvent& e) {
if (it != m_widgets.end()) { if (it != m_widgets.end()) {
auto* start_game = dynamic_cast<Button*>(it->second.get()); auto* start_game = dynamic_cast<Button*>(it->second.get());
if (start_game) { if (start_game) {
start_game->set_position(e.width / 2 + start_game->width() / 2, start_game->set_position(e.width / 2 - start_game->width() / 2,
e.height / 2 + start_game->height() / 2); e.height / 2 - start_game->height() / 2);
} }
} }
for (auto& w : m_widgets) { for (auto& w : m_widgets) {

View File

@@ -31,16 +31,10 @@ Widget& Widget::set_position(float x, float y) {
return *this; return *this;
} }
Widget& Widget::set_scale(float scale) {
m_scale = scale;
return *this;
}
float Widget::width() const { return 0.0f; } float Widget::width() const { return 0.0f; }
float Widget::height() const { return 0.0f; } float Widget::height() const { return 0.0f; }
const glm::vec2& Widget::pos() const { return m_pos; } const glm::vec2& Widget::pos() const { return m_pos; }
float Widget::scale() const { return m_scale; }
const std::string& Widget::id() const { return m_id; } const std::string& Widget::id() const { return m_id; }
bool Widget::handle_key_event(const KeyEvent& e) { bool Widget::handle_key_event(const KeyEvent& e) {

View File

@@ -16,10 +16,11 @@ void WorldUIManager::init() {
crosshair->set_image("texture/ui/0.png", crosshair->set_image("texture/ui/0.png",
m_scene.scene_manager().app().texture_manager()); m_scene.scene_manager().app().texture_manager());
auto& renderer = m_scene.scene_manager().app().renderer(); auto& renderer = m_scene.scene_manager().app().renderer();
crosshair
->set_position(renderer.window_width() / 2 + crosshair->width() / 2, crosshair->set_scale(3.0f).set_position(
renderer.window_height() / 2 + crosshair->height() / 2) renderer.window_width() / 2 - crosshair->width() / 2,
.set_scale(3.0f); renderer.window_height() / 2 - crosshair->height() / 2);
m_widgets.try_emplace("crosshair", std::move(crosshair)); m_widgets.try_emplace("crosshair", std::move(crosshair));
} }
void WorldUIManager::update(float dt) { void WorldUIManager::update(float dt) {
@@ -96,8 +97,9 @@ bool WorldUIManager::handle_window_resize_event(const WindowResizeEvent& e) {
if (it != m_widgets.end()) { if (it != m_widgets.end()) {
auto* crosshair = dynamic_cast<Image*>(it->second.get()); auto* crosshair = dynamic_cast<Image*>(it->second.get());
if (crosshair) { if (crosshair) {
crosshair->set_position(e.width / 2 + crosshair->width() / 2, crosshair->set_position(e.width / 2.0f - crosshair->width() / 2.0f,
e.height / 2 + crosshair->height() / 2); e.height / 2.0f -
crosshair->height() / 2.0f);
} }
} }
for (auto& w : m_widgets) { for (auto& w : m_widgets) {