[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()); |
---|
| 135 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
| 136 | this->mapKeys(iniParser->getCurrentName(), index[1]); |
---|
[5015] | 137 | iniParser->nextVar(); |
---|
[4866] | 138 | } |
---|
[4369] | 139 | |
---|
| 140 | |
---|
| 141 | // PARSE MISC SECTION |
---|
[5014] | 142 | if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) |
---|
[4866] | 143 | { |
---|
[5014] | 144 | PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); |
---|
[4866] | 145 | return; |
---|
| 146 | } |
---|
[4369] | 147 | |
---|
[5015] | 148 | iniParser->getFirstVar(); |
---|
| 149 | while(iniParser->getCurrentName()) |
---|
[4866] | 150 | { |
---|
[5014] | 151 | PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
| 152 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
| 153 | this->mapKeys(iniParser->getCurrentName(), index[1]); |
---|
[5015] | 154 | iniParser->nextVar(); |
---|
[4866] | 155 | } |
---|
[4369] | 156 | } |
---|
[4386] | 157 | |
---|
[4452] | 158 | /** |
---|
[4836] | 159 | * this function looks up name to key index |
---|
| 160 | * @param the name of the button |
---|
[4452] | 161 | */ |
---|
[5014] | 162 | int* KeyMapper::nameToIndex (const char* name) |
---|
[4386] | 163 | { |
---|
| 164 | coord[0] = -1; |
---|
| 165 | coord[1] = -1; |
---|
| 166 | int c; |
---|
| 167 | if( (c = keynameToSDLK (name)) != -1) |
---|
| 168 | { |
---|
| 169 | coord[1] = c; |
---|
| 170 | coord[0] = 0; |
---|
| 171 | } |
---|
| 172 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
| 173 | { |
---|
| 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 | */ |
---|
[5014] | 186 | void KeyMapper::mapKeys(const char* name, int keyID) |
---|
[4399] | 187 | { |
---|
[4402] | 188 | for(int i = 0; map[i].pValue != NULL; ++i ) |
---|
[4399] | 189 | { |
---|
[4582] | 190 | if( !strcmp (name, map[i].pName)) |
---|
| 191 | { |
---|
| 192 | *map[i].pValue = keyID; |
---|
[5299] | 193 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLKToKeyname(keyID), keyID); |
---|
[4582] | 194 | break; |
---|
| 195 | } |
---|
[4399] | 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
[4452] | 200 | /** |
---|
[4836] | 201 | * this function gives some debug information about the key mapper class |
---|
[4452] | 202 | */ |
---|
[4389] | 203 | void KeyMapper::debug() |
---|
| 204 | { |
---|
[4582] | 205 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
[4403] | 206 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
| 207 | { |
---|
| 208 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
| 209 | } |
---|
[4582] | 210 | PRINT(0)("=======================================================\n"); |
---|
[4389] | 211 | } |
---|