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 "widget.hpp"
12
13namespace hi { inline namespace v1 {
14
23public:
24 using super = widget;
25
26 observer<float> content_width;
27 observer<float> content_height;
28 observer<float> aperture_width;
29 observer<float> aperture_height;
30 observer<float> offset_x;
31 observer<float> offset_y;
32
33 scroll_aperture_widget(widget *parent) noexcept : super(parent)
34 {
35 hi_axiom(loop::main().on_thread());
37
38 // The aperture-widget will not draw itself, only its selected content.
39 semantic_layer = parent->semantic_layer;
40
41 _content_width_cbt = content_width.subscribe([&](auto...) {
42 ++global_counter<"scroll_aperture_widget:content_width:relayout">;
44 });
45 _content_height_cbt = content_height.subscribe([&](auto...) {
46 ++global_counter<"scroll_aperture_widget:content_height:relayout">;
48 });
49 _aperture_width_cbt = aperture_width.subscribe([&](auto...) {
50 ++global_counter<"scroll_aperture_widget:aperture_width:relayout">;
52 });
53 _aperture_height_cbt = aperture_height.subscribe([&](auto...) {
54 ++global_counter<"scroll_aperture_widget:aperture_height:relayout">;
56 });
57 _offset_x_cbt = offset_x.subscribe([&](auto...) {
58 ++global_counter<"scroll_aperture_widget:offset_x:relayout">;
60 });
61 _offset_y_cbt = offset_y.subscribe([&](auto...) {
62 ++global_counter<"scroll_aperture_widget:offset_y:relayout">;
64 });
65 _minimum_cbt = minimum.subscribe([&](auto...) {
66 ++global_counter<"scroll_aperture_widget:minimum:reconstrain">;
68 });
69 }
70
71 template<typename Widget, typename... Args>
72 Widget& make_widget(Args&&...args) noexcept
73 {
74 hi_axiom(loop::main().on_thread());
75 hi_axiom(_content == nullptr);
76
77 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
78 auto& ref = *tmp;
79 _content = std::move(tmp);
80 return ref;
81 }
82
83 [[nodiscard]] bool x_axis_scrolls() const noexcept
84 {
85 return *content_width > *aperture_width;
86 }
87
88 [[nodiscard]] bool y_axis_scrolls() const noexcept
89 {
90 return *content_height > *aperture_height;
91 }
92
94 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
95 {
96 co_yield *_content;
97 }
98
99 [[nodiscard]] box_constraints update_constraints() noexcept override
100 {
101 _layout = {};
102 _content_constraints = _content->update_constraints();
103
104 // The aperture can scroll so its minimum width and height are zero.
105 auto aperture_constraints = _content_constraints;
106 aperture_constraints.minimum = extent2{0, 0};
107
108 return aperture_constraints.internalize_margins().constrain(*minimum, *maximum);
109 }
110
111 void set_layout(widget_layout const& context) noexcept override
112 {
113 if (compare_store(_layout, context)) {
114 aperture_width = context.width() - _content_constraints.margins.left() - _content_constraints.margins.right();
115 aperture_height = context.height() - _content_constraints.margins.bottom() - _content_constraints.margins.top();
116
117 // Start scrolling with the preferred size as minimum, so
118 // that widgets in the content don't get unnecessarily squeezed.
119 content_width = *aperture_width < _content_constraints.preferred.width() ? _content_constraints.preferred.width() :
120 *aperture_width;
121 content_height = *aperture_height < _content_constraints.preferred.height() ?
122 _content_constraints.preferred.height() :
123 *aperture_height;
124 }
125
126 // Make sure the offsets are limited to the scrollable area.
127 hilet offset_x_max = std::max(*content_width - *aperture_width, 0.0f);
128 hilet offset_y_max = std::max(*content_height - *aperture_height, 0.0f);
129 offset_x = std::clamp(*offset_x, 0.0f, offset_x_max);
130 offset_y = std::clamp(*offset_y, 0.0f, offset_y_max);
131
132 // The position of the content rectangle relative to the scroll view.
133 // The size is further adjusted if the either the horizontal or vertical scroll bar is invisible.
134 _content_shape = box_shape{
135 _content_constraints,
137 -*offset_x + _content_constraints.margins.left(),
138 -*offset_y + _content_constraints.margins.bottom(),
139 *content_width,
140 *content_height},
141 theme().baseline_adjustment()};
142
143 // The content needs to be at a higher elevation, so that hitbox check
144 // will work correctly for handling scrolling with mouse wheel.
145 _content->set_layout(context.transform(_content_shape, 1.0f, context.rectangle()));
146 }
147
148 void draw(draw_context const& context) noexcept override
149 {
151 _content->draw(context);
152 }
153 }
154
155 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
156 {
157 hi_axiom(loop::main().on_thread());
158
159 if (*mode >= widget_mode::partial) {
160 auto r = _content->hitbox_test_from_parent(position);
161
162 if (layout().contains(position)) {
163 r = std::max(r, hitbox{id, _layout.elevation});
164 }
165 return r;
166
167 } else {
168 return {};
169 }
170 }
171
172 bool handle_event(gui_event const& event) noexcept override
173 {
174 hi_axiom(loop::main().on_thread());
175
176 if (event == gui_event_type::mouse_wheel) {
177 hilet new_offset_x = *offset_x + std::round(event.mouse().wheel_delta.x() * theme().scale);
178 hilet new_offset_y = *offset_y + std::round(event.mouse().wheel_delta.y() * theme().scale);
179 hilet max_offset_x = std::max(0.0f, *content_width - *aperture_width);
180 hilet max_offset_y = std::max(0.0f, *content_height - *aperture_height);
181
182 offset_x = std::clamp(new_offset_x, 0.0f, max_offset_x);
183 offset_y = std::clamp(new_offset_y, 0.0f, max_offset_y);
184 ++global_counter<"scroll_aperture_widget:mouse_wheel:relayout">;
186 return true;
187 } else {
188 return super::handle_event(event);
189 }
190 }
191
192 void scroll_to_show(hi::aarectangle to_show) noexcept override
193 {
194 if (_layout) {
195 auto safe_rectangle = intersect(_layout.rectangle(), _layout.clipping_rectangle);
196 auto delta_x = 0.0f;
197 auto delta_y = 0.0f;
198
199 if (safe_rectangle.width() > theme().margin<float>() * 2.0f and safe_rectangle.height() > theme().margin<float>() * 2.0f) {
200 // This will look visually better, if the selected widget is moved with some margin from
201 // the edge of the scroll widget. The margins of the content do not have anything to do
202 // with the margins that are needed here.
203 safe_rectangle = safe_rectangle - theme().margin<float>();
204
205 if (to_show.right() > safe_rectangle.right()) {
206 delta_x = to_show.right() - safe_rectangle.right();
207 } else if (to_show.left() < safe_rectangle.left()) {
208 delta_x = to_show.left() - safe_rectangle.left();
209 }
210
211 if (to_show.top() > safe_rectangle.top()) {
212 delta_y = to_show.top() - safe_rectangle.top();
213 } else if (to_show.bottom() < safe_rectangle.bottom()) {
214 delta_y = to_show.bottom() - safe_rectangle.bottom();
215 }
216
217 // Scroll the widget
218 offset_x = std::round(offset_x + delta_x);
219 offset_y = std::round(offset_y + delta_y);
220 }
221
222 // There may be recursive scroll view, and they all need to move until the rectangle is visible.
223 if (parent) {
224 parent->scroll_to_show(_layout.to_parent * translate2(delta_x, delta_y) * to_show);
225 }
226
227 } else {
228 return super::scroll_to_show(to_show);
229 }
230 }
232private:
233 box_constraints _content_constraints;
234 box_shape _content_shape;
236 decltype(content_width)::callback_token _content_width_cbt;
237 decltype(content_height)::callback_token _content_height_cbt;
238 decltype(aperture_width)::callback_token _aperture_width_cbt;
239 decltype(aperture_height)::callback_token _aperture_height_cbt;
240 decltype(offset_x)::callback_token _offset_x_cbt;
241 decltype(offset_y)::callback_token _offset_y_cbt;
242 decltype(minimum)::callback_token _minimum_cbt;
243};
244
245}} // namespace hi::v1
Defines widget.
#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:200
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:26
A high-level geometric extent.
Definition extent2.hpp:26
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:101
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:112
Definition translate2.hpp:13
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:56
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:64
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:209
vector2 wheel_delta
Change in wheel rotation, in points (pt).
Definition gui_event.hpp:50
A user interface event.
Definition gui_event.hpp:74
mouse_event_data & mouse() noexcept
Get the mouse event information.
Definition gui_event.hpp:256
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:22
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:27
virtual void scroll_to_show(hi::aarectangle rectangle) noexcept=0
Scroll to show the given rectangle on the window.
The layout of a widget.
Definition widget_layout.hpp:35
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
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
A scroll aperture widget.
Definition scroll_aperture_widget.hpp:22
An interactive graphical object as part of the user-interface.
Definition widget.hpp:36
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:155
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:65
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:78
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:195
widget(widget *parent) noexcept
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:164
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:41
bool handle_event(gui_event const &event) noexcept override
Handle command.
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:82
T max(T... args)
T move(T... args)
T round(T... args)