HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget_layout.hpp
1// Copyright Take Vos 2021.
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
5#pragma once
6
7#include "../geometry/matrix.hpp"
8#include "../geometry/axis_aligned_rectangle.hpp"
9#include "../geometry/transform.hpp"
10#include "../geometry/translate.hpp"
11#include "../unicode/unicode_bidi_class.hpp"
13#include "../chrono.hpp"
14
15namespace hi::inline v1 {
16
18public:
23 static constexpr float redraw_overhang = 2.0f;
24
28
32
36
40
44
56
62
67 unicode_bidi_class writing_direction;
68
71 utc_nanoseconds display_time_point;
72
73 constexpr widget_layout(widget_layout const &) noexcept = default;
74 constexpr widget_layout(widget_layout &&) noexcept = default;
75 constexpr widget_layout &operator=(widget_layout const &) noexcept = default;
76 constexpr widget_layout &operator=(widget_layout &&) noexcept = default;
77 constexpr widget_layout() noexcept = default;
78
79 [[nodiscard]] constexpr friend bool operator==(widget_layout const &lhs, widget_layout const &rhs) noexcept
80 {
81 hi_axiom((lhs.to_parent == rhs.to_parent) == (lhs.from_parent == rhs.from_parent));
82 hi_axiom((lhs.to_window == rhs.to_window) == (lhs.from_window == rhs.from_window));
83
84 // clang-format on
85 return
86 lhs.size == rhs.size and
87 lhs.to_parent == rhs.to_parent and
88 lhs.to_window == rhs.to_window and
89 lhs.clipping_rectangle == rhs.clipping_rectangle and
90 lhs.sub_pixel_size == rhs.sub_pixel_size and
91 lhs.writing_direction == rhs.writing_direction;
92 // clang-format off
93 }
94
100 [[nodiscard]] constexpr bool contains(point3 mouse_position) const noexcept
101 {
102 return rectangle().contains(mouse_position) and clipping_rectangle.contains(mouse_position);
103 }
104
105 [[nodiscard]] constexpr aarectangle rectangle() const noexcept
106 {
107 return aarectangle{size};
108 }
109
112 [[nodiscard]] constexpr aarectangle window_clipping_rectangle() const noexcept
113 {
114 return bounding_rectangle(to_window * clipping_rectangle);
115 }
116
122 [[nodiscard]] constexpr aarectangle window_clipping_rectangle(aarectangle narrow_clipping_rectangle) const noexcept
123 {
124 return bounding_rectangle(to_window * intersect(clipping_rectangle, narrow_clipping_rectangle));
125 }
126
127 [[nodiscard]] constexpr float width() const noexcept
128 {
129 return size.width();
130 }
131
132 [[nodiscard]] constexpr float height() const noexcept
133 {
134 return size.height();
135 }
136
139 constexpr widget_layout(
140 extent2 window_size,
141 hi::subpixel_orientation subpixel_orientation,
142 unicode_bidi_class writing_direction,
143 utc_nanoseconds display_time_point) noexcept :
144 to_parent(),
145 from_parent(),
146 to_window(),
147 from_window(),
148 size(window_size),
149 clipping_rectangle(window_size),
150 sub_pixel_size(hi::sub_pixel_size(subpixel_orientation)),
151 writing_direction(writing_direction),
152 display_time_point(display_time_point)
153 {
154 }
155
163 [[nodiscard]] constexpr widget_layout
164 transform(aarectangle const &child_rectangle, float elevation, aarectangle new_clipping_rectangle) const noexcept
165 {
166 auto to_parent3 = translate3{child_rectangle, elevation};
167 auto from_parent3 = ~to_parent3;
168
170 r.to_parent = to_parent3;
171 r.from_parent = from_parent3;
172 r.to_window = to_parent3 * this->to_window;
173 r.from_window = from_parent3 * this->from_window;
174 r.size = child_rectangle.size();
175 r.clipping_rectangle = bounding_rectangle(from_parent3 * intersect(this->clipping_rectangle, new_clipping_rectangle));
176 r.sub_pixel_size = this->sub_pixel_size;
177 r.writing_direction = this->writing_direction;
178 r.display_time_point = this->display_time_point;
179 return r;
180 }
181
188 [[nodiscard]] constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation = 1.0f) const noexcept
189 {
190 return transform(child_rectangle, elevation, child_rectangle + redraw_overhang);
191 }
192
198 [[nodiscard]] constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
199 {
200 auto r = *this;
201 r.clipping_rectangle = new_clipping_rectangle;
202 return r;
203 }
204};
205
206} // namespace hi::inline v1
constexpr extent2 sub_pixel_size(subpixel_orientation orientation) noexcept
Get the size of a sub-pixel based on the sub-pixel orientation.
Definition subpixel_orientation.hpp:38
subpixel_orientation
The orientation of the RGB sub-pixels of and LCD/LED panel.
Definition subpixel_orientation.hpp:18
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:235
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:151
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:140
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
Definition translate.hpp:15
Definition widget_layout.hpp:17
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:139
constexpr aarectangle window_clipping_rectangle() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:112
constexpr aarectangle window_clipping_rectangle(aarectangle narrow_clipping_rectangle) const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:122
matrix3 to_window
This matrix transforms local coordinates to window coordinates.
Definition widget_layout.hpp:35
matrix3 to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:27
aarectangle clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:55
constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:164
extent2 sub_pixel_size
The size of a sub-pixel.
Definition widget_layout.hpp:61
extent2 size
Size of the widget.
Definition widget_layout.hpp:43
matrix3 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:31
matrix3 from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:39
utc_nanoseconds display_time_point
The layout created for displaying at this time point.
Definition widget_layout.hpp:71
constexpr bool contains(point3 mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:100
constexpr widget_layout override_clip(aarectangle new_clipping_rectangle) const noexcept
Override e context with the new clipping rectangle.
Definition widget_layout.hpp:198
constexpr widget_layout transform(aarectangle const &child_rectangle, float elevation=1.0f) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:188
unicode_bidi_class writing_direction
The default writing direction.
Definition widget_layout.hpp:67