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 | /* initialize all variables to a reasonable value*/ |
---|
30 | int KeyMapper::PEV_UP = EV_UNKNOWN; |
---|
31 | int KeyMapper::PEV_DOWN = EV_UNKNOWN; |
---|
32 | int KeyMapper::PEV_LEFT = EV_UNKNOWN; |
---|
33 | int KeyMapper::PEV_RIGHT = EV_UNKNOWN; |
---|
34 | int KeyMapper::PEV_STRAFE_LEFT = EV_UNKNOWN; |
---|
35 | int KeyMapper::PEV_STRAFE_RIGHT = EV_UNKNOWN; |
---|
36 | |
---|
37 | int KeyMapper::PEV_FIRE1 = EV_UNKNOWN; |
---|
38 | int KeyMapper::PEV_FIRE2 = EV_UNKNOWN; |
---|
39 | int KeyMapper::PEV_PREVIOUS_WEAPON = EV_UNKNOWN; |
---|
40 | int KeyMapper::PEV_NEXT_WEAPON = EV_UNKNOWN; |
---|
41 | |
---|
42 | int KeyMapper::PEV_VIEW0 = EV_UNKNOWN; |
---|
43 | int KeyMapper::PEV_VIEW1 = EV_UNKNOWN; |
---|
44 | int KeyMapper::PEV_VIEW2 = EV_UNKNOWN; |
---|
45 | int KeyMapper::PEV_VIEW3 = EV_UNKNOWN; |
---|
46 | int KeyMapper::PEV_VIEW4 = EV_UNKNOWN; |
---|
47 | int KeyMapper::PEV_VIEW5 = EV_UNKNOWN; |
---|
48 | |
---|
49 | int KeyMapper::PEV_NEXT_WORLD = EV_UNKNOWN; |
---|
50 | int KeyMapper::PEV_PREVIOUS_WORLD = EV_UNKNOWN; |
---|
51 | |
---|
52 | int KeyMapper::PEV_PAUSE = EV_UNKNOWN; |
---|
53 | int KeyMapper::PEV_QUIT = EV_UNKNOWN; |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here |
---|
58 | /** @todo use globals.h for this.... everything is done there for those Options, |
---|
59 | * and you do not have to care about The namings, as they might change |
---|
60 | */ |
---|
61 | orxKeyMapping map[] = { |
---|
62 | {&KeyMapper::PEV_UP, CONFIG_NAME_PLAYER_UP}, |
---|
63 | {&KeyMapper::PEV_DOWN, CONFIG_NAME_PLAYER_DOWN}, |
---|
64 | {&KeyMapper::PEV_LEFT, CONFIG_NAME_PLAYER_LEFT}, |
---|
65 | {&KeyMapper::PEV_RIGHT, CONFIG_NAME_PLAYER_RIGHT}, |
---|
66 | {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"}, |
---|
67 | {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"}, |
---|
68 | |
---|
69 | {&KeyMapper::PEV_FIRE1, CONFIG_NAME_PLAYER_FIRE}, |
---|
70 | {&KeyMapper::PEV_FIRE1, "Fire1"}, |
---|
71 | {&KeyMapper::PEV_FIRE2, "Fire2"}, |
---|
72 | {&KeyMapper::PEV_NEXT_WEAPON, "Next"}, |
---|
73 | {&KeyMapper::PEV_PREVIOUS_WEAPON, "Prev"}, |
---|
74 | |
---|
75 | |
---|
76 | {&KeyMapper::PEV_VIEW0, CONFIG_NAME_VIEW0}, |
---|
77 | {&KeyMapper::PEV_VIEW1, CONFIG_NAME_VIEW1}, |
---|
78 | {&KeyMapper::PEV_VIEW2, CONFIG_NAME_VIEW2}, |
---|
79 | {&KeyMapper::PEV_VIEW3, CONFIG_NAME_VIEW3}, |
---|
80 | {&KeyMapper::PEV_VIEW4, CONFIG_NAME_VIEW4}, |
---|
81 | {&KeyMapper::PEV_VIEW5, CONFIG_NAME_VIEW5}, |
---|
82 | |
---|
83 | {&KeyMapper::PEV_NEXT_WORLD, CONFIG_NAME_NEXT_WORLD}, |
---|
84 | {&KeyMapper::PEV_PREVIOUS_WORLD, CONFIG_NAME_PREV_WORLD}, |
---|
85 | |
---|
86 | {&KeyMapper::PEV_PAUSE, CONFIG_NAME_PAUSE}, |
---|
87 | {&KeyMapper::PEV_QUIT, CONFIG_NAME_QUIT}, |
---|
88 | {NULL, NULL} |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * standard constructor |
---|
95 | */ |
---|
96 | KeyMapper::KeyMapper () |
---|
97 | { |
---|
98 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | * standard deconstructor |
---|
104 | */ |
---|
105 | KeyMapper::~KeyMapper () |
---|
106 | { |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * loads new key bindings from a file |
---|
112 | * @param filename: The path and name of the file to load the bindings from |
---|
113 | */ |
---|
114 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
115 | { |
---|
116 | FILE* stream; |
---|
117 | |
---|
118 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
119 | |
---|
120 | // create parser |
---|
121 | IniParser parser(fileName); |
---|
122 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
123 | { |
---|
124 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
125 | return; |
---|
126 | } |
---|
127 | // allocate empty lookup table |
---|
128 | |
---|
129 | char namebuf[256]; |
---|
130 | char valuebuf[256]; |
---|
131 | memset (namebuf, 0, 256); |
---|
132 | memset (valuebuf, 0, 256); |
---|
133 | int* index; |
---|
134 | |
---|
135 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
136 | { |
---|
137 | PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf); |
---|
138 | index = nameToIndex (valuebuf); |
---|
139 | this->mapKeys(namebuf, index[1]); |
---|
140 | |
---|
141 | /* |
---|
142 | switch( index[0]) |
---|
143 | { |
---|
144 | case 0: |
---|
145 | this->mapKeys(namebuf, index[1]); |
---|
146 | break; |
---|
147 | case 1: |
---|
148 | this->mapKeys(namebuf, index[1]); |
---|
149 | break; |
---|
150 | default: |
---|
151 | break; |
---|
152 | } |
---|
153 | */ |
---|
154 | memset (namebuf, 0, 256); |
---|
155 | memset (valuebuf, 0, 256); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | // PARSE MISC SECTION |
---|
160 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
161 | { |
---|
162 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
163 | return; |
---|
164 | } |
---|
165 | |
---|
166 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
167 | { |
---|
168 | PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf); |
---|
169 | index = nameToIndex (valuebuf); |
---|
170 | this->mapKeys(namebuf, index[1]); |
---|
171 | /* |
---|
172 | switch( index[0]) |
---|
173 | { |
---|
174 | case 0: |
---|
175 | this->mapKeys(namebuf, index[1]); |
---|
176 | break; |
---|
177 | case 1: |
---|
178 | this->mapKeys(namebuf, index[1]); |
---|
179 | break; |
---|
180 | default: |
---|
181 | break; |
---|
182 | } |
---|
183 | */ |
---|
184 | memset (namebuf, 0, 256); |
---|
185 | memset (valuebuf, 0, 256); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /** |
---|
191 | * this function looks up name to key index |
---|
192 | * @param the name of the button |
---|
193 | */ |
---|
194 | int* KeyMapper::nameToIndex (char* name) |
---|
195 | { |
---|
196 | coord[0] = -1; |
---|
197 | coord[1] = -1; |
---|
198 | int c; |
---|
199 | if( (c = keynameToSDLK (name)) != -1) |
---|
200 | { |
---|
201 | coord[1] = c; |
---|
202 | coord[0] = 0; |
---|
203 | } |
---|
204 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
205 | { |
---|
206 | coord[1] = c; |
---|
207 | coord[0] = 1; |
---|
208 | } |
---|
209 | return coord; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /** |
---|
214 | * the function maps name to key ids |
---|
215 | * @param name of the key |
---|
216 | * @param id of the key |
---|
217 | */ |
---|
218 | void KeyMapper::mapKeys(char* name, int keyID) |
---|
219 | { |
---|
220 | for(int i = 0; map[i].pValue != NULL; ++i ) |
---|
221 | { |
---|
222 | if( !strcmp (name, map[i].pName)) |
---|
223 | { |
---|
224 | *map[i].pValue = keyID; |
---|
225 | PRINTF(0)("Mapping %s to id %i\n", name, keyID); |
---|
226 | break; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | * this function gives some debug information about the key mapper class |
---|
234 | */ |
---|
235 | void KeyMapper::debug() |
---|
236 | { |
---|
237 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
238 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
239 | { |
---|
240 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
241 | } |
---|
242 | PRINT(0)("=======================================================\n"); |
---|
243 | } |
---|