8#include "../GUI/theme.hpp"
9#include "../GUI/mouse_event.hpp"
10#include "../geometry/axis.hpp"
11#include "../observable.hpp"
25 static constexpr tt::axis axis = Axis;
27 template<
typename Content,
typename Aperture,
typename Offset>
32 Offset &&offset) noexcept :
34 content(std::forward<Content>(content)),
35 aperture(std::forward<Aperture>(aperture)),
36 offset(std::forward<Offset>(offset))
38 _content_callback = this->content.
subscribe([
this](
auto...) {
41 _aperture_callback = this->aperture.
subscribe([
this](
auto...) {
44 _offset_callback = this->offset.
subscribe([
this](
auto...) {
53 tt_axiom(is_gui_thread());
56 if constexpr (axis == axis::vertical) {
57 _minimum_size = _preferred_size = {theme::global().icon_size, theme::global().large_size};
58 _maximum_size = {theme::global().icon_size, 32767.0f};
60 _minimum_size = _preferred_size = {theme::global().large_size, theme::global().icon_size};
61 _maximum_size = {32767.0f, theme::global().icon_size};
63 tt_axiom(_minimum_size <= _preferred_size && _preferred_size <= _maximum_size);
72 tt_axiom(is_gui_thread());
74 need_layout |= _request_layout.exchange(
false);
76 tt_axiom(*content != 0.0f);
79 ttlet slider_offset = *offset * travel_vs_hidden_content_ratio();
81 if constexpr (axis == axis::vertical) {
95 tt_axiom(is_gui_thread());
97 if (overlaps(context, this->_clipping_rectangle) and
visible) {
106 tt_axiom(is_gui_thread());
108 if (
visible and _visible_rectangle.contains(position) and slider_rectangle.
contains(position)) {
117 tt_axiom(is_gui_thread());
120 if (event.cause.leftButton) {
123 switch (event.type) {
124 using enum mouse_event::Type;
127 offset_before_drag = *offset;
133 ttlet slider_movement = axis == axis::vertical ?
event.delta().y() :
event.delta().x();
134 ttlet content_movement = slider_movement * hidden_content_vs_travel_ratio();
135 offset = offset_before_drag + content_movement;
149 [[nodiscard]]
color background_color() const noexcept
override
154 [[nodiscard]] color foreground_color() const noexcept
override
164 observable<float> offset;
165 observable<float> aperture;
166 observable<float> content;
168 typename decltype(offset)::callback_ptr_type _offset_callback;
169 typename decltype(aperture)::callback_ptr_type _aperture_callback;
170 typename decltype(content)::callback_ptr_type _content_callback;
172 aarectangle slider_rectangle;
174 float offset_before_drag;
176 [[nodiscard]]
float rail_length() const noexcept
178 tt_axiom(is_gui_thread());
182 [[nodiscard]]
float slider_length() const noexcept
184 tt_axiom(is_gui_thread());
186 ttlet content_aperture_ratio = *aperture / *content;
187 return std::max(rail_length() * content_aperture_ratio, theme::global().size * 2.0f);
192 [[nodiscard]]
float slider_travel_range() const noexcept
194 tt_axiom(is_gui_thread());
195 return rail_length() - slider_length();
200 [[nodiscard]]
float hidden_content() const noexcept
202 tt_axiom(is_gui_thread());
203 return *content - *aperture;
210 [[nodiscard]]
float hidden_content_vs_travel_ratio() const noexcept
212 tt_axiom(is_gui_thread());
214 ttlet _slider_travel_range = slider_travel_range();
215 return _slider_travel_range != 0.0f ? hidden_content() / _slider_travel_range : 0.0f;
222 [[nodiscard]]
float travel_vs_hidden_content_ratio() const noexcept
224 tt_axiom(is_gui_thread());
226 ttlet _hidden_content = hidden_content();
227 return _hidden_content != 0.0f ? slider_travel_range() / _hidden_content : 0.0f;
230 void draw_rails(draw_context context)
noexcept
232 tt_axiom(is_gui_thread());
234 ttlet corner_shapes =
236 context.draw_box(
rectangle(), background_color(), corner_shapes);
239 void draw_slider(draw_context context)
noexcept
241 tt_axiom(is_gui_thread());
243 ttlet corner_shapes = axis == axis::vertical ?
tt::corner_shapes{slider_rectangle.width() * 0.5f} :
246 context.draw_box(translate_z(0.1f) * slider_rectangle, foreground_color(), corner_shapes);
250using horizontal_scroll_bar_widget = scroll_bar_widget<axis::horizontal>;
251using vertical_scroll_bar_widget = scroll_bar_widget<axis::vertical>;
This is a RGBA floating point color.
Definition color.hpp:36
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:224
Definition corner_shapes.hpp:9
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:29
Definition gui_window.hpp:36
std::atomic< bool > requestLayout
When set to true the widgets will be laid out.
Definition gui_window.hpp:53
Definition mouse_event.hpp:15
callback_ptr_type subscribe(Callback &&callback) noexcept
Subscribe a callback function.
Definition observable.hpp:442
Definition scroll_bar_widget.hpp:21
void layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition scroll_bar_widget.hpp:70
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition scroll_bar_widget.hpp:93
bool constrain(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition scroll_bar_widget.hpp:51
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition scroll_bar_widget.hpp:144
hitbox hitbox_test(point2 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition scroll_bar_widget.hpp:104
bool handle_event(mouse_event const &event) noexcept
Definition scroll_bar_widget.hpp:115
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:48
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:92
observable< bool > visible
The widget is visible.
Definition widget.hpp:62
aarectangle rectangle() const noexcept
Get the rectangle in local coordinates.
virtual bool handle_event(command command) noexcept
Handle command.
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:43
virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept
Draw the widget.
virtual bool constrain(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
widget(gui_window &window, widget *parent) noexcept
float draw_layer
The draw layer of the widget.
Definition widget.hpp:76
virtual void layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.