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