aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/Base.hpp
blob: db360c07fc25c27ee2a43e0f44321ed73e7c2d5a (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
#pragma once
#include "cru/common/Base.hpp"
#include "cru/platform/graphics/Base.hpp"
#include "cru/platform/gui/Base.hpp"

#include <functional>
#include <memory>
#include <optional>
#include <vector>

#ifdef CRU_PLATFORM_WINDOWS
#ifdef CRU_UI_EXPORT_API
#define CRU_UI_API __declspec(dllexport)
#else
#define CRU_UI_API __declspec(dllimport)
#endif
#else
#define CRU_UI_API
#endif

namespace cru::ui {
//-------------------- region: import --------------------
using cru::platform::Color;
using cru::platform::Ellipse;
using cru::platform::Matrix;
using cru::platform::Point;
using cru::platform::Rect;
using cru::platform::RoundedRect;
using cru::platform::Size;
using cru::platform::TextRange;
using cru::platform::Thickness;
using cru::platform::gui::MouseButton;

namespace mouse_buttons = cru::platform::gui::mouse_buttons;

namespace colors = cru::platform::colors;

//-------------------- region: forward declaration --------------------

namespace controls {
class Window;
class Control;
}  // namespace controls

namespace host {
class WindowHost;
}

namespace render {
class RenderObject;
}

namespace style {
class StyleRuleSet;
class StyleRuleSetBind;
}  // namespace style

//-------------------- region: basic types --------------------
enum class Direction { Horizontal, Vertical };

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

enum class Alignment {
  Start = internal::align_start,
  End = internal::align_end,
  Center = internal::align_center,
};

struct CornerRadius {
  constexpr CornerRadius()
      : left_top(), right_top(), left_bottom(), right_bottom() {}
  constexpr CornerRadius(const float& value)
      : CornerRadius(Point{value, value}) {}
  constexpr CornerRadius(const Point& value)
      : left_top(value),
        right_top(value),
        left_bottom(value),
        right_bottom(value) {}
  constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom,
                         Point right_bottom)
      : left_top(left_top),
        right_top(right_top),
        left_bottom(left_bottom),
        right_bottom(right_bottom) {}

  CornerRadius& SetAll(const Point& point) {
    left_top = right_top = left_bottom = right_bottom = point;
    return *this;
  }

  Point left_top;
  Point right_top;
  Point left_bottom;
  Point right_bottom;
};

inline bool operator==(const CornerRadius& left, const CornerRadius& right) {
  return left.left_top == right.left_top &&
         left.left_bottom == right.left_bottom &&
         left.right_top == right.right_top &&
         left.right_bottom == right.right_bottom;
}

inline bool operator!=(const CornerRadius& left, const CornerRadius& right) {
  return !(left == right);
}

class CanvasPaintEventArgs {
 public:
  CanvasPaintEventArgs(platform::graphics::IPainter* painter,
                       const Size& paint_size)
      : painter_(painter), paint_size_(paint_size) {}
  CRU_DEFAULT_COPY(CanvasPaintEventArgs)
  CRU_DEFAULT_MOVE(CanvasPaintEventArgs)
  ~CanvasPaintEventArgs() = default;

  platform::graphics::IPainter* GetPainter() const { return painter_; }
  Size GetPaintSize() const { return paint_size_; }

 private:
  platform::graphics::IPainter* painter_;
  Size paint_size_;
};

enum class FlexDirection {
  Horizontal,
  HorizontalReverse,
  Vertical,
  VertivalReverse
};

using FlexMainAlignment = Alignment;
using FlexCrossAlignment = Alignment;

struct FlexChildLayoutData {
  float expand_factor = 0;
  float shrink_factor = 1;
  // nullopt stands for looking at parent's setting
  std::optional<FlexCrossAlignment> cross_alignment = std::nullopt;
};

struct StackChildLayoutData {
  Alignment horizontal = Alignment::Start;
  Alignment vertical = Alignment::Start;
};
}  // namespace cru::ui