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

Base class for all UI widgets. More...

#include <widget.h>

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

Public Member Functions

 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 measure (Context &ctx)
 Set this widget's own size, before its parent places it. Does nothing by default.
 
virtual void layout (Context &ctx)
 Lay out this widget and its children.
 
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.
 
virtual void activate (Context &ctx)
 Called when the widget is clicked, or activated with the keyboard or a controller while focused. Does nothing by default.
 
virtual void draw (Context &ctx)
 Draw this widget and its children.
 
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

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

void place_anchored (Widget &child) const
 Place a child that has an anchor inside this widget.
 

Protected Attributes

bool _hovered = false
 
bool _pressed = false
 
bool _focused = false
 
bool _captured = false
 

Private Member Functions

void adopt_children ()
 
void detach_children ()
 

Static Private Member Functions

static int generate_id ()
 

Private Attributes

std::vector< std::unique_ptr< Widget > > _children
 
std::vector< std::unique_ptr< Widget > > _removed
 
WidgetId _id
 

Static Private Attributes

static int _id_counter { 1 }
 

Friends

class Root
 
class FocusManager
 

Detailed Description

Base class for all UI widgets.

Definition at line 43 of file widget.h.

Constructor & Destructor Documentation

◆ Widget() [1/3]

asw::ui::Widget::Widget ( )
inline

Default constructor for Widget.

Definition at line 47 of file widget.h.

◆ ~Widget()

virtual asw::ui::Widget::~Widget ( )
virtualdefault

Default virtual destructor.

◆ Widget() [2/3]

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

Definition at line 8 of file widget.cpp.

◆ Widget() [3/3]

asw::ui::Widget::Widget ( const Widget & )
delete

Member Function Documentation

◆ activate()

void asw::ui::Widget::activate ( Context & ctx)
virtual

Called when the widget is clicked, or activated with the keyboard or a controller while focused. Does nothing by default.

Parameters
ctxThe UI context.

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

Definition at line 153 of file widget.cpp.

◆ add_child()

template<class T , class... Args>
T & asw::ui::Widget::add_child ( Args &&... args)
inline

Add a child widget.

Template Parameters
TThe type of widget to add. Must derive from Widget.
ArgsConstructor argument types.
Parameters
argsConstructor arguments forwarded to T.
Returns
A reference to the newly added child widget.

Definition at line 168 of file widget.h.

◆ adopt_children()

void asw::ui::Widget::adopt_children ( )
private

Definition at line 52 of file widget.cpp.

◆ children()

const std::vector< std::unique_ptr< Widget > > & asw::ui::Widget::children ( ) const
inline

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.

Returns
The child widgets.

Definition at line 115 of file widget.h.

◆ clear_children()

void asw::ui::Widget::clear_children ( )

Remove every child widget, destroyed as for remove_child.

Definition at line 185 of file widget.cpp.

◆ detach_children()

void asw::ui::Widget::detach_children ( )
private

Definition at line 59 of file widget.cpp.

◆ draw()

void asw::ui::Widget::draw ( Context & ctx)
virtual

Draw this widget and its children.

Parameters
ctxThe UI context.

Reimplemented in asw::ui::Button, asw::ui::Checkbox, asw::ui::Choice, asw::ui::Image, asw::ui::InputBox, asw::ui::Label, asw::ui::Modal, asw::ui::Panel, and asw::ui::Slider.

Definition at line 163 of file widget.cpp.

◆ generate_id()

static int asw::ui::Widget::generate_id ( )
inlinestaticprivate

Definition at line 256 of file widget.h.

◆ id()

WidgetId asw::ui::Widget::id ( ) const
inline

Get the unique identifier for this widget.

Returns
The widget's unique identifier.

Definition at line 75 of file widget.h.

◆ is_captured()

bool asw::ui::Widget::is_captured ( ) const
inline

Whether this widget was pressed and the pointer is still held, even if it has moved off the widget.

Definition at line 214 of file widget.h.

◆ is_focused()

bool asw::ui::Widget::is_focused ( ) const
inline

Whether this widget currently holds focus.

Definition at line 210 of file widget.h.

◆ is_highlighted()

bool asw::ui::Widget::is_highlighted ( const Context & ctx) const

Whether to draw the hover look: hovered, pressed, or focused while focus is shown.

Parameters
ctxThe UI context.
Returns
True if the hover look should be drawn.

Definition at line 158 of file widget.cpp.

◆ is_hovered()

bool asw::ui::Widget::is_hovered ( ) const
inline

Whether the pointer is currently over this widget.

Definition at line 204 of file widget.h.

◆ is_pressed()

bool asw::ui::Widget::is_pressed ( ) const
inline

Whether this widget is currently being pressed.

Definition at line 207 of file widget.h.

◆ layout()

void asw::ui::Widget::layout ( Context & ctx)
virtual

Lay out this widget and its children.

Parameters
ctxThe UI context.

