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::Choice Class Reference

A button that cycles through a list of options, e.g. difficulty or window mode. More...

#include <choice.h>

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

Public Member Functions

bool adjusts_left_right () const
 Whether left and right change the option right now.
 
std::size_t count () const
 Number of options, the larger of options and images.
 
void select (std::size_t i)
 Select an option by index without calling on_change.
 
void next ()
 Move to the next option, calls on_change.
 
void prev ()
 Move to the previous option, calls on_change.
 
void activate (Context &ctx) override
 Move to the next option, then call on_click.
 
bool on_event (Context &ctx, const UIEvent &e) override
 Handle left and right while focused.
 
void draw (Context &ctx) override
 Draw the selected option.
 
- Public Member Functions inherited from asw::ui::Button
 Button ()
 Default constructor.
 
const ButtonStyle & get_style (const Context &ctx) const
 Get the style this button draws with.
 
void set_texture (const asw::Texture &tex, bool auto_size=false)
 Set the texture, optionally resizing the button to match.
 
void set_images (const asw::Texture &normal, const asw::Texture &hover=nullptr, const asw::Texture &pressed=nullptr, const asw::Texture &disabled=nullptr, bool auto_size=true)
 Make an image button: set the state textures, turn off the background, and optionally size the button to the normal texture.
 
void set_text (const std::string &t, bool auto_size=false)
 Set the text, optionally resizing the button to match.
 
void measure (Context &ctx) override
 Size to the text if set_text asked for it.
 
void layout (Context &ctx) override
 Measure, in case the parent did not, then lay out the children.
 
- 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 on_focus_changed (Context &ctx, bool focused)
 Called when focus state changes.
 
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

std::vector< std::string > options
 Option labels.
 
std::vector< asw::Texture > images
 Option textures, one per option. Optional.
 
std::size_t index = 0
 Index of the selected option.
 
bool wrap = true
 Wrap around at either end.
 
std::optional< bool > adjust_on_left_right
 Whether left and right change the option. Empty to decide from the layout: off in a row with other focusable widgets, on otherwise.
 
bool show_arrows = true
 Show < and > at the sides while highlighted, when there is text and left and right change the option.
 
std::function< void(std::size_t)> on_change
 Callback invoked with the new index when the user changes it.
 
- Public Attributes inherited from asw::ui::Button
std::function< void()> on_click
 Callback invoked when the button is clicked.
 
float padding = 0.0f
 Padding applied inside the button on all sides.
 
asw::Font font
 The font for the text. Uses the theme font when empty.
 
std::string text
 The button text.
 
asw::Texture texture
 The texture to display on the button.
 
asw::Texture texture_hover
 Texture shown while hovered or focused. Falls back to texture.
 
asw::Texture texture_pressed
 Texture shown while pressed. Falls back to texture_hover, then texture.
 
asw::Texture texture_disabled
 Texture shown while disabled. Falls back to texture.
 
bool draw_background = true
 Fill the button with the theme background. Turn off for buttons that are only an image.
 
std::optional< ButtonStyle > style
 Style for this button only. Uses the theme button 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 step (int dir)
 

Additional Inherited Members

- Protected Member Functions inherited from asw::ui::Button
const asw::Texture & current_texture (const Context &ctx) const
 Get the texture for the current state.
 
- 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 button that cycles through a list of options, e.g. difficulty or window mode.

Clicking or activating moves to the next option. While focused, left and right move back and forward, unless the choice sits in a row with other focusable widgets, where they move focus instead (see adjust_on_left_right). Each option shows its label from options, its texture from images, or both. The rest of the look is the Button style.

Definition at line 32 of file choice.h.

Member Function Documentation

◆ activate()

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

Move to the next option, then call on_click.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Button.

Definition at line 68 of file choice.cpp.

◆ adjusts_left_right()

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

Whether left and right change the option right now.

Returns
adjust_on_left_right, or the layout default.

Definition at line 14 of file choice.cpp.

◆ count()

std::size_t asw::ui::Choice::count ( ) const

Number of options, the larger of options and images.

Returns
The option count.

Definition at line 9 of file choice.cpp.

◆ draw()

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

Draw the selected option.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Button.

Definition at line 91 of file choice.cpp.

◆ next()

void asw::ui::Choice::next ( )

Move to the next option, calls on_change.

Definition at line 58 of file choice.cpp.

◆ on_event()

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

Handle 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 74 of file choice.cpp.

◆ prev()

void asw::ui::Choice::prev ( )

Move to the previous option, calls on_change.

Definition at line 63 of file choice.cpp.

◆ select()

void asw::ui::Choice::select ( std::size_t i)

Select an option by index without calling on_change.

Parameters
iThe index, clamped to the options.

Definition at line 19 of file choice.cpp.

◆ step()

void asw::ui::Choice::step ( int dir)
private

Definition at line 25 of file choice.cpp.

Member Data Documentation

◆ adjust_on_left_right

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

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

Definition at line 48 of file choice.h.

◆ images

std::vector<asw::Texture> asw::ui::Choice::images

Option textures, one per option. Optional.

Definition at line 38 of file choice.h.

◆ index

std::size_t asw::ui::Choice::index = 0

Index of the selected option.

Definition at line 41 of file choice.h.

◆ on_change

std::function<void(std::size_t)> asw::ui::Choice::on_change

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

Definition at line 61 of file choice.h.

◆ options

std::vector<std::string> asw::ui::Choice::options

Option labels.

Definition at line 35 of file choice.h.

◆ show_arrows

bool asw::ui::Choice::show_arrows = true

Show < and > at the sides while highlighted, when there is text and left and right change the option.

Definition at line 58 of file choice.h.

◆ wrap

bool asw::ui::Choice::wrap = true

Wrap around at either end.

Definition at line 44 of file choice.h.


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