HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
alignment.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019, 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "assert.hpp"
11#include "cast.hpp"
12
13namespace hi::inline v1 {
14
17enum class vertical_alignment : uint8_t {
20 top = 0,
21
24 middle = 1,
25
28 bottom = 2
29};
30
31enum class horizontal_alignment : uint8_t {
37 flush = 0,
38
43 left = 1,
44
49 center = 2,
50
55 justified = 3,
56
61 right = 4,
62};
63
64[[nodiscard]] constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
65{
66 if (rhs == horizontal_alignment::left) {
67 return horizontal_alignment::right;
68 } else if (rhs == horizontal_alignment::right) {
69 return horizontal_alignment::left;
70 } else {
71 return rhs;
72 }
73}
74
75class alignment {
76public:
77 constexpr alignment() noexcept : _value(0) {}
78 constexpr alignment(alignment const &) noexcept = default;
79 constexpr alignment(alignment &&) noexcept = default;
80 constexpr alignment &operator=(alignment const &) noexcept = default;
81 constexpr alignment &operator=(alignment &&) noexcept = default;
82
83 constexpr explicit alignment(uint8_t value) noexcept : _value(value) {}
84
85 constexpr alignment(horizontal_alignment t, vertical_alignment v) noexcept :
86 _value((to_underlying(v) << 4) | to_underlying(t))
87 {
88 hi_axiom(to_underlying(v) <= 0xf);
89 hi_axiom(to_underlying(t) <= 0xf);
90 }
91
92 constexpr alignment(vertical_alignment v, horizontal_alignment t) noexcept :
93 _value((to_underlying(v) << 4) | to_underlying(t))
94 {
95 hi_axiom(to_underlying(v) <= 0xf);
96 hi_axiom(to_underlying(t) <= 0xf);
97 }
98
99 [[nodiscard]] static constexpr alignment top_flush() noexcept
100 {
101 return {horizontal_alignment::flush, vertical_alignment::top};
102 }
103
104 [[nodiscard]] static constexpr alignment top_left() noexcept
105 {
106 return {horizontal_alignment::left, vertical_alignment::top};
107 }
108
109 [[nodiscard]] static constexpr alignment top_center() noexcept
110 {
111 return {horizontal_alignment::center, vertical_alignment::top};
112 }
113
114 [[nodiscard]] static constexpr alignment top_justified() noexcept
115 {
116 return {horizontal_alignment::justified, vertical_alignment::top};
117 }
118
119 [[nodiscard]] static constexpr alignment top_right() noexcept
120 {
121 return {horizontal_alignment::right, vertical_alignment::top};
122 }
123
124 [[nodiscard]] static constexpr alignment middle_flush() noexcept
125 {
126 return {horizontal_alignment::flush, vertical_alignment::middle};
127 }
128
129 [[nodiscard]] static constexpr alignment middle_left() noexcept
130 {
131 return {horizontal_alignment::left, vertical_alignment::middle};
132 }
133
134 [[nodiscard]] static constexpr alignment middle_center() noexcept
135 {
136 return {horizontal_alignment::center, vertical_alignment::middle};
137 }
138
139 [[nodiscard]] static constexpr alignment middle_justified() noexcept
140 {
141 return {horizontal_alignment::justified, vertical_alignment::middle};
142 }
143
144 [[nodiscard]] static constexpr alignment middle_right() noexcept
145 {
146 return {horizontal_alignment::right, vertical_alignment::middle};
147 }
148
149 [[nodiscard]] static constexpr alignment bottom_left() noexcept
150 {
151 return {horizontal_alignment::left, vertical_alignment::bottom};
152 }
153
154 [[nodiscard]] static constexpr alignment bottom_center() noexcept
155 {
156 return {horizontal_alignment::center, vertical_alignment::bottom};
157 }
158
159 [[nodiscard]] static constexpr alignment bottom_right() noexcept
160 {
161 return {horizontal_alignment::right, vertical_alignment::bottom};
162 }
163
164 [[nodiscard]] constexpr horizontal_alignment text() const noexcept
165 {
166 return static_cast<horizontal_alignment>(_value & 0xf);
167 }
168
169 [[nodiscard]] constexpr vertical_alignment vertical() const noexcept
170 {
171 return static_cast<vertical_alignment>(_value >> 4);
172 }
173
174 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, alignment const &rhs) noexcept = default;
175
176 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, horizontal_alignment const &rhs) noexcept
177 {
178 return lhs.text() == rhs;
179 }
180
181 [[nodiscard]] constexpr friend bool operator==(horizontal_alignment const &lhs, alignment const &rhs) noexcept
182 {
183 return lhs == rhs.text();
184 }
185
186 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, vertical_alignment const &rhs) noexcept
187 {
188 return lhs.vertical() == rhs;
189 }
190
191 [[nodiscard]] constexpr friend bool operator==(vertical_alignment const &lhs, alignment const &rhs) noexcept
192 {
193 return lhs == rhs.vertical();
194 }
195
196 [[nodiscard]] constexpr friend alignment mirror(alignment const &rhs) noexcept
197 {
198 return alignment{mirror(rhs.text()), rhs.vertical()};
199 }
200
201private:
207 uint8_t _value;
208};
209
217{
218 return alignment{lhs, rhs};
219}
220
228{
229 return alignment{lhs, rhs};
230}
231
232} // namespace hi::inline v1
Utilities to assert and bound check.
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:216
vertical_alignment
Vertical alignment.
Definition alignment.hpp:17
@ middle
Align to the vertical-middle.
@ bottom
Align to the bottom.
@ top
Align to the top.
horizontal_alignment
Definition alignment.hpp:31
@ justified
Stretch the text to be flush to both sides.
@ center
Align the text in the center.
Definition alignment.hpp:75
A variant of text.
Definition label.hpp:36