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
asw::ui::Slider Class Reference

A horizontal slider for a value between min and max, e.g. volume. More...

#include <slider.h>

Inheritance diagram for asw::ui::Slider:
[legend]
Collaboration diagram for asw::ui::Slider:
[legend]

Public Member Functions

 Slider ()
 Default constructor.
 
bool adjusts_left_right () const
 Whether left and right change the value right now, without editing.
 
bool is_editing () const
 Whether the slider is in edit mode.
 
const SliderStyle & get_style (const Context &ctx) const
 Get the style this slider draws with.
 
void set_value (float v)
 Set the value without calling on_change.
 
void activate (Context &ctx) override
 Enter or leave edit mode when left and right move focus.
 
void on_focus_changed (Context &ctx, bool focused) override
 Leave edit mode when focus moves away.
 
bool on_event (Context &ctx, const UIEvent &e) override
 Handle dragging and left and right while focused.
 
void draw (Context &ctx) override
 Draw the slider.
 
- Public Member Functions inherited from asw::ui::Widget
 Widget ()
 Default constructor for Widget.
 
virtual ~Widget ()=default
 Default virtual destructor.
 
 Widget (Widget &&other) noexcept
 Move constructor. The children move over and point to the new widget as their parent. The new widget gets its own id and no parent, since it is not in the old parent's children. Removed children stay with the old widget, which is where Root finds and frees them.
 
Widget & operator= (Widget &&other) noexcept
 Move assignment operator. The children move over and point to this widget as their parent. This widget keeps its place in the tree: its parent, id and state. Its old children are removed as by clear_children. Removed children of the other widget stay with it.
 
 Widget (const Widget &)=delete
 
Widget & operator= (const Widget &)=delete
 
WidgetId id () const
 Get the unique identifier for this widget.
 
const std::vector< std::unique_ptr< Widget > > & children () const
 Get the child widgets. Change them with add_child, remove_child and clear_children, which keep removed widgets alive until Root is done with them.
 
virtual void measure (Context &ctx)
 Set this widget's own size, before its parent places it. Does nothing by default.
 
virtual void layout (Context &ctx)
 Lay out this widget and its children.
 
template<class T , class... Args>
T & add_child (Args &&... args)
 Add a child widget.
 
bool remove_child (const Widget &child)
 Remove a child widget. It leaves the tree now and is destroyed at the next Root::update, so a callback can remove any widget, even the one it runs in.
 
void clear_children ()
 Remove every child widget, destroyed as for remove_child.
 
bool is_hovered () const
 Whether the pointer is currently over this widget.
 
bool is_pressed () const
 Whether this widget is currently being pressed.
 
bool is_focused () const
 Whether this widget currently holds focus.
 
bool is_captured () const
 Whether this widget was pressed and the pointer is still held, even if it has moved off the widget.
 
bool is_highlighted (const Context &ctx) const
 Whether to draw the hover look: hovered, pressed, or focused while focus is shown.
 

Public Attributes

float value = 0.0F
 Current value.
 
float min = 0.0F
 Smallest value.
 
float max = 1.0F
 Largest value.
 
float step = 0.1F
 Change per left or right press. Dragging snaps to it when snap is set.
 
bool snap = false
 Snap dragged values to step.
 
std::function< void(float)> on_change
 Callback invoked with the new value when the user changes it.
 
std::optional< bool > adjust_on_left_right
 Whether left and right change the value without editing first. Empty to decide from the layout: off in a row with other focusable widgets, on otherwise.
 
std::optional< SliderStyle > style
 Style for this slider only. Uses the theme slider style when empty.
 
- Public Attributes inherited from asw::ui::Widget
bool visible = true
 Whether the widget is visible.
 
bool enabled = true
 Whether the widget is enabled.
 
bool focusable = false
 Whether the widget can receive focus. Focusable widgets are also the ones the pointer can press.
 
Widget * nav_up = nullptr
 Widget to focus on up, instead of the nearest one. Optional.
 
Widget * nav_down = nullptr
 Widget to focus on down, instead of the nearest one. Optional.
 
Widget * nav_left = nullptr
 Widget to focus on left, instead of the nearest one. Optional.
 
Widget * nav_right = nullptr
 Widget to focus on right, instead of the nearest one. Optional.
 
bool focus_ring = true
 Draw the theme focus ring around this widget while it has visible focus. Turn off for widgets that show focus themselves.
 
Widget * parent = nullptr
 Pointer to the parent widget.
 
asw::Quad< float > transform
 The transform (position and size) of the widget.
 
Anchor anchor = Anchor::None
 Pin this widget to a point of its parent, e.g. BottomRight for a corner button. The parent places it on every layout, so it stays in place when the screen or its own size changes. Stack and Grid place their children themselves and ignore it.
 
