Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ui::WidgetTree Class Reference

Widgets of a menu or a form, in groups, and which one has the focus. More...

#include <aegis2d/ui/widget_tree.hpp>

Public Member Functions

 WidgetTree ()
 Creates a tree with its root group only, and no focus.
 
WidgetId add (render::Rect bounds, WidgetId parent=root)
 Adds a widget that can take the focus.
 
WidgetId add_group (WidgetId parent=root)
 Adds a group, which never takes the focus.
 
std::size_t size () const noexcept
 Number of widgets and groups, the root included.
 
WidgetId parent (WidgetId id) const
 Group a widget or a group is in.
 
std::span< const WidgetId > children (WidgetId id) const
 Widgets and groups in a group.
 
void set_bounds (WidgetId id, render::Rect bounds)
 Moves or resizes a widget.
 
render::Rect bounds (WidgetId id) const
 Rectangle of a widget on screen, at the tree's scale.
 
void set_enabled (WidgetId id, bool enabled)
 Enables or disables a widget or a whole group.
 
bool enabled (WidgetId id) const
 Tells whether a widget can be used.
 
void set_scale (float scale, Vec2 origin={0.0F, 0.0F}) noexcept
 Scales the tree, such as with the player's text size.
 
float scale () const noexcept
 Factor given to set_scale().
 
Vec2 scaled (Vec2 point) const noexcept
 Where a point given at 100% is on screen, at the tree's scale.
 
std::optional< WidgetId > focused () const noexcept
 Widget that has the focus.
 
bool focus (WidgetId id)
 Gives the focus to a widget.
 
void clear_focus () noexcept
 Takes the focus away from every widget.
 
bool focus_next ()
 Focuses the next widget in the tree's order, the first one after the last.
 
bool focus_previous ()
 Focuses the previous widget in the tree's order, the last one before the first.
 
bool move_focus (Direction direction)
 Focuses the nearest widget in a direction.
 
std::optional< WidgetId > at (Vec2 point) const
 Widget under a point.
 
bool update (const Navigation &navigation)
 Moves the focus as the player asked during a frame.
 

Detailed Description

Widgets of a menu or a form, in groups, and which one has the focus.

