1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the different input handlers. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "InputHandler.h" |
---|
35 | #include "util/Convert.h" |
---|
36 | #include "util/SubString.h" |
---|
37 | #include "util/String.h" |
---|
38 | #include "Debug.h" |
---|
39 | #include "ConfigValueIncludes.h" |
---|
40 | #include "CoreIncludes.h" |
---|
41 | #include "CommandExecutor.h" |
---|
42 | #include "Executor.h" |
---|
43 | |
---|
44 | namespace orxonox |
---|
45 | { |
---|
46 | // ############################### |
---|
47 | // ###### KeyBinder ###### |
---|
48 | // ############################### |
---|
49 | |
---|
50 | /** |
---|
51 | @brief Constructor that does as little as necessary. |
---|
52 | */ |
---|
53 | KeyBinder::KeyBinder() |
---|
54 | { |
---|
55 | RegisterObject(KeyBinder); |
---|
56 | clearBindings(true); |
---|
57 | |
---|
58 | // keys |
---|
59 | std::string keyNames[] = { |
---|
60 | "UNASSIGNED", |
---|
61 | "ESCAPE", |
---|
62 | "1", |
---|
63 | "2", |
---|
64 | "3", |
---|
65 | "4", |
---|
66 | "5", |
---|
67 | "6", |
---|
68 | "7", |
---|
69 | "8", |
---|
70 | "9", |
---|
71 | "0", |
---|
72 | "MINUS", |
---|
73 | "EQUALS", |
---|
74 | "BACK", |
---|
75 | "TAB", |
---|
76 | "Q", |
---|
77 | "W", |
---|
78 | "E", |
---|
79 | "R", |
---|
80 | "T", |
---|
81 | "Y", |
---|
82 | "U", |
---|
83 | "I", |
---|
84 | "O", |
---|
85 | "P", |
---|
86 | "LBRACKET", |
---|
87 | "RBRACKET", |
---|
88 | "RETURN", |
---|
89 | "LCONTROL", |
---|
90 | "A", |
---|
91 | "S", |
---|
92 | "D", |
---|
93 | "F", |
---|
94 | "G", |
---|
95 | "H", |
---|
96 | "J", |
---|
97 | "K", |
---|
98 | "L", |
---|
99 | "SEMICOLON", |
---|
100 | "APOSTROPHE", |
---|
101 | "GRAVE", |
---|
102 | "LSHIFT", |
---|
103 | "BACKSLASH", |
---|
104 | "Z", |
---|
105 | "X", |
---|
106 | "C", |
---|
107 | "V", |
---|
108 | "B", |
---|
109 | "N", |
---|
110 | "M", |
---|
111 | "COMMA", |
---|
112 | "PERIOD", |
---|
113 | "SLASH", |
---|
114 | "RSHIFT", |
---|
115 | "MULTIPLY", |
---|
116 | "LMENU", |
---|
117 | "SPACE", |
---|
118 | "CAPITAL", |
---|
119 | "F1", |
---|
120 | "F2", |
---|
121 | "F3", |
---|
122 | "F4", |
---|
123 | "F5", |
---|
124 | "F6", |
---|
125 | "F7", |
---|
126 | "F8", |
---|
127 | "F9", |
---|
128 | "F10", |
---|
129 | "NUMLOCK", |
---|
130 | "SCROLL", |
---|
131 | "NUMPAD7", |
---|
132 | "NUMPAD8", |
---|
133 | "NUMPAD9", |
---|
134 | "SUBTRACT", |
---|
135 | "NUMPAD4", |
---|
136 | "NUMPAD5", |
---|
137 | "NUMPAD6", |
---|
138 | "ADD", |
---|
139 | "NUMPAD1", |
---|
140 | "NUMPAD2", |
---|
141 | "NUMPAD3", |
---|
142 | "NUMPAD0", |
---|
143 | "DECIMAL", |
---|
144 | "","", |
---|
145 | "OEM_102", |
---|
146 | "F11", |
---|
147 | "F12", |
---|
148 | "","","","","","","","","","","", |
---|
149 | "F13", |
---|
150 | "F14", |
---|
151 | "F15", |
---|
152 | "","","","","","","","","","", |
---|
153 | "KANA", |
---|
154 | "","", |
---|
155 | "ABNT_C1", |
---|
156 | "","","","","", |
---|
157 | "CONVERT", |
---|
158 | "", |
---|
159 | "NOCONVERT", |
---|
160 | "", |
---|
161 | "YEN", |
---|
162 | "ABNT_C2", |
---|
163 | "","","","","","","","","","","","","","", |
---|
164 | "NUMPADEQUALS", |
---|
165 | "","", |
---|
166 | "PREVTRACK", |
---|
167 | "AT", |
---|
168 | "COLON", |
---|
169 | "UNDERLINE", |
---|
170 | "KANJI", |
---|
171 | "STOP", |
---|
172 | "AX", |
---|
173 | "UNLABELED", |
---|
174 | "NEXTTRACK", |
---|
175 | "","", |
---|
176 | "NUMPADENTER", |
---|
177 | "RCONTROL", |
---|
178 | "","", |
---|
179 | "MUTE", |
---|
180 | "CALCULATOR", |
---|
181 | "PLAYPAUSE", |
---|
182 | "", |
---|
183 | "MEDIASTOP", |
---|
184 | "","","","","","","","","", |
---|
185 | "VOLUMEDOWN", |
---|
186 | "", |
---|
187 | "VOLUMEUP", |
---|
188 | "", |
---|
189 | "WEBHOME", |
---|
190 | "NUMPADCOMMA", |
---|
191 | "", |
---|
192 | "DIVIDE", |
---|
193 | "", |
---|
194 | "SYSRQ", |
---|
195 | "RMENU", |
---|
196 | "","","","","","","","","","","","", |
---|
197 | "PAUSE", |
---|
198 | "", |
---|
199 | "HOME", |
---|
200 | "UP", |
---|
201 | "PGUP", |
---|
202 | "", |
---|
203 | "LEFT", |
---|
204 | "", |
---|
205 | "RIGHT", |
---|
206 | "", |
---|
207 | "END", |
---|
208 | "DOWN", |
---|
209 | "PGDOWN", |
---|
210 | "INSERT", |
---|
211 | "DELETE", |
---|
212 | "","","","","","","", |
---|
213 | "LWIN", |
---|
214 | "RWIN", |
---|
215 | "APPS", |
---|
216 | "POWER", |
---|
217 | "SLEEP", |
---|
218 | "","","", |
---|
219 | "WAKE", |
---|
220 | "", |
---|
221 | "WEBSEARCH", |
---|
222 | "WEBFAVORITES", |
---|
223 | "WEBREFRESH", |
---|
224 | "WEBSTOP", |
---|
225 | "WEBFORWARD", |
---|
226 | "WEBBACK", |
---|
227 | "MYCOMPUTER", |
---|
228 | "MAIL", |
---|
229 | "MEDIASELECT" |
---|
230 | }; |
---|
231 | for (int i = 0; i < nKeys_s; i++) |
---|
232 | namesKeys_[i] = "Key" + keyNames[i]; |
---|
233 | |
---|
234 | // mouse buttons |
---|
235 | std::string mouseButtonNames[] = { |
---|
236 | "MouseLeft", "MouseRight", "MouseMiddle", |
---|
237 | "MouseButton3", "MouseButton4", "MouseButton5", |
---|
238 | "MouseButton6", "MouseButton7" }; |
---|
239 | for (int i = 0; i < nMouseButtons_s; i++) |
---|
240 | namesMouseButtons_[i] = mouseButtonNames[i]; |
---|
241 | |
---|
242 | // joy stick buttons |
---|
243 | for (int i = 0; i < 32; i++) |
---|
244 | namesJoyStickButtons_[i] = "JoyButton" + getConvertedValue<int, std::string>(i); |
---|
245 | for (int i = 32; i < nJoyStickButtons_s; i += 4) |
---|
246 | { |
---|
247 | namesJoyStickButtons_[i + 0] = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North"; |
---|
248 | namesJoyStickButtons_[i + 1] = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South"; |
---|
249 | namesJoyStickButtons_[i + 2] = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East"; |
---|
250 | namesJoyStickButtons_[i + 3] = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West"; |
---|
251 | } |
---|
252 | |
---|
253 | // half axes |
---|
254 | std::string rawNames[nHalfAxes_s/2]; |
---|
255 | rawNames[0] = "MouseX"; |
---|
256 | rawNames[1] = "MouseY"; |
---|
257 | rawNames[2] = "MouseWheel1"; |
---|
258 | rawNames[3] = "MouseWheel2"; |
---|
259 | for (unsigned int i = 4; i < nHalfAxes_s/2; i++) |
---|
260 | rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3); |
---|
261 | for (unsigned int i = 0; i < nHalfAxes_s/2; i++) |
---|
262 | { |
---|
263 | namesHalfAxes_[i * 2 + 0] = rawNames[i] + "Pos"; |
---|
264 | namesHalfAxes_[i * 2 + 1] = rawNames[i] + "Neg"; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | @brief Destructor |
---|
270 | */ |
---|
271 | KeyBinder::~KeyBinder() |
---|
272 | { |
---|
273 | // almost no destructors required because most of the arrays are static. |
---|
274 | clearBindings(); // does some destruction work |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | @brief Loader for the key bindings, managed by config values. |
---|
279 | */ |
---|
280 | void KeyBinder::setConfigValues() |
---|
281 | { |
---|
282 | bool success = true; |
---|
283 | // keys |
---|
284 | success |= readBindings(namesKeys_, bindingStringsKeys_, bindingsKeys_, nKeys_s); |
---|
285 | // mouse buttons |
---|
286 | success |= readBindings(namesMouseButtons_, bindingStringsMouseButtons_, bindingsMouseButtons_, nMouseButtons_s); |
---|
287 | // joy stick buttons |
---|
288 | success |= readBindings(namesJoyStickButtons_, bindingStringsJoyStickButtons_, |
---|
289 | bindingsJoyStickButtons_, nJoyStickButtons_s); |
---|
290 | // half axes |
---|
291 | success |= readBindings(namesHalfAxes_, bindingStringsHalfAxes_, bindingsHalfAxes_, nHalfAxes_s); |
---|
292 | |
---|
293 | // TODO: what happens if parsing didn't succeed in all parts? nothing? |
---|
294 | } |
---|
295 | |
---|
296 | bool KeyBinder::readBindings(std::string* names, std::string* bindingStrings, |
---|
297 | KeyBindingBundle* bindings, unsigned int size) |
---|
298 | { |
---|
299 | for (unsigned int i = 0; i < size; i++) |
---|
300 | { |
---|
301 | // config value stuff |
---|
302 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(names[i]); |
---|
303 | if (!cont) |
---|
304 | { |
---|
305 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), names[i], ""); |
---|
306 | getIdentifier()->addConfigValueContainer(names[i], cont); |
---|
307 | } |
---|
308 | std::string old = bindingStrings[i]; |
---|
309 | cont->getValue(&bindingStrings[i]); |
---|
310 | |
---|
311 | // keybinder stuff |
---|
312 | if (old != bindingStrings[i]) |
---|
313 | { |
---|
314 | // binding has changed |
---|
315 | if (bindingStrings[i] == "") |
---|
316 | { |
---|
317 | // empty binding, occurs at least the first time since init value is " " |
---|
318 | bindings[i].OnPress.clear(); |
---|
319 | bindings[i].OnRelease.clear(); |
---|
320 | bindings[i].OnHold.clear(); |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | // actually parse the command(s) |
---|
325 | SubString commands(bindingStrings[i], "|", SubString::WhiteSpaces, false, |
---|
326 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
327 | bindings[i].OnHold.nCommands = 0; |
---|
328 | bindings[i].OnHold.commands = new SimpleCommand[64]; |
---|
329 | bindings[i].OnPress.nCommands = 0; |
---|
330 | bindings[i].OnPress.commands = new SimpleCommand[64]; |
---|
331 | bindings[i].OnRelease.nCommands = 0; |
---|
332 | bindings[i].OnRelease.commands = new SimpleCommand[64]; |
---|
333 | for (unsigned int iCommand = 0; iCommand < commands.size(); iCommand++) |
---|
334 | { |
---|
335 | if (commands[iCommand] != "") |
---|
336 | { |
---|
337 | SubString tokens(commands[iCommand], " ", SubString::WhiteSpaces, false, |
---|
338 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
339 | |
---|
340 | unsigned int iToken = 0; |
---|
341 | |
---|
342 | // first argument can be OnPress, OnHold OnRelease or nothing |
---|
343 | KeybindMode::Enum mode = KeybindMode::None; |
---|
344 | if (getLowercase(tokens[iToken]) == "onpress") |
---|
345 | mode = KeybindMode::OnPress, iToken++; |
---|
346 | if (getLowercase(tokens[iToken]) == "onrelease") |
---|
347 | mode = KeybindMode::OnRelease, iToken++; |
---|
348 | if (getLowercase(tokens[iToken]) == "onhold") |
---|
349 | mode = KeybindMode::OnHold, iToken++; |
---|
350 | |
---|
351 | if (iToken == tokens.size()) |
---|
352 | continue; |
---|
353 | |
---|
354 | SimpleCommand* cmd = new SimpleCommand(); |
---|
355 | |
---|
356 | // second argument can be the amplitude for the case it as an axis command |
---|
357 | // default amplitude is 1.0f |
---|
358 | if (getLowercase(tokens[iToken]) == "axisamp") |
---|
359 | { |
---|
360 | iToken++; |
---|
361 | float value; |
---|
362 | if (iToken == tokens.size() || !convertValue(&value, tokens[iToken])) |
---|
363 | { |
---|
364 | CCOUT(2) << "Error while parsing key binding " << names[i] |
---|
365 | << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl; |
---|
366 | if (iToken == tokens.size()) |
---|
367 | { |
---|
368 | delete cmd; |
---|
369 | continue; |
---|
370 | } |
---|
371 | cmd->axisModifier = 1.0f; |
---|
372 | } |
---|
373 | else |
---|
374 | cmd->axisModifier = value; |
---|
375 | iToken++; |
---|
376 | } |
---|
377 | else |
---|
378 | cmd->axisModifier = 1.0f; |
---|
379 | |
---|
380 | // no more arguments expected except for the actual command |
---|
381 | if (iToken == tokens.size()) |
---|
382 | { // no command given |
---|
383 | delete cmd; |
---|
384 | continue; |
---|
385 | } |
---|
386 | while (iToken != tokens.size()) |
---|
387 | cmd->commandStr += tokens[iToken++] + " "; |
---|
388 | |
---|
389 | // check whether we exceed 64 commands... |
---|
390 | if (bindings[i].OnHold.nCommands == 64 || bindings[i].OnPress.nCommands == 64 |
---|
391 | || bindings[i].OnRelease.nCommands == 64) |
---|
392 | { |
---|
393 | CCOUT(2) << "Error while parsing key binding " << names[i] |
---|
394 | << ". You shouldn't assign more than 64 key bindings to one key " |
---|
395 | << "just to test the parser" << std::endl; |
---|
396 | } |
---|
397 | |
---|
398 | // evaluate the command |
---|
399 | cmd->axisCommand = 0; |
---|
400 | CommandEvaluation& eval = CommandExecutor::evaluate(cmd->commandStr); |
---|
401 | // TOOD: check for axis command |
---|
402 | if (false) |
---|
403 | { |
---|
404 | cmd->axisCommand->commandStr = cmd->commandStr; |
---|
405 | cmd->commandStr = ""; |
---|
406 | cmd->axisCommand->evaluation = eval; |
---|
407 | // add command to the buffer if not yet existing |
---|
408 | for (unsigned int iAxisCmd = 0; iAxisCmd < axisCommands_.size(); iAxisCmd++) |
---|
409 | { |
---|
410 | if (getLowercase(axisCommands_[iAxisCmd]->commandStr) == getLowercase(cmd->commandStr)) |
---|
411 | { |
---|
412 | // already in list |
---|
413 | cmd->axisCommand = axisCommands_[iAxisCmd]; |
---|
414 | break; |
---|
415 | } |
---|
416 | } |
---|
417 | if (cmd->axisCommand == 0) |
---|
418 | { |
---|
419 | cmd->axisCommand = new AxisCommand(); |
---|
420 | axisCommands_.push_back(cmd->axisCommand); |
---|
421 | } |
---|
422 | // TODO: check for relative/absolute command |
---|
423 | cmd->axisCommand->bRelative = false; |
---|
424 | |
---|
425 | // axis commands are always OnHold |
---|
426 | *(bindings[i].OnHold.commands + bindings[i].OnHold.nCommands++) = *cmd; |
---|
427 | } |
---|
428 | else |
---|
429 | { |
---|
430 | cmd->evaluation = eval; |
---|
431 | |
---|
432 | // TODO: determine whether the command is OnHold, OnPress or OnRelease |
---|
433 | switch (mode) |
---|
434 | { |
---|
435 | case KeybindMode::None: |
---|
436 | *(bindings[i].OnPress.commands + bindings[i].OnPress.nCommands++) = *cmd; |
---|
437 | break; |
---|
438 | case KeybindMode::OnPress: |
---|
439 | *(bindings[i].OnPress.commands + bindings[i].OnPress.nCommands++) = *cmd; |
---|
440 | break; |
---|
441 | case KeybindMode::OnHold: |
---|
442 | *(bindings[i].OnHold.commands + bindings[i].OnHold.nCommands++) = *cmd; |
---|
443 | break; |
---|
444 | case KeybindMode::OnRelease: |
---|
445 | *(bindings[i].OnRelease.commands + bindings[i].OnRelease.nCommands++) = *cmd; |
---|
446 | break; |
---|
447 | } |
---|
448 | } |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | // redimension arrays with simple commands |
---|
453 | SimpleCommand* sCmd = bindings[i].OnHold.commands; |
---|
454 | if (bindings[i].OnHold.nCommands) |
---|
455 | { |
---|
456 | bindings[i].OnHold.commands = new SimpleCommand[bindings[i].OnHold.nCommands]; |
---|
457 | for (unsigned int iCmd = 0; iCmd < bindings[i].OnHold.nCommands; iCmd++) |
---|
458 | bindings[i].OnHold.commands[iCmd] = sCmd[iCmd]; |
---|
459 | } |
---|
460 | else |
---|
461 | bindings[i].OnHold.commands = 0; |
---|
462 | delete[] sCmd; |
---|
463 | |
---|
464 | sCmd = bindings[i].OnPress.commands; |
---|
465 | if (bindings[i].OnPress.nCommands) |
---|
466 | { |
---|
467 | bindings[i].OnPress.commands = new SimpleCommand[bindings[i].OnPress.nCommands]; |
---|
468 | for (unsigned int iCmd = 0; iCmd < bindings[i].OnPress.nCommands; iCmd++) |
---|
469 | bindings[i].OnPress.commands[iCmd] = sCmd[iCmd]; |
---|
470 | } |
---|
471 | else |
---|
472 | bindings[i].OnPress.commands = 0; |
---|
473 | delete[] sCmd; |
---|
474 | |
---|
475 | sCmd = bindings[i].OnRelease.commands; |
---|
476 | if (bindings[i].OnRelease.nCommands) |
---|
477 | { |
---|
478 | bindings[i].OnRelease.commands = new SimpleCommand[bindings[i].OnRelease.nCommands]; |
---|
479 | for (unsigned int iCmd = 0; iCmd < bindings[i].OnRelease.nCommands; iCmd++) |
---|
480 | bindings[i].OnRelease.commands[iCmd] = sCmd[iCmd]; |
---|
481 | } |
---|
482 | else |
---|
483 | bindings[i].OnRelease.commands = 0; |
---|
484 | delete[] sCmd; |
---|
485 | } |
---|
486 | } |
---|
487 | } |
---|
488 | return true; |
---|
489 | } |
---|
490 | |
---|
491 | /** |
---|
492 | @brief Overwrites all bindings with "" |
---|
493 | */ |
---|
494 | void KeyBinder::clearBindings(bool bInit) |
---|
495 | { |
---|
496 | for (int i = 0; i < nKeys_s; i++) |
---|
497 | { |
---|
498 | clearBundle(bindingsKeys_[i], bInit); |
---|
499 | bindingStringsKeys_[i] = " "; |
---|
500 | } |
---|
501 | for (int i = 0; i < nMouseButtons_s; i++) |
---|
502 | { |
---|
503 | clearBundle(bindingsMouseButtons_[i], bInit); |
---|
504 | bindingStringsMouseButtons_[i] = " "; |
---|
505 | } |
---|
506 | for (int i = 0; i < nJoyStickButtons_s; i++) |
---|
507 | { |
---|
508 | clearBundle(bindingsJoyStickButtons_[i], bInit); |
---|
509 | bindingStringsJoyStickButtons_[i] = " "; |
---|
510 | } |
---|
511 | for (int i = 0; i < nHalfAxes_s; i++) |
---|
512 | { |
---|
513 | clearBundle(bindingsHalfAxes_[i], bInit); |
---|
514 | bindingStringsHalfAxes_[i] = " "; |
---|
515 | } |
---|
516 | for (unsigned int i = 0; i < axisCommands_.size(); i++) |
---|
517 | delete axisCommands_[i]; |
---|
518 | axisCommands_.clear(); |
---|
519 | } |
---|
520 | |
---|
521 | void KeyBinder::clearBundle(KeyBindingBundle& bundle, bool bInit) |
---|
522 | { |
---|
523 | if (!bInit) |
---|
524 | { |
---|
525 | if (bundle.OnHold.nCommands) |
---|
526 | delete[] bundle.OnHold.commands; |
---|
527 | if (bundle.OnPress.nCommands) |
---|
528 | delete[] bundle.OnPress.commands; |
---|
529 | if (bundle.OnRelease.nCommands) |
---|
530 | delete[] bundle.OnRelease.commands; |
---|
531 | } |
---|
532 | bundle.OnPress.nCommands = 0; |
---|
533 | bundle.OnHold.nCommands = 0; |
---|
534 | bundle.OnRelease.nCommands = 0; |
---|
535 | } |
---|
536 | |
---|
537 | /** |
---|
538 | @brief Loads the key and button bindings. |
---|
539 | @return True if loading succeeded. |
---|
540 | */ |
---|
541 | bool KeyBinder::loadBindings() |
---|
542 | { |
---|
543 | COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; |
---|
544 | |
---|
545 | // clear half axes |
---|
546 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
---|
547 | { |
---|
548 | halfAxes_[i].hasChanged = false; |
---|
549 | halfAxes_[i].abs = 0.0f; |
---|
550 | halfAxes_[i].rel = 0.0f; |
---|
551 | halfAxes_[i].wasDown = false; |
---|
552 | halfAxes_[i].threshold = 0.01f; |
---|
553 | } |
---|
554 | |
---|
555 | ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); |
---|
556 | clearBindings(); |
---|
557 | setConfigValues(); |
---|
558 | |
---|
559 | COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; |
---|
560 | return true; |
---|
561 | } |
---|
562 | |
---|
563 | void KeyBinder::tick(float dt) |
---|
564 | { |
---|
565 | // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event. |
---|
566 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
---|
567 | { |
---|
568 | if (!halfAxes_[i].hasChanged) |
---|
569 | { |
---|
570 | if (!halfAxes_[i].wasDown && halfAxes_[i].abs > halfAxes_[i].threshold) |
---|
571 | { |
---|
572 | halfAxes_[i].wasDown = true; |
---|
573 | if (bindingsHalfAxes_[i].OnPress.nCommands) |
---|
574 | executeBinding(bindingsHalfAxes_[i].OnPress, halfAxes_[i].rel, halfAxes_[i].abs); |
---|
575 | } |
---|
576 | else if (halfAxes_[i].wasDown && halfAxes_[i].abs < halfAxes_[i].threshold) |
---|
577 | { |
---|
578 | halfAxes_[i].wasDown = false; |
---|
579 | if (bindingsHalfAxes_[i].OnRelease.nCommands) |
---|
580 | executeBinding(bindingsHalfAxes_[i].OnRelease, halfAxes_[i].rel, halfAxes_[i].abs); |
---|
581 | } |
---|
582 | if (halfAxes_[i].wasDown) |
---|
583 | { |
---|
584 | executeBinding(bindingsHalfAxes_[i].OnHold, halfAxes_[i].rel, halfAxes_[i].abs); |
---|
585 | } |
---|
586 | halfAxes_[i].hasChanged = false; |
---|
587 | } |
---|
588 | } |
---|
589 | |
---|
590 | // execute all buffered bindings (addional parameter) |
---|
591 | for (unsigned int i = 0; i < axisCommands_.size(); i++) |
---|
592 | { |
---|
593 | if (axisCommands_[i]->nValuesAdded > 0) |
---|
594 | { |
---|
595 | axisCommands_[i]->evaluation.setEvaluatedParameter(0, axisCommands_[i]->value); |
---|
596 | // reset |
---|
597 | axisCommands_[i]->nValuesAdded = 0; |
---|
598 | axisCommands_[i]->value = 0.0f; |
---|
599 | } |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | bool KeyBinder::executeBinding(KeyBinding& binding, float axisRel, float axisAbs) |
---|
604 | { |
---|
605 | // execute all the parsed commands in the string |
---|
606 | for (unsigned int iCommand = 0; iCommand < binding.nCommands; iCommand++) |
---|
607 | { |
---|
608 | SimpleCommand& command = binding.commands[iCommand]; |
---|
609 | if (command.axisCommand) |
---|
610 | { |
---|
611 | AxisCommand& axisCommand = *command.axisCommand; |
---|
612 | // command has an additional parameter |
---|
613 | if (command.axisCommand->bRelative) |
---|
614 | { |
---|
615 | // we have to calculate a relative movement. |
---|
616 | // amplitude says how much one keystroke is |
---|
617 | axisCommand.value += command.axisModifier * axisRel; |
---|
618 | } |
---|
619 | else |
---|
620 | { |
---|
621 | // we have to calculate absolute position of the axis. |
---|
622 | // for a key this simply is 1, but multiplied by a user defined factor |
---|
623 | // since there might be another axis that is affected, we have to wait and |
---|
624 | // store the result in a temporary place |
---|
625 | axisCommand.value = |
---|
626 | (axisCommand.value * (axisCommand.nValuesAdded++) + command.axisModifier * axisAbs) |
---|
627 | / axisCommand.nValuesAdded; |
---|
628 | } |
---|
629 | } |
---|
630 | else |
---|
631 | { |
---|
632 | // simple command, just execute directly |
---|
633 | // TODO: calculate whether this a Press, Release or Hold event |
---|
634 | CommandExecutor::execute(command.evaluation); |
---|
635 | } |
---|
636 | } |
---|
637 | return true; |
---|
638 | } |
---|
639 | |
---|
640 | |
---|
641 | /** |
---|
642 | @brief Event handler for the keyPressed Event. |
---|
643 | @param e Event information |
---|
644 | */ |
---|
645 | bool KeyBinder::keyPressed(const KeyEvent& evt) |
---|
646 | { |
---|
647 | // find the appropriate key binding |
---|
648 | executeBinding(bindingsKeys_[int(evt.key)].OnPress, 1.0, 1.0); |
---|
649 | |
---|
650 | return true; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | @brief Event handler for the keyReleased Event. |
---|
655 | @param e Event information |
---|
656 | */ |
---|
657 | bool KeyBinder::keyReleased(const KeyEvent& evt) |
---|
658 | { |
---|
659 | // find the appropriate key binding |
---|
660 | executeBinding(bindingsKeys_[int(evt.key)].OnRelease, 1.0, 1.0); |
---|
661 | |
---|
662 | return true; |
---|
663 | } |
---|
664 | |
---|
665 | /** |
---|
666 | @brief Event handler for the keyHeld Event. |
---|
667 | @param e Mouse state information |
---|
668 | */ |
---|
669 | bool KeyBinder::keyHeld(const KeyEvent& evt) |
---|
670 | { |
---|
671 | // find the appropriate key binding |
---|
672 | executeBinding(bindingsKeys_[int(evt.key)].OnHold, 1.0, 1.0); |
---|
673 | |
---|
674 | return true; |
---|
675 | } |
---|
676 | |
---|
677 | /** |
---|
678 | @brief Event handler for the mouseMoved Event. |
---|
679 | @param e Mouse state information |
---|
680 | */ |
---|
681 | bool KeyBinder::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
682 | { |
---|
683 | halfAxes_[0].hasChanged = true; |
---|
684 | halfAxes_[1].hasChanged = true; |
---|
685 | halfAxes_[2].hasChanged = true; |
---|
686 | halfAxes_[3].hasChanged = true; |
---|
687 | // translate absolute mouse position into joystick like behaviour |
---|
688 | if (clippingSize.x > clippingSize.y) |
---|
689 | { |
---|
690 | int margin = (clippingSize.x - clippingSize.y) / 2; |
---|
691 | if (abs.x - margin > clippingSize.y) |
---|
692 | { |
---|
693 | halfAxes_[0].abs = 1.0f; |
---|
694 | halfAxes_[1].abs = 0.0f; |
---|
695 | } |
---|
696 | else if (abs.x < margin) |
---|
697 | { |
---|
698 | halfAxes_[0].abs = 0.0f; |
---|
699 | halfAxes_[1].abs = 1.0f; |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | float temp = ((float)abs.x) / clippingSize.y * 2 - 1; |
---|
704 | if (temp > 0) |
---|
705 | { |
---|
706 | halfAxes_[0].abs = temp; |
---|
707 | halfAxes_[1].abs = 0.0f; |
---|
708 | } |
---|
709 | else |
---|
710 | { |
---|
711 | halfAxes_[0].abs = 0.0f; |
---|
712 | halfAxes_[1].abs = -temp; |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | float temp = -((float)abs.y) / clippingSize.y * 2 + 1; |
---|
717 | if (temp > 0) |
---|
718 | { |
---|
719 | halfAxes_[2].abs = temp; |
---|
720 | halfAxes_[3].abs = 0.0; |
---|
721 | } |
---|
722 | else |
---|
723 | { |
---|
724 | halfAxes_[2].abs = 0.0; |
---|
725 | halfAxes_[3].abs = -temp; |
---|
726 | } |
---|
727 | } |
---|
728 | else |
---|
729 | { |
---|
730 | float temp = ((float)abs.x) / clippingSize.x * 2 - 1; |
---|
731 | if (temp > 0) |
---|
732 | { |
---|
733 | halfAxes_[0].abs = temp; |
---|
734 | halfAxes_[1].abs = 0.0; |
---|
735 | } |
---|
736 | else |
---|
737 | { |
---|
738 | halfAxes_[0].abs = 0.0; |
---|
739 | halfAxes_[1].abs = -temp; |
---|
740 | } |
---|
741 | |
---|
742 | int margin = (clippingSize.y - clippingSize.x) / 2; |
---|
743 | if (abs.y - margin > clippingSize.x) |
---|
744 | { |
---|
745 | halfAxes_[2].abs = 0.0; |
---|
746 | halfAxes_[3].abs = 1.0; |
---|
747 | } |
---|
748 | else if (abs.y < margin) |
---|
749 | { |
---|
750 | halfAxes_[2].abs = 1.0; |
---|
751 | halfAxes_[3].abs = 0.0; |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | float temp = -((float)abs.y) / clippingSize.x * 2 + 1; |
---|
756 | if (temp > 0) |
---|
757 | { |
---|
758 | halfAxes_[2].abs = temp; |
---|
759 | halfAxes_[3].abs = 0.0; |
---|
760 | } |
---|
761 | else |
---|
762 | { |
---|
763 | halfAxes_[2].abs = 0.0; |
---|
764 | halfAxes_[3].abs = -temp; |
---|
765 | } |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | // relative movements |
---|
770 | if (rel.x > 0) |
---|
771 | { |
---|
772 | halfAxes_[0].rel = rel.x; |
---|
773 | halfAxes_[1].rel = 0.0; |
---|
774 | } |
---|
775 | else |
---|
776 | { |
---|
777 | halfAxes_[0].rel = 0.0; |
---|
778 | halfAxes_[1].rel = rel.x; |
---|
779 | } |
---|
780 | |
---|
781 | if (rel.y /*!*/ < /*!*/ 0) |
---|
782 | { |
---|
783 | halfAxes_[0].rel = -rel.y; |
---|
784 | halfAxes_[1].rel = 0.0; |
---|
785 | } |
---|
786 | else |
---|
787 | { |
---|
788 | halfAxes_[0].rel = 0.0; |
---|
789 | halfAxes_[1].rel = -rel.y; |
---|
790 | } |
---|
791 | |
---|
792 | return true; |
---|
793 | } |
---|
794 | |
---|
795 | /** |
---|
796 | @brief Event handler for the mouseScrolled Event. |
---|
797 | @param e Mouse state information |
---|
798 | */ |
---|
799 | bool KeyBinder::mouseScrolled(int abs, int rel) |
---|
800 | { |
---|
801 | // TODO: obvious... |
---|
802 | return true; |
---|
803 | } |
---|
804 | |
---|
805 | /** |
---|
806 | @brief Event handler for the mousePressed Event. |
---|
807 | @param e Event information |
---|
808 | @param id The ID of the mouse button |
---|
809 | */ |
---|
810 | bool KeyBinder::mouseButtonPressed(MouseButton::Enum id) |
---|
811 | { |
---|
812 | // find the appropriate key binding |
---|
813 | executeBinding(bindingsMouseButtons_[int(id)].OnPress, 1.0, 1.0); |
---|
814 | |
---|
815 | return true; |
---|
816 | } |
---|
817 | |
---|
818 | /** |
---|
819 | @brief Event handler for the mouseReleased Event. |
---|
820 | @param e Event information |
---|
821 | @param id The ID of the mouse button |
---|
822 | */ |
---|
823 | bool KeyBinder::mouseButtonReleased(MouseButton::Enum id) |
---|
824 | { |
---|
825 | // find the appropriate key binding |
---|
826 | executeBinding(bindingsMouseButtons_[int(id)].OnRelease, 1.0, 1.0); |
---|
827 | |
---|
828 | return true; |
---|
829 | } |
---|
830 | |
---|
831 | /** |
---|
832 | @brief Event handler for the mouseHeld Event. |
---|
833 | @param e Event information |
---|
834 | @param id The ID of the mouse button |
---|
835 | */ |
---|
836 | bool KeyBinder::mouseButtonHeld(MouseButton::Enum id) |
---|
837 | { |
---|
838 | // find the appropriate key binding |
---|
839 | executeBinding(bindingsMouseButtons_[int(id)].OnHold, 1.0, 1.0); |
---|
840 | |
---|
841 | return true; |
---|
842 | } |
---|
843 | |
---|
844 | bool KeyBinder::joyStickButtonPressed(int joyStickID, int button) |
---|
845 | { |
---|
846 | // find the appropriate key binding |
---|
847 | executeBinding(bindingsJoyStickButtons_[button].OnPress, 1.0, 1.0); |
---|
848 | |
---|
849 | return true; |
---|
850 | } |
---|
851 | |
---|
852 | bool KeyBinder::joyStickButtonReleased(int joyStickID, int button) |
---|
853 | { |
---|
854 | // find the appropriate key binding |
---|
855 | executeBinding(bindingsJoyStickButtons_[button].OnRelease, 1.0, 1.0); |
---|
856 | |
---|
857 | return true; |
---|
858 | } |
---|
859 | |
---|
860 | bool KeyBinder::joyStickButtonHeld(int joyStickID, int button) |
---|
861 | { |
---|
862 | // find the appropriate key binding |
---|
863 | executeBinding(bindingsJoyStickButtons_[button].OnHold, 1.0, 1.0); |
---|
864 | |
---|
865 | return true; |
---|
866 | } |
---|
867 | |
---|
868 | bool KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value) |
---|
869 | { |
---|
870 | // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows) |
---|
871 | halfAxes_[8 + axis].hasChanged = true; |
---|
872 | if (value >= 0) |
---|
873 | { |
---|
874 | halfAxes_[8 + axis].abs = ((float)value)/0x1000; |
---|
875 | halfAxes_[8 + axis].hasChanged = true; |
---|
876 | } |
---|
877 | else |
---|
878 | { |
---|
879 | halfAxes_[8 + axis + 1].abs = -((float)value)/0x1000; |
---|
880 | halfAxes_[8 + axis + 1].hasChanged = true; |
---|
881 | } |
---|
882 | return true; |
---|
883 | } |
---|
884 | |
---|
885 | |
---|
886 | // ############################### |
---|
887 | // ### GUIInputHandler ### |
---|
888 | // ############################### |
---|
889 | |
---|
890 | ///** |
---|
891 | // @brief standard constructor |
---|
892 | //*/ |
---|
893 | //GUIInputHandler::GUIInputHandler() |
---|
894 | //{ |
---|
895 | //} |
---|
896 | |
---|
897 | ///** |
---|
898 | // @brief Destructor |
---|
899 | //*/ |
---|
900 | //GUIInputHandler::~GUIInputHandler() |
---|
901 | //{ |
---|
902 | //} |
---|
903 | |
---|
904 | ///** |
---|
905 | // @brief Event handler for the keyPressed Event. |
---|
906 | // @param e Event information |
---|
907 | //*/ |
---|
908 | //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
909 | //{ |
---|
910 | ////CEGUI::System::getSingleton().injectKeyDown( arg.key ); |
---|
911 | ////CEGUI::System::getSingleton().injectChar( arg.text ); |
---|
912 | // return true; |
---|
913 | //} |
---|
914 | |
---|
915 | ///** |
---|
916 | // @brief Event handler for the keyReleased Event. |
---|
917 | // @param e Event information |
---|
918 | //*/ |
---|
919 | //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
920 | //{ |
---|
921 | ////CEGUI::System::getSingleton().injectKeyUp( arg.key ); |
---|
922 | // return true; |
---|
923 | //} |
---|
924 | |
---|
925 | ///** |
---|
926 | // @brief Event handler for the mouseMoved Event. |
---|
927 | // @param e Event information |
---|
928 | //*/ |
---|
929 | //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
930 | //{ |
---|
931 | ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); |
---|
932 | // return true; |
---|
933 | //} |
---|
934 | |
---|
935 | ///** |
---|
936 | // @brief Event handler for the mousePressed Event. |
---|
937 | // @param e Event information |
---|
938 | // @param id The ID of the mouse button |
---|
939 | //*/ |
---|
940 | //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButton id) |
---|
941 | //{ |
---|
942 | ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); |
---|
943 | // return true; |
---|
944 | //} |
---|
945 | |
---|
946 | ///** |
---|
947 | // @brief Event handler for the mouseReleased Event. |
---|
948 | // @param e Event information |
---|
949 | // @param id The ID of the mouse button |
---|
950 | //*/ |
---|
951 | //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButton id) |
---|
952 | //{ |
---|
953 | ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); |
---|
954 | // return true; |
---|
955 | //} |
---|
956 | |
---|
957 | } |
---|