Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/InputManager.cc @ 1203

Last change on this file since 1203 was 1203, checked in by rgrieder, 16 years ago
  • InputManager fully functional (most parts tested), if there wasn't that selfish SpaceShip who claims all the mouse input…
  • InputHandler still loads hard coded key bindings, but works fine otherwise
  • I've tried to give full multiple joy stick support. Couldn't yet test that however. And more than one Joystick still doesn't make sense as long as we don't have split view ;)
File size: 32.7 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 listeners if necessary.
33 */
34
35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
38#include "InputBuffer.h"
39#include "ConsoleCommand.h"
40#include "util/Convert.h"
41
42namespace orxonox
43{
44  // ###############################
45  // ###    Internal Methods     ###
46  // ###############################
47  // ###############################
48
49  /**
50    @brief Constructor only sets member fields to initial zero values
51           and registers the class in the class hierarchy.
52  */
53  InputManager::InputManager() :
54      inputSystem_(0), keyboard_(0), mouse_(0),
55      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
56  {
57    RegisterObject(InputManager);
58  }
59
60  /**
61    @brief The one instance of the InputManager is stored in this function.
62    @return A reference to the only instance of the InputManager
63  */
64  InputManager& InputManager::_getSingleton()
65  {
66    static InputManager theOnlyInstance;
67    return theOnlyInstance;
68  }
69
70  /**
71    @brief Destructor only called at the end of the program, after main.
72  */
73  InputManager::~InputManager()
74  {
75    _destroy();
76  }
77
78  /**
79    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
80           the joysticks and assigns the key bindings.
81    @param windowHnd The window handle of the render window
82    @param windowWidth The width of the render window
83    @param windowHeight The height of the render window
84  */
85  bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight,
86        bool createKeyboard, bool createMouse, bool createJoySticks)
87  {
88    if (state_ == IS_UNINIT)
89    {
90      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
91
92      OIS::ParamList paramList;
93      std::ostringstream windowHndStr;
94
95      // Fill parameter list
96      windowHndStr << (unsigned int)windowHnd;
97      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
98
99//#if defined OIS_LINUX_PLATFORM
100//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
101//#endif
102
103      try
104      {
105        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
106        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
107      }
108      catch (OIS::Exception ex)
109      {
110        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
111            << "OIS message: \"" << ex.eText << "\"" << std::endl;
112        inputSystem_ = 0;
113        return false;
114      }
115
116      if (createKeyboard)
117        _initialiseKeyboard();
118
119      if (createMouse)
120        _initialiseMouse();
121
122      if (createJoySticks)
123        _initialiseJoySticks();
124
125      // Set mouse/joystick region
126      setWindowExtents(windowWidth, windowHeight);
127
128      state_ = IS_NONE;
129      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
130    }
131    else
132    {
133      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
134    }
135
136    // InputManager holds the input buffer --> create one and add it.
137    addKeyListener(new InputBuffer(), "buffer");
138
139    KeyBinder* binder = new KeyBinder();
140    binder->loadBindings();
141    addKeyListener(binder, "keybinder");
142    addMouseListener(binder, "keybinder");
143
144    // Read all the key bindings and assign them
145    //if (!_loadBindings())
146    //  return false;
147
148    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
149    return true;
150  }
151
152  /**
153    @brief Creates a keyboard and sets the event handler.
154    @return False if keyboard stays uninitialised, true otherwise.
155  */
156  bool InputManager::_initialiseKeyboard()
157  {
158    if (keyboard_ != 0)
159    {
160      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
161      return true;
162    }
163    try
164    {
165      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
166      {
167        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
168        // register our listener in OIS.
169        keyboard_->setEventCallback(this);
170        // note: OIS will not detect keys that have already been down when the keyboard was created.
171        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
172        return true;
173      }
174      else
175      {
176        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
177        return false;
178      }
179    }
180    catch (OIS::Exception ex)
181    {
182      // TODO: Test this output regarding formatting
183      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
184          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
185      keyboard_ = 0;
186      return false;
187    }
188  }
189
190  /**
191    @brief Creates a mouse and sets the event handler.
192    @return False if mouse stays uninitialised, true otherwise.
193  */
194  bool InputManager::_initialiseMouse()
195  {
196    if (mouse_ != 0)
197    {
198      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
199      return true;
200    }
201    try
202    {
203      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
204      {
205        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
206        // register our listener in OIS.
207        mouse_->setEventCallback(this);
208        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
209        return true;
210      }
211      else
212      {
213        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
214        return false;
215      }
216    }
217    catch (OIS::Exception ex)
218    {
219      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
220          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
221      mouse_ = 0;
222      return false;
223    }
224  }
225
226  /**
227    @brief Creates all joy sticks and sets the event handler.
228    @return False joy stick stay uninitialised, true otherwise.
229  */
230  bool InputManager::_initialiseJoySticks()
231  {
232    if (joySticks_.size() > 0)
233    {
234      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
235      return true;
236    }
237    bool success = false;
238    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
239    {
240      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
241      {
242        try
243        {
244          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
245          joySticks_[stig->getID()] = stig;
246          // register our listener in OIS.
247          stig->setEventCallback(this);
248          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
249          success = true;
250        }
251        catch (OIS::Exception ex)
252        {
253          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
254              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
255        }
256      }
257    }
258    else
259    {
260      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
261      return false;
262    }
263    return success;
264  }
265
266  /**
267    @brief Destroys all the created input devices and sets the InputManager to construction state.
268  */
269  void InputManager::_destroy()
270  {
271    CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
272
273    if (state_ != IS_UNINIT)
274    {
275      if (listenersKey_.find("buffer") != listenersKey_.end())
276        delete listenersKey_["buffer"];
277
278      if (listenersKey_.find("keybinder") != listenersKey_.end())
279        delete listenersKey_["keybinder"];
280
281      listenersKeyActive_.clear();
282      listenersMouseActive_.clear();
283      listenersJoySticksActive_.clear();
284      listenersKey_.clear();
285      listenersMouse_.clear();
286      listenersJoySticks_.clear();
287
288      keysDown_.clear();
289      mouseButtonsDown_.clear();
290      joySticksButtonsDown_.clear();
291
292      _destroyKeyboard();
293      _destroyMouse();
294      _destroyJoySticks();
295
296      // inputSystem_ can never be 0, or else the code is mistaken
297      OIS::InputManager::destroyInputSystem(inputSystem_);
298      inputSystem_ = 0;
299
300      state_ = IS_UNINIT;
301    }
302    else
303      CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl;
304
305    CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
306  }
307
308  /**
309    @brief Destroys the keyboard and sets it to 0.
310  */
311  void InputManager::_destroyKeyboard()
312  {
313    if (keyboard_)
314      // inputSystem_ can never be 0, or else the code is mistaken
315      inputSystem_->destroyInputObject(keyboard_);
316    keyboard_ = 0;
317    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
318  }
319
320  /**
321    @brief Destroys the mouse and sets it to 0.
322  */
323  void InputManager::_destroyMouse()
324  {
325    if (mouse_)
326      // inputSystem_ can never be 0, or else the code is mistaken
327      inputSystem_->destroyInputObject(mouse_);
328    mouse_ = 0;
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 (joySticks_.size() > 0)
338    {
339      // note: inputSystem_ can never be 0, or else the code is mistaken
340      for (std::map<int, OIS::JoyStick*>::iterator itstick = joySticks_.begin(); itstick != joySticks_.end(); itstick++)
341      {
342        if ((*itstick).second != 0)
343          inputSystem_->destroyInputObject((*itstick).second);
344      }
345      joySticks_.clear();
346    }
347    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
348  }
349
350
351  // #################################
352  // ### Private Interface Methods ###
353  // #################################
354  // #################################
355
356  /**
357    @brief Updates the InputManager. Tick is called by Orxonox.
358    @param dt Delta time
359  */
360  void InputManager::tick(float dt)
361  {
362    if (state_ == IS_UNINIT)
363      return;
364
365    // reset the game if it has changed
366    if (state_ != stateRequest_)
367    {
368      if (stateRequest_ != IS_CUSTOM)
369      {
370        listenersKeyActive_.clear();
371        listenersMouseActive_.clear();
372        listenersJoySticksActive_.clear();
373
374        switch (stateRequest_)
375        {
376        case IS_NORMAL:
377          // normal play mode
378          if (listenersKey_.find("keybinder") != listenersKey_.end())
379            listenersKeyActive_.push_back(listenersKey_["keybinder"]);
380          if (listenersMouse_.find("keybinder") != listenersMouse_.end())
381            listenersMouseActive_.push_back(listenersMouse_["keybinder"]);
382          if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end())
383          {
384            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
385                  it != joySticks_.end(); it++)
386              listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]);
387          }
388          break;
389
390        case IS_GUI:
391          // FIXME: do stuff
392          break;
393
394        case IS_CONSOLE:
395          if (listenersMouse_.find("keybinder") != listenersMouse_.end())
396            listenersMouseActive_.push_back(listenersMouse_["keybinder"]);
397          if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end())
398          {
399            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
400                  it != joySticks_.end(); it++)
401              listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]);
402          }
403
404          if (listenersKey_.find("buffer") != listenersKey_.end())
405            listenersKeyActive_.push_back(listenersKey_["buffer"]);
406          else
407          {
408            // someone fiddled with the InputBuffer
409            CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl;
410            if (listenersKey_.find("keybinder") != listenersKey_.end())
411              listenersKeyActive_.push_back(listenersKey_["keybinder"]);
412            else
413              // this is bad
414              CCOUT(2) << "Error: Cannot reactivate key binder: not found!" << std::endl;
415          }
416          break;
417
418        default:
419          break;
420        }
421        state_ = stateRequest_;
422      }
423    }
424
425    // Capture all the input. This calls the event handlers in InputManager.
426    if (mouse_)
427      mouse_->capture();
428    if (keyboard_)
429      keyboard_->capture();
430
431
432    // call all the listeners for the held key events
433    for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin();
434          itKey != keysDown_.end(); itKey++)
435    {
436      OIS::KeyEvent keyArg(keyboard_, *itKey, 0);
437      for (std::list<KeyHandler*>::const_iterator itKeyHandler = listenersKeyActive_.begin();
438            itKeyHandler != listenersKeyActive_.end(); itKeyHandler++)
439      {
440        (*itKeyHandler)->keyHeld(keyArg);
441      }
442    }
443
444    // call all the listeners for the held mouse button events
445    for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin();
446          itMouseButton != mouseButtonsDown_.end(); itMouseButton++)
447    {
448      OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState());
449      for (std::list<MouseHandler*>::const_iterator itMouseHandler = listenersMouseActive_.begin();
450            itMouseHandler != listenersMouseActive_.end(); itMouseHandler++)
451      {
452        (*itMouseHandler)->mouseHeld(mouseButtonArg, *itMouseButton);
453      }
454    }
455
456    // call all the listeners for the held joy stick button events
457    for (std::map<int, std::list <int> >::const_iterator itJoyStick = joySticksButtonsDown_.begin();
458          itJoyStick != joySticksButtonsDown_.end(); itJoyStick++)
459    {
460      int id = (*itJoyStick).first;
461      for (std::list<int>::const_iterator itJoyStickButton = (*itJoyStick).second.begin();
462            itJoyStickButton != (*itJoyStick).second.end(); itJoyStickButton++)
463      {
464        OIS::JoyStickEvent joyStickButtonArg(joySticks_[id], joySticks_[id]->getJoyStickState());
465        for (std::list<JoyStickHandler*>::const_iterator itJoyStickHandler = listenersJoySticksActive_[id].begin();
466              itJoyStickHandler != listenersJoySticksActive_[id].end(); itJoyStickHandler++)
467        {
468          (*itJoyStickHandler)->buttonHeld(joyStickButtonArg, *itJoyStickButton);
469        }
470      }
471    }
472  }
473
474
475  /**
476    @brief Event handler for the keyPressed Event.
477    @param e Event information
478  */
479  bool InputManager::keyPressed(const OIS::KeyEvent &e)
480  {
481    // check whether the key is already in the list (can happen when focus was lost)
482    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
483    {
484      if (*it == e.key)
485      {
486        keysDown_.erase(it);
487        break;
488      }
489    }
490    keysDown_.push_back(e.key);
491
492    for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
493      (*it)->keyPressed(e);
494
495    return true;
496  }
497
498  /**
499    @brief Event handler for the keyReleased Event.
500    @param e Event information
501  */
502  bool InputManager::keyReleased(const OIS::KeyEvent &e)
503  {
504    // remove the key from the keysDown_ list
505    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
506    {
507      if (*it == e.key)
508      {
509        keysDown_.erase(it);
510        break;
511      }
512    }
513
514    for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
515      (*it)->keyReleased(e);
516
517    return true;
518  }
519
520  /**
521    @brief Event handler for the mouseMoved Event.
522    @param e Event information
523  */
524  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
525  {
526    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
527      (*it)->mouseMoved(e);
528
529    return true;
530  }
531
532  /**
533    @brief Event handler for the mousePressed Event.
534    @param e Event information
535    @param id The ID of the mouse button
536  */
537  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
538  {
539    // check whether the button is already in the list (can happen when focus was lost)
540    for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++)
541    {
542      if (*it == id)
543      {
544        mouseButtonsDown_.erase(it);
545        break;
546      }
547    }
548    mouseButtonsDown_.push_back(id);
549
550    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
551      (*it)->mousePressed(e, id);
552
553    return true;
554  }
555
556  /**
557    @brief Event handler for the mouseReleased Event.
558    @param e Event information
559    @param id The ID of the mouse button
560  */
561  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
562  {
563    // remove the button from the keysDown_ list
564    for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++)
565    {
566      if (*it == id)
567      {
568        mouseButtonsDown_.erase(it);
569        break;
570      }
571    }
572
573    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
574      (*it)->mouseReleased(e, id);
575
576    return true;
577  }
578
579  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
580  {
581    int devID = arg.device->getID();
582
583    // check whether the button is already in the list (can happen when focus was lost)
584    std::list<int>& buttonsDownList = joySticksButtonsDown_[devID];
585    for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++)
586    {
587      if (*it == button)
588      {
589        buttonsDownList.erase(it);
590        break;
591      }
592    }
593    joySticksButtonsDown_[devID].push_back(button);
594
595    std::list<JoyStickHandler*>::iterator end = listenersJoySticksActive_[devID].end();
596    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
597      (*it)->buttonPressed(arg, button);
598
599    return true;
600  }
601
602  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
603  {
604    int devID = arg.device->getID();
605
606    // remove the button from the joySticksButtonsDown_ list
607    std::list<int>& buttonsDownList = joySticksButtonsDown_[devID];
608    for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++)
609    {
610      if (*it == button)
611      {
612        buttonsDownList.erase(it);
613        break;
614      }
615    }
616
617    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
618    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
619      (*it)->buttonReleased(arg, button);
620
621    return true;
622  }
623
624  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
625  {
626    int devID = arg.device->getID();
627    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
628    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
629      (*it)->axisMoved(arg, axis);
630
631    return true;
632  }
633
634  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
635  {
636    int devID = arg.device->getID();
637    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
638    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
639      (*it)->sliderMoved(arg, id);
640
641    return true;
642  }
643
644  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
645  {
646    int devID = arg.device->getID();
647    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
648    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
649      (*it)->povMoved(arg, id);
650
651    return true;
652  }
653
654
655  // ################################
656  // ### Static Interface Methods ###
657  // ################################
658  // ################################
659
660  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight,
661    bool createKeyboard, bool createMouse, bool createJoySticks)
662  {
663    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
664          createKeyboard, createMouse, createJoySticks);
665  }
666
667  bool InputManager::initialiseKeyboard()
668  {
669    return _getSingleton()._initialiseKeyboard();
670  }
671
672  bool InputManager::initialiseMouse()
673  {
674    return _getSingleton()._initialiseMouse();
675  }
676
677  bool InputManager::initialiseJoySticks()
678  {
679    return _getSingleton()._initialiseJoySticks();
680  }
681
682
683  void InputManager::destroy()
684  {
685    _getSingleton()._destroy();
686  }
687
688  void InputManager::destroyKeyboard()
689  {
690    return _getSingleton()._destroyKeyboard();
691  }
692
693  void InputManager::destroyMouse()
694  {
695    return _getSingleton()._destroyMouse();
696  }
697
698  void InputManager::destroyJoySticks()
699  {
700    return _getSingleton()._destroyJoySticks();
701  }
702
703
704  /**
705    @brief Adjusts the mouse window metrics.
706    This method has to be called every time the size of the window changes.
707    @param width The new width of the render window
708    @param height the new height of the render window
709  */
710  void InputManager::setWindowExtents(int width, int height)
711  {
712    if (_getSingleton().mouse_)
713    {
714      // Set mouse region (if window resizes, we should alter this to reflect as well)
715      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
716      mouseState.width  = width;
717      mouseState.height = height;
718    }
719  }
720
721  /**
722    @brief Sets the input mode to either GUI, inGame or Buffer
723    @param mode The new input mode
724    @remark Only has an affect if the mode actually changes
725  */
726  void InputManager::setInputState(const InputState state)
727  {
728    _getSingleton().stateRequest_ = state;
729  }
730
731  /**
732    @brief Returns the current input handling method
733    @return The current input mode.
734  */
735  InputManager::InputState InputManager::getInputState()
736  {
737    return _getSingleton().state_;
738  }
739
740
741  // ###### KeyHandler ######
742
743  /**
744    @brief Adds a new key listener.
745    @param listener Pointer to the listener object.
746    @param name Unique name of the listener.
747    @return True if added, false if name already existed.
748  */
749  bool InputManager::addKeyListener(KeyHandler* listener, const std::string& name)
750  {
751    if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end())
752    {
753      _getSingleton().listenersKey_[name] = listener;
754      return true;
755    }
756    else
757      return false;
758  }
759
760  /**
761    @brief Removes a Key Listener from the list.
762    @param name Unique name of the listener.
763    @return True if removal was successful, false if name was not found.
764  */
765  bool InputManager::removeKeyListener(const std::string &name)
766  {
767    disableKeyListener(name);
768    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name);
769    if (it != _getSingleton().listenersKey_.end())
770    {
771      _getSingleton().listenersKey_.erase(it);
772      return true;
773    }
774    else
775      return false;
776  }
777
778  /**
779    @brief Returns the pointer to a listener.
780    @param name Unique name of the listener.
781    @return Pointer to the instance, 0 if name was not found.
782  */
783  KeyHandler* InputManager::getKeyListener(const std::string& name)
784  {
785    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name);
786    if (it != _getSingleton().listenersKey_.end())
787    {
788      return (*it).second;
789    }
790    else
791      return 0;
792  }
793
794  /**
795    @brief Enables a specific key listener that has already been added.
796    @param name Unique name of the listener.
797    @return False if name was not found, true otherwise.
798  */
799  bool InputManager::enableKeyListener(const std::string& name)
800  {
801    // get pointer from the map with all stored listeners
802    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name);
803    if (mapIt == _getSingleton().listenersKey_.end())
804      return false;
805    // see whether the listener is already in the list
806    for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin();
807          it != _getSingleton().listenersKeyActive_.end(); it++)
808    {
809      if ((*it) == (*mapIt).second)
810        return true;
811    }
812    _getSingleton().listenersKeyActive_.push_back((*mapIt).second);
813    return true;
814  }
815
816  /**
817    @brief Disables a specific key listener.
818    @param name Unique name of the listener.
819    @return False if name was not found, true otherwise.
820  */
821  bool InputManager::disableKeyListener(const std::string &name)
822  {
823    // get pointer from the map with all stored listeners
824    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name);
825    if (mapIt == _getSingleton().listenersKey_.end())
826      return false;
827    // look for the listener in the list
828    for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin();
829          it != _getSingleton().listenersKeyActive_.end(); it++)
830    {
831      if ((*it) == (*mapIt).second)
832      {
833        _getSingleton().listenersKeyActive_.erase(it);
834        return true;
835      }
836    }
837    return true;
838  }
839
840
841  // ###### MouseHandler ######
842  /**
843    @brief Adds a new mouse listener.
844    @param listener Pointer to the listener object.
845    @param name Unique name of the listener.
846    @return True if added, false if name already existed.
847  */
848  bool InputManager::addMouseListener(MouseHandler* listener, const std::string& name)
849  {
850    if (_getSingleton().listenersMouse_.find(name) == _getSingleton().listenersMouse_.end())
851    {
852      _getSingleton().listenersMouse_[name] = listener;
853      return true;
854    }
855    else
856      return false;
857  }
858
859  /**
860    @brief Removes a Mouse Listener from the list.
861    @param name Unique name of the listener.
862    @return True if removal was successful, false if name was not found.
863  */
864  bool InputManager::removeMouseListener(const std::string &name)
865  {
866    disableMouseListener(name);
867    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name);
868    if (it != _getSingleton().listenersMouse_.end())
869    {
870      _getSingleton().listenersMouse_.erase(it);
871      return true;
872    }
873    else
874      return false;
875  }
876
877  /**
878    @brief Returns the pointer to a listener.
879    @param name Unique name of the listener.
880    @return Pointer to the instance, 0 if name was not found.
881  */
882  MouseHandler* InputManager::getMouseListener(const std::string& name)
883  {
884    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name);
885    if (it != _getSingleton().listenersMouse_.end())
886    {
887      return (*it).second;
888    }
889    else
890      return 0;
891  }
892
893  /**
894    @brief Enables a specific mouse listener that has already been added.
895    @param name Unique name of the listener.
896    @return False if name was not found, true otherwise.
897  */
898  bool InputManager::enableMouseListener(const std::string& name)
899  {
900    // get pointer from the map with all stored listeners
901    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name);
902    if (mapIt == _getSingleton().listenersMouse_.end())
903      return false;
904    // see whether the listener is already in the list
905    for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin();
906          it != _getSingleton().listenersMouseActive_.end(); it++)
907    {
908      if ((*it) == (*mapIt).second)
909        return true;
910    }
911    _getSingleton().listenersMouseActive_.push_back((*mapIt).second);
912    return true;
913  }
914
915  /**
916    @brief Disables a specific mouse listener.
917    @param name Unique name of the listener.
918    @return False if name was not found, true otherwise.
919  */
920  bool InputManager::disableMouseListener(const std::string &name)
921  {
922    // get pointer from the map with all stored listeners
923    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name);
924    if (mapIt == _getSingleton().listenersMouse_.end())
925      return false;
926    // look for the listener in the list
927    for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin();
928          it != _getSingleton().listenersMouseActive_.end(); it++)
929    {
930      if ((*it) == (*mapIt).second)
931      {
932        _getSingleton().listenersMouseActive_.erase(it);
933        return true;
934      }
935    }
936    return true;
937  }
938
939
940  // ###### JoyStickHandler ######
941
942  /**
943    @brief Adds a new joy stick listener.
944    @param listener Pointer to the listener object.
945    @param name Unique name of the listener.
946    @return True if added, false if name already existed.
947  */
948  bool InputManager::addJoyStickListener(JoyStickHandler* listener, const std::string& name)
949  {
950    if (_getSingleton().listenersJoySticks_.find(name) == _getSingleton().listenersJoySticks_.end())
951    {
952      _getSingleton().listenersJoySticks_[name] = listener;
953      return true;
954    }
955    else
956      return false;
957  }
958
959  /**
960    @brief Removes a JoyStick Listener from the list.
961    @param name Unique name of the listener.
962    @return True if removal was successful, false if name was not found.
963  */
964  bool InputManager::removeJoyStickListener(const std::string &name)
965  {
966    for (std::map<int, OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); itstick != _getSingleton().joySticks_.end(); itstick++)
967      disableJoyStickListener(name, (*itstick).first);
968
969    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name);
970    if (it != _getSingleton().listenersJoySticks_.end())
971    {
972      _getSingleton().listenersJoySticks_.erase(it);
973      return true;
974    }
975    else
976      return false;
977  }
978
979  /**
980    @brief Returns the pointer to a listener.
981    @param name Unique name of the listener.
982    @return Pointer to the instance, 0 if name was not found.
983  */
984  JoyStickHandler* InputManager::getJoyStickListener(const std::string& name)
985  {
986    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name);
987    if (it != _getSingleton().listenersJoySticks_.end())
988    {
989      return (*it).second;
990    }
991    else
992      return 0;
993  }
994
995  /**
996    @brief Enables a specific joy stick listener that has already been added.
997    @param name Unique name of the listener.
998    @return False if name or id was not found, true otherwise.
999  */
1000  bool InputManager::enableJoyStickListener(const std::string& name, const int id)
1001  {
1002    // get pointer from the map with all stored listeners
1003    std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name);
1004    if (listenerIt == _getSingleton().listenersJoySticks_.end())
1005      return false;
1006
1007    // check for existance of the ID
1008    std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id);
1009    if (joyStickIt == _getSingleton().joySticks_.end())
1010      return false;
1011
1012    // see whether the listener is already in the list
1013    for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin();
1014          it != _getSingleton().listenersJoySticksActive_[id].end(); it++)
1015    {
1016      if ((*it) == (*listenerIt).second)
1017        return true;
1018    }
1019    _getSingleton().listenersJoySticksActive_[id].push_back((*listenerIt).second);
1020    return true;
1021  }
1022
1023  /**
1024    @brief Disables a specific joy stick listener.
1025    @param name Unique name of the listener.
1026    @return False if name or id was not found, true otherwise.
1027  */
1028  bool InputManager::disableJoyStickListener(const std::string &name, int id)
1029  {
1030    // get pointer from the map with all stored listeners
1031    std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name);
1032    if (listenerIt == _getSingleton().listenersJoySticks_.end())
1033      return false;
1034
1035    // check for existance of the ID
1036    std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id);
1037    if (joyStickIt == _getSingleton().joySticks_.end())
1038      return false;
1039
1040    // look for the listener in the list
1041    for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin();
1042          it != _getSingleton().listenersJoySticksActive_[id].end(); it++)
1043    {
1044      if ((*it) == (*listenerIt).second)
1045      {
1046        _getSingleton().listenersJoySticksActive_[id].erase(it);
1047        return true;
1048      }
1049    }
1050    return true;
1051  }
1052
1053}
Note: See TracBrowser for help on using the repository browser.