Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/core/InputManager.cc @ 1268

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