| [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 |  | 
|---|
| [4864] | 25 | #include "globals.h" | 
|---|
| [5944] | 26 | #include "parser/ini_parser/ini_parser.h" | 
|---|
| [7256] | 27 | #include "util/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 | using namespace std; | 
|---|
 | 34 |  | 
|---|
 | 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; | 
|---|
| [4386] | 47 |  | 
|---|
| [4452] | 48 | int KeyMapper::PEV_FIRE1             = EV_UNKNOWN; | 
|---|
 | 49 | int KeyMapper::PEV_FIRE2             = EV_UNKNOWN; | 
|---|
 | 50 | int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN; | 
|---|
 | 51 | int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN; | 
|---|
| [4386] | 52 |  | 
|---|
| [6998] | 53 | int KeyMapper::PEV_CHANGE_SHIP       = EV_UNKNOWN; | 
|---|
 | 54 |  | 
|---|
| [4452] | 55 | int KeyMapper::PEV_VIEW0             = EV_UNKNOWN; | 
|---|
 | 56 | int KeyMapper::PEV_VIEW1             = EV_UNKNOWN; | 
|---|
 | 57 | int KeyMapper::PEV_VIEW2             = EV_UNKNOWN; | 
|---|
 | 58 | int KeyMapper::PEV_VIEW3             = EV_UNKNOWN; | 
|---|
 | 59 | int KeyMapper::PEV_VIEW4             = EV_UNKNOWN; | 
|---|
| [4582] | 60 | int KeyMapper::PEV_VIEW5             = EV_UNKNOWN; | 
|---|
| [4386] | 61 |  | 
|---|
| [4452] | 62 | int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN; | 
|---|
 | 63 | int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN; | 
|---|
| [4386] | 64 |  | 
|---|
| [4452] | 65 | int KeyMapper::PEV_PAUSE             = EV_UNKNOWN; | 
|---|
 | 66 | int KeyMapper::PEV_QUIT              = EV_UNKNOWN; | 
|---|
| [4386] | 67 |  | 
|---|
| [4410] | 68 |  | 
|---|
 | 69 |  | 
|---|
| [4452] | 70 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here | 
|---|
| [4833] | 71 | /** @todo use globals.h for this.... everything is done there for those Options, | 
|---|
| [4834] | 72 |  * and you do not have to care about The namings, as they might change | 
|---|
| [4833] | 73 |  */ | 
|---|
| [7661] | 74 | KeyMapper::KeyMapping KeyMapper::map[] = { | 
|---|
 | 75 |   {&KeyMapper::PEV_FORWARD,              CONFIG_NAME_PLAYER_FORWARD,         SDLK_w}, | 
|---|
 | 76 |   {&KeyMapper::PEV_BACKWARD,             CONFIG_NAME_PLAYER_BACKWARD,        SDLK_s}, | 
|---|
 | 77 |   {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP,              SDLK_r}, | 
|---|
 | 78 |   {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN,            SDLK_f}, | 
|---|
 | 79 |   {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT,            SDLK_a}, | 
|---|
 | 80 |   {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT,           SDLK_d}, | 
|---|
 | 81 |   {&KeyMapper::PEV_ROLL_RIGHT,           CONFIG_NAME_PLAYER_ROLL_LEFT,       SDLK_z}, | 
|---|
 | 82 |   {&KeyMapper::PEV_ROLL_LEFT,            CONFIG_NAME_PLAYER_ROLL_RIGHT,      SDLK_c}, | 
|---|
 | 83 |   {&KeyMapper::PEV_STRAFE_LEFT,          "StrafeLeft",                       SDLK_q}, | 
|---|
 | 84 |   {&KeyMapper::PEV_STRAFE_RIGHT,         "StrafeRight",                      SDLK_e}, | 
|---|
| [4386] | 85 |  | 
|---|
| [7661] | 86 |   {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE,            EV_MOUSE_BUTTON_LEFT}, | 
|---|
 | 87 |   {&KeyMapper::PEV_FIRE2,                "Fire2",                            EV_MOUSE_BUTTON_RIGHT}, | 
|---|
 | 88 |   {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON,     EV_MOUSE_BUTTON_WHEELUP}, | 
|---|
 | 89 |   {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON,     EV_MOUSE_BUTTON_WHEELDOWN}, | 
|---|
| [4412] | 90 |  | 
|---|
| [7661] | 91 |   {&KeyMapper::PEV_CHANGE_SHIP,          CONFIG_NAME_PLAYER_CHANGE_SHIP,     SDLK_g}, | 
|---|
| [4410] | 92 |  | 
|---|
| [6998] | 93 |  | 
|---|
| [7661] | 94 |   {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0,                  SDLK_1}, | 
|---|
 | 95 |   {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1,                  SDLK_2}, | 
|---|
 | 96 |   {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2,                  SDLK_3}, | 
|---|
 | 97 |   {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3,                  SDLK_4}, | 
|---|
 | 98 |   {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4,                  SDLK_5}, | 
|---|
 | 99 |   {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5,                  SDLK_6}, | 
|---|
| [4410] | 100 |  | 
|---|
| [7661] | 101 |   {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD,             SDLK_x}, | 
|---|
 | 102 |   {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD,             SDLK_z}, | 
|---|
| [4582] | 103 |  | 
|---|
| [7661] | 104 |   {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE,                  SDLK_p}, | 
|---|
 | 105 |   {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT,                   SDLK_ESCAPE}, | 
|---|
 | 106 |   {NULL, "", 0} | 
|---|
| [4833] | 107 | }; | 
|---|
| [4400] | 108 |  | 
|---|
 | 109 |  | 
|---|
| [4452] | 110 |  | 
|---|
| [4367] | 111 | /** | 
|---|
| [4836] | 112 |  *  standard constructor | 
|---|
| [4367] | 113 | */ | 
|---|
| [4582] | 114 | KeyMapper::KeyMapper () | 
|---|
| [4367] | 115 | { | 
|---|
| [4582] | 116 |    this->setClassID(CL_KEY_MAPPER, "KeyMapper"); | 
|---|
| [4367] | 117 | } | 
|---|
 | 118 |  | 
|---|
 | 119 |  | 
|---|
 | 120 | /** | 
|---|
| [4836] | 121 |  *  standard deconstructor | 
|---|
| [4367] | 122 | */ | 
|---|
| [4582] | 123 | KeyMapper::~KeyMapper () | 
|---|
| [4367] | 124 | { | 
|---|
 | 125 | } | 
|---|
| [4369] | 126 |  | 
|---|
| [4452] | 127 |  | 
|---|
| [4369] | 128 | /** | 
|---|
| [4836] | 129 |  *  loads new key bindings from a file | 
|---|
 | 130 |  * @param filename: The path and name of the file to load the bindings from | 
|---|
| [4369] | 131 | */ | 
|---|
| [7221] | 132 | void KeyMapper::loadKeyBindings (const std::string& fileName) | 
|---|
| [4369] | 133 | { | 
|---|
| [4866] | 134 |   IniParser parser(fileName); | 
|---|
 | 135 |   this->loadKeyBindings(&parser); | 
|---|
 | 136 | } | 
|---|
| [4582] | 137 |  | 
|---|
| [4866] | 138 | void KeyMapper::loadKeyBindings(IniParser* iniParser) | 
|---|
 | 139 | { | 
|---|
| [7661] | 140 |   if( !iniParser->getSection (CONFIG_SECTION_CONTROL)) | 
|---|
| [4866] | 141 |   { | 
|---|
| [7661] | 142 |     PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n"); | 
|---|
| [4866] | 143 |     return; | 
|---|
 | 144 |   } | 
|---|
| [4369] | 145 |   int* index; | 
|---|
| [4582] | 146 |  | 
|---|
| [5936] | 147 |   iniParser->firstVar(); | 
|---|
| [7256] | 148 |   while( iniParser->getCurrentName() != "" ) | 
|---|
| [4866] | 149 |   { | 
|---|
| [7677] | 150 |     PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName().c_str(), iniParser->getCurrentValue().c_str()); | 
|---|
| [5474] | 151 |     // map the name to an sdl index | 
|---|
| [5014] | 152 |     index = nameToIndex (iniParser->getCurrentValue()); | 
|---|
| [5474] | 153 |     // map the index to a internal name | 
|---|
 | 154 |     this->mapKeys(iniParser->getCurrentName(), index); | 
|---|
| [5015] | 155 |     iniParser->nextVar(); | 
|---|
| [4866] | 156 |   } | 
|---|
| [4369] | 157 |  | 
|---|
 | 158 |  | 
|---|
 | 159 |   // PARSE MISC SECTION | 
|---|
| [7661] | 160 | //   if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) | 
|---|
 | 161 | //   { | 
|---|
 | 162 | //     PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); | 
|---|
 | 163 | //     return; | 
|---|
 | 164 | //   } | 
|---|
 | 165 | // | 
|---|
 | 166 | //   iniParser->firstVar(); | 
|---|
 | 167 | //   while( iniParser->getCurrentName() != "" ) | 
|---|
 | 168 | //   { | 
|---|
 | 169 | //     PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); | 
|---|
 | 170 | //     index = nameToIndex (iniParser->getCurrentValue()); | 
|---|
 | 171 | //     this->mapKeys(iniParser->getCurrentName(), index); | 
|---|
 | 172 | //     iniParser->nextVar(); | 
|---|
 | 173 | //   } | 
|---|
| [4369] | 174 | } | 
|---|
| [4386] | 175 |  | 
|---|
| [7256] | 176 | void KeyMapper::loadKeyBindings() | 
|---|
 | 177 | { | 
|---|
| [7661] | 178 |   if( !Preferences::getInstance()->sectionExists(CONFIG_SECTION_CONTROL)) | 
|---|
| [7256] | 179 |   { | 
|---|
| [7661] | 180 |     PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n"); | 
|---|
| [7256] | 181 |     return; | 
|---|
 | 182 |   } | 
|---|
 | 183 |   int* index; | 
|---|
 | 184 |  | 
|---|
| [7661] | 185 |   std::list<std::string> keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_CONTROL); | 
|---|
| [7256] | 186 |   for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ ) | 
|---|
 | 187 |   { | 
|---|
| [7661] | 188 |     PRINTF(3)("Keys: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "").c_str()); | 
|---|
| [7256] | 189 |     // map the name to an sdl index | 
|---|
| [7661] | 190 |     index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "")); | 
|---|
| [7256] | 191 |     // map the index to a internal name | 
|---|
 | 192 |     this->mapKeys(*it, index); | 
