| 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 |
|---|
| 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. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
|---|
| 20 | |
|---|
| 21 | #include "key_mapper.h" |
|---|
| 22 | |
|---|
| 23 | #include "ini_parser.h" |
|---|
| 24 | #include "key_names.h" |
|---|
| 25 | |
|---|
| 26 | using namespace std; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 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 | |
|---|
| 40 | int KeyMapper::PEV_VIEW0 = -1; |
|---|
| 41 | int KeyMapper::PEV_VIEW1 = -1; |
|---|
| 42 | int KeyMapper::PEV_VIEW2 = -1; |
|---|
| 43 | int KeyMapper::PEV_VIEW3 = -1; |
|---|
| 44 | int KeyMapper::PEV_VIEW4 = -1; |
|---|
| 45 | int KeyMapper::PEV_VIEW5 = -1; |
|---|
| 46 | |
|---|
| 47 | int KeyMapper::PEV_NEXT_WORLD = -1; |
|---|
| 48 | int KeyMapper::PEV_PREVIOUS_WORLD = -1; |
|---|
| 49 | |
|---|
| 50 | int KeyMapper::PEV_PAUSE = -1; |
|---|
| 51 | int KeyMapper::PEV_QUIT = -1; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 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"}, |
|---|
| 61 | {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"}, |
|---|
| 62 | |
|---|
| 63 | {&KeyMapper::PEV_FIRE1, "Fire"}, |
|---|
| 64 | {&KeyMapper::PEV_FIRE1, "Fire1"}, |
|---|
| 65 | {&KeyMapper::PEV_FIRE2, "Fire2"}, |
|---|
| 66 | |
|---|
| 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"}, |
|---|
| 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"}, |
|---|
| 79 | {NULL, NULL}}; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 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 | } |
|---|
| 100 | |
|---|
| 101 | \ |
|---|
| 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 | { |
|---|
| 129 | PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf); |
|---|
| 130 | index = nameToIndex (valuebuf); |
|---|
| 131 | this->mapKeys(namebuf, index[1]); |
|---|
| 132 | |
|---|
| 133 | /* |
|---|
| 134 | switch( index[0]) |
|---|
| 135 | { |
|---|
| 136 | case 0: |
|---|
| 137 | this->mapKeys(namebuf, index[1]); |
|---|
| 138 | break; |
|---|
| 139 | case 1: |
|---|
| 140 | this->mapKeys(namebuf, index[1]); |
|---|
| 141 | break; |
|---|
| 142 | default: |
|---|
| 143 | break; |
|---|
| 144 | } |
|---|
| 145 | */ |
|---|
| 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 | { |
|---|
| 160 | PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf); |
|---|
| 161 | index = nameToIndex (valuebuf); |
|---|
| 162 | this->mapKeys(namebuf, index[1]); |
|---|
| 163 | /* |
|---|
| 164 | switch( index[0]) |
|---|
| 165 | { |
|---|
| 166 | case 0: |
|---|
| 167 | this->mapKeys(namebuf, index[1]); |
|---|
| 168 | break; |
|---|
| 169 | case 1: |
|---|
| 170 | this->mapKeys(namebuf, index[1]); |
|---|
| 171 | break; |
|---|
| 172 | default: |
|---|
| 173 | break; |
|---|
| 174 | } |
|---|
| 175 | */ |
|---|
| 176 | memset (namebuf, 0, 256); |
|---|
| 177 | memset (valuebuf, 0, 256); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 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 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | void KeyMapper::mapKeys(char* name, int keyID) |
|---|
| 204 | { |
|---|
| 205 | for(int i = 0; map[i].pValue != NULL; ++i ) |
|---|
| 206 | { |
|---|
| 207 | if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;} |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | void KeyMapper::debug() |
|---|
| 213 | { |
|---|
| 214 | //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
|---|
| 215 | |
|---|
| 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 | |
|---|
| 221 | //PRINT(0)("=======================================================\n"); |
|---|
| 222 | } |
|---|