[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 | |
---|
[4386] | 23 | #include "ini_parser.h" |
---|
[4400] | 24 | #include "key_names.h" |
---|
[4386] | 25 | |
---|
[4367] | 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | |
---|
[4452] | 29 | /* initialize all variables to a reasonable value*/ |
---|
| 30 | int KeyMapper::PEV_UP = EV_UNKNOWN; |
---|
| 31 | int KeyMapper::PEV_DOWN = EV_UNKNOWN; |
---|
| 32 | int KeyMapper::PEV_LEFT = EV_UNKNOWN; |
---|
| 33 | int KeyMapper::PEV_RIGHT = EV_UNKNOWN; |
---|
| 34 | int KeyMapper::PEV_STRAFE_LEFT = EV_UNKNOWN; |
---|
| 35 | int KeyMapper::PEV_STRAFE_RIGHT = EV_UNKNOWN; |
---|
[4386] | 36 | |
---|
[4452] | 37 | int KeyMapper::PEV_FIRE1 = EV_UNKNOWN; |
---|
| 38 | int KeyMapper::PEV_FIRE2 = EV_UNKNOWN; |
---|
| 39 | int KeyMapper::PEV_PREVIOUS_WEAPON = EV_UNKNOWN; |
---|
| 40 | int KeyMapper::PEV_NEXT_WEAPON = EV_UNKNOWN; |
---|
[4386] | 41 | |
---|
[4452] | 42 | int KeyMapper::PEV_VIEW0 = EV_UNKNOWN; |
---|
| 43 | int KeyMapper::PEV_VIEW1 = EV_UNKNOWN; |
---|
| 44 | int KeyMapper::PEV_VIEW2 = EV_UNKNOWN; |
---|
| 45 | int KeyMapper::PEV_VIEW3 = EV_UNKNOWN; |
---|
| 46 | int KeyMapper::PEV_VIEW4 = EV_UNKNOWN; |
---|
[4582] | 47 | int KeyMapper::PEV_VIEW5 = EV_UNKNOWN; |
---|
[4386] | 48 | |
---|
[4452] | 49 | int KeyMapper::PEV_NEXT_WORLD = EV_UNKNOWN; |
---|
| 50 | int KeyMapper::PEV_PREVIOUS_WORLD = EV_UNKNOWN; |
---|
[4386] | 51 | |
---|
[4452] | 52 | int KeyMapper::PEV_PAUSE = EV_UNKNOWN; |
---|
| 53 | int KeyMapper::PEV_QUIT = EV_UNKNOWN; |
---|
[4386] | 54 | |
---|
[4410] | 55 | |
---|
| 56 | |
---|
[4452] | 57 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here |
---|
[4400] | 58 | orxKeyMapping map[] = { {&KeyMapper::PEV_UP, "Up"}, |
---|
[4582] | 59 | {&KeyMapper::PEV_DOWN, "Down"}, |
---|
| 60 | {&KeyMapper::PEV_LEFT, "Left"}, |
---|
| 61 | {&KeyMapper::PEV_RIGHT, "Right"}, |
---|
| 62 | {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"}, |
---|
| 63 | {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"}, |
---|
[4386] | 64 | |
---|
[4582] | 65 | {&KeyMapper::PEV_FIRE1, "Fire"}, |
---|
| 66 | {&KeyMapper::PEV_FIRE1, "Fire1"}, |
---|
| 67 | {&KeyMapper::PEV_FIRE2, "Fire2"}, |
---|
| 68 | {&KeyMapper::PEV_NEXT_WEAPON, "Next"}, |
---|
| 69 | {&KeyMapper::PEV_PREVIOUS_WEAPON, "Prev"}, |
---|
[4412] | 70 | |
---|
[4410] | 71 | |
---|
[4582] | 72 | {&KeyMapper::PEV_VIEW0, "view0"}, |
---|
| 73 | {&KeyMapper::PEV_VIEW1, "view1"}, |
---|
| 74 | {&KeyMapper::PEV_VIEW2, "view2"}, |
---|
| 75 | {&KeyMapper::PEV_VIEW3, "view3"}, |
---|
| 76 | {&KeyMapper::PEV_VIEW4, "view4"}, |
---|
| 77 | {&KeyMapper::PEV_VIEW5, "view5"}, |
---|
[4410] | 78 | |
---|
[4582] | 79 | {&KeyMapper::PEV_NEXT_WORLD, "Next-World"}, |
---|
| 80 | {&KeyMapper::PEV_PREVIOUS_WORLD, "Prev-World"}, |
---|
| 81 | |
---|
| 82 | {&KeyMapper::PEV_PAUSE, "Pause"}, |
---|
| 83 | {&KeyMapper::PEV_QUIT, "Quit"}, |
---|
[4400] | 84 | {NULL, NULL}}; |
---|
| 85 | |
---|
| 86 | |
---|
[4452] | 87 | |
---|
[4367] | 88 | /** |
---|
| 89 | \brief standard constructor |
---|
| 90 | */ |
---|
[4582] | 91 | KeyMapper::KeyMapper () |
---|
[4367] | 92 | { |
---|
[4582] | 93 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
[4367] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | \brief standard deconstructor |
---|
| 99 | */ |
---|
[4582] | 100 | KeyMapper::~KeyMapper () |
---|
[4367] | 101 | { |
---|
| 102 | } |
---|
[4369] | 103 | |
---|
[4452] | 104 | |
---|
[4369] | 105 | /** |
---|
| 106 | \brief loads new key bindings from a file |
---|
| 107 | \param filename: The path and name of the file to load the bindings from |
---|
| 108 | */ |
---|
| 109 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
| 110 | { |
---|
| 111 | FILE* stream; |
---|
[4582] | 112 | |
---|
[4369] | 113 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
[4582] | 114 | |
---|
[4369] | 115 | // create parser |
---|
| 116 | IniParser parser(fileName); |
---|
| 117 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
| 118 | { |
---|
| 119 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
| 120 | return; |
---|
| 121 | } |
---|
| 122 | // allocate empty lookup table |
---|
[4582] | 123 | |
---|
[4369] | 124 | char namebuf[256]; |
---|
| 125 | char valuebuf[256]; |
---|
| 126 | memset (namebuf, 0, 256); |
---|
| 127 | memset (valuebuf, 0, 256); |
---|
| 128 | int* index; |
---|
[4582] | 129 | |
---|
[4369] | 130 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 131 | { |
---|
[4399] | 132 | PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf); |
---|
[4398] | 133 | index = nameToIndex (valuebuf); |
---|
[4403] | 134 | this->mapKeys(namebuf, index[1]); |
---|
[4369] | 135 | |
---|
[4403] | 136 | /* |
---|
[4369] | 137 | switch( index[0]) |
---|
[4582] | 138 | { |
---|
| 139 | case 0: |
---|
| 140 | this->mapKeys(namebuf, index[1]); |
---|
| 141 | break; |
---|
| 142 | case 1: |
---|
| 143 | this->mapKeys(namebuf, index[1]); |
---|
| 144 | break; |
---|
| 145 | default: |
---|
| 146 | break; |
---|
| 147 | } |
---|
[4403] | 148 | */ |
---|
[4369] | 149 | memset (namebuf, 0, 256); |
---|
| 150 | memset (valuebuf, 0, 256); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | // PARSE MISC SECTION |
---|
| 155 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
| 156 | { |
---|
| 157 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
| 158 | return; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 162 | { |
---|
[4399] | 163 | PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf); |
---|
[4582] | 164 | index = nameToIndex (valuebuf); |
---|
[4403] | 165 | this->mapKeys(namebuf, index[1]); |
---|
| 166 | /* |
---|
[4369] | 167 | switch( index[0]) |
---|
[4582] | 168 | { |
---|
| 169 | case 0: |
---|
| 170 | this->mapKeys(namebuf, index[1]); |
---|
| 171 | break; |
---|
| 172 | case 1: |
---|
| 173 | this->mapKeys(namebuf, index[1]); |
---|
| 174 | break; |
---|
| 175 | default: |
---|
| 176 | break; |
---|
| 177 | } |
---|
[4403] | 178 | */ |
---|
[4369] | 179 | memset (namebuf, 0, 256); |
---|
| 180 | memset (valuebuf, 0, 256); |
---|
| 181 | } |
---|
| 182 | } |
---|
[4386] | 183 | |
---|
| 184 | |
---|
[4452] | 185 | /** |
---|
| 186 | \brief this function looks up name to key index |
---|
| 187 | \param the name of the button |
---|
| 188 | */ |
---|
[4386] | 189 | int* KeyMapper::nameToIndex (char* name) |
---|
| 190 | { |
---|
| 191 | coord[0] = -1; |
---|
| 192 | coord[1] = -1; |
---|
| 193 | int c; |
---|
| 194 | if( (c = keynameToSDLK (name)) != -1) |
---|
| 195 | { |
---|
| 196 | coord[1] = c; |
---|
| 197 | coord[0] = 0; |
---|
| 198 | } |
---|
| 199 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
| 200 | { |
---|
| 201 | coord[1] = c; |
---|
| 202 | coord[0] = 1; |
---|
| 203 | } |
---|
| 204 | return coord; |
---|
| 205 | } |
---|
[4389] | 206 | |
---|
| 207 | |
---|
[4452] | 208 | /** |
---|
| 209 | \brief the function maps name to key ids |
---|
| 210 | \param name of the key |
---|
| 211 | \param id of the key |
---|
| 212 | */ |
---|
[4402] | 213 | void KeyMapper::mapKeys(char* name, int keyID) |
---|
[4399] | 214 | { |
---|
[4402] | 215 | for(int i = 0; map[i].pValue != NULL; ++i ) |
---|
[4399] | 216 | { |
---|
[4582] | 217 | if( !strcmp (name, map[i].pName)) |
---|
| 218 | { |
---|
| 219 | *map[i].pValue = keyID; |
---|
| 220 | PRINTF(0)("Mapping %s to id %i\n", name, keyID); |
---|
| 221 | break; |
---|
| 222 | } |
---|
[4399] | 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | |
---|
[4452] | 227 | /** |
---|
| 228 | \brief this function gives some debug information about the key mapper class |
---|
| 229 | */ |
---|
[4389] | 230 | void KeyMapper::debug() |
---|
| 231 | { |
---|
[4582] | 232 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
[4403] | 233 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
| 234 | { |
---|
| 235 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
| 236 | } |
---|
[4582] | 237 | PRINT(0)("=======================================================\n"); |
---|
[4389] | 238 | } |
---|