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 |
---|
32 | Implementation of the InputManager that captures all the input from OIS |
---|
33 | and redirects it to handlers. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "InputManager.h" |
---|
37 | |
---|
38 | #include <climits> |
---|
39 | #include <cassert> |
---|
40 | |
---|
41 | #include "ois/OISException.h" |
---|
42 | #include "ois/OISInputManager.h" |
---|
43 | |
---|
44 | #include "util/Exception.h" |
---|
45 | #include "core/CoreIncludes.h" |
---|
46 | #include "core/ConfigValueIncludes.h" |
---|
47 | #include "core/CommandExecutor.h" |
---|
48 | #include "core/ConsoleCommand.h" |
---|
49 | #include "core/CommandLine.h" |
---|
50 | #include "util/Debug.h" |
---|
51 | |
---|
52 | #include "InputBuffer.h" |
---|
53 | #include "KeyBinder.h" |
---|
54 | #include "KeyDetector.h" |
---|
55 | #include "CalibratorCallback.h" |
---|
56 | #include "InputState.h" |
---|
57 | #include "SimpleInputState.h" |
---|
58 | #include "ExtendedInputState.h" |
---|
59 | #include "JoyStickDeviceNumberListener.h" |
---|
60 | |
---|
61 | namespace orxonox |
---|
62 | { |
---|
63 | SetConsoleCommand(InputManager, calibrate, true); |
---|
64 | SetConsoleCommand(InputManager, reload, false); |
---|
65 | SetCommandLineSwitch(keyboard_no_grab); |
---|
66 | |
---|
67 | std::string InputManager::bindingCommmandString_s = ""; |
---|
68 | EmptyHandler InputManager::EMPTY_HANDLER; |
---|
69 | InputManager* InputManager::singletonRef_s = 0; |
---|
70 | |
---|
71 | using namespace InputDevice; |
---|
72 | |
---|
73 | /** |
---|
74 | @brief |
---|
75 | Defines the |= operator for easier use. |
---|
76 | */ |
---|
77 | inline InputManager::InputManagerState operator|=(InputManager::InputManagerState& lval, |
---|
78 | InputManager::InputManagerState rval) |
---|
79 | { |
---|
80 | return (lval = (InputManager::InputManagerState)(lval | rval)); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | @brief |
---|
85 | Defines the &= operator for easier use. |
---|
86 | */ |
---|
87 | inline InputManager::InputManagerState operator&=(InputManager::InputManagerState& lval, int rval) |
---|
88 | { |
---|
89 | return (lval = (InputManager::InputManagerState)(lval & rval)); |
---|
90 | } |
---|
91 | |
---|
92 | // ############################################################ |
---|
93 | // ##### Initialisation ##### |
---|
94 | // ########## ########## |
---|
95 | // ############################################################ |
---|
96 | |
---|
97 | /** |
---|
98 | @brief |
---|
99 | Constructor only sets member fields to initial zero values |
---|
100 | and registers the class in the class hierarchy. |
---|
101 | */ |
---|
102 | InputManager::InputManager() |
---|
103 | : inputSystem_(0) |
---|
104 | , keyboard_(0) |
---|
105 | , mouse_(0) |
---|
106 | , joySticksSize_(0) |
---|
107 | , devicesNum_(0) |
---|
108 | , windowHnd_(0) |
---|
109 | , internalState_(Uninitialised) |
---|
110 | , stateEmpty_(0) |
---|
111 | , stateMaster_(0) |
---|
112 | , keyDetector_(0) |
---|
113 | , calibratorCallbackBuffer_(0) |
---|
114 | , bCalibrating_(false) |
---|
115 | , keyboardModifiers_(0) |
---|
116 | { |
---|
117 | RegisterRootObject(InputManager); |
---|
118 | |
---|
119 | assert(singletonRef_s == 0); |
---|
120 | singletonRef_s = this; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | @brief |
---|
125 | Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
126 | the joysticks and assigns the key bindings. |
---|
127 | @param windowHnd |
---|
128 | The window handle of the render window |
---|
129 | @param windowWidth |
---|
130 | The width of the render window |
---|
131 | @param windowHeight |
---|
132 | The height of the render window |
---|
133 | @param joyStickSupport |
---|
134 | Whether or not to load the joy sticks as well |
---|
135 | */ |
---|
136 | void InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport) |
---|
137 | { |
---|
138 | CCOUT(3) << "Initialising Input System..." << std::endl; |
---|
139 | |
---|
140 | if (!(internalState_ & OISReady)) |
---|
141 | { |
---|
142 | CCOUT(4) << "Initialising OIS components..." << std::endl; |
---|
143 | |
---|
144 | // store handle internally so we can reload OIS |
---|
145 | windowHnd_ = windowHnd; |
---|
146 | |
---|
147 | OIS::ParamList paramList; |
---|
148 | std::ostringstream windowHndStr; |
---|
149 | |
---|
150 | // Fill parameter list |
---|
151 | windowHndStr << (unsigned int)windowHnd_; |
---|
152 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
153 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); |
---|
154 | //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); |
---|
155 | #if defined OIS_LINUX_PLATFORM |
---|
156 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
157 | paramList.insert(std::make_pair(std::string("x11_mouse_grab"), "true")); |
---|
158 | paramList.insert(std::make_pair(std::string("x11_mouse_hide"), "true")); |
---|
159 | bool kbNoGrab; |
---|
160 | CommandLine::getValue("keyboard_no_grab", &kbNoGrab); |
---|
161 | if (kbNoGrab) |
---|
162 | paramList.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false"))); |
---|
163 | else |
---|
164 | paramList.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("true"))); |
---|
165 | #endif |
---|
166 | |
---|
167 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
168 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
169 | |
---|
170 | _initialiseKeyboard(); |
---|
171 | |
---|
172 | _initialiseMouse(); |
---|
173 | |
---|
174 | if (joyStickSupport) |
---|
175 | _initialiseJoySticks(); |
---|
176 | // Do this anyway to also inform everyone if a joystick was detached. |
---|
177 | _configureNumberOfJoySticks(); |
---|
178 | |
---|
179 | // Set mouse/joystick region |
---|
180 | if (mouse_) |
---|
181 | setWindowExtents(windowWidth, windowHeight); |
---|
182 | |
---|
183 | // clear all buffers |
---|
184 | _clearBuffers(); |
---|
185 | |
---|
186 | // load joy stick calibration |
---|
187 | setConfigValues(); |
---|
188 | |
---|
189 | internalState_ |= OISReady; |
---|
190 | |
---|
191 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | CCOUT(2) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
196 | } |
---|
197 | |
---|
198 | if (!(internalState_ & InternalsReady)) |
---|
199 | { |
---|
200 | CCOUT(4) << "Initialising InputStates components..." << std::endl; |
---|
201 | |
---|
202 | // Lowest priority empty InputState |
---|
203 | stateEmpty_ = createInputState<SimpleInputState>("empty", -1); |
---|
204 | stateEmpty_->setHandler(&EMPTY_HANDLER); |
---|
205 | activeStates_[stateEmpty_->getPriority()] = stateEmpty_; |
---|
206 | |
---|
207 | // Always active master InputState |
---|
208 | stateMaster_ = new ExtendedInputState(); |
---|
209 | stateMaster_->setName("master"); |
---|
210 | stateMaster_->setNumOfJoySticks(joySticksSize_); |
---|
211 | |
---|
212 | // KeyDetector to evaluate a pressed key's name |
---|
213 | SimpleInputState* detector = createInputState<SimpleInputState>("detector", 101); |
---|
214 | keyDetector_ = new KeyDetector(); |
---|
215 | detector->setHandler(keyDetector_); |
---|
216 | |
---|
217 | // Joy stick calibration helper callback |
---|
218 | SimpleInputState* calibrator = createInputState<SimpleInputState>("calibrator", 100); |
---|
219 | calibrator->setHandler(&EMPTY_HANDLER); |
---|
220 | calibratorCallbackBuffer_ = new InputBuffer(); |
---|
221 | calibratorCallbackBuffer_->registerListener(this, &InputManager::_completeCalibration, '\r', true); |
---|
222 | calibrator->setKeyHandler(calibratorCallbackBuffer_); |
---|
223 | |
---|
224 | internalState_ |= InternalsReady; |
---|
225 | |
---|
226 | CCOUT(4) << "Initialising InputStates complete." << std::endl; |
---|
227 | } |
---|
228 | |
---|
229 | _updateActiveStates(); |
---|
230 | |
---|
231 | CCOUT(3) << "Initialising complete." << std::endl; |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | @brief |
---|
236 | Creates a keyboard and sets the event handler. |
---|
237 | @return |
---|
238 | False if keyboard stays uninitialised, true otherwise. |
---|
239 | */ |
---|
240 | void InputManager::_initialiseKeyboard() |
---|
241 | { |
---|
242 | if (keyboard_ != 0) |
---|
243 | { |
---|
244 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
245 | return; |
---|
246 | } |
---|
247 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
248 | { |
---|
249 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
250 | // register our listener in OIS. |
---|
251 | keyboard_->setEventCallback(this); |
---|
252 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
253 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
254 | } |
---|
255 | else |
---|
256 | { |
---|
257 | ThrowException(InitialisationFailed, "No keyboard found!"); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | @brief |
---|
263 | Creates a mouse and sets the event handler. |
---|
264 | @return |
---|
265 | False if mouse stays uninitialised, true otherwise. |
---|
266 | */ |
---|
267 | void InputManager::_initialiseMouse() |
---|
268 | { |
---|
269 | if (mouse_ != 0) |
---|
270 | { |
---|
271 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
272 | return; |
---|
273 | } |
---|
274 | try |
---|
275 | { |
---|
276 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
277 | { |
---|
278 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
279 | // register our listener in OIS. |
---|
280 | mouse_->setEventCallback(this); |
---|
281 | CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl; |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; |
---|
286 | } |
---|
287 | } |
---|
288 | catch (OIS::Exception ex) |
---|
289 | { |
---|
290 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
291 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
292 | mouse_ = 0; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | @brief |
---|
298 | Creates all joy sticks and sets the event handler. |
---|
299 | @return |
---|
300 | False joy stick stay uninitialised, true otherwise. |
---|
301 | */ |
---|
302 | void InputManager::_initialiseJoySticks() |
---|
303 | { |
---|
304 | if (joySticksSize_ > 0) |
---|
305 | { |
---|
306 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
307 | return; |
---|
308 | } |
---|
309 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
310 | { |
---|
311 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
312 | { |
---|
313 | try |
---|
314 | { |
---|
315 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*> |
---|
316 | (inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
317 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
318 | joySticks_.push_back(stig); |
---|
319 | // register our listener in OIS. |
---|
320 | stig->setEventCallback(this); |
---|
321 | } |
---|
322 | catch (OIS::Exception ex) |
---|
323 | { |
---|
324 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
325 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | else |
---|
330 | { |
---|
331 | //CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | @brief |
---|
337 | Sets the size of all the different lists that are dependent on the number |
---|
338 | of joy stick devices created. |
---|
339 | @remarks |
---|
340 | No matter whether there are a mouse and/or keyboard, they will always |
---|
341 | occupy 2 places in the device number dependent lists. |
---|
342 | */ |
---|
343 | void InputManager::_configureNumberOfJoySticks() |
---|
344 | { |
---|
345 | joySticksSize_ = joySticks_.size(); |
---|
346 | devicesNum_ = 2 + joySticksSize_; |
---|
347 | joyStickButtonsDown_ .resize(joySticksSize_); |
---|
348 | povStates_ .resize(joySticksSize_); |
---|
349 | sliderStates_ .resize(joySticksSize_); |
---|
350 | joySticksCalibration_.resize(joySticksSize_); |
---|
351 | |
---|
352 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
353 | { |
---|
354 | // reset the calibration with default values |
---|
355 | for (unsigned int i = 0; i < 24; i++) |
---|
356 | { |
---|
357 | joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; |
---|
358 | joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; |
---|
359 | joySticksCalibration_[iJoyStick].zeroStates[i] = 0; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | // state management |
---|
364 | activeStatesTop_.resize(devicesNum_); |
---|
365 | |
---|
366 | // inform all states |
---|
367 | for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin(); |
---|
368 | it != inputStatesByPriority_.end(); ++it) |
---|
369 | { |
---|
370 | it->second->setNumOfJoySticks(joySticksSize_); |
---|
371 | } |
---|
372 | // inform master state |
---|
373 | if (stateMaster_) |
---|
374 | this->stateMaster_->setNumOfJoySticks(joySticksSize_); |
---|
375 | |
---|
376 | // inform all JoyStick Device Number Listeners |
---|
377 | for (ObjectList<JoyStickDeviceNumberListener>::iterator it = ObjectList<JoyStickDeviceNumberListener>::begin(); it; ++it) |
---|
378 | it->JoyStickDeviceNumberChanged(joySticksSize_); |
---|
379 | |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | @brief |
---|
384 | Sets the configurable values. |
---|
385 | This mainly concerns joy stick calibrations. |
---|
386 | */ |
---|
387 | void InputManager::setConfigValues() |
---|
388 | { |
---|
389 | if (joySticksSize_ > 0) |
---|
390 | { |
---|
391 | std::vector<double> coeffPos; |
---|
392 | std::vector<double> coeffNeg; |
---|
393 | std::vector<int> zero; |
---|
394 | coeffPos.resize(24); |
---|
395 | coeffNeg.resize(24); |
---|
396 | zero.resize(24); |
---|
397 | for (unsigned int i = 0; i < 24; i++) |
---|
398 | { |
---|
399 | coeffPos[i] = 1.0f/32767.0f; |
---|
400 | coeffNeg[i] = 1.0f/32768.0f; |
---|
401 | zero[i] = 0; |
---|
402 | } |
---|
403 | |
---|
404 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
405 | if (!cont) |
---|
406 | { |
---|
407 | cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "CoeffPos", coeffPos); |
---|
408 | getIdentifier()->addConfigValueContainer("CoeffPos", cont); |
---|
409 | } |
---|
410 | cont->getValue(&coeffPos, this); |
---|
411 | |
---|
412 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
413 | if (!cont) |
---|
414 | { |
---|
415 | cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "CoeffNeg", coeffNeg); |
---|
416 | getIdentifier()->addConfigValueContainer("CoeffNeg", cont); |
---|
417 | } |
---|
418 | cont->getValue(&coeffNeg, this); |
---|
419 | |
---|
420 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
421 | if (!cont) |
---|
422 | { |
---|
423 | cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "Zero", zero); |
---|
424 | getIdentifier()->addConfigValueContainer("Zero", cont); |
---|
425 | } |
---|
426 | cont->getValue(&zero, this); |
---|
427 | |
---|
428 | // copy values to our own variables |
---|
429 | for (unsigned int i = 0; i < 24; i++) |
---|
430 | { |
---|
431 | joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; |
---|
432 | joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; |
---|
433 | joySticksCalibration_[0].zeroStates[i] = zero[i]; |
---|
434 | } |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | |
---|
439 | // ############################################################ |
---|
440 | // ##### Destruction ##### |
---|
441 | // ########## ########## |
---|
442 | // ############################################################ |
---|
443 | |
---|
444 | /** |
---|
445 | @brief |
---|
446 | Destroys all the created input devices and states. |
---|
447 | */ |
---|
448 | InputManager::~InputManager() |
---|
449 | { |
---|
450 | if (internalState_ != Uninitialised) |
---|
451 | { |
---|
452 | try |
---|
453 | { |
---|
454 | CCOUT(3) << "Destroying ..." << std::endl; |
---|
455 | |
---|
456 | // kick all active states 'nicely' |
---|
457 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); |
---|
458 | rit != activeStates_.rend(); ++rit) |
---|
459 | { |
---|
460 | (*rit).second->onLeave(); |
---|
461 | } |
---|
462 | |
---|
463 | // Destroy calibrator helper handler and state |
---|
464 | delete keyDetector_; |
---|
465 | requestDestroyState("calibrator"); |
---|
466 | // Destroy KeyDetector and state |
---|
467 | delete calibratorCallbackBuffer_; |
---|
468 | requestDestroyState("detector"); |
---|
469 | // destroy the empty InputState |
---|
470 | _destroyState(this->stateEmpty_); |
---|
471 | // destroy the master input state. This might trigger a memory leak |
---|
472 | // because the user has forgotten to destroy the KeyBinder or any Handler! |
---|
473 | delete stateMaster_; |
---|
474 | |
---|
475 | // destroy all user InputStates |
---|
476 | while (inputStatesByPriority_.size() > 0) |
---|
477 | _destroyState((*inputStatesByPriority_.rbegin()).second); |
---|
478 | |
---|
479 | // destroy the devices |
---|
480 | _destroyKeyboard(); |
---|
481 | _destroyMouse(); |
---|
482 | _destroyJoySticks(); |
---|
483 | |
---|
484 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
485 | |
---|
486 | CCOUT(3) << "Destroying done." << std::endl; |
---|
487 | } |
---|
488 | catch (OIS::Exception& ex) |
---|
489 | { |
---|
490 | CCOUT(1) << "An exception has occured while destroying:\n" << ex.what() |
---|
491 | << "This could lead to a possible memory/resource leak!" << std::endl; |
---|
492 | } |
---|
493 | } |
---|
494 | singletonRef_s = 0; |
---|
495 | } |
---|
496 | |
---|
497 | /** |
---|
498 | @brief |
---|
499 | Destroys the keyboard and sets it to 0. |
---|
500 | */ |
---|
501 | void InputManager::_destroyKeyboard() |
---|
502 | { |
---|
503 | assert(inputSystem_); |
---|
504 | if (keyboard_) |
---|
505 | inputSystem_->destroyInputObject(keyboard_); |
---|
506 | keyboard_ = 0; |
---|
507 | CCOUT(4) << "Keyboard destroyed." << std::endl; |
---|
508 | } |
---|
509 | |
---|
510 | /** |
---|
511 | @brief |
---|
512 | Destroys the mouse and sets it to 0. |
---|
513 | */ |
---|
514 | void InputManager::_destroyMouse() |
---|
515 | { |
---|
516 | assert(inputSystem_); |
---|
517 | if (mouse_) |
---|
518 | inputSystem_->destroyInputObject(mouse_); |
---|
519 | mouse_ = 0; |
---|
520 | CCOUT(4) << "Mouse destroyed." << std::endl; |
---|
521 | } |
---|
522 | |
---|
523 | /** |
---|
524 | @brief |
---|
525 | Destroys all the joy sticks and resizes the lists to 0. |
---|
526 | */ |
---|
527 | void InputManager::_destroyJoySticks() |
---|
528 | { |
---|
529 | if (joySticksSize_ > 0) |
---|
530 | { |
---|
531 | assert(inputSystem_); |
---|
532 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
533 | if (joySticks_[i] != 0) |
---|
534 | inputSystem_->destroyInputObject(joySticks_[i]); |
---|
535 | |
---|
536 | joySticks_.clear(); |
---|
537 | // don't use _configureNumberOfJoySticks(), might mess with registered handler if |
---|
538 | // downgrading from 2 to 1 joystick |
---|
539 | //_configureNumberOfJoySticks(); |
---|
540 | joySticksSize_ = 0; |
---|
541 | } |
---|
542 | CCOUT(4) << "Joy sticks destroyed." << std::endl; |
---|
543 | } |
---|
544 | |
---|
545 | /** |
---|
546 | @brief |
---|
547 | Removes and destroys an InputState. |
---|
548 | @return |
---|
549 | True if state was removed immediately, false if postponed. |
---|
550 | */ |
---|
551 | void InputManager::_destroyState(InputState* state) |
---|
552 | { |
---|
553 | assert(state && !(this->internalState_ & Ticking)); |
---|
554 | std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority()); |
---|
555 | if (it != this->activeStates_.end()) |
---|
556 | { |
---|
557 | this->activeStates_.erase(it); |
---|
558 | _updateActiveStates(); |
---|
559 | } |
---|
560 | inputStatesByPriority_.erase(state->getPriority()); |
---|
561 | inputStatesByName_.erase(state->getName()); |
---|
562 | delete state; |
---|
563 | } |
---|
564 | |
---|
565 | void InputManager::_clearBuffers() |
---|
566 | { |
---|
567 | keysDown_.clear(); |
---|
568 | keyboardModifiers_ = 0; |
---|
569 | mouseButtonsDown_.clear(); |
---|
570 | for (unsigned int i = 0; i < joySticksSize_; ++i) |
---|
571 | { |
---|
572 | joyStickButtonsDown_[i].clear(); |
---|
573 | for (int j = 0; j < 4; ++j) |
---|
574 | { |
---|
575 | sliderStates_[i].sliderStates[j].x = 0; |
---|
576 | sliderStates_[i].sliderStates[j].y = 0; |
---|
577 | povStates_[i][j] = 0; |
---|
578 | } |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | |
---|
583 | // ############################################################ |
---|
584 | // ##### Reloading ##### |
---|
585 | // ########## ########## |
---|
586 | // ############################################################ |
---|
587 | |
---|
588 | /** |
---|
589 | @brief |
---|
590 | Public interface. Only reloads immediately if the call stack doesn't |
---|
591 | include the tick() method. |
---|
592 | @param joyStickSupport |
---|
593 | Whether or not to initialise joy sticks as well. |
---|
594 | */ |
---|
595 | void InputManager::reloadInputSystem(bool joyStickSupport) |
---|
596 | { |
---|
597 | if (internalState_ & Ticking) |
---|
598 | { |
---|
599 | // We cannot destroy OIS right now, because reload was probably |
---|
600 | // caused by a user clicking on a GUI item. The backtrace would then |
---|
601 | // include an OIS method. So it would be a very bad thing to destroy it.. |
---|
602 | internalState_ |= ReloadRequest; |
---|
603 | // Misuse of internalState_: We can easily store the joyStickSupport bool. |
---|
604 | // use Uninitialised as 0 value in order to make use of the overloaded |= operator |
---|
605 | internalState_ |= joyStickSupport ? JoyStickSupport : Uninitialised; |
---|
606 | } |
---|
607 | else if (internalState_ & OISReady) |
---|
608 | { |
---|
609 | _reload(joyStickSupport); |
---|
610 | } |
---|
611 | else |
---|
612 | { |
---|
613 | CCOUT(2) << "Warning: Cannot reload OIS. May not yet be initialised or" |
---|
614 | << "joy sticks are currently calibrating." << std::endl; |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | @brief |
---|
620 | Internal reload method. Destroys the OIS devices and loads them again. |
---|
621 | */ |
---|
622 | void InputManager::_reload(bool joyStickSupport) |
---|
623 | { |
---|
624 | try |
---|
625 | { |
---|
626 | CCOUT(3) << "Reloading ..." << std::endl; |
---|
627 | |
---|
628 | // Save mouse clipping size |
---|
629 | int mouseWidth = mouse_->getMouseState().width; |
---|
630 | int mouseHeight = mouse_->getMouseState().height; |
---|
631 | |
---|
632 | internalState_ &= ~OISReady; |
---|
633 | |
---|
634 | // destroy the devices |
---|
635 | _destroyKeyboard(); |
---|
636 | _destroyMouse(); |
---|
637 | _destroyJoySticks(); |
---|
638 | |
---|
639 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
640 | inputSystem_ = 0; |
---|
641 | |
---|
642 | // clear all buffers containing input information |
---|
643 | _clearBuffers(); |
---|
644 | |
---|
645 | initialise(windowHnd_, mouseWidth, mouseHeight, joyStickSupport); |
---|
646 | |
---|
647 | CCOUT(3) << "Reloading done." << std::endl; |
---|
648 | } |
---|
649 | catch (OIS::Exception& ex) |
---|
650 | { |
---|
651 | CCOUT(1) << "An exception has occured while reloading:\n" << ex.what() << std::endl; |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | // ############################################################ |
---|
656 | // ##### Runtime Methods ##### |
---|
657 | // ########## ########## |
---|
658 | // ############################################################ |
---|
659 | |
---|
660 | /** |
---|
661 | @brief |
---|
662 | Updates the InputManager. Tick is called by the Core class. |
---|
663 | @param dt |
---|
664 | Delta time |
---|
665 | */ |
---|
666 | void InputManager::tick(float dt) |
---|
667 | { |
---|
668 | if (internalState_ == Uninitialised) |
---|
669 | return; |
---|
670 | else if (internalState_ & ReloadRequest) |
---|
671 | { |
---|
672 | _reload(internalState_ & JoyStickSupport); |
---|
673 | internalState_ &= ~ReloadRequest; |
---|
674 | internalState_ &= ~JoyStickSupport; |
---|
675 | } |
---|
676 | |
---|
677 | // check for states to leave |
---|
678 | for (std::set<InputState*>::reverse_iterator rit = stateLeaveRequests_.rbegin(); |
---|
679 | rit != stateLeaveRequests_.rend(); ++rit) |
---|
680 | { |
---|
681 | (*rit)->onLeave(); |
---|
682 | // just to be sure that the state actually is registered |
---|
683 | assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); |
---|
684 | |
---|
685 | activeStates_.erase((*rit)->getPriority()); |
---|
686 | _updateActiveStates(); |
---|
687 | } |
---|
688 | stateLeaveRequests_.clear(); |
---|
689 | |
---|
690 | // check for states to enter |
---|
691 | for (std::set<InputState*>::reverse_iterator rit = stateEnterRequests_.rbegin(); |
---|
692 | rit != stateEnterRequests_.rend(); ++rit) |
---|
693 | { |
---|
694 | // just to be sure that the state actually is registered |
---|
695 | assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end()); |
---|
696 | |
---|
697 | activeStates_[(*rit)->getPriority()] = (*rit); |
---|
698 | _updateActiveStates(); |
---|
699 | (*rit)->onEnter(); |
---|
700 | } |
---|
701 | stateEnterRequests_.clear(); |
---|
702 | |
---|
703 | // check for states to destroy |
---|
704 | for (std::set<InputState*>::reverse_iterator rit = stateDestroyRequests_.rbegin(); |
---|
705 | rit != stateDestroyRequests_.rend(); ++rit) |
---|
706 | { |
---|
707 | _destroyState((*rit)); |
---|
708 | } |
---|
709 | stateDestroyRequests_.clear(); |
---|
710 | |
---|
711 | // check whether a state has changed its EMPTY_HANDLER situation |
---|
712 | bool bUpdateRequired = false; |
---|
713 | for (std::map<int, InputState*>::iterator it = activeStates_.begin(); it != activeStates_.end(); ++it) |
---|
714 | { |
---|
715 | if (it->second->handlersChanged()) |
---|
716 | { |
---|
717 | it->second->resetHandlersChanged(); |
---|
718 | bUpdateRequired = true; |
---|
719 | } |
---|
720 | } |
---|
721 | if (bUpdateRequired) |
---|
722 | _updateActiveStates(); |
---|
723 | |
---|
724 | // mark that we capture and distribute input |
---|
725 | internalState_ |= Ticking; |
---|
726 | |
---|
727 | // Capture all the input. This calls the event handlers in InputManager. |
---|
728 | if (keyboard_) |
---|
729 | keyboard_->capture(); |
---|
730 | if (mouse_) |
---|
731 | mouse_->capture(); |
---|
732 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
733 | joySticks_[i]->capture(); |
---|
734 | |
---|
735 | if (!bCalibrating_) |
---|
736 | { |
---|
737 | // call all the handlers for the held key events |
---|
738 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
739 | { |
---|
740 | KeyEvent kEvt(keysDown_[iKey], keyboardModifiers_); |
---|
741 | activeStatesTop_[Keyboard]->keyHeld(kEvt); |
---|
742 | stateMaster_->keyHeld(kEvt); |
---|
743 | } |
---|
744 | |
---|
745 | // call all the handlers for the held mouse button events |
---|
746 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
747 | { |
---|
748 | activeStatesTop_[Mouse]->mouseButtonHeld(mouseButtonsDown_[iButton]); |
---|
749 | stateMaster_->mouseButtonHeld(mouseButtonsDown_[iButton]); |
---|
750 | } |
---|
751 | |
---|
752 | // call all the handlers for the held joy stick button events |
---|
753 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
754 | for (unsigned int iButton = 0; iButton < joyStickButtonsDown_[iJoyStick].size(); iButton++) |
---|
755 | { |
---|
756 | activeStatesTop_[JoyStick0 + iJoyStick] |
---|
757 | ->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); |
---|
758 | stateMaster_->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]); |
---|
759 | } |
---|
760 | |
---|
761 | // tick the handlers for each active handler |
---|
762 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
763 | { |
---|
764 | activeStatesTop_[i]->tickInput(dt, i); |
---|
765 | if (stateMaster_->isInputDeviceEnabled(i)) |
---|
766 | stateMaster_->tickInput(dt, i); |
---|
767 | } |
---|
768 | |
---|
769 | // tick the handler with a general tick afterwards |
---|
770 | for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i) |
---|
771 | activeStatesTicked_[i]->tickInput(dt); |
---|
772 | stateMaster_->tickInput(dt); |
---|
773 | } |
---|
774 | |
---|
775 | internalState_ &= ~Ticking; |
---|
776 | } |
---|
777 | |
---|
778 | /** |
---|
779 | @brief |
---|
780 | Updates the currently active states (according to activeStates_) for each device. |
---|
781 | Also, a list of all active states (no duplicates!) is compiled for the general tick. |
---|
782 | */ |
---|
783 | void InputManager::_updateActiveStates() |
---|
784 | { |
---|
785 | for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it) |
---|
786 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
787 | if (it->second->isInputDeviceEnabled(i)) |
---|
788 | activeStatesTop_[i] = it->second; |
---|
789 | |
---|
790 | // update tickables (every state will only appear once) |
---|
791 | // Using a std::set to avoid duplicates |
---|
792 | std::set<InputState*> tempSet; |
---|
793 | for (unsigned int i = 0; i < devicesNum_; ++i) |
---|
794 | tempSet.insert(activeStatesTop_[i]); |
---|
795 | |
---|
796 | // copy the content of the set back to the actual vector |
---|
797 | activeStatesTicked_.clear(); |
---|
798 | for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it) |
---|
799 | activeStatesTicked_.push_back(*it); |
---|
800 | |
---|
801 | this->mouseButtonsDown_.clear(); |
---|
802 | } |
---|
803 | |
---|
804 | /** |
---|
805 | @brief |
---|
806 | Processes the accumultated data for the joy stick calibration. |
---|
807 | */ |
---|
808 | void InputManager::_completeCalibration() |
---|
809 | { |
---|
810 | for (unsigned int i = 0; i < 24; i++) |
---|
811 | { |
---|
812 | // positive coefficient |
---|
813 | if (marginalsMax_[i] == INT_MIN) |
---|
814 | marginalsMax_[i] = 32767; |
---|
815 | // coefficients |
---|
816 | if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
817 | { |
---|
818 | joySticksCalibration_[0].positiveCoeff[i] |
---|
819 | = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
820 | } |
---|
821 | else |
---|
822 | joySticksCalibration_[0].positiveCoeff[i] = 1.0f; |
---|
823 | |
---|
824 | // config value |
---|
825 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); |
---|
826 | assert(cont); |
---|
827 | cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); |
---|
828 | |
---|
829 | // negative coefficient |
---|
830 | if (marginalsMin_[i] == INT_MAX) |
---|
831 | marginalsMin_[i] = -32768; |
---|
832 | // coefficients |
---|
833 | if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) |
---|
834 | { |
---|
835 | joySticksCalibration_[0].negativeCoeff[i] = -1.0f |
---|
836 | / (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); |
---|
837 | } |
---|
838 | else |
---|
839 | joySticksCalibration_[0].negativeCoeff[i] = 1.0f; |
---|
840 | // config value |
---|
841 | cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); |
---|
842 | assert(cont); |
---|
843 | cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); |
---|
844 | |
---|
845 | // zero states |
---|
846 | if (i < 8) |
---|
847 | { |
---|
848 | if (!(i & 1)) |
---|
849 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; |
---|
850 | else |
---|
851 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; |
---|
852 | } |
---|
853 | else |
---|
854 | { |
---|
855 | if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) |
---|
856 | joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; |
---|
857 | else |
---|
858 | joySticksCalibration_[0].zeroStates[i] = 0; |
---|
859 | } |
---|
860 | // config value |
---|
861 | cont = getIdentifier()->getConfigValueContainer("Zero"); |
---|
862 | assert(cont); |
---|
863 | cont->set(i, joySticksCalibration_[0].zeroStates[i]); |
---|
864 | } |
---|
865 | |
---|
866 | // restore old input state |
---|
867 | requestLeaveState("calibrator"); |
---|
868 | bCalibrating_ = false; |
---|
869 | } |
---|
870 | |
---|
871 | void InputManager::clearBuffers() |
---|
872 | { |
---|
873 | this->keysDown_.clear(); |
---|
874 | this->mouseButtonsDown_.clear(); |
---|
875 | for (unsigned int i = 0; i < this->joySticksSize_; ++i) |
---|
876 | this->joyStickButtonsDown_[i].clear(); |
---|
877 | } |
---|
878 | |
---|
879 | |
---|
880 | // ############################################################ |
---|
881 | // ##### OIS events ##### |
---|
882 | // ########## ########## |
---|
883 | // ############################################################ |
---|
884 | |
---|
885 | // ###### Key Events ###### |
---|
886 | |
---|
887 | /** |
---|
888 | @brief |
---|
889 | Event handler for the keyPressed Event. |
---|
890 | @param e |
---|
891 | Event information |
---|
892 | */ |
---|
893 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
894 | { |
---|
895 | // check whether the key already is in the list (can happen when focus was lost) |
---|
896 | unsigned int iKey = 0; |
---|
897 | while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::ByEnum)e.key) |
---|
898 | iKey++; |
---|
899 | if (iKey == keysDown_.size()) |
---|
900 | keysDown_.push_back(Key(e)); |
---|
901 | else |
---|
902 | { |
---|
903 | // This happens when XAutoRepeat is set under linux. The KeyPressed event gets then sent |
---|
904 | // continuously. |
---|
905 | return true; |
---|
906 | } |
---|
907 | |
---|
908 | // update modifiers |
---|
909 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
910 | keyboardModifiers_ |= KeyboardModifier::Alt; // alt key |
---|
911 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
912 | keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key |
---|
913 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
914 | keyboardModifiers_ |= KeyboardModifier::Shift; // shift key |
---|
915 | |
---|
916 | KeyEvent kEvt(e, keyboardModifiers_); |
---|
917 | activeStatesTop_[Keyboard]->keyPressed(kEvt); |
---|
918 | stateMaster_->keyPressed(kEvt); |
---|
919 | |
---|
920 | return true; |
---|
921 | } |
---|
922 | |
---|
923 | /** |
---|
924 | @brief |
---|
925 | Event handler for the keyReleased Event. |
---|
926 | @param e |
---|
927 | Event information |
---|
928 | */ |
---|
929 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
930 | { |
---|
931 | // remove the key from the keysDown_ list |
---|
932 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
933 | { |
---|
934 | if (keysDown_[iKey].key == (KeyCode::ByEnum)e.key) |
---|
935 | { |
---|
936 | keysDown_.erase(keysDown_.begin() + iKey); |
---|
937 | break; |
---|
938 | } |
---|
939 | } |
---|
940 | |
---|
941 | // update modifiers |
---|
942 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
943 | keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key |
---|
944 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
945 | keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key |
---|
946 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
947 | keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key |
---|
948 | |
---|
949 | KeyEvent kEvt(e, keyboardModifiers_); |
---|
950 | activeStatesTop_[Keyboard]->keyReleased(kEvt); |
---|
951 | stateMaster_->keyReleased(kEvt); |
---|
952 | |
---|
953 | return true; |
---|
954 | } |
---|
955 | |
---|
956 | |
---|
957 | // ###### Mouse Events ###### |
---|
958 | |
---|
959 | /** |
---|
960 | @brief |
---|
961 | Event handler for the mouseMoved Event. |
---|
962 | @param e |
---|
963 | Event information |
---|
964 | */ |
---|
965 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
966 | { |
---|
967 | // check for actual moved event |
---|
968 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
969 | { |
---|
970 | IntVector2 abs(e.state.X.abs, e.state.Y.abs); |
---|
971 | IntVector2 rel(e.state.X.rel, e.state.Y.rel); |
---|
972 | IntVector2 clippingSize(e.state.width, e.state.height); |
---|
973 | activeStatesTop_[Mouse]->mouseMoved(abs, rel, clippingSize); |
---|
974 | stateMaster_->mouseMoved(abs, rel, clippingSize); |
---|
975 | } |
---|
976 | |
---|
977 | // check for mouse scrolled event |
---|
978 | if (e.state.Z.rel != 0) |
---|
979 | { |
---|
980 | activeStatesTop_[Mouse]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
981 | stateMaster_->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
982 | } |
---|
983 | |
---|
984 | return true; |
---|
985 | } |
---|
986 | |
---|
987 | /** |
---|
988 | @brief |
---|
989 | Event handler for the mousePressed Event. |
---|
990 | @param e |
---|
991 | Event information |
---|
992 | @param id |
---|
993 | The ID of the mouse button |
---|
994 | */ |
---|
995 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
996 | { |
---|
997 | // check whether the button already is in the list (can happen when focus was lost) |
---|
998 | unsigned int iButton = 0; |
---|
999 | while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButtonCode::ByEnum)id) |
---|
1000 | iButton++; |
---|
1001 | if (iButton == mouseButtonsDown_.size()) |
---|
1002 | mouseButtonsDown_.push_back((MouseButtonCode::ByEnum)id); |
---|
1003 | |
---|
1004 | activeStatesTop_[Mouse]->mouseButtonPressed((MouseButtonCode::ByEnum)id); |
---|
1005 | stateMaster_->mouseButtonPressed((MouseButtonCode::ByEnum)id); |
---|
1006 | |
---|
1007 | return true; |
---|
1008 | } |
---|
1009 | |
---|
1010 | /** |
---|
1011 | @brief |
---|
1012 | Event handler for the mouseReleased Event. |
---|
1013 | @param e |
---|
1014 | Event information |
---|
1015 | @param id |
---|
1016 | The ID of the mouse button |
---|
1017 | */ |
---|
1018 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
1019 | { |
---|
1020 | // remove the button from the keysDown_ list |
---|
1021 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
1022 | { |
---|
1023 | if (mouseButtonsDown_[iButton] == (MouseButtonCode::ByEnum)id) |
---|
1024 | { |
---|
1025 | mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); |
---|
1026 | break; |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | activeStatesTop_[Mouse]->mouseButtonReleased((MouseButtonCode::ByEnum)id); |
---|
1031 | stateMaster_->mouseButtonReleased((MouseButtonCode::ByEnum)id); |
---|
1032 | |
---|
1033 | return true; |
---|
1034 | } |
---|
1035 | |
---|
1036 | |
---|
1037 | // ###### Joy Stick Events ###### |
---|
1038 | |
---|
1039 | /** |
---|
1040 | @brief |
---|
1041 | Returns the joy stick ID (orxonox) according to a OIS::JoyStickEvent |
---|
1042 | */ |
---|
1043 | inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg) |
---|
1044 | { |
---|
1045 | // use the device to identify which one called the method |
---|
1046 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
1047 | unsigned int iJoyStick = 0; |
---|
1048 | while (joySticks_[iJoyStick] != joyStick) |
---|
1049 | iJoyStick++; |
---|
1050 | // assert: Unknown joystick fired an event. |
---|
1051 | assert(iJoyStick != joySticksSize_); |
---|
1052 | return iJoyStick; |
---|
1053 | } |
---|
1054 | |
---|
1055 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
1056 | { |
---|
1057 | unsigned int iJoyStick = _getJoystick(arg); |
---|
1058 | |
---|
1059 | // check whether the button already is in the list (can happen when focus was lost) |
---|
1060 | std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
1061 | unsigned int iButton = 0; |
---|
1062 | while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) |
---|
1063 | iButton++; |
---|
1064 | if (iButton == buttonsDown.size()) |
---|
1065 | buttonsDown.push_back((JoyStickButtonCode::ByEnum)button); |
---|
1066 | |
---|
1067 | activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
1068 | stateMaster_->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
1069 | |
---|
1070 | return true; |
---|
1071 | } |
---|
1072 | |
---|
1073 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
1074 | { |
---|
1075 | unsigned int iJoyStick = _getJoystick(arg); |
---|
1076 | |
---|
1077 | // remove the button from the joyStickButtonsDown_ list |
---|
1078 | std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
1079 | for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) |
---|
1080 | { |
---|
1081 | if (buttonsDown[iButton] == button) |
---|
1082 | { |
---|
1083 | buttonsDown.erase(buttonsDown.begin() + iButton); |
---|
1084 | break; |
---|
1085 | } |
---|
1086 | } |
---|
1087 | |
---|
1088 | activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
1089 | stateMaster_->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button); |
---|
1090 | |
---|
1091 | return true; |
---|
1092 | } |
---|
1093 | |
---|
1094 | /** |
---|
1095 | @brief |
---|
1096 | Calls the states for a particular axis with our enumeration. |
---|
1097 | Used by OIS sliders and OIS axes. |
---|
1098 | */ |
---|
1099 | void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) |
---|
1100 | { |
---|
1101 | if (bCalibrating_) |
---|
1102 | { |
---|
1103 | if (value > marginalsMax_[axis]) |
---|
1104 | marginalsMax_[axis] = value; |
---|
1105 | if (value < marginalsMin_[axis]) |
---|
1106 | marginalsMin_[axis] = value; |
---|
1107 | } |
---|
1108 | else |
---|
1109 | { |
---|
1110 | float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis]; |
---|
1111 | if (fValue > 0.0f) |
---|
1112 | fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis]; |
---|
1113 | else |
---|
1114 | fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis]; |
---|
1115 | |
---|
1116 | activeStatesTop_[2 + iJoyStick]->joyStickAxisMoved(iJoyStick, axis, fValue); |
---|
1117 | stateMaster_->joyStickAxisMoved(iJoyStick, axis, fValue); |
---|
1118 | } |
---|
1119 | } |
---|
1120 | |
---|
1121 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
1122 | { |
---|
1123 | unsigned int iJoyStick = _getJoystick(arg); |
---|
1124 | |
---|
1125 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
1126 | _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs); |
---|
1127 | |
---|
1128 | return true; |
---|
1129 | } |
---|
1130 | |
---|
1131 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
1132 | { |
---|
1133 | unsigned int iJoyStick = _getJoystick(arg); |
---|
1134 | |
---|
1135 | if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX) |
---|
1136 | _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX); |
---|
1137 | else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY) |
---|
1138 | _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY); |
---|
1139 | |
---|
1140 | return true; |
---|
1141 | } |
---|
1142 | |
---|
1143 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
1144 | { |
---|
1145 | unsigned int iJoyStick = _getJoystick(arg); |
---|
1146 | |
---|
1147 | // translate the POV into 8 simple buttons |
---|
1148 | |
---|
1149 | int lastState = povStates_[iJoyStick][id]; |
---|
1150 | if (lastState & OIS::Pov::North) |
---|
1151 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
1152 | if (lastState & OIS::Pov::South) |
---|
1153 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
1154 | if (lastState & OIS::Pov::East) |
---|
1155 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
1156 | if (lastState & OIS::Pov::West) |
---|
1157 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
1158 | |
---|
1159 | povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction; |
---|
1160 | |
---|
1161 | int currentState = povStates_[iJoyStick][id]; |
---|
1162 | if (currentState & OIS::Pov::North) |
---|
1163 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
1164 | if (currentState & OIS::Pov::South) |
---|
1165 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
1166 | if (currentState & OIS::Pov::East) |
---|
1167 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
1168 | if (currentState & OIS::Pov::West) |
---|
1169 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
1170 | |
---|
1171 | return true; |
---|
1172 | } |
---|
1173 | |
---|
1174 | |
---|
1175 | // ############################################################ |
---|
1176 | // ##### Other Public Interface Methods ##### |
---|
1177 | // ########## ########## |
---|
1178 | // ############################################################ |
---|
1179 | |
---|
1180 | /** |
---|
1181 | @brief |
---|
1182 | Adjusts the mouse window metrics. |
---|
1183 | This method has to be called every time the size of the window changes. |
---|
1184 | @param width |
---|
1185 | The new width of the render window |
---|
1186 | @param^height |
---|
1187 | The new height of the render window |
---|
1188 | */ |
---|
1189 | void InputManager::setWindowExtents(const int width, const int height) |
---|
1190 | { |
---|
1191 | if (mouse_) |
---|
1192 | { |
---|
1193 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
1194 | mouse_->getMouseState().width = width; |
---|
1195 | mouse_->getMouseState().height = height; |
---|
1196 | } |
---|
1197 | } |
---|
1198 | |
---|
1199 | /** |
---|
1200 | @brief |
---|
1201 | Sets the the name of the command used by the KeyDetector as callback. |
---|
1202 | @param command |
---|
1203 | Command name as string |
---|
1204 | */ |
---|
1205 | void InputManager::setKeyDetectorCallback(const std::string& command) |
---|
1206 | { |
---|
1207 | this->keyDetector_->setCallbackCommand(command); |
---|
1208 | } |
---|
1209 | |
---|
1210 | // ###### InputStates ###### |
---|
1211 | |
---|
1212 | /** |
---|
1213 | @brief |
---|
1214 | Adds a new key handler. |
---|
1215 | @param handler |
---|
1216 | Pointer to the handler object. |
---|
1217 | @param name |
---|
1218 | Unique name of the handler. |
---|
1219 | @param priority |
---|
1220 | Unique integer number. Higher means more prioritised. |
---|
1221 | @return |
---|
1222 | True if added, false if name or priority already existed. |
---|
1223 | */ |
---|
1224 | bool InputManager::_configureInputState(InputState* state, const std::string& name, int priority) |
---|
1225 | { |
---|
1226 | if (name == "") |
---|
1227 | return false; |
---|
1228 | if (!state) |
---|
1229 | return false; |
---|
1230 | if (inputStatesByName_.find(name) == inputStatesByName_.end()) |
---|
1231 | { |
---|
1232 | if (inputStatesByPriority_.find(priority) |
---|
1233 | == inputStatesByPriority_.end()) |
---|
1234 | { |
---|
1235 | inputStatesByName_[name] = state; |
---|
1236 | inputStatesByPriority_[priority] = state; |
---|
1237 | state->setNumOfJoySticks(numberOfJoySticks()); |
---|
1238 | state->setName(name); |
---|
1239 | state->setPriority(priority); |
---|
1240 | return true; |
---|
1241 | } |
---|
1242 | else |
---|
1243 | { |
---|
1244 | COUT(2) << "Warning: Could not add an InputState with the same priority '" |
---|
1245 | << priority << "'." << std::endl; |
---|
1246 | return false; |
---|
1247 | } |
---|
1248 | } |
---|
1249 | else |
---|
1250 | { |
---|
1251 | COUT(2) << "Warning: Could not add an InputState with the same name '" << name << "'." << std::endl; |
---|
1252 | return false; |
---|
1253 | } |
---|
1254 | } |
---|
1255 | |
---|
1256 | /** |
---|
1257 | @brief |
---|
1258 | Removes and destroys an input state internally. |
---|
1259 | @param name |
---|
1260 | Name of the handler. |
---|
1261 | @return |
---|
1262 | True if removal was successful, false if name was not found. |
---|
1263 | @remarks |
---|
1264 | You can't remove the internal states "empty", "calibrator" and "detector". |
---|
1265 | The removal process is being postponed if InputManager::tick() is currently running. |
---|
1266 | */ |
---|
1267 | bool InputManager::requestDestroyState(const std::string& name) |
---|
1268 | { |
---|
1269 | if (name == "empty") |
---|
1270 | { |
---|
1271 | COUT(2) << "InputManager: Removing the empty state is not allowed!" << std::endl; |
---|
1272 | return false; |
---|
1273 | } |
---|
1274 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
1275 | if (it != inputStatesByName_.end()) |
---|
1276 | { |
---|
1277 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
1278 | { |
---|
1279 | // The state is still active. We have to postpone |
---|
1280 | stateLeaveRequests_.insert(it->second); |
---|
1281 | stateDestroyRequests_.insert(it->second); |
---|
1282 | } |
---|
1283 | else if (this->internalState_ & Ticking) |
---|
1284 | { |
---|
1285 | // cannot remove state while ticking |
---|
1286 | stateDestroyRequests_.insert(it->second); |
---|
1287 | } |
---|
1288 | else |
---|
1289 | _destroyState(it->second); |
---|
1290 | |
---|
1291 | return true; |
---|
1292 | } |
---|
1293 | return false; |
---|
1294 | } |
---|
1295 | |
---|
1296 | /** |
---|
1297 | @brief |
---|
1298 | Returns the pointer to the requested InputState. |
---|
1299 | @param name |
---|
1300 | Unique name of the state. |
---|
1301 | @return |
---|
1302 | Pointer to the instance, 0 if name was not found. |
---|
1303 | */ |
---|
1304 | InputState* InputManager::getState(const std::string& name) |
---|
1305 | { |
---|
1306 | std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name); |
---|
1307 | if (it != inputStatesByName_.end()) |
---|
1308 | return it->second; |
---|
1309 | else |
---|
1310 | return 0; |
---|
1311 | } |
---|
1312 | |
---|
1313 | /** |
---|
1314 | @brief |
---|
1315 | Returns the current input state (there might be others active too!) |
---|
1316 | @return |
---|
1317 | The current highest prioritised active input state. |
---|
1318 | */ |
---|
1319 | InputState* InputManager::getCurrentState() |
---|
1320 | { |
---|
1321 | return (*activeStates_.rbegin()).second; |
---|
1322 | } |
---|
1323 | |
---|
1324 | /** |
---|
1325 | @brief |
---|
1326 | Activates a specific input state. |
---|
1327 | It might not be really activated if the priority is too low! |
---|
1328 | @param name |
---|
1329 | Unique name of the state. |
---|
1330 | @return |
---|
1331 | False if name was not found, true otherwise. |
---|
1332 | */ |
---|
1333 | bool InputManager::requestEnterState(const std::string& name) |
---|
1334 | { |
---|
1335 | // get pointer from the map with all stored handlers |
---|
1336 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
1337 | if (it != inputStatesByName_.end()) |
---|
1338 | { |
---|
1339 | // exists |
---|
1340 | if (activeStates_.find(it->second->getPriority()) == activeStates_.end()) |
---|
1341 | { |
---|
1342 | // not active |
---|
1343 | if (stateDestroyRequests_.find(it->second) == stateDestroyRequests_.end()) |
---|
1344 | { |
---|
1345 | // not scheduled for destruction |
---|
1346 | // set prevents a state being added multiple times |
---|
1347 | stateEnterRequests_.insert(it->second); |
---|
1348 | return true; |
---|
1349 | } |
---|
1350 | } |
---|
1351 | } |
---|
1352 | return false; |
---|
1353 | } |
---|
1354 | |
---|
1355 | /** |
---|
1356 | @brief |
---|
1357 | Deactivates a specific input state. |
---|
1358 | @param name |
---|
1359 | Unique name of the state. |
---|
1360 | @return |
---|
1361 | False if name was not found, true otherwise. |
---|
1362 | */ |
---|
1363 | bool InputManager::requestLeaveState(const std::string& name) |
---|
1364 | { |
---|
1365 | // get pointer from the map with all stored handlers |
---|
1366 | std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name); |
---|
1367 | if (it != inputStatesByName_.end()) |
---|
1368 | { |
---|
1369 | // exists |
---|
1370 | if (activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
1371 | { |
---|
1372 | // active |
---|
1373 | stateLeaveRequests_.insert(it->second); |
---|
1374 | return true; |
---|
1375 | } |
---|
1376 | } |
---|
1377 | return false; |
---|
1378 | } |
---|
1379 | |
---|
1380 | |
---|
1381 | // ############################################################ |
---|
1382 | // ##### Console Commands ##### |
---|
1383 | // ########## ########## |
---|
1384 | // ############################################################ |
---|
1385 | |
---|
1386 | /** |
---|
1387 | @brief |
---|
1388 | Starts joy stick calibration. |
---|
1389 | */ |
---|
1390 | void InputManager::calibrate() |
---|
1391 | { |
---|
1392 | getInstance().bCalibrating_ = true; |
---|
1393 | getInstance().requestEnterState("calibrator"); |
---|
1394 | } |
---|
1395 | |
---|
1396 | /** |
---|
1397 | @brief |
---|
1398 | Reloads the input system |
---|
1399 | */ |
---|
1400 | void InputManager::reload(bool joyStickSupport) |
---|
1401 | { |
---|
1402 | getInstance().reloadInputSystem(joyStickSupport); |
---|
1403 | } |
---|
1404 | } |
---|