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