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"
17#include "../chrono.hpp"
18#include "widget_baseline.hpp"
19
20namespace hi { inline namespace v1 {
21
38public:
43 static constexpr float redraw_overhang = 2.0f;
44
47 matrix3 to_parent;
48
51 matrix3 from_parent;
52
55 matrix3 to_window;
56
59 matrix3 from_window;
60
63 extent2 size;
64
75 aarectangle clipping_rectangle;
76
82
87 unicode_bidi_class writing_direction;
88
91 utc_nanoseconds display_time_point;
92
95 float baseline;
96
97 constexpr widget_layout(widget_layout const&) noexcept = default;
98 constexpr widget_layout(widget_layout&&) noexcept = default;
99 constexpr widget_layout& operator=(widget_layout const&) noexcept = default;
100 constexpr widget_layout& operator=(widget_layout&&) noexcept = default;
101 constexpr widget_layout() noexcept = default;
102
103 [[nodiscard]] constexpr friend bool operator==(widget_layout const& lhs, widget_layout const& rhs) noexcept
104 {
105 hi_axiom((lhs.to_parent == rhs.to_parent) == (lhs.from_parent == rhs.from_parent));
106 hi_axiom((lhs.to_window == rhs.to_window) == (lhs.from_window == rhs.from_window));
107
108 // clang-format on
109 return lhs.size == rhs.size and lhs.to_parent == rhs.to_parent and lhs.to_window == rhs.to_window and
110 lhs.clipping_rectangle == rhs.clipping_rectangle and lhs.sub_pixel_size == rhs.sub_pixel_size and
111 lhs.writing_direction == rhs.writing_direction and lhs.baseline == rhs.baseline;
112 // clang-format off
113 }
114
120 [[nodiscard]] constexpr bool contains(point3 mouse_position) const noexcept
121 {
122 return rectangle().contains(mouse_position) and clipping_rectangle.contains(mouse_position);
123 }
124
125 [[nodiscard]] constexpr aarectangle rectangle() const noexcept
126 {
127 return aarectangle{size};
128 }
129
132 [[nodiscard]] constexpr aarectangle window_clipping_rectangle() const noexcept
133 {
134 return bounding_rectangle(to_window * clipping_rectangle);
135 }
136
142 [[nodiscard]] constexpr aarectangle window_clipping_rectangle(aarectangle narrow_clipping_rectangle) const noexcept
143 {
144 return bounding_rectangle(to_window * intersect(clipping_rectangle, narrow_clipping_rectangle));
145 }
146
147 [[nodiscard]] constexpr float width() const noexcept
148 {
149 return size.width();
150 }
151
152 [[nodiscard]] constexpr float height() const noexcept
153 {
154 return size.height();
155 }
156
159 constexpr widget_layout(
160 extent2 window_size,
161 hi::subpixel_orientation subpixel_orientation,
162 unicode_bidi_class writing_direction,
163 utc_nanoseconds display_time_point) noexcept :
164 to_parent(),
165 from_parent(),
166 to_window(),
167 from_window(),
168 size(window_size),
169 clipping_rectangle(window_size),
170 sub_pixel_size(hi::sub_pixel_size(subpixel_orientation)),
173 baseline()
174 {
175 }
176
185 [[nodiscard]] constexpr widget_layout
186 transform(aarectangle const &child_rectangle, float elevation, aarectangle new_clipping_rectangle, widget_baseline new_baseline = widget_baseline{}) const noexcept
187 {
188 auto to_parent3 = translate3{child_rectangle, elevation};
189 auto from_parent3 = ~to_parent3;
190
192 r.to_parent = to_parent3;
193 r.from_parent = from_parent3;
194 r.to_window = to_parent3 * this->to_window;
195 r.from_window = from_parent3 * this->from_window;
196 r.size = child_rectangle.size();
197 r.clipping_rectangle = bounding_rectangle(from_parent3 * intersect(this->clipping_rectangle, new_clipping_rectangle));
201 if (new_baseline.empty()) {
202 r.baseline = this->baseline - child_rectangle.bottom();
203 } else {
204 r.baseline = new_baseline.absolute(child_rectangle.height());
205 }
206 return r;
207 }
208
216 [[nodiscard]] constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation = 1.0f, widget_baseline new_baseline = widget_baseline{}) const noexcept
217 {
218 return transform(child_rectangle, elevation, child_rectangle + redraw_overhang, new_baseline);
219 }
220
227 [[nodiscard]] constexpr widget_layout transform(aarectangle const &child_rectangle, widget_baseline new_baseline) const noexcept
228 {
229 return transform(child_rectangle, 1.0f, child_rectangle + redraw_overhang, new_baseline);
230 }
231
237 [[nodiscard]] constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
238 {
239 auto r = *this;
240 r.clipping_rectangle = new_clipping_rectangle;
241 return r;
242 }
243};
244
245}} // 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:37
utc_nanoseconds display_time_point
The layout created for displaying at this time point.
Definition widget_layout.hpp:91
matrix3 to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:47
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:216
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:186
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:227
aarectangle clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:75
matrix3 from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:59
constexpr bool contains(point3 mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:120
static constexpr float redraw_overhang
The amount of pixels that the redraw request will overhang the widget.
Definition widget_layout.hpp:43
constexpr aarectangle window_clipping_rectangle() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:132
float baseline
The base-line in widget local y-coordinate.
Definition widget_layout.hpp:95
extent2 sub_pixel_size
The size of a sub-pixel.
Definition widget_layout.hpp:81
constexpr widget_layout(extent2 window_size, 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:159
constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
Override e context with the new clipping rectangle.
Definition widget_layout.hpp:237
extent2 size
Size of the widget.
Definition widget_layout.hpp:63
matrix3 to_window
This matrix transforms local coordinates to window coordinates.
Definition widget_layout.hpp:55
matrix3 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:51
constexpr aarectangle window_clipping_rectangle(aarectangle narrow_clipping_rectangle) const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:142
unicode_bidi_class writing_direction
The default writing direction.
Definition widget_layout.hpp:87