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

An editable line of text, as a widget of a WidgetTree. More...

#include <aegis2d/ui/text_field.hpp>

Public Member Functions

 TextField (WidgetTree &tree, render::Rect bounds, TextFieldConfig config={}, WidgetId group=root)
 Adds the field to a tree.
 
WidgetId id () const noexcept
 The field's widget in its tree.
 
const std::string & text () const noexcept
 The typed text.
 
void set_text (std::string text)
 Replaces the text and puts the cursor at its end.
 
std::size_t cursor () const noexcept
 Where the cursor is.
 
bool update (Navigation &navigation, const platform::TextInput &input, const platform::Keyboard &keyboard, const WidgetTree &tree)
 Applies the frame's input to the text.
 
void draw (render::IRenderer &renderer, const WidgetTree &tree, const render::BitmapFont &font, const TextFieldStyle &style={}) const
 Draws the field, its text or placeholder and the cursor at the tree's scale.
 

Detailed Description

An editable line of text, as a widget of a WidgetTree.

The field is a widget of the tree, so it takes the focus like a button. While it is focused, the frame's typed text (platform::TextInput, so the characters follow the keyboard layout) is inserted at the cursor, up to the limit; Backspace and Delete remove one character (accents and emoji whole), Left and Right move the cursor (consumed, so the focus stays), Home and End jump to the ends, and Space types a space instead of activating. Up, Down and Tab leave the field through the tree. The caller starts the window's text input while a field is focused (Window::set_text_input()).

ui::TextField address{tree, {.x = 40, .y = 40, .width = 280, .height = 32},
{.placeholder = "server address", .max_length = 64}};
// Each frame:
window.set_text_input(tree.focused() == address.id());
address.update(navigation, window.text_input(), window.keyboard(), tree);
An editable line of text, as a widget of a WidgetTree.
Definition text_field.hpp:69
bool update(Navigation &navigation, const platform::TextInput &input, const platform::Keyboard &keyboard, const WidgetTree &tree)
Applies the frame's input to the text.

Constructor & Destructor Documentation

◆ TextField()

aegis2d::ui::TextField::TextField ( WidgetTree &  tree,
render::Rect  bounds,
TextFieldConfig  config = {},
WidgetId  group = root 
)

Adds the field to a tree.

Parameters
treeTree it lives in; the field keeps only the widget's identifier.
boundsRectangle at 100%, in logical pixels.
configStarting text, placeholder and limit.
groupGroup of the tree to put it in, root by default.
Exceptions
std::invalid_argumentif max_length is 0; as WidgetTree::add() for group.

Member Function Documentation

◆ cursor()

std::size_t aegis2d::ui::TextField::cursor ( ) const
noexcept

Where the cursor is.

Returns
In characters (code points) from the start of the text.

◆ draw()

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

Draws the field, its text or placeholder and the cursor at the tree's scale.

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

◆ id()

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

The field's widget in its tree.

Returns
The identifier given by the tree.

◆ set_text()

void aegis2d::ui::TextField::set_text ( std::string  text)

Replaces the text and puts the cursor at its end.

Parameters
textNew text, cut at max_length characters.

◆ text()

const std::string & aegis2d::ui::TextField::text ( ) const
noexcept

The typed text.

Returns
UTF-8 text, at most max_length characters.

◆ update()

bool aegis2d::ui::TextField::update ( Navigation &  navigation,
const platform::TextInput &  input,
const platform::Keyboard &  keyboard,
const WidgetTree &  tree 
)

Applies the frame's input to the text.

Only while the field is focused and enabled; gaining the focus puts the cursor at the end. Left and Right are consumed out of navigation, and activate is consumed when it comes from the Space key, which types a space instead.

Parameters
navigationInput of the frame; its direction and activate are consumed when used.
inputThe frame's typed text, from Window::text_input().
keyboardThe frame's keyboard, for the editing keys.
treeThe tree the field was added to.
Returns
true if the text changed.

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