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