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

Key binding step of a settings menu: "press a key for Fire". More...

#include <aegis2d/platform/key_capture.hpp>

Public Member Functions

 KeyCapture (Key cancel_key=Key::Escape) noexcept
 Creates a capture that waits for nothing yet.
 
void start (ActionId action, const Keyboard &keyboard) noexcept
 Waits for a key for an action, replacing a capture in progress.
 
void cancel () noexcept
 Stops waiting, without changing any binding.
 
std::optional< ActionId > waiting () const noexcept
 Action waiting for a key.
 
std::optional< CapturedKey > update (ActionMap &actions, const Keyboard &keyboard)
 Binds the first key pressed during the frame, if any.
 

Detailed Description

Key binding step of a settings menu: "press a key for Fire".

The menu calls start() when the player picks an action, then update() once per frame after Window::poll_events(). The next key pressed becomes the only key of the action (ActionMap::rebind()); the cancel key (Escape by default) gives up and leaves the action unchanged, so it cannot be bound this way. Keys pressed in the frame start() was called in are ignored: the key that chose the action is not taken as its new key.

if (fire_chosen_in_menu) {
capture.start(fire, window.keyboard());
}
if (capture.update(actions, window.keyboard())) {
auto settings = platform::read_settings(file);
actions.save(settings, "input");
settings.save(file);
}
core::Config read_settings(const std::filesystem::path &file)
Reads the player's settings file.

Constructor & Destructor Documentation

◆ KeyCapture()

aegis2d::platform::KeyCapture::KeyCapture ( Key  cancel_key = Key::Escape)
explicitnoexcept

Creates a capture that waits for nothing yet.

Parameters
cancel_keyKey that cancels a capture.

Member Function Documentation

◆ cancel()

void aegis2d::platform::KeyCapture::cancel ( )
noexcept

Stops waiting, without changing any binding.

◆ start()

void aegis2d::platform::KeyCapture::start ( ActionId  action,
const Keyboard &  keyboard 
)
noexcept

Waits for a key for an action, replacing a capture in progress.

Parameters
actionAction to bind, declared in the ActionMap given to update().
keyboardKeyboard given to update(); its current frame is ignored.

◆ update()

std::optional< CapturedKey > aegis2d::platform::KeyCapture::update ( ActionMap &  actions,
const Keyboard &  keyboard 
)

Binds the first key pressed during the frame, if any.

Parameters
actionsActions of the menu's game; the waiting action gets the key.
keyboardState of the keyboard, usually Window::keyboard().
Returns
What happened once the capture ends, or nothing while it waits.
Exceptions
std::out_of_rangeif the waiting action is not declared in actions.

◆ waiting()

std::optional< ActionId > aegis2d::platform::KeyCapture::waiting ( ) const
noexcept

Action waiting for a key.

Returns
The action given to start(), or nothing if no capture is in progress.

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