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

Actions of the game and the keys and gamepad buttons that trigger them. More...

#include <aegis2d/platform/actions.hpp>

Public Member Functions

ActionId add (std::string name, std::span< const Key > defaults)
 Declares an action, bound to its default keys.
 
ActionId add (std::string name, std::initializer_list< Key > defaults)
 Declares an action, bound to its default keys.
 
ActionId add (std::string name, std::span< const Key > keys, std::span< const GamepadButton > buttons)
 Declares an action, bound to its default keys and gamepad buttons.
 
ActionId add (std::string name, std::initializer_list< Key > keys, std::initializer_list< GamepadButton > buttons)
 Declares an action, bound to its default keys and gamepad buttons.
 
std::size_t size () const noexcept
 Number of actions declared.
 
std::optional< ActionId > find (std::string_view name) const
 Finds an action by name.
 
std::string_view name (ActionId action) const
 Name of an action.
 
std::span< const Key > keys (ActionId action) const
 Keys currently bound to an action.
 
std::span< const Key > default_keys (ActionId action) const
 Default keys of an action.
 
std::optional< ActionId > action_of (Key key) const noexcept
 Action a key is bound to.
 
std::span< const GamepadButton > buttons (ActionId action) const
 Gamepad buttons currently bound to an action.
 
std::span< const GamepadButton > default_buttons (ActionId action) const
 Default gamepad buttons of an action.
 
std::optional< ActionId > action_of (GamepadButton button) const noexcept
 Action a gamepad button is bound to.
 
std::optional< ActionId > bind (ActionId action, Key key)
 Binds one more key to an action.
 
std::optional< ActionId > bind (ActionId action, GamepadButton button)
 Binds one more gamepad button to an action.
 
std::optional< ActionId > rebind (ActionId action, Key key)
 Makes a key the only key of an action, as a settings menu does.
 
std::optional< ActionId > rebind (ActionId action, GamepadButton button)
 Makes a gamepad button the only button of an action; its keys are kept.
 
bool unbind (ActionId action, Key key)
 Removes a key from an action.
 
bool unbind (ActionId action, GamepadButton button)
 Removes a gamepad button from an action.
 
void clear (ActionId action)
 Removes every key and button of an action, which can then no longer be triggered.
 
void reset (ActionId action)
 Binds an action to its default keys and buttons again.
 
void reset_all ()
 Binds every action to its default keys and buttons again.
 
void save (core::Config &settings, std::string_view section) const
 Writes the keys of every action into the player's settings.
 
std::size_t load (const core::Config &settings, std::string_view section)
 Reads the keys written by save(): each action found gets the keys listed.
 
void save_buttons (core::Config &settings, std::string_view section) const
 Writes the gamepad buttons of every action into the player's settings.
 
std::size_t load_buttons (const core::Config &settings, std::string_view section)
 Reads the gamepad buttons written by save_buttons(), as load() reads keys.
 
bool held (ActionId action, const Keyboard &keyboard) const
 Tells whether an action is held.
 
bool pressed (ActionId action, const Keyboard &keyboard) const
 Tells whether an action was triggered during the frame.
 
bool released (ActionId action, const Keyboard &keyboard) const
 Tells whether an action stopped being held during the frame.
 
bool held (ActionId action, const Keyboard &keyboard, const Gamepad &gamepad) const
 Tells whether an action is held on the keyboard or the gamepad.
 
bool pressed (ActionId action, const Keyboard &keyboard, const Gamepad &gamepad) const
 Tells whether an action was triggered on the keyboard or the gamepad during the frame.
 
bool released (ActionId action, const Keyboard &keyboard, const Gamepad &gamepad) const
 Tells whether an action stopped being held during the frame.
 

Detailed Description

Actions of the game and the keys and gamepad buttons that trigger them.

The game declares its actions once with their default keys and buttons, then reads them every frame against the window's Keyboard and Gamepad:

