Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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