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