aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/LayoutControl.h
blob: b42cfc9a6e61488c332bb829603ab863392e9749 (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
61
62
63
64
65
66
67
68
69
#pragma once
#include "Control.h"

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

 public:
  LayoutControl(const LayoutControl& other) = delete;
  LayoutControl(LayoutControl&& other) = delete;
  LayoutControl& operator=(const LayoutControl& other) = delete;
  LayoutControl& operator=(LayoutControl&& other) = delete;
  ~LayoutControl() override = default;

 public:
  void ForEachChild(const std::function<void(Control*)>& callback) override {
    for (auto child : children_) {
      callback(child);
    }
  }

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

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

  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:
  std::unique_ptr<TRenderObject> container_render_object_;

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