108 lines
2.5 KiB
C++
108 lines
2.5 KiB
C++
/**
|
|
* @author: Thomas Brandého
|
|
*/
|
|
|
|
#include "keybindings.h"
|
|
#include <iostream>
|
|
|
|
/* Implementation of IKeysMap class
|
|
* @author: Thomas Brandého
|
|
* @info: This class register all the association between key and action.
|
|
*/
|
|
|
|
IKeysMap::IKeysMap()
|
|
{
|
|
|
|
}
|
|
|
|
std::vector<Binding> IKeysMap::getBindings(int action) const {
|
|
std::vector<Binding> bindings;
|
|
for (auto binding : keys)
|
|
if (binding.action == action) bindings.push_back(binding);
|
|
return bindings;
|
|
}
|
|
|
|
/* Implementation of KeyBindings class
|
|
* @author: Thomas Brandého
|
|
* @info: This class map a list of action with the associated keys, for a quick access in-game.
|
|
* The map is read-only, so if you want to add of remove action from the map, you have to create a new instance of KeyBindings
|
|
*/
|
|
|
|
KeyBindings::KeyBindings()
|
|
{
|
|
}
|
|
KeyBindings::KeyBindings(const Context &context, const IKeysMap &keysmap)
|
|
{
|
|
std::vector<int> actions = context.getActions();
|
|
for (int action : actions){
|
|
for (Binding binding : keysmap.getBindings(action)){
|
|
switch(binding.type){
|
|
case IKeysMap::PRESSED:
|
|
setPressedAction(binding.key,binding.action);
|
|
break;
|
|
case IKeysMap::RELEASED:
|
|
setReleasedAction(binding.key,binding.action);
|
|
break;
|
|
case IKeysMap::HOLD:
|
|
setHoldAction(binding.key,binding.action);
|
|
break;
|
|
default:
|
|
//raiseError;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int KeyBindings::getPressedAction(int key) const
|
|
{
|
|
return bindings_pressed.count(key) == 1? bindings_pressed.at(key) : -1;
|
|
}
|
|
int KeyBindings::getReleasedAction(int key) const
|
|
{
|
|
return bindings_released.count(key) == 1? bindings_released.at(key) : -1;
|
|
}
|
|
int KeyBindings::getHoldAction(int key) const
|
|
{
|
|
return bindings_hold.count(key) == 1? bindings_hold.at(key) : -1;
|
|
}
|
|
|
|
void KeyBindings::setPressedAction(int key, int action)
|
|
{
|
|
bindings_pressed[key]=action;
|
|
}
|
|
void KeyBindings::setReleasedAction(int key, int action)
|
|
{
|
|
bindings_released[key]=action;
|
|
}
|
|
|
|
void KeyBindings::setHoldAction(int key, int action)
|
|
{
|
|
bindings_hold[key]=action;
|
|
}
|
|
|
|
/* Implementation of Context class
|
|
* @author: Thomas Brandého
|
|
* @info: This class contains a list of actions available in a given situation (context).
|
|
*/
|
|
|
|
/* Implementation of Context class
|
|
* @author: Thomas Brandého
|
|
* @info: This class contains a list of action associated to a given situation (context).
|
|
*/
|
|
|
|
|
|
Context::Context(std::string _name, std::vector<int> _actions): name(_name), actions(_actions)
|
|
{
|
|
|
|
}
|
|
|
|
std::string Context::getName()
|
|
{
|
|
return name;
|
|
}
|
|
std::vector<int> Context::getActions() const
|
|
{
|
|
return actions;
|
|
}
|