ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
slider.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5
8
10{
11 const float lo = std::min(min, max);
12 const float hi = std::max(min, max);
13 value = std::clamp(v, lo, hi);
14}
15
17{
18 return adjust_on_left_right.value_or(!in_row_with_focusables(*this));
19}
20
22{
23 // Only keyboard and controller activation edits, clicks set the value
24 if (!ctx.show_focus) {
25 return;
26 }
27 _editing = !adjusts_left_right() && !_editing;
28}
29
31{
32 (void)ctx;
33 if (!focused) {
34 _editing = false;
35 }
36}
37
39{
40 const float old = value;
41 set_value(v);
42 if (value != old && on_change) {
43 on_change(value);
44 }
45}
46
48{
49 const float range = max - min;
50 const float width = transform.size.x;
51 if (width <= 0.0F || range == 0.0F) {
52 return;
53 }
54
55 float v = min + (std::clamp((x - transform.position.x) / width, 0.0F, 1.0F) * range);
56 if (snap && step > 0.0F) {
57 v = min + (std::round((v - min) / step) * step);
58 }
59 change(v);
60}
61
63{
64 (void)ctx;
65 if (!enabled) {
66 return false;
67 }
68
69 switch (e.type) {
72 return false;
73 }
74 set_from_pointer(e.pointer_pos.x);
75 return true;
77 // Root sends moves to the pressed widget while the button is held
78 if (_captured) {
79 set_from_pointer(e.pointer_pos.x);
80 return true;
81 }
82 return false;
84 if (_editing) {
85 _editing = false;
86 return true;
87 }
88 return false;
90 if (!adjusts_left_right() && !_editing) {
91 return false;
92 }
93 if (e.key == asw::input::Key::Left) {
94 change(value - step);
95 return true;
96 }
97 if (e.key == asw::input::Key::Right) {
98 change(value + step);
99 return true;
100 }
101 return false;
102 default:
103 return false;
104 }
105}
106
108{
109 const auto& s = get_style(ctx);
110 const float range = max - min;
111 const float t = range == 0.0F ? 0.0F : std::clamp((value - min) / range, 0.0F, 1.0F);
112
113 const float track_y = transform.position.y + ((transform.size.y - s.track_height) / 2.0F);
114 const asw::Quad<float> track { { transform.position.x, track_y },
115 { transform.size.x, s.track_height } };
116 asw::draw::rect_fill(track, s.track);
117
118 const asw::Quad<float> fill { track.position, { transform.size.x * t, s.track_height } };
119 asw::draw::rect_fill(fill, enabled ? s.fill : s.disabled);
120
121 const float knob_x = transform.position.x + (transform.size.x * t) - (s.knob_width / 2.0F);
122 const asw::Quad<float> knob { { knob_x, transform.position.y },
123 { s.knob_width, transform.size.y } };
124
125 asw::Color knob_color = s.knob;
126 if (!enabled) {
127 knob_color = s.disabled;
128 } else if (_editing) {
129 knob_color = s.knob_editing;
130 } else if (is_highlighted(ctx)) {
131 knob_color = s.knob_hover;
132 }
133 asw::draw::rect_fill(knob, knob_color);
134
135 Widget::draw(ctx);
136}
A 2D rectangle in space.
Definition geometry.h:504
T x
The x component of the vector.
Definition geometry.h:277
Shared state for the UI system.
Definition context.h:97
bool show_focus
Whether focus is shown. Root turns it on for keyboard and controller navigation and off when the mous...
Definition context.h:115
bool on_event(Context &ctx, const UIEvent &e) override
Handle dragging and left and right while focused.
Definition slider.cpp:62
void draw(Context &ctx) override
Draw the slider.
Definition slider.cpp:107
bool adjusts_left_right() const
Whether left and right change the value right now, without editing.
Definition slider.cpp:16
void change(float v)
Definition slider.cpp:38
void set_from_pointer(float x)
Definition slider.cpp:47
void on_focus_changed(Context &ctx, bool focused) override
Leave edit mode when focus moves away.
Definition slider.cpp:30
float min
Smallest value.
Definition slider.h:40
float max
Largest value.
Definition slider.h:43
float value
Current value.
Definition slider.h:37
void set_value(float v)
Set the value without calling on_change.
Definition slider.cpp:9
void activate(Context &ctx) override
Enter or leave edit mode when left and right move focus.
Definition slider.cpp:21
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:163
Routines for drawing sprites and primitives to the screen.
Layout widgets for the ASW UI module.
void rect_fill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:536
bool in_row_with_focusables(const Widget &w)
Whether a widget sits in a row with other widgets that can take focus: its parent is a horizontal Sta...
Definition layout.cpp:138
Slider widget for the ASW UI module.
RGBA color struct with 8-bit channels.
Definition color.h:18
Event structure for UI interactions.
Definition event.h:21
asw::Vec2< float > pointer_pos
The pointer position.
Definition event.h:45
asw::input::MouseButton mouse_button
The mouse button associated with the event.
Definition event.h:48
asw::input::Key key
The key associated with the event.
Definition event.h:39
Type type
The type of the event.
Definition event.h:36