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

The in-game console: the key under Escape opens it over the game, a command typed on its input line runs with Enter, and the output scrolls above. More...

#include <aegis2d/ui/console.hpp>

Public Member Functions

 Console (std::size_t capacity=500)
 Closed console, with help and clear.
 
 Console (const Console &)=delete
 
Console & operator= (const Console &)=delete
 
 Console (Console &&)=delete
 
Console & operator= (Console &&)=delete
 
 ~Console ()=default
 
core::Commands & commands () noexcept
 Commands the console runs.
 
bool open () const noexcept
 Tells whether the console is open.
 
void set_open (bool open) noexcept
 Opens or closes the console; closing it keeps the input line.
 
bool update (const platform::Keyboard &keyboard, const platform::TextInput &input)
 Applies a frame's keyboard and typed text.
 
core::CommandResult submit (std::string_view line)
 Runs a line as if typed and run with Enter.
 
std::span< const std::string > history () const noexcept
 The lines run, oldest first, for Up and Down.
 
void recall_previous ()
 Puts the line run before the one shown on the input line, as Up does.
 
void recall_next ()
 Puts the line run after the one shown on the input line, as Down does.
 
std::vector< std::string > complete ()
 Completes the name being typed on the input line, as Tab does.
 
void print (std::string_view text, ConsoleLine::Kind kind=ConsoleLine::Kind::Output)
 Adds text to the output, a line per line break.
 
void clear () noexcept
 Empties the output, as the clear command does.
 
const std::string & input () const noexcept
 The input line.
 
void set_input (std::string_view text)
 Replaces the input line.
 
std::span< const ConsoleLine > lines () const noexcept
 The output, oldest first.
 
std::size_t scroll () const noexcept
 How far the output is scrolled up.
 
void scroll_by (std::ptrdiff_t lines) noexcept
 Scrolls the output.
 
void draw (render::IRenderer &renderer, const render::BitmapFont &font, Vec2 screen, const ConsoleStyle &style={}) const
 Draws the console, if open, at the top of the screen.
 

Static Public Attributes

static constexpr platform::Key toggle_key = platform::Key::Grave
 Key that opens and closes the console: the one under Escape, whatever the layout (the backquote on QWERTY, ² on AZERTY).
 
static constexpr std::size_t history_capacity = 100
 Lines of history kept.
 

Detailed Description

The in-game console: the key under Escape opens it over the game, a command typed on its input line runs with Enter, and the output scrolls above.

The console owns its core::Commands: the game adds its own commands to commands(), and its console variables with commands().add_variables(); the console adds clear, and help comes with the commands. While it is open, update() takes the keyboard: the frame's typed text goes to the input line (so the characters follow the keyboard layout), Backspace removes a character, Enter runs the line, Up and Down go through the lines run before, Tab completes the name of a command or a variable, Page Up and Page Down scroll the output, and Escape or the key under it closes it. The game ignores the keyboard then, and starts the window's text input while the console is open. The output keeps its last capacity lines. Not thread-safe; not movable, as clear refers to it.

ui::Console console;
console.commands().add_variables(variables);
console.commands().add("quit", {}, "Leaves the game", [&](const core::CommandCall&) {
loop.stop();
return std::string{};
});
// Each frame:
window.poll_events();
const bool typing = console.update(window.keyboard(), window.text_input());
window.set_text_input(console.open());
if (!typing) {
// ... the game reads the keyboard ...
}
// ... the game's sprites ...
console.draw(renderer, font, {1280.0F, 720.0F});
The arguments of a command being run, read in their types.
Definition commands.hpp:45
void add_variables(ConsoleVariables &variables) noexcept
Makes the variables of a registry commands too.
void add(std::string_view name, std::vector< CommandArg > arguments, std::string_view description, CommandHandler handler)
Adds a command.
The in-game console: the key under Escape opens it over the game, a command typed on its input line r...
Definition console.hpp:88
core::Commands & commands() noexcept
Commands the console runs.
void draw(render::IRenderer &renderer, const render::BitmapFont &font, Vec2 screen, const ConsoleStyle &style={}) const
Draws the console, if open, at the top of the screen.
bool update(const platform::Keyboard &keyboard, const platform::TextInput &input)
Applies a frame's keyboard and typed text.
bool open() const noexcept
Tells whether the console is open.

Constructor & Destructor Documentation

◆ Console() [1/3]

aegis2d::ui::Console::Console ( std::size_t  capacity = 500)
explicit

Closed console, with help and clear.

Parameters
capacityLines of output kept, at least 1.
Exceptions
std::invalid_argumentif capacity is 0.

◆ Console() [2/3]

aegis2d::ui::Console::Console ( const Console &  )
delete

◆ Console() [3/3]

aegis2d::ui::Console::Console ( Console &&  )
delete

◆ ~Console()

aegis2d::ui::Console::~Console ( )
default

