1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the InputManager that captures all the input from OIS |
---|
32 | and redirects it to handlers. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "InputManager.h" |
---|
36 | #include "CoreIncludes.h" |
---|
37 | #include "Debug.h" |
---|
38 | #include "InputBuffer.h" |
---|
39 | #include "InputHandler.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | // ############################### |
---|
44 | // ### Internal Methods ### |
---|
45 | // ############################### |
---|
46 | // ############################### |
---|
47 | |
---|
48 | /** |
---|
49 | @brief Constructor only sets member fields to initial zero values |
---|
50 | and registers the class in the class hierarchy. |
---|
51 | */ |
---|
52 | InputManager::InputManager() : |
---|
53 | inputSystem_(0), keyboard_(0), mouse_(0), |
---|
54 | state_(IS_UNINIT), stateRequest_(IS_UNINIT), |
---|
55 | joySticksSize_(0) |
---|
56 | { |
---|
57 | RegisterObject(InputManager); |
---|
58 | |
---|
59 | //this->joySticks_.reserve(5); |
---|
60 | //this->activeJoyStickHandlers_.reserve(10); |
---|
61 | this->activeKeyHandlers_.reserve(10); |
---|
62 | this->activeMouseHandlers_.reserve(10); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief The one instance of the InputManager is stored in this function. |
---|
67 | @return A reference to the only instance of the InputManager |
---|
68 | */ |
---|
69 | InputManager& InputManager::_getSingleton() |
---|
70 | { |
---|
71 | static InputManager theOnlyInstance; |
---|
72 | return theOnlyInstance; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | @brief Destructor only called at the end of the program, after main. |
---|
77 | */ |
---|
78 | InputManager::~InputManager() |
---|
79 | { |
---|
80 | _destroy(); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | @brief Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
85 | the joysticks and assigns the key bindings. |
---|
86 | @param windowHnd The window handle of the render window |
---|
87 | @param windowWidth The width of the render window |
---|
88 | @param windowHeight The height of the render window |
---|
89 | */ |
---|
90 | bool InputManager::_initialise(const size_t windowHnd, const int windowWidth, const int windowHeight, |
---|
91 | const bool createKeyboard, const bool createMouse, const bool createJoySticks) |
---|
92 | { |
---|
93 | if (state_ == IS_UNINIT) |
---|
94 | { |
---|
95 | CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl; |
---|
96 | |
---|
97 | OIS::ParamList paramList; |
---|
98 | std::ostringstream windowHndStr; |
---|
99 | |
---|
100 | // Fill parameter list |
---|
101 | windowHndStr << (unsigned int)windowHnd; |
---|
102 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
103 | |
---|
104 | //#if defined OIS_LINUX_PLATFORM |
---|
105 | // paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
106 | //#endif |
---|
107 | |
---|
108 | try |
---|
109 | { |
---|
110 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
111 | CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl; |
---|
112 | } |
---|
113 | catch (OIS::Exception ex) |
---|
114 | { |
---|
115 | CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system." |
---|
116 | << "OIS message: \"" << ex.eText << "\"" << std::endl; |
---|
117 | inputSystem_ = 0; |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | if (createKeyboard) |
---|
122 | _initialiseKeyboard(); |
---|
123 | |
---|
124 | if (createMouse) |
---|
125 | _initialiseMouse(); |
---|
126 | |
---|
127 | if (createJoySticks) |
---|
128 | _initialiseJoySticks(); |
---|
129 | |
---|
130 | // Set mouse/joystick region |
---|
131 | setWindowExtents(windowWidth, windowHeight); |
---|
132 | |
---|
133 | state_ = IS_NONE; |
---|
134 | CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl; |
---|
139 | } |
---|
140 | |
---|
141 | // InputManager holds the input buffer --> create one and add it. |
---|
142 | addKeyHandler(new InputBuffer(), "buffer"); |
---|
143 | |
---|
144 | KeyBinder* binder = new KeyBinder(); |
---|
145 | binder->loadBindings(); |
---|
146 | addKeyHandler(binder, "keybinder"); |
---|
147 | addMouseHandler(binder, "keybinder"); |
---|
148 | |
---|
149 | // Read all the key bindings and assign them |
---|
150 | //if (!_loadBindings()) |
---|
151 | // return false; |
---|
152 | |
---|
153 | CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; |
---|
154 | return true; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | @brief Creates a keyboard and sets the event handler. |
---|
159 | @return False if keyboard stays uninitialised, true otherwise. |
---|
160 | */ |
---|
161 | bool InputManager::_initialiseKeyboard() |
---|
162 | { |
---|
163 | if (keyboard_ != 0) |
---|
164 | { |
---|
165 | CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; |
---|
166 | return true; |
---|
167 | } |
---|
168 | try |
---|
169 | { |
---|
170 | if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
171 | { |
---|
172 | keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true); |
---|
173 | // register our listener in OIS. |
---|
174 | keyboard_->setEventCallback(this); |
---|
175 | // note: OIS will not detect keys that have already been down when the keyboard was created. |
---|
176 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
177 | return true; |
---|
178 | } |
---|
179 | else |
---|
180 | { |
---|
181 | CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; |
---|
182 | return false; |
---|
183 | } |
---|
184 | } |
---|
185 | catch (OIS::Exception ex) |
---|
186 | { |
---|
187 | // TODO: Test this output regarding formatting |
---|
188 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" |
---|
189 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
190 | keyboard_ = 0; |
---|
191 | return false; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | @brief Creates a mouse and sets the event handler. |
---|
197 | @return False if mouse stays uninitialised, true otherwise. |
---|
198 | */ |
---|
199 | bool InputManager::_initialiseMouse() |
---|
200 | { |
---|
201 | if (mouse_ != 0) |
---|
202 | { |
---|
203 | CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; |
---|
204 | return true; |
---|
205 | } |
---|
206 | try |
---|
207 | { |
---|
208 | if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
209 | { |
---|
210 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
211 | // register our listener in OIS. |
---|
212 | mouse_->setEventCallback(this); |
---|
213 | CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; |
---|
214 | return true; |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; |
---|
219 | return false; |
---|
220 | } |
---|
221 | } |
---|
222 | catch (OIS::Exception ex) |
---|
223 | { |
---|
224 | CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n" |
---|
225 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
226 | mouse_ = 0; |
---|
227 | return false; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | @brief Creates all joy sticks and sets the event handler. |
---|
233 | @return False joy stick stay uninitialised, true otherwise. |
---|
234 | */ |
---|
235 | bool InputManager::_initialiseJoySticks() |
---|
236 | { |
---|
237 | if (joySticksSize_ > 0) |
---|
238 | { |
---|
239 | CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; |
---|
240 | return true; |
---|
241 | } |
---|
242 | bool success = false; |
---|
243 | if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) |
---|
244 | { |
---|
245 | for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
246 | { |
---|
247 | try |
---|
248 | { |
---|
249 | OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); |
---|
250 | joySticks_.push_back(stig); |
---|
251 | // register our listener in OIS. |
---|
252 | stig->setEventCallback(this); |
---|
253 | CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; |
---|
254 | success = true; |
---|
255 | } |
---|
256 | catch (OIS::Exception ex) |
---|
257 | { |
---|
258 | CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" |
---|
259 | << "OIS error message: \"" << ex.eText << "\"" << std::endl; |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; |
---|
266 | return false; |
---|
267 | } |
---|
268 | joySticksSize_ = joySticks_.size(); |
---|
269 | return success; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | @brief Destroys all the created input devices and sets the InputManager to construction state. |
---|
274 | */ |
---|
275 | void InputManager::_destroy() |
---|
276 | { |
---|
277 | CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl; |
---|
278 | |
---|
279 | if (state_ != IS_UNINIT) |
---|
280 | { |
---|
281 | if (keyHandlers_.find("buffer") != keyHandlers_.end()) |
---|
282 | delete keyHandlers_["buffer"]; |
---|
283 | |
---|
284 | if (keyHandlers_.find("keybinder") != keyHandlers_.end()) |
---|
285 | delete keyHandlers_["keybinder"]; |
---|
286 | |
---|
287 | keyHandlers_.clear(); |
---|
288 | mouseHandlers_.clear(); |
---|
289 | joyStickHandlers_.clear(); |
---|
290 | |
---|
291 | _destroyKeyboard(); |
---|
292 | _destroyMouse(); |
---|
293 | _destroyJoySticks(); |
---|
294 | |
---|
295 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
296 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
297 | inputSystem_ = 0; |
---|
298 | |
---|
299 | state_ = IS_UNINIT; |
---|
300 | } |
---|
301 | else |
---|
302 | CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl; |
---|
303 | |
---|
304 | CCOUT(ORX_DEBUG) << "Destroying done." << std::endl; |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | @brief Destroys the keyboard and sets it to 0. |
---|
309 | */ |
---|
310 | void InputManager::_destroyKeyboard() |
---|
311 | { |
---|
312 | if (keyboard_) |
---|
313 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
314 | inputSystem_->destroyInputObject(keyboard_); |
---|
315 | keyboard_ = 0; |
---|
316 | activeKeyHandlers_.clear(); |
---|
317 | keysDown_.clear(); |
---|
318 | CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | @brief Destroys the mouse and sets it to 0. |
---|
323 | */ |
---|
324 | void InputManager::_destroyMouse() |
---|
325 | { |
---|
326 | if (mouse_) |
---|
327 | // inputSystem_ can never be 0, or else the code is mistaken |
---|
328 | inputSystem_->destroyInputObject(mouse_); |
---|
329 | mouse_ = 0; |
---|
330 | activeMouseHandlers_.clear(); |
---|
331 | mouseButtonsDown_.clear(); |
---|
332 | CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | @brief Destroys all the joy sticks and resizes the lists to 0. |
---|
337 | */ |
---|
338 | void InputManager::_destroyJoySticks() |
---|
339 | { |
---|
340 | if (joySticksSize_ > 0) |
---|
341 | { |
---|
342 | // note: inputSystem_ can never be 0, or else the code is mistaken |
---|
343 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
344 | if (joySticks_[i] != 0) |
---|
345 | inputSystem_->destroyInputObject(joySticks_[i]); |
---|
346 | |
---|
347 | joySticks_.clear(); |
---|
348 | joySticksSize_ = 0; |
---|
349 | activeJoyStickHandlers_.clear(); |
---|
350 | joyStickButtonsDown_.clear(); |
---|
351 | } |
---|
352 | CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | // ################################# |
---|
357 | // ### Private Interface Methods ### |
---|
358 | // ################################# |
---|
359 | // ################################# |
---|
360 | |
---|
361 | /** |
---|
362 | @brief Updates the InputManager. Tick is called by Orxonox. |
---|
363 | @param dt Delta time |
---|
364 | */ |
---|
365 | void InputManager::tick(float dt) |
---|
366 | { |
---|
367 | if (state_ == IS_UNINIT) |
---|
368 | return; |
---|
369 | |
---|
370 | // reset the game if it has changed |
---|
371 | if (state_ != stateRequest_) |
---|
372 | { |
---|
373 | if (stateRequest_ != IS_CUSTOM) |
---|
374 | { |
---|
375 | activeKeyHandlers_.clear(); |
---|
376 | activeMouseHandlers_.clear(); |
---|
377 | activeJoyStickHandlers_.clear(); |
---|
378 | |
---|
379 | switch (stateRequest_) |
---|
380 | { |
---|
381 | case IS_NORMAL: |
---|
382 | // normal play mode |
---|
383 | // note: we assume that the handlers exist since otherwise, something's wrong anyway. |
---|
384 | activeKeyHandlers_.push_back(keyHandlers_["keybinder"]); |
---|
385 | activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]); |
---|
386 | activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]); |
---|
387 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
388 | activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]); |
---|
389 | break; |
---|
390 | |
---|
391 | case IS_GUI: |
---|
392 | // FIXME: do stuff |
---|
393 | break; |
---|
394 | |
---|
395 | case IS_CONSOLE: |
---|
396 | activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]); |
---|
397 | activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]); |
---|
398 | for (unsigned int i = 0; i < joySticksSize_; i++) |
---|
399 | activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]); |
---|
400 | |
---|
401 | activeKeyHandlers_.push_back(keyHandlers_["buffer"]); |
---|
402 | break; |
---|
403 | |
---|
404 | default: |
---|
405 | break; |
---|
406 | } |
---|
407 | state_ = stateRequest_; |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | // Capture all the input. This calls the event handlers in InputManager. |
---|
412 | if (mouse_) |
---|
413 | mouse_->capture(); |
---|
414 | if (keyboard_) |
---|
415 | keyboard_->capture(); |
---|
416 | |
---|
417 | |
---|
418 | // call all the handlers for the held key events |
---|
419 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
420 | for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++) |
---|
421 | activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_)); |
---|
422 | |
---|
423 | // call all the handlers for the held mouse button events |
---|
424 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
425 | for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++) |
---|
426 | activeMouseHandlers_[iHandler]->mouseButtonHeld(mouse_->getMouseState(), mouseButtonsDown_[iButton]); |
---|
427 | |
---|
428 | // call all the handlers for the held joy stick button events |
---|
429 | for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) |
---|
430 | for (unsigned int iButton = 0; iButton < joyStickButtonsDown_.size(); iButton++) |
---|
431 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
432 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld( |
---|
433 | JoyStickState(joySticks_[iJoyStick]->getJoyStickState(), iJoyStick), joyStickButtonsDown_[iJoyStick][iButton]); |
---|
434 | } |
---|
435 | |
---|
436 | // ###### Key Events ###### |
---|
437 | |
---|
438 | /** |
---|
439 | @brief Event handler for the keyPressed Event. |
---|
440 | @param e Event information |
---|
441 | */ |
---|
442 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
443 | { |
---|
444 | // check whether the key already is in the list (can happen when focus was lost) |
---|
445 | unsigned int iKey = 0; |
---|
446 | while (iKey < keysDown_.size() && keysDown_[iKey].key != e.key) |
---|
447 | iKey++; |
---|
448 | if (iKey == keysDown_.size()) |
---|
449 | keysDown_.push_back(Key(e)); |
---|
450 | |
---|
451 | // update modifiers |
---|
452 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
453 | keyboardModifiers_ |= KeyboardModifier::Alt; // alt key |
---|
454 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
455 | keyboardModifiers_ |= KeyboardModifier::Ctrl; // ctrl key |
---|
456 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
457 | keyboardModifiers_ |= KeyboardModifier::Shift; // shift key |
---|
458 | |
---|
459 | for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) |
---|
460 | activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_)); |
---|
461 | |
---|
462 | return true; |
---|
463 | } |
---|
464 | |
---|
465 | /** |
---|
466 | @brief Event handler for the keyReleased Event. |
---|
467 | @param e Event information |
---|
468 | */ |
---|
469 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
470 | { |
---|
471 | // remove the key from the keysDown_ list |
---|
472 | for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++) |
---|
473 | { |
---|
474 | if (keysDown_[iKey].key == e.key) |
---|
475 | { |
---|
476 | keysDown_.erase(keysDown_.begin() + iKey); |
---|
477 | break; |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | // update modifiers |
---|
482 | if(e.key == OIS::KC_RMENU || e.key == OIS::KC_LMENU) |
---|
483 | keyboardModifiers_ &= ~KeyboardModifier::Alt; // alt key |
---|
484 | if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL) |
---|
485 | keyboardModifiers_ &= ~KeyboardModifier::Ctrl; // ctrl key |
---|
486 | if(e.key == OIS::KC_RSHIFT || e.key == OIS::KC_LSHIFT) |
---|
487 | keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key |
---|
488 | |
---|
489 | for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++) |
---|
490 | activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_)); |
---|
491 | |
---|
492 | return true; |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | // ###### Mouse Events ###### |
---|
497 | |
---|
498 | /** |
---|
499 | @brief Event handler for the mouseMoved Event. |
---|
500 | @param e Event information |
---|
501 | */ |
---|
502 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
503 | { |
---|
504 | // check for actual moved event |
---|
505 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
506 | { |
---|
507 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
508 | activeMouseHandlers_[i]->mouseMoved(e.state); |
---|
509 | } |
---|
510 | |
---|
511 | // check for mouse wheel turned event |
---|
512 | if (e.state.Z.rel != 0) |
---|
513 | { |
---|
514 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
515 | activeMouseHandlers_[i]->mouseWheelTurned(e.state); |
---|
516 | } |
---|
517 | |
---|
518 | return true; |
---|
519 | } |
---|
520 | |
---|
521 | /** |
---|
522 | @brief Event handler for the mousePressed Event. |
---|
523 | @param e Event information |
---|
524 | @param id The ID of the mouse button |
---|
525 | */ |
---|
526 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
527 | { |
---|
528 | // check whether the button already is in the list (can happen when focus was lost) |
---|
529 | unsigned int iButton = 0; |
---|
530 | while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != id) |
---|
531 | iButton++; |
---|
532 | if (iButton == mouseButtonsDown_.size()) |
---|
533 | mouseButtonsDown_.push_back((MouseButton::Enum)id); |
---|
534 | |
---|
535 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
536 | activeMouseHandlers_[i]->mouseButtonPressed(e.state, (MouseButton::Enum)id); |
---|
537 | |
---|
538 | return true; |
---|
539 | } |
---|
540 | |
---|
541 | /** |
---|
542 | @brief Event handler for the mouseReleased Event. |
---|
543 | @param e Event information |
---|
544 | @param id The ID of the mouse button |
---|
545 | */ |
---|
546 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
547 | { |
---|
548 | // remove the button from the keysDown_ list |
---|
549 | for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++) |
---|
550 | { |
---|
551 | if (mouseButtonsDown_[iButton] == id) |
---|
552 | { |
---|
553 | mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton); |
---|
554 | break; |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++) |
---|
559 | activeMouseHandlers_[i]->mouseButtonReleased(e.state, (MouseButton::Enum)id); |
---|
560 | |
---|
561 | return true; |
---|
562 | } |
---|
563 | |
---|
564 | |
---|
565 | // ###### Joy Stick Events ###### |
---|
566 | |
---|
567 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
568 | { |
---|
569 | // use the device to identify which one called the method |
---|
570 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
571 | unsigned int iJoyStick = 0; |
---|
572 | while (joySticks_[iJoyStick] != joyStick) |
---|
573 | iJoyStick++; |
---|
574 | |
---|
575 | // check whether the button already is in the list (can happen when focus was lost) |
---|
576 | std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
577 | unsigned int iButton = 0; |
---|
578 | while (iButton < buttonsDown.size() && buttonsDown[iButton] != button) |
---|
579 | iButton++; |
---|
580 | if (iButton == buttonsDown.size()) |
---|
581 | buttonsDown.push_back(button); |
---|
582 | |
---|
583 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
584 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(JoyStickState(arg.state, iJoyStick), button); |
---|
585 | |
---|
586 | return true; |
---|
587 | } |
---|
588 | |
---|
589 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
590 | { |
---|
591 | // use the device to identify which one called the method |
---|
592 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
593 | unsigned int iJoyStick = 0; |
---|
594 | while (joySticks_[iJoyStick] != joyStick) |
---|
595 | iJoyStick++; |
---|
596 | |
---|
597 | // remove the button from the joyStickButtonsDown_ list |
---|
598 | std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick]; |
---|
599 | for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++) |
---|
600 | { |
---|
601 | if (buttonsDown[iButton] == button) |
---|
602 | { |
---|
603 | buttonsDown.erase(buttonsDown.begin() + iButton); |
---|
604 | break; |
---|
605 | } |
---|
606 | } |
---|
607 | |
---|
608 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
609 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(JoyStickState(arg.state, iJoyStick), button); |
---|
610 | |
---|
611 | return true; |
---|
612 | } |
---|
613 | |
---|
614 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
615 | { |
---|
616 | // use the device to identify which one called the method |
---|
617 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
618 | unsigned int iJoyStick = 0; |
---|
619 | while (joySticks_[iJoyStick] != joyStick) |
---|
620 | iJoyStick++; |
---|
621 | |
---|
622 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
623 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(JoyStickState(arg.state, iJoyStick), axis); |
---|
624 | |
---|
625 | return true; |
---|
626 | } |
---|
627 | |
---|
628 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
629 | { |
---|
630 | // use the device to identify which one called the method |
---|
631 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
632 | unsigned int iJoyStick = 0; |
---|
633 | while (joySticks_[iJoyStick] != joyStick) |
---|
634 | iJoyStick++; |
---|
635 | |
---|
636 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
637 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickSliderMoved(JoyStickState(arg.state, iJoyStick), id); |
---|
638 | |
---|
639 | return true; |
---|
640 | } |
---|
641 | |
---|
642 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
643 | { |
---|
644 | // use the device to identify which one called the method |
---|
645 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
646 | unsigned int iJoyStick = 0; |
---|
647 | while (joySticks_[iJoyStick] != joyStick) |
---|
648 | iJoyStick++; |
---|
649 | |
---|
650 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
651 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickPovMoved(JoyStickState(arg.state, iJoyStick), id); |
---|
652 | |
---|
653 | return true; |
---|
654 | } |
---|
655 | |
---|
656 | bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id) |
---|
657 | { |
---|
658 | // use the device to identify which one called the method |
---|
659 | OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device; |
---|
660 | unsigned int iJoyStick = 0; |
---|
661 | while (joySticks_[iJoyStick] != joyStick) |
---|
662 | iJoyStick++; |
---|
663 | |
---|
664 | for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
665 | activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id); |
---|
666 | |
---|
667 | return true; |
---|
668 | } |
---|
669 | |
---|
670 | |
---|
671 | // ################################ |
---|
672 | // ### Static Interface Methods ### |
---|
673 | // ################################ |
---|
674 | // ################################ |
---|
675 | |
---|
676 | bool InputManager::initialise(const size_t windowHnd, const int windowWidth, const int windowHeight, |
---|
677 | const bool createKeyboard, const bool createMouse, const bool createJoySticks) |
---|
678 | { |
---|
679 | return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, |
---|
680 | createKeyboard, createMouse, createJoySticks); |
---|
681 | } |
---|
682 | |
---|
683 | bool InputManager::initialiseKeyboard() |
---|
684 | { |
---|
685 | return _getSingleton()._initialiseKeyboard(); |
---|
686 | } |
---|
687 | |
---|
688 | bool InputManager::initialiseMouse() |
---|
689 | { |
---|
690 | return _getSingleton()._initialiseMouse(); |
---|
691 | } |
---|
692 | |
---|
693 | bool InputManager::initialiseJoySticks() |
---|
694 | { |
---|
695 | return _getSingleton()._initialiseJoySticks(); |
---|
696 | } |
---|
697 | |
---|
698 | int InputManager::numberOfKeyboards() |
---|
699 | { |
---|
700 | if (_getSingleton().keyboard_ != 0) |
---|
701 | return 1; |
---|
702 | else |
---|
703 | return 0; |
---|
704 | } |
---|
705 | |
---|
706 | int InputManager::numberOfMice() |
---|
707 | { |
---|
708 | if (_getSingleton().mouse_ != 0) |
---|
709 | return 1; |
---|
710 | else |
---|
711 | return 0; |
---|
712 | } |
---|
713 | |
---|
714 | int InputManager::numberOfJoySticks() |
---|
715 | { |
---|
716 | return _getSingleton().joySticksSize_; |
---|
717 | } |
---|
718 | |
---|
719 | bool InputManager::isKeyDown(KeyCode::Enum key) |
---|
720 | { |
---|
721 | if (_getSingleton().keyboard_) |
---|
722 | return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key); |
---|
723 | else |
---|
724 | return false; |
---|
725 | } |
---|
726 | |
---|
727 | bool InputManager::isModifierDown(KeyboardModifier::Enum modifier) |
---|
728 | { |
---|
729 | if (_getSingleton().keyboard_) |
---|
730 | return isModifierDown(modifier); |
---|
731 | else |
---|
732 | return false; |
---|
733 | } |
---|
734 | |
---|
735 | const MouseState InputManager::getMouseState() |
---|
736 | { |
---|
737 | if (_getSingleton().mouse_) |
---|
738 | return _getSingleton().mouse_->getMouseState(); |
---|
739 | else |
---|
740 | return MouseState(); |
---|
741 | } |
---|
742 | |
---|
743 | const JoyStickState InputManager::getJoyStickState(unsigned int ID) |
---|
744 | { |
---|
745 | if (ID < _getSingleton().joySticksSize_) |
---|
746 | return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID); |
---|
747 | else |
---|
748 | return JoyStickState(); |
---|
749 | } |
---|
750 | |
---|
751 | |
---|
752 | void InputManager::destroy() |
---|
753 | { |
---|
754 | _getSingleton()._destroy(); |
---|
755 | } |
---|
756 | |
---|
757 | void InputManager::destroyKeyboard() |
---|
758 | { |
---|
759 | return _getSingleton()._destroyKeyboard(); |
---|
760 | } |
---|
761 | |
---|
762 | void InputManager::destroyMouse() |
---|
763 | { |
---|
764 | return _getSingleton()._destroyMouse(); |
---|
765 | } |
---|
766 | |
---|
767 | void InputManager::destroyJoySticks() |
---|
768 | { |
---|
769 | return _getSingleton()._destroyJoySticks(); |
---|
770 | } |
---|
771 | |
---|
772 | |
---|
773 | /** |
---|
774 | @brief Adjusts the mouse window metrics. |
---|
775 | This method has to be called every time the size of the window changes. |
---|
776 | @param width The new width of the render window |
---|
777 | @param height the new height of the render window |
---|
778 | */ |
---|
779 | void InputManager::setWindowExtents(const int width, const int height) |
---|
780 | { |
---|
781 | if (_getSingleton().mouse_) |
---|
782 | { |
---|
783 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
784 | const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); |
---|
785 | mouseState.width = width; |
---|
786 | mouseState.height = height; |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | /** |
---|
791 | @brief Sets the input mode to either GUI, inGame or Buffer |
---|
792 | @param mode The new input mode |
---|
793 | @remark Only has an affect if the mode actually changes |
---|
794 | */ |
---|
795 | void InputManager::setInputState(const InputState state) |
---|
796 | { |
---|
797 | _getSingleton().stateRequest_ = state; |
---|
798 | } |
---|
799 | |
---|
800 | /** |
---|
801 | @brief Returns the current input handling method |
---|
802 | @return The current input mode. |
---|
803 | */ |
---|
804 | InputManager::InputState InputManager::getInputState() |
---|
805 | { |
---|
806 | return _getSingleton().state_; |
---|
807 | } |
---|
808 | |
---|
809 | |
---|
810 | // ###### KeyHandler ###### |
---|
811 | |
---|
812 | /** |
---|
813 | @brief Adds a new key handler. |
---|
814 | @param handler Pointer to the handler object. |
---|
815 | @param name Unique name of the handler. |
---|
816 | @return True if added, false if name already existed. |
---|
817 | */ |
---|
818 | bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name) |
---|
819 | { |
---|
820 | if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end()) |
---|
821 | { |
---|
822 | _getSingleton().keyHandlers_[name] = handler; |
---|
823 | return true; |
---|
824 | } |
---|
825 | else |
---|
826 | return false; |
---|
827 | } |
---|
828 | |
---|
829 | /** |
---|
830 | @brief Removes a Key handler from the list. |
---|
831 | @param name Unique name of the handler. |
---|
832 | @return True if removal was successful, false if name was not found. |
---|
833 | */ |
---|
834 | bool InputManager::removeKeyHandler(const std::string &name) |
---|
835 | { |
---|
836 | disableKeyHandler(name); |
---|
837 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); |
---|
838 | if (it != _getSingleton().keyHandlers_.end()) |
---|
839 | { |
---|
840 | _getSingleton().keyHandlers_.erase(it); |
---|
841 | return true; |
---|
842 | } |
---|
843 | else |
---|
844 | return false; |
---|
845 | } |
---|
846 | |
---|
847 | /** |
---|
848 | @brief Returns the pointer to a handler. |
---|
849 | @param name Unique name of the handler. |
---|
850 | @return Pointer to the instance, 0 if name was not found. |
---|
851 | */ |
---|
852 | KeyHandler* InputManager::getKeyHandler(const std::string& name) |
---|
853 | { |
---|
854 | std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name); |
---|
855 | if (it != _getSingleton().keyHandlers_.end()) |
---|
856 | { |
---|
857 | return (*it).second; |
---|
858 | } |
---|
859 | else |
---|
860 | return 0; |
---|
861 | } |
---|
862 | |
---|
863 | /** |
---|
864 | @brief Enables a specific key handler that has already been added. |
---|
865 | @param name Unique name of the handler. |
---|
866 | @return False if name was not found, true otherwise. |
---|
867 | */ |
---|
868 | bool InputManager::enableKeyHandler(const std::string& name) |
---|
869 | { |
---|
870 | // get pointer from the map with all stored handlers |
---|
871 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
872 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
873 | return false; |
---|
874 | // see whether the handler already is in the list |
---|
875 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
876 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
877 | { |
---|
878 | if ((*it) == (*mapIt).second) |
---|
879 | { |
---|
880 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
881 | return true; |
---|
882 | } |
---|
883 | } |
---|
884 | _getSingleton().activeKeyHandlers_.push_back((*mapIt).second); |
---|
885 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
886 | return true; |
---|
887 | } |
---|
888 | |
---|
889 | /** |
---|
890 | @brief Disables a specific key handler. |
---|
891 | @param name Unique name of the handler. |
---|
892 | @return False if name was not found, true otherwise. |
---|
893 | */ |
---|
894 | bool InputManager::disableKeyHandler(const std::string &name) |
---|
895 | { |
---|
896 | // get pointer from the map with all stored handlers |
---|
897 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
898 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
899 | return false; |
---|
900 | // look for the handler in the list |
---|
901 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
902 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
903 | { |
---|
904 | if ((*it) == (*mapIt).second) |
---|
905 | { |
---|
906 | _getSingleton().activeKeyHandlers_.erase(it); |
---|
907 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
908 | return true; |
---|
909 | } |
---|
910 | } |
---|
911 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
912 | return true; |
---|
913 | } |
---|
914 | |
---|
915 | /** |
---|
916 | @brief Checks whether a key handler is active |
---|
917 | @param name Unique name of the handler. |
---|
918 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
919 | */ |
---|
920 | bool InputManager::isKeyHandlerActive(const std::string& name) |
---|
921 | { |
---|
922 | // get pointer from the map with all stored handlers |
---|
923 | std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name); |
---|
924 | if (mapIt == _getSingleton().keyHandlers_.end()) |
---|
925 | return false; |
---|
926 | // see whether the handler already is in the list |
---|
927 | for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin(); |
---|
928 | it != _getSingleton().activeKeyHandlers_.end(); it++) |
---|
929 | { |
---|
930 | if ((*it) == (*mapIt).second) |
---|
931 | return true; |
---|
932 | } |
---|
933 | return false; |
---|
934 | } |
---|
935 | |
---|
936 | |
---|
937 | // ###### MouseHandler ###### |
---|
938 | /** |
---|
939 | @brief Adds a new mouse handler. |
---|
940 | @param handler Pointer to the handler object. |
---|
941 | @param name Unique name of the handler. |
---|
942 | @return True if added, false if name already existed. |
---|
943 | */ |
---|
944 | bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name) |
---|
945 | { |
---|
946 | if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end()) |
---|
947 | { |
---|
948 | _getSingleton().mouseHandlers_[name] = handler; |
---|
949 | return true; |
---|
950 | } |
---|
951 | else |
---|
952 | return false; |
---|
953 | } |
---|
954 | |
---|
955 | /** |
---|
956 | @brief Removes a Mouse handler from the list. |
---|
957 | @param name Unique name of the handler. |
---|
958 | @return True if removal was successful, false if name was not found. |
---|
959 | */ |
---|
960 | bool InputManager::removeMouseHandler(const std::string &name) |
---|
961 | { |
---|
962 | disableMouseHandler(name); |
---|
963 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); |
---|
964 | if (it != _getSingleton().mouseHandlers_.end()) |
---|
965 | { |
---|
966 | _getSingleton().mouseHandlers_.erase(it); |
---|
967 | return true; |
---|
968 | } |
---|
969 | else |
---|
970 | return false; |
---|
971 | } |
---|
972 | |
---|
973 | /** |
---|
974 | @brief Returns the pointer to a handler. |
---|
975 | @param name Unique name of the handler. |
---|
976 | @return Pointer to the instance, 0 if name was not found. |
---|
977 | */ |
---|
978 | MouseHandler* InputManager::getMouseHandler(const std::string& name) |
---|
979 | { |
---|
980 | std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name); |
---|
981 | if (it != _getSingleton().mouseHandlers_.end()) |
---|
982 | { |
---|
983 | return (*it).second; |
---|
984 | } |
---|
985 | else |
---|
986 | return 0; |
---|
987 | } |
---|
988 | |
---|
989 | /** |
---|
990 | @brief Enables a specific mouse handler that has already been added. |
---|
991 | @param name Unique name of the handler. |
---|
992 | @return False if name was not found, true otherwise. |
---|
993 | */ |
---|
994 | bool InputManager::enableMouseHandler(const std::string& name) |
---|
995 | { |
---|
996 | // get pointer from the map with all stored handlers |
---|
997 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
998 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
999 | return false; |
---|
1000 | // see whether the handler already is in the list |
---|
1001 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
1002 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
1003 | { |
---|
1004 | if ((*it) == (*mapIt).second) |
---|
1005 | { |
---|
1006 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1007 | return true; |
---|
1008 | } |
---|
1009 | } |
---|
1010 | _getSingleton().activeMouseHandlers_.push_back((*mapIt).second); |
---|
1011 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1012 | return true; |
---|
1013 | } |
---|
1014 | |
---|
1015 | /** |
---|
1016 | @brief Disables a specific mouse handler. |
---|
1017 | @param name Unique name of the handler. |
---|
1018 | @return False if name was not found, true otherwise. |
---|
1019 | */ |
---|
1020 | bool InputManager::disableMouseHandler(const std::string &name) |
---|
1021 | { |
---|
1022 | // get pointer from the map with all stored handlers |
---|
1023 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
1024 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
1025 | return false; |
---|
1026 | // look for the handler in the list |
---|
1027 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
1028 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
1029 | { |
---|
1030 | if ((*it) == (*mapIt).second) |
---|
1031 | { |
---|
1032 | _getSingleton().activeMouseHandlers_.erase(it); |
---|
1033 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1034 | return true; |
---|
1035 | } |
---|
1036 | } |
---|
1037 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1038 | return true; |
---|
1039 | } |
---|
1040 | |
---|
1041 | /** |
---|
1042 | @brief Checks whether a mouse handler is active |
---|
1043 | @param name Unique name of the handler. |
---|
1044 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
1045 | */ |
---|
1046 | bool InputManager::isMouseHandlerActive(const std::string& name) |
---|
1047 | { |
---|
1048 | // get pointer from the map with all stored handlers |
---|
1049 | std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name); |
---|
1050 | if (mapIt == _getSingleton().mouseHandlers_.end()) |
---|
1051 | return false; |
---|
1052 | // see whether the handler already is in the list |
---|
1053 | for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin(); |
---|
1054 | it != _getSingleton().activeMouseHandlers_.end(); it++) |
---|
1055 | { |
---|
1056 | if ((*it) == (*mapIt).second) |
---|
1057 | return true; |
---|
1058 | } |
---|
1059 | return false; |
---|
1060 | } |
---|
1061 | |
---|
1062 | |
---|
1063 | // ###### JoyStickHandler ###### |
---|
1064 | |
---|
1065 | /** |
---|
1066 | @brief Adds a new joy stick handler. |
---|
1067 | @param handler Pointer to the handler object. |
---|
1068 | @param name Unique name of the handler. |
---|
1069 | @return True if added, false if name already existed. |
---|
1070 | */ |
---|
1071 | bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name) |
---|
1072 | { |
---|
1073 | if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end()) |
---|
1074 | { |
---|
1075 | _getSingleton().joyStickHandlers_[name] = handler; |
---|
1076 | return true; |
---|
1077 | } |
---|
1078 | else |
---|
1079 | return false; |
---|
1080 | } |
---|
1081 | |
---|
1082 | /** |
---|
1083 | @brief Removes a JoyStick handler from the list. |
---|
1084 | @param name Unique name of the handler. |
---|
1085 | @return True if removal was successful, false if name was not found. |
---|
1086 | */ |
---|
1087 | bool InputManager::removeJoyStickHandler(const std::string &name) |
---|
1088 | { |
---|
1089 | for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); |
---|
1090 | itstick != _getSingleton().joySticks_.end(); itstick++) |
---|
1091 | disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin()); |
---|
1092 | |
---|
1093 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); |
---|
1094 | if (it != _getSingleton().joyStickHandlers_.end()) |
---|
1095 | { |
---|
1096 | _getSingleton().joyStickHandlers_.erase(it); |
---|
1097 | return true; |
---|
1098 | } |
---|
1099 | else |
---|
1100 | return false; |
---|
1101 | } |
---|
1102 | |
---|
1103 | /** |
---|
1104 | @brief Returns the pointer to a handler. |
---|
1105 | @param name Unique name of the handler. |
---|
1106 | @return Pointer to the instance, 0 if name was not found. |
---|
1107 | */ |
---|
1108 | JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name) |
---|
1109 | { |
---|
1110 | std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name); |
---|
1111 | if (it != _getSingleton().joyStickHandlers_.end()) |
---|
1112 | { |
---|
1113 | return (*it).second; |
---|
1114 | } |
---|
1115 | else |
---|
1116 | return 0; |
---|
1117 | } |
---|
1118 | |
---|
1119 | /** |
---|
1120 | @brief Enables a specific joy stick handler that has already been added. |
---|
1121 | @param name Unique name of the handler. |
---|
1122 | @return False if name or id was not found, true otherwise. |
---|
1123 | */ |
---|
1124 | bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID) |
---|
1125 | { |
---|
1126 | // get handler pointer from the map with all stored handlers |
---|
1127 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
1128 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
1129 | return false; |
---|
1130 | |
---|
1131 | // check for existence of the ID |
---|
1132 | if (ID >= _getSingleton().joySticksSize_) |
---|
1133 | return false; |
---|
1134 | |
---|
1135 | // see whether the handler already is in the list |
---|
1136 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
1137 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
1138 | { |
---|
1139 | if ((*it) == (*handlerIt).second) |
---|
1140 | { |
---|
1141 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1142 | return true; |
---|
1143 | } |
---|
1144 | } |
---|
1145 | _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second); |
---|
1146 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1147 | return true; |
---|
1148 | } |
---|
1149 | |
---|
1150 | /** |
---|
1151 | @brief Disables a specific joy stick handler. |
---|
1152 | @param name Unique name of the handler. |
---|
1153 | @return False if name or id was not found, true otherwise. |
---|
1154 | */ |
---|
1155 | bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID) |
---|
1156 | { |
---|
1157 | // get handler pointer from the map with all stored handlers |
---|
1158 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
1159 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
1160 | return false; |
---|
1161 | |
---|
1162 | // check for existence of the ID |
---|
1163 | if (ID >= _getSingleton().joySticksSize_) |
---|
1164 | return false; |
---|
1165 | |
---|
1166 | // look for the handler in the list |
---|
1167 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
1168 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
1169 | { |
---|
1170 | if ((*it) == (*handlerIt).second) |
---|
1171 | { |
---|
1172 | _getSingleton().activeJoyStickHandlers_[ID].erase(it); |
---|
1173 | _getSingleton().stateRequest_ = IS_CUSTOM; |
---|
1174 | return true; |
---|
1175 | } |
---|
1176 | } |
---|
1177 | return true; |
---|
1178 | } |
---|
1179 | |
---|
1180 | /** |
---|
1181 | @brief Checks whether a joy stick handler is active |
---|
1182 | @param name Unique name of the handler. |
---|
1183 | @return False if key handler is not active or doesn't exist, true otherwise. |
---|
1184 | */ |
---|
1185 | bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID) |
---|
1186 | { |
---|
1187 | // get handler pointer from the map with all stored handlers |
---|
1188 | std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name); |
---|
1189 | if (handlerIt == _getSingleton().joyStickHandlers_.end()) |
---|
1190 | return false; |
---|
1191 | |
---|
1192 | // check for existence of the ID |
---|
1193 | if (ID >= _getSingleton().joySticksSize_) |
---|
1194 | return false; |
---|
1195 | |
---|
1196 | // see whether the handler already is in the list |
---|
1197 | for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin(); |
---|
1198 | it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++) |
---|
1199 | { |
---|
1200 | if ((*it) == (*handlerIt).second) |
---|
1201 | return true; |
---|
1202 | } |
---|
1203 | return false; |
---|
1204 | } |
---|
1205 | |
---|
1206 | } |
---|