aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/FlexLayout.cpp
blob: 05f6999fe5d233edf4546113bf0b6cfb1715f2bb (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
#include "cru/ui/controls/FlexLayout.hpp"

#include "cru/ui/render/FlexLayoutRenderObject.hpp"

namespace cru::ui::controls {
using render::FlexLayoutRenderObject;

FlexLayout::FlexLayout() {
  render_object_.reset(new FlexLayoutRenderObject());
  render_object_->SetAttachedControl(this);
}

FlexLayout::~FlexLayout() = default;

render::RenderObject* FlexLayout::GetRenderObject() const {
  return render_object_.get();
}

namespace {
Index FindPosition(render::RenderObject* parent, render::RenderObject* child) {
  const auto& render_objects = parent->GetChildren();
  const auto find_result =
      std::find(render_objects.cbegin(), render_objects.cend(), child);
  if (find_result == render_objects.cend()) {
    throw std::logic_error("Control is not a child of FlexLayout.");
  }
  return static_cast<Index>(find_result - render_objects.cbegin());
}
}  // namespace

FlexChildLayoutData FlexLayout::GetChildLayoutData(Control* control) {
  Expects(control);
  return render_object_->GetChildLayoutData(
      FindPosition(render_object_.get(), control->GetRenderObject()));
}

void FlexLayout::SetChildLayoutData(Control* control,
                                    FlexChildLayoutData data) {
  Expects(control);
  render_object_->SetChildLayoutData(
      FindPosition(render_object_.get(), control->GetRenderObject()),
      std::move(data));
}

FlexMainAlignment FlexLayout::GetContentMainAlign() const {
  return render_object_->GetContentMainAlign();
}

void FlexLayout::SetContentMainAlign(FlexMainAlignment value) {
  if (value == GetContentMainAlign()) return;
  render_object_->SetContentMainAlign(value);
}

FlexDirection FlexLayout::GetFlexDirection() const {
  return render_object_->GetFlexDirection();
}

void FlexLayout::SetFlexDirection(FlexDirection direction) {
  if (direction == GetFlexDirection()) return;
  render_object_->SetFlexDirection(direction);
}

FlexCrossAlignment FlexLayout::GetItemCrossAlign() const {
  return render_object_->GetItemCrossAlign();
}

void FlexLayout::SetItemCrossAlign(FlexCrossAlignment alignment) {
  if (alignment == GetItemCrossAlign()) return;
  render_object_->SetItemCrossAlign(alignment);
}

void FlexLayout::OnAddChild(Control* child, const Index position) {
  render_object_->AddChild(child->GetRenderObject(), position);
}

void FlexLayout::OnRemoveChild(Control* child, const Index position) {
  CRU_UNUSED(child)

  render_object_->RemoveChild(position);
}
}  // namespace cru::ui::controls