aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/LayoutControl.h
blob: fad865308c8d841daa8eb0491a7be01187d14691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include "Control.h"

namespace cru::ui::controls {
template <typename TRenderObject>
class LayoutControl : public Control {
 protected:
  LayoutControl() : container_render_object_(new TRenderObject()) {
    container_render_object_->SetAttachedControl(this);
  }

 public:
  using Control::AddChild;
  using Control::InsertChildAt;
  using Control::RemoveChildAt;

  Index GetChildCount() const { return children_.size(); }
  Control* GetChildAt(Index index) const { return children_[index]; }

  Index IndexOfChild(Control* control) const {
    auto it = std::find(children_.begin(), children_.end(), control);
    if (it == children_.end()) {
      return -1;
    }
    return it - children_.begin();
  }

  render::RenderObject* GetRenderObject() const override {
    return container_render_object_.get();
  }

  TRenderObject* GetContainerRenderObject() const {
    return container_render_object_.get();
  }

  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);
  }

 protected:
  void OnChildInserted(Control* control, Index index) override {
    container_render_object_->AddChild(control->GetRenderObject(), index);
  }

  void OnChildRemoved([[maybe_unused]] Control* control, Index index) override {
    container_render_object_->RemoveChild(index);
  }

 private:
  std::unique_ptr<TRenderObject> container_render_object_;

  std::vector<Control*> children_;
};
}  // namespace cru::ui::controls