Member Function Documentation

◆ clear()

void aegis2d::ui::Console::clear ( )
noexcept

Empties the output, as the clear command does.

◆ commands()

core::Commands & aegis2d::ui::Console::commands ( )
noexcept

Commands the console runs.

Returns
Its commands, to add the game's.

◆ complete()

std::vector< std::string > aegis2d::ui::Console::complete ( )

Completes the name being typed on the input line, as Tab does.

The first word of the line is completed with the names of the commands and the variables, as is the name after help. One name that fits is completed whole, followed by a space; several are completed as far as they agree, and listed in the output once the line cannot go further.

Returns
The names that fit, sorted; none when there is no name to complete.

◆ draw()

void aegis2d::ui::Console::draw ( render::IRenderer &  renderer,
const render::BitmapFont &  font,
Vec2  screen,
const ConsoleStyle &  style = {} 
) const

Draws the console, if open, at the top of the screen.

The input line is at the bottom of the panel, after the prompt > and with a cursor; the output fills the panel above it, its lines wrapped to its width.

Parameters
rendererRenderer in a frame.
fontFont of the text.
screenSize of the screen, in logical pixels.
styleWhere and how it is drawn.
Exceptions
std::logic_erroroutside a frame (as render::IRenderer::draw()).

◆ history()

std::span< const std::string > aegis2d::ui::Console::history ( ) const
noexcept

The lines run, oldest first, for Up and Down.

Blank lines are left out, and a line is kept once when run twice in a row; the last history_capacity are kept.

Returns
The lines.

◆ input()

const std::string & aegis2d::ui::Console::input ( ) const
noexcept

The input line.

Returns
The text typed and not run yet, in UTF-8.

◆ lines()

std::span< const ConsoleLine > aegis2d::ui::Console::lines ( ) const
noexcept

The output, oldest first.

Returns
At most the capacity's last lines.

◆ open()

bool aegis2d::ui::Console::open ( ) const
noexcept

Tells whether the console is open.

Returns
false by default.

◆ operator=() [1/2]

Console & aegis2d::ui::Console::operator= ( Console &&  )
delete

◆ operator=() [2/2]

Console & aegis2d::ui::Console::operator= ( const Console &  )
delete

◆ print()

void aegis2d::ui::Console::print ( std::string_view  text,
ConsoleLine::Kind  kind = ConsoleLine::Kind::Output 
)

Adds text to the output, a line per line break.

Parameters
textText to show.
kindIts kind.

◆ recall_next()

void aegis2d::ui::Console::recall_next ( )

Puts the line run after the one shown on the input line, as Down does.

◆ recall_previous()

void aegis2d::ui::Console::recall_previous ( )

Puts the line run before the one shown on the input line, as Up does.

The first one keeps the line being typed, which recall_next() brings back after the last line run; typing or Backspace starts from the last line run again.

◆ scroll()

std::size_t aegis2d::ui::Console::scroll ( ) const
noexcept

How far the output is scrolled up.

Returns
Lines of output above the last one shown: 0 shows the last lines.

◆ scroll_by()

void aegis2d::ui::Console::scroll_by ( std::ptrdiff_t  lines)
noexcept

Scrolls the output.

Parameters
linesLines up (positive) or down (negative), kept within the output.

◆ set_input()

void aegis2d::ui::Console::set_input ( std::string_view  text)

Replaces the input line.

Parameters
textNew text.

◆ set_open()

void aegis2d::ui::Console::set_open ( bool  open)
noexcept

Opens or closes the console; closing it keeps the input line.

Parameters
opentrue to open it.

◆ submit()

core::CommandResult aegis2d::ui::Console::submit ( std::string_view  line)

Runs a line as if typed and run with Enter.

The line is echoed after the prompt, then what the command shows, or why the line did not run as an error; a blank line only shows the prompt. The output goes back to its last line.

Parameters
lineSuch as "help".
Returns
How it went.

◆ update()

bool aegis2d::ui::Console::update ( const platform::Keyboard &  keyboard,
const platform::TextInput &  input 
)

Applies a frame's keyboard and typed text.

The toggle key opens or closes it; the frame's typed text is dropped then, as it holds that key's character. While open, as told in the class.

Parameters
keyboardThe frame's keyboard.
inputThe frame's typed text, from platform::Window::text_input().
Returns
true if the console took the frame's keyboard (it is open, or the toggle key was pressed): the game ignores it then.

Member Data Documentation

◆ history_capacity

constexpr std::size_t aegis2d::ui::Console::history_capacity = 100
staticconstexpr

Lines of history kept.

◆ toggle_key

constexpr platform::Key aegis2d::ui::Console::toggle_key = platform::Key::Grave
staticconstexpr

Key that opens and closes the console: the one under Escape, whatever the layout (the backquote on QWERTY, ² on AZERTY).


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