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/module.hpp"
13#include "../geometry/module.hpp"
14#include "../time/module.hpp"
15#include "../utility/module.hpp"
16#include "../settings/module.hpp"
17
18namespace hi { inline namespace v1 {
19
36public:
41 static constexpr int redraw_overhang = 2;
42
47
51
55
59
63
67
70 gui_window_size window_size_state = gui_window_size::normal;
71
74 float elevation = 0.0f;
75
87
92 extent2 sub_pixel_size = {1.0f, 1.0f};
93
96 utc_nanoseconds display_time_point = {};
97
98 constexpr widget_layout(widget_layout const&) noexcept = default;
99 constexpr widget_layout(widget_layout&&) noexcept = default;
100 constexpr widget_layout& operator=(widget_layout const&) noexcept = default;
101 constexpr widget_layout& operator=(widget_layout&&) noexcept = default;
102 constexpr widget_layout() noexcept = default;
103 [[nodiscard]] constexpr friend bool operator==(widget_layout const&, widget_layout const&) noexcept = default;
104
107 constexpr widget_layout(
109 gui_window_size window_size_state,
110 hi::subpixel_orientation subpixel_orientation,
111 utc_nanoseconds display_time_point) noexcept :
112 to_parent(),
113 from_parent(),
114 to_window(),
115 from_window(),
120 sub_pixel_size(hi::sub_pixel_size(subpixel_orientation)),
122 {
123 }
124
125 [[nodiscard]] constexpr bool empty() const noexcept
126 {
127 // Theme must always be set if layout is valid.
128 return display_time_point == utc_nanoseconds{};
129 }
130
131 [[nodiscard]] constexpr explicit operator bool() const noexcept
132 {
133 return not empty();
134 }
135
136 [[nodiscard]] constexpr translate3 to_window3() const noexcept
137 {
138 return translate3{to_window, elevation};
139 }
140
146 [[nodiscard]] constexpr bool contains(point3 mouse_position) const noexcept
147 {
148 return rectangle().contains(mouse_position) and clipping_rectangle.contains(mouse_position);
149 }
150
151 [[nodiscard]] constexpr aarectangle rectangle() const noexcept
152 {
153 return shape.rectangle;
154 }
155
158 [[nodiscard]] constexpr aarectangle rectangle_on_window() const noexcept
159 {
160 return to_window * rectangle();
161 }
162
165 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window() const noexcept
166 {
168 }
169
175 [[nodiscard]] constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
176 {
177 return to_window * intersect(clipping_rectangle, narrow_clipping_rectangle);
178 }
179
180 [[nodiscard]] constexpr float width() const noexcept
181 {
182 return shape.width();
183 }
184
185 [[nodiscard]] constexpr float height() const noexcept
186 {
187 return shape.height();
188 }
189
190 [[nodiscard]] constexpr extent2 size() const noexcept
191 {
192 return shape.size();
193 }
194
202 [[nodiscard]] constexpr widget_layout
203 transform(box_shape const& child_shape, float child_elevation, aarectangle new_clipping_rectangle) const noexcept
204 {
205 widget_layout r = *this;
206 r.shape.rectangle = aarectangle{child_shape.size()};
207
208 if (child_shape.baseline) {
209 r.shape.baseline = *child_shape.baseline - child_shape.y();
210
211 } else if (r.shape.baseline) {
212 // Use the baseline of the current layout and translate it.
213 *r.shape.baseline -= child_shape.y();
214 }
215
216 if (child_shape.centerline) {
217 r.shape.centerline = *child_shape.centerline - child_shape.x();
218
219 } else if (r.shape.centerline) {
220 // Use the baseline of the current layout and translate it.
221 *r.shape.centerline -= child_shape.x();
222 }
223
224 r.to_parent = translate2{child_shape.x(), child_shape.y()};
225 r.from_parent = ~r.to_parent;
226 r.to_window = r.to_parent * this->to_window;
227 r.from_window = r.from_parent * this->from_window;
228 r.clipping_rectangle = r.from_parent * intersect(this->clipping_rectangle, new_clipping_rectangle);
229 r.elevation += child_elevation;
230 return r;
231 }
232
239 [[nodiscard]] constexpr widget_layout transform(box_shape const& child_shape, float child_elevation = 1.0f) const noexcept
240 {
241 return transform(child_shape, child_elevation, child_shape.rectangle + redraw_overhang);
242 }
243
249 [[nodiscard]] constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
250 {
251 auto r = *this;
252 r.clipping_rectangle = new_clipping_rectangle;
253 return r;
254 }
255};
256
257}} // namespace hi::v1
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:26
constexpr extent2 size() const noexcept
Get size of the rectangle.
Definition aarectangle.hpp:179
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarectangle.hpp:261
A high-level geometric extent.
Definition extent2.hpp:26
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
Definition translate2.hpp:13
The layout of a widget.
Definition widget_layout.hpp:35
translate2 to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:50
utc_nanoseconds display_time_point
The layout created for displaying at this time point.
Definition widget_layout.hpp:96
constexpr aarectangle clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:165
translate2 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:54
aarectangle clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:86
constexpr bool contains(point3 mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:146
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:74
constexpr aarectangle clipping_rectangle_on_window(aarectangle narrow_clipping_rectangle) const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:175
translate2 to_window
This matrix transforms local coordinates to window coordinates.
Definition widget_layout.hpp:58
extent2 sub_pixel_size
The size of a sub-pixel.
Definition widget_layout.hpp:92
static constexpr int redraw_overhang
The amount of pixels that the redraw request will overhang the widget.
Definition widget_layout.hpp:41
translate2 from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:62
constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
Override e context with the new clipping rectangle.
Definition widget_layout.hpp:249
extent2 window_size
Size of the window.
Definition widget_layout.hpp:66
box_shape shape
Shape of the widget.
Definition widget_layout.hpp:46
constexpr aarectangle rectangle_on_window() const noexcept
Get the rectangle in window coordinate system.
Definition widget_layout.hpp:158
gui_window_size window_size_state
The size state of the window.
Definition widget_layout.hpp:70
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:203
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation=1.0f) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:239
Definition box_shape.hpp:15