Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputManager.cc @ 1283

Last change on this file since 1283 was 1219, checked in by rgrieder, 17 years ago

merged input branch back to trunk. Not yet tested on tardis

File size: 34.9 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    this->joySticks_.reserve(5);
59    //this->activeJoyStickHandlers_.reserve(10);
60    this->activeKeyHandlers_.reserve(10);
61    this->activeMouseHandlers_.reserve(10);
62  }
63
64  /**
65    @brief The one instance of the InputManager is stored in this function.
66    @return A reference to the only instance of the InputManager
67  */
68  InputManager& InputManager::_getSingleton()
69  {
70    static InputManager theOnlyInstance;
71    return theOnlyInstance;
72  }
73
74  /**
75    @brief Destructor only called at the end of the program, after main.
76  */
77  InputManager::~InputManager()
78  {
79    _destroy();
80  }
81
82  /**
83    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
84           the joysticks and assigns the key bindings.
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  */
89  bool InputManager::_initialise(const size_t windowHnd, const int windowWidth, const int windowHeight,
90        const bool createKeyboard, const bool createMouse, const bool createJoySticks)
91  {
92    if (state_ == IS_UNINIT)
93    {
94      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
95
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
103//#if defined OIS_LINUX_PLATFORM
104//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
105//#endif
106
107      try
108      {
109        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
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      }
119
120      if (createKeyboard)
121        _initialiseKeyboard();
122
123      if (createMouse)
124        _initialiseMouse();
125
126      if (createJoySticks)
127        _initialiseJoySticks();
128
129      // Set mouse/joystick region
130      setWindowExtents(windowWidth, windowHeight);
131
132      state_ = IS_NONE;
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.
141    addKeyHandler(new InputBuffer(), "buffer");
142
143    KeyBinder* binder = new KeyBinder();
144    binder->loadBindings();
145    addKeyHandler(binder, "keybinder");
146    addMouseHandler(binder, "keybinder");
147
148    // Read all the key bindings and assign them
149    //if (!_loadBindings())
150    //  return false;
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.
158    @return False if keyboard stays uninitialised, true otherwise.
159  */
160  bool InputManager::_initialiseKeyboard()
161  {
162    if (keyboard_ != 0)
163    {
164      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
165      return true;
166    }
167    try
168    {
169      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
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;
176        return true;
177      }
178      else
179      {
180        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
181        return false;
182      }
183    }
184    catch (OIS::Exception ex)
185    {
186      // TODO: Test this output regarding formatting
187      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
188          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
189      keyboard_ = 0;
190      return false;
191    }
192  }
193
194  /**
195    @brief Creates a mouse and sets the event handler.
196    @return False if mouse stays uninitialised, true otherwise.
197  */
198  bool InputManager::_initialiseMouse()
199  {
200    if (mouse_ != 0)
201    {
202      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
203      return true;
204    }
205    try
206    {
207      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
208      {
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;
213        return true;
214      }
215      else
216      {
217        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
218        return false;
219      }
220    }
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;
226      return false;
227    }
228  }
229
230  /**
231    @brief Creates all joy sticks and sets the event handler.
232    @return False joy stick stay uninitialised, true otherwise.
233  */
234  bool InputManager::_initialiseJoySticks()
235  {
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;
242    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
243    {
244      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
245      {
246        try
247        {
248          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
249          joySticks_.push_back(stig);
250          // register our listener in OIS.
251          stig->setEventCallback(this);
252          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
253          success = true;
254        }
255        catch (OIS::Exception ex)
256        {
257          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
258              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
259        }
260      }
261    }
262    else
263    {
264      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
265      return false;
266    }
267    return success;
268  }
269
270  /**
271    @brief Destroys all the created input devices and sets the InputManager to construction state.
272  */
273  void InputManager::_destroy()
274  {
275    CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
276
277    if (state_ != IS_UNINIT)
278    {
279      if (keyHandlers_.find("buffer") != keyHandlers_.end())
280        delete keyHandlers_["buffer"];
281
282      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
283        delete keyHandlers_["keybinder"];
284
285      keyHandlers_.clear();
286      mouseHandlers_.clear();
287      joyStickHandlers_.clear();
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;
298    }
299    else
300      CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl;
301
302    CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
303  }
304
305  /**
306    @brief Destroys the keyboard and sets it to 0.
307  */
308  void InputManager::_destroyKeyboard()
309  {
310    if (keyboard_)
311      // inputSystem_ can never be 0, or else the code is mistaken
312      inputSystem_->destroyInputObject(keyboard_);
313    keyboard_ = 0;
314    activeKeyHandlers_.clear();
315    keysDown_.clear();
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    activeMouseHandlers_.clear();
329    mouseButtonsDown_.clear();
330    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
331  }
332
333  /**
334    @brief Destroys all the joy sticks and resizes the lists to 0.
335  */
336  void InputManager::_destroyJoySticks()
337  {
338    if (joySticks_.size() > 0)
339    {
340      // note: inputSystem_ can never be 0, or else the code is mistaken
341      for (unsigned int i = 0; i < joySticks_.size(); i++)
342        if (joySticks_[i] != 0)
343          inputSystem_->destroyInputObject(joySticks_[i]);
344
345      joySticks_.clear();
346      activeJoyStickHandlers_.clear();
347      joyStickButtonsDown_.clear();
348    }
349    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
350  }
351
352
353  // #################################
354  // ### Private Interface Methods ###
355  // #################################
356  // #################################
357
358  /**
359    @brief Updates the InputManager. Tick is called by Orxonox.
360    @param dt Delta time
361  */
362  void InputManager::tick(float dt)
363  {
364    if (state_ == IS_UNINIT)
365      return;
366
367    // reset the game if it has changed
368    if (state_ != stateRequest_)
369    {
370      if (stateRequest_ != IS_CUSTOM)
371      {
372        activeKeyHandlers_.clear();
373        activeMouseHandlers_.clear();
374        activeJoyStickHandlers_.clear();
375
376        switch (stateRequest_)
377        {
378        case IS_NORMAL:
379          // normal play mode
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"]);
387          break;
388
389        case IS_GUI:
390          // FIXME: do stuff
391          break;
392
393        case IS_CONSOLE:
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"]);
398
399          activeKeyHandlers_.push_back(keyHandlers_["buffer"]);
400          break;
401
402        default:
403          break;
404        }
405        state_ = stateRequest_;
406      }
407    }
408
409    // Capture all the input. This calls the event handlers in InputManager.
410    if (mouse_)
411      mouse_->capture();
412    if (keyboard_)
413      keyboard_->capture();
414
415
416    // call all the handlers for the held key events
417    for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin();
418          itKey != keysDown_.end(); itKey++)
419    {
420      OIS::KeyEvent keyArg(keyboard_, *itKey, 0);
421      for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
422        activeKeyHandlers_[i]->keyHeld(keyArg);
423    }
424
425    // call all the handlers for the held mouse button events
426    for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin();
427          itMouseButton != mouseButtonsDown_.end(); itMouseButton++)
428    {
429      OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState());
430      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
431        activeMouseHandlers_[i]->mouseHeld(mouseButtonArg, *itMouseButton);
432    }
433
434    // call all the handlers for the held joy stick button events
435    for (std::map< OIS::JoyStick*, std::list <int> >::const_iterator itJoyStick = joyStickButtonsDown_.begin();
436          itJoyStick != joyStickButtonsDown_.end(); itJoyStick++)
437    {
438      OIS::JoyStick* joyStick = (*itJoyStick).first;
439      for (std::list<int>::const_iterator itJoyStickButton = (*itJoyStick).second.begin();
440            itJoyStickButton != (*itJoyStick).second.end(); itJoyStickButton++)
441      {
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);
445      }
446    }
447  }
448
449
450  // ###### Key Events ######
451
452  /**
453    @brief Event handler for the keyPressed Event.
454    @param e Event information
455  */
456  bool InputManager::keyPressed(const OIS::KeyEvent &e)
457  {
458    // check whether the key already is in the list (can happen when focus was lost)
459    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
460    {
461      if (*it == e.key)
462      {
463        keysDown_.erase(it);
464        break;
465      }
466    }
467    keysDown_.push_back(e.key);
468
469    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
470      activeKeyHandlers_[i]->keyPressed(e);
471
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
491    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
492      activeKeyHandlers_[i]->keyReleased(e);
493
494    return true;
495  }
496
497
498  // ###### Mouse Events ######
499
500  /**
501    @brief Event handler for the mouseMoved Event.
502    @param e Event information
503  */
504  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
505  {
506    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
507      activeMouseHandlers_[i]->mouseMoved(e);
508
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  {
519    // check whether the button already is in the list (can happen when focus was lost)
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
530    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
531      activeMouseHandlers_[i]->mousePressed(e, id);
532
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  {
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
553    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
554      activeMouseHandlers_[i]->mouseReleased(e, id);
555
556    return true;
557  }
558
559
560  // ###### Joy Stick Events ######
561
562  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
563  {
564    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
565
566    // check whether the button already is in the list (can happen when focus was lost)
567    std::list<int>& buttonsDownList = joyStickButtonsDown_[joyStick];
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    }
576    joyStickButtonsDown_[joyStick].push_back(button);
577
578    for (unsigned int i = 0; i < activeJoyStickHandlers_[joyStick].size(); i++)
579      activeJoyStickHandlers_[joyStick][i]->buttonPressed(i, arg, button);
580
581    return true;
582  }
583
584  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
585  {
586    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
587
588    // remove the button from the joyStickButtonsDown_ list
589    std::list<int>& buttonsDownList = joyStickButtonsDown_[joyStick];
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
599    for (unsigned int i = 0; i < activeJoyStickHandlers_[joyStick].size(); i++)
600      activeJoyStickHandlers_[joyStick][i]->buttonReleased(i, arg, button);
601
602    return true;
603  }
604
605  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
606  {
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);
610
611    return true;
612  }
613
614  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
615  {
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);
619
620    return true;
621  }
622
623  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
624  {
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);
628
629    return true;
630  }
631
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  }
640
641
642  // ################################
643  // ### Static Interface Methods ###
644  // ################################
645  // ################################
646
647  bool InputManager::initialise(const size_t windowHnd, const int windowWidth, const int windowHeight,
648    const bool createKeyboard, const bool createMouse, const bool createJoySticks)
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
669  int InputManager::numberOfKeyboards()
670  {
671    if (_getSingleton().keyboard_ != 0)
672      return 1;
673    else
674      return 0;
675  }
676
677  int InputManager::numberOfMice()
678  {
679    if (_getSingleton().mouse_ != 0)
680      return 1;
681    else
682      return 0;
683  }
684
685  int InputManager::numberOfJoySticks()
686  {
687    return _getSingleton().joySticks_.size();
688  }
689
690
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
712  /**
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  */
718  void InputManager::setWindowExtents(const int width, const int height)
719  {
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    }
727  }
728
729  /**
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
733  */
734  void InputManager::setInputState(const InputState state)
735  {
736    _getSingleton().stateRequest_ = state;
737  }
738
739  /**
740    @brief Returns the current input handling method
741    @return The current input mode.
742  */
743  InputManager::InputState InputManager::getInputState()
744  {
745    return _getSingleton().state_;
746  }
747
748
749  // ###### KeyHandler ######
750
751  /**
752    @brief Adds a new key handler.
753    @param handler Pointer to the handler object.
754    @param name Unique name of the handler.
755    @return True if added, false if name already existed.
756  */
757  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
758  {
759    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
760    {
761      _getSingleton().keyHandlers_[name] = handler;
762      return true;
763    }
764    else
765      return false;
766  }
767
768  /**
769    @brief Removes a Key handler from the list.
770    @param name Unique name of the handler.
771    @return True if removal was successful, false if name was not found.
772  */
773  bool InputManager::removeKeyHandler(const std::string &name)
774  {
775    disableKeyHandler(name);
776    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
777    if (it != _getSingleton().keyHandlers_.end())
778    {
779      _getSingleton().keyHandlers_.erase(it);
780      return true;
781    }
782    else
783      return false;
784  }
785
786  /**
787    @brief Returns the pointer to a handler.
788    @param name Unique name of the handler.
789    @return Pointer to the instance, 0 if name was not found.
790  */
791  KeyHandler* InputManager::getKeyHandler(const std::string& name)
792  {
793    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
794    if (it != _getSingleton().keyHandlers_.end())
795    {
796      return (*it).second;
797    }
798    else
799      return 0;
800  }
801
802  /**
803    @brief Enables a specific key handler that has already been added.
804    @param name Unique name of the handler.
805    @return False if name was not found, true otherwise.
806  */
807  bool InputManager::enableKeyHandler(const std::string& name)
808  {
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())
812      return false;
813    // see whether the handler already is in the list
814    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
815          it != _getSingleton().activeKeyHandlers_.end(); it++)
816    {
817      if ((*it) == (*mapIt).second)
818      {
819        _getSingleton().stateRequest_ = IS_CUSTOM;
820        return true;
821      }
822    }
823    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
824    _getSingleton().stateRequest_ = IS_CUSTOM;
825    return true;
826  }
827
828  /**
829    @brief Disables a specific key handler.
830    @param name Unique name of the handler.
831    @return False if name was not found, true otherwise.
832  */
833  bool InputManager::disableKeyHandler(const std::string &name)
834  {
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())
838      return false;
839    // look for the handler in the list
840    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
841          it != _getSingleton().activeKeyHandlers_.end(); it++)
842    {
843      if ((*it) == (*mapIt).second)
844      {
845        _getSingleton().activeKeyHandlers_.erase(it);
846        _getSingleton().stateRequest_ = IS_CUSTOM;
847        return true;
848      }
849    }
850    _getSingleton().stateRequest_ = IS_CUSTOM;
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 already is in the list
866    for (std::vector<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 already is in the list
940    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
941          it != _getSingleton().activeMouseHandlers_.end(); it++)
942    {
943      if ((*it) == (*mapIt).second)
944      {
945        _getSingleton().stateRequest_ = IS_CUSTOM;
946        return true;
947      }
948    }
949    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
950    _getSingleton().stateRequest_ = IS_CUSTOM;
951    return true;
952  }
953
954  /**
955    @brief Disables a specific mouse handler.
956    @param name Unique name of the handler.
957    @return False if name was not found, true otherwise.
958  */
959  bool InputManager::disableMouseHandler(const std::string &name)
960  {
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())
964      return false;
965    // look for the handler in the list
966    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
967          it != _getSingleton().activeMouseHandlers_.end(); it++)
968    {
969      if ((*it) == (*mapIt).second)
970      {
971        _getSingleton().activeMouseHandlers_.erase(it);
972        _getSingleton().stateRequest_ = IS_CUSTOM;
973        return true;
974      }
975    }
976    _getSingleton().stateRequest_ = IS_CUSTOM;
977    return true;
978  }
979
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;
991    // see whether the handler already is in the list
992    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
993          it != _getSingleton().activeMouseHandlers_.end(); it++)
994    {
995      if ((*it) == (*mapIt).second)
996        return true;
997    }
998    return false;
999  }
1000
1001
1002  // ###### JoyStickHandler ######
1003
1004  /**
1005    @brief Adds a new joy stick handler.
1006    @param handler Pointer to the handler object.
1007    @param name Unique name of the handler.
1008    @return True if added, false if name already existed.
1009  */
1010  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1011  {
1012    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1013    {
1014      _getSingleton().joyStickHandlers_[name] = handler;
1015      return true;
1016    }
1017    else
1018      return false;
1019  }
1020
1021  /**
1022    @brief Removes a JoyStick handler from the list.
1023    @param name Unique name of the handler.
1024    @return True if removal was successful, false if name was not found.
1025  */
1026  bool InputManager::removeJoyStickHandler(const std::string &name)
1027  {
1028    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1029          itstick != _getSingleton().joySticks_.end(); itstick++)
1030      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1031
1032    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1033    if (it != _getSingleton().joyStickHandlers_.end())
1034    {
1035      _getSingleton().joyStickHandlers_.erase(it);
1036      return true;
1037    }
1038    else
1039      return false;
1040  }
1041
1042  /**
1043    @brief Returns the pointer to a handler.
1044    @param name Unique name of the handler.
1045    @return Pointer to the instance, 0 if name was not found.
1046  */
1047  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1048  {
1049    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1050    if (it != _getSingleton().joyStickHandlers_.end())
1051    {
1052      return (*it).second;
1053    }
1054    else
1055      return 0;
1056  }
1057
1058  /**
1059    @brief Enables a specific joy stick handler that has already been added.
1060    @param name Unique name of the handler.
1061    @return False if name or id was not found, true otherwise.
1062  */
1063  bool InputManager::enableJoyStickHandler(const std::string& name, const int ID)
1064  {
1065    // get handler pointer from the map with all stored handlers
1066    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1067    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1068      return false;
1069
1070    // check for existence of the ID
1071    if ((unsigned int)ID >= _getSingleton().joySticks_.size())
1072      return false;
1073    OIS::JoyStick* joyStick = _getSingleton().joySticks_[ID];
1074
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++)
1078    {
1079      if ((*it) == (*handlerIt).second)
1080      {
1081        _getSingleton().stateRequest_ = IS_CUSTOM;
1082        return true;
1083      }
1084    }
1085    _getSingleton().activeJoyStickHandlers_[joyStick].push_back((*handlerIt).second);
1086    _getSingleton().stateRequest_ = IS_CUSTOM;
1087    return true;
1088  }
1089
1090  /**
1091    @brief Disables a specific joy stick handler.
1092    @param name Unique name of the handler.
1093    @return False if name or id was not found, true otherwise.
1094  */
1095  bool InputManager::disableJoyStickHandler(const std::string &name, int ID)
1096  {
1097    // get handler pointer from the map with all stored handlers
1098    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1099    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1100      return false;
1101
1102    // check for existence of the ID
1103    if ((unsigned int)ID >= _getSingleton().joySticks_.size())
1104      return false;
1105    OIS::JoyStick* joyStick = _getSingleton().joySticks_[ID];
1106
1107    // look for the handler in the list
1108    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[joyStick].begin();
1109          it != _getSingleton().activeJoyStickHandlers_[joyStick].end(); it++)
1110    {
1111      if ((*it) == (*handlerIt).second)
1112      {
1113        _getSingleton().activeJoyStickHandlers_[joyStick].erase(it);
1114        _getSingleton().stateRequest_ = IS_CUSTOM;
1115        return true;
1116      }
1117    }
1118    return true;
1119  }
1120
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  */
1126  bool InputManager::isJoyStickHandlerActive(const std::string& name, const int ID)
1127  {
1128    // get handler pointer from the map with all stored handlers
1129    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1130    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1131      return false;
1132
1133    // check for existence of the ID
1134    if ((unsigned int)ID >= _getSingleton().joySticks_.size())
1135      return false;
1136    OIS::JoyStick* joyStick = _getSingleton().joySticks_[ID];
1137
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++)
1141    {
1142      if ((*it) == (*handlerIt).second)
1143        return true;
1144    }
1145    return false;
1146  }
1147
1148}
Note: See TracBrowser for help on using the repository browser.