aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/LayoutRenderObject.h
blob: c8678deb9fc297a38c35366d627160724509fede (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "RenderObject.h"

#include "cru/platform/graphics/Painter.h"

namespace cru::ui::render {
template <typename TChildLayoutData>
class CRU_UI_API LayoutRenderObject : public RenderObject {
 public:
  using ChildLayoutData = TChildLayoutData;

 private:
  struct ChildData {
    RenderObject* render_object;
    ChildLayoutData layout_data;
  };

 protected:
  LayoutRenderObject() = default;

 public:
  CRU_DELETE_COPY(LayoutRenderObject)
  CRU_DELETE_MOVE(LayoutRenderObject)

  ~LayoutRenderObject() override = default;

  Index GetChildCount() const { return static_cast<Index>(children_.size()); }
  RenderObject* GetChildAt(Index position) {
    Expects(position >= 0 && position < GetChildCount());
    return children_[position].render_object;
  }
  void AddChild(RenderObject* render_object, Index position) {
    if (position < 0) position = 0;
    if (position > GetChildCount()) position = GetChildCount();
    children_.insert(children_.begin() + position,
                     ChildData{render_object, ChildLayoutData()});
    render_object->SetParent(this);
    InvalidateLayout();
  }

  void RemoveChild(Index position) {
    Expects(position > 0 && position < GetChildCount());
    children_[position].render_object->SetParent(nullptr);
    children_.erase(children_.begin() + position);
    InvalidateLayout();
  }

  void ClearChildren() {
    for (auto child : children_) {
      child.render_object->SetParent(nullptr);
    }
    children_.clear();
    InvalidateLayout();
  }

  const ChildLayoutData& GetChildLayoutDataAt(Index position) const {
    Expects(position >= 0 && position < GetChildCount());
    return children_[position].layout_data;
  }

  void SetChildLayoutDataAt(Index position, ChildLayoutData data) {
    Expects(position >= 0 && position < GetChildCount());
    children_[position].layout_data = std::move(data);
    InvalidateLayout();
  }

  void Draw(platform::graphics::IPainter* painter) override {
    for (const auto& child : children_) {
      painter->PushState();
      painter->ConcatTransform(
          Matrix::Translation(child.render_object->GetOffset()));
      child.render_object->Draw(painter);
      painter->PopState();
    }
  }

  RenderObject* HitTest(const Point& point) override {
    const auto child_count = GetChildCount();
    for (auto i = child_count - 1; i >= 0; --i) {
      const auto child = GetChildAt(i);
      const auto result = child->HitTest(point - child->GetOffset());
      if (result != nullptr) {
        return result;
      }
    }

    return GetPaddingRect().IsPointInside(point) ? this : nullptr;
  }

 private:
  std::vector<ChildData> children_;
};
}  // namespace cru::ui::render