A widget is a rectangle that can take the focus, such as a button; a group holds widgets and other groups, such as a panel, and never takes the focus itself. The focus goes from widget to widget in the order of the tree (each group's children in the order they were added), with Tab and Shift+Tab, wrapping around at the ends; to the nearest widget in a direction with the arrows, the D-pad or a stick; and to the widget under the mouse when it moves or clicks. Disabled widgets, and every widget of a disabled group, are skipped. Bounds are given at 100% and in logical pixels; with set_scale(), the tree follows the player's text size (render::TextSize::scale()), so a form laid out for 100% grows from its origin.

const auto name = form.add({.x = 40, .y = 40, .width = 200, .height = 24});
const auto port = form.add({.x = 40, .y = 72, .width = 200, .height = 24});
form.set_scale(text_size.scale(), {40.0F, 40.0F});
form.update(navigation.read(window.keyboard(), window.gamepad(), window.mouse(),
renderer.to_logical(window.mouse().position())));
Widgets of a menu or a form, in groups, and which one has the focus.
Definition widget_tree.hpp:72
bool update(const Navigation &navigation)
Moves the focus as the player asked during a frame.
void set_scale(float scale, Vec2 origin={0.0F, 0.0F}) noexcept
Scales the tree, such as with the player's text size.
WidgetId add(render::Rect bounds, WidgetId parent=root)
Adds a widget that can take the focus.
AEGIS2D_UI_EXPORT std::string_view name() noexcept
Name of the module, used to check that it is linked.

Constructor & Destructor Documentation

◆ WidgetTree()

aegis2d::ui::WidgetTree::WidgetTree ( )

Creates a tree with its root group only, and no focus.

Member Function Documentation

◆ add()

WidgetId aegis2d::ui::WidgetTree::add ( render::Rect  bounds,
WidgetId  parent = root 
)

Adds a widget that can take the focus.

Parameters
boundsRectangle at 100%, in logical pixels; also what the mouse points at.
parentGroup to put it in, root by default.
Returns
The identifier of the new widget.
Exceptions
std::out_of_rangeif parent does not exist.
std::invalid_argumentif parent is a widget, not a group.

◆ add_group()

WidgetId aegis2d::ui::WidgetTree::add_group ( WidgetId  parent = root)

Adds a group, which never takes the focus.

Parameters
parentGroup to put it in, root by default.
Returns
The identifier of the new group.
Exceptions
std::out_of_rangeif parent does not exist.
std::invalid_argumentif parent is a widget, not a group.

◆ at()

std::optional< WidgetId > aegis2d::ui::WidgetTree::at ( Vec2  point) const

Widget under a point.

Parameters
pointPoint in logical pixels, such as the mouse from Renderer::to_logical().
Returns
The enabled widget whose scaled bounds contain it, the last in the tree's order when several do (drawn on top); nothing over a gap.

◆ bounds()

render::Rect aegis2d::ui::WidgetTree::bounds ( WidgetId  id) const

Rectangle of a widget on screen, at the tree's scale.

Parameters
idWidget.
Returns
Its bounds scaled from the origin; an empty rectangle for a group.
Exceptions
std::out_of_rangeif id does not exist.

◆ children()

std::span< const WidgetId > aegis2d::ui::WidgetTree::children ( WidgetId  id) const

Widgets and groups in a group.

Parameters
idGroup.
Returns
Its children in the order they were added; empty for a widget.
Exceptions
std::out_of_rangeif id does not exist.

◆ clear_focus()

void aegis2d::ui::WidgetTree::clear_focus ( )
noexcept

Takes the focus away from every widget.

◆ enabled()

bool aegis2d::ui::WidgetTree::enabled ( WidgetId  id) const

Tells whether a widget can be used.

Parameters
idWidget or group.
Returns
false if it or one of its groups is disabled.
Exceptions
std::out_of_rangeif id does not exist.

◆ focus()

bool aegis2d::ui::WidgetTree::focus ( WidgetId  id)

Gives the focus to a widget.

Parameters
idWidget.
Returns
true if it has the focus now; false for a group or a disabled widget, which leave the focus where it was.
Exceptions
std::out_of_rangeif id does not exist.

◆ focus_next()

bool aegis2d::ui::WidgetTree::focus_next ( )

Focuses the next widget in the tree's order, the first one after the last.

Returns
true if the focus changed; false when no other widget can take it.

◆ focus_previous()

bool aegis2d::ui::WidgetTree::focus_previous ( )

Focuses the previous widget in the tree's order, the last one before the first.

Returns
true if the focus changed; false when no other widget can take it.

◆ focused()

std::optional< WidgetId > aegis2d::ui::WidgetTree::focused ( ) const
noexcept

Widget that has the focus.

Returns
It, or nothing before the first move or when no widget can take it.

◆ move_focus()

bool aegis2d::ui::WidgetTree::move_focus ( Direction  direction)

Focuses the nearest widget in a direction.

Among the widgets whose center is past the focused one's in direction, picks the one with the smallest distance along it plus twice the offset across it, so the widget straight ahead wins over a closer one to the side; the first in the tree's order on a tie. Without a focus, focuses the first widget.

Parameters
directionWhere to go.
Returns
true if the focus changed; false at the edge of the form.

◆ parent()

WidgetId aegis2d::ui::WidgetTree::parent ( WidgetId  id) const

Group a widget or a group is in.

Parameters
idWidget or group.
Returns
Its group; root for root itself.
Exceptions
std::out_of_rangeif id does not exist.

◆ scale()

float aegis2d::ui::WidgetTree::scale ( ) const
noexcept

Factor given to set_scale().

Returns
1 by default.

◆ scaled()

Vec2 aegis2d::ui::WidgetTree::scaled ( Vec2  point) const
noexcept

Where a point given at 100% is on screen, at the tree's scale.

Parameters
pointPoint at 100%, such as the corner of a label.
Returns
point scaled from the origin.

◆ set_bounds()

void aegis2d::ui::WidgetTree::set_bounds ( WidgetId  id,
render::Rect  bounds 
)

Moves or resizes a widget.

Parameters
idWidget.
boundsNew rectangle at 100%.
Exceptions
std::out_of_rangeif id does not exist.

◆ set_enabled()

void aegis2d::ui::WidgetTree::set_enabled ( WidgetId  id,
bool  enabled 
)

Enables or disables a widget or a whole group.

A disabled widget cannot take the focus; if it has it, the focus goes to the next widget, or nowhere when none is left.

Parameters
idWidget or group.
enabledfalse to disable.
Exceptions
std::out_of_rangeif id does not exist.

◆ set_scale()

void aegis2d::ui::WidgetTree::set_scale ( float  scale,
Vec2  origin = {0.0F, 0.0F} 
)
noexcept

Scales the tree, such as with the player's text size.

Parameters
scaleFactor for every position and size, 1 for 100%; values not above 0 or not finite are taken as 1.
originPoint that stays in place, such as the top-left corner of the form.

◆ size()

std::size_t aegis2d::ui::WidgetTree::size ( ) const
noexcept

Number of widgets and groups, the root included.

Returns
1 for a new tree.

◆ update()

bool aegis2d::ui::WidgetTree::update ( const Navigation &  navigation)

Moves the focus as the player asked during a frame.

The mouse first: the widget under the pointer takes the focus (a pointer over a gap leaves it where it is); then Shift+Tab or Tab; then a direction.

Parameters
navigationInput of the frame, from NavigationInput::read().
Returns
true if the focus changed.

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