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
64class alignment {
65public:
66 constexpr alignment() noexcept : _value(0) {}
67 constexpr alignment(alignment const &) noexcept = default;
68 constexpr alignment(alignment &&) noexcept = default;
69 constexpr alignment &operator=(alignment const &) noexcept = default;
70 constexpr alignment &operator=(alignment &&) noexcept = default;
71
72 constexpr explicit alignment(uint8_t value) noexcept : _value(value) {}
73
74 constexpr alignment(horizontal_alignment t, vertical_alignment v) noexcept :
75 _value((to_underlying(v) << 4) | to_underlying(t))
76 {
77 hi_axiom(to_underlying(v) <= 0xf);
78 hi_axiom(to_underlying(t) <= 0xf);
79 }
80
81 constexpr alignment(vertical_alignment v, horizontal_alignment t) noexcept :
82 _value((to_underlying(v) << 4) | to_underlying(t))
83 {
84 hi_axiom(to_underlying(v) <= 0xf);
85 hi_axiom(to_underlying(t) <= 0xf);
86 }
87
88 [[nodiscard]] static constexpr alignment top_flush() noexcept
89 {
90 return {horizontal_alignment::flush, vertical_alignment::top};
91 }
92
93 [[nodiscard]] static constexpr alignment top_left() noexcept
94 {
95 return {horizontal_alignment::left, vertical_alignment::top};
96 }
97
98 [[nodiscard]] static constexpr alignment top_center() noexcept
99 {
100 return {horizontal_alignment::center, vertical_alignment::top};
101 }
102
103 [[nodiscard]] static constexpr alignment top_justified() noexcept
104 {
105 return {horizontal_alignment::justified, vertical_alignment::top};
106 }
107
108 [[nodiscard]] static constexpr alignment top_right() noexcept
109 {
110 return {horizontal_alignment::right, vertical_alignment::top};
111 }
112
113 [[nodiscard]] static constexpr alignment middle_flush() noexcept
114 {
115 return {horizontal_alignment::flush, vertical_alignment::middle};
116 }
117
118 [[nodiscard]] static constexpr alignment middle_left() noexcept
119 {
120 return {horizontal_alignment::left, vertical_alignment::middle};
121 }
122
123 [[nodiscard]] static constexpr alignment middle_center() noexcept
124 {
125 return {horizontal_alignment::center, vertical_alignment::middle};
126 }
127
128 [[nodiscard]] static constexpr alignment middle_justified() noexcept
129 {
130 return {horizontal_alignment::justified, vertical_alignment::middle};
131 }
132
133 [[nodiscard]] static constexpr alignment middle_right() noexcept
134 {
135 return {horizontal_alignment::right, vertical_alignment::middle};
136 }
137
138 [[nodiscard]] static constexpr alignment bottom_left() noexcept
139 {
140 return {horizontal_alignment::left, vertical_alignment::bottom};
141 }
142
143 [[nodiscard]] static constexpr alignment bottom_center() noexcept
144 {
145 return {horizontal_alignment::center, vertical_alignment::bottom};
146 }
147
148 [[nodiscard]] static constexpr alignment bottom_right() noexcept
149 {
150 return {horizontal_alignment::right, vertical_alignment::bottom};
151 }
152
153 [[nodiscard]] constexpr horizontal_alignment text() const noexcept
154 {
155 return static_cast<horizontal_alignment>(_value & 0xf);
156 }
157
158 [[nodiscard]] constexpr vertical_alignment vertical() const noexcept
159 {
160 return static_cast<vertical_alignment>(_value >> 4);
161 }
162
163 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, alignment const &rhs) noexcept = default;
164
165 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, horizontal_alignment const &rhs) noexcept
166 {
167 return lhs.text() == rhs;
168 }
169
170 [[nodiscard]] constexpr friend bool operator==(horizontal_alignment const &lhs, alignment const &rhs) noexcept
171 {
172 return lhs == rhs.text();
173 }
174
175 [[nodiscard]] constexpr friend bool operator==(alignment const &lhs, vertical_alignment const &rhs) noexcept
176 {
177 return lhs.vertical() == rhs;
178 }
179
180 [[nodiscard]] constexpr friend bool operator==(vertical_alignment const &lhs, alignment const &rhs) noexcept
181 {
182 return lhs == rhs.vertical();
183 }
184
185private:
191 uint8_t _value;
192};
193
201{
202 return alignment{lhs, rhs};
203}
204
212{
213 return alignment{lhs, rhs};
214}
215
216} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:200
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:64
A variant of text.
Definition label.hpp:37