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.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_SLIDER_H
10#define ASW_MODULES_UI_SLIDER_H
11
12#include <functional>
13#include <optional>
14
15#include "context.h"
16#include "widget.h"
17
18namespace asw::ui {
19
27class Slider : public Widget {
28public:
32 {
33 focusable = true;
34 }
35
37 float value = 0.0F;
38
40 float min = 0.0F;
41
43 float max = 1.0F;
44
47 float step = 0.1F;
48
50 bool snap = false;
51
53 std::function<void(float)> on_change;
54
58 std::optional<bool> adjust_on_left_right;
59
65 bool adjusts_left_right() const;
66
71 bool is_editing() const
72 {
73 return _editing;
74 }
75
77 std::optional<SliderStyle> style;
78
84 const SliderStyle& get_style(const Context& ctx) const
85 {
86 return style ? *style : ctx.theme.slider;
87 }
88
93 void set_value(float v);
94
99 void activate(Context& ctx) override;
100
106 void on_focus_changed(Context& ctx, bool focused) override;
107
114 bool on_event(Context& ctx, const UIEvent& e) override;
115
120 void draw(Context& ctx) override;
121
122private:
123 bool _editing = false;
124
125 void change(float v);
126 void set_from_pointer(float x);
127};
128
129} // namespace asw::ui
130
131#endif // ASW_MODULES_UI_SLIDER_H
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
A horizontal slider for a value between min and max, e.g. volume.
Definition slider.h:27
const SliderStyle & get_style(const Context &ctx) const
Get the style this slider draws with.
Definition slider.h:84
std::function< void(float)> on_change
Callback invoked with the new value when the user changes it.
Definition slider.h:53
bool on_event(Context &ctx, const UIEvent &e) override
Handle dragging and left and right while focused.
Definition slider.cpp:62
bool adjusts_left_right() const
Whether left and right change the value right now, without editing.
Definition slider.cpp:16
float step
Change per left or right press. Dragging snaps to it when snap is set.
Definition slider.h:47
std::optional< bool > adjust_on_left_right
Whether left and right change the value without editing first. Empty to decide from the layout: off i...
Definition slider.h:58
void change(float v)
Definition slider.cpp:38
bool snap
Snap dragged values to step.
Definition slider.h:50
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
Slider()
Default constructor.
Definition slider.h:31
bool is_editing() const
Whether the slider is in edit mode.
Definition slider.h:71
std::optional< SliderStyle > style
Style for this slider only. Uses the theme slider style when empty.
Definition slider.h:77
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
Base class for all UI widgets.
Definition widget.h:43
bool focusable
Whether the widget can receive focus. Focusable widgets are also the ones the pointer can press.
Definition widget.h:88
UI context and focus management for the ASW UI module.
How a slider looks. Set on Theme::slider for every slider, or on Slider::style for one slider.
Definition theme.h:145
SliderStyle slider
Default slider style.
Definition theme.h:273
Event structure for UI interactions.
Definition event.h:21
Base widget class for the ASW UI module.