aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/flex_layout_render_object.cpp
blob: 0093f1adb5ee5c6c4d9803a1ca63dc18ea4b5cb5 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "cru/ui/render/flex_layout_render_object.hpp"

#include "cru/platform/debug.hpp"
#include "cru/platform/graph/util/painter_util.hpp"

#include <algorithm>
#include <cassert>
#include <functional>

namespace cru::ui::render {
FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) {
  assert(position >= 0 &&
         position < child_layout_data_.size());  // Position out of bound.

  return &child_layout_data_[position];
}

void FlexLayoutRenderObject::Draw(platform::graph::IPainter* painter) {
  for (const auto child : GetChildren()) {
    auto offset = child->GetOffset();
    platform::graph::util::WithTransform(
        painter, platform::Matrix::Translation(offset.x, offset.y),
        [child](auto p) { child->Draw(p); });
  }
}

RenderObject* FlexLayoutRenderObject::HitTest(const Point& point) {
  const auto& children = GetChildren();
  for (auto i = children.crbegin(); i != children.crend(); ++i) {
    auto offset = (*i)->GetOffset();
    Point p{point.x - offset.x, point.y - offset.y};
    const auto result = (*i)->HitTest(point);
    if (result != nullptr) {
      return result;
    }
  }

  const auto margin = GetMargin();
  const auto size = GetSize();
  return Rect{margin.left, margin.top,
              std::max(size.width - margin.GetHorizontalTotal(), 0.0f),
              std::max(size.height - margin.GetVerticalTotal(), 0.0f)}
                 .IsPointInside(point)
             ? this
             : nullptr;
}  // namespace cru::ui::render

void FlexLayoutRenderObject::OnAddChild(RenderObject* new_child, int position) {
  child_layout_data_.emplace(child_layout_data_.cbegin() + position);
}

void FlexLayoutRenderObject::OnRemoveChild(RenderObject* removed_child,
                                           int position) {
  child_layout_data_.erase(child_layout_data_.cbegin() + position);
}

Size FlexLayoutRenderObject::OnMeasureContent(const Size& available_size) {
  std::vector<int> has_basis_children;
  std::vector<int> no_basis_children;
  std::vector<int> grow_children;
  std::vector<int> shrink_chilren;
  for (int i = 0; i < child_layout_data_.size(); i++) {
    const auto& layout_data = child_layout_data_[i];
    if (layout_data.flex_basis.has_value())
      has_basis_children.push_back(i);
    else
      no_basis_children.push_back(i);
    if (layout_data.flex_grow > 0) grow_children.push_back(i);
    if (layout_data.flex_shrink > 0) shrink_chilren.push_back(i);
  }

  std::function<float(const Size&)> get_main_length;
  std::function<float(const Size&)> get_cross_length;
  std::function<Size(float main, float cross)> create_size;

  if (direction_ == FlexDirection::Horizontal ||
      direction_ == FlexDirection::HorizontalReverse) {
    get_main_length = [](const Size& size) { return size.width; };
    get_cross_length = [](const Size& size) { return size.height; };
    create_size = [](float main, float cross) { return Size(main, cross); };
  } else {
    get_main_length = [](const Size& size) { return size.height; };
    get_cross_length = [](const Size& size) { return size.width; };
    create_size = [](float main, float cross) { return Size(cross, main); };
  }

  const auto& children = GetChildren();

  float remain_main_length = get_main_length(available_size);
  float max_cross_length = 0;

  for (const int i : has_basis_children) {
    const auto child = children[i];
    const float basis = child_layout_data_[i].flex_basis.value();
    child->Measure(create_size(basis, get_cross_length(available_size)));
    remain_main_length -= basis;
    const float child_preferred_cross_length =
        get_cross_length(child->GetPreferredSize());
    child->SetPreferredSize(create_size(basis, child_preferred_cross_length));
    max_cross_length = std::max(max_cross_length, child_preferred_cross_length);
  }

  for (const int i : no_basis_children) {
    const auto child = children[i];
    child->Measure(create_size(remain_main_length > 0 ? remain_main_length : 0,
                               get_cross_length(available_size)));
    remain_main_length -= get_main_length(child->GetPreferredSize());
    max_cross_length =
        std::max(max_cross_length, get_cross_length(child->GetPreferredSize()));
  }

  if (remain_main_length > 0) {
    float total_grow = 0;
    for (const int i : grow_children)
      total_grow += child_layout_data_[i].flex_grow;

    for (const int i : grow_children) {
      const float distributed_grow_length =
          remain_main_length * (child_layout_data_[i].flex_grow / total_grow);
      const auto child = children[i];
      const float new_main_length =
          get_main_length(child->GetPreferredSize()) + distributed_grow_length;
      child->Measure(
          create_size(new_main_length, get_cross_length(available_size)));
      const float new_child_preferred_cross_length =
          get_cross_length(child->GetPreferredSize());
      child->SetPreferredSize(
          create_size(new_main_length, new_child_preferred_cross_length));
      max_cross_length =
          std::max(max_cross_length, new_child_preferred_cross_length);
    }
  }

  if (remain_main_length < 0) {
    float total_shrink = 0;
    for (const int i : shrink_chilren)
      total_shrink += child_layout_data_[i].flex_shrink;

    for (const int i : shrink_chilren) {
      const float distributed_shrink_length =  // negative
          remain_main_length *
          (child_layout_data_[i].flex_shrink / total_shrink);
      const auto child = children[i];
      float new_main_length = get_main_length(child->GetPreferredSize()) +
                              distributed_shrink_length;
      new_main_length = new_main_length > 0 ? new_main_length : 0;
      child->Measure(
          create_size(new_main_length, get_cross_length(available_size)));
      const float new_child_preferred_cross_length =
          get_cross_length(child->GetPreferredSize());
      child->SetPreferredSize(
          create_size(new_main_length, new_child_preferred_cross_length));
      max_cross_length =
          std::max(max_cross_length, new_child_preferred_cross_length);
    }
  }

  return create_size(get_main_length(available_size) -
                         (remain_main_length > 0 ? remain_main_length : 0),
                     max_cross_length);
}

