aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ui_base.hpp
blob: d9c9d0b2ab565010b68690ec9506082c405bcf33 (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
#pragma once

#include <optional>


namespace cru::ui
{
    struct Point
    {
        constexpr static Point Zero()
        {
            return Point(0, 0);
        }

        constexpr Point() = default;
        constexpr Point(const float x, const float y) : x(x), y(y) { }

        float x = 0;
        float y = 0;
    };

    constexpr bool operator==(const Point& left, const Point& right)
    {
        return left.x == right.x && left.y == right.y;
    }

    constexpr bool operator!=(const Point& left, const Point& right)
    {
        return !(left == right);
    }

    struct Size
    {
        constexpr static Size Zero()
        {
            return Size(0, 0);
        }

        constexpr Size() = default;
        constexpr Size(const float width, const float height) : width(width), height(height) { }

        float width = 0;
        float height = 0;
    };

    constexpr Size operator + (const Size& left, const Size& right)
    {
        return Size(left.width + right.width, left.height + right.height);
    }

    constexpr Size operator - (const Size& left, const Size& right)
    {
        return Size(left.width - right.width, left.height - right.height);
    }

    constexpr bool operator==(const Size& left, const Size& right)
    {
        return left.width == right.width && left.height == right.height;
    }

    constexpr bool operator!=(const Size& left, const Size& right)
    {
        return !(left == right);
    }

    struct Thickness
    {
        constexpr static Thickness Zero()
        {
            return Thickness(0);
        }

        constexpr Thickness() : Thickness(0) { }

        constexpr explicit Thickness(const float width)
            : left(width), top(width), right(width), bottom(width) { }

        constexpr explicit Thickness(const float horizontal, const float vertical)
            : left(horizontal), top(vertical), right(horizontal), bottom(vertical) { }

        constexpr Thickness(const float left, const float top, const float right, const float bottom)
            : left(left), top(top), right(right), bottom(bottom) { }

        float GetHorizontalTotal() const
        {
            return left + right;
        }

        float GetVerticalTotal() const
        {
            return top + bottom;
        }

        void SetLeftRight(const float value)
        {
            left = right = value;
        }

        void SetTopBottom(const float value)
        {
            top = bottom = value;
        }

        void SetAll(const float value)
        {
            left = top = right = bottom = value;
        }

        float Validate() const
        {
            return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0;
        }

        float left;
        float top;
        float right;
        float bottom;
    };

    struct Rect
    {
        constexpr Rect() = default;
        constexpr Rect(const float left, const float top, const float width, const float height)
            : left(left), top(top), width(width), height(height) { }
        constexpr Rect(const Point& lefttop, const Size& size)
            : left(lefttop.x), top(lefttop.y), width(size.width), height(size.height) { }

        constexpr static Rect FromVertices(const float left, const float top, const float right, const float bottom)
        {
            return Rect(left, top, right - left, bottom - top);
        }

        constexpr float GetRight() const
        {
            return left + width;
        }

        constexpr float GetBottom() const
        {
            return top + height;
        }

        constexpr Point GetLeftTop() const
        {
            return Point(left, top);
        }

        constexpr Point GetRightBottom() const
        {
            return Point(left + width, top + height);
        }

        constexpr Size GetSize() const
        {
            return Size(width, height);
        }

        constexpr Rect Shrink(const Thickness& thickness) const
        {
            return Rect(left + thickness.left, top + thickness.top, width - thickness.GetHorizontalTotal(), height - thickness.GetVerticalTotal());
        }

        constexpr bool IsPointInside(const Point& point) const
        {
            return
                point.x >= left &&
                point.x < GetRight() &&
                point.y >= top &&
                point.y < GetBottom();
        }

        float left = 0.0f;
        float top = 0.0f;
        float width = 0.0f;
        float height = 0.0f;
    };

    enum class MouseButton
    {
        Left,
        Right,
        Middle
    };

    struct TextRange
    {
        constexpr static std::optional<TextRange> FromTwoSides(unsigned first, unsigned second)
        {
            if (first > second)
                return std::make_optional<TextRange>(second, first - second);
            if (first < second)
                return std::make_optional<TextRange>(first, second - first);
            return std::nullopt;
        }

        constexpr static std::pair<unsigned, unsigned> ToTwoSides(std::optional<TextRange> text_range, unsigned default_position = 0)
        {
            if (text_range.has_value())
                return std::make_pair(text_range.value().position, text_range.value().position + text_range.value().count);
            return std::make_pair(default_position, default_position);
        }

        constexpr TextRange() = default;
        constexpr TextRange(const unsigned position, const unsigned count)
            : position(position), count(count)
        {

        }

        unsigned position = 0;
        unsigned count = 0;
    };

    bool IsKeyDown(int virtual_code);
    bool IsKeyToggled(int virtual_code);
    bool IsAnyMouseButtonDown();
}