HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scroll_aperture_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 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/module.hpp"
12
13namespace hi { inline namespace v1 {
14
22template<fixed_string Name = "">
24public:
25 using super = widget;
26 constexpr static auto prefix = Name / "aperture";
27
28 observer<int> content_width;
29 observer<int> content_height;
30 observer<int> aperture_width;
31 observer<int> aperture_height;
32 observer<int> offset_x;
33 observer<int> offset_y;
34
35 scroll_aperture_widget(widget *parent) noexcept : super(parent)
36 {
37 hi_axiom(loop::main().on_thread());
39
40 // The aperture-widget will not draw itself, only its selected content.
42
43 _content_width_cbt = content_width.subscribe([&](auto...) {
44 ++global_counter<"scroll_aperture_widget:content_width:relayout">;
45 process_event({gui_event_type::window_relayout});
46 });
47 _content_height_cbt = content_height.subscribe([&](auto...) {
48 ++global_counter<"scroll_aperture_widget:content_height:relayout">;
49 process_event({gui_event_type::window_relayout});
50 });
51 _aperture_width_cbt = aperture_width.subscribe([&](auto...) {
52 ++global_counter<"scroll_aperture_widget:aperture_width:relayout">;
53 process_event({gui_event_type::window_relayout});
54 });
55 _aperture_height_cbt = aperture_height.subscribe([&](auto...) {
56 ++global_counter<"scroll_aperture_widget:aperture_height:relayout">;
57 process_event({gui_event_type::window_relayout});
58 });
59 _offset_x_cbt = offset_x.subscribe([&](auto...) {
60 ++global_counter<"scroll_aperture_widget:offset_x:relayout">;
61 process_event({gui_event_type::window_relayout});
62 });
63 _offset_y_cbt = offset_y.subscribe([&](auto...) {
64 ++global_counter<"scroll_aperture_widget:offset_y:relayout">;
65 process_event({gui_event_type::window_relayout});
66 });
67 _minimum_cbt = minimum.subscribe([&](auto...) {
68 ++global_counter<"scroll_aperture_widget:minimum:reconstrain">;
70 });
71 }
72
73 template<typename Widget, typename... Args>
74 Widget& make_widget(Args&&...args) noexcept
75 {
76 hi_axiom(loop::main().on_thread());
77 hi_axiom(_content == nullptr);
78
79 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
80 auto& ref = *tmp;
81 _content = std::move(tmp);
82 return ref;
83 }
84
85 [[nodiscard]] bool x_axis_scrolls() const noexcept
86 {
87 return *content_width > *aperture_width;
88 }
89
90 [[nodiscard]] bool y_axis_scrolls() const noexcept
91 {
92 return *content_height > *aperture_height;
93 }
94
96 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
97 {
98 co_yield *_content;
99 }
100
101 [[nodiscard]] box_constraints update_constraints() noexcept override
102 {
103 _content_constraints = _content->update_constraints();
104
105 // The aperture can scroll so its minimum width and height are zero.
106 auto aperture_constraints = _content_constraints;
107 aperture_constraints.minimum = extent2i{0, 0};
108
109 return aperture_constraints.internalize_margins().constrain(*minimum, *maximum);
110 }
111
112 void set_layout(widget_layout const& context) noexcept override
113 {
114 if (compare_store(layout, context)) {
115 aperture_width = context.width() - _content_constraints.margins.left() - _content_constraints.margins.right();
116 aperture_height = context.height() - _content_constraints.margins.bottom() - _content_constraints.margins.top();
117
118 // Start scrolling with the preferred size as minimum, so
119 // that widgets in the content don't get unnecessarily squeezed.
120 content_width = *aperture_width < _content_constraints.preferred.width() ? _content_constraints.preferred.width() :
121 *aperture_width;
122 content_height = *aperture_height < _content_constraints.preferred.height() ?
123 _content_constraints.preferred.height() :
124 *aperture_height;
125 }
126
127 // Make sure the offsets are limited to the scrollable area.
128 hilet offset_x_max = std::max(*content_width - *aperture_width, 0);
129 hilet offset_y_max = std::max(*content_height - *aperture_height, 0);
130 offset_x = std::clamp(*offset_x, 0, offset_x_max);
131 offset_y = std::clamp(*offset_y, 0, offset_y_max);
132
133 // The position of the content rectangle relative to the scroll view.
134 // The size is further adjusted if the either the horizontal or vertical scroll bar is invisible.
135 _content_shape = box_shape{
136 _content_constraints,
138 -*offset_x + _content_constraints.margins.left(),
139 -*offset_y + _content_constraints.margins.bottom(),
140 *content_width,
141 *content_height},
142 theme<prefix>.cap_height(this)};
143
144 // The content needs to be at a higher elevation, so that hitbox check
145 // will work correctly for handling scrolling with mouse wheel.
146 _content->set_layout(context.transform(_content_shape, 1.0f, context.rectangle()));
147 }
148
149 void draw(widget_draw_context& context) noexcept override
150 {
152 _content->draw(context);
153 }
154 }
155
156 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
157 {
158 hi_axiom(loop::main().on_thread());
159
160 if (*mode >= widget_mode::partial) {
161 auto r = _content->hitbox_test_from_parent(position);
162
163 if (layout.contains(position)) {
164 r = std::max(r, hitbox{id, layout.elevation});
165 }
166 return r;
167
168 } else {
169 return {};
170 }
171 }
172
173 bool handle_event(gui_event const& event) noexcept override
174 {
175 hi_axiom(loop::main().on_thread());
176
177 if (event == gui_event_type::mouse_wheel) {
178 hilet new_offset_x = *offset_x + narrow_cast<int>(event.mouse().wheel_delta.x() * _scale / -4);
179 hilet new_offset_y = *offset_y + narrow_cast<int>(event.mouse().wheel_delta.y() * _scale / -4);
180 hilet max_offset_x = std::max(0, *content_width - *aperture_width);
181 hilet max_offset_y = std::max(0, *content_height - *aperture_height);
182
183 offset_x = std::clamp(new_offset_x, 0, max_offset_x);
184 offset_y = std::clamp(new_offset_y, 0, max_offset_y);
185 ++global_counter<"scroll_aperture_widget:mouse_wheel:relayout">;
186 process_event({gui_event_type::window_relayout});
187 return true;
188 } else {
189 return super::handle_event(event);
190 }
191 }
192
193 void scroll_to_show(hi::aarectanglei to_show) noexcept override
194 {
195 if (layout) {
196 auto safe_rectangle = intersect(layout.rectangle(), layout.clipping_rectangle);
197 int delta_x = 0;
198 int delta_y = 0;
199
200 hilet margin = std::max({
201 theme<prefix>.margin_left(this),
202 theme<prefix>.margin_right(this),
203 theme<prefix>.margin_top(this),
204 theme<prefix>.margin_bottom(this)});
205 if (safe_rectangle.width() > margin * 2 and safe_rectangle.height() > margin * 2) {
206 // This will look visually better, if the selected widget is moved with some margin from
207 // the edge of the scroll widget. The margins of the content do not have anything to do
208 // with the margins that are needed here.
209 safe_rectangle = safe_rectangle - margin;
210
211 if (to_show.right() > safe_rectangle.right()) {
212 delta_x = to_show.right() - safe_rectangle.right();
213 } else if (to_show.left() < safe_rectangle.left()) {
214 delta_x = to_show.left() - safe_rectangle.left();
215 }
216
217 if (to_show.top() > safe_rectangle.top()) {
218 delta_y = to_show.top() - safe_rectangle.top();
219 } else if (to_show.bottom() < safe_rectangle.bottom()) {
220 delta_y = to_show.bottom() - safe_rectangle.bottom();
221 }
222
223 // Scroll the widget
224 offset_x += delta_x;
225 offset_y += delta_y;
226 }
227
228 // There may be recursive scroll view, and they all need to move until the rectangle is visible.
229 if (parent) {
230 parent->scroll_to_show(layout.to_parent * translate2i(delta_x, delta_y) * to_show);
231 }
232
233 } else {
234 return super::scroll_to_show(to_show);
235 }
236 }
238private:
239 box_constraints _content_constraints;
240 box_shape _content_shape;
242 decltype(content_width)::callback_token _content_width_cbt;
243 decltype(content_height)::callback_token _content_height_cbt;
244 decltype(aperture_width)::callback_token _aperture_width_cbt;
245 decltype(aperture_height)::callback_token _aperture_height_cbt;
246 decltype(offset_x)::callback_token _offset_x_cbt;
247 decltype(offset_y)::callback_token _offset_y_cbt;
248 decltype(minimum)::callback_token _minimum_cbt;
249};
250
251}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ window_relayout
Request that widgets get laid out on the next frame.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:166
constexpr value_type & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:177
vector2i wheel_delta
Change in wheel rotation, in points (pt).
Definition gui_event.hpp:51
A user interface event.
Definition gui_event.hpp:75
mouse_event_data & mouse() noexcept
Get the mouse event information.
Definition gui_event.hpp:257
Definition widget.hpp:26
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
observer< extent2i > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:85
virtual void scroll_to_show(hi::aarectanglei rectangle) noexcept
Scroll to show the given rectangle on the window.
Definition widget.hpp:487
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
Definition widget.hpp:274
observer< extent2i > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:89
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
size_t semantic_layer
The draw layer of the widget.
Definition widget.hpp:81
Draw context for drawing using the HikoGUI shaders.
Definition widget_draw_context.hpp:205
The layout of a widget.
Definition widget_layout.hpp:37
translate2i to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:52
constexpr bool contains(point3i mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:126
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:72
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation, aarectanglei new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:203
aarectanglei clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:86
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
A scroll aperture widget.
Definition scroll_aperture_widget.hpp:23
T max(T... args)
T move(T... args)