Nameless Engine
Loading...
Searching...
No Matches
ne::InputManager Class Reference

#include <InputManager.h>

Public Member Functions

 InputManager (const InputManager &)=delete
 
InputManageroperator= (const InputManager &)=delete
 
std::optional< ErroraddActionEvent (unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys)
 
std::optional< ErroraddAxisEvent (unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis)
 
std::optional< ErrormodifyActionEventKey (unsigned int iActionId, std::variant< KeyboardKey, MouseButton > oldKey, std::variant< KeyboardKey, MouseButton > newKey)
 
std::optional< ErrormodifyAxisEventKey (unsigned int iAxisEventId, std::pair< KeyboardKey, KeyboardKey > oldPair, std::pair< KeyboardKey, KeyboardKey > newPair)
 
std::optional< ErrorsaveToFile (std::string_view sFileName)
 
std::optional< ErrorloadFromFile (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 Public Member Functions

static std::vector< std::string > splitString (const std::string &sStringToSplit, const std::string &sDelimiter)
 

Private Member Functions

std::optional< ErroroverwriteActionEvent (unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys)
 
std::optional< ErroroverwriteAxisEvent (unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis)
 

Private Attributes

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
 

Static Private Attributes

static const std::string_view sActionEventSectionName = "action event"
 
static const std::string_view sAxisEventSectionName = "axis event"
 

Friends

class GameManager
 

Detailed Description

Allows binding IDs with multiple input keys.

Stored in GameInstance object.

Member Function Documentation

◆ 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
iActionIdUnique ID of the new action event.
vKeysKeyboard/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
iAxisEventIdUnique ID of the new axis event.
vAxisA 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
iActionIdUnique 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
iAxisEventIdUnique 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
iAxisEventIdUnique 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
keyA 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
sFileNameName 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
iActionIdUnique ID of the action event to modify.
oldKeyKey/button of the specified action event that you want to replace.
newKeyNew 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
iAxisEventIdUnique ID of the axis event to modify.
oldPairA pair of buttons of the specified axis event that you want to replace.
newPairA 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
iActionIdUnique ID of an action event to overwrite.
vKeysKeyboard/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
iAxisEventIdUnique ID of an axis event to overwrite.
vAxisA 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
iActionIdUnique 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
iAxisEventIdUnique 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
sFileNameName 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
sStringToSplitString to split.
sDelimiterDelimiter.
Returns
Splitted string.

Member Data Documentation

◆ 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

Mutex for axis editing.

◆ 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: