HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scroll_bar_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-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#include "../GUI/gui_event.hpp"
13#include "../geometry/axis.hpp"
14#include "../observer.hpp"
15#include "../numbers.hpp"
16#include <memory>
17#include <string>
18#include <array>
19#include <optional>
20#include <future>
21
22namespace hi { inline namespace v1 {
23
33template<axis Axis>
34class scroll_bar_widget final : public widget {
35public:
36 using super = widget;
37
38 static constexpr hi::axis axis = Axis;
39
40 observer<int> offset;
41 observer<int> aperture;
42 observer<int> content;
43
46 forward_of<observer<int>> auto&& content,
47 forward_of<observer<int>> auto&& aperture,
48 forward_of<observer<int>> auto&& offset) noexcept :
49 widget(parent), content(hi_forward(content)), aperture(hi_forward(aperture)), offset(hi_forward(offset))
50 {
51 _content_cbt = this->content.subscribe([&](auto...) {
52 ++global_counter<"scroll_bar_widget:content:relayout">;
53 process_event({gui_event_type::window_relayout});
54 });
55 _aperture_cbt = this->aperture.subscribe([&](auto...) {
56 ++global_counter<"scroll_bar_widget:aperture:relayout">;
57 process_event({gui_event_type::window_relayout});
58 });
59 _offset_cbt = this->offset.subscribe([&](auto...) {
60 ++global_counter<"scroll_bar_widget:offset:relayout">;
61 process_event({gui_event_type::window_relayout});
62 });
63 }
64
66
67 [[nodiscard]] box_constraints update_constraints() noexcept override
68 {
69 _layout = {};
70
72 return {};
73 }
74
75 // The minimum size is twice the length of the slider, which is twice the theme().size()
76 if constexpr (axis == axis::vertical) {
77 return {
78 extent2i{theme().icon_size, theme().size * 4},
79 extent2i{theme().icon_size, theme().size * 4},
80 extent2i{theme().icon_size, large_number_v<int>}};
81 } else {
82 return {
83 extent2i{theme().size * 4, theme().icon_size},
84 extent2i{theme().size * 4, theme().icon_size},
85 extent2i{large_number_v<int>, theme().icon_size}};
86 }
87 }
88
89 void set_layout(widget_layout const& context) noexcept override
90 {
91 _layout = context;
92
94 _slider_rectangle = {};
95 return;
96 }
97
98 // Calculate the position of the slider.
99 hilet slider_offset = narrow_cast<int>(std::round(*offset * travel_vs_hidden_content_ratio()));
100 if constexpr (axis == axis::vertical) {
101 _slider_rectangle = aarectanglei{0, slider_offset, context.width(), slider_length()};
102 } else {
103 _slider_rectangle = aarectanglei{slider_offset, 0, slider_length(), context.height()};
104 }
105 }
106
107 [[nodiscard]] bool visible() const noexcept
108 {
109 return *aperture < *content;
110 }
111
112 void draw(draw_context const& context) noexcept override
113 {
114 if (*mode > widget_mode::invisible and overlaps(context, layout()) and visible()) {
115 draw_rails(context);
116 draw_slider(context);
117 }
118 }
119
120 hitbox hitbox_test(point2i position) const noexcept override
121 {
122 hi_axiom(loop::main().on_thread());
123
124 if (*mode >= widget_mode::partial and layout().contains(position) and visible() and
125 _slider_rectangle.contains(position)) {
126 return {this, _layout.elevation, hitbox_type::scroll_bar};
127 } else {
128 return {};
129 }
130 }
131
132 bool handle_event(gui_event const& event) noexcept
133 {
134 switch (event.type()) {
135 case gui_event_type::mouse_down:
136 if (event.mouse().cause.left_button) {
137 // Record the original scroll-position before the drag starts.
138 _offset_before_drag = *offset;
139 return true;
140 }
141 break;
142
143 case gui_event_type::mouse_drag:
144 if (event.mouse().cause.left_button) {
145 // The distance the slider has to move relative to the slider position at the
146 // start of the drag.
147 hilet slider_movement =
148 narrow_cast<int>(axis == axis::vertical ? event.drag_delta().y() : event.drag_delta().x());
149 hilet content_movement = narrow_cast<int>(std::round(slider_movement * hidden_content_vs_travel_ratio()));
150 hilet new_offset = _offset_before_drag + content_movement;
151 offset = clamp_offset(new_offset);
152 return true;
153 }
154 break;
155
156 default:;
157 }
158
159 return super::handle_event(event);
160 }
161
162 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
163 {
164 return false;
165 }
166
167 [[nodiscard]] color background_color() const noexcept override
168 {
169 return theme().color(semantic_color::fill, semantic_layer);
170 }
171
172 [[nodiscard]] color foreground_color() const noexcept override
173 {
174 if (*hover) {
175 return theme().color(semantic_color::fill, semantic_layer + 2);
176 } else {
177 return theme().color(semantic_color::fill, semantic_layer + 1);
178 }
179 }
180
181private:
182 aarectanglei _slider_rectangle;
183
184 int _offset_before_drag;
185
186 typename decltype(content)::callback_token _content_cbt;
187 typename decltype(aperture)::callback_token _aperture_cbt;
188 typename decltype(offset)::callback_token _offset_cbt;
189
194 [[nodiscard]] int clamp_offset(int new_offset) const noexcept
195 {
196 hilet scrollable_distance = std::max(0, *content - *aperture);
197 return std::clamp(new_offset, 0, scrollable_distance);
198 }
199
200 [[nodiscard]] int rail_length() const noexcept
201 {
202 hi_axiom(loop::main().on_thread());
203 return axis == axis::vertical ? layout().height() : layout().width();
204 }
205
206 [[nodiscard]] int slider_length() const noexcept
207 {
208 hi_axiom(loop::main().on_thread());
209
210 hilet preferred_length = [&] {
211 if (*content == 0) {
212 return rail_length();
213 } else {
214 return *aperture * rail_length() / *content;
215 }
216 }();
217
218 return std::clamp(preferred_length, narrow_cast<int>(theme().size) * 2, rail_length());
219 }
220
223 [[nodiscard]] int slider_travel_range() const noexcept
224 {
225 hi_axiom(loop::main().on_thread());
226 return rail_length() - slider_length();
227 }
228
231 [[nodiscard]] int hidden_content() const noexcept
232 {
233 hi_axiom(loop::main().on_thread());
234 return *content - *aperture;
235 }
236
241 [[nodiscard]] float hidden_content_vs_travel_ratio() const noexcept
242 {
243 hi_axiom(loop::main().on_thread());
244
245 hilet _slider_travel_range = slider_travel_range();
246 return _slider_travel_range != 0 ? narrow_cast<float>(hidden_content()) / _slider_travel_range : 0.0f;
247 }
248
253 [[nodiscard]] float travel_vs_hidden_content_ratio() const noexcept
254 {
255 hi_axiom(loop::main().on_thread());
256
257 hilet _hidden_content = hidden_content();
258 return _hidden_content != 0 ? narrow_cast<float>(slider_travel_range()) / _hidden_content : 0.0f;
259 }
260
261 void draw_rails(draw_context const& context) noexcept
262 {
263 hilet corner_radii =
264 axis == axis::vertical ? hi::corner_radii{layout().width() * 0.5f} : hi::corner_radii{layout().height() * 0.5f};
265 context.draw_box(layout(), layout().rectangle(), background_color(), corner_radii);
266 }
267
268 void draw_slider(draw_context const& context) noexcept
269 {
270 hilet corner_radii = axis == axis::vertical ? hi::corner_radii{narrow_cast<float>(_slider_rectangle.width() / 2)} :
271 hi::corner_radii{narrow_cast<float>(_slider_rectangle.height() / 2)};
272
273 context.draw_box(
274 layout(), translate_z(0.1f) * narrow_cast<aarectangle>(_slider_rectangle), foreground_color(), corner_radii);
275 }
276};
277
278}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
The axis data type.
This file contains constants and conversion functions.
Defines widget.
Definition of GUI event types.
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:18
@ window_relayout
Request that widgets get laid out on the next frame.
@ rectangle
The gui_event has rectangle data.
@ partial
A widget is partially enabled.
@ collapse
The widget is collapsed.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
constexpr bool contains(point< value_type, 2 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:266
The 4 radiuses of the corners of a quad or rectangle.
Definition corner_radii.hpp:17
A user interface event.
Definition gui_event.hpp:77
2D constraints.
Definition box_constraints.hpp:25
Scroll bar widget This widget is used in a pair of a vertical and horizontal scrollbar as a child of ...
Definition scroll_bar_widget.hpp:34
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition scroll_bar_widget.hpp:112
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition scroll_bar_widget.hpp:89
hitbox hitbox_test(point2i position) const noexcept override
Find the widget that is under the mouse cursor.
Definition scroll_bar_widget.hpp:120
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition scroll_bar_widget.hpp:67
bool handle_event(gui_event const &event) noexcept
Handle command.
Definition scroll_bar_widget.hpp:132
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition scroll_bar_widget.hpp:162
An interactive graphical object as part of the user-interface.
Definition widget.hpp:46
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:194
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:86
observer< bool > hover
Mouse cursor is hovering over the widget.
Definition widget.hpp:66
widget(widget *parent) noexcept
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
widget * parent
Pointer to the parent widget.
Definition widget.hpp:51
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
The layout of a widget.
Definition widget_layout.hpp:41
T max(T... args)
T round(T... args)