[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" |
---|
| 24 | #include "keynames.h" |
---|
| 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 | |
---|
| 40 | int KeyMapper::PEV_VIEW1 = -1; |
---|
| 41 | int KeyMapper::PEV_VIEW2 = -1; |
---|
| 42 | int KeyMapper::PEV_VIEW3 = -1; |
---|
| 43 | int KeyMapper::PEV_VIEW4 = -1; |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
[4367] | 47 | /** |
---|
| 48 | \brief standard constructor |
---|
| 49 | \todo this constructor is not jet implemented - do it |
---|
| 50 | */ |
---|
| 51 | KeyMapper::KeyMapper () |
---|
| 52 | { |
---|
| 53 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
[4386] | 54 | this->keyAliases = NULL; |
---|
[4367] | 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | \brief standard deconstructor |
---|
| 60 | |
---|
| 61 | */ |
---|
| 62 | KeyMapper::~KeyMapper () |
---|
| 63 | { |
---|
| 64 | // delete what has to be deleted here |
---|
| 65 | } |
---|
[4369] | 66 | |
---|
[4386] | 67 | \ |
---|
[4369] | 68 | /** |
---|
| 69 | \brief loads new key bindings from a file |
---|
| 70 | \param filename: The path and name of the file to load the bindings from |
---|
| 71 | */ |
---|
| 72 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
| 73 | { |
---|
| 74 | FILE* stream; |
---|
| 75 | |
---|
| 76 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
| 77 | |
---|
| 78 | // remove old bindings if present |
---|
| 79 | if( this->keyAliases != NULL) |
---|
| 80 | { |
---|
| 81 | free (this->keyAliases); |
---|
| 82 | this->keyAliases = NULL; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // create parser |
---|
| 86 | IniParser parser(fileName); |
---|
| 87 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
| 88 | { |
---|
| 89 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
| 90 | return; |
---|
| 91 | } |
---|
| 92 | // allocate empty lookup table |
---|
| 93 | this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
| 94 | |
---|
| 95 | char namebuf[256]; |
---|
| 96 | char valuebuf[256]; |
---|
| 97 | memset (namebuf, 0, 256); |
---|
| 98 | memset (valuebuf, 0, 256); |
---|
| 99 | int* index; |
---|
| 100 | |
---|
| 101 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 102 | { |
---|
| 103 | //index = nameToIndex (valuebuf); |
---|
| 104 | int c; |
---|
| 105 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
| 106 | { |
---|
| 107 | index[1] = c; index[0] = 0; |
---|
| 108 | } |
---|
| 109 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
| 110 | { |
---|
| 111 | index[1] = c; index[0] = 1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | switch( index[0]) |
---|
| 115 | { |
---|
| 116 | case 0: |
---|
| 117 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
| 118 | strcpy (this->keyAliases->keys[index[1]], namebuf); |
---|
| 119 | break; |
---|
| 120 | case 1: |
---|
| 121 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
| 122 | strcpy (this->keyAliases->buttons[index[1]], namebuf); |
---|
| 123 | break; |
---|
| 124 | default: |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | memset (namebuf, 0, 256); |
---|
| 128 | memset (valuebuf, 0, 256); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | // PARSE MISC SECTION |
---|
| 133 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
| 134 | { |
---|
| 135 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
| 136 | return; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 140 | { |
---|
[4386] | 141 | index = nameToIndex (valuebuf); |
---|
[4369] | 142 | |
---|
| 143 | switch( index[0]) |
---|
| 144 | { |
---|
| 145 | case 0: |
---|
| 146 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
| 147 | strcpy (keyAliases->keys[index[1]], namebuf); |
---|
| 148 | break; |
---|
| 149 | case 1: |
---|
| 150 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
| 151 | strcpy (keyAliases->buttons[index[1]], namebuf); |
---|
| 152 | break; |
---|
| 153 | default: |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | memset (namebuf, 0, 256); |
---|
| 157 | memset (valuebuf, 0, 256); |
---|
| 158 | } |
---|
| 159 | } |
---|
[4386] | 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | int* KeyMapper::nameToIndex (char* name) |
---|
| 164 | { |
---|
| 165 | coord[0] = -1; |
---|
| 166 | coord[1] = -1; |
---|
| 167 | int c; |
---|
| 168 | if( (c = keynameToSDLK (name)) != -1) |
---|
| 169 | { |
---|
| 170 | coord[1] = c; |
---|
| 171 | coord[0] = 0; |
---|
| 172 | } |
---|
| 173 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
| 174 | { |
---|
| 175 | coord[1] = c; |
---|
| 176 | coord[0] = 1; |
---|
| 177 | } |
---|
| 178 | return coord; |
---|
| 179 | } |
---|