From 8c79808b86ee77381245a1f00f2e18e20240538a Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 19 Jul 2026 20:56:31 +0800 Subject: [PATCH] refactor(ui): move width, height, and fill properties to base Widget Consolidate width, height, and fill (fill_parent, fill_width, fill_height) into the base Widget class. Replace per-widget set_fill(bool) with set_fill_parent(bool), set_fill_width(bool), and set_fill_height(bool). --- include/Cubed/ui/button.hpp | 6 ++-- include/Cubed/ui/chat_box.hpp | 3 -- include/Cubed/ui/column_layout.hpp | 4 +-- include/Cubed/ui/image.hpp | 8 ----- include/Cubed/ui/rect.hpp | 8 ----- include/Cubed/ui/slider.hpp | 7 ++-- include/Cubed/ui/text_field.hpp | 6 ++-- include/Cubed/ui/widget.hpp | 19 ++++++++--- src/ui/button.cpp | 22 +++++++++++-- src/ui/chat_box.cpp | 14 ++++---- src/ui/column_layout.cpp | 2 +- src/ui/credits_ui.cpp | 11 ++++--- src/ui/error_ui.cpp | 4 +-- src/ui/host_game_ui.cpp | 12 +++---- src/ui/image.cpp | 37 ++++++--------------- src/ui/join_game_ui.cpp | 8 ++--- src/ui/label.cpp | 25 +++++++++++--- src/ui/main_menu_ui_manager.cpp | 2 +- src/ui/pause_menu_ui_manager.cpp | 2 +- src/ui/rect.cpp | 42 ++++++++++-------------- src/ui/settings_ui.cpp | 4 +-- src/ui/slider.cpp | 22 +++++++++++-- src/ui/text_field.cpp | 16 +-------- src/ui/widget.cpp | 52 +++++++++++++++++++++++------- src/ui/world_ui_manager.cpp | 3 +- 25 files changed, 183 insertions(+), 156 deletions(-) diff --git a/include/Cubed/ui/button.hpp b/include/Cubed/ui/button.hpp index ec2a570..1d8b306 100644 --- a/include/Cubed/ui/button.hpp +++ b/include/Cubed/ui/button.hpp @@ -23,8 +23,8 @@ public: float width() const override; float height() const override; - Button& set_width(float width); - Button& set_height(float height); + Button& set_width(float width) override; + Button& set_height(float height) override; Button& set_background_image(const std::string& path, TextureManager& texture_manager); Button& set_default_image(TextureManager& texture_manager); @@ -54,8 +54,6 @@ private: "texture/ui/button001.png"; std::function m_clicked; - float m_width = NORMAL_BUTTON_WIDTH; - float m_height = NORMAL_BUTTON_HEIGHT; float m_scale = DEFAULT_SCALE; bool m_auto_scale = false; void on_render(Renderer& renderer) override; diff --git a/include/Cubed/ui/chat_box.hpp b/include/Cubed/ui/chat_box.hpp index 1fef375..f074341 100644 --- a/include/Cubed/ui/chat_box.hpp +++ b/include/Cubed/ui/chat_box.hpp @@ -17,7 +17,6 @@ public: ChatBox& set_text_scale(float scale); // Only the width of TextField can be set; the height is calculated // dynamically - ChatBox& set_width(float width); ChatBox& set_show_lines(int lines); ChatBox& set_spacing(float spacing); ChatBox& clear_input(); @@ -42,8 +41,6 @@ private: int m_lines = 10; bool m_scale = 1.0f; float m_spacing = 0.0f; - float m_width = 0.0f; - float m_height = 0.0f; float m_text_scale = 1.0f; std::deque m_messages; std::unique_ptr m_text_field; diff --git a/include/Cubed/ui/column_layout.hpp b/include/Cubed/ui/column_layout.hpp index e687897..5874e79 100644 --- a/include/Cubed/ui/column_layout.hpp +++ b/include/Cubed/ui/column_layout.hpp @@ -26,8 +26,8 @@ public: private: int m_spacing = 0; - float m_content_height = 0; - float m_content_width = 0; + float m_content_height = 0.0f; + float m_content_width = 0.0f; ColumnLayoutAnchor m_layout_anchor = ColumnLayoutAnchor::CENTER; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/ui/image.hpp b/include/Cubed/ui/image.hpp index 81a392d..967caab 100644 --- a/include/Cubed/ui/image.hpp +++ b/include/Cubed/ui/image.hpp @@ -21,17 +21,9 @@ public: Image& set_scale(float scale); float scale() const; - Image& set_height(float height); - Image& set_width(float width); - - Image& set_fill(bool fill); - private: void on_render(Renderer& renderer) override; void on_update(float dt) override; - float m_width = 0.0f; - float m_height = 0.0f; - bool m_fill = false; const Texture* m_texture = nullptr; glm::vec2 m_pos{0.0f, 0.0f}; diff --git a/include/Cubed/ui/rect.hpp b/include/Cubed/ui/rect.hpp index ef25703..3a0c3ae 100644 --- a/include/Cubed/ui/rect.hpp +++ b/include/Cubed/ui/rect.hpp @@ -16,11 +16,6 @@ public: float height() const override; float alpha() const; - Rect& set_width(float width); - Rect& set_height(float height); - - Rect& set_fill(bool fill); - Rect& set_scale(float scale); Rect& set_color(Color color); Rect& set_alpha(float alpha); @@ -32,10 +27,7 @@ private: void on_render(Renderer& renderer) override; Color m_color = Color::WHITE; - float m_width = 0.0f; - float m_height = 0.0f; float m_scale = 1.0f; float m_alpha = 1.0f; - bool m_fill = false; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/ui/slider.hpp b/include/Cubed/ui/slider.hpp index fe77ff4..131a16b 100644 --- a/include/Cubed/ui/slider.hpp +++ b/include/Cubed/ui/slider.hpp @@ -21,8 +21,8 @@ public: float height() const override; Slider& set_scale(float scale); - Slider& set_width(float width); - Slider& set_height(float h); + Slider& set_width(float width) override; + Slider& set_height(float h) override; Slider& set_slider_text(const std::string& key, const std::string& variable); Slider& set_track_image(const std::string& path, @@ -48,8 +48,7 @@ private: std::unique_ptr