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

Root container that manages the UI tree, input, and rendering. More...

#include <root.h>

Collaboration diagram for asw::ui::Root:
[legend]

Classes

struct  ShownToast
 

Public Member Functions

 Root ()
 Default constructor.
 
void set_size (float w, float h)
 Set a fixed size for the root panel.
 
bool update ()
 Process input for this frame.
 
void draw ()
 Draw the UI tree, then the focus ring.
 
void focus (Widget &w, bool show=false)
 Focus a widget.
 
void clear_focus ()
 Remove focus from every widget.
 
void toast (const std::string &text)
 Show a message at the top of the screen for a few seconds, e.g. "Achievement unlocked". Messages queue when several arrive together.
 
void toast (Toast message)
 Show a message with an icon, colour or length of its own.
 
void clear_toasts ()
 Remove every message, shown or waiting.
 
std::size_t toast_count () const
 Messages shown or waiting.
 
template<class T = Modal, class... Args>
T & open_modal (Args &&... args)
 Open a modal over the rest of the UI. Its first focusable widget takes focus, and focus goes back where it was once every modal closes.
 
bool has_modal () const
 True while a modal is open.
 
void close_modals ()
 Close every open modal, top first.
 

Public Attributes

Context ctx
 The UI context.
 
Panel root
 The root panel widget. See through by default, set bg for a background.
 
std::function< void()> on_back
 Called when back is pressed and the focused widget does not handle it, e.g. to leave a menu.
 
bool auto_size = true
 Keep the root panel the size of the logical screen. Turned off by set_size.
 

Private Member Functions

void fit_to_screen ()
 
bool attached (const Widget *w) const
 
bool in_root (const Widget *w) const
 
void validate ()
 
void free_removed (Widget &w)
 
Widget * hit_test (Widget &w, const asw::Vec2< float > &pointer_pos)
 
bool bubble (Widget *target, const UIEvent &e)
 
bool dispatch_pointer (const UIEvent &e)
 
bool dispatch_to_focused (const UIEvent &e)
 
void update_pointer ()
 
void update_keys ()
 
void activate (Widget &w)
 
Modal * top_modal () const
 
Widget & active_root ()
 
void opened_modal ()
 
void layout_modals ()
 
void update_modal_focus ()
 
void update_toasts ()
 
void draw_toasts ()
 

Private Attributes

bool _used = false
 
Panel _modals
 
Widget * _focus_before_modal = nullptr
 
Widget * _focused_modal = nullptr
 
std::deque< ShownToast > _toasts
 
std::optional< std::chrono::steady_clock::time_point > _last_update
 

Detailed Description

Root container that manages the UI tree, input, and rendering.

Call update() once per frame after asw::core::update(), then draw(). Root tracks hover, press, focus and the focus ring for every widget, and reads navigation from Context::navigation.

Definition at line 49 of file root.h.

Constructor & Destructor Documentation

◆ Root()

asw::ui::Root::Root ( )

Default constructor.

Definition at line 48 of file root.cpp.

Member Function Documentation

◆ activate()

void asw::ui::Root::activate ( Widget & w)
private

Definition at line 290 of file root.cpp.

◆ active_root()

asw::ui::Widget & asw::ui::Root::active_root ( )
private

Definition at line 105 of file root.cpp.

◆ attached()

bool asw::ui::Root::attached ( const Widget * w) const
private

Definition at line 78 of file root.cpp.

◆ bubble()

bool asw::ui::Root::bubble ( Widget * target,
const UIEvent & e )
private

Definition at line 235 of file root.cpp.

◆ clear_focus()

void asw::ui::Root::clear_focus ( )

Remove focus from every widget.

Definition at line 664 of file root.cpp.

◆ clear_toasts()

void asw::ui::Root::clear_toasts ( )

Remove every message, shown or waiting.

Definition at line 562 of file root.cpp.

◆ close_modals()

void asw::ui::Root::close_modals ( )

Close every open modal, top first.

Definition at line 129 of file root.cpp.

◆ dispatch_pointer()

bool asw::ui::Root::dispatch_pointer ( const UIEvent & e)
private

Definition at line 268 of file root.cpp.

◆ dispatch_to_focused()

bool asw::ui::Root::dispatch_to_focused ( const UIEvent & e)
private

Definition at line 281 of file root.cpp.

◆ draw()

void asw::ui::Root::draw ( )

Draw the UI tree, then the focus ring.

Definition at line 670 of file root.cpp.

◆ draw_toasts()

void asw::ui::Root::draw_toasts ( )
private

Definition at line 591 of file root.cpp.

◆ fit_to_screen()

void asw::ui::Root::fit_to_screen ( )
private

Definition at line 56 of file root.cpp.

◆ focus()

