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 | // ###### Button ###### |
---|
48 | // ############################### |
---|
49 | |
---|
50 | bool BufferedParamCommand::execute() |
---|
51 | { |
---|
52 | if (nValuesAdded_) |
---|
53 | { |
---|
54 | BufferedParamCommand& cmd = *this; |
---|
55 | cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_); |
---|
56 | // reset |
---|
57 | cmd.nValuesAdded_ = 0; |
---|
58 | cmd.value_ = 0; |
---|
59 | return CommandExecutor::execute(cmd.evaluation_); |
---|
60 | } |
---|
61 | else |
---|
62 | return true; |
---|
63 | } |
---|
64 | |
---|
65 | bool SimpleCommand::execute(float abs, float rel) |
---|
66 | { |
---|
67 | return CommandExecutor::execute(evaluation_); |
---|
68 | } |
---|
69 | |
---|
70 | bool ParamCommand::execute(float abs, float rel) |
---|
71 | { |
---|
72 | BufferedParamCommand& paramCommand = *paramCommand_; |
---|
73 | // command has an additional parameter |
---|
74 | if (bRelative_) |
---|
75 | { |
---|
76 | // we have to calculate a relative movement. |
---|
77 | // amplitude says how much one keystroke is |
---|
78 | paramCommand.value_ += paramModifier_ * rel; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | // we have to calculate absolute position of the axis. |
---|
83 | // for a key this simply is 1, but multiplied by a user defined factor |
---|
84 | // since there might be another axis that is affected, we have to wait and |
---|
85 | // store the result in a temporary place |
---|
86 | paramCommand.value_ = (paramCommand.value_ * paramCommand.nValuesAdded_ + paramModifier_ * abs) |
---|
87 | /++paramCommand.nValuesAdded_; |
---|
88 | } |
---|
89 | return true; |
---|
90 | } |
---|
91 | |
---|
92 | void Button::clear() |
---|
93 | { |
---|
94 | for (unsigned int j = 0; j < 3; j++) |
---|
95 | { |
---|
96 | if (nCommands_[j]) |
---|
97 | { |
---|
98 | // delete all commands and the command pointer array |
---|
99 | for (unsigned int i = 0; i < nCommands_[j]; i++) |
---|
100 | delete commands_[j][i]; |
---|
101 | delete[] commands_[j]; |
---|
102 | commands_[j] = 0; |
---|
103 | nCommands_[j] = 0; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | commands_[j] = 0; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer) |
---|
113 | { |
---|
114 | if (isEmpty(bindingString_)) |
---|
115 | { |
---|
116 | clear(); |
---|
117 | return; |
---|
118 | } |
---|
119 | |
---|
120 | // use std::vector for a temporary dynamic array |
---|
121 | std::vector<BaseCommand*> commands[3]; |
---|
122 | |
---|
123 | |
---|
124 | // separate the commands |
---|
125 | SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, |
---|
126 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
127 | |
---|
128 | for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) |
---|
129 | { |
---|
130 | if (commandStrings[iCommand] != "") |
---|
131 | { |
---|
132 | SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, |
---|
133 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
134 | |
---|
135 | unsigned int iToken = 0; |
---|
136 | |
---|
137 | // for real axes, we can feed a ButtonThreshold argument as entire command |
---|
138 | if (getLowercase(tokens[0]) == "buttonthreshold") |
---|
139 | { |
---|
140 | if (tokens.size() == 1) |
---|
141 | continue; |
---|
142 | // may fail, but doesn't matter |
---|
143 | convertValue(&buttonThreshold_, tokens[1]); |
---|
144 | continue; |
---|
145 | } |
---|
146 | |
---|
147 | // first argument can be OnPress, OnHold OnRelease or nothing |
---|
148 | KeybindMode::Enum mode = KeybindMode::None; |
---|
149 | if (getLowercase(tokens[iToken]) == "onpress") |
---|
150 | mode = KeybindMode::OnPress, iToken++; |
---|
151 | if (getLowercase(tokens[iToken]) == "onrelease") |
---|
152 | mode = KeybindMode::OnRelease, iToken++; |
---|
153 | if (getLowercase(tokens[iToken]) == "onhold") |
---|
154 | mode = KeybindMode::OnHold, iToken++; |
---|
155 | |
---|
156 | if (iToken == tokens.size()) |
---|
157 | continue; |
---|
158 | |
---|
159 | // second argument can be the amplitude for the case it as an axis command |
---|
160 | // default amplitude is 1.0f |
---|
161 | float paramModifier = 1.0f; |
---|
162 | if (getLowercase(tokens[iToken]) == "axisamp") |
---|
163 | { |
---|
164 | iToken++; |
---|
165 | if (iToken == tokens.size() || !convertValue(¶mModifier, tokens[iToken])) |
---|
166 | { |
---|
167 | COUT(2) << "Error while parsing key binding " << name_ |
---|
168 | << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl; |
---|
169 | if (iToken == tokens.size()) |
---|
170 | continue; |
---|
171 | } |
---|
172 | iToken++; |
---|
173 | } |
---|
174 | |
---|
175 | // no more arguments expected except for the actual command |
---|
176 | if (iToken == tokens.size()) |
---|
177 | continue; |
---|
178 | |
---|
179 | std::string commandStr; |
---|
180 | while (iToken != tokens.size()) |
---|
181 | commandStr += tokens[iToken++] + " "; |
---|
182 | |
---|
183 | // evaluate the command |
---|
184 | CommandEvaluation& eval = CommandExecutor::evaluate(commandStr); |
---|
185 | if (!eval.isValid()) |
---|
186 | continue; |
---|
187 | |
---|
188 | // check for param command |
---|
189 | int paramIndex = eval.getEvaluatedExecutor()->getAxisParamIndex(); |
---|
190 | // TODO: check in Executor for correct paramIndex |
---|
191 | if (paramIndex >= 0) |
---|
192 | { |
---|
193 | // parameter supported command |
---|
194 | ParamCommand* cmd = new ParamCommand(); |
---|
195 | cmd->paramModifier_ = paramModifier; |
---|
196 | cmd->bRelative_ = eval.getEvaluatedExecutor()->getIsAxisRelative(); |
---|
197 | |
---|
198 | // add command to the buffer if not yet existing |
---|
199 | for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++) |
---|
200 | { |
---|
201 | if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getCommandString()) |
---|
202 | == getLowercase(commandStr)) |
---|
203 | { |
---|
204 | // already in list |
---|
205 | cmd->paramCommand_ = paramCommandBuffer[iParamCmd]; |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | if (cmd->paramCommand_ == 0) |
---|
210 | { |
---|
211 | cmd->paramCommand_ = new BufferedParamCommand(); |
---|
212 | paramCommandBuffer.push_back(cmd->paramCommand_); |
---|
213 | cmd->paramCommand_->evaluation_ = eval; |
---|
214 | cmd->paramCommand_->paramIndex_ = paramIndex; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | // we don't know whether this is an actual axis or just a button |
---|
219 | if (mode == KeybindMode::None) |
---|
220 | { |
---|
221 | if (!addParamCommand(cmd)) |
---|
222 | { |
---|
223 | mode = eval.getEvaluatedExecutor()->getKeybindMode(); |
---|
224 | commands[mode].push_back(cmd); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | SimpleCommand* cmd = new SimpleCommand(); |
---|
231 | cmd->evaluation_ = eval; |
---|
232 | |
---|
233 | //TODO: check CommandEvaluation for correct KeybindMode |
---|
234 | if (mode == KeybindMode::None) |
---|
235 | mode = eval.getEvaluatedExecutor()->getKeybindMode(); |
---|
236 | |
---|
237 | commands[mode].push_back(cmd); |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | for (unsigned int j = 0; j < 3; j++) |
---|
243 | { |
---|
244 | nCommands_[j] = commands[j].size(); |
---|
245 | if (nCommands_[j]) |
---|
246 | { |
---|
247 | commands_[j] = new BaseCommand*[nCommands_[j]]; |
---|
248 | for (unsigned int i = 0; i < commands[j].size(); i++) |
---|
249 | commands_[j][i] = commands[j][i]; |
---|
250 | } |
---|
251 | else |
---|
252 | commands_[j] = 0; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | bool Button::execute(KeybindMode::Enum mode) |
---|
257 | { |
---|
258 | // execute all the parsed commands in the string |
---|
259 | for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++) |
---|
260 | commands_[mode][iCommand]->execute(); |
---|
261 | return true; |
---|
262 | } |
---|
263 | |
---|
264 | void HalfAxis::clear() |
---|
265 | { |
---|
266 | Button::clear(); |
---|
267 | if (nParamCommands_) |
---|
268 | { |
---|
269 | // delete all commands and the command pointer array |
---|
270 | for (unsigned int i = 0; i < nParamCommands_; i++) |
---|
271 | delete paramCommands_[i]; |
---|
272 | delete[] paramCommands_; |
---|
273 | } |
---|
274 | else |
---|
275 | { |
---|
276 | nParamCommands_ = 0; nParamCommands_ = 0; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | bool HalfAxis::addParamCommand(ParamCommand* command) |
---|
281 | { |
---|
282 | ParamCommand** cmds = paramCommands_; |
---|
283 | paramCommands_ = new ParamCommand*[++nParamCommands_]; |
---|
284 | unsigned int i; |
---|
285 | for (i = 0; i < nParamCommands_ - 1; i++) |
---|
286 | paramCommands_[i] = cmds[i]; |
---|
287 | paramCommands_[i] = command; |
---|
288 | delete[] cmds; |
---|
289 | return true; |
---|
290 | } |
---|
291 | |
---|
292 | bool HalfAxis::execute() |
---|
293 | { |
---|
294 | bool success = true; |
---|
295 | for (unsigned int i = 0; i < nParamCommands_; i++) |
---|
296 | success = success && paramCommands_[i]->execute(absVal_, relVal_); |
---|
297 | return success; |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | // ############################### |
---|
302 | // ###### KeyBinder ###### |
---|
303 | // ############################### |
---|
304 | |
---|
305 | /** |
---|
306 | @brief Constructor that does as little as necessary. |
---|
307 | */ |
---|
308 | KeyBinder::KeyBinder() |
---|
309 | { |
---|
310 | RegisterObject(KeyBinder); |
---|
311 | |
---|
312 | // keys |
---|
313 | std::string keyNames[] = { |
---|
314 | "UNASSIGNED", |
---|
315 | "ESCAPE", |
---|
316 | "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", |
---|
317 | "MINUS", "EQUALS", "BACK", "TAB", |
---|
318 | "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", |
---|
319 | "LBRACKET", "RBRACKET", |
---|
320 | "RETURN", "LCONTROL", |
---|
321 | "A", "S", "D", "F", "G", "H", "J", "K", "L", |
---|
322 | "SEMICOLON", "APOSTROPHE", "GRAVE", |
---|
323 | "LSHIFT", "BACKSLASH", |
---|
324 | "Z", "X", "C", "V", "B", "N", "M", |
---|
325 | "COMMA", "PERIOD", "SLASH", |
---|
326 | "RSHIFT", |
---|
327 | "MULTIPLY", |
---|
328 | "LMENU", |
---|
329 | "SPACE", |
---|
330 | "CAPITAL", |
---|
331 | "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", |
---|
332 | "NUMLOCK", "SCROLL", |
---|
333 | "NUMPAD7", "NUMPAD8", "NUMPAD9", |
---|
334 | "SUBTRACT", |
---|
335 | "NUMPAD4", "NUMPAD5", "NUMPAD6", |
---|
336 | "ADD", |
---|
337 | "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0", |
---|
338 | "DECIMAL", |
---|
339 | "","", |
---|
340 | "OEM_102", |
---|
341 | "F11", "F12", |
---|
342 | "","","","","","","","","","","", |
---|
343 | "F13", "F14", "F15", |
---|
344 | "","","","","","","","","","", |
---|
345 | "KANA", |
---|
346 | "","", |
---|
347 | "ABNT_C1", |
---|
348 | "","","","","", |
---|
349 | "CONVERT", |
---|
350 | "", |
---|
351 | "NOCONVERT", |
---|
352 | "", |
---|
353 | "YEN", |
---|
354 | "ABNT_C2", |
---|
355 | "","","","","","","","","","","","","","", |
---|
356 | "NUMPADEQUALS", |
---|
357 | "","", |
---|
358 | "PREVTRACK", |
---|
359 | "AT", |
---|
360 | "COLON", "UNDERLINE", |
---|
361 | "KANJI", |
---|
362 | "STOP", |
---|
363 | "AX", |
---|
364 | "UNLABELED", |
---|
365 | "NEXTTRACK", |
---|
366 | "","", |
---|
367 | "NUMPADENTER", |
---|
368 | "RCONTROL", |
---|
369 | "","", |
---|
370 | "MUTE", |
---|
371 | "CALCULATOR", |
---|
372 | "PLAYPAUSE", |
---|
373 | "", |
---|
374 | "MEDIASTOP", |
---|
375 | "","","","","","","","","", |
---|
376 | "VOLUMEDOWN", |
---|
377 | "", |
---|
378 | "VOLUMEUP", |
---|
379 | "", |
---|
380 | "WEBHOME", |
---|
381 | "NUMPADCOMMA", |
---|
382 | "", |
---|
383 | "DIVIDE", |
---|
384 | "", |
---|
385 | "SYSRQ", |
---|
386 | "RMENU", |
---|
387 | "","","","","","","","","","","","", |
---|
388 | "PAUSE", |
---|
389 | "", |
---|
390 | "HOME", |
---|
391 | "UP", |
---|
392 | "PGUP", |
---|
393 | "", |
---|
394 | "LEFT", |
---|
395 | "", |
---|
396 | "RIGHT", |
---|
397 | "", |
---|
398 | "END", "DOWN", "PGDOWN", "INSERT", "DELETE", |
---|
399 | "","","","","","","", |
---|
400 | "LWIN", "RWIN", "APPS", |
---|
401 | "POWER", "SLEEP", |
---|
402 | "","","", |
---|
403 | "WAKE", |
---|
404 | "", |
---|
405 | "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK", |
---|
406 | "MYCOMPUTER", "MAIL", "MEDIASELECT" |
---|
407 | }; |
---|
408 | for (int i = 0; i < nKeys_s; i++) |
---|
409 | keys_[i].name_ = "Key" + keyNames[i]; |
---|
410 | |
---|
411 | // mouse buttons |
---|
412 | std::string mouseButtonNames[] = { |
---|
413 | "MouseLeft", "MouseRight", "MouseMiddle", |
---|
414 | "MouseButton3", "MouseButton4", "MouseButton5", |
---|
415 | "MouseButton6", "MouseButton7" }; |
---|
416 | for (int i = 0; i < nMouseButtons_s; i++) |
---|
417 | mouseButtons_[i].name_ = mouseButtonNames[i]; |
---|
418 | |
---|
419 | // joy stick buttons |
---|
420 | for (int i = 0; i < 32; i++) |
---|
421 | joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i); |
---|
422 | for (int i = 32; i < nJoyStickButtons_s; i += 4) |
---|
423 | { |
---|
424 | joyStickButtons_[i + 0].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North"; |
---|
425 | joyStickButtons_[i + 1].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South"; |
---|
426 | joyStickButtons_[i + 2].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East"; |
---|
427 | joyStickButtons_[i + 3].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West"; |
---|
428 | } |
---|
429 | |
---|
430 | // half axes |
---|
431 | std::string rawNames[nHalfAxes_s/2]; |
---|
432 | rawNames[0] = "MouseX"; |
---|
433 | rawNames[1] = "MouseY"; |
---|
434 | rawNames[2] = "MouseWheel1"; |
---|
435 | rawNames[3] = "MouseWheel2"; |
---|
436 | for (unsigned int i = 4; i < nHalfAxes_s/2; i++) |
---|
437 | rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3); |
---|
438 | for (unsigned int i = 0; i < nHalfAxes_s/2; i++) |
---|
439 | { |
---|
440 | halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos"; |
---|
441 | halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg"; |
---|
442 | } |
---|
443 | |
---|
444 | for (unsigned int i = 0; i < this->nHalfAxes_s; i++) |
---|
445 | halfAxes_[i].buttonThreshold_ = buttonThreshold_; |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | @brief Destructor |
---|
450 | */ |
---|
451 | KeyBinder::~KeyBinder() |
---|
452 | { |
---|
453 | // almost no destructors required because most of the arrays are static. |
---|
454 | clearBindings(); // does some destruction work |
---|
455 | } |
---|
456 | |
---|
457 | /** |
---|
458 | @brief Loads the key and button bindings. |
---|
459 | @return True if loading succeeded. |
---|
460 | */ |
---|
461 | void KeyBinder::loadBindings() |
---|
462 | { |
---|
463 | COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; |
---|
464 | |
---|
465 | ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); |
---|
466 | clearBindings(); |
---|
467 | setConfigValues(); |
---|
468 | |
---|
469 | COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; |
---|
470 | } |
---|
471 | |
---|
472 | /** |
---|
473 | @brief Loader for the key bindings, managed by config values. |
---|
474 | */ |
---|
475 | void KeyBinder::setConfigValues() |
---|
476 | { |
---|
477 | SetConfigValue(analogThreshold_, 0.01f).description("Threshold for analog axes until which the state is 0."); |
---|
478 | float oldThresh = buttonThreshold_; |
---|
479 | SetConfigValue(buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed."); |
---|
480 | if (oldThresh != buttonThreshold_) |
---|
481 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
---|
482 | if (halfAxes_[i].buttonThreshold_ == oldThresh) |
---|
483 | halfAxes_[i].buttonThreshold_ = buttonThreshold_; |
---|
484 | |
---|
485 | // keys |
---|
486 | for (unsigned int i = 0; i < nKeys_s; i++) |
---|
487 | readTrigger(keys_[i]); |
---|
488 | // mouse buttons |
---|
489 | for (unsigned int i = 0; i < nMouseButtons_s; i++) |
---|
490 | readTrigger(mouseButtons_[i]); |
---|
491 | // joy stick buttons |
---|
492 | for (unsigned int i = 0; i < nJoyStickButtons_s; i++) |
---|
493 | readTrigger(joyStickButtons_[i]); |
---|
494 | // half axes |
---|
495 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
---|
496 | readTrigger(halfAxes_[i]); |
---|
497 | } |
---|
498 | |
---|
499 | void KeyBinder::readTrigger(Button& button) |
---|
500 | { |
---|
501 | // config value stuff |
---|
502 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(button.name_); |
---|
503 | if (!cont) |
---|
504 | { |
---|
505 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), button.name_, ""); |
---|
506 | getIdentifier()->addConfigValueContainer(button.name_, cont); |
---|
507 | } |
---|
508 | std::string old = button.bindingString_; |
---|
509 | cont->getValue(&button.bindingString_); |
---|
510 | |
---|
511 | // keybinder stuff |
---|
512 | if (old != button.bindingString_) |
---|
513 | { |
---|
514 | // binding has changed |
---|
515 | button.parse(paramCommandBuffer_); |
---|
516 | } |
---|
517 | } |
---|
518 | |
---|
519 | /** |
---|
520 | @brief Overwrites all bindings with "" |
---|
521 | */ |
---|
522 | void KeyBinder::clearBindings(bool bInit) |
---|
523 | { |
---|
524 | for (int i = 0; i < nKeys_s; i++) |
---|
525 | keys_[i].clear(); |
---|
526 | |
---|
527 | for (int i = 0; i < nMouseButtons_s; i++) |
---|
528 | mouseButtons_[i].clear(); |
---|
529 | |
---|
530 | for (int i = 0; i < nJoyStickButtons_s; i++) |
---|
531 | joyStickButtons_[i].clear(); |
---|
532 | |
---|
533 | for (int i = 0; i < nHalfAxes_s; i++) |
---|
534 | halfAxes_[i].clear(); |
---|
535 | |
---|
536 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
---|
537 | delete paramCommandBuffer_[i]; |
---|
538 | paramCommandBuffer_.clear(); |
---|
539 | } |
---|
540 | |
---|
541 | void KeyBinder::tick(float dt) |
---|
542 | { |
---|
543 | // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event. |
---|
544 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
---|
545 | { |
---|
546 | if (halfAxes_[i].hasChanged_) |
---|
547 | { |
---|
548 | if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_) |
---|
549 | { |
---|
550 | halfAxes_[i].wasDown_ = true; |
---|
551 | if (halfAxes_[i].nCommands_[KeybindMode::OnPress]) |
---|
552 | halfAxes_[i].execute(KeybindMode::OnPress); |
---|
553 | } |
---|
554 | else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_) |
---|
555 | { |
---|
556 | halfAxes_[i].wasDown_ = false; |
---|
557 | if (halfAxes_[i].nCommands_[KeybindMode::OnRelease]) |
---|
558 | halfAxes_[i].execute(KeybindMode::OnRelease); |
---|
559 | } |
---|
560 | if (halfAxes_[i].wasDown_) |
---|
561 | { |
---|
562 | if (halfAxes_[i].nCommands_[KeybindMode::OnHold]) |
---|
563 | halfAxes_[i].execute(KeybindMode::OnHold); |
---|
564 | } |
---|
565 | halfAxes_[i].hasChanged_ = false; |
---|
566 | } |
---|
567 | |
---|
568 | // these are the actually useful axis bindings for analog input AND output |
---|
569 | if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) |
---|
570 | { |
---|
571 | halfAxes_[i].execute(); |
---|
572 | } |
---|
573 | } |
---|
574 | |
---|
575 | // execute all buffered bindings (addional parameter) |
---|
576 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
---|
577 | paramCommandBuffer_[i]->execute(); |
---|
578 | |
---|
579 | // always reset the relative movement of the mouse |
---|
580 | for (unsigned int i = 0; i < 4; i++) |
---|
581 | halfAxes_[i].relVal_ = 0; |
---|
582 | } |
---|
583 | |
---|
584 | void KeyBinder::keyPressed (const KeyEvent& evt) |
---|
585 | { keys_[evt.key].execute(KeybindMode::OnPress); } |
---|
586 | |
---|
587 | void KeyBinder::keyReleased(const KeyEvent& evt) |
---|
588 | { keys_[evt.key].execute(KeybindMode::OnRelease); } |
---|
589 | |
---|
590 | void KeyBinder::keyHeld (const KeyEvent& evt) |
---|
591 | { keys_[evt.key].execute(KeybindMode::OnHold); } |
---|
592 | |
---|
593 | |
---|
594 | void KeyBinder::mouseButtonPressed (MouseButton::Enum id) |
---|
595 | { mouseButtons_[id].execute(KeybindMode::OnPress); } |
---|
596 | |
---|
597 | void KeyBinder::mouseButtonReleased(MouseButton::Enum id) |
---|
598 | { mouseButtons_[id].execute(KeybindMode::OnRelease); } |
---|
599 | |
---|
600 | void KeyBinder::mouseButtonHeld (MouseButton::Enum id) |
---|
601 | { mouseButtons_[id].execute(KeybindMode::OnHold); } |
---|
602 | |
---|
603 | |
---|
604 | void KeyBinder::joyStickButtonPressed (int joyStickID, int button) |
---|
605 | { joyStickButtons_[button].execute(KeybindMode::OnPress); } |
---|
606 | |
---|
607 | void KeyBinder::joyStickButtonReleased(int joyStickID, int button) |
---|
608 | { joyStickButtons_[button].execute(KeybindMode::OnRelease); } |
---|
609 | |
---|
610 | void KeyBinder::joyStickButtonHeld (int joyStickID, int button) |
---|
611 | { joyStickButtons_[button].execute(KeybindMode::OnHold); } |
---|
612 | |
---|
613 | /** |
---|
614 | @brief Event handler for the mouseMoved Event. |
---|
615 | @param e Mouse state information |
---|
616 | */ |
---|
617 | void KeyBinder::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
618 | { |
---|
619 | // translate absolute mouse position into joystick like behaviour |
---|
620 | if (clippingSize.x > clippingSize.y) |
---|
621 | { |
---|
622 | int margin = (clippingSize.x - clippingSize.y) / 2; |
---|
623 | if (abs.x - margin > clippingSize.y) |
---|
624 | { |
---|
625 | halfAxes_[0].absVal_ = 1.0f; |
---|
626 | halfAxes_[1].absVal_ = 0.0f; |
---|
627 | } |
---|
628 | else if (abs.x < margin) |
---|
629 | { |
---|
630 | halfAxes_[0].absVal_ = 0.0f; |
---|
631 | halfAxes_[1].absVal_ = 1.0f; |
---|
632 | } |
---|
633 | else |
---|
634 | { |
---|
635 | float temp = ((float)abs.x) / clippingSize.y * 2 - 1; |
---|
636 | if (temp > 0) |
---|
637 | { |
---|
638 | halfAxes_[0].absVal_ = temp; |
---|
639 | halfAxes_[1].absVal_ = 0.0f; |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | halfAxes_[0].absVal_ = 0.0f; |
---|
644 | halfAxes_[1].absVal_ = -temp; |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | float temp = -((float)abs.y) / clippingSize.y * 2 + 1; |
---|
649 | if (temp > 0) |
---|
650 | { |
---|
651 | halfAxes_[2].absVal_ = temp; |
---|
652 | halfAxes_[3].absVal_ = 0.0; |
---|
653 | } |
---|
654 | else |
---|
655 | { |
---|
656 | halfAxes_[2].absVal_ = 0.0; |
---|
657 | halfAxes_[3].absVal_ = -temp; |
---|
658 | } |
---|
659 | } |
---|
660 | else |
---|
661 | { |
---|
662 | float temp = ((float)abs.x) / clippingSize.x * 2 - 1; |
---|
663 | if (temp > 0) |
---|
664 | { |
---|
665 | halfAxes_[0].absVal_ = temp; |
---|
666 | halfAxes_[1].absVal_ = 0.0; |
---|
667 | } |
---|
668 | else |
---|
669 | { |
---|
670 | halfAxes_[0].absVal_ = 0.0; |
---|
671 | halfAxes_[1].absVal_ = -temp; |
---|
672 | } |
---|
673 | |
---|
674 | int margin = (clippingSize.y - clippingSize.x) / 2; |
---|
675 | if (abs.y - margin > clippingSize.x) |
---|
676 | { |
---|
677 | halfAxes_[2].absVal_ = 0.0; |
---|
678 | halfAxes_[3].absVal_ = 1.0; |
---|
679 | } |
---|
680 | else if (abs.y < margin) |
---|
681 | { |
---|
682 | halfAxes_[2].absVal_ = 1.0; |
---|
683 | halfAxes_[3].absVal_ = 0.0; |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | float temp = -((float)abs.y) / clippingSize.x * 2 + 1; |
---|
688 | if (temp > 0) |
---|
689 | { |
---|
690 | halfAxes_[2].absVal_ = temp; |
---|
691 | halfAxes_[3].absVal_ = 0.0; |
---|
692 | } |
---|
693 | else |
---|
694 | { |
---|
695 | halfAxes_[2].absVal_ = 0.0; |
---|
696 | halfAxes_[3].absVal_ = -temp; |
---|
697 | } |
---|
698 | } |
---|
699 | } |
---|
700 | |
---|
701 | // relative movements |
---|
702 | if (rel.x > 0) |
---|
703 | { |
---|
704 | halfAxes_[0].hasChanged_ = true; |
---|
705 | halfAxes_[1].hasChanged_ = true; |
---|
706 | halfAxes_[0].relVal_ = rel.x; |
---|
707 | halfAxes_[1].relVal_ = 0.0; |
---|
708 | } |
---|
709 | else if (rel.x < 0) |
---|
710 | { |
---|
711 | halfAxes_[0].hasChanged_ = true; |
---|
712 | halfAxes_[1].hasChanged_ = true; |
---|
713 | halfAxes_[0].relVal_ = 0.0; |
---|
714 | halfAxes_[1].relVal_ = rel.x; |
---|
715 | } |
---|
716 | |
---|
717 | if (rel.y /*!*/ < /*!*/ 0) |
---|
718 | { |
---|
719 | halfAxes_[2].hasChanged_ = true; |
---|
720 | halfAxes_[3].hasChanged_ = true; |
---|
721 | halfAxes_[0].relVal_ = -rel.y; |
---|
722 | halfAxes_[1].relVal_ = 0.0; |
---|
723 | } |
---|
724 | else if (rel.y > 0) |
---|
725 | { |
---|
726 | halfAxes_[2].hasChanged_ = true; |
---|
727 | halfAxes_[3].hasChanged_ = true; |
---|
728 | halfAxes_[0].relVal_ = 0.0; |
---|
729 | halfAxes_[1].relVal_ = -rel.y; |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | /** |
---|
734 | @brief Event handler for the mouseScrolled Event. |
---|
735 | @param e Mouse state information |
---|
736 | */ |
---|
737 | void KeyBinder::mouseScrolled(int abs, int rel) |
---|
738 | { |
---|
739 | // TODO: obvious... |
---|
740 | } |
---|
741 | |
---|
742 | void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value) |
---|
743 | { |
---|
744 | // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows) |
---|
745 | //CCOUT(3) << axis << std::endl; |
---|
746 | if (value >= 0) |
---|
747 | { |
---|
748 | halfAxes_[8 + axis].absVal_ = ((float)value)/0x8000; |
---|
749 | halfAxes_[8 + axis].relVal_ = ((float)value)/0x8000; |
---|
750 | halfAxes_[8 + axis].hasChanged_ = true; |
---|
751 | } |
---|
752 | else |
---|
753 | { |
---|
754 | halfAxes_[8 + axis + 1].absVal_ = -((float)value)/0x8000; |
---|
755 | halfAxes_[8 + axis + 1].relVal_ = -((float)value)/0x8000; |
---|
756 | halfAxes_[8 + axis + 1].hasChanged_ = true; |
---|
757 | } |
---|
758 | } |
---|
759 | |
---|
760 | |
---|
761 | // ############################### |
---|
762 | // ### GUIInputHandler ### |
---|
763 | // ############################### |
---|
764 | |
---|
765 | ///** |
---|
766 | // @brief standard constructor |
---|
767 | //*/ |
---|
768 | //GUIInputHandler::GUIInputHandler() |
---|
769 | //{ |
---|
770 | //} |
---|
771 | |
---|
772 | ///** |
---|
773 | // @brief Destructor |
---|
774 | //*/ |
---|
775 | //GUIInputHandler::~GUIInputHandler() |
---|
776 | //{ |
---|
777 | //} |
---|
778 | |
---|
779 | ///** |
---|
780 | // @brief Event handler for the keyPressed Event. |
---|
781 | // @param e Event information |
---|
782 | //*/ |
---|
783 | //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
784 | //{ |
---|
785 | ////CEGUI::System::getSingleton().injectKeyDown( arg.key ); |
---|
786 | ////CEGUI::System::getSingleton().injectChar( arg.text ); |
---|
787 | // return true; |
---|
788 | //} |
---|
789 | |
---|
790 | ///** |
---|
791 | // @brief Event handler for the keyReleased Event. |
---|
792 | // @param e Event information |
---|
793 | //*/ |
---|
794 | //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
795 | //{ |
---|
796 | ////CEGUI::System::getSingleton().injectKeyUp( arg.key ); |
---|
797 | // return true; |
---|
798 | //} |
---|
799 | |
---|
800 | ///** |
---|
801 | // @brief Event handler for the mouseMoved Event. |
---|
802 | // @param e Event information |
---|
803 | //*/ |
---|
804 | //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
805 | //{ |
---|
806 | ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); |
---|
807 | // return true; |
---|
808 | //} |
---|
809 | |
---|
810 | ///** |
---|
811 | // @brief Event handler for the mousePressed Event. |
---|
812 | // @param e Event information |
---|
813 | // @param id The ID of the mouse button |
---|
814 | //*/ |
---|
815 | //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButton id) |
---|
816 | //{ |
---|
817 | ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); |
---|
818 | // return true; |
---|
819 | //} |
---|
820 | |
---|
821 | ///** |
---|
822 | // @brief Event handler for the mouseReleased Event. |
---|
823 | // @param e Event information |
---|
824 | // @param id The ID of the mouse button |
---|
825 | //*/ |
---|
826 | //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButton id) |
---|
827 | //{ |
---|
828 | ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); |
---|
829 | // return true; |
---|
830 | //} |
---|
831 | |
---|
832 | } |
---|