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

A box over the rest of the UI that has to be dealt with first, e.g. a question or a login code. More...

#include <modal.h>

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

Public Member Functions

 Modal ()
 Default constructor.
 
const ModalStyle & get_style (const Context &ctx) const
 Get the style this modal draws with.
 
Label & add_text (const std::string &text, const asw::Font &font=nullptr)
 Add a line of text, sized to fit.
 
Button & add_button (const std::string &text, std::function< void()> on_click)
 Add a button, sized to its text.
 
void close ()
 Close the modal. Calls on_close, then takes it off the screen. Safe to call from its own buttons or from on_close.
 
bool closed () const
 True once closed.
 
void measure (Context &ctx) override
 Size to the children, at least the style's min_width.
 
bool on_event (Context &ctx, const UIEvent &e) override
 Close on back.
 
void draw (Context &ctx) override
 Draw the box, then the children.
 
- Public Member Functions inherited from asw::ui::Stack
void layout (Context &ctx) override
 Place 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 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.
 
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_close
 Called once when the modal closes, from close() or back.
 
bool close_on_back = true
 Close back when pressed. Turn off for a modal the player must answer with one of its buttons.
 
std::optional< ModalStyle > style
 Style for this modal only. Uses the theme modal style when empty.
 
- Public Attributes inherited from asw::ui::Stack
Direction direction = Direction::Vertical
 Direction children are placed in.
 
Align align = Align::Stretch
 Where children sit across the stack.
 
float gap = 8.0F
 Space between children.
 
float padding = 0.0F
 Space inside the stack on every side.
 
- 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.
 

Private Attributes

bool _closed = false
 

Additional Inherited Members

- 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 box over the rest of the UI that has to be dealt with first, e.g. a question or a login code.

Open one with Root::open_modal. While it is open the screen behind is dimmed, the pointer and focus only reach the modal, and back closes it. Its children stack top to bottom, centred, and it sizes itself to them.

Definition at line 31 of file modal.h.

Constructor & Destructor Documentation

◆ Modal()

asw::ui::Modal::Modal ( )

Default constructor.

Definition at line 8 of file modal.cpp.

Member Function Documentation

◆ add_button()

asw::ui::Button & asw::ui::Modal::add_button ( const std::string & text,
std::function< void()> on_click )

Add a button, sized to its text.

Parameters
textThe button text.
on_clickCalled when it is clicked.
Returns
The button.

Definition at line 28 of file modal.cpp.

◆ add_text()

asw::ui::Label & asw::ui::Modal::add_text ( const std::string & text,
const asw::Font & font = nullptr )

Add a line of text, sized to fit.

Parameters
textThe text.
fontThe font, the theme font when empty.
Returns
The label, to change its colour or justify.

Definition at line 19 of file modal.cpp.

◆ close()

void asw::ui::Modal::close ( )

Close the modal. Calls on_close, then takes it off the screen. Safe to call from its own buttons or from on_close.

Definition at line 37 of file modal.cpp.

◆ closed()

bool asw::ui::Modal::closed ( ) const
inline

True once closed.

Returns
true - If close() was called.

Definition at line 79 of file modal.h.

◆ draw()

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

Draw the box, then the children.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 88 of file modal.cpp.

◆ get_style()

const asw::ui::ModalStyle & asw::ui::Modal::get_style ( const Context & ctx) const

Get the style this modal draws with.

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

Definition at line 14 of file modal.cpp.

◆ measure()

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

Size to the children, at least the style's min_width.

Parameters
ctxThe UI context.

Reimplemented from asw::ui::Widget.

Definition at line 54 of file modal.cpp.

◆ on_event()

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

Close on back.

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

Reimplemented from asw::ui::Widget.

Definition at line 79 of file modal.cpp.

Member Data Documentation

◆ _closed

bool asw::ui::Modal::_closed = false
private

Definition at line 105 of file modal.h.

◆ close_on_back

bool asw::ui::Modal::close_on_back = true

Close back when pressed. Turn off for a modal the player must answer with one of its buttons.

Definition at line 42 of file modal.h.

◆ on_close

std::function<void()> asw::ui::Modal::on_close

Called once when the modal closes, from close() or back.

Definition at line 38 of file modal.h.

◆ style

std::optional<ModalStyle> asw::ui::Modal::style

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

Definition at line 45 of file modal.h.


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