/** * @author: Thomas Brandého */ #include "keybindings.h" #include /* Implementation of KeysMap class * @author: Thomas Brandého * @info: This class register all the association between key and action. */ KeysMap::KeysMap() { } KeysMap::KeysMap(std::string keysmapping, int nb_actions):keysmapping_file(keysmapping), size_keys_map(nb_actions*2) { keys_map = (int*) malloc(size_keys_map*sizeof(int)); for(int i = 0; i < size_keys_map; i++) keys_map[i] = NO_KEY; actiontype_map = (int*) malloc((size_keys_map/2)*sizeof(int)); for (int i = 0; i < size_keys_map/2 ; i++) actiontype_map[i] = PRESSED; loadKeysMap(); } int KeysMap::getKeyBinding(int action,int num) { return keys_map[action*NB_BINDINGS+num]; } void KeysMap::setKeyBinding(int action, int num, int key) { keys_map[(action*NB_BINDINGS)+num] = key; } int KeysMap::getTypeAction(int action) { return actiontype_map[action]; } void KeysMap::setTypeAction(int action, int type) { actiontype_map[action]=type; } void KeysMap::saveKeysMap() { //TODO: Serialize map_keys //use keysmapping_file } void KeysMap::loadKeysMap() { //TODO: Load map_keys //use keysmapping_file } /* 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(std::vector action_list, KeysMap* keysmap) { int key; for (int action : action_list){ switch(keysmap->getTypeAction(action)){ case KeysMap::PRESSED: for(int i=0; igetKeyBinding(action,i); if (key != NO_KEY) setPressedAction(key,action); } break; case KeysMap::RELEASED: for(int i=0; igetKeyBinding(action,i); if (key != NO_KEY) setReleasedAction(key,action); } break; case KeysMap::HOLD: for(int i=0; igetKeyBinding(action,i); if (key != NO_KEY) setHoldAction(key,action); } break; default: //raiseError; break; } } } int KeyBindings::getPressedAction(int key) { return bindings_pressed.count(key) == 1? bindings_pressed[key] : -1; } int KeyBindings::getReleasedAction(int key) { return bindings_released.count(key) == 1? bindings_released[key] : -1; } int KeyBindings::getHoldAction(int key) { return bindings_hold.count(key) == 1? bindings_hold[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; }