[4582] | 1 | /* |
---|
[4367] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
[4388] | 13 | co-programmer: Christian Meyer |
---|
[4582] | 14 | |
---|
[4388] | 15 | This code was inspired by the command_node.cc code from Christian Meyer in revision |
---|
| 16 | 4386 and earlier. |
---|
[4367] | 17 | */ |
---|
| 18 | |
---|
| 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
| 20 | |
---|
[4386] | 21 | #include "key_mapper.h" |
---|
[4367] | 22 | |
---|
[5915] | 23 | #include "event_def.h" |
---|
| 24 | |
---|
[10618] | 25 | #include "orxonox_globals.h" |
---|
[5944] | 26 | #include "parser/ini_parser/ini_parser.h" |
---|
[9869] | 27 | #include "parser/preferences/preferences.h" |
---|
[4400] | 28 | #include "key_names.h" |
---|
[7661] | 29 | #include "event_def.h" |
---|
[5075] | 30 | #include "debug.h" |
---|
[4386] | 31 | |
---|
[5915] | 32 | |
---|
[4367] | 33 | |
---|
[9869] | 34 | ObjectListDefinition(KeyMapper); |
---|
[4367] | 35 | |
---|
[4452] | 36 | /* initialize all variables to a reasonable value*/ |
---|
[6997] | 37 | int KeyMapper::PEV_FORWARD = EV_UNKNOWN; |
---|
| 38 | int KeyMapper::PEV_BACKWARD = EV_UNKNOWN; |
---|
[4452] | 39 | int KeyMapper::PEV_LEFT = EV_UNKNOWN; |
---|
| 40 | int KeyMapper::PEV_RIGHT = EV_UNKNOWN; |
---|
[6998] | 41 | int KeyMapper::PEV_UP = EV_UNKNOWN; |
---|
| 42 | int KeyMapper::PEV_DOWN = EV_UNKNOWN; |
---|
[5978] | 43 | int KeyMapper::PEV_ROLL_LEFT = EV_UNKNOWN; |
---|
| 44 | int KeyMapper::PEV_ROLL_RIGHT = EV_UNKNOWN; |
---|
[4452] | 45 | int KeyMapper::PEV_STRAFE_LEFT = EV_UNKNOWN; |
---|
| 46 | int KeyMapper::PEV_STRAFE_RIGHT = EV_UNKNOWN; |
---|
[8724] | 47 | int KeyMapper::PEV_JUMP = EV_UNKNOWN; |
---|
[10638] | 48 | int KeyMapper::PEV_ACTION = EV_UNKNOWN; |
---|
[10698] | 49 | int KeyMapper::PEV_CROUCH = EV_UNKNOWN; |
---|
[4386] | 50 | |
---|
[4452] | 51 | int KeyMapper::PEV_FIRE1 = EV_UNKNOWN; |
---|
| 52 | int KeyMapper::PEV_FIRE2 = EV_UNKNOWN; |
---|
| 53 | int KeyMapper::PEV_PREVIOUS_WEAPON = EV_UNKNOWN; |
---|
| 54 | int KeyMapper::PEV_NEXT_WEAPON = EV_UNKNOWN; |
---|
[4386] | 55 | |
---|
[6998] | 56 | int KeyMapper::PEV_CHANGE_SHIP = EV_UNKNOWN; |
---|
| 57 | |
---|
[4452] | 58 | int KeyMapper::PEV_VIEW0 = EV_UNKNOWN; |
---|
| 59 | int KeyMapper::PEV_VIEW1 = EV_UNKNOWN; |
---|
| 60 | int KeyMapper::PEV_VIEW2 = EV_UNKNOWN; |
---|
| 61 | int KeyMapper::PEV_VIEW3 = EV_UNKNOWN; |
---|
| 62 | int KeyMapper::PEV_VIEW4 = EV_UNKNOWN; |
---|
[4582] | 63 | int KeyMapper::PEV_VIEW5 = EV_UNKNOWN; |
---|
[4386] | 64 | |
---|
[4452] | 65 | int KeyMapper::PEV_NEXT_WORLD = EV_UNKNOWN; |
---|
| 66 | int KeyMapper::PEV_PREVIOUS_WORLD = EV_UNKNOWN; |
---|
[4386] | 67 | |
---|
[4452] | 68 | int KeyMapper::PEV_PAUSE = EV_UNKNOWN; |
---|
| 69 | int KeyMapper::PEV_QUIT = EV_UNKNOWN; |
---|
[4386] | 70 | |
---|
[4410] | 71 | |
---|
| 72 | |
---|
[4452] | 73 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here |
---|
[4833] | 74 | /** @todo use globals.h for this.... everything is done there for those Options, |
---|
[4834] | 75 | * and you do not have to care about The namings, as they might change |
---|
[4833] | 76 | */ |
---|
[7661] | 77 | KeyMapper::KeyMapping KeyMapper::map[] = { |
---|
[9880] | 78 | {&KeyMapper::PEV_FORWARD, CONFIG_NAME_PLAYER_FORWARD, SDLK_w}, |
---|
| 79 | {&KeyMapper::PEV_BACKWARD, CONFIG_NAME_PLAYER_BACKWARD, SDLK_s}, |
---|
| 80 | {&KeyMapper::PEV_UP, CONFIG_NAME_PLAYER_UP, SDLK_r}, |
---|
| 81 | {&KeyMapper::PEV_DOWN, CONFIG_NAME_PLAYER_DOWN, SDLK_f}, |
---|
| 82 | {&KeyMapper::PEV_LEFT, CONFIG_NAME_PLAYER_LEFT, SDLK_a}, |
---|
| 83 | {&KeyMapper::PEV_RIGHT, CONFIG_NAME_PLAYER_RIGHT, SDLK_d}, |
---|
| 84 | {&KeyMapper::PEV_ROLL_RIGHT, CONFIG_NAME_PLAYER_ROLL_LEFT, SDLK_z}, |
---|
| 85 | {&KeyMapper::PEV_ROLL_LEFT, CONFIG_NAME_PLAYER_ROLL_RIGHT, SDLK_c}, |
---|
[10638] | 86 | {&KeyMapper::PEV_STRAFE_LEFT, CONFIG_NAME_PLAYER_STRAFE_LEFT, SDLK_q}, |
---|
| 87 | {&KeyMapper::PEV_STRAFE_RIGHT, CONFIG_NAME_PLAYER_STRAFE_RIGHT, SDLK_e}, |
---|
| 88 | {&KeyMapper::PEV_JUMP, CONFIG_NAME_PLAYER_JUMP, SDLK_SPACE}, |
---|
| 89 | {&KeyMapper::PEV_ACTION, CONFIG_NAME_PLAYER_ACTION, SDLK_u}, |
---|
[10698] | 90 | {&KeyMapper::PEV_CROUCH, CONFIG_NAME_PLAYER_CROUCH, SDLK_LCTRL}, |
---|
[4386] | 91 | |
---|
[10698] | 92 | |
---|
[9880] | 93 | {&KeyMapper::PEV_FIRE1, CONFIG_NAME_PLAYER_FIRE, EV_MOUSE_BUTTON_LEFT}, |
---|
[10638] | 94 | {&KeyMapper::PEV_FIRE2, CONFIG_NAME_PLAYER_FIRE2, EV_MOUSE_BUTTON_RIGHT}, |
---|
[9880] | 95 | {&KeyMapper::PEV_NEXT_WEAPON, CONFIG_NAME_PLAYER_NEXT_WEAPON, EV_MOUSE_BUTTON_WHEELUP}, |
---|
| 96 | {&KeyMapper::PEV_PREVIOUS_WEAPON, CONFIG_NAME_PLAYER_PREV_WEAPON, EV_MOUSE_BUTTON_WHEELDOWN}, |
---|
[4412] | 97 | |
---|
[9880] | 98 | {&KeyMapper::PEV_CHANGE_SHIP, CONFIG_NAME_PLAYER_CHANGE_SHIP, SDLK_g}, |
---|
[4410] | 99 | |
---|
[6998] | 100 | |
---|
[9880] | 101 | {&KeyMapper::PEV_VIEW0, CONFIG_NAME_VIEW0, SDLK_1}, |
---|
| 102 | {&KeyMapper::PEV_VIEW1, CONFIG_NAME_VIEW1, SDLK_2}, |
---|
| 103 | {&KeyMapper::PEV_VIEW2, CONFIG_NAME_VIEW2, SDLK_3}, |
---|
| 104 | {&KeyMapper::PEV_VIEW3, CONFIG_NAME_VIEW3, SDLK_4}, |
---|
| 105 | {&KeyMapper::PEV_VIEW4, CONFIG_NAME_VIEW4, SDLK_5}, |
---|
| 106 | {&KeyMapper::PEV_VIEW5, CONFIG_NAME_VIEW5, SDLK_6}, |
---|
[4410] | 107 | |
---|
[9880] | 108 | {&KeyMapper::PEV_NEXT_WORLD, CONFIG_NAME_NEXT_WORLD, SDLK_x}, |
---|
| 109 | {&KeyMapper::PEV_PREVIOUS_WORLD, CONFIG_NAME_PREV_WORLD, SDLK_z}, |
---|
[4582] | 110 | |
---|
[9880] | 111 | {&KeyMapper::PEV_PAUSE, CONFIG_NAME_PAUSE, SDLK_p}, |
---|
| 112 | {&KeyMapper::PEV_QUIT, CONFIG_NAME_QUIT, SDLK_ESCAPE}, |
---|
| 113 | {NULL, "", 0} |
---|
| 114 | }; |
---|
[4400] | 115 | |
---|
| 116 | |
---|
[4452] | 117 | |
---|
[4367] | 118 | /** |
---|
[4836] | 119 | * standard constructor |
---|
[4367] | 120 | */ |
---|
[4582] | 121 | KeyMapper::KeyMapper () |
---|
[4367] | 122 | { |
---|
[9869] | 123 | this->registerObject(this, KeyMapper::_objectList); |
---|
[4367] | 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | /** |
---|
[4836] | 128 | * standard deconstructor |
---|
[4367] | 129 | */ |
---|
[4582] | 130 | KeyMapper::~KeyMapper () |
---|
[9880] | 131 | {} |
---|
[4369] | 132 | |
---|
[4452] | 133 | |
---|
[4369] | 134 | /** |
---|
[4836] | 135 | * loads new key bindings from a file |
---|
| 136 | * @param filename: The path and name of the file to load the bindings from |
---|
[4369] | 137 | */ |
---|
[7221] | 138 | void KeyMapper::loadKeyBindings (const std::string& fileName) |
---|
[4369] | 139 | { |
---|
[4866] | 140 | IniParser parser(fileName); |
---|
| 141 | this->loadKeyBindings(&parser); |
---|
| 142 | } |
---|
[4582] | 143 | |
---|
[4866] | 144 | void KeyMapper::loadKeyBindings(IniParser* iniParser) |
---|
| 145 | { |
---|
[9880] | 146 | IniParser::Section* section = iniParser->getSection (CONFIG_SECTION_CONTROL); |
---|
| 147 | if( section == NULL) |
---|
[4866] | 148 | { |
---|
[7661] | 149 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n"); |
---|
[4866] | 150 | return; |
---|
| 151 | } |
---|
[4369] | 152 | int* index; |
---|
[4582] | 153 | |
---|
[9880] | 154 | |
---|
| 155 | for(IniParser::Entry::iterator entry = section->begin(); |
---|
| 156 | entry != section->end(); |
---|
| 157 | ++entry) |
---|
[4866] | 158 | { |
---|
[9880] | 159 | PRINTF(3)("Keys: Parsing %s, %s now.\n", (*entry).name().c_str(), (*entry).value().c_str()); |
---|
[5474] | 160 | // map the name to an sdl index |
---|
[9880] | 161 | index = nameToIndex ((*entry).value()); |
---|
[5474] | 162 | // map the index to a internal name |
---|
[9880] | 163 | this->mapKeys((*entry).name(), index); |
---|
[4866] | 164 | } |
---|
[4369] | 165 | |
---|
| 166 | |
---|
| 167 | // PARSE MISC SECTION |
---|
[9880] | 168 | // if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) |
---|
| 169 | // { |
---|
| 170 | // PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); |
---|
| 171 | // return; |
---|
| 172 | // } |
---|
| 173 | // |
---|
| 174 | // iniParser->firstVar(); |
---|
| 175 | // while( iniParser->getCurrentName() != "" ) |
---|
| 176 | // { |
---|
| 177 | // PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
| 178 | // index = nameToIndex (iniParser->getCurrentValue()); |
---|
| 179 | // this->mapKeys(iniParser->getCurrentName(), index); |
---|
| 180 | // iniParser->nextVar(); |
---|
| 181 | // } |
---|
[4369] | 182 | } |
---|
[4386] | 183 | |
---|
[7256] | 184 | void KeyMapper::loadKeyBindings() |
---|
| 185 | { |
---|
[7661] | 186 | if( !Preferences::getInstance()->sectionExists(CONFIG_SECTION_CONTROL)) |
---|
[7256] | 187 | { |
---|
[7661] | 188 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n"); |
---|
[7256] | 189 | return; |
---|
| 190 | } |
---|
| 191 | int* index; |
---|
| 192 | |
---|
[7661] | 193 | std::list<std::string> keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_CONTROL); |
---|
[7256] | 194 | for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ ) |
---|
| 195 | { |
---|
[7661] | 196 | PRINTF(3)("Keys: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "").c_str()); |
---|
[7256] | 197 | // map the name to an sdl index |
---|
[7661] | 198 | index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "")); |
---|
[7256] | 199 | // map the index to a internal name |
---|
| 200 | this->mapKeys(*it, index); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
[4452] | 204 | /** |
---|
[4836] | 205 | * this function looks up name to key index |
---|
| 206 | * @param the name of the button |
---|
[4452] | 207 | */ |
---|
[7221] | 208 | int* KeyMapper::nameToIndex (const std::string& name) |
---|
[4386] | 209 | { |
---|
| 210 | coord[0] = -1; |
---|
| 211 | coord[1] = -1; |
---|
| 212 | int c; |
---|
[9880] | 213 | if( (c = keynameToSDLK (name)) != -1) |
---|
| 214 | { |
---|
| 215 | coord[1] = c; |
---|
| 216 | coord[0] = 0; |
---|
| 217 | } |
---|
| 218 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
| 219 | { |
---|
| 220 | coord[1] = c; |
---|
| 221 | coord[0] = 1; |
---|
| 222 | } |
---|
[4386] | 223 | return coord; |
---|
| 224 | } |
---|
[4389] | 225 | |
---|
| 226 | |
---|
[4452] | 227 | /** |
---|
[4836] | 228 | * the function maps name to key ids |
---|
| 229 | * @param name of the key |
---|
| 230 | * @param id of the key |
---|
[4452] | 231 | */ |
---|
[7221] | 232 | void KeyMapper::mapKeys(const std::string& name, int* index) |
---|
[4399] | 233 | { |
---|
[5474] | 234 | for( int i = 0; map[i].pValue != NULL; ++i ) |
---|
[9880] | 235 | { |
---|
| 236 | if( name == map[i].pName) |
---|
[4399] | 237 | { |
---|
[9880] | 238 | if( index[0] == 0) |
---|
[4582] | 239 | { |
---|
[9880] | 240 | *map[i].pValue = index[1]; |
---|
| 241 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLKToKeyname(index[1]).c_str(), index[1]); |
---|
| 242 | break; |
---|
[4582] | 243 | } |
---|
[9880] | 244 | else |
---|
| 245 | { |
---|
| 246 | *map[i].pValue = index[1]; |
---|
| 247 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLBToButtonname(index[1]).c_str(), index[1]); |
---|
| 248 | break; |
---|
| 249 | } |
---|
[4399] | 250 | } |
---|
[9880] | 251 | } |
---|
[4399] | 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
[4452] | 255 | /** |
---|
[4836] | 256 | * this function gives some debug information about the key mapper class |
---|
[4452] | 257 | */ |
---|
[4389] | 258 | void KeyMapper::debug() |
---|
| 259 | { |
---|
[4582] | 260 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
[4403] | 261 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
[9880] | 262 | { |
---|
| 263 | PRINT(0)("%s = %i\n",map[i].pName.c_str(), *map[i].pValue); |
---|
| 264 | } |
---|
[4582] | 265 | PRINT(0)("=======================================================\n"); |
---|
[4389] | 266 | } |
---|