Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/script_trigger/src/core/InputManager.cc @ 2137

Last change on this file since 2137 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

File size: 37.8 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          if (getMouseHandler("SpaceShip"))
385            activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
386          for (unsigned int i = 0; i < joySticksSize_; i++)
387            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
388          break;
389
390        case IS_GUI:
391          // FIXME: do stuff
392          break;
393
394        case IS_CONSOLE:
395          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
396          if (getMouseHandler("SpaceShip"))
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    for (unsigned  int i = 0; i < joySticksSize_; i++)
417      joySticks_[i]->capture();
418
419
420    // call all the handlers for the held key events
421    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
422      for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
423        activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_));
424
425    // call all the handlers for the held mouse button events
426    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
427      for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
428        activeMouseHandlers_[iHandler]->mouseButtonHeld(mouse_->getMouseState(), mouseButtonsDown_[iButton]);
429
430    // call all the handlers for the held joy stick button events
431    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
432      for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
433        for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
434          activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(
435              JoyStickState(joySticks_[iJoyStick]->getJoyStickState(), iJoyStick), joyStickButtonsDown_[iJoyStick][iButton]);
436  }
437
438  // ###### Key Events ######
439
440  /**
441    @brief Event handler for the keyPressed Event.
442    @param e Event information
443  */
444  bool InputManager::keyPressed(const OIS::KeyEvent &e)
445  {
446    // check whether the key already is in the list (can happen when focus was lost)
447    unsigned int iKey = 0;
448    while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key)
449      iKey++;
450    if (iKey == keysDown_.size())
451      keysDown_.push_back(Key(e));
452
453    // update modifiers
454    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
455      keyboardModifiers_ |= KeyboardModifier::Alt;   // alt key
456    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
457      keyboardModifiers_ |= KeyboardModifier::Ctrl;  // ctrl key
458    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
459      keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
460
461    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
462      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
463
464    return true;
465  }
466
467  /**
468    @brief Event handler for the keyReleased Event.
469    @param e Event information
470  */
471  bool InputManager::keyReleased(const OIS::KeyEvent &e)
472  {
473    // remove the key from the keysDown_ list
474    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
475    {
476      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
477      {
478        keysDown_.erase(keysDown_.begin() + iKey);
479        break;
480      }
481    }
482
483    // update modifiers
484    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
485      keyboardModifiers_ &= ~KeyboardModifier::Alt;   // alt key
486    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
487      keyboardModifiers_ &= ~KeyboardModifier::Ctrl;  // ctrl key
488    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
489      keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
490
491    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
492      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
493
494    return true;
495  }
496
497
498  // ###### Mouse Events ######
499
500  /**
501    @brief Event handler for the mouseMoved Event.
502    @param e Event information
503  */
504  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
505  {
506    // check for actual moved event
507    if (e.state.X.rel != 0 || e.state.Y.rel != 0)
508    {
509      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
510        activeMouseHandlers_[i]->mouseMoved(e.state);
511    }
512
513    // check for mouse scrolled event
514    if (e.state.Z.rel != 0)
515    {
516      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
517        activeMouseHandlers_[i]->mouseScrolled(e.state);
518    }
519
520    return true;
521  }
522
523  /**
524    @brief Event handler for the mousePressed Event.
525    @param e Event information
526    @param id The ID of the mouse button
527  */
528  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
529  {
530    // check whether the button already is in the list (can happen when focus was lost)
531    unsigned int iButton = 0;
532    while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id)
533      iButton++;
534    if (iButton == mouseButtonsDown_.size())
535      mouseButtonsDown_.push_back((MouseButton::Enum)id);
536
537    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
538      activeMouseHandlers_[i]->mouseButtonPressed(e.state, (MouseButton::Enum)id);
539
540    return true;
541  }
542
543  /**
544    @brief Event handler for the mouseReleased Event.
545    @param e Event information
546    @param id The ID of the mouse button
547  */
548  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
549  {
550    // remove the button from the keysDown_ list
551    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
552    {
553      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
554      {
555        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
556        break;
557      }
558    }
559
560    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
561      activeMouseHandlers_[i]->mouseButtonReleased(e.state, (MouseButton::Enum)id);
562
563    return true;
564  }
565
566
567  // ###### Joy Stick Events ######
568
569  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
570  {
571    // use the device to identify which one called the method
572    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
573    unsigned int iJoyStick = 0;
574    while (joySticks_[iJoyStick] != joyStick)
575      iJoyStick++;
576
577    // check whether the button already is in the list (can happen when focus was lost)
578    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
579    unsigned int iButton = 0;
580    while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
581      iButton++;
582    if (iButton == buttonsDown.size())
583      buttonsDown.push_back(button);
584
585    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
586      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(JoyStickState(arg.state, iJoyStick), button);
587
588    return true;
589  }
590
591  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
592  {
593    // use the device to identify which one called the method
594    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
595    unsigned int iJoyStick = 0;
596    while (joySticks_[iJoyStick] != joyStick)
597      iJoyStick++;
598
599    // remove the button from the joyStickButtonsDown_ list
600    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
601    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
602    {
603      if (buttonsDown[iButton] == button)
604      {
605        buttonsDown.erase(buttonsDown.begin() + iButton);
606        break;
607      }
608    }
609
610    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
611      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(JoyStickState(arg.state, iJoyStick), button);
612
613    return true;
614  }
615
616  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
617  {
618    // use the device to identify which one called the method
619    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
620    unsigned int iJoyStick = 0;
621    while (joySticks_[iJoyStick] != joyStick)
622      iJoyStick++;
623
624    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
625      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(JoyStickState(arg.state, iJoyStick), axis);
626
627    return true;
628  }
629
630  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
631  {
632    // use the device to identify which one called the method
633    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
634    unsigned int iJoyStick = 0;
635    while (joySticks_[iJoyStick] != joyStick)
636      iJoyStick++;
637
638    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
639      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickSliderMoved(JoyStickState(arg.state, iJoyStick), id);
640
641    return true;
642  }
643
644  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
645  {
646    // use the device to identify which one called the method
647    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
648    unsigned int iJoyStick = 0;
649    while (joySticks_[iJoyStick] != joyStick)
650      iJoyStick++;
651
652    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
653      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickPovMoved(JoyStickState(arg.state, iJoyStick), id);
654
655    return true;
656  }
657
658  bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
659  {
660    // use the device to identify which one called the method
661    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
662    unsigned int iJoyStick = 0;
663    while (joySticks_[iJoyStick] != joyStick)
664      iJoyStick++;
665
666    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
667      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
668
669    return true;
670  }
671
672
673  // ################################
674  // ### Static Interface Methods ###
675  // ################################
676  // ################################
677
678  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
679    bool createKeyboard, bool createMouse, bool createJoySticks)
680  {
681    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
682          createKeyboard, createMouse, createJoySticks);
683  }
684
685  bool InputManager::initialiseKeyboard()
686  {
687    return _getSingleton()._initialiseKeyboard();
688  }
689
690  bool InputManager::initialiseMouse()
691  {
692    return _getSingleton()._initialiseMouse();
693  }
694
695  bool InputManager::initialiseJoySticks()
696  {
697    return _getSingleton()._initialiseJoySticks();
698  }
699
700  int InputManager::numberOfKeyboards()
701  {
702    if (_getSingleton().keyboard_ != 0)
703      return 1;
704    else
705      return 0;
706  }
707
708  int InputManager::numberOfMice()
709  {
710    if (_getSingleton().mouse_ != 0)
711      return 1;
712    else
713      return 0;
714  }
715
716  int InputManager::numberOfJoySticks()
717  {
718    return _getSingleton().joySticksSize_;
719  }
720
721  bool InputManager::isKeyDown(KeyCode::Enum key)
722  {
723    if (_getSingleton().keyboard_)
724      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
725    else
726      return false;
727  }
728
729  bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
730  {
731    if (_getSingleton().keyboard_)
732      return isModifierDown(modifier);
733    else
734      return false;
735  }
736
737  const MouseState InputManager::getMouseState()
738  {
739    if (_getSingleton().mouse_)
740      return _getSingleton().mouse_->getMouseState();
741    else
742      return MouseState();
743  }
744
745  const JoyStickState InputManager::getJoyStickState(unsigned int ID)
746  {
747    if (ID < _getSingleton().joySticksSize_)
748      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
749    else
750      return JoyStickState();
751  }
752
753
754  void InputManager::destroy()
755  {
756    _getSingleton()._destroy();
757  }
758
759  void InputManager::destroyKeyboard()
760  {
761    return _getSingleton()._destroyKeyboard();
762  }
763
764  void InputManager::destroyMouse()
765  {
766    return _getSingleton()._destroyMouse();
767  }
768
769  void InputManager::destroyJoySticks()
770  {
771    return _getSingleton()._destroyJoySticks();
772  }
773
774
775  /**
776    @brief Adjusts the mouse window metrics.
777    This method has to be called every time the size of the window changes.
778    @param width The new width of the render window
779    @param height the new height of the render window
780  */
781  void InputManager::setWindowExtents(const int width, const int height)
782  {
783    if (_getSingleton().mouse_)
784    {
785      // Set mouse region (if window resizes, we should alter this to reflect as well)
786      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
787      mouseState.width  = width;
788      mouseState.height = height;
789    }
790  }
791
792  /**
793    @brief Sets the input mode to either GUI, inGame or Buffer
794    @param mode The new input mode
795    @remark Only has an affect if the mode actually changes
796  */
797  void InputManager::setInputState(const InputState state)
798  {
799    _getSingleton().stateRequest_ = state;
800  }
801
802  /**
803    @brief Returns the current input handling method
804    @return The current input mode.
805  */
806  InputManager::InputState InputManager::getInputState()
807  {
808    return _getSingleton().state_;
809  }
810
811
812  // ###### KeyHandler ######
813
814  /**
815    @brief Adds a new key handler.
816    @param handler Pointer to the handler object.
817    @param name Unique name of the handler.
818    @return True if added, false if name already existed.
819  */
820  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
821  {
822    if (!handler)
823      return false;
824    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
825    {
826      _getSingleton().keyHandlers_[name] = handler;
827      return true;
828    }
829    else
830      return false;
831  }
832
833  /**
834    @brief Removes a Key handler from the list.
835    @param name Unique name of the handler.
836    @return True if removal was successful, false if name was not found.
837  */
838  bool InputManager::removeKeyHandler(const std::string &name)
839  {
840    disableKeyHandler(name);
841    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
842    if (it != _getSingleton().keyHandlers_.end())
843    {
844      _getSingleton().keyHandlers_.erase(it);
845      return true;
846    }
847    else
848      return false;
849  }
850
851  /**
852    @brief Returns the pointer to a handler.
853    @param name Unique name of the handler.
854    @return Pointer to the instance, 0 if name was not found.
855  */
856  KeyHandler* InputManager::getKeyHandler(const std::string& name)
857  {
858    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
859    if (it != _getSingleton().keyHandlers_.end())
860    {
861      return (*it).second;
862    }
863    else
864      return 0;
865  }
866
867  /**
868    @brief Enables a specific key handler that has already been added.
869    @param name Unique name of the handler.
870    @return False if name was not found, true otherwise.
871  */
872  bool InputManager::enableKeyHandler(const std::string& name)
873  {
874    // get pointer from the map with all stored handlers
875    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
876    if (mapIt == _getSingleton().keyHandlers_.end())
877      return false;
878    // see whether the handler already is in the list
879    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
880          it != _getSingleton().activeKeyHandlers_.end(); it++)
881    {
882      if ((*it) == (*mapIt).second)
883      {
884        _getSingleton().stateRequest_ = IS_CUSTOM;
885        return true;
886      }
887    }
888    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
889    _getSingleton().stateRequest_ = IS_CUSTOM;
890    return true;
891  }
892
893  /**
894    @brief Disables a specific key handler.
895    @param name Unique name of the handler.
896    @return False if name was not found, true otherwise.
897  */
898  bool InputManager::disableKeyHandler(const std::string &name)
899  {
900    // get pointer from the map with all stored handlers
901    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
902    if (mapIt == _getSingleton().keyHandlers_.end())
903      return false;
904    // look for the handler in the list
905    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
906          it != _getSingleton().activeKeyHandlers_.end(); it++)
907    {
908      if ((*it) == (*mapIt).second)
909      {
910        _getSingleton().activeKeyHandlers_.erase(it);
911        _getSingleton().stateRequest_ = IS_CUSTOM;
912        return true;
913      }
914    }
915    _getSingleton().stateRequest_ = IS_CUSTOM;
916    return true;
917  }
918
919  /**
920    @brief Checks whether a key handler is active
921    @param name Unique name of the handler.
922    @return False if key handler is not active or doesn't exist, true otherwise.
923  */
924  bool InputManager::isKeyHandlerActive(const std::string& name)
925  {
926    // get pointer from the map with all stored handlers
927    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
928    if (mapIt == _getSingleton().keyHandlers_.end())
929      return false;
930    // see whether the handler already is in the list
931    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
932          it != _getSingleton().activeKeyHandlers_.end(); it++)
933    {
934      if ((*it) == (*mapIt).second)
935        return true;
936    }
937    return false;
938  }
939
940
941  // ###### MouseHandler ######
942  /**
943    @brief Adds a new mouse handler.
944    @param handler Pointer to the handler object.
945    @param name Unique name of the handler.
946    @return True if added, false if name already existed.
947  */
948  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
949  {
950    if (!handler)
951      return false;
952    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
953    {
954      _getSingleton().mouseHandlers_[name] = handler;
955      return true;
956    }
957    else
958      return false;
959  }
960
961  /**
962    @brief Removes a Mouse handler from the list.
963    @param name Unique name of the handler.
964    @return True if removal was successful, false if name was not found.
965  */
966  bool InputManager::removeMouseHandler(const std::string &name)
967  {
968    disableMouseHandler(name);
969    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
970    if (it != _getSingleton().mouseHandlers_.end())
971    {
972      _getSingleton().mouseHandlers_.erase(it);
973      return true;
974    }
975    else
976      return false;
977  }
978
979  /**
980    @brief Returns the pointer to a handler.
981    @param name Unique name of the handler.
982    @return Pointer to the instance, 0 if name was not found.
983  */
984  MouseHandler* InputManager::getMouseHandler(const std::string& name)
985  {
986    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
987    if (it != _getSingleton().mouseHandlers_.end())
988    {
989      return (*it).second;
990    }
991    else
992      return 0;
993  }
994
995  /**
996    @brief Enables a specific mouse handler that has already been added.
997    @param name Unique name of the handler.
998    @return False if name was not found, true otherwise.
999  */
1000  bool InputManager::enableMouseHandler(const std::string& name)
1001  {
1002    // get pointer from the map with all stored handlers
1003    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1004    if (mapIt == _getSingleton().mouseHandlers_.end())
1005      return false;
1006    // see whether the handler already is in the list
1007    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1008          it != _getSingleton().activeMouseHandlers_.end(); it++)
1009    {
1010      if ((*it) == (*mapIt).second)
1011      {
1012        _getSingleton().stateRequest_ = IS_CUSTOM;
1013        return true;
1014      }
1015    }
1016    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1017    _getSingleton().stateRequest_ = IS_CUSTOM;
1018    return true;
1019  }
1020
1021  /**
1022    @brief Disables a specific mouse handler.
1023    @param name Unique name of the handler.
1024    @return False if name was not found, true otherwise.
1025  */
1026  bool InputManager::disableMouseHandler(const std::string &name)
1027  {
1028    // get pointer from the map with all stored handlers
1029    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1030    if (mapIt == _getSingleton().mouseHandlers_.end())
1031      return false;
1032    // look for the handler in the list
1033    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1034          it != _getSingleton().activeMouseHandlers_.end(); it++)
1035    {
1036      if ((*it) == (*mapIt).second)
1037      {
1038        _getSingleton().activeMouseHandlers_.erase(it);
1039        _getSingleton().stateRequest_ = IS_CUSTOM;
1040        return true;
1041      }
1042    }
1043    _getSingleton().stateRequest_ = IS_CUSTOM;
1044    return true;
1045  }
1046
1047  /**
1048    @brief Checks whether a mouse handler is active
1049    @param name Unique name of the handler.
1050    @return False if key handler is not active or doesn't exist, true otherwise.
1051  */
1052  bool InputManager::isMouseHandlerActive(const std::string& name)
1053  {
1054    // get pointer from the map with all stored handlers
1055    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1056    if (mapIt == _getSingleton().mouseHandlers_.end())
1057      return false;
1058    // see whether the handler already is in the list
1059    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1060          it != _getSingleton().activeMouseHandlers_.end(); it++)
1061    {
1062      if ((*it) == (*mapIt).second)
1063        return true;
1064    }
1065    return false;
1066  }
1067
1068
1069  // ###### JoyStickHandler ######
1070
1071  /**
1072    @brief Adds a new joy stick handler.
1073    @param handler Pointer to the handler object.
1074    @param name Unique name of the handler.
1075    @return True if added, false if name already existed.
1076  */
1077  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1078  {
1079    if (!handler)
1080      return false;
1081    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1082    {
1083      _getSingleton().joyStickHandlers_[name] = handler;
1084      return true;
1085    }
1086    else
1087      return false;
1088  }
1089
1090  /**
1091    @brief Removes a JoyStick handler from the list.
1092    @param name Unique name of the handler.
1093    @return True if removal was successful, false if name was not found.
1094  */
1095  bool InputManager::removeJoyStickHandler(const std::string &name)
1096  {
1097    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1098          itstick != _getSingleton().joySticks_.end(); itstick++)
1099      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1100
1101    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1102    if (it != _getSingleton().joyStickHandlers_.end())
1103    {
1104      _getSingleton().joyStickHandlers_.erase(it);
1105      return true;
1106    }
1107    else
1108      return false;
1109  }
1110
1111  /**
1112    @brief Returns the pointer to a handler.
1113    @param name Unique name of the handler.
1114    @return Pointer to the instance, 0 if name was not found.
1115  */
1116  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1117  {
1118    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1119    if (it != _getSingleton().joyStickHandlers_.end())
1120    {
1121      return (*it).second;
1122    }
1123    else
1124      return 0;
1125  }
1126
1127  /**
1128    @brief Enables a specific joy stick handler that has already been added.
1129    @param name Unique name of the handler.
1130    @return False if name or id was not found, true otherwise.
1131  */
1132  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1133  {
1134    // get handler pointer from the map with all stored handlers
1135    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1136    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1137      return false;
1138
1139    // check for existence of the ID
1140    if (ID >= _getSingleton().joySticksSize_)
1141      return false;
1142
1143    // see whether the handler already is in the list
1144    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1145          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1146    {
1147      if ((*it) == (*handlerIt).second)
1148      {
1149        _getSingleton().stateRequest_ = IS_CUSTOM;
1150        return true;
1151      }
1152    }
1153    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1154    _getSingleton().stateRequest_ = IS_CUSTOM;
1155    return true;
1156  }
1157
1158  /**
1159    @brief Disables a specific joy stick handler.
1160    @param name Unique name of the handler.
1161    @return False if name or id was not found, true otherwise.
1162  */
1163  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1164  {
1165    // get handler pointer from the map with all stored handlers
1166    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1167    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1168      return false;
1169
1170    // check for existence of the ID
1171    if (ID >= _getSingleton().joySticksSize_)
1172      return false;
1173
1174    // look for the handler in the list
1175    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1176          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1177    {
1178      if ((*it) == (*handlerIt).second)
1179      {
1180        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1181        _getSingleton().stateRequest_ = IS_CUSTOM;
1182        return true;
1183      }
1184    }
1185    return true;
1186  }
1187
1188  /**
1189    @brief Checks whether a joy stick handler is active
1190    @param name Unique name of the handler.
1191    @return False if key handler is not active or doesn't exist, true otherwise.
1192  */
1193  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1194  {
1195    // get handler pointer from the map with all stored handlers
1196    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1197    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1198      return false;
1199
1200    // check for existence of the ID
1201    if (ID >= _getSingleton().joySticksSize_)
1202      return false;
1203
1204    // see whether the handler already is in the list
1205    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1206          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1207    {
1208      if ((*it) == (*handlerIt).second)
1209        return true;
1210    }
1211    return false;
1212  }
1213
1214}
Note: See TracBrowser for help on using the repository browser.