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