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

An interactive button widget. More...

#include <button.h>

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

Public Member Functions

 Button ()
 Default constructor.
 
void activate (Context &ctx) override
 Calls on_click.
 
void draw (Context &ctx) override
 Draw the button.
 
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::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.
 

Protected Member Functions

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.
 

Private Member Functions

void fit_text (const asw::Font &f)
 

Private Attributes

bool _fit_text = false
 

Additional Inherited Members

- Protected Attributes inherited from asw::ui::Widget
bool _hovered = false
 
bool _pressed = false
 
bool _focused = false
 
bool _captured = false
 

Detailed Description

An interactive button widget.

Definition at line 24 of file button.h.

Constructor & Destructor Documentation

◆ Button()

asw::ui::Button::Button ( )
inline

Default constructor.

Definition at line 28 of file button.h.

Member Function Documentation

◆ activate()

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

Calls on_click.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Reimplemented in asw::ui::Checkbox, and asw::ui::Choice.

Definition at line 6 of file button.cpp.

◆ current_texture()

const asw::Texture & asw::ui::Button::current_texture ( const Context & ctx) const
protected

Get the texture for the current state.

Parameters
ctxThe UI context.
Returns
The texture to draw, may be nullptr.

Definition at line 14 of file button.cpp.

◆ draw()

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

Draw the button.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Reimplemented in asw::ui::Checkbox, and asw::ui::Choice.

Definition at line 89 of file button.cpp.

◆ fit_text()

void asw::ui::Button::fit_text ( const asw::Font & f)
private

Definition at line 63 of file button.cpp.

◆ get_style()

const ButtonStyle & asw::ui::Button::get_style ( const Context & ctx) const
inline

Get the style this button draws with.

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

Definition at line 81 of file button.h.

◆ layout()

void asw::ui::Button::layout ( Context & ctx)
overridevirtual

Measure, in case the parent did not, then lay out the children.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 81 of file button.cpp.

◆ measure()

void asw::ui::Button::measure ( Context & ctx)
overridevirtual

Size to the text if set_text asked for it.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 71 of file button.cpp.

◆ set_images()

void asw::ui::Button::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.

Parameters
normalTexture when idle.
hoverTexture while hovered or focused, optional.
pressedTexture while pressed, optional.
disabledTexture while disabled, optional.
auto_sizeIf true, resizes the button to the normal texture.

Definition at line 37 of file button.cpp.

◆ set_text()

void asw::ui::Button::set_text ( const std::string & t,
bool auto_size = false )

Set the text, optionally resizing the button to match.

Without its own font the button is sized with the theme font when it is next measured, before its parent places it.

Parameters
tThe text to set.
auto_sizeIf true, resizes the button to fit the text.

Definition at line 47 of file button.cpp.

◆ set_texture()

void asw::ui::Button::set_texture ( const asw::Texture & tex,
bool auto_size = false )

Set the texture, optionally resizing the button to match.

Parameters
texThe texture to set.
auto_sizeIf true, resizes the button to the texture dimensions.

Definition at line 28 of file button.cpp.

Member Data Documentation

◆ _fit_text

bool asw::ui::Button::_fit_text = false
private

Definition at line 138 of file button.h.

◆ draw_background

bool asw::ui::Button::draw_background = true

Fill the button with the theme background. Turn off for buttons that are only an image.

Definition at line 71 of file button.h.

◆ font

asw::Font asw::ui::Button::font

The font for the text. Uses the theme font when empty.

Definition at line 52 of file button.h.

◆ on_click

std::function<void()> asw::ui::Button::on_click

Callback invoked when the button is clicked.

Definition at line 34 of file button.h.

◆ padding

float asw::ui::Button::padding = 0.0f

Padding applied inside the button on all sides.

Definition at line 49 of file button.h.

◆ style

std::optional<ButtonStyle> asw::ui::Button::style

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

Definition at line 74 of file button.h.

◆ text

std::string asw::ui::Button::text

The button text.

Definition at line 55 of file button.h.

◆ texture

asw::Texture asw::ui::Button::texture

The texture to display on the button.

Definition at line 58 of file button.h.

◆ texture_disabled

asw::Texture asw::ui::Button::texture_disabled

Texture shown while disabled. Falls back to texture.

Definition at line 67 of file button.h.

◆ texture_hover

asw::Texture asw::ui::Button::texture_hover

Texture shown while hovered or focused. Falls back to texture.

Definition at line 61 of file button.h.

◆ texture_pressed

asw::Texture asw::ui::Button::texture_pressed

Texture shown while pressed. Falls back to texture_hover, then texture.

Definition at line 64 of file button.h.


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