HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
alignment.hpp
1// Copyright Take Vos 2019-2020.
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
5#pragma once
6
7#include "required.hpp"
8#include <compare>
9
10namespace tt {
11
12enum class arrangement : bool { column, row };
13
14enum class vertical_alignment { top, middle, bottom };
15
16enum class horizontal_alignment { left, center, right };
17
18enum class alignment {
19 top_left,
20 top_center,
21 top_right,
22 middle_left,
23 middle_center,
24 middle_right,
25 bottom_left,
26 bottom_center,
27 bottom_right
28};
29
30inline alignment operator|(vertical_alignment lhs, horizontal_alignment rhs) noexcept
31{
32 switch (lhs) {
33 case vertical_alignment::top:
34 switch (rhs) {
35 case horizontal_alignment::left: return alignment::top_left;
36 case horizontal_alignment::center: return alignment::top_center;
37 case horizontal_alignment::right: return alignment::top_right;
38 default: tt_no_default();
39 }
40 case vertical_alignment::middle:
41 switch (rhs) {
42 case horizontal_alignment::left: return alignment::middle_left;
43 case horizontal_alignment::center: return alignment::middle_center;
44 case horizontal_alignment::right: return alignment::middle_right;
45 default: tt_no_default();
46 }
47 case vertical_alignment::bottom:
48 switch (rhs) {
49 case horizontal_alignment::left: return alignment::bottom_left;
50 case horizontal_alignment::center: return alignment::bottom_center;
51 case horizontal_alignment::right: return alignment::bottom_right;
52 default: tt_no_default();
53 }
54 default: tt_no_default();
55 }
56}
57
58inline alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
59{
60 return rhs | lhs;
61}
62
63inline bool operator==(alignment lhs, horizontal_alignment rhs) noexcept
64{
65 switch (rhs) {
66 case horizontal_alignment::left:
67 return lhs == alignment::top_left || lhs == alignment::middle_left || lhs == alignment::bottom_left;
68
69 case horizontal_alignment::center:
70 return lhs == alignment::top_center || lhs == alignment::middle_center || lhs == alignment::bottom_center;
71
72 case horizontal_alignment::right:
73 return lhs == alignment::top_right || lhs == alignment::middle_right || lhs == alignment::bottom_right;
74
75 default: tt_no_default();
76 }
77}
78
79inline bool operator==(alignment lhs, vertical_alignment rhs) noexcept
80{
81 switch (rhs) {
82 case vertical_alignment::top: return lhs == alignment::top_left || lhs == alignment::top_center || lhs == alignment::top_right;
83
84 case vertical_alignment::middle:
85 return lhs == alignment::middle_left || lhs == alignment::middle_center || lhs == alignment::middle_right;
86
87 case vertical_alignment::bottom:
88 return lhs == alignment::bottom_left || lhs == alignment::bottom_center || lhs == alignment::bottom_right;
89
90 default: tt_no_default();
91 }
92}
93
94inline bool operator!=(alignment lhs, horizontal_alignment rhs) noexcept
95{
96 return !(lhs == rhs);
97}
98
99inline bool operator!=(alignment lhs, vertical_alignment rhs) noexcept
100{
101 return !(lhs == rhs);
102}
103
105 vertical_alignment alignment;
106 float offset;
107 float priority;
108
109public:
110 /* Construct a base-line.
111 * @param alignment Start position of the base line.
112 * @param offset Number of pt above the start position.
113 * @param priority Higher values will have priority over lower values.
114 */
115 constexpr relative_base_line(vertical_alignment alignment, float offset = 0.0f, float priority = 100.0f) noexcept :
116 alignment(alignment), offset(offset), priority(priority)
117 {
118 }
119
122 constexpr relative_base_line() noexcept : relative_base_line(vertical_alignment::middle, 0.0f, 0.0f) {}
123
126 constexpr float position(float bottom, float top) const noexcept
127 {
128 switch (alignment) {
129 case vertical_alignment::bottom: return bottom + offset;
130 case vertical_alignment::top: return top + offset;
131 case vertical_alignment::middle: return (bottom + top) * 0.5f + offset;
132 }
133 tt_no_default();
134 }
135
136 [[nodiscard]] auto operator==(relative_base_line const &rhs) const noexcept
137 {
138 return this->priority == rhs.priority;
139 }
140
141 [[nodiscard]] auto operator<=>(relative_base_line const &rhs) const noexcept
142 {
143 return this->priority <=> rhs.priority;
144 }
145};
146
147} // namespace tt
Definition alignment.hpp:104
constexpr relative_base_line() noexcept
Constructs a low-priority base line in the middle.
Definition alignment.hpp:122
constexpr float position(float bottom, float top) const noexcept
Get a base-line position.
Definition alignment.hpp:126
T left(T... args)
T operator!=(T... args)