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, 17 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
RevLine 
[918]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[918]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:
[934]25 *      ...
[918]26 *
27 */
[973]28
[918]29/**
[1193]30  @file
31  @brief Implementation of the InputManager that captures all the input from OIS
[1204]32         and redirects it to handlers if necessary.
[918]33 */
34
[1062]35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
[1066]38#include "InputBuffer.h"
[1204]39#include "InputHandler.h"
[918]40
41namespace orxonox
42{
[1193]43  // ###############################
44  // ###    Internal Methods     ###
45  // ###############################
[1203]46  // ###############################
[1193]47
[919]48  /**
[1193]49    @brief Constructor only sets member fields to initial zero values
50           and registers the class in the class hierarchy.
[919]51  */
[973]52  InputManager::InputManager() :
[1034]53      inputSystem_(0), keyboard_(0), mouse_(0),
[1182]54      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
[918]55  {
[1089]56    RegisterObject(InputManager);
[918]57  }
58
[919]59  /**
[1182]60    @brief The one instance of the InputManager is stored in this function.
61    @return A reference to the only instance of the InputManager
[929]62  */
[1182]63  InputManager& InputManager::_getSingleton()
[929]64  {
[1182]65    static InputManager theOnlyInstance;
66    return theOnlyInstance;
[929]67  }
68
69  /**
[1193]70    @brief Destructor only called at the end of the program, after main.
[919]71  */
[1182]72  InputManager::~InputManager()
[919]73  {
[1203]74    _destroy();
[919]75  }
76
77  /**
[1193]78    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
79           the joysticks and assigns the key bindings.
[919]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  */
[1204]84  bool InputManager::_initialise(const size_t windowHnd, const int windowWidth, const int windowHeight,
85        const bool createKeyboard, const bool createMouse, const bool createJoySticks)
[919]86  {
[1193]87    if (state_ == IS_UNINIT)
[918]88    {
[1193]89      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
90
[918]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
[1182]98//#if defined OIS_LINUX_PLATFORM
99//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
100//#endif
[928]101
[934]102      try
[918]103      {
[934]104        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
[1193]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      }
[918]114
[1193]115      if (createKeyboard)
116        _initialiseKeyboard();
[918]117
[1193]118      if (createMouse)
119        _initialiseMouse();
[1182]120
[1193]121      if (createJoySticks)
122        _initialiseJoySticks();
[934]123
[1193]124      // Set mouse/joystick region
125      setWindowExtents(windowWidth, windowHeight);
[1182]126
[1203]127      state_ = IS_NONE;
[1193]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.
[1204]136    addKeyHandler(new InputBuffer(), "buffer");
[1193]137
[1203]138    KeyBinder* binder = new KeyBinder();
139    binder->loadBindings();
[1204]140    addKeyHandler(binder, "keybinder");
141    addMouseHandler(binder, "keybinder");
[1203]142
[1193]143    // Read all the key bindings and assign them
[1203]144    //if (!_loadBindings())
145    //  return false;
[1193]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.
[1203]153    @return False if keyboard stays uninitialised, true otherwise.
[1193]154  */
[1203]155  bool InputManager::_initialiseKeyboard()
[1193]156  {
[1203]157    if (keyboard_ != 0)
158    {
159      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
160      return true;
161    }
[1193]162    try
163    {
[1195]164      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
[1193]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;
[1203]171        return true;
[918]172      }
[1193]173      else
[1203]174      {
[1193]175        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
[1203]176        return false;
177      }
[1193]178    }
179    catch (OIS::Exception ex)
180    {
[1203]181      // TODO: Test this output regarding formatting
[1193]182      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
183          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
184      keyboard_ = 0;
[1203]185      return false;
[1193]186    }
187  }
188
189  /**
190    @brief Creates a mouse and sets the event handler.
[1203]191    @return False if mouse stays uninitialised, true otherwise.
[1193]192  */
[1203]193  bool InputManager::_initialiseMouse()
[1193]194  {
[1203]195    if (mouse_ != 0)
196    {
197      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
198      return true;
199    }
[1193]200    try
201    {
[1195]202      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
[934]203      {
[1193]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;
[1203]208        return true;
[934]209      }
[1193]210      else
[1203]211      {
[1193]212        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
[1203]213        return false;
214      }
[918]215    }
[1193]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;
[1203]221      return false;
[1193]222    }
223  }
[922]224
[1193]225  /**
226    @brief Creates all joy sticks and sets the event handler.
[1203]227    @return False joy stick stay uninitialised, true otherwise.
[1193]228  */
[1203]229  bool InputManager::_initialiseJoySticks()
[1193]230  {
[1203]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;
[1193]237    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
238    {
[1203]239      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
[1193]240      {
241        try
242        {
[1203]243          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
244          joySticks_[stig->getID()] = stig;
[1193]245          // register our listener in OIS.
[1203]246          stig->setEventCallback(this);
247          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
248          success = true;
[1193]249        }
250        catch (OIS::Exception ex)
251        {
[1203]252          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
[1193]253              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
254        }
255      }
256    }
257    else
[1203]258    {
[1193]259      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
[1203]260      return false;
261    }
262    return success;
[1193]263  }
[1022]264
[1193]265  /**
[1203]266    @brief Destroys all the created input devices and sets the InputManager to construction state.
[1193]267  */
[1203]268  void InputManager::_destroy()
[1193]269  {
[1203]270    CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
271
272    if (state_ != IS_UNINIT)
[1193]273    {
[1204]274      if (keyHandlers_.find("buffer") != keyHandlers_.end())
275        delete keyHandlers_["buffer"];
[1203]276
[1204]277      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
278        delete keyHandlers_["keybinder"];
[1203]279
[1204]280      activeKeyHandlers_.clear();
281      activeMouseHandlers_.clear();
282      activeJoyStickHandlers_.clear();
283      keyHandlers_.clear();
284      mouseHandlers_.clear();
285      joyStickHandlers_.clear();
[1203]286
287      keysDown_.clear();
288      mouseButtonsDown_.clear();
[1204]289      joyStickButtonsDown_.clear();
[1203]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;
[1193]300    }
[1203]301    else
302      CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl;
303
304    CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
[1193]305  }
[922]306
[1193]307  /**
[1203]308    @brief Destroys the keyboard and sets it to 0.
[1193]309  */
[1203]310  void InputManager::_destroyKeyboard()
[1193]311  {
[1203]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;
[918]317  }
318
[1193]319  /**
[1203]320    @brief Destroys the mouse and sets it to 0.
[1193]321  */
[1203]322  void InputManager::_destroyMouse()
[1182]323  {
[1203]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;
[1182]329  }
330
[919]331  /**
[1203]332    @brief Destroys all the joy sticks and resizes the lists to 0.
[928]333  */
[1203]334  void InputManager::_destroyJoySticks()
[928]335  {
[1203]336    if (joySticks_.size() > 0)
[1193]337    {
[1203]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++)
[1193]340      {
[1203]341        if ((*itstick).second != 0)
342          inputSystem_->destroyInputObject((*itstick).second);
[1193]343      }
[1203]344      joySticks_.clear();
[1182]345    }
[1203]346    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
[928]347  }
348
[1193]349
[1203]350  // #################################
351  // ### Private Interface Methods ###
352  // #################################
353  // #################################
[1193]354
[928]355  /**
[1195]356    @brief Updates the InputManager. Tick is called by Orxonox.
[919]357    @param dt Delta time
358  */
[973]359  void InputManager::tick(float dt)
[918]360  {
[1182]361    if (state_ == IS_UNINIT)
362      return;
363
[1022]364    // reset the game if it has changed
[1182]365    if (state_ != stateRequest_)
[1022]366    {
[1203]367      if (stateRequest_ != IS_CUSTOM)
[1022]368      {
[1204]369        activeKeyHandlers_.clear();
370        activeMouseHandlers_.clear();
371        activeJoyStickHandlers_.clear();
[1193]372
[1203]373        switch (stateRequest_)
[1193]374        {
[1203]375        case IS_NORMAL:
376          // normal play mode
[1204]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())
[1203]382          {
383            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
384                  it != joySticks_.end(); it++)
[1204]385              activeJoyStickHandlers_[(*it).first].push_back(joyStickHandlers_["keybinder"]);
[1203]386          }
387          break;
[1193]388
[1203]389        case IS_GUI:
390          // FIXME: do stuff
391          break;
[1193]392
[1203]393        case IS_CONSOLE:
[1204]394          if (mouseHandlers_.find("keybinder") != mouseHandlers_.end())
395            activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
396          if (joyStickHandlers_.find("keybinder") != joyStickHandlers_.end())
[1203]397          {
398            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
399                  it != joySticks_.end(); it++)
[1204]400              activeJoyStickHandlers_[(*it).first].push_back(joyStickHandlers_["keybinder"]);
[1203]401          }
[1195]402
[1204]403          if (keyHandlers_.find("buffer") != keyHandlers_.end())
404            activeKeyHandlers_.push_back(keyHandlers_["buffer"]);
[1203]405          else
406          {
407            // someone fiddled with the InputBuffer
408            CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl;
[1204]409            if (keyHandlers_.find("keybinder") != keyHandlers_.end())
410              activeKeyHandlers_.push_back(keyHandlers_["keybinder"]);
[1203]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_;
[1022]421      }
422    }
423
[1193]424    // Capture all the input. This calls the event handlers in InputManager.
[918]425    if (mouse_)
426      mouse_->capture();
427    if (keyboard_)
428      keyboard_->capture();
[1203]429
430
[1204]431    // call all the handlers for the held key events
[1203]432    for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin();
433          itKey != keysDown_.end(); itKey++)
434    {
435      OIS::KeyEvent keyArg(keyboard_, *itKey, 0);
[1204]436      for (std::list<KeyHandler*>::const_iterator itKeyHandler = activeKeyHandlers_.begin();
437            itKeyHandler != activeKeyHandlers_.end(); itKeyHandler++)
[1203]438      {
439        (*itKeyHandler)->keyHeld(keyArg);
440      }
441    }
442
[1204]443    // call all the handlers for the held mouse button events
[1203]444    for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin();
445          itMouseButton != mouseButtonsDown_.end(); itMouseButton++)
446    {
447      OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState());
[1204]448      for (std::list<MouseHandler*>::const_iterator itMouseHandler = activeMouseHandlers_.begin();
449            itMouseHandler != activeMouseHandlers_.end(); itMouseHandler++)
[1203]450      {
451        (*itMouseHandler)->mouseHeld(mouseButtonArg, *itMouseButton);
452      }
453    }
454
[1204]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++)
[1203]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());
[1204]464        for (std::list<JoyStickHandler*>::const_iterator itJoyStickHandler = activeJoyStickHandlers_[id].begin();
465              itJoyStickHandler != activeJoyStickHandlers_[id].end(); itJoyStickHandler++)
[1203]466        {
467          (*itJoyStickHandler)->buttonHeld(joyStickButtonArg, *itJoyStickButton);
468        }
469      }
470    }
[918]471  }
472
[1182]473
[919]474  /**
[1182]475    @brief Event handler for the keyPressed Event.
476    @param e Event information
477  */
478  bool InputManager::keyPressed(const OIS::KeyEvent &e)
479  {
[1203]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++)
[1182]482    {
[1203]483      if (*it == e.key)
[1182]484      {
[1203]485        keysDown_.erase(it);
486        break;
[1182]487      }
488    }
[1203]489    keysDown_.push_back(e.key);
490
[1204]491    for (std::list<KeyHandler*>::const_iterator it = activeKeyHandlers_.begin(); it != activeKeyHandlers_.end(); it++)
[1203]492      (*it)->keyPressed(e);
493
[1182]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
[1204]513    for (std::list<KeyHandler*>::const_iterator it = activeKeyHandlers_.begin(); it != activeKeyHandlers_.end(); it++)
[1203]514      (*it)->keyReleased(e);
515
[1182]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  {
[1204]525    for (std::list<MouseHandler*>::const_iterator it = activeMouseHandlers_.begin(); it != activeMouseHandlers_.end(); it++)
[1203]526      (*it)->mouseMoved(e);
527
[1182]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  {
[1203]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
[1204]549    for (std::list<MouseHandler*>::const_iterator it = activeMouseHandlers_.begin(); it != activeMouseHandlers_.end(); it++)
[1203]550      (*it)->mousePressed(e, id);
551
[1182]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  {
[1203]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
[1204]572    for (std::list<MouseHandler*>::const_iterator it = activeMouseHandlers_.begin(); it != activeMouseHandlers_.end(); it++)
[1203]573      (*it)->mouseReleased(e, id);
574
[1182]575    return true;
576  }
577
578  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
579  {
[1203]580    int devID = arg.device->getID();
581
582    // check whether the button is already in the list (can happen when focus was lost)
[1204]583    std::list<int>& buttonsDownList = joyStickButtonsDown_[devID];
[1203]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    }
[1204]592    joyStickButtonsDown_[devID].push_back(button);
[1203]593
[1204]594    std::list<JoyStickHandler*>::iterator end = activeJoyStickHandlers_[devID].end();
595    for (std::list<JoyStickHandler*>::const_iterator it = activeJoyStickHandlers_[devID].begin(); it != end; it++)
[1203]596      (*it)->buttonPressed(arg, button);
597
598    return true;
[1182]599  }
600
601  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
602  {
[1203]603    int devID = arg.device->getID();
604
[1204]605    // remove the button from the joyStickButtonsDown_ list
606    std::list<int>& buttonsDownList = joyStickButtonsDown_[devID];
[1203]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
[1204]616    std::list<JoyStickHandler*>::const_iterator end = activeJoyStickHandlers_[devID].end();
617    for (std::list<JoyStickHandler*>::const_iterator it = activeJoyStickHandlers_[devID].begin(); it != end; it++)
[1203]618      (*it)->buttonReleased(arg, button);
619
620    return true;
[1182]621  }
622
623  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
624  {
[1203]625    int devID = arg.device->getID();
[1204]626    std::list<JoyStickHandler*>::const_iterator end = activeJoyStickHandlers_[devID].end();
627    for (std::list<JoyStickHandler*>::const_iterator it = activeJoyStickHandlers_[devID].begin(); it != end; it++)
[1203]628      (*it)->axisMoved(arg, axis);
629
[1182]630    return true;
631  }
632
633  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
634  {
[1203]635    int devID = arg.device->getID();
[1204]636    std::list<JoyStickHandler*>::const_iterator end = activeJoyStickHandlers_[devID].end();
637    for (std::list<JoyStickHandler*>::const_iterator it = activeJoyStickHandlers_[devID].begin(); it != end; it++)
[1203]638      (*it)->sliderMoved(arg, id);
639
[1182]640    return true;
641  }
642
643  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
644  {
[1203]645    int devID = arg.device->getID();
[1204]646    std::list<JoyStickHandler*>::const_iterator end = activeJoyStickHandlers_[devID].end();
647    for (std::list<JoyStickHandler*>::const_iterator it = activeJoyStickHandlers_[devID].begin(); it != end; it++)
[1203]648      (*it)->povMoved(arg, id);
649
[1182]650    return true;
651  }
652
653
[1193]654  // ################################
655  // ### Static Interface Methods ###
656  // ################################
[1203]657  // ################################
[1182]658
[1204]659  bool InputManager::initialise(const size_t windowHnd, const int windowWidth, const int windowHeight,
660    const bool createKeyboard, const bool createMouse, const bool createJoySticks)
[1203]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
[1204]681  bool InputManager::isKeyboardInitialised()
682  {
683    return (_getSingleton().keyboard_ != 0);
684  }
[1203]685
[1204]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
[1203]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
[1182]718  /**
[919]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  */
[1204]724  void InputManager::setWindowExtents(const int width, const int height)
[918]725  {
[1193]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    }
[918]733  }
734
[919]735  /**
[1022]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
[922]739  */
[1182]740  void InputManager::setInputState(const InputState state)
[922]741  {
[1182]742    _getSingleton().stateRequest_ = state;
[922]743  }
744
745  /**
[1022]746    @brief Returns the current input handling method
747    @return The current input mode.
[919]748  */
[1182]749  InputManager::InputState InputManager::getInputState()
[918]750  {
[1182]751    return _getSingleton().state_;
[918]752  }
753
[1203]754
755  // ###### KeyHandler ######
756
757  /**
[1204]758    @brief Adds a new key handler.
759    @param handler Pointer to the handler object.
760    @param name Unique name of the handler.
[1203]761    @return True if added, false if name already existed.
762  */
[1204]763  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
[1066]764  {
[1204]765    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
[1203]766    {
[1204]767      _getSingleton().keyHandlers_[name] = handler;
[1203]768      return true;
769    }
770    else
771      return false;
[1066]772  }
773
[1203]774  /**
[1204]775    @brief Removes a Key handler from the list.
776    @param name Unique name of the handler.
[1203]777    @return True if removal was successful, false if name was not found.
778  */
[1204]779  bool InputManager::removeKeyHandler(const std::string &name)
[1182]780  {
[1204]781    disableKeyHandler(name);
782    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
783    if (it != _getSingleton().keyHandlers_.end())
[1203]784    {
[1204]785      _getSingleton().keyHandlers_.erase(it);
[1203]786      return true;
787    }
788    else
789      return false;
[1182]790  }
[1066]791
[1203]792  /**
[1204]793    @brief Returns the pointer to a handler.
794    @param name Unique name of the handler.
[1203]795    @return Pointer to the instance, 0 if name was not found.
796  */
[1204]797  KeyHandler* InputManager::getKeyHandler(const std::string& name)
[1195]798  {
[1204]799    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
800    if (it != _getSingleton().keyHandlers_.end())
[1203]801    {
802      return (*it).second;
803    }
804    else
805      return 0;
[1195]806  }
807
[1203]808  /**
[1204]809    @brief Enables a specific key handler that has already been added.
810    @param name Unique name of the handler.
[1203]811    @return False if name was not found, true otherwise.
812  */
[1204]813  bool InputManager::enableKeyHandler(const std::string& name)
[1195]814  {
[1204]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())
[1203]818      return false;
[1204]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++)
[1203]822    {
823      if ((*it) == (*mapIt).second)
824        return true;
825    }
[1204]826    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
[1203]827    return true;
[1195]828  }
829
[1203]830  /**
[1204]831    @brief Disables a specific key handler.
832    @param name Unique name of the handler.
[1203]833    @return False if name was not found, true otherwise.
834  */
[1204]835  bool InputManager::disableKeyHandler(const std::string &name)
[1195]836  {
[1204]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())
[1203]840      return false;
[1204]841    // look for the handler in the list
842    for (std::list<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
843          it != _getSingleton().activeKeyHandlers_.end(); it++)
[1203]844    {
845      if ((*it) == (*mapIt).second)
846      {
[1204]847        _getSingleton().activeKeyHandlers_.erase(it);
[1203]848        return true;
849      }
850    }
851    return true;
852  }
[1195]853
[1204]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  }
[1203]874
[1204]875
[1203]876  // ###### MouseHandler ######
877  /**
[1204]878    @brief Adds a new mouse handler.
879    @param handler Pointer to the handler object.
880    @param name Unique name of the handler.
[1203]881    @return True if added, false if name already existed.
882  */
[1204]883  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
[1182]884  {
[1204]885    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
[1182]886    {
[1204]887      _getSingleton().mouseHandlers_[name] = handler;
[1182]888      return true;
889    }
890    else
891      return false;
892  }
893
[1203]894  /**
[1204]895    @brief Removes a Mouse handler from the list.
896    @param name Unique name of the handler.
[1203]897    @return True if removal was successful, false if name was not found.
898  */
[1204]899  bool InputManager::removeMouseHandler(const std::string &name)
[1182]900  {
[1204]901    disableMouseHandler(name);
902    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
903    if (it != _getSingleton().mouseHandlers_.end())
[1182]904    {
[1204]905      _getSingleton().mouseHandlers_.erase(it);
[1182]906      return true;
907    }
908    else
909      return false;
910  }
911
[1203]912  /**
[1204]913    @brief Returns the pointer to a handler.
914    @param name Unique name of the handler.
[1203]915    @return Pointer to the instance, 0 if name was not found.
916  */
[1204]917  MouseHandler* InputManager::getMouseHandler(const std::string& name)
[1182]918  {
[1204]919    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
920    if (it != _getSingleton().mouseHandlers_.end())
[1182]921    {
922      return (*it).second;
923    }
924    else
925      return 0;
926  }
927
[1203]928  /**
[1204]929    @brief Enables a specific mouse handler that has already been added.
930    @param name Unique name of the handler.
[1203]931    @return False if name was not found, true otherwise.
932  */
[1204]933  bool InputManager::enableMouseHandler(const std::string& name)
[1203]934  {
[1204]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())
[1203]938      return false;
[1204]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++)
[1203]942    {
943      if ((*it) == (*mapIt).second)
944        return true;
945    }
[1204]946    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
[1203]947    return true;
948  }
949
950  /**
[1204]951    @brief Disables a specific mouse handler.
952    @param name Unique name of the handler.
[1203]953    @return False if name was not found, true otherwise.
954  */
[1204]955  bool InputManager::disableMouseHandler(const std::string &name)
[1203]956  {
[1204]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())
[1203]960      return false;
[1204]961    // look for the handler in the list
962    for (std::list<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
963          it != _getSingleton().activeMouseHandlers_.end(); it++)
[1203]964    {
965      if ((*it) == (*mapIt).second)
966      {
[1204]967        _getSingleton().activeMouseHandlers_.erase(it);
[1203]968        return true;
969      }
970    }
971    return true;
972  }
973
[1204]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  }
[1203]994
[1204]995
[1203]996  // ###### JoyStickHandler ######
997
998  /**
[1204]999    @brief Adds a new joy stick handler.
1000    @param handler Pointer to the handler object.
1001    @param name Unique name of the handler.
[1203]1002    @return True if added, false if name already existed.
1003  */
[1204]1004  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
[1203]1005  {
[1204]1006    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
[1203]1007    {
[1204]1008      _getSingleton().joyStickHandlers_[name] = handler;
[1203]1009      return true;
1010    }
1011    else
1012      return false;
1013  }
1014
1015  /**
[1204]1016    @brief Removes a JoyStick handler from the list.
1017    @param name Unique name of the handler.
[1203]1018    @return True if removal was successful, false if name was not found.
1019  */
[1204]1020  bool InputManager::removeJoyStickHandler(const std::string &name)
[1203]1021  {
1022    for (std::map<int, OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); itstick != _getSingleton().joySticks_.end(); itstick++)
[1204]1023      disableJoyStickHandler(name, (*itstick).first);
[1203]1024
[1204]1025    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1026    if (it != _getSingleton().joyStickHandlers_.end())
[1203]1027    {
[1204]1028      _getSingleton().joyStickHandlers_.erase(it);
[1203]1029      return true;
1030    }
1031    else
1032      return false;
1033  }
1034
1035  /**
[1204]1036    @brief Returns the pointer to a handler.
1037    @param name Unique name of the handler.
[1203]1038    @return Pointer to the instance, 0 if name was not found.
1039  */
[1204]1040  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
[1203]1041  {
[1204]1042    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1043    if (it != _getSingleton().joyStickHandlers_.end())
[1203]1044    {
1045      return (*it).second;
1046    }
1047    else
1048      return 0;
1049  }
1050
1051  /**
[1204]1052    @brief Enables a specific joy stick handler that has already been added.
1053    @param name Unique name of the handler.
[1203]1054    @return False if name or id was not found, true otherwise.
1055  */
[1204]1056  bool InputManager::enableJoyStickHandler(const std::string& name, const int id)
[1203]1057  {
[1204]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())
[1203]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
[1204]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++)
[1203]1071    {
[1204]1072      if ((*it) == (*handlerIt).second)
[1203]1073        return true;
1074    }
[1204]1075    _getSingleton().activeJoyStickHandlers_[id].push_back((*handlerIt).second);
[1203]1076    return true;
1077  }
1078
1079  /**
[1204]1080    @brief Disables a specific joy stick handler.
1081    @param name Unique name of the handler.
[1203]1082    @return False if name or id was not found, true otherwise.
1083  */
[1204]1084  bool InputManager::disableJoyStickHandler(const std::string &name, int id)
[1203]1085  {
[1204]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())
[1203]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
[1204]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++)
[1203]1099    {
[1204]1100      if ((*it) == (*handlerIt).second)
[1203]1101      {
[1204]1102        _getSingleton().activeJoyStickHandlers_[id].erase(it);
[1203]1103        return true;
1104      }
1105    }
1106    return true;
1107  }
1108
[1204]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
[918]1136}
Note: See TracBrowser for help on using the repository browser.