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.
This commit is contained in:
2026-07-13 16:01:00 +08:00
parent f5e53fd13d
commit e7082bdbe0
7 changed files with 97 additions and 61 deletions

View File

@@ -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<std::string, Label*> m_component;
};

View File

@@ -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

View File

@@ -32,16 +32,13 @@ public:
virtual bool handle_mouse_move_event(const MouseMoveEvent& e);
template <typename T, typename... Args> T& add_child(Args&&... args) {
auto widget = std::make_unique<T>(std::forward<Args>(args)...);
auto widget = std::make_unique<T>(std::forward<Args>(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<std::unique_ptr<Widget>>& children();
const std::vector<std::unique_ptr<Widget>>& children() const;
virtual void on_update(float dt);
virtual void on_render(Renderer& renderer);
virtual glm::vec2 compute_position() const;
private:
std::vector<std::unique_ptr<Widget>> m_children;
};