Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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