asw::Vec2< float > anchor_margin { 0.0F, 0.0F }
 Space between an anchored widget and the parent's edges.
 

Private Member Functions

void change (float v)
 
void set_from_pointer (float x)
 

Private Attributes

bool _editing = false
 

Additional Inherited Members

- Protected Member Functions inherited from asw::ui::Widget
void place_anchored (Widget &child) const
 Place a child that has an anchor inside this widget.
 
- Protected Attributes inherited from asw::ui::Widget
bool _hovered = false
 
bool _pressed = false
 
bool _focused = false
 
bool _captured = false
 

Detailed Description

A horizontal slider for a value between min and max, e.g. volume.

Drag or click the track to set the value. While focused, left and right move it by step and up and down move focus. In a row with other focusable widgets left and right move focus instead: activate the slider to edit it with left and right, then activate or go back to finish.

Definition at line 27 of file slider.h.

Constructor & Destructor Documentation

◆ Slider()

asw::ui::Slider::Slider ( )
inline

Default constructor.

Definition at line 31 of file slider.h.

Member Function Documentation

◆ activate()

void asw::ui::Slider::activate ( Context & ctx)
overridevirtual

Enter or leave edit mode when left and right move focus.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 21 of file slider.cpp.

◆ adjusts_left_right()

bool asw::ui::Slider::adjusts_left_right ( ) const

Whether left and right change the value right now, without editing.

Returns
adjust_on_left_right, or the layout default.

Definition at line 16 of file slider.cpp.

◆ change()

void asw::ui::Slider::change ( float v)
private

Definition at line 38 of file slider.cpp.

◆ draw()

void asw::ui::Slider::draw ( Context & ctx)
overridevirtual

Draw the slider.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 107 of file slider.cpp.

◆ get_style()

const SliderStyle & asw::ui::Slider::get_style ( const Context & ctx) const
inline

Get the style this slider draws with.

Parameters
ctxThe UI context.
Returns
The slider's own style, or the theme slider style.

Definition at line 84 of file slider.h.

◆ is_editing()

bool asw::ui::Slider::is_editing ( ) const
inline

Whether the slider is in edit mode.

Returns
True while left and right edit the value after activating.

Definition at line 71 of file slider.h.

◆ on_event()

bool asw::ui::Slider::on_event ( Context & ctx,
const UIEvent & e )
overridevirtual

Handle dragging and left and right while focused.

Parameters
ctxThe UI context.
eThe event.
Returns
True if the event was handled.

Reimplemented from asw::ui::Widget.

Definition at line 62 of file slider.cpp.

◆ on_focus_changed()

void asw::ui::Slider::on_focus_changed ( Context & ctx,
bool focused )
overridevirtual

Leave edit mode when focus moves away.

Parameters
ctxThe UI context.
focusedWhether the widget is now focused.

Reimplemented from asw::ui::Widget.

Definition at line 30 of file slider.cpp.

◆ set_from_pointer()

void asw::ui::Slider::set_from_pointer ( float x)
private

Definition at line 47 of file slider.cpp.

◆ set_value()

void asw::ui::Slider::set_value ( float v)

Set the value without calling on_change.

Parameters
vThe value, clamped to min and max.

Definition at line 9 of file slider.cpp.

Member Data Documentation

◆ _editing

bool asw::ui::Slider::_editing = false
private

Definition at line 123 of file slider.h.

◆ adjust_on_left_right

std::optional<bool> asw::ui::Slider::adjust_on_left_right

Whether left and right change the value without editing first. Empty to decide from the layout: off in a row with other focusable widgets, on otherwise.

Definition at line 58 of file slider.h.

◆ max

float asw::ui::Slider::max = 1.0F

Largest value.

Definition at line 43 of file slider.h.

◆ min

float asw::ui::Slider::min = 0.0F

Smallest value.

Definition at line 40 of file slider.h.

◆ on_change

std::function<void(float)> asw::ui::Slider::on_change

Callback invoked with the new value when the user changes it.

Definition at line 53 of file slider.h.

◆ snap

bool asw::ui::Slider::snap = false

Snap dragged values to step.

Definition at line 50 of file slider.h.

◆ step

float asw::ui::Slider::step = 0.1F

Change per left or right press. Dragging snaps to it when snap is set.

Definition at line 47 of file slider.h.

◆ style

std::optional<SliderStyle> asw::ui::Slider::style

Style for this slider only. Uses the theme slider style when empty.

Definition at line 77 of file slider.h.

◆ value

float asw::ui::Slider::value = 0.0F

Current value.

Definition at line 37 of file slider.h.


The documentation for this class was generated from the following files: