From e7082bdbe0f638664e042150916d154bc2ca421e Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Mon, 13 Jul 2026 16:01:00 +0800 Subject: [PATCH] feat(ui): introduce ColumnLayout widget and refactor widget parenting - Add ColumnLayout widget that arranges children vertically with spacing. - Refactor Widget::add_child to automatically pass `this` as parent. - Update DebugCollector to use ColumnLayout for consistent spacing. - Expose children() accessor in Widget for layout management. --- include/Cubed/debug_collector.hpp | 3 +- include/Cubed/ui/column_layout.hpp | 24 +++++++++ include/Cubed/ui/widget.hpp | 13 +++-- src/CMakeLists.txt | 1 + src/debug_collector.cpp | 87 +++++++++++------------------- src/ui/column_layout.cpp | 24 +++++++++ src/ui/widget.cpp | 6 +++ 7 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 include/Cubed/ui/column_layout.hpp create mode 100644 src/ui/column_layout.cpp diff --git a/include/Cubed/debug_collector.hpp b/include/Cubed/debug_collector.hpp index ce6d253..5aeaca7 100644 --- a/include/Cubed/debug_collector.hpp +++ b/include/Cubed/debug_collector.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Cubed/ui/column_layout.hpp" #include "Cubed/ui/label.hpp" #include "Cubed/ui/widget.hpp" @@ -18,7 +19,7 @@ public: bool handle_event(const Event& e); private: - Widget m_widget; + ColumnLayout m_widget; std::unordered_map m_component; }; diff --git a/include/Cubed/ui/column_layout.hpp b/include/Cubed/ui/column_layout.hpp new file mode 100644 index 0000000..aa7ce19 --- /dev/null +++ b/include/Cubed/ui/column_layout.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Cubed/ui/widget.hpp" +namespace Cubed { +class ColumnLayout : public Widget { +public: + ColumnLayout(const ColumnLayout&) = delete; + ColumnLayout(ColumnLayout&&) = delete; + ColumnLayout& operator=(const ColumnLayout&) = delete; + ColumnLayout& operator=(ColumnLayout&&) = delete; + ColumnLayout(Widget* parent); + ~ColumnLayout(); + + void update(float dt) override; + + void set_spacing(int spacing); + // No need for parent node pointer; do not modify children's anchors and + // scale. + + void layout(); + +private: + int m_spacing = 0; +}; +} // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/ui/widget.hpp b/include/Cubed/ui/widget.hpp index 16412b1..77944d0 100644 --- a/include/Cubed/ui/widget.hpp +++ b/include/Cubed/ui/widget.hpp @@ -32,16 +32,13 @@ public: virtual bool handle_mouse_move_event(const MouseMoveEvent& e); template T& add_child(Args&&... args) { - auto widget = std::make_unique(std::forward(args)...); + auto widget = std::make_unique(std::forward(args)..., this); T& ref = *widget; m_children.emplace_back(std::move(widget)); return ref; }; protected: - virtual void on_update(float dt); - virtual void on_render(Renderer& renderer); - virtual glm::vec2 compute_position() const; Widget* m_parent = nullptr; std::string m_id; float m_window_height = 0; @@ -50,6 +47,14 @@ protected: Anchor m_anchor = Anchor::TOP_LEFT; glm::ivec2 m_offset{0, 0}; + std::vector>& children(); + + const std::vector>& children() const; + + virtual void on_update(float dt); + virtual void on_render(Renderer& renderer); + virtual glm::vec2 compute_position() const; + private: std::vector> m_children; }; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc68bbf..eac6f63 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -67,4 +67,5 @@ target_sources(${PROJECT_NAME} ui/world_ui_manager.cpp ui/main_menu_ui_manager.cpp scene/main_menu_scene.cpp + ui/column_layout.cpp ) \ No newline at end of file diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index 775b3c2..3eb4ba3 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -13,61 +13,50 @@ DebugCollector& DebugCollector::get() { } void DebugCollector::init(int width, int height) { - constexpr int SPACE = 50; - // version_text + constexpr float SCALE = 0.6f; + m_widget.set_window_size(width, height); - auto& version_text = m_widget.add_child