Reimplemented in asw::ui::Button, asw::ui::Grid, and asw::ui::Stack.

Definition at line 73 of file widget.cpp.

◆ measure()

void asw::ui::Widget::measure ( Context & ctx)
virtual

Set this widget's own size, before its parent places it. Does nothing by default.

Parameters
ctxThe UI context.

Reimplemented in asw::ui::Button, asw::ui::Label, and asw::ui::Modal.

Definition at line 68 of file widget.cpp.

◆ on_event()

bool asw::ui::Widget::on_event ( Context & ctx,
const UIEvent & e )
virtual

Handle a UI event.

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

Reimplemented in asw::ui::Choice, asw::ui::InputBox, asw::ui::Modal, and asw::ui::Slider.

Definition at line 140 of file widget.cpp.

◆ on_focus_changed()

void asw::ui::Widget::on_focus_changed ( Context & ctx,
bool focused )
virtual

Called when focus state changes.

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

Reimplemented in asw::ui::InputBox, and asw::ui::Slider.

Definition at line 147 of file widget.cpp.

◆ operator=() [1/2]

Widget & asw::ui::Widget::operator= ( const Widget & )
delete

◆ operator=() [2/2]

asw::ui::Widget & asw::ui::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.

Definition at line 26 of file widget.cpp.

◆ place_anchored()

void asw::ui::Widget::place_anchored ( Widget & child) const
protected

Place a child that has an anchor inside this widget.

Parameters
childThe child to place.

Definition at line 84 of file widget.cpp.

◆ remove_child()

bool asw::ui::Widget::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.

Parameters
childThe child to remove.
Returns
True if it was a child of this widget.

Definition at line 172 of file widget.cpp.

Friends And Related Symbol Documentation

◆ FocusManager

friend class FocusManager
friend

Definition at line 227 of file widget.h.

◆ Root

friend class Root
friend

Definition at line 226 of file widget.h.

Member Data Documentation

◆ _captured

bool asw::ui::Widget::_captured = false
protected

Definition at line 238 of file widget.h.

◆ _children

std::vector<std::unique_ptr<Widget> > asw::ui::Widget::_children
private

Definition at line 249 of file widget.h.

◆ _focused

bool asw::ui::Widget::_focused = false
protected

Definition at line 237 of file widget.h.

◆ _hovered

bool asw::ui::Widget::_hovered = false
protected

Definition at line 235 of file widget.h.

◆ _id

WidgetId asw::ui::Widget::_id
private

Definition at line 261 of file widget.h.

◆ _id_counter

int asw::ui::Widget::_id_counter { 1 }
inlinestaticprivate

Definition at line 254 of file widget.h.

◆ _pressed

bool asw::ui::Widget::_pressed = false
protected

Definition at line 236 of file widget.h.

◆ _removed

std::vector<std::unique_ptr<Widget> > asw::ui::Widget::_removed
private

Definition at line 252 of file widget.h.

◆ anchor

Anchor asw::ui::Widget::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.

Definition at line 198 of file widget.h.

◆ anchor_margin

asw::Vec2<float> asw::ui::Widget::anchor_margin { 0.0F, 0.0F }

Space between an anchored widget and the parent's edges.

Definition at line 201 of file widget.h.

◆ enabled

bool asw::ui::Widget::enabled = true

Whether the widget is enabled.

Definition at line 84 of file widget.h.

◆ focus_ring

bool asw::ui::Widget::focus_ring = true

Draw the theme focus ring around this widget while it has visible focus. Turn off for widgets that show focus themselves.

Definition at line 104 of file widget.h.

◆ focusable

bool asw::ui::Widget::focusable = false

Whether the widget can receive focus. Focusable widgets are also the ones the pointer can press.

Definition at line 88 of file widget.h.

◆ nav_down

Widget* asw::ui::Widget::nav_down = nullptr

Widget to focus on down, instead of the nearest one. Optional.

Definition at line 94 of file widget.h.

◆ nav_left

Widget* asw::ui::Widget::nav_left = nullptr

Widget to focus on left, instead of the nearest one. Optional.

Definition at line 97 of file widget.h.

◆ nav_right

Widget* asw::ui::Widget::nav_right = nullptr

Widget to focus on right, instead of the nearest one. Optional.

Definition at line 100 of file widget.h.

◆ nav_up

Widget* asw::ui::Widget::nav_up = nullptr

Widget to focus on up, instead of the nearest one. Optional.

Definition at line 91 of file widget.h.

◆ parent

Widget* asw::ui::Widget::parent = nullptr

Pointer to the parent widget.

Definition at line 107 of file widget.h.

◆ transform

asw::Quad<float> asw::ui::Widget::transform

The transform (position and size) of the widget.

Definition at line 192 of file widget.h.

◆ visible

bool asw::ui::Widget::visible = true

Whether the widget is visible.

Definition at line 81 of file widget.h.


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