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 on_focus_changed (Context &ctx, bool focused) override
 Called when focus state changes.
 
bool on_event (Context &ctx, const UIEvent &e) override
 Handle a UI event.
 
void draw (Context &ctx) override
 Draw the button.
 
void set_texture (const asw::Texture &tex, bool auto_size=false)
 Set the texture, optionally resizing the button to match.
 
void set_text (const std::string &t, bool auto_size=false)
 Set the text, optionally resizing the button to match.
 
- Public Member Functions inherited from asw::ui::Widget
 Widget ()
 Default constructor for Widget.
 
virtual ~Widget ()=default
 Default virtual destructor.
 
 Widget (Widget &&)=default
 Move constructor.
 
Widgetoperator= (Widget &&)=default
 Move assignment operator.
 
 Widget (const Widget &)=delete
 
Widgetoperator= (const Widget &)=delete
 
WidgetId id () const
 Get the unique identifier for this widget.
 
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 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.
 

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 to use for the button text.
 
std::string text
 The button text.
 
asw::Texture texture
 The texture to display on the button.
 
- 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.
 
Widgetparent = nullptr
 Pointer to the parent widget.
 
std::vector< std::unique_ptr< Widget > > children
 Child widgets.
 
asw::Quad< float > transform
 The transform (position and size) of the widget.
 

Additional Inherited Members

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

Detailed Description

An interactive button widget.

Definition at line 23 of file button.h.

Constructor & Destructor Documentation

◆ Button()

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

Default constructor.

Definition at line 27 of file button.h.

Member Function Documentation

◆ draw()

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

Draw the button.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 87 of file button.cpp.

◆ on_event()

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

Handle a UI event.

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

Reimplemented from asw::ui::Widget.

Definition at line 12 of file button.cpp.

◆ on_focus_changed()

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

Called when focus state changes.

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

Reimplemented from asw::ui::Widget.

Definition at line 6 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.

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

Definition at line 76 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 67 of file button.cpp.

Member Data Documentation

◆ font

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

The font to use for the button text.

Definition at line 60 of file button.h.

◆ on_click

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

Callback invoked when the button is clicked.

Definition at line 33 of file button.h.

◆ padding

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

Padding applied inside the button on all sides.

Definition at line 57 of file button.h.

◆ text

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

The button text.

Definition at line 63 of file button.h.

◆ texture

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

The texture to display on the button.

Definition at line 66 of file button.h.


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