feat(ui): support scaling of labels and images

Modify width() and height() methods to multiply by m_scale for both Label and Image. Rename internal dimensions to m_real_width and m_real_height for clarity. Adjust Button foreground position to vertically center when label is scaled.
This commit is contained in:
2026-07-13 11:40:27 +08:00
parent b5e0bb9290
commit 1fa3b9cce8
5 changed files with 10 additions and 9 deletions

View File

@@ -36,8 +36,8 @@ protected:
private:
TextStyle m_text;
UIVertexData m_data;
float m_width = 0.0f;
float m_height = 0.0f;
float m_real_width = 0.0f;
float m_real_height = 0.0f;
float m_offset_x = 0.0f;
float m_offset_y = 0.0f;
void update_vertices();

View File

@@ -48,7 +48,7 @@ bool Button::handle_mouse_button_event(const MouseButtonEvent& e) {
Widget& Button::set_position(const glm::vec2& pos) {
m_pos = pos;
m_background->set_position(pos);
m_foreground->set_position(pos);
m_foreground->set_position(pos.x, pos.y + m_foreground->height() / 2.0f);
m_min_pos = pos;
m_max_pos.x = m_min_pos.x + width();

View File

@@ -19,7 +19,7 @@ float Image::height() const {
Logger::error("Image id {} not set image!", m_id);
return 0.0f;
}
return m_texture->height();
return m_texture->height() * m_scale;
}
float Image::width() const {
@@ -27,7 +27,7 @@ float Image::width() const {
Logger::error("Image id {} not set image!", m_id);
return 0.0f;
}
return m_texture->width();
return m_texture->width() * m_scale;
}
const Texture* Image::texture() const { return m_texture; }

View File

@@ -30,8 +30,8 @@ void Label::update_vertices() {
m_offset_x = textmesh.min_x;
m_offset_y = textmesh.min_y;
m_width = textmesh.width;
m_height = textmesh.height;
m_real_width = textmesh.width;
m_real_height = textmesh.height;
m_data.update_sum();
m_data.upload();
@@ -39,8 +39,8 @@ void Label::update_vertices() {
const UIVertexData& Label::data() const { return m_data; }
const TextStyle& Label::text_style() const { return m_text; }
float Label::width() const { return m_width; }
float Label::height() const { return m_height; }
float Label::width() const { return m_real_width * m_scale; }
float Label::height() const { return m_real_height * m_scale; }
float Label::offset_x() const { return m_offset_x; }
float Label::offset_y() const { return m_offset_y; }

View File

@@ -18,6 +18,7 @@ void MainMenuUIManager::init() {
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_clicked(
[this]() { m_scene.scene_manager().request_push(SceneType::WORLD); });