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