void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) {
  auto calculate_anchor = [](Alignment alignment, float start_point,
                             float total_length,
                             float content_length) -> float {
    switch (alignment) {
      case Alignment::Start:
        return start_point;
      case Alignment::Center:
        return start_point + (total_length - content_length) / 2.0f;
      case Alignment::End:
        return start_point + total_length - content_length;
      default:
        return 0;
    }
  };

  const auto& children = GetChildren();
  if (direction_ == FlexDirection::Horizontal ||
      direction_ == FlexDirection::HorizontalReverse) {
    float actual_content_width = 0;
    for (const auto child : children) {
      actual_content_width += child->GetPreferredSize().width;
    }

    const float content_anchor_x = calculate_anchor(
        content_main_align_, 0, content_rect.width, actual_content_width);

    float anchor_x = 0;
    for (int i = 0; i < children.size(); i++) {
      const auto child = children[i];
      const auto size = child->GetPreferredSize();

      float real_anchor_x = anchor_x + content_anchor_x;
      if (direction_ == FlexDirection::Horizontal)
        real_anchor_x = content_rect.left + real_anchor_x;
      else
        real_anchor_x = content_rect.GetRight() - real_anchor_x;
      child->Layout(Rect{
          real_anchor_x,
          calculate_anchor(child_layout_data_[i].alignment, content_rect.top,
                           content_rect.height, size.height),
          size.width, size.height});

      anchor_x += size.width;
    }
  } else {
    float actual_content_height = 0;
    for (const auto child : children) {
      actual_content_height = child->GetPreferredSize().height;
    }

    const float content_anchor_y = calculate_anchor(
        content_main_align_, 0, content_rect.height, actual_content_height);

    float anchor_y = 0;
    for (int i = 0; i < children.size(); i++) {
      const auto child = children[i];
      const auto size = child->GetPreferredSize();

      float real_anchor_y = anchor_y + content_anchor_y;
      if (direction_ == FlexDirection::Vertical) {
        real_anchor_y = content_rect.top + real_anchor_y;
      } else {
        real_anchor_y = content_rect.GetBottom() - real_anchor_y;
      }
      child->Layout(Rect{
          real_anchor_y,
          calculate_anchor(child_layout_data_[i].alignment, content_rect.left,
                           content_rect.width, size.width),
          size.width, size.height});

      anchor_y += size.height;
    }
  }
}
}  // namespace cru::ui::render