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
9#pragma once
10
11#include "../utility/utility.hpp"
12#include "../macros.hpp"
13#include <optional>
14
15
16
17namespace hi { inline namespace v1 {
18
22enum class vertical_alignment : uint8_t {
25 none = 0,
26
29 top = 1,
30
33 middle = 2,
34
37 bottom = 3
38};
39
60[[nodiscard]] constexpr std::optional<float> make_guideline(
62 float bottom,
63 float top,
64 float padding_bottom,
65 float padding_top,
66 float guideline_width)
67{
68 hi_axiom(bottom <= top);
69 hi_axiom(guideline_width >= 0.0f);
70
73 hilet guideline_middle = (bottom + top - guideline_width) / 2.0f;
74
75 switch (alignment) {
77 return {};
79 if (guideline_bottom <= top) {
80 return guideline_top;
81 } else {
82 return {};
83 }
85 if (guideline_top >= bottom) {
86 return guideline_top;
87 } else {
88 return {};
89 }
93 } else {
94 return {};
95 }
96 }
97 hi_no_default();
98}
99
103enum class horizontal_alignment : uint8_t {
106 none = 0,
107
113 flush = 1,
114
119 left = 2,
120
125 center = 3,
126
131 justified = 4,
132
137 right = 5,
138};
139
161[[nodiscard]] constexpr std::optional<float> make_guideline(
163 float left,
164 float right,
165 float padding_left,
166 float padding_right,
167 float guideline_width = 0.0f)
168{
169 hi_axiom(left <= right);
170 hi_axiom(guideline_width >= 0.0f);
171
174 hilet guideline_center = (left + right - guideline_width) / 2.0f;
175
176 switch (alignment) {
178 return {};
180 if (guideline_left <= right) {
181 return guideline_left;
182 } else {
183 return {};
184 }
186 if (guideline_right >= left) {
187 return guideline_right;
188 } else {
189 return {};
190 }
194 } else {
195 return {};
196 }
197 }
198 hi_no_default();
199}
200
204{
205 if (rhs == horizontal_alignment::left) {
207 } else if (rhs == horizontal_alignment::right) {
209 } else {
210 return rhs;
211 }
212}
213
216[[nodiscard]] constexpr horizontal_alignment mirror(horizontal_alignment const& rhs, bool left_to_right) noexcept
217{
218 if (left_to_right) {
219 return rhs;
220 } else {
221 return mirror(rhs);
222 }
223}
224
225[[nodiscard]] constexpr horizontal_alignment resolve(horizontal_alignment const& rhs, bool left_to_right) noexcept
226{
229 } else {
230 return rhs;
231 }
232}
233
234[[nodiscard]] constexpr horizontal_alignment resolve_mirror(horizontal_alignment const& rhs, bool left_to_right) noexcept
235{
236 return resolve(mirror(rhs, left_to_right), left_to_right);
237}
238
243public:
244 constexpr alignment() noexcept : _value(0) {}
245 constexpr alignment(alignment const&) noexcept = default;
246 constexpr alignment(alignment&&) noexcept = default;
247 constexpr alignment& operator=(alignment const&) noexcept = default;
248 constexpr alignment& operator=(alignment&&) noexcept = default;
249
250 constexpr explicit alignment(uint8_t value) noexcept : _value(value) {}
251
253 _value((std::to_underlying(v) << 4) | std::to_underlying(t))
254 {
255 hi_axiom(std::to_underlying(v) <= 0xf);
256 hi_axiom(std::to_underlying(t) <= 0xf);
257 }
258
260 _value((std::to_underlying(v) << 4) | std::to_underlying(h))
261 {
262 hi_axiom(std::to_underlying(v) <= 0xf);
263 hi_axiom(std::to_underlying(h) <= 0xf);
264 }
265
266 [[nodiscard]] constexpr static alignment top_flush() noexcept
267 {
269 }
270
271 [[nodiscard]] constexpr static alignment top_left() noexcept
272 {
274 }
275
276 [[nodiscard]] constexpr static alignment top_center() noexcept
277 {
279 }
280
281 [[nodiscard]] constexpr static alignment top_justified() noexcept
282 {
284 }
285
286 [[nodiscard]] constexpr static alignment top_right() noexcept
287 {
289 }
290
291 [[nodiscard]] constexpr static alignment middle_flush() noexcept
292 {
294 }
295
296 [[nodiscard]] constexpr static alignment middle_left() noexcept
297 {
299 }
300
301 [[nodiscard]] constexpr static alignment middle_center() noexcept
302 {
304 }
305
306 [[nodiscard]] constexpr static alignment middle_justified() noexcept
307 {
309 }
310
311 [[nodiscard]] constexpr static alignment middle_right() noexcept
312 {
314 }
315
316 [[nodiscard]] constexpr static alignment bottom_left() noexcept
317 {
319 }
320
321 [[nodiscard]] constexpr static alignment bottom_center() noexcept
322 {
324 }
325
326 [[nodiscard]] constexpr static alignment bottom_right() noexcept
327 {
329 }
330
331 [[nodiscard]] constexpr horizontal_alignment horizontal() const noexcept
332 {
333 return static_cast<horizontal_alignment>(_value & 0xf);
334 }
335
336 [[nodiscard]] constexpr vertical_alignment vertical() const noexcept
337 {
338 return static_cast<vertical_alignment>(_value >> 4);
339 }
340
341 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, alignment const& rhs) noexcept = default;
342
343 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, horizontal_alignment const& rhs) noexcept
344 {
345 return lhs.horizontal() == rhs;
346 }
347
348 [[nodiscard]] constexpr friend bool operator==(horizontal_alignment const& lhs, alignment const& rhs) noexcept
349 {
350 return lhs == rhs.horizontal();
351 }
352
353 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, vertical_alignment const& rhs) noexcept
354 {
355 return lhs.vertical() == rhs;
356 }
357
358 [[nodiscard]] constexpr friend bool operator==(vertical_alignment const& lhs, alignment const& rhs) noexcept
359 {
360 return lhs == rhs.vertical();
361 }
362
363 [[nodiscard]] constexpr friend alignment mirror(alignment const& rhs) noexcept
364 {
365 return alignment{mirror(rhs.horizontal()), rhs.vertical()};
366 }
367
368 [[nodiscard]] constexpr friend alignment mirror(alignment const& rhs, bool left_to_right) noexcept
369 {
370 return alignment{mirror(rhs.horizontal(), left_to_right), rhs.vertical()};
371 }
372
373 [[nodiscard]] constexpr friend alignment resolve(alignment const& rhs, bool left_to_right) noexcept
374 {
375 return alignment{resolve(rhs.horizontal(), left_to_right), rhs.vertical()};
376 }
377
378 [[nodiscard]] constexpr friend alignment resolve_mirror(alignment const& rhs, bool left_to_right) noexcept
379 {
380 return alignment{resolve_mirror(rhs.horizontal(), left_to_right), rhs.vertical()};
381 }
382
383private:
389 uint8_t _value;
390};
391
398constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
399{
400 return alignment{lhs, rhs};
401}
402
409constexpr alignment operator|(vertical_alignment lhs, horizontal_alignment rhs) noexcept
410{
411 return alignment{lhs, rhs};
412}
413
414}} // namespace hi::v1
vertical_alignment
Vertical alignment.
Definition alignment.hpp:22
constexpr std::optional< float > make_guideline(vertical_alignment alignment, float bottom, float top, float padding_bottom, float padding_top, float guideline_width)
Create a guideline between two points.
Definition alignment.hpp:60
horizontal_alignment
Horizontal alignment.
Definition alignment.hpp:103
@ middle
Align to the vertical-middle.
@ bottom
Align to the bottom.
@ top
Align to the top.
@ right
Align the text to the right side.
@ left
Align the text to the left side.
@ flush
Align the text naturally based on the writing direction of each paragraph.
@ justified
Stretch the text to be flush to both sides.
@ center
Align the text in the center.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
Mirror the horizontal alignment.
Definition alignment.hpp:203
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Horizontal/Vertical alignment combination.
Definition alignment.hpp:242