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