|
Aegis2D
Modular C++26 2D game engine for networked games
|
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. | |
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:
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.
|
noexcept |
Action a gamepad button is bound to.
| button | Button to look up. |
Action a key is bound to.
| key | Key to look up. |
| ActionId aegis2d::platform::ActionMap::add | ( | std::string | name, |
| std::initializer_list< Key > | defaults | ||
| ) |
Declares an action, bound to its default keys.
| name | Name of the action: letters, digits, _ and - only. |
| defaults | Keys bound to the action, such as {Key::Up, Key::W}. |
| std::invalid_argument | as add(std::string, std::span<const Key>). |
| 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.
| name | Name of the action: letters, digits, _ and - only. |
| keys | Default keys, such as {Key::Up, Key::W}. |
| buttons | Default buttons, such as {GamepadButton::DpadUp, GamepadButton::LeftStickUp}. |
| std::invalid_argument | as the other add() overloads. |
Declares an action, bound to its default keys.
| name | Name of the action, used to find it and in settings files: letters, digits, _ and - only. |
| defaults | Keys 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. |
| std::invalid_argument | if name is empty, has other characters or is already used, or if a default key is a default key of another action. |
| 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.
| name | Name of the action: letters, digits, _ and - only. |
| keys | Default keys; a key listed twice is kept once. |
| buttons | Default 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. |
| std::invalid_argument | if name is invalid or already used, or if a default key or button is a default of another action. |
| std::optional< ActionId > aegis2d::platform::ActionMap::bind | ( | ActionId | action, |
| GamepadButton | button | ||
| ) |
Binds one more gamepad button to an action.
| action | Identifier returned by add(). |
| button | Button to bind; taken from the action that had it, if any. |
action. | std::out_of_range | if action is not declared. |
Binds one more key to an action.
| action | Identifier returned by add(). |
| key | Key to bind; taken from the action that had it, if any. |
action. | std::out_of_range | if action is not declared. |
| std::span< const GamepadButton > aegis2d::platform::ActionMap::buttons | ( | ActionId | action | ) | const |
| void aegis2d::platform::ActionMap::clear | ( | ActionId | action | ) |
Removes every key and button of an action, which can then no longer be triggered.
| action | Identifier returned by add(). |
| std::out_of_range | if action is not declared. |
| std::span< const GamepadButton > aegis2d::platform::ActionMap::default_buttons | ( | ActionId | action | ) | const |
| std::optional< ActionId > aegis2d::platform::ActionMap::find | ( | std::string_view | name | ) | const |
Finds an action by name.
| name | Name given to add(), case-sensitive. |
Tells whether an action is held.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
true if one of its keys is down. | std::out_of_range | if action is not declared. |
| 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.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
| gamepad | State of the gamepads, usually Window::gamepad(). |
true if one of its keys or buttons is down. | std::out_of_range | if action is not declared. |
Keys currently bound to an action.
| action | Identifier returned by add(). |
| std::out_of_range | if action is not declared. |
| 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.
| settings | Settings read with read_settings() or core::Config::from_file(). |
| section | Dotted path given to save(). |
| 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.
| settings | Settings read with read_settings() or core::Config::from_file(). |
| section | Dotted path given to save_buttons(). |
| std::string_view aegis2d::platform::ActionMap::name | ( | ActionId | action | ) | const |
Tells whether an action was triggered during the frame.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
true if one of its keys went down during the frame. | std::out_of_range | if action is not declared. |
| 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.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
| gamepad | State of the gamepads, usually Window::gamepad(). |
true if one of its keys or buttons went down during the frame. | std::out_of_range | if action is not declared. |
| 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.
| action | Identifier returned by add(). |
| button | New button of the action; taken from the action that had it, if any. |
| std::out_of_range | if action is not declared. |
Makes a key the only key of an action, as a settings menu does.
The action's gamepad buttons are kept.
| action | Identifier returned by add(). |
| key | New key of the action; taken from the action that had it, if any. |
| std::out_of_range | if action is not declared. |
Tells whether an action stopped being held during the frame.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
true if one of its keys went up during the frame and none is down. | std::out_of_range | if action is not declared. |
| bool aegis2d::platform::ActionMap::released | ( | ActionId | action, |
| const Keyboard & | keyboard, | ||
| const Gamepad & | gamepad | ||
| ) | const |
Tells whether an action stopped being held during the frame.
| action | Identifier returned by add(). |
| keyboard | State of the keyboard, usually Window::keyboard(). |
| gamepad | State of the gamepads, usually Window::gamepad(). |
true if one of its keys or buttons went up during the frame and none is down. | std::out_of_range | if action is not declared. |
| 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.
| action | Identifier returned by add(). |
| std::out_of_range | if action is not declared. |
| void aegis2d::platform::ActionMap::reset_all | ( | ) |
Binds every action to its default keys and buttons again.
| 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.
| settings | Settings to update, then saved with core::Config::save(). |
| section | Dotted path of the object holding the actions; empty for the root. |
| 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'.
| settings | Settings to update, then saved with core::Config::save(). |
| section | Dotted path of the object holding the actions; empty for the root. |
|
noexcept |
Number of actions declared.
| bool aegis2d::platform::ActionMap::unbind | ( | ActionId | action, |
| GamepadButton | button | ||
| ) |
Removes a gamepad button from an action.
| action | Identifier returned by add(). |
| button | Button to remove. |
true if the button was bound to action. | std::out_of_range | if action is not declared. |
Removes a key from an action.
| action | Identifier returned by add(). |
| key | Key to remove. |
true if the key was bound to action. | std::out_of_range | if action is not declared. |