144 lines
3.5 KiB
C++
144 lines
3.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(Action action) const {
|
|
std::vector<Binding> bindings;
|
|
for (auto binding : keys)
|
|
if (binding.action.action == action.action) bindings.push_back(binding);
|
|
return bindings;
|
|
}
|
|
|
|
Action Action::getNull()
|
|
{
|
|
Action a;
|
|
a.action = NO_ACTION;
|
|
a.source = input::NONE;
|
|
return a;
|
|
}
|
|
|
|
Action& Action::operator=(const Action& newAction)
|
|
{
|
|
action = newAction.action;
|
|
source = newAction.source;
|
|
controller_id = newAction.controller_id;
|
|
}
|
|
|
|
Binding& Binding::operator=(const Binding newBinding)
|
|
{
|
|
action = newBinding.action;
|
|
key = newBinding.key;
|
|
type = newBinding.type;
|
|
}
|
|
|
|
|
|
/* 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)
|
|
{
|
|
init_map_bindings();
|
|
for (Action action : context.getActions()){
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Action KeyBindings::getPressedAction(input::Source src, int key) const
|
|
{
|
|
if (bindings_pressed.at(src).count(key))
|
|
return {bindings_pressed.at(src).at(key),src};
|
|
else
|
|
return Action::getNull();
|
|
}
|
|
Action KeyBindings::getReleasedAction(input::Source src, int key) const
|
|
{
|
|
if(bindings_released.at(src).count(key))
|
|
return {bindings_released.at(src).at(key),src};
|
|
else
|
|
return Action::getNull();
|
|
}
|
|
Action KeyBindings::getHoldAction(input::Source src, int key) const
|
|
{
|
|
if(bindings_hold.at(src).count(key))
|
|
return {bindings_hold.at(src).at(key),src};
|
|
else
|
|
return Action::getNull();
|
|
}
|
|
|
|
void KeyBindings::setPressedAction(int key, Action action)
|
|
{
|
|
bindings_pressed[action.source][key]=action.action;
|
|
}
|
|
void KeyBindings::setReleasedAction(int key, Action action)
|
|
{
|
|
bindings_released[action.source][key]=action.action;
|
|
}
|
|
|
|
void KeyBindings::setHoldAction(int key, Action action)
|
|
{
|
|
bindings_hold[action.source][key]=action.action;
|
|
}
|
|
|
|
void KeyBindings::init_map_bindings(){
|
|
for(int i = 0; i < input::SOURCE_COUNT;i++){
|
|
bindings_pressed[i] = std::unordered_map<int,int>();
|
|
bindings_released[i] = std::unordered_map<int,int>();
|
|
bindings_hold[i] = std::unordered_map<int,int>();
|
|
}
|
|
}
|
|
|
|
/* Implementation of Context class
|
|
* @author: Thomas Brandého
|
|
* @info: This class contains a list of actions available in a given situation (context).
|
|
*/
|
|
|
|
Context::Context(std::string _name, std::vector<Action> _actions): name(_name), actions(_actions)
|
|
{
|
|
}
|
|
|
|
std::string Context::getName()
|
|
{
|
|
return name;
|
|
}
|
|
std::vector<Action> Context::getActions() const
|
|
{
|
|
return actions;
|
|
}
|
|
|