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

Buttons and labels of a menu, on a WidgetTree. More...

#include <aegis2d/ui/menu.hpp>

Public Member Functions

 Menu (const MenuStyle &style={})
 Creates an empty menu.
 
WidgetId add_button (std::string text, render::Rect bounds, WidgetId group=root)
 Adds a button.
 
std::size_t add_label (std::string text, Vec2 position)
 Adds a label, a text that never takes the focus.
 
void set_text (WidgetId button, std::string text)
 Changes the text of a button.
 
void set_label (std::size_t label, std::string text)
 Changes the text of a label.
 
const std::string & text (WidgetId button) const
 Text of a button.
 
WidgetTree & tree () noexcept
 The tree of the buttons: focus, enabled buttons, scale.
 
const WidgetTree & tree () const noexcept
 The tree of the buttons.
 
std::optional< WidgetId > update (const Navigation &navigation)
 Moves the focus and tells which button was used during the frame.
 
WidgetState state (WidgetId button) const
 How a button looks now.
 
void draw (render::IRenderer &renderer, const render::BitmapFont &font) const
 Draws the buttons and the labels at the tree's scale.
 

Detailed Description

Buttons and labels of a menu, on a WidgetTree.

Buttons are widgets of the tree, with a text centered in them; labels are texts that never take the focus. update() moves the focus and tells which button the player used: the focused one when activate is pressed (Enter, Space, South), or the one clicked. draw() shows each button in its state, and follows the tree's scale, so the menu grows with the player's text size.

ui::Menu menu;
const auto start = menu.add_button("Start", {.x = 540, .y = 300, .width = 200, .height = 40});
const auto quit = menu.add_button("Quit", {.x = 540, .y = 350, .width = 200, .height = 40});
// Each frame:
if (menu.update(navigation.read(...)) == quit) { window.request_close(); }
menu.draw(renderer, font);
Buttons and labels of a menu, on a WidgetTree.
Definition menu.hpp:73
std::optional< WidgetId > update(const Navigation &navigation)
Moves the focus and tells which button was used during the frame.
void draw(render::IRenderer &renderer, const render::BitmapFont &font) const
Draws the buttons and the labels at the tree's scale.
WidgetId add_button(std::string text, render::Rect bounds, WidgetId group=root)
Adds a button.

Constructor & Destructor Documentation

◆ Menu()

aegis2d::ui::Menu::Menu ( const MenuStyle &  style = {})
explicit

Creates an empty menu.

Parameters
styleColors, text size and layer.

Member Function Documentation

◆ add_button()

WidgetId aegis2d::ui::Menu::add_button ( std::string  text,
render::Rect  bounds,
WidgetId  group = root 
)

Adds a button.

Parameters
textText shown centered in it.
boundsRectangle at 100%, in logical pixels.
groupGroup of the tree to put it in, root by default.
Returns
The button's widget.
Exceptions
asWidgetTree::add().

◆ add_label()

std::size_t aegis2d::ui::Menu::add_label ( std::string  text,
Vec2  position 
)

Adds a label, a text that never takes the focus.

Parameters
textText, lines separated by \n.
positionTop-left corner at 100%, in logical pixels.
Returns
The index of the label, for set_label().

◆ draw()

void aegis2d::ui::Menu::draw ( render::IRenderer &  renderer,
const render::BitmapFont &  font 
) const

Draws the buttons and the labels at the tree's scale.

Parameters
rendererRenderer in a frame.
fontFont of the texts.

◆ set_label()

void aegis2d::ui::Menu::set_label ( std::size_t  label,
std::string  text 
)

Changes the text of a label.

Parameters
labelIndex returned by add_label().
textNew text.
Exceptions
std::out_of_rangeif label does not exist.

◆ set_text()

void aegis2d::ui::Menu::set_text ( WidgetId  button,
std::string  text 
)

Changes the text of a button.

Parameters
buttonWidget returned by add_button().
textNew text.
Exceptions
std::out_of_rangeif button is not a button of this menu.

◆ state()

WidgetState aegis2d::ui::Menu::state ( WidgetId  button) const

How a button looks now.

Parameters
buttonWidget returned by add_button().
Returns
Disabled, Pressed (focused and held since the last update()), Focused or Normal.
Exceptions
std::out_of_rangeif button does not exist.

◆ text()

const std::string & aegis2d::ui::Menu::text ( WidgetId  button) const

Text of a button.

Parameters
buttonWidget returned by add_button().
Returns
Its text.
Exceptions
std::out_of_rangeif button is not a button of this menu.

◆ tree() [1/2]

const WidgetTree & aegis2d::ui::Menu::tree ( ) const
noexcept

The tree of the buttons.

Returns
The tree.

◆ tree() [2/2]

WidgetTree & aegis2d::ui::Menu::tree ( )
noexcept

The tree of the buttons: focus, enabled buttons, scale.

Returns
The tree.

◆ update()

std::optional< WidgetId > aegis2d::ui::Menu::update ( const Navigation &  navigation)

Moves the focus and tells which button was used during the frame.

Parameters
navigationInput of the frame, from NavigationInput::read().
Returns
The button activated or clicked, if any; a disabled button never is.

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