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

A scrollable list of items, one selected, as a widget of a WidgetTree. More...

#include <aegis2d/ui/widgets.hpp>

Public Member Functions

 ListView (WidgetTree &tree, render::Rect bounds, std::vector< std::string > items, std::size_t visible, WidgetId group=root)
 Adds the list to a tree.
 
WidgetId id () const noexcept
 The list's widget in its tree.
 
std::span< const std::string > items () const noexcept
 Texts of the rows.
 
std::size_t selected () const noexcept
 Item selected now.
 
void select (std::size_t item)
 Selects an item and scrolls to show it.
 
std::size_t top () const noexcept
 First visible item.
 
std::size_t visible () const noexcept
 Rows shown at once.
 
std::optional< std::size_t > update (Navigation &navigation, const WidgetTree &tree)
 Applies the frame's input to the selection.
 
void draw (render::IRenderer &renderer, const WidgetTree &tree, const render::BitmapFont &font, const ListStyle &style={}) const
 Draws the visible rows, the selection and the scrollbar at the tree's scale.
 

Detailed Description

A scrollable list of items, one selected, as a widget of a WidgetTree.

The list is one widget of the tree, so it takes the focus like a button. While it is focused, Up and Down move the selection and are consumed; at the first or last item the move is left to the tree, so the focus walks out of the list. The list scrolls to keep the selection visible, and a scrollbar shows where it is when some items are off screen. A click selects and chooses the row under the mouse; activate chooses the selected one. Bounds are given at 100% and shared by the visible rows.

ui::ListView modes{menu.tree(), {.x = 40, .y = 40, .width = 240, .height = 96},
{"1280 x 720", "1600 x 900", "1920 x 1080"}, 3};
// Each frame, before menu.update():
if (const auto chosen = modes.update(navigation, menu.tree())) { apply(*chosen); }
A scrollable list of items, one selected, as a widget of a WidgetTree.
Definition widgets.hpp:151

Constructor & Destructor Documentation

◆ ListView()

aegis2d::ui::ListView::ListView ( WidgetTree &  tree,
render::Rect  bounds,
std::vector< std::string >  items,
std::size_t  visible,
WidgetId  group = root 
)

Adds the list to a tree.

Parameters
treeTree it lives in; the list keeps only the widget's identifier.
boundsRectangle at 100% for the visible rows together, in logical pixels.
itemsTexts of the rows, at least one.
visibleRows shown at once, at least 1; fewer items show them all.
groupGroup of the tree to put it in, root by default.
Exceptions
std::invalid_argumentif items is empty or visible is 0; as WidgetTree::add() for group.

Member Function Documentation

◆ draw()

void aegis2d::ui::ListView::draw ( render::IRenderer &  renderer,
const WidgetTree &  tree,
const render::BitmapFont &  font,
const ListStyle &  style = {} 
) const

Draws the visible rows, the selection and the scrollbar at the tree's scale.

Parameters
rendererRenderer in a frame.
treeThe tree the list was added to.
fontFont of the texts.
styleColors, text size and layer.

◆ id()

WidgetId aegis2d::ui::ListView::id ( ) const
noexcept

The list's widget in its tree.

Returns
The identifier given by the tree.

◆ items()

std::span< const std::string > aegis2d::ui::ListView::items ( ) const
noexcept

Texts of the rows.

Returns
The items given to the constructor.

◆ select()

void aegis2d::ui::ListView::select ( std::size_t  item)

Selects an item and scrolls to show it.

Parameters
itemIndex of the item.
Exceptions
std::out_of_rangeif item does not exist.

◆ selected()

std::size_t aegis2d::ui::ListView::selected ( ) const
noexcept

Item selected now.

Returns
Its index; 0 at the start.

◆ top()

std::size_t aegis2d::ui::ListView::top ( ) const
noexcept

First visible item.

Returns
Its index; the list scrolls so the selection stays visible.

◆ update()

std::optional< std::size_t > aegis2d::ui::ListView::update ( Navigation &  navigation,
const WidgetTree &  tree 
)

Applies the frame's input to the selection.

While the list is focused, Up or Down moves the selection and is consumed (taken out of navigation), except at the first or last item, where the tree moves the focus out instead. A click on a row selects and chooses it; activate chooses the selected one. A disabled list changes nothing.

Parameters
navigationInput of the frame; its direction is consumed when used.
treeThe tree the list was added to.
Returns
The item chosen during the frame, if any.

◆ visible()

std::size_t aegis2d::ui::ListView::visible ( ) const
noexcept

Rows shown at once.

Returns
The count given to the constructor, at most the number of items.

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