aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/SingleChildControl.h
blob: 037d6dd39570a8e4b947103575a07a83d5e211cd (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
#pragma once
#include "Control.h"

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

 public:
  CRU_DELETE_COPY(SingleChildControl)
  CRU_DELETE_MOVE(SingleChildControl)

  ~SingleChildControl() override {}

  Control* GetChild() const { return child_; }
  void SetChild(Control* child) {
    if (child == child_) return;

    assert(child == nullptr || child->GetParent() == nullptr);

    if (child_) {
      child_->SetParent(nullptr);
    }

    child_ = child;

    if (child) {
      child->SetParent(this);
    }

    container_render_object_->SetChild(child->GetRenderObject());
  }

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

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

  void ForEachChild(const std::function<void(Control*)>& predicate) override {
    if (child_) {
      predicate(child_);
    }
  }

 private:
  Control* child_;
  std::unique_ptr<TRenderObject> container_render_object_;
};
}  // namespace cru::ui::controls