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

A button that toggles between checked and unchecked. More...

#include <checkbox.h>

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

Public Member Functions

const CheckboxStyle & get_checkbox_style (const Context &ctx) const
 Get the style this checkbox draws with.
 
void draw (Context &ctx) override
 Draw the checkbox.
 
void activate (Context &ctx) override
 Toggle checked, then call on_change and on_click.
 
- 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 bool on_event (Context &ctx, const UIEvent &e)
 Handle a UI event.
 
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::optional< CheckboxStyle > checkbox_style
 Style for this checkbox only. Uses the theme checkbox style when empty. The inherited Button::style is not used.
 
bool checked = false
 Whether the box is checked.
 
std::function< void(bool)> on_change
 Callback invoked when checked changes by user input.
 
asw::Texture texture_checked
 Texture shown when checked. Falls back to texture.
 
asw::Texture texture_checked_hover
 Texture shown when checked and hovered or focused. Falls back to texture_checked.
 
- 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.
 

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 toggles between checked and unchecked.

Clicking or activating it flips checked, then calls on_change and on_click. Without textures it draws a box with the text beside it, styled by CheckboxStyle. With textures, texture is the unchecked image and texture_checked the checked one, the hover textures work like Button's.

Definition at line 25 of file checkbox.h.

Member Function Documentation

◆ activate()

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

Toggle checked, then call on_change and on_click.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Button.

Definition at line 8 of file checkbox.cpp.

◆ draw()

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

Draw the checkbox.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Button.

Definition at line 17 of file checkbox.cpp.

◆ get_checkbox_style()

const CheckboxStyle & asw::ui::Checkbox::get_checkbox_style ( const Context & ctx) const
inline

Get the style this checkbox draws with.

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

Definition at line 36 of file checkbox.h.

Member Data Documentation

◆ checkbox_style

std::optional<CheckboxStyle> asw::ui::Checkbox::checkbox_style

Style for this checkbox only. Uses the theme checkbox style when empty. The inherited Button::style is not used.

Definition at line 29 of file checkbox.h.

◆ checked

bool asw::ui::Checkbox::checked = false

Whether the box is checked.

Definition at line 42 of file checkbox.h.

◆ on_change

std::function<void(bool)> asw::ui::Checkbox::on_change

Callback invoked when checked changes by user input.

Definition at line 45 of file checkbox.h.

◆ texture_checked

asw::Texture asw::ui::Checkbox::texture_checked

Texture shown when checked. Falls back to texture.

Definition at line 48 of file checkbox.h.

◆ texture_checked_hover

asw::Texture asw::ui::Checkbox::texture_checked_hover

Texture shown when checked and hovered or focused. Falls back to texture_checked.

Definition at line 52 of file checkbox.h.


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