Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ui::Slider Class Reference

A value picked between two bounds, as a widget of a WidgetTree. More...

#include <aegis2d/ui/widgets.hpp>

Public Member Functions

 Slider (WidgetTree &tree, render::Rect bounds, const SliderConfig &config, WidgetId group=root)
 Adds the slider to a tree.
 
WidgetId id () const noexcept
 The slider's widget in its tree.
 
const SliderConfig & config () const noexcept
 Range, step and starting value.
 
float value () const noexcept
 Value now.
 
void set_value (float value) noexcept
 Moves the value.
 
bool update (Navigation &navigation, const WidgetTree &tree)
 Applies the frame's input to the value.
 
void draw (render::IRenderer &renderer, const WidgetTree &tree, const SliderStyle &style={}) const
 Draws the track, its filled part and the handle at the tree's scale.
 

Detailed Description

A value picked between two bounds, as a widget of a WidgetTree.

The slider is a widget of the tree, so it takes the focus like a button. While it is focused, Left and Right move the value by one step (the move is consumed, so the focus stays); the mouse sets the value by pressing or dragging over it. Bounds are given at 100%, and drawing follows the tree's scale.

ui::Slider music{menu.tree(), {.x = 40, .y = 100, .width = 200, .height = 24},
{.min = 0, .max = 100, .step = 10, .value = 80}};
// Each frame, before menu.update():
if (music.update(navigation, menu.tree())) { mixer.set_volume(music.value() / 100); }
A value picked between two bounds, as a widget of a WidgetTree.
Definition widgets.hpp:65
float value() const noexcept
Value now.

Constructor & Destructor Documentation

◆ Slider()

aegis2d::ui::Slider::Slider ( WidgetTree &  tree,
render::Rect  bounds,
const SliderConfig &  config,
WidgetId  group = root 
)

Adds the slider to a tree.

Parameters
treeTree it lives in; the slider keeps only the widget's identifier.
boundsRectangle at 100%, in logical pixels.
configRange, step and starting value.
groupGroup of the tree to put it in, root by default.
Exceptions
std::invalid_argumentif min is not below max, or the step is not above 0, or a bound is not finite; as WidgetTree::add() for group.

Member Function Documentation

◆ config()

const SliderConfig & aegis2d::ui::Slider::config ( ) const
noexcept

Range, step and starting value.

Returns
The configuration given to the constructor.

◆ draw()

void aegis2d::ui::Slider::draw ( render::IRenderer &  renderer,
const WidgetTree &  tree,
const SliderStyle &  style = {} 
) const

Draws the track, its filled part and the handle at the tree's scale.

Parameters
rendererRenderer in a frame.
treeThe tree the slider was added to.
styleColors and layer.

◆ id()

WidgetId aegis2d::ui::Slider::id ( ) const
noexcept

The slider's widget in its tree.

Returns
The identifier given by the tree.

◆ set_value()

void aegis2d::ui::Slider::set_value ( float  value)
noexcept

Moves the value.

Parameters
valueNew value, clamped between min and max; NaN is ignored.

◆ update()

bool aegis2d::ui::Slider::update ( Navigation &  navigation,
const WidgetTree &  tree 
)

Applies the frame's input to the value.

While the slider is focused, Left or Right moves the value by one step and is consumed (taken out of navigation), so the focus stays on the slider; at an end the move is still consumed. The mouse sets the value from where it presses or drags over the slider. A disabled slider changes nothing.

Parameters
navigationInput of the frame; its direction is consumed when used.
treeThe tree the slider was added to.
Returns
true if the value changed.

◆ value()

float aegis2d::ui::Slider::value ( ) const
noexcept

Value now.

Returns
Between min and max.

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