Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud3/src/core/InputManager.cc @ 1295

Last change on this file since 1295 was 1273, checked in by rgrieder, 17 years ago
  • fixed the visual problem with overlays: They're now correctly displayed (Problem was RTT Mode in ogre.cfg which I have changed half a year ago. hope it does't cause any segfaults now…)
  • removed false debug output in InputManager
  • removed 2 gcc warnings
File size: 37.6 KB
Line 
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
41namespace 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      joySticksSize_(0),
55      state_(IS_UNINIT), stateRequest_(IS_UNINIT),
56      keyboardModifiers_(0)
57  {
58    RegisterObject(InputManager);
59  }
60
61  /**
62    @brief The one instance of the InputManager is stored in this function.
63    @return A reference to the only instance of the InputManager
64  */
65  InputManager& InputManager::_getSingleton()
66  {
67    static InputManager theOnlyInstance;
68    return theOnlyInstance;
69  }
70
71  /**
72    @brief Destructor only called at the end of the program, after main.
73  */
74  InputManager::~InputManager()
75  {
76    _destroy();
77  }
78
79  /**
80    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
81           the joysticks and assigns the key bindings.
82    @param windowHnd The window handle of the render window
83    @param windowWidth The width of the render window
84    @param windowHeight The height of the render window
85  */
86  bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight,
87        bool createKeyboard, bool createMouse, bool createJoySticks)
88  {
89    if (state_ == IS_UNINIT)
90    {
91      CCOUT(3) << "Initialising Input System..." << std::endl;
92      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
93
94      OIS::ParamList paramList;
95      std::ostringstream windowHndStr;
96
97      // Fill parameter list
98      windowHndStr << (unsigned int)windowHnd;
99      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
100
101//#if defined OIS_LINUX_PLATFORM
102//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
103//#endif
104
105      try
106      {
107        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
108        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
109      }
110      catch (OIS::Exception ex)
111      {
112        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
113            << "OIS message: \"" << ex.eText << "\"" << std::endl;
114        inputSystem_ = 0;
115        return false;
116      }
117
118      if (createKeyboard)
119        _initialiseKeyboard();
120
121      if (createMouse)
122        _initialiseMouse();
123
124      if (createJoySticks)
125        _initialiseJoySticks();
126
127      // Set mouse/joystick region
128      setWindowExtents(windowWidth, windowHeight);
129
130      state_ = IS_NONE;
131      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
132    }
133    else
134    {
135      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
136    }
137
138    // InputManager holds the input buffer --> create one and add it.
139    addKeyHandler(new InputBuffer(), "buffer");
140
141    KeyBinder* binder = new KeyBinder();
142    binder->loadBindings();
143    addKeyHandler(binder, "keybinder");
144    addMouseHandler(binder, "keybinder");
145    addJoyStickHandler(binder, "keybinder");
146
147    // Read all the key bindings and assign them
148    //if (!_loadBindings())
149    //  return false;
150
151    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
152    return true;
153  }
154
155  /**
156    @brief Creates a keyboard and sets the event handler.
157    @return False if keyboard stays uninitialised, true otherwise.
158  */
159  bool InputManager::_initialiseKeyboard()
160  {
161    if (keyboard_ != 0)
162    {
163      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
164      return true;
165    }
166    try
167    {
168      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
169      {
170        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
171        // register our listener in OIS.
172        keyboard_->setEventCallback(this);
173        // note: OIS will not detect keys that have already been down when the keyboard was created.
174        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
175        return true;
176      }
177      else
178      {
179        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
180        return false;
181      }
182    }
183    catch (OIS::Exception ex)
184    {
185      // TODO: Test this output regarding formatting
186      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
187          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
188      keyboard_ = 0;
189      return false;
190    }
191  }
192
193  /**
194    @brief Creates a mouse and sets the event handler.
195    @return False if mouse stays uninitialised, true otherwise.
196  */
197  bool InputManager::_initialiseMouse()
198  {
199    if (mouse_ != 0)
200    {
201      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
202      return true;
203    }
204    try
205    {
206      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
207      {
208        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
209        // register our listener in OIS.
210        mouse_->setEventCallback(this);
211        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
212        return true;
213      }
214      else
215      {
216        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
217        return false;
218      }
219    }
220    catch (OIS::Exception ex)
221    {
222      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
223          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
224      mouse_ = 0;
225      return false;
226    }
227  }
228
229  /**
230    @brief Creates all joy sticks and sets the event handler.
231    @return False joy stick stay uninitialised, true otherwise.
232  */
233  bool InputManager::_initialiseJoySticks()
234  {
235    if (joySticksSize_ > 0)
236    {
237      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
238      return true;
239    }
240    bool success = false;
241    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
242    {
243      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
244      {
245        try
246        {
247          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
248          joySticks_.push_back(stig);
249          // register our listener in OIS.
250          stig->setEventCallback(this);
251          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
252          success = true;
253        }
254        catch (OIS::Exception ex)
255        {
256          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
257              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
258        }
259      }
260    }
261    else
262    {
263      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
264      return false;
265    }
266    joySticksSize_ = joySticks_.size();
267    activeJoyStickHandlers_.resize(joySticksSize_);
268    joyStickButtonsDown_.resize(joySticksSize_);
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    if (state_ != IS_UNINIT)
278    {
279      CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
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      CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
301    }
302  }
303
304  /**
305    @brief Destroys the keyboard and sets it to 0.
306  */
307  void InputManager::_destroyKeyboard()
308  {
309    if (keyboard_)
310      // inputSystem_ can never be 0, or else the code is mistaken
311      inputSystem_->destroyInputObject(keyboard_);
312    keyboard_ = 0;
313    activeKeyHandlers_.clear();
314    keysDown_.clear();
315    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
316  }
317
318  /**
319    @brief Destroys the mouse and sets it to 0.
320  */
321  void InputManager::_destroyMouse()
322  {
323    if (mouse_)
324      // inputSystem_ can never be 0, or else the code is mistaken
325      inputSystem_->destroyInputObject(mouse_);
326    mouse_ = 0;
327    activeMouseHandlers_.clear();
328    mouseButtonsDown_.clear();
329    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
330  }
331
332  /**
333    @brief Destroys all the joy sticks and resizes the lists to 0.
334  */
335  void InputManager::_destroyJoySticks()
336  {
337    if (joySticksSize_ > 0)
338    {
339      // note: inputSystem_ can never be 0, or else the code is mistaken
340      for (unsigned int i = 0; i < joySticksSize_; i++)
341        if (joySticks_[i] != 0)
342          inputSystem_->destroyInputObject(joySticks_[i]);
343
344      joySticks_.clear();
345      joySticksSize_ = 0;
346      activeJoyStickHandlers_.clear();
347      joyStickButtonsDown_.clear();
348    }
349    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
350  }
351
352
353  // #################################
354  // ### Private Interface Methods ###
355  // #################################
356  // #################################
357
358  /**
359    @brief Updates the InputManager. Tick is called by Orxonox.
360    @param dt Delta time
361  */
362  void InputManager::tick(float dt)
363  {
364    if (state_ == IS_UNINIT)
365      return;
366
367    // reset the game if it has changed
368    if (state_ != stateRequest_)
369    {
370      if (stateRequest_ != IS_CUSTOM)
371      {
372        activeKeyHandlers_.clear();
373        activeMouseHandlers_.clear();
374        for (unsigned int i = 0; i < joySticksSize_; i++)
375          activeJoyStickHandlers_[i].clear();
376
377        switch (stateRequest_)
378        {
379        case IS_NORMAL:
380          // normal play mode
381          // note: we assume that the handlers exist since otherwise, something's wrong anyway.
382          activeKeyHandlers_.push_back(keyHandlers_["keybinder"]);
383          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
384          activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
385          for (unsigned int i = 0; i < joySticksSize_; i++)
386            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
387          break;
388
389        case IS_GUI:
390          // FIXME: do stuff
391          break;
392
393        case IS_CONSOLE:
394          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
395          activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
396          for (unsigned int i = 0; i < joySticksSize_; i++)
397            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
398
399          activeKeyHandlers_.push_back(keyHandlers_["buffer"]);
400          break;
401
402        default:
403          break;
404        }
405        state_ = stateRequest_;
406      }
407    }
408
409    // Capture all the input. This calls the event handlers in InputManager.
410    if (mouse_)
411      mouse_->capture();
412    if (keyboard_)
413      keyboard_->capture();
414    for (unsigned  int i = 0; i < joySticksSize_; i++)
415      joySticks_[i]->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_[iJoyStick].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 != (KeyCode::Enum)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 == (KeyCode::Enum)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 scrolled event
512    if (e.state.Z.rel != 0)
513    {
514      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
515        activeMouseHandlers_[i]->mouseScrolled(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] != (MouseButton::Enum)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] == (MouseButton::Enum)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, int windowWidth, int windowHeight,
677    bool createKeyboard, bool createMouse, 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}
Note: See TracBrowser for help on using the repository browser.