|---|
 | 193 |   } | 
|---|
 | 194 | } | 
|---|
 | 195 |  | 
|---|
| [4452] | 196 | /** | 
|---|
| [4836] | 197 |  *  this function looks up name to key index | 
|---|
 | 198 |  * @param the name of the button | 
|---|
| [4452] | 199 | */ | 
|---|
| [7221] | 200 | int* KeyMapper::nameToIndex (const std::string& name) | 
|---|
| [4386] | 201 | { | 
|---|
 | 202 |   coord[0] = -1; | 
|---|
 | 203 |   coord[1] = -1; | 
|---|
 | 204 |   int c; | 
|---|
| [5474] | 205 |   if( (c = keynameToSDLK (name)) != -1) { | 
|---|
| [4386] | 206 |       coord[1] = c; | 
|---|
 | 207 |       coord[0] = 0; | 
|---|
 | 208 |     } | 
|---|
| [5474] | 209 |   if( (c = buttonnameToSDLB (name)) != -1) { | 
|---|
| [4386] | 210 |       coord[1] = c; | 
|---|
 | 211 |       coord[0] = 1; | 
|---|
 | 212 |     } | 
|---|
 | 213 |   return coord; | 
|---|
 | 214 | } | 
|---|
| [4389] | 215 |  | 
|---|
 | 216 |  | 