const auto fire = actions.add("fire", {platform::Key::Space},
...
window.poll_events();
if (actions.held(fire, window.keyboard(), window.gamepad())) { ... }
Actions of the game and the keys and gamepad buttons that trigger them.
Definition actions.hpp:38
bool held(ActionId action, const Keyboard &keyboard) const
Tells whether an action is held.
ActionId add(std::string name, std::span< const Key > defaults)
Declares an action, bound to its default keys.
@ South
Bottom face button.

An action can have several keys and buttons, and a key or a button triggers at most one action: binding it takes it from the action that had it. Use one map per context (gameplay, menus) when the same key must mean different things.

Member Function Documentation

◆ action_of() [1/2]

std::optional< ActionId > aegis2d::platform::ActionMap::action_of ( GamepadButton  button) const
noexcept

Action a gamepad button is bound to.

Parameters
buttonButton to look up.
Returns
The action, or nothing if the button is not bound.

◆ action_of() [2/2]

std::optional< ActionId > aegis2d::platform::ActionMap::action_of ( Key  key) const
noexcept

Action a key is bound to.

Parameters
keyKey to look up.
Returns
The action, or nothing if the key is not bound.

◆ add() [1/4]

ActionId aegis2d::platform::ActionMap::add ( std::string  name,
std::initializer_list< Key >  defaults 
)

Declares an action, bound to its default keys.

Parameters
nameName of the action: letters, digits, _ and - only.
defaultsKeys bound to the action, such as {Key::Up, Key::W}.
Returns
The identifier of the new action.
Exceptions
std::invalid_argumentas add(std::string, std::span<const Key>).

◆ add() [2/4]

ActionId aegis2d::platform::ActionMap::add ( std::string  name,
std::initializer_list< Key >  keys,
std::initializer_list< GamepadButton >  buttons 
)

Declares an action, bound to its default keys and gamepad buttons.

Parameters
nameName of the action: letters, digits, _ and - only.
keysDefault keys, such as {Key::Up, Key::W}.
buttonsDefault buttons, such as {GamepadButton::DpadUp, GamepadButton::LeftStickUp}.
Returns
The identifier of the new action.
Exceptions
std::invalid_argumentas the other add() overloads.

◆ add() [3/4]

ActionId aegis2d::platform::ActionMap::add ( std::string  name,
std::span< const Key >  defaults 
)

Declares an action, bound to its default keys.

Parameters
nameName of the action, used to find it and in settings files: letters, digits, _ and - only.
defaultsKeys bound to the action; a key listed twice is kept once. A key currently bound to another action by the player is taken from it.
Returns
The identifier of the new action: 0 for the first, then 1, 2...
Exceptions
std::invalid_argumentif name is empty, has other characters or is already used, or if a default key is a default key of another action.

◆ add() [4/4]

ActionId aegis2d::platform::ActionMap::add ( std::string  name,
std::span< const Key >  keys,
std::span< const GamepadButton >  buttons 
)

Declares an action, bound to its default keys and gamepad buttons.

Parameters
nameName of the action: letters, digits, _ and - only.
keysDefault keys; a key listed twice is kept once.
buttonsDefault buttons; a button listed twice is kept once. A key or a button currently bound to another action by the player is taken from it.
Returns
The identifier of the new action.
Exceptions
std::invalid_argumentif name is invalid or already used, or if a default key or button is a default of another action.

◆ bind() [1/2]

std::optional< ActionId > aegis2d::platform::ActionMap::bind ( ActionId  action,
GamepadButton  button 
)

Binds one more gamepad button to an action.

Parameters
actionIdentifier returned by add().
buttonButton to bind; taken from the action that had it, if any.
Returns
The action the button was taken from, or nothing if it was free or already bound to action.
Exceptions
std::out_of_rangeif action is not declared.

◆ bind() [2/2]

std::optional< ActionId > aegis2d::platform::ActionMap::bind ( ActionId  action,
Key  key 
)

Binds one more key to an action.

Parameters
actionIdentifier returned by add().
keyKey to bind; taken from the action that had it, if any.
Returns
The action the key was taken from, or nothing if it was free or already bound to action.
Exceptions
std::out_of_rangeif action is not declared.

◆ buttons()

std::span< const GamepadButton > aegis2d::platform::ActionMap::buttons ( ActionId  action) const

Gamepad buttons currently bound to an action.

Parameters
actionIdentifier returned by add().
Returns
The buttons, in the order they were bound; empty if none is.
Exceptions
std::out_of_rangeif action is not declared.

◆ clear()

void aegis2d::platform::ActionMap::clear ( ActionId  action)

Removes every key and button of an action, which can then no longer be triggered.

Parameters
actionIdentifier returned by add().
Exceptions
std::out_of_rangeif action is not declared.

◆ default_buttons()

std::span< const GamepadButton > aegis2d::platform::ActionMap::default_buttons ( ActionId  action) const

Default gamepad buttons of an action.

Parameters
actionIdentifier returned by add().
Returns
The buttons given to add().
Exceptions
std::out_of_rangeif action is not declared.

◆ default_keys()

std::span< const Key > aegis2d::platform::ActionMap::default_keys ( ActionId  action) const

Default keys of an action.

Parameters
actionIdentifier returned by add().
Returns
The keys given to add().
Exceptions
std::out_of_rangeif action is not declared.

◆ find()

std::optional< ActionId > aegis2d::platform::ActionMap::find ( std::string_view  name) const

Finds an action by name.

Parameters
nameName given to add(), case-sensitive.
Returns
Its identifier, or nothing if no action has this name.

◆ held() [1/2]

bool aegis2d::platform::ActionMap::held ( ActionId  action,
const Keyboard &  keyboard 
) const

Tells whether an action is held.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
Returns
true if one of its keys is down.
Exceptions
std::out_of_rangeif action is not declared.

◆ held() [2/2]

bool aegis2d::platform::ActionMap::held ( ActionId  action,
const Keyboard &  keyboard,
const Gamepad &  gamepad 
) const

Tells whether an action is held on the keyboard or the gamepad.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
gamepadState of the gamepads, usually Window::gamepad().
Returns
true if one of its keys or buttons is down.
Exceptions
std::out_of_rangeif action is not declared.

◆ keys()

std::span< const Key > aegis2d::platform::ActionMap::keys ( ActionId  action) const

Keys currently bound to an action.

Parameters
actionIdentifier returned by add().
Returns
The keys, in the order they were bound; empty if the action is unbound.
Exceptions
std::out_of_rangeif action is not declared.

◆ load()

std::size_t aegis2d::platform::ActionMap::load ( const core::Config &  settings,
std::string_view  section 
)

Reads the keys written by save(): each action found gets the keys listed.

Actions missing from the settings keep their keys, so a game update can add actions. Unknown key names are skipped with a warning, and an action whose keys are all unknown, or which is not an array, keeps its keys; an empty array leaves it unbound. A key listed for two actions goes to the last one.

Parameters
settingsSettings read with read_settings() or core::Config::from_file().
sectionDotted path given to save().
Returns
Number of actions whose keys were read.

◆ load_buttons()

std::size_t aegis2d::platform::ActionMap::load_buttons ( const core::Config &  settings,
std::string_view  section 
)

Reads the gamepad buttons written by save_buttons(), as load() reads keys.

Parameters
settingsSettings read with read_settings() or core::Config::from_file().
sectionDotted path given to save_buttons().
Returns
Number of actions whose buttons were read.

◆ name()

std::string_view aegis2d::platform::ActionMap::name ( ActionId  action) const

Name of an action.

Parameters
actionIdentifier returned by add().
Returns
The name given to add().
Exceptions
std::out_of_rangeif action is not declared.

◆ pressed() [1/2]

bool aegis2d::platform::ActionMap::pressed ( ActionId  action,
const Keyboard &  keyboard 
) const

Tells whether an action was triggered during the frame.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
Returns
true if one of its keys went down during the frame.
Exceptions
std::out_of_rangeif action is not declared.

◆ pressed() [2/2]

bool aegis2d::platform::ActionMap::pressed ( ActionId  action,
const Keyboard &  keyboard,
const Gamepad &  gamepad 
) const

Tells whether an action was triggered on the keyboard or the gamepad during the frame.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
gamepadState of the gamepads, usually Window::gamepad().
Returns
true if one of its keys or buttons went down during the frame.
Exceptions
std::out_of_rangeif action is not declared.

◆ rebind() [1/2]

std::optional< ActionId > aegis2d::platform::ActionMap::rebind ( ActionId  action,
GamepadButton  button 
)

Makes a gamepad button the only button of an action; its keys are kept.

Parameters
actionIdentifier returned by add().
buttonNew button of the action; taken from the action that had it, if any.
Returns
The action the button was taken from, or nothing.
Exceptions
std::out_of_rangeif action is not declared.

◆ rebind() [2/2]

std::optional< ActionId > aegis2d::platform::ActionMap::rebind ( ActionId  action,
Key  key 
)

Makes a key the only key of an action, as a settings menu does.

The action's gamepad buttons are kept.

Parameters
actionIdentifier returned by add().
keyNew key of the action; taken from the action that had it, if any.
Returns
The action the key was taken from, or nothing.
Exceptions
std::out_of_rangeif action is not declared.

◆ released() [1/2]

bool aegis2d::platform::ActionMap::released ( ActionId  action,
const Keyboard &  keyboard 
) const

Tells whether an action stopped being held during the frame.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
Returns
true if one of its keys went up during the frame and none is down.
Exceptions
std::out_of_rangeif action is not declared.

◆ released() [2/2]

bool aegis2d::platform::ActionMap::released ( ActionId  action,
const Keyboard &  keyboard,
const Gamepad &  gamepad 
) const

Tells whether an action stopped being held during the frame.

Parameters
actionIdentifier returned by add().
keyboardState of the keyboard, usually Window::keyboard().
gamepadState of the gamepads, usually Window::gamepad().
Returns
true if one of its keys or buttons went up during the frame and none is down.
Exceptions
std::out_of_rangeif action is not declared.

◆ reset()

void aegis2d::platform::ActionMap::reset ( ActionId  action)

Binds an action to its default keys and buttons again.

Defaults bound to other actions are taken from them.

Parameters
actionIdentifier returned by add().
Exceptions
std::out_of_rangeif action is not declared.

◆ reset_all()

void aegis2d::platform::ActionMap::reset_all ( )

Binds every action to its default keys and buttons again.

◆ save()

void aegis2d::platform::ActionMap::save ( core::Config &  settings,
std::string_view  section 
) const

Writes the keys of every action into the player's settings.

Each action becomes an array of key names under section, such as {"input": {"up": ["Up", "W"], "fire": ["Space"]}} for the section "input"; an unbound action is an empty array. Other settings are left as they are.

Parameters
settingsSettings to update, then saved with core::Config::save().
sectionDotted path of the object holding the actions; empty for the root.

◆ save_buttons()

void aegis2d::platform::ActionMap::save_buttons ( core::Config &  settings,
std::string_view  section 
) const

Writes the gamepad buttons of every action into the player's settings.

As save() for the keys, with button names (button_name()), such as {"gamepad": {"fire": ["South", "RightTrigger"]}} for the section "gamepad". Use another section than the keys'.

Parameters
settingsSettings to update, then saved with core::Config::save().
sectionDotted path of the object holding the actions; empty for the root.

◆ size()

std::size_t aegis2d::platform::ActionMap::size ( ) const
noexcept

Number of actions declared.

Returns
Every ActionId below it is valid.

◆ unbind() [1/2]

bool aegis2d::platform::ActionMap::unbind ( ActionId  action,
GamepadButton  button 
)

Removes a gamepad button from an action.

Parameters
actionIdentifier returned by add().
buttonButton to remove.
Returns
true if the button was bound to action.
Exceptions
std::out_of_rangeif action is not declared.

◆ unbind() [2/2]

bool aegis2d::platform::ActionMap::unbind ( ActionId  action,
Key  key 
)

Removes a key from an action.

Parameters
actionIdentifier returned by add().
keyKey to remove.
Returns
true if the key was bound to action.
Exceptions
std::out_of_rangeif action is not declared.

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