#include <InputManager.h>
|
| InputManager (const InputManager &)=delete |
|
InputManager & | operator= (const InputManager &)=delete |
|
std::optional< Error > | addActionEvent (unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys) |
|
std::optional< Error > | addAxisEvent (unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis) |
|
std::optional< Error > | modifyActionEventKey (unsigned int iActionId, std::variant< KeyboardKey, MouseButton > oldKey, std::variant< KeyboardKey, MouseButton > newKey) |
|
std::optional< Error > | modifyAxisEventKey (unsigned int iAxisEventId, std::pair< KeyboardKey, KeyboardKey > oldPair, std::pair< KeyboardKey, KeyboardKey > newPair) |
|
std::optional< Error > | saveToFile (std::string_view sFileName) |
|
std::optional< Error > | loadFromFile (std::string_view sFileName) |
|
std::pair< std::vector< unsigned int >, std::vector< unsigned int > > | isKeyUsed (const std::variant< KeyboardKey, MouseButton > &key) |
|
std::vector< std::variant< KeyboardKey, MouseButton > > | getActionEvent (unsigned int iActionId) |
|
std::vector< std::pair< KeyboardKey, KeyboardKey > > | getAxisEvent (unsigned int iAxisEventId) |
|
float | getCurrentAxisEventState (unsigned int iAxisEventId) |
|
bool | removeActionEvent (unsigned int iActionId) |
|
bool | removeAxisEvent (unsigned int iAxisEventId) |
|
std::unordered_map< unsigned int, std::vector< std::variant< KeyboardKey, MouseButton > > > | getAllActionEvents () |
|
std::unordered_map< unsigned int, std::vector< std::pair< KeyboardKey, KeyboardKey > > > | getAllAxisEvents () |
|
|
static std::vector< std::string > | splitString (const std::string &sStringToSplit, const std::string &sDelimiter) |
|
|
std::optional< Error > | overwriteActionEvent (unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys) |
|
std::optional< Error > | overwriteAxisEvent (unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis) |
|
|
std::unordered_map< std::variant< KeyboardKey, MouseButton >, std::vector< unsigned int > > | actionEvents |
|
std::unordered_map< unsigned int, std::pair< std::vector< ActionState >, bool > > | actionState |
|
std::unordered_map< KeyboardKey, std::vector< std::pair< unsigned int, bool > > > | axisEvents |
|
std::unordered_map< unsigned int, std::pair< std::vector< AxisState >, int > > | axisState |
|
std::recursive_mutex | mtxActionEvents |
|
std::recursive_mutex | mtxAxisEvents |
|
Allows binding IDs with multiple input keys.
Stored in GameInstance object.
◆ addActionEvent()
std::optional< Error > ne::InputManager::addActionEvent |
( |
unsigned int |
iActionId, |
|
|
const std::vector< std::variant< KeyboardKey, MouseButton > > & |
vKeys |
|
) |
| |
Adds a new action event.
Action event allows binding mouse button(s) and/or keyboard key(s) with an ID. When one of the specified buttons is pressed you will receive an action event with the specified ID.
This way you can have an action "jump" with a space bar button and can easily change input key space bar to something else if the user wants to. For this, just call modifyActionEventKey to change one button of the action.
- Parameters
-
iActionId | Unique ID of the new action event. |
vKeys | Keyboard/mouse keys/buttons associated with this action. If empty, no event will be added. |
- Returns
- Returns an error if passed 'vKeys' argument is empty or if an action event with this ID is already registered.
◆ addAxisEvent()
std::optional< Error > ne::InputManager::addAxisEvent |
( |
unsigned int |
iAxisEventId, |
|
|
const std::vector< std::pair< KeyboardKey, KeyboardKey > > & |
vAxis |
|
) |
| |
Adds a new axis event.
Using axis event you can easily implement player movement (for example). An axis event consists of 2 buttons: one for '+1' input and the other for '-1' input. When the first button is pressed this event will be triggered with '+1' value, if other is pressed this event will be triggered with '-1' value, if both buttons were released this event will be triggered with '0' value. If both buttons were pressed this event will be triggered with the value of the last pressed button ('+1' if the first button was pressed last, '-1' if the second button was pressed last).
You can specify multiple pairs, for example: W/S buttons and up/down arrow keys.
- Parameters
-
iAxisEventId | Unique ID of the new axis event. |
vAxis | A pair of keyboard buttons associated with this axis, first button will be associated with '+1' input and the second with '-1' input. |
- Returns
- Returns an error if passed 'vAxis' argument is empty or if an axis event with this ID is already registered.
◆ getActionEvent()
std::vector< std::variant< KeyboardKey, MouseButton > > ne::InputManager::getActionEvent |
( |
unsigned int |
iActionId | ) |
|
Looks for an action event with the specified ID, if one is found a copy of this action's keys will be returned. Changes made to the returned vector will not be applied to the action, use addActionEvent for this purpose.
- Parameters
-
iActionId | Unique ID of an action to look for. |
- Returns
- Empty if no keys were associated with this event, otherwise keys associated with the action event.
◆ getAllActionEvents()
std::unordered_map< unsigned int, std::vector< std::variant< KeyboardKey, MouseButton > > > ne::InputManager::getAllActionEvents |
( |
| ) |
|
Returns all action events.
- Returns
- A copy of all action events.
◆ getAllAxisEvents()
std::unordered_map< unsigned int, std::vector< std::pair< KeyboardKey, KeyboardKey > > > ne::InputManager::getAllAxisEvents |
( |
| ) |
|
Returns all axis events.
- Returns
- A copy of all axis events.
◆ getAxisEvent()
std::vector< std::pair< KeyboardKey, KeyboardKey > > ne::InputManager::getAxisEvent |
( |
unsigned int |
iAxisEventId | ) |
|
Looks for an axis event with the specified ID, if one is found a copy of this axis's keys will be returned. Changes made to the returned vector will not be applied to the axis, use addAxisEvent for this purpose.
- Parameters
-
iAxisEventId | Unique ID of the axis event to look for. |
- Returns
- Empty if no keys were associated with this event, otherwise keys associated with the axis event.
◆ getCurrentAxisEventState()
float ne::InputManager::getCurrentAxisEventState |
( |
unsigned int |
iAxisEventId | ) |
|
Returns the current value of an axis event. This value is equal to the last value passed to GameInstance::onInputAxisEvent.
- Parameters
-
iAxisEventId | Unique ID of the axis event that you used in addAxisEvent. |
- Returns
- Zero if axis event with this ID does not exist, last input value otherwise.
◆ isKeyUsed()
std::pair< std::vector< unsigned int >, std::vector< unsigned int > > ne::InputManager::isKeyUsed |
( |
const std::variant< KeyboardKey, MouseButton > & |
key | ) |
|
Returns action and axis event IDs that the specified key is used in.
You can use this function to detect conflicting keys. For example: when the user wants to modify some event and you receive a 'newKey', look if this 'newKey' is already used somewhere and if it's, show a message to the user like: "this key is already used somewhere else
and cannot be assigned twice".
- Parameters
-
key | A key to see where it's used. |
- Returns
- A pair of action and axis event IDs that the specified key is used in.
◆ loadFromFile()
std::optional< Error > ne::InputManager::loadFromFile |
( |
std::string_view |
sFileName | ) |
|
Loads action/axis events from a file.
- Warning
- This function will only read action/axis events that exist in this InputManager. File's keys for action/axis event will replace the keys of existing action/axis event.
The usual workflow for working with input goes like this:
- add your action/axis events with some default keys,
- the user may change the keys of action/axis events during game,
- save changed events using saveToFile,
- on next startup add your action/axis events with some default keys,
- use loadFromFile to load previously changed keys.
- Parameters
-
sFileName | Name of the file to load, prefer to have only ASCII characters in the file name. We will load it from a predefined directory using SETTINGS category, the .toml extension will be added if the passed name does not have it. |
- Returns
- Error if something went wrong, this usually means that the file was corrupted.
◆ modifyActionEventKey()
std::optional< Error > ne::InputManager::modifyActionEventKey |
( |
unsigned int |
iActionId, |
|
|
std::variant< KeyboardKey, MouseButton > |
oldKey, |
|
|
std::variant< KeyboardKey, MouseButton > |
newKey |
|
) |
| |
Change action event's key.
- Parameters
-
iActionId | Unique ID of the action event to modify. |
oldKey | Key/button of the specified action event that you want to replace. |
newKey | New key/button that should replace the old key. |
- Returns
- Error if something went wrong.
◆ modifyAxisEventKey()
std::optional< Error > ne::InputManager::modifyAxisEventKey |
( |
unsigned int |
iAxisEventId, |
|
|
std::pair< KeyboardKey, KeyboardKey > |
oldPair, |
|
|
std::pair< KeyboardKey, KeyboardKey > |
newPair |
|
) |
| |
Change axis event's key.
- Parameters
-
iAxisEventId | Unique ID of the axis event to modify. |
oldPair | A pair of buttons of the specified axis event that you want to replace. |
newPair | A new pair of buttons that should replace the old pair. |
- Returns
- Error if something went wrong.
◆ overwriteActionEvent()
std::optional< Error > ne::InputManager::overwriteActionEvent |
( |
unsigned int |
iActionId, |
|
|
const std::vector< std::variant< KeyboardKey, MouseButton > > & |
vKeys |
|
) |
| |
|
private |
Adds a new action event. If an action with this ID already exists it will be removed to register this new action event.
- Warning
- If this action is triggered with an old key right now (when you call this function), there is a chance that this action will be triggered using old keys for the last time (even if after you removed this action). This is because when we receive input key we make a copy of all actions associated with the key and then call these actions, because we operate on a copy, removed elements will be reflected only on the next user input.
- Parameters
-
iActionId | Unique ID of an action event to overwrite. |
vKeys | Keyboard/mouse keys/buttons associated with this action. If empty, no event will be added. |
- Returns
- Error if something went wrong.
◆ overwriteAxisEvent()
std::optional< Error > ne::InputManager::overwriteAxisEvent |
( |
unsigned int |
iAxisEventId, |
|
|
const std::vector< std::pair< KeyboardKey, KeyboardKey > > & |
vAxis |
|
) |
| |
|
private |
Adds a new axis event. If an axis event with this ID already exists it will be removed to register this new axis event.
- Warning
- If this axis event is triggered with an old key right now (when you call this function), there is a chance that this axis event will be triggered using old keys for the last time (even if after you removed this axis event). This is because when we receive input key we make a copy of all axes associated with the key and then call these axes, because we operate on a copy, removed elements will be reflected only on the next user input.
- Parameters
-
iAxisEventId | Unique ID of an axis event to overwrite. |
vAxis | A pair of keyboard buttons associated with this axis, first button will be associated with '+1' input and the second with '-1' input. |
- Returns
- Error if something went wrong.
◆ removeActionEvent()
bool ne::InputManager::removeActionEvent |
( |
unsigned int |
iActionId | ) |
|
Removes an action event with the specified ID.
- Parameters
-
iActionId | Unique ID of the action to remove. |
- Returns
false
if the action was found and removed, true
if not.
◆ removeAxisEvent()
bool ne::InputManager::removeAxisEvent |
( |
unsigned int |
iAxisEventId | ) |
|
Removes an axis event with the specified ID.
- Parameters
-
iAxisEventId | Unique ID of the axis event to remove. |
- Returns
false
if the axis was found and removed, true
if not.
◆ saveToFile()
std::optional< Error > ne::InputManager::saveToFile |
( |
std::string_view |
sFileName | ) |
|
Saves added action/axis events to a file.
- Parameters
-
sFileName | Name of the file to save, prefer to have only ASCII characters in the file name. We will save it to a predefined directory using SETTINGS category, the .toml extension will be added if the passed name does not have it. |
- Returns
- Error if something went wrong.
◆ splitString()
std::vector< std::string > ne::InputManager::splitString |
( |
const std::string & |
sStringToSplit, |
|
|
const std::string & |
sDelimiter |
|
) |
| |
|
static |
Splits the string using a delimiter.
- Parameters
-
sStringToSplit | String to split. |
sDelimiter | Delimiter. |
- Returns
- Splitted string.
◆ actionEvents
std::unordered_map<std::variant<KeyboardKey, MouseButton>, std::vector<unsigned int> > ne::InputManager::actionEvents |
|
private |
Map that stores pairs of "key that triggers event" - "registered events".
TODO: add GamepadKey into variant when gamepad will be supported and save/load them in TODO: saveToFile/loadFromFile (+ add tests).
◆ actionState
std::unordered_map<unsigned int, std::pair<std::vector<ActionState>, bool > > ne::InputManager::actionState |
|
private |
Stores pairs of "action event ID" and a pair of "registered keys" - "last input
(pressed/released)".
◆ axisEvents
std::unordered_map<KeyboardKey, std::vector<std::pair<unsigned int, bool> > > ne::InputManager::axisEvents |
|
private |
Map that stores pair of "keyboard key that triggers event" - "array of registered events
with an input value that should be triggered (either +1 (`true`) or -1 (`false`))".
TODO: add GamepadAxis when gamepad will be supported and save/load TODO: them in saveToFile/loadFromFile (+ add tests).
◆ axisState
std::unordered_map<unsigned int, std::pair<std::vector<AxisState>, int > > ne::InputManager::axisState |
|
private |
Stores pairs of "axis event ID" and a pair of "registered keys" - "last input in
range [-1; 1]".
◆ mtxActionEvents
std::recursive_mutex ne::InputManager::mtxActionEvents |
|
private |
Mutex for actions editing.
◆ mtxAxisEvents
std::recursive_mutex ne::InputManager::mtxAxisEvents |
|
private |
◆ sActionEventSectionName
const std::string_view ne::InputManager::sActionEventSectionName = "action event" |
|
inlinestaticprivate |
Section name to store action events, used in .toml files.
◆ sAxisEventSectionName
const std::string_view ne::InputManager::sAxisEventSectionName = "axis event" |
|
inlinestaticprivate |
Section name to store axis events, used in .toml files.
The documentation for this class was generated from the following files:
- src/engine_lib/public/input/InputManager.h
- src/engine_lib/private/input/InputManager.cpp