|---|
| [4452] | 217 | /** | 
|---|
| [4836] | 218 |  *  the function maps name to key ids | 
|---|
 | 219 |  * @param name of the key | 
|---|
 | 220 |  * @param id of the key | 
|---|
| [4452] | 221 | */ | 
|---|
| [7221] | 222 | void KeyMapper::mapKeys(const std::string& name, int* index) | 
|---|
| [4399] | 223 | { | 
|---|
| [5474] | 224 |   for( int i = 0; map[i].pValue != NULL; ++i ) | 
|---|
| [4399] | 225 |     { | 
|---|
| [7221] | 226 |       if( name == map[i].pName) | 
|---|
| [4582] | 227 |       { | 
|---|
| [5474] | 228 |         if( index[0] == 0) | 
|---|
| [5915] | 229 |         { | 
|---|
| [5474] | 230 |           *map[i].pValue = index[1]; | 
|---|
| [7677] | 231 |           PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLKToKeyname(index[1]).c_str(), index[1]); | 
|---|
| [5474] | 232 |           break; | 
|---|
| [5915] | 233 |         } | 
|---|
 | 234 |         else { | 
|---|
| [5474] | 235 |           *map[i].pValue = index[1]; | 
|---|
| [7677] | 236 |           PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLBToButtonname(index[1]).c_str(), index[1]); | 
|---|
| [5474] | 237 |           break; | 
|---|
| [5915] | 238 |         } | 
|---|
| [4582] | 239 |       } | 
|---|
| [4399] | 240 |     } | 
|---|
 | 241 | } | 
|---|
 | 242 |  | 
|---|
 | 243 |  | 
|---|
| [4452] | 244 | /** | 
|---|
| [4836] | 245 |  *  this function gives some debug information about the key mapper class | 
|---|
| [4452] | 246 | */ | 
|---|
| [4389] | 247 | void KeyMapper::debug() | 
|---|
 | 248 | { | 
|---|
| [4582] | 249 |   PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); | 
|---|
| [4403] | 250 |   for(int i = 0; map[i].pValue != NULL; ++i) | 
|---|
 | 251 |     { | 
|---|
| [7661] | 252 |       PRINT(0)("%s = %i\n",map[i].pName.c_str(), *map[i].pValue); | 
|---|
| [4403] | 253 |     } | 
|---|
| [4582] | 254 |   PRINT(0)("=======================================================\n"); | 
|---|
| [4389] | 255 | } | 
|---|