void asw::ui::Root::focus ( Widget & w,
bool show = false )

Focus a widget.

Parameters
wThe widget, it must be focusable and in this tree.
showShow the focus ring, as keyboard navigation would.

Definition at line 658 of file root.cpp.

◆ free_removed()

void asw::ui::Root::free_removed ( Widget & w)
private

Definition at line 204 of file root.cpp.

◆ has_modal()

bool asw::ui::Root::has_modal ( ) const

True while a modal is open.

Returns
true - If a modal is open.

Definition at line 113 of file root.cpp.

◆ hit_test()

asw::ui::Widget * asw::ui::Root::hit_test ( Widget & w,
const asw::Vec2< float > & pointer_pos )
private

Definition at line 212 of file root.cpp.

◆ in_root()

bool asw::ui::Root::in_root ( const Widget * w) const
private

Definition at line 87 of file root.cpp.

◆ layout_modals()

void asw::ui::Root::layout_modals ( )
private

Definition at line 137 of file root.cpp.

◆ open_modal()

template<class T = Modal, class... Args>
T & asw::ui::Root::open_modal ( Args &&... args)
inline

Open a modal over the rest of the UI. Its first focusable widget takes focus, and focus goes back where it was once every modal closes.

Template Parameters
TModal or a class derived from it.
Parameters
argsArguments for the modal's constructor.
Returns
The modal, add its content with add_text and add_button.

Definition at line 134 of file root.h.

◆ opened_modal()

void asw::ui::Root::opened_modal ( )
private

Definition at line 118 of file root.cpp.

◆ set_size()

void asw::ui::Root::set_size ( float w,
float h )

Set a fixed size for the root panel.

Parameters
wThe width.
hThe height.

Definition at line 68 of file root.cpp.

◆ toast() [1/2]

void asw::ui::Root::toast ( const std::string & text)

Show a message at the top of the screen for a few seconds, e.g. "Achievement unlocked". Messages queue when several arrive together.

Parameters
textThe message.

Definition at line 552 of file root.cpp.

◆ toast() [2/2]

void asw::ui::Root::toast ( Toast message)

Show a message with an icon, colour or length of its own.

Parameters
messageThe message.

Definition at line 557 of file root.cpp.

◆ toast_count()

std::size_t asw::ui::Root::toast_count ( ) const
inline

Messages shown or waiting.

Returns
The number of messages.

Definition at line 121 of file root.h.

◆ top_modal()

asw::ui::Modal * asw::ui::Root::top_modal ( ) const
private

Definition at line 95 of file root.cpp.

◆ update()

bool asw::ui::Root::update ( )

Process input for this frame.

Returns
True if the UI used input this frame: the pointer is over a widget or pressing one, or a navigation, activate, back or text key went to the UI. Games can skip their own input for the frame.

Definition at line 521 of file root.cpp.

◆ update_keys()

void asw::ui::Root::update_keys ( )
private

Definition at line 405 of file root.cpp.

◆ update_modal_focus()

void asw::ui::Root::update_modal_focus ( )
private

Definition at line 151 of file root.cpp.

◆ update_pointer()

void asw::ui::Root::update_pointer ( )
private

Definition at line 302 of file root.cpp.

◆ update_toasts()

void asw::ui::Root::update_toasts ( )
private

Definition at line 567 of file root.cpp.

◆ validate()

void asw::ui::Root::validate ( )
private

Definition at line 177 of file root.cpp.

Member Data Documentation

◆ _focus_before_modal

Widget* asw::ui::Root::_focus_before_modal = nullptr
private

Definition at line 186 of file root.h.

◆ _focused_modal

Widget* asw::ui::Root::_focused_modal = nullptr
private

Definition at line 187 of file root.h.

◆ _last_update

std::optional<std::chrono::steady_clock::time_point> asw::ui::Root::_last_update
private

Definition at line 194 of file root.h.

◆ _modals

Panel asw::ui::Root::_modals
private

Definition at line 182 of file root.h.

◆ _toasts

std::deque<ShownToast> asw::ui::Root::_toasts
private

Definition at line 193 of file root.h.

◆ _used

bool asw::ui::Root::_used = false
private

Definition at line 179 of file root.h.

◆ auto_size

bool asw::ui::Root::auto_size = true

Keep the root panel the size of the logical screen. Turned off by set_size.

Definition at line 68 of file root.h.

◆ ctx

Context asw::ui::Root::ctx

The UI context.

Definition at line 56 of file root.h.

◆ on_back

std::function<void()> asw::ui::Root::on_back

Called when back is pressed and the focused widget does not handle it, e.g. to leave a menu.

Definition at line 64 of file root.h.

◆ root

Panel asw::ui::Root::root

The root panel widget. See through by default, set bg for a background.

Definition at line 60 of file root.h.


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