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