aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/flex_layout_render_object.hpp
blob: 7b71b96e6df14acb52712855b700b547120ba1fa (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
#pragma once
#include "render_object.hpp"

#include <optional>

namespace cru::ui::render {
enum class FlexDirection {
  Horizontal,
  HorizontalReverse,
  Vertical,
  VertivalReverse
};

namespace internal {
constexpr int align_start = 0;
constexpr int align_end = align_start + 1;
constexpr int align_center = align_end + 1;
// constexpr int align_stretch = align_center + 1;
}  // namespace internal

enum class FlexMainAlignment {
  Start = internal::align_start,
  End = internal::align_end,
  Center = internal::align_center
};
enum class FlexCrossAlignment {
  Start = internal::align_start,
  End = internal::align_end,
  Center = internal::align_center,
  //  Stretch = internal::align_stretch
};

struct FlexChildLayoutData {
  // nullopt stands for looking at my content
  std::optional<float> flex_basis = std::nullopt;
  float flex_grow = 0;
  float flex_shrink = 0;
  // nullopt stands for looking at parent's setting
  std::optional<FlexCrossAlignment> cross_alignment = std::nullopt;
};

class FlexLayoutRenderObject : public RenderObject {
 public:
  FlexLayoutRenderObject();
  FlexLayoutRenderObject(const FlexLayoutRenderObject& other) = delete;
  FlexLayoutRenderObject& operator=(const FlexLayoutRenderObject& other) =
      delete;
  FlexLayoutRenderObject(FlexLayoutRenderObject&& other) = delete;
  FlexLayoutRenderObject& operator=(FlexLayoutRenderObject&& other) = delete;
  ~FlexLayoutRenderObject() override = default;

  FlexDirection GetFlexDirection() const { return direction_; }
  void SetFlexDirection(FlexDirection direction) {
    direction_ = direction;
    InvalidateLayout();
  }

  FlexMainAlignment GetContentMainAlign() const { return content_main_align_; }
  void SetContentMainAlign(FlexMainAlignment align) {
    content_main_align_ = align;
    InvalidateLayout();
  }

  FlexCrossAlignment GetItemCrossAlign() const { return item_cross_align_; }
  void SetItemCrossAlign(FlexCrossAlignment align) {
    item_cross_align_ = align;
    InvalidateLayout();
  }

  FlexChildLayoutData GetChildLayoutData(int position) {
    assert(position >= 0 && position < child_layout_data_.size());
    return child_layout_data_[position];
  }

  void SetChildLayoutData(int position, const FlexChildLayoutData& data) {
    assert(position >= 0 && position < child_layout_data_.size());
    child_layout_data_[position] = data;
    InvalidateLayout();
  }

  void Draw(platform::graph::Painter* painter) override;

  RenderObject* HitTest(const Point& point) override;

 protected:
  void OnAddChild(RenderObject* new_child, int position) override;
  void OnRemoveChild(RenderObject* removed_child, int position) override;

  Size OnMeasureContent(const Size& available_size) override;
  void OnLayoutContent(const Rect& content_rect) override;

 private:
  FlexDirection direction_ = FlexDirection::Horizontal;
  FlexMainAlignment content_main_align_ = FlexMainAlignment::Start;
  FlexCrossAlignment item_cross_align_ = FlexCrossAlignment::Center;
  std::vector<FlexChildLayoutData> child_layout_data_{};
};
}  // namespace cru::ui::render