HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget_layout.hpp
Go to the documentation of this file.
1// Copyright Take Vos 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 "../geometry/matrix.hpp"
12#include "../geometry/axis_aligned_rectangle.hpp"
13#include "../geometry/transform.hpp"
14#include "../geometry/translate.hpp"
15#include "../unicode/unicode_bidi_class.hpp"
16#include "../text/font_book.hpp"
17#include "../GUI/gui_window_size.hpp"
18#include "../GUI/theme.hpp"
20#include "../chrono.hpp"
21#include "widget_baseline.hpp"
22
23namespace hi { inline namespace v1 {
24
41public:
46 static constexpr float redraw_overhang = 2.0f;
47
50 matrix3 to_parent = {};
51
54 matrix3 from_parent = {};
55
58 matrix3 to_window = {};
59
62 matrix3 from_window = {};
63
66 extent2 size = {};
67
70 extent2 window_size = {};
71
72 gui_window_size window_size_state = gui_window_size::normal;
73
74 hi::font_book *font_book = nullptr;
75
76 hi::theme const *theme = nullptr;
77
88 aarectangle clipping_rectangle = {};
89
94 extent2 sub_pixel_size = {1.0f, 1.0f};
95
100 unicode_bidi_class writing_direction = unicode_bidi_class::L;
101
104 utc_nanoseconds display_time_point = {};
105
108 float baseline = 0.0f;
109
110 constexpr widget_layout(widget_layout const&) noexcept = default;
111 constexpr widget_layout(widget_layout&&) noexcept = default;
112 constexpr widget_layout& operator=(widget_layout const&) noexcept = default;
113 constexpr widget_layout& operator=(widget_layout&&) noexcept = default;
114 constexpr widget_layout() noexcept = default;
115 [[nodiscard]] constexpr friend bool operator==(widget_layout const&, widget_layout const&) noexcept = default;
116
117 [[nodiscard]] constexpr bool empty() const noexcept
118 {
119 // Theme must always be set if layout is valid.
120 return theme == nullptr;
121 }
122
123 [[nodiscard]] constexpr explicit operator bool() const noexcept
124 {
125 return not empty();
126 }
127
133 [[nodiscard]] constexpr bool contains(point3 mouse_position) const noexcept
134 {
135 return rectangle().contains(mouse_position) and clipping_rectangle.contains(mouse_position);
136 }
137
138 [[nodiscard]] constexpr aarectangle rectangle() const noexcept
139 {
140 return aarectangle{size};
141 }
142
145 [[nodiscard]] constexpr aarectangle rectangle_on_window() const noexcept
146 {
147 return bounding_rectangle(to_window * rectangle());
148 }
149
152 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window() const noexcept
153 {
154 return bounding_rectangle(to_window * clipping_rectangle);
155 }
156
162 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
163 {
164 return bounding_rectangle(to_window * intersect(clipping_rectangle, narrow_clipping_rectangle));
165 }
166
167 [[nodiscard]] constexpr float width() const noexcept
168 {
169 return size.width();
170 }
171
172 [[nodiscard]] constexpr float height() const noexcept
173 {
174 return size.height();
175 }
176
179 [[nodiscard]] constexpr bool left_to_right() const noexcept
180 {
181 return writing_direction == unicode_bidi_class::L;
182 }
183
186 [[nodiscard]] constexpr bool right_to_left() const noexcept
187 {
188 return not left_to_right();
189 }
190
193 constexpr widget_layout(
194 extent2 window_size,
195 gui_window_size window_size_state,
196 hi::font_book& font_book,
197 hi::theme const& theme,
198 hi::subpixel_orientation subpixel_orientation,
199 unicode_bidi_class writing_direction,
200 utc_nanoseconds display_time_point) noexcept :
201 to_parent(),
202 from_parent(),
203 to_window(),
204 from_window(),
207 window_size_state(window_size_state),
208 font_book(&font_book),
209 theme(&theme),
211 sub_pixel_size(hi::sub_pixel_size(subpixel_orientation)),
214 baseline()
215 {
216 }
217
226 [[nodiscard]] constexpr widget_layout
227 transform(aarectangle const &child_rectangle, float elevation, aarectangle new_clipping_rectangle, widget_baseline new_baseline = widget_baseline{}) const noexcept
228 {
229 auto to_parent3 = translate3{child_rectangle, elevation};
230 auto from_parent3 = ~to_parent3;
231
232 widget_layout r = *this;
233 r.to_parent = to_parent3;
234 r.from_parent = from_parent3;
235 r.to_window = to_parent3 * this->to_window;
236 r.from_window = from_parent3 * this->from_window;
237 r.size = child_rectangle.size();
238 r.clipping_rectangle = bounding_rectangle(from_parent3 * intersect(this->clipping_rectangle, new_clipping_rectangle));
239 if (new_baseline.empty()) {
240 r.baseline = this->baseline - child_rectangle.bottom();
241 } else {
242 r.baseline = new_baseline.absolute(child_rectangle.height());
243 }
244 return r;
245 }
246
254 [[nodiscard]] constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation = 1.0f, widget_baseline new_baseline = widget_baseline{}) const noexcept
255 {
256 return transform(child_rectangle, elevation, child_rectangle + redraw_overhang, new_baseline);
257 }
258
265 [[nodiscard]] constexpr widget_layout transform(aarectangle const &child_rectangle, widget_baseline new_baseline) const noexcept
266 {
267 return transform(child_rectangle, 1.0f, child_rectangle + redraw_overhang, new_baseline);
268 }
269
275 [[nodiscard]] constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
276 {
277 auto r = *this;
278 r.clipping_rectangle = new_clipping_rectangle;
279 return r;
280 }
281};
282
283}} // namespace hi::inline v1
Defines widget_baseline.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
The base-line of a widget on which to set the text and graphics.
Definition widget_baseline.hpp:19
The layout of a widget.
Definition widget_layout.hpp:40
utc_nanoseconds display_time_point
The layout created for displaying at this time point.
Definition widget_layout.hpp:104
matrix3 to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:50
constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation=1.0f, widget_baseline new_baseline=widget_baseline{}) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:254
constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation, aarectangle new_clipping_rectangle, widget_baseline new_baseline=widget_baseline{}) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:227
constexpr widget_layout transform(aarectangle const &child_rectangle, widget_baseline new_baseline) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:265
constexpr aarectangle clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:152
aarectangle clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:88
matrix3 from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:62
constexpr bool left_to_right() const noexcept
Check if the writing direction is left-to-right.
Definition widget_layout.hpp:179
constexpr bool contains(point3 mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:133
constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:162
static constexpr float redraw_overhang
The amount of pixels that the redraw request will overhang the widget.
Definition widget_layout.hpp:46
float baseline
The base-line in widget local y-coordinate.
Definition widget_layout.hpp:108
constexpr bool right_to_left() const noexcept
Check if the writing direction is right_to_left.
Definition widget_layout.hpp:186
extent2 sub_pixel_size
The size of a sub-pixel.
Definition widget_layout.hpp:94
constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
Override e context with the new clipping rectangle.
Definition widget_layout.hpp:275
extent2 window_size
Size of the window.
Definition widget_layout.hpp:70
extent2 size
Size of the widget.
Definition widget_layout.hpp:66
constexpr aarectangle rectangle_on_window() const noexcept
Get the rectangle in window coordinate system.
Definition widget_layout.hpp:145
matrix3 to_window
This matrix transforms local coordinates to window coordinates.
Definition widget_layout.hpp:58
constexpr widget_layout(extent2 window_size, gui_window_size window_size_state, hi::font_book &font_book, hi::theme const &theme, hi::subpixel_orientation subpixel_orientation, unicode_bidi_class writing_direction, utc_nanoseconds display_time_point) noexcept
Construct a widget_layout from inside the window.
Definition widget_layout.hpp:193
matrix3 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:54
unicode_bidi_class writing_direction
The default writing direction.
Definition widget_layout.hpp:100