HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget_layout.hpp
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 "gui_window_size.hpp"
12#include "../layout/layout.hpp"
13#include "../geometry/geometry.hpp"
14#include "../time/time.hpp"
15#include "../utility/utility.hpp"
16#include "../settings/settings.hpp"
17#include "../macros.hpp"
18
19hi_export_module(hikogui.GUI : widget_layout);
20
21hi_export namespace hi { inline namespace v1 {
22
26 level,
27
31
35
39};
40
57public:
62 constexpr static int redraw_overhang = 2;
63
68
72
76
80
84
88
91 gui_window_size window_size_state = gui_window_size::normal;
92
95 float elevation = 0.0f;
96
107 int layer = 0;
108
120
125 extent2 sub_pixel_size = {1.0f, 1.0f};
126
129 utc_nanoseconds display_time_point = {};
130
131 constexpr widget_layout(widget_layout const&) noexcept = default;
132 constexpr widget_layout(widget_layout&&) noexcept = default;
133 constexpr widget_layout& operator=(widget_layout const&) noexcept = default;
134 constexpr widget_layout& operator=(widget_layout&&) noexcept = default;
135 constexpr widget_layout() noexcept = default;
136 [[nodiscard]] constexpr friend bool operator==(widget_layout const&, widget_layout const&) noexcept = default;
137
140 constexpr widget_layout(
142 gui_window_size window_size_state,
143 hi::subpixel_orientation subpixel_orientation,
144 utc_nanoseconds display_time_point) noexcept :
145 to_parent(),
146 from_parent(),
147 to_window(),
148 from_window(),
153 sub_pixel_size(hi::sub_pixel_size(subpixel_orientation)),
155 {
156 }
157
158 [[nodiscard]] constexpr bool empty() const noexcept
159 {
160 // Theme must always be set if layout is valid.
161 return display_time_point == utc_nanoseconds{};
162 }
163
164 [[nodiscard]] constexpr explicit operator bool() const noexcept
165 {
166 return not empty();
167 }
168
169 [[nodiscard]] constexpr translate3 to_window3() const noexcept
170 {
171 return translate3{to_window, elevation};
172 }
173
179 [[nodiscard]] constexpr bool contains(point3 mouse_position) const noexcept
180 {
181 return rectangle().contains(mouse_position) and clipping_rectangle.contains(mouse_position);
182 }
183
184 [[nodiscard]] constexpr aarectangle rectangle() const noexcept
185 {
186 return shape.rectangle;
187 }
188
191 [[nodiscard]] constexpr aarectangle rectangle_on_window() const noexcept
192 {
193 return to_window * rectangle();
194 }
195
198 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window() const noexcept
199 {
201 }
202
208 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
209 {
210 return to_window * intersect(clipping_rectangle, narrow_clipping_rectangle);
211 }
212
213 [[nodiscard]] constexpr float width() const noexcept
214 {
215 return shape.width();
216 }
217
218 [[nodiscard]] constexpr float height() const noexcept
219 {
220 return shape.height();
221 }
222
223 [[nodiscard]] constexpr extent2 size() const noexcept
224 {
225 return shape.size();
226 }
227
235 [[nodiscard]] constexpr widget_layout
236 transform(box_shape const& child_shape, transform_command command, aarectangle new_clipping_rectangle) const noexcept
237 {
238 widget_layout r = *this;
239 r.shape.rectangle = aarectangle{child_shape.size()};
240
241 if (child_shape.baseline) {
242 r.shape.baseline = *child_shape.baseline - child_shape.y();
243
244 } else if (r.shape.baseline) {
245 // Use the baseline of the current layout and translate it.
246 *r.shape.baseline -= child_shape.y();
247 }
248
249 if (child_shape.centerline) {
250 r.shape.centerline = *child_shape.centerline - child_shape.x();
251
252 } else if (r.shape.centerline) {
253 // Use the baseline of the current layout and translate it.
254 *r.shape.centerline -= child_shape.x();
255 }
256
257 r.to_parent = translate2{child_shape.x(), child_shape.y()};
258 r.from_parent = ~r.to_parent;
259 r.to_window = r.to_parent * this->to_window;
260 r.from_window = r.from_parent * this->from_window;
261 r.clipping_rectangle = r.from_parent * intersect(this->clipping_rectangle, new_clipping_rectangle);
262
263 switch (command) {
265 r.elevation += 0.0f;
266 r.layer += 0;
267 break;
269 r.elevation += 1.0f;
270 r.layer += 1;
271 break;
273 r.elevation += 1.0f;
274 r.layer += 0;
275 break;
277 r.elevation += 20.0f;
278 r.layer = 0;
279 break;
280 default:
281 hi_no_default();
282 }
283 return r;
284 }
285
292 [[nodiscard]] constexpr widget_layout
293 transform(box_shape const& child_shape, transform_command command = transform_command::increment) const noexcept
294 {
295 return transform(child_shape, command, child_shape.rectangle + redraw_overhang);
296 }
297
304 [[nodiscard]] constexpr widget_layout
305 transform(box_shape const& child_shape, aarectangle new_clipping_rectangle) const noexcept
306 {
307 return transform(child_shape, transform_command::increment, new_clipping_rectangle);
308 }
309
315 [[nodiscard]] constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
316 {
317 auto r = *this;
318 r.clipping_rectangle = new_clipping_rectangle;
319 return r;
320 }
321};
322
323}} // namespace hi::v1
The HikoGUI namespace.
Definition array_generic.hpp:20
transform_command
Definition widget_layout.hpp:23
@ overlay
The child widget increases the elevation by 20 and resets the layer.
@ menu_item
The child widget increments to the next elevation but layer stays the same.
@ increment
The child widget increments to the next elevation and layer.
@ level
The child widget stays at the same elevation and layer.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
constexpr extent2 size() const noexcept
Get size of the rectangle.
Definition aarectangle.hpp:186
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarectangle.hpp:268
A high-level geometric extent.
Definition extent2.hpp:32
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:25
Definition translate2.hpp:18
The layout of a widget.
Definition widget_layout.hpp:56
translate2 to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:71
utc_nanoseconds display_time_point
The layout created for displaying at this time point.
Definition widget_layout.hpp:129
constexpr widget_layout transform(box_shape const &child_shape, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:305
constexpr aarectangle clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:198
translate2 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:75
aarectangle clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:119
constexpr bool contains(point3 mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:179
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:95
constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:208
translate2 to_window
This matrix transforms local coordinates to window coordinates.
Definition widget_layout.hpp:79
int layer
The number of visible layers above the window.
Definition widget_layout.hpp:107
extent2 sub_pixel_size
The size of a sub-pixel.
Definition widget_layout.hpp:125
constexpr widget_layout transform(box_shape const &child_shape, transform_command command=transform_command::increment) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:293
static constexpr int redraw_overhang
The amount of pixels that the redraw request will overhang the widget.
Definition widget_layout.hpp:62
translate2 from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:83
constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
Override e context with the new clipping rectangle.
Definition widget_layout.hpp:315
constexpr widget_layout transform(box_shape const &child_shape, transform_command command, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:236
extent2 window_size
Size of the window.
Definition widget_layout.hpp:87
box_shape shape
Shape of the widget.
Definition widget_layout.hpp:67
constexpr aarectangle rectangle_on_window() const noexcept
Get the rectangle in window coordinate system.
Definition widget_layout.hpp:191
gui_window_size window_size_state
The size state of the window.
Definition widget_layout.hpp:91
Definition box_shape.hpp:18