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 <memory>
16#include <string>
17#include <array>
18#include <optional>
19#include <future>
20
21namespace hi { inline namespace v1 {
22
32template<axis Axis>
33class scroll_bar_widget final : public widget {
34public:
35 using super = widget;
36
37 static constexpr hi::axis axis = Axis;
38
39 observer<float> offset;
40 observer<float> aperture;
41 observer<float> content;
42
44 gui_window& window,
46 forward_of<observer<float>> auto&& content,
47 forward_of<observer<float>> auto&& aperture,
48 forward_of<observer<float>> auto&& offset) noexcept :
49 widget(window, parent), content(hi_forward(content)), aperture(hi_forward(aperture)), offset(hi_forward(offset))
50 {
51 // clang-format off
52 _content_cbt = this->content.subscribe([&](auto...){ request_relayout(); });
53 _aperture_cbt = this->aperture.subscribe([&](auto...){ request_relayout(); });
54 _offset_cbt = this->offset.subscribe([&](auto...){ request_relayout(); });
55 // clang-format on
56 }
57
59
60 widget_constraints const& set_constraints() noexcept override
61 {
62 _layout = {};
63
64 // The minimum size is twice the length of the slider, which is twice the theme().size()
65 if constexpr (axis == axis::vertical) {
66 return _constraints = {
67 {theme().icon_size, theme().size * 4.0f},
68 {theme().icon_size, theme().size * 4.0f},
69 {theme().icon_size, 32767.0f}};
70 } else {
71 return _constraints = {
72 {theme().size * 4.0f, theme().icon_size},
73 {theme().size * 4.0f, theme().icon_size},
74 {32767.0f, theme().icon_size}};
75 }
76 }
77
78 void set_layout(widget_layout const& layout) noexcept override
79 {
80 _layout = layout;
81
82 // Calculate the position of the slider.
83 hilet slider_offset = *offset * travel_vs_hidden_content_ratio();
84 if constexpr (axis == axis::vertical) {
85 _slider_rectangle = aarectangle{0.0f, slider_offset, layout.width(), slider_length()};
86 } else {
87 _slider_rectangle = aarectangle{slider_offset, 0.0f, slider_length(), layout.height()};
88 }
89 }
90
91 void draw(draw_context const& context) noexcept override
92 {
93 if (*mode > widget_mode::invisible and overlaps(context, layout())) {
94 draw_rails(context);
95 draw_slider(context);
96 }
97 }
98
99 hitbox hitbox_test(point3 position) const noexcept override
100 {
101 hi_axiom(is_gui_thread());
102
103 if (*mode >= widget_mode::partial and layout().contains(position) and _slider_rectangle.contains(position)) {
104 return {this, position};
105 } else {
106 return {};
107 }
108 }
109
110 bool handle_event(gui_event const& event) noexcept
111 {
112 switch (event.type()) {
113 case gui_event_type::mouse_down:
114 if (event.mouse().cause.left_button) {
115 // Record the original scroll-position before the drag starts.
116 _offset_before_drag = *offset;
117 return true;
118 }
119 break;
120
121 case gui_event_type::mouse_drag:
122 if (event.mouse().cause.left_button) {
123 // The distance the slider has to move relative to the slider position at the
124 // start of the drag.
125 hilet slider_movement = axis == axis::vertical ? event.drag_delta().y() : event.drag_delta().x();
126 hilet content_movement = slider_movement * hidden_content_vs_travel_ratio();
127 hilet new_offset = _offset_before_drag + content_movement;
128 offset = clamp_offset(new_offset);
129 return true;
130 }
131 break;
132
133 default:;
134 }
135
136 return super::handle_event(event);
137 }
138
139 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
140 {
141 return false;
142 }
143
144 [[nodiscard]] color background_color() const noexcept override
145 {
146 return theme().color(semantic_color::fill, semantic_layer);
147 }
148
149 [[nodiscard]] color foreground_color() const noexcept override
150 {
151 if (*hover) {
152 return theme().color(semantic_color::fill, semantic_layer + 2);
153 } else {
154 return theme().color(semantic_color::fill, semantic_layer + 1);
155 }
156 }
157
158private:
159 aarectangle _slider_rectangle;
160
161 float _offset_before_drag;
162
163 typename decltype(content)::callback_token _content_cbt;
164 typename decltype(aperture)::callback_token _aperture_cbt;
165 typename decltype(offset)::callback_token _offset_cbt;
166
171 [[nodiscard]] float clamp_offset(float new_offset) const noexcept
172 {
173 hilet scrollable_distance = std::max(0.0f, *content - *aperture);
174 return std::clamp(new_offset, 0.0f, scrollable_distance);
175 }
176
177 [[nodiscard]] float rail_length() const noexcept
178 {
179 hi_axiom(is_gui_thread());
180 return axis == axis::vertical ? layout().height() : layout().width();
181 }
182
183 [[nodiscard]] float slider_length() const noexcept
184 {
185 hi_axiom(is_gui_thread());
186
187 hilet content_aperture_ratio = *content != 0.0f ? *aperture / *content : 1.0f;
188 hilet rail_length_ = rail_length();
189 return std::clamp(rail_length_ * content_aperture_ratio, theme().size * 2.0f, rail_length_);
190 }
191
194 [[nodiscard]] float slider_travel_range() const noexcept
195 {
196 hi_axiom(is_gui_thread());
197 return rail_length() - slider_length();
198 }
199
202 [[nodiscard]] float hidden_content() const noexcept
203 {
204 hi_axiom(is_gui_thread());
205 return *content - *aperture;
206 }
207
212 [[nodiscard]] float hidden_content_vs_travel_ratio() const noexcept
213 {
214 hi_axiom(is_gui_thread());
215
216 hilet _slider_travel_range = slider_travel_range();
217 return _slider_travel_range != 0.0f ? hidden_content() / _slider_travel_range : 0.0f;
218 }
219
224 [[nodiscard]] float travel_vs_hidden_content_ratio() const noexcept
225 {
226 hi_axiom(is_gui_thread());
227
228 hilet _hidden_content = hidden_content();
229 return _hidden_content != 0.0f ? slider_travel_range() / _hidden_content : 0.0f;
230 }
231
232 void draw_rails(draw_context const& context) noexcept
233 {
234 hilet corner_radii =
235 axis == axis::vertical ? hi::corner_radii{layout().width() * 0.5f} : hi::corner_radii{layout().height() * 0.5f};
236 context.draw_box(layout(), layout().rectangle(), background_color(), corner_radii);
237 }
238
239 void draw_slider(draw_context const& context) noexcept
240 {
241 hilet corner_radii = axis == axis::vertical ? hi::corner_radii{_slider_rectangle.width() * 0.5f} :
242 hi::corner_radii{_slider_rectangle.height() * 0.5f};
243
244 context.draw_box(layout(), translate_z(0.1f) * _slider_rectangle, foreground_color(), corner_radii);
245 }
246};
247
248using horizontal_scroll_bar_widget = scroll_bar_widget<axis::horizontal>;
249using vertical_scroll_bar_widget = scroll_bar_widget<axis::vertical>;
250
251}} // namespace hi::v1
#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
Defines widget.
Definition of GUI event types.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
A user interface event.
Definition gui_event.hpp:66
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:33
hitbox hitbox_test(point3 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition scroll_bar_widget.hpp:99
void set_layout(widget_layout const &layout) noexcept override
Update the internal layout of the widget.
Definition scroll_bar_widget.hpp:78
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition scroll_bar_widget.hpp:91
bool handle_event(gui_event const &event) noexcept
Handle command.
Definition scroll_bar_widget.hpp:110
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition scroll_bar_widget.hpp:139
widget_constraints const & set_constraints() noexcept override
Update the constraints of the widget.
Definition scroll_bar_widget.hpp:60
An interactive graphical object as part of the user-interface.
Definition widget.hpp:44
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:198
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 *const parent
Pointer to the parent widget.
Definition widget.hpp:53
widget(gui_window &window, widget *parent) noexcept
hi::theme const & theme() const noexcept
Get the theme.
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
void request_relayout() const noexcept
Request the window to be relayout on the next frame.
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:48
The constraints of a widget.
Definition widget_constraints.hpp:26
The layout of a widget.
Definition widget_layout.hpp:37
T max(T... args)