aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/LayoutRenderObject.h
blob: 99dd80579c40ada0227641e9711a63d7a2889684 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include "RenderObject.h"

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

 private:
  struct ChildData {
    /**
     * May be nullptr.
     */
    RenderObject* render_object;
    ChildLayoutData layout_data;
    EventHandlerRevokerListGuard event_guard;
  };

 protected:
  LayoutRenderObject(std::string name) : RenderObject(std::move(name)) {}

 public:
  Index GetChildCount() { 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();
    auto iter = children_.insert(children_.begin() + position,
                                 ChildData{render_object, ChildLayoutData()});
    render_object->SetParent(this);
    iter->event_guard +=
        render_object->DestroyEvent()->AddSpyOnlyHandler([this, render_object] {
          auto iter = std::ranges::find_if(
              children_, [render_object](const ChildData& data) {
                return data.render_object == render_object;
              });
          if (iter != children_.cend()) {
            iter->render_object = nullptr;
          }
        });
    InvalidateLayout();
  }

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

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

  const ChildLayoutData& GetChildLayoutDataAt(Index position) {
    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();
  }

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

 protected:
  void OnDraw(RenderObjectDrawContext& context) override {
    auto painter = context.painter;
    for (const auto& child : children_) {
      context.DrawChild(child.render_object);
    }
  }

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