aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/LayoutControl.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/ui/controls/LayoutControl.h')
-rw-r--r--include/cru/ui/controls/LayoutControl.h62
1 files changed, 47 insertions, 15 deletions
diff --git a/include/cru/ui/controls/LayoutControl.h b/include/cru/ui/controls/LayoutControl.h
index 639ecc74..b42cfc9a 100644
--- a/include/cru/ui/controls/LayoutControl.h
+++ b/include/cru/ui/controls/LayoutControl.h
@@ -2,11 +2,12 @@
#include "Control.h"
namespace cru::ui::controls {
+template <typename TRenderObject>
class CRU_UI_API LayoutControl : public Control {
protected:
- LayoutControl() = default;
- explicit LayoutControl(render::RenderObject* container_render_object)
- : container_render_object_(container_render_object) {}
+ LayoutControl() : container_render_object_(new TRenderObject()) {
+ container_render_object_->SetAttachedControl(this);
+ }
public:
LayoutControl(const LayoutControl& other) = delete;
@@ -15,23 +16,54 @@ class CRU_UI_API LayoutControl : public Control {
LayoutControl& operator=(LayoutControl&& other) = delete;
~LayoutControl() override = default;
- using Control::AddChild;
- using Control::RemoveChild;
+ public:
+ void ForEachChild(const std::function<void(Control*)>& callback) override {
+ for (auto child : children_) {
+ callback(child);
+ }
+ }
- void ClearChildren();
+ render::RenderObject* GetRenderObject() const override {
+ return container_render_object_.get();
+ }
- protected:
- // If container render object is not null. Render object of added or removed
- // child control will automatically sync to the container render object.
- render::RenderObject* GetContainerRenderObject() const;
- void SetContainerRenderObject(render::RenderObject* ro) {
- container_render_object_ = ro;
+ TRenderObject* GetContainerRenderObject() const {
+ return container_render_object_.get();
+ }
+
+ void AddChild(Control* child, Index position) {
+ Expects(child);
+ Expects(child->GetParent() == nullptr);
+ if (position < 0) position = 0;
+ if (position > children_.size()) position = children_.size();
+ children_.insert(children_.begin() + position, child);
+ child->SetParent(this);
+
+ assert(child->GetRenderObject());
+ container_render_object_->AddChild(child->GetRenderObject(), position);
+ }
+
+ void RemoveChild(Index position) {
+ if (position < 0 || position >= children_.size()) return;
+ auto child = children_[position];
+ children_.erase(children_.begin() + position);
+ child->SetParent(nullptr);
+ container_render_object_->RemoveChild(position);
}
- void OnAddChild(Control* child, Index position) override;
- void OnRemoveChild(Control* child, Index position) override;
+ const typename TRenderObject::ChildLayoutData& GetChildLayoutData(
+ Index position) {
+ return container_render_object_->GetChildLayoutDataAt(position);
+ }
+
+ void SetChildLayoutData(Index position,
+ typename TRenderObject::ChildLayoutData data) {
+ container_render_object_->SetChildLayoutDataAt(position, data);
+ }
private:
- render::RenderObject* container_render_object_ = nullptr;
+ std::unique_ptr<TRenderObject> container_render_object_;
+
+ std::vector<Control*> children_;
};
} // namespace cru::ui::controls