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#include <algorithm>
15#include <exception>
16#include <compare>
17
18hi_export_module(hikogui.geometry : alignment);
19
20hi_export namespace hi { inline namespace v1 {
21
25enum class vertical_alignment : uint8_t {
28 none = 0,
29
32 top = 1,
33
36 middle = 2,
37
40 bottom = 3
41};
42
61[[nodiscard]] constexpr std::optional<float> make_guideline(
63 float bottom,
64 float top,
65 float guideline_width)
66{
67 hi_axiom(bottom <= top);
68 hi_axiom(guideline_width >= 0.0f);
69
70 auto const guideline_bottom = bottom;
71 auto const guideline_top = top - guideline_width;
72 auto const guideline_middle = (bottom + top - guideline_width) / 2.0f;
73
74 switch (alignment) {
76 return {};
78 if (guideline_bottom <= top) {
79 return guideline_top;
80 } else {
81 return {};
82 }
84 if (guideline_top >= bottom) {
85 return guideline_top;
86 } else {
87 return {};
88 }
90 if (guideline_bottom <= guideline_top) {
91 return std::clamp(guideline_middle, guideline_bottom, guideline_top);
92 } else {
93 return {};
94 }
95 }
96 hi_no_default();
97}
98
102enum class horizontal_alignment : uint8_t {
105 none = 0,
106
112 flush = 1,
113
118 left = 2,
119
124 center = 3,
125
130 justified = 4,
131
136 right = 5,
137};
138
158[[nodiscard]] constexpr std::optional<float> make_guideline(
160 float left,
161 float right,
162 float guideline_width = 0.0f)
163{
164 hi_axiom(left <= right);
165 hi_axiom(guideline_width >= 0.0f);
166
167 auto const guideline_left = left;
168 auto const guideline_right = right - guideline_width;
169 auto const guideline_center = (left + right - guideline_width) / 2.0f;
170
171 switch (alignment) {
173 return {};
175 if (guideline_left <= right) {
176 return guideline_left;
177 } else {
178 return {};
179 }
181 if (guideline_right >= left) {
182 return guideline_right;
183 } else {
184 return {};
185 }
186
188 if (guideline_left <= guideline_right) {
189 return std::clamp(guideline_center, guideline_left, guideline_right);
190 } else {
191 return {};
192 }
193
195 hi_no_default();
196
198 hi_no_default();
199 }
200 hi_no_default();
201}
202
205[[nodiscard]] constexpr horizontal_alignment mirror(horizontal_alignment const& rhs) noexcept
206{
207 if (rhs == horizontal_alignment::left) {
209 } else if (rhs == horizontal_alignment::right) {
211 } else {
212 return rhs;
213 }
214}
215
218[[nodiscard]] constexpr horizontal_alignment mirror(horizontal_alignment const& rhs, bool left_to_right) noexcept
219{
220 if (left_to_right) {
221 return rhs;
222 } else {
223 return mirror(rhs);
224 }
225}
226
227[[nodiscard]] constexpr horizontal_alignment resolve(horizontal_alignment const& rhs, bool left_to_right) noexcept
228{
231 } else {
232 return rhs;
233 }
234}
235
236[[nodiscard]] constexpr horizontal_alignment resolve_mirror(horizontal_alignment const& rhs, bool left_to_right) noexcept
237{
238 return resolve(mirror(rhs, left_to_right), left_to_right);
239}
240
245public:
246 constexpr alignment() noexcept : _value(0) {}
247 constexpr alignment(alignment const&) noexcept = default;
248 constexpr alignment(alignment&&) noexcept = default;
249 constexpr alignment& operator=(alignment const&) noexcept = default;
250 constexpr alignment& operator=(alignment&&) noexcept = default;
251
252 constexpr explicit alignment(uint8_t value) noexcept : _value(value) {}
253
255 _value((std::to_underlying(v) << 4) | std::to_underlying(t))
256 {
257 hi_axiom(std::to_underlying(v) <= 0xf);
258 hi_axiom(std::to_underlying(t) <= 0xf);
259 }
260
262 _value((std::to_underlying(v) << 4) | std::to_underlying(h))
263 {
264 hi_axiom(std::to_underlying(v) <= 0xf);
265 hi_axiom(std::to_underlying(h) <= 0xf);
266 }
267
268 [[nodiscard]] constexpr static alignment top_flush() noexcept
269 {
271 }
272
273 [[nodiscard]] constexpr static alignment top_left() noexcept
274 {
276 }
277
278 [[nodiscard]] constexpr static alignment top_center() noexcept
279 {
281 }
282
283 [[nodiscard]] constexpr static alignment top_justified() noexcept
284 {
286 }
287
288 [[nodiscard]] constexpr static alignment top_right() noexcept
289 {
291 }
292
293 [[nodiscard]] constexpr static alignment middle_flush() noexcept
294 {
296 }
297
298 [[nodiscard]] constexpr static alignment middle_left() noexcept
299 {
301 }
302
303 [[nodiscard]] constexpr static alignment middle_center() noexcept
304 {
306 }
307
308 [[nodiscard]] constexpr static alignment middle_justified() noexcept
309 {
311 }
312
313 [[nodiscard]] constexpr static alignment middle_right() noexcept
314 {
316 }
317
318 [[nodiscard]] constexpr static alignment bottom_left() noexcept
319 {
321 }
322
323 [[nodiscard]] constexpr static alignment bottom_center() noexcept
324 {
326 }
327
328 [[nodiscard]] constexpr static alignment bottom_right() noexcept
329 {
331 }
332
333 [[nodiscard]] constexpr horizontal_alignment horizontal() const noexcept
334 {
335 return static_cast<horizontal_alignment>(_value & 0xf);
336 }
337
338 [[nodiscard]] constexpr vertical_alignment vertical() const noexcept
339 {
340 return static_cast<vertical_alignment>(_value >> 4);
341 }
342
343 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, alignment const& rhs) noexcept = default;
344
345 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, horizontal_alignment const& rhs) noexcept
346 {
347 return lhs.horizontal() == rhs;
348 }
349
350 [[nodiscard]] constexpr friend bool operator==(horizontal_alignment const& lhs, alignment const& rhs) noexcept
351 {
352 return lhs == rhs.horizontal();
353 }
354
355 [[nodiscard]] constexpr friend bool operator==(alignment const& lhs, vertical_alignment const& rhs) noexcept
356 {
357 return lhs.vertical() == rhs;
358 }
359
360 [[nodiscard]] constexpr friend bool operator==(vertical_alignment const& lhs, alignment const& rhs) noexcept
361 {
362 return lhs == rhs.vertical();
363 }
364
365 [[nodiscard]] constexpr friend alignment mirror(alignment const& rhs) noexcept
366 {
367 return alignment{mirror(rhs.horizontal()), rhs.vertical()};
368 }
369
370 [[nodiscard]] constexpr friend alignment mirror(alignment const& rhs, bool left_to_right) noexcept
371 {
372 return alignment{mirror(rhs.horizontal(), left_to_right), rhs.vertical()};
373 }
374
375 [[nodiscard]] constexpr friend alignment resolve(alignment const& rhs, bool left_to_right) noexcept
376 {
377 return alignment{resolve(rhs.horizontal(), left_to_right), rhs.vertical()};
378 }
379
380 [[nodiscard]] constexpr friend alignment resolve_mirror(alignment const& rhs, bool left_to_right) noexcept
381 {
382 return alignment{resolve_mirror(rhs.horizontal(), left_to_right), rhs.vertical()};
383 }
384
385private:
391 uint8_t _value;
392};
393
400constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
401{
402 return alignment{lhs, rhs};
403}
404
411constexpr alignment operator|(vertical_alignment lhs, horizontal_alignment rhs) noexcept
412{
413 return alignment{lhs, rhs};
414}
415
416}} // namespace hi::v1
vertical_alignment
Vertical alignment.
Definition alignment.hpp:25
horizontal_alignment
Horizontal alignment.
Definition alignment.hpp:102
constexpr std::optional< float > make_guideline(vertical_alignment alignment, float bottom, float top, float guideline_width)
Create a guideline between two points.
Definition alignment.hpp:61
@ 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.
The HikoGUI namespace.
Definition array_generic.hpp:20
constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
Mirror the horizontal alignment.
Definition alignment.hpp:205
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244