Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/input/InputManager.cc @ 1567

Last change on this file since 1567 was 1555, checked in by landauf, 16 years ago
  • ParticleInterface uses Orxonox::getTimeFactor to make particle effects independend of gamespeed (but there's still a drawback with thrusters)
  • reduced OIS includes in input-related classes, changed OIS search and include to avoid conflicts with other versions of OIS (for example in Ogre). not yet tested on tardis.
  • Property svn:eol-style set to native
File size: 49.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.
33 */
34
35#include "InputManager.h"
36
37#include <limits.h>
38
39#include "core/CoreIncludes.h"
40#include "core/ConfigValueIncludes.h"
41#include "core/Debug.h"
42#include "core/CommandExecutor.h"
43#include "core/ConsoleCommand.h"
44#include "core/Shell.h"               // hack!
45
46#include "InputBuffer.h"
47#include "KeyBinder.h"
48#include "KeyDetector.h"
49#include "CalibratorCallback.h"
50
51#include "src/ois/OISException.h"
52#include "src/ois/OISInputManager.h"
53
54namespace orxonox
55{
56  SetConsoleCommandShortcut(InputManager, keyBind);
57  SetConsoleCommandShortcut(InputManager, storeKeyStroke);
58  SetConsoleCommandShortcut(InputManager, calibrate);
59
60  // ###############################
61  // ###    Internal Methods     ###
62  // ###############################
63  // ###############################
64
65  /**
66    @brief Constructor only sets member fields to initial zero values
67           and registers the class in the class hierarchy.
68  */
69  InputManager::InputManager() :
70      inputSystem_(0), keyboard_(0), mouse_(0),
71      joySticksSize_(0),
72      keyBinder_(0), keyDetector_(0), buffer_(0), calibratorCallback_(0),
73      state_(IS_UNINIT), stateRequest_(IS_UNINIT), savedState_(IS_UNINIT),
74      keyboardModifiers_(0)
75  {
76    RegisterRootObject(InputManager);
77  }
78
79  /**
80    @brief The one instance of the InputManager is stored in this function.
81    @return A reference to the only instance of the InputManager
82  */
83  InputManager& InputManager::_getSingleton()
84  {
85    static InputManager theOnlyInstance;
86    return theOnlyInstance;
87  }
88
89  /**
90    @brief Destructor only called at the end of the program, after main.
91  */
92  InputManager::~InputManager()
93  {
94    _destroy();
95  }
96
97  /**
98    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
99           the joysticks and assigns the key bindings.
100    @param windowHnd The window handle of the render window
101    @param windowWidth The width of the render window
102    @param windowHeight The height of the render window
103  */
104  bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight,
105        bool createKeyboard, bool createMouse, bool createJoySticks)
106  {
107    if (state_ == IS_UNINIT)
108    {
109      CCOUT(3) << "Initialising Input System..." << std::endl;
110      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
111
112      OIS::ParamList paramList;
113      std::ostringstream windowHndStr;
114
115      // Fill parameter list
116      windowHndStr << (unsigned int)windowHnd;
117      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
118      //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
119      //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
120//#if defined OIS_LINUX_PLATFORM
121//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
122//#endif
123
124      try
125      {
126        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
127        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
128      }
129      catch (OIS::Exception ex)
130      {
131        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
132            << "OIS message: \"" << ex.eText << "\"" << std::endl;
133        inputSystem_ = 0;
134        return false;
135      }
136
137      if (createKeyboard)
138        _initialiseKeyboard();
139
140      if (createMouse)
141        _initialiseMouse();
142
143      if (createJoySticks)
144        _initialiseJoySticks();
145
146      // Set mouse/joystick region
147      if (mouse_)
148      {
149        setWindowExtents(windowWidth, windowHeight);
150      }
151
152      state_ = IS_NONE;
153      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
154
155      // InputManager holds the input buffer --> create one and add it.
156      buffer_ = new InputBuffer();
157      addKeyHandler(buffer_, "buffer");
158      Shell::getInstance().setInputBuffer(buffer_);
159
160      keyBinder_ = new KeyBinder();
161      keyBinder_->loadBindings();
162      addKeyHandler(keyBinder_, "keybinder");
163      addMouseHandler(keyBinder_, "keybinder");
164      addJoyStickHandler(keyBinder_, "keybinder");
165
166      keyDetector_ = new KeyDetector();
167      keyDetector_->loadBindings();
168      addKeyHandler(keyDetector_, "keydetector");
169      addMouseHandler(keyDetector_, "keydetector");
170      addJoyStickHandler(keyDetector_, "keydetector");
171
172      calibratorCallback_ = new CalibratorCallback();
173      addKeyHandler(calibratorCallback_, "calibratorcallback");
174
175      setConfigValues();
176
177      CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
178    }
179    else
180    {
181      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
182    }
183    return true;
184  }
185
186  /**
187    @brief Creates a keyboard and sets the event handler.
188    @return False if keyboard stays uninitialised, true otherwise.
189  */
190  bool InputManager::_initialiseKeyboard()
191  {
192    if (keyboard_ != 0)
193    {
194      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
195      return true;
196    }
197    try
198    {
199      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
200      {
201        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
202        // register our listener in OIS.
203        keyboard_->setEventCallback(this);
204        // note: OIS will not detect keys that have already been down when the keyboard was created.
205        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
206        return true;
207      }
208      else
209      {
210        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
211        return false;
212      }
213    }
214    catch (OIS::Exception ex)
215    {
216      // TODO: Test this output regarding formatting
217      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
218          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
219      keyboard_ = 0;
220      return false;
221    }
222  }
223
224  /**
225    @brief Creates a mouse and sets the event handler.
226    @return False if mouse stays uninitialised, true otherwise.
227  */
228  bool InputManager::_initialiseMouse()
229  {
230    if (mouse_ != 0)
231    {
232      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
233      return true;
234    }
235    try
236    {
237      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
238      {
239        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
240        // register our listener in OIS.
241        mouse_->setEventCallback(this);
242        CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl;
243        return true;
244      }
245      else
246      {
247        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
248        return false;
249      }
250    }
251    catch (OIS::Exception ex)
252    {
253      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
254          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
255      mouse_ = 0;
256      return false;
257    }
258  }
259
260  /**
261    @brief Creates all joy sticks and sets the event handler.
262    @return False joy stick stay uninitialised, true otherwise.
263  */
264  bool InputManager::_initialiseJoySticks()
265  {
266    if (joySticksSize_ > 0)
267    {
268      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
269      return true;
270    }
271    bool success = false;
272    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
273    {
274      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
275      {
276        try
277        {
278          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
279          joySticks_.push_back(stig);
280          // register our listener in OIS.
281          stig->setEventCallback(this);
282          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
283          success = true;
284        }
285        catch (OIS::Exception ex)
286        {
287          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
288              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
289        }
290      }
291    }
292    else
293    {
294      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
295      return false;
296    }
297    joySticksSize_ = joySticks_.size();
298    activeJoyStickHandlers_.resize(joySticksSize_);
299    joyStickButtonsDown_.resize(joySticksSize_);
300    povStates_.resize(joySticksSize_);
301    sliderStates_.resize(joySticksSize_);
302    joySticksCalibration_.resize(joySticksSize_);
303    for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++)
304    {
305      // reset the calibration with default values
306      for (unsigned int i = 0; i < 24; i++)
307      {
308        joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f;
309        joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f;
310        joySticksCalibration_[iJoyStick].zeroStates[i] = 0;
311      }
312    }
313    return success;
314  }
315
316  /**
317    @brief Sets the configurable values. Use keybindings.ini as file..
318  */
319  void InputManager::setConfigValues()
320  {
321    if (joySticksSize_)
322    {
323      std::vector<MultiTypeMath> coeffPos;
324      std::vector<MultiTypeMath> coeffNeg;
325      std::vector<MultiTypeMath> zero;
326      coeffPos.resize(24);
327      coeffNeg.resize(24);
328      zero.resize(24);
329      for (unsigned int i = 0; i < 24; i++)
330      {
331        coeffPos[i] =  1.0f/32767.0f;
332        coeffNeg[i] =  1.0f/32768.0f;
333        zero[i]     =  0;
334      }
335
336      ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos");
337      if (!cont)
338      {
339          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffPos", coeffPos);
340          getIdentifier()->addConfigValueContainer("CoeffPos", cont);
341      }
342      cont->getValue(&coeffPos);
343
344      cont = getIdentifier()->getConfigValueContainer("CoeffNeg");
345      if (!cont)
346      {
347          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "CoeffNeg", coeffNeg);
348          getIdentifier()->addConfigValueContainer("CoeffNeg", cont);
349      }
350      cont->getValue(&coeffNeg);
351
352      cont = getIdentifier()->getConfigValueContainer("Zero");
353      if (!cont)
354      {
355          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), "Zero", zero);
356          getIdentifier()->addConfigValueContainer("Zero", cont);
357      }
358      cont->getValue(&zero);
359
360      // copy values to our own variables
361      for (unsigned int i = 0; i < 24; i++)
362      {
363        joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i];
364        joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i];
365        joySticksCalibration_[0].zeroStates[i]    = zero[i];
366      }
367    }
368  }
369
370  /**
371    @brief Destroys all the created input devices and sets the InputManager to construction state.
372  */
373  void InputManager::_destroy()
374  {
375    if (state_ != IS_UNINIT)
376    {
377      CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
378
379      if (buffer_)
380        delete buffer_;
381
382      if (keyBinder_)
383        delete keyBinder_;
384
385      if (keyDetector_)
386        delete keyDetector_;
387
388      if (calibratorCallback_)
389        delete calibratorCallback_;
390
391      keyHandlers_.clear();
392      mouseHandlers_.clear();
393      joyStickHandlers_.clear();
394
395      _destroyKeyboard();
396      _destroyMouse();
397      _destroyJoySticks();
398
399      activeHandlers_.clear();
400
401      // inputSystem_ can never be 0, or else the code is mistaken
402      OIS::InputManager::destroyInputSystem(inputSystem_);
403      inputSystem_ = 0;
404
405      state_ = IS_UNINIT;
406      CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
407    }
408  }
409
410  /**
411    @brief Destroys the keyboard and sets it to 0.
412  */
413  void InputManager::_destroyKeyboard()
414  {
415    if (keyboard_)
416      // inputSystem_ can never be 0, or else the code is mistaken
417      inputSystem_->destroyInputObject(keyboard_);
418    keyboard_ = 0;
419    activeKeyHandlers_.clear();
420    keysDown_.clear();
421    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
422  }
423
424  /**
425    @brief Destroys the mouse and sets it to 0.
426  */
427  void InputManager::_destroyMouse()
428  {
429    if (mouse_)
430      // inputSystem_ can never be 0, or else the code is mistaken
431      inputSystem_->destroyInputObject(mouse_);
432    mouse_ = 0;
433    activeMouseHandlers_.clear();
434    mouseButtonsDown_.clear();
435    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
436  }
437
438  /**
439    @brief Destroys all the joy sticks and resizes the lists to 0.
440  */
441  void InputManager::_destroyJoySticks()
442  {
443    if (joySticksSize_ > 0)
444    {
445      // note: inputSystem_ can never be 0, or else the code is mistaken
446      for (unsigned int i = 0; i < joySticksSize_; i++)
447        if (joySticks_[i] != 0)
448          inputSystem_->destroyInputObject(joySticks_[i]);
449
450      joySticks_.clear();
451      joySticksSize_ = 0;
452      activeJoyStickHandlers_.clear();
453      joyStickButtonsDown_.clear();
454      povStates_.clear();
455      sliderStates_.clear();
456      joySticksCalibration_.clear();
457    }
458    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
459  }
460
461  void InputManager::_saveState()
462  {
463    savedHandlers_.activeHandlers_ = activeHandlers_;
464    savedHandlers_.activeJoyStickHandlers_ = activeJoyStickHandlers_;
465    savedHandlers_.activeKeyHandlers_ = activeKeyHandlers_;
466    savedHandlers_.activeMouseHandlers_ = activeMouseHandlers_;
467  }
468
469  void InputManager::_restoreState()
470  {
471    activeHandlers_ = savedHandlers_.activeHandlers_;
472    activeJoyStickHandlers_ = savedHandlers_.activeJoyStickHandlers_;
473    activeKeyHandlers_ = savedHandlers_.activeKeyHandlers_;
474    activeMouseHandlers_ = savedHandlers_.activeMouseHandlers_;
475  }
476
477  void InputManager::_updateTickables()
478  {
479    // we can use a map to have a list of unique pointers (an object can implement all 3 handlers)
480    std::map<InputTickable*, HandlerState> tempSet;
481    for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
482      tempSet[activeKeyHandlers_[iHandler]].joyStick = true;
483    for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
484      tempSet[activeMouseHandlers_[iHandler]].mouse = true;
485    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
486      for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
487        tempSet[activeJoyStickHandlers_[iJoyStick][iHandler]].joyStick = true;
488
489    // copy the content of the map back to the actual vector
490    activeHandlers_.clear();
491    for (std::map<InputTickable*, HandlerState>::const_iterator itHandler = tempSet.begin();
492        itHandler != tempSet.end(); itHandler++)
493      activeHandlers_.push_back(std::pair<InputTickable*, HandlerState>((*itHandler).first, (*itHandler).second));
494  }
495
496
497  // #################################
498  // ### Private Interface Methods ###
499  // #################################
500  // #################################
501
502  /**
503    @brief Updates the InputManager. Tick is called by Orxonox.
504    @param dt Delta time
505  */
506  void InputManager::_tick(float dt)
507  {
508    if (state_ == IS_UNINIT)
509      return;
510
511    if (state_ != stateRequest_)
512    {
513      InputState sr = stateRequest_;
514      switch (sr)
515      {
516      case IS_NORMAL:
517        activeKeyHandlers_.clear();
518        activeMouseHandlers_.clear();
519        for (unsigned int i = 0; i < joySticksSize_; i++)
520          activeJoyStickHandlers_[i].clear();
521
522        // normal play mode
523        // note: we assume that the handlers exist since otherwise, something's wrong anyway.
524        enableKeyHandler("keybinder");
525        enableMouseHandler("keybinder");
526        enableJoyStickHandler("keybinder", 0);
527        stateRequest_ = IS_NORMAL;
528        state_ = IS_NORMAL;
529        break;
530
531      case IS_GUI:
532        state_ = IS_GUI;
533        break;
534
535      case IS_CONSOLE:
536        activeKeyHandlers_.clear();
537        activeMouseHandlers_.clear();
538        for (unsigned int i = 0; i < joySticksSize_; i++)
539          activeJoyStickHandlers_[i].clear();
540
541        enableMouseHandler("keybinder");
542        enableJoyStickHandler("keybinder", 0);
543        enableKeyHandler("buffer");
544        stateRequest_ = IS_CONSOLE;
545        state_ = IS_CONSOLE;
546        break;
547
548      case IS_DETECT:
549        savedState_ = state_;
550        _saveState();
551
552        activeKeyHandlers_.clear();
553        activeMouseHandlers_.clear();
554        for (unsigned int i = 0; i < joySticksSize_; i++)
555          activeJoyStickHandlers_[i].clear();
556
557        enableKeyHandler("keydetector");
558        enableMouseHandler("keydetector");
559        enableJoyStickHandler("keydetector", 0);
560
561        stateRequest_ = IS_DETECT;
562        state_ = IS_DETECT;
563        break;
564
565      case IS_NODETECT:
566        _restoreState();
567        keysDown_.clear();
568        mouseButtonsDown_.clear();
569        for (unsigned int i = 0; i < joySticksSize_; i++)
570          joyStickButtonsDown_[i].clear();
571        state_ = IS_NODETECT;
572        stateRequest_ = savedState_;
573        break;
574
575      case IS_CALIBRATE:
576        if (joySticksSize_)
577        {
578          savedState_ = _getSingleton().state_;
579          for (unsigned int i = 0; i < 24; i++)
580          {
581            marginalsMax_[i] = INT_MIN;
582            marginalsMin_[i] = INT_MAX;
583          }
584          COUT(0) << "Move all joy stick axes in all directions a few times. "
585            << "Then put all axes in zero state and hit enter." << std::endl;
586
587          savedState_ = state_;
588          _saveState();
589
590          activeKeyHandlers_.clear();
591          activeMouseHandlers_.clear();
592          for (unsigned int i = 0; i < joySticksSize_; i++)
593            activeJoyStickHandlers_[i].clear();
594
595          enableKeyHandler("calibratorcallback");
596          stateRequest_ = IS_CALIBRATE;
597          state_ = IS_CALIBRATE;
598        }
599        else
600        {
601          COUT(3) << "Connot calibrate, no joy stick found!" << std::endl;
602          stateRequest_ = state_;
603        }
604        break;
605
606      case IS_NOCALIBRATE:
607        _completeCalibration();
608        _restoreState();
609        keyBinder_->resetJoyStickAxes();
610        state_ = IS_NOCALIBRATE;
611        stateRequest_ = savedState_;
612        break;
613
614      case IS_NONE:
615        activeKeyHandlers_.clear();
616        activeMouseHandlers_.clear();
617        for (unsigned int i = 0; i < joySticksSize_; i++)
618          activeJoyStickHandlers_[i].clear();
619        state_ = IS_NONE;
620
621      default:
622        break;
623      }
624    }
625
626    // Capture all the input. This calls the event handlers in InputManager.
627    if (mouse_)
628      mouse_->capture();
629    if (keyboard_)
630      keyboard_->capture();
631    for (unsigned  int i = 0; i < joySticksSize_; i++)
632      joySticks_[i]->capture();
633
634    if (state_ != IS_CALIBRATE)
635    {
636      // call all the handlers for the held key events
637      for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
638        for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
639          activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_));
640
641      // call all the handlers for the held mouse button events
642      for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
643        for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
644          activeMouseHandlers_[iHandler]->mouseButtonHeld(mouseButtonsDown_[iButton]);
645
646      // call all the handlers for the held joy stick button events
647      for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
648        for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
649          for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
650            activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
651    }
652
653    // call the ticks for the handlers (need to be treated specially)
654    for (unsigned int iHandler = 0; iHandler < activeHandlers_.size(); iHandler++)
655      activeHandlers_[iHandler].first->tickInput(dt, activeHandlers_[iHandler].second);
656  }
657
658  void InputManager::_completeCalibration()
659  {
660    for (unsigned int i = 0; i < 24; i++)
661    {
662      // positive coefficient
663      if (marginalsMax_[i] == INT_MIN)
664        marginalsMax_[i] =  32767;
665      // coefficients
666      if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i])
667        joySticksCalibration_[0].positiveCoeff[i] =  1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]);
668      else
669        joySticksCalibration_[0].positiveCoeff[i] =  1.0f;
670
671      // config value
672      ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos");
673      assert(cont);
674      cont->set(i, joySticksCalibration_[0].positiveCoeff[i]);
675
676      // negative coefficient
677      if (marginalsMin_[i] == INT_MAX)
678        marginalsMin_[i] = -32768;
679      // coefficients
680      if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i])
681        joySticksCalibration_[0].negativeCoeff[i] = -1.0f/(marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]);
682      else
683        joySticksCalibration_[0].negativeCoeff[i] =  1.0f;
684      // config value
685      cont = getIdentifier()->getConfigValueContainer("CoeffNeg");
686      assert(cont);
687      cont->set(i, joySticksCalibration_[0].negativeCoeff[i]);
688
689      // zero states
690      if (i < 8)
691      {
692        if (!(i & 1))
693          joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX;
694        else
695          joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY;
696      }
697      else
698      {
699        if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size())
700          joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs;
701        else
702          joySticksCalibration_[0].zeroStates[i] = 0;
703      }
704      // config value
705      cont = getIdentifier()->getConfigValueContainer("Zero");
706      assert(cont);
707      cont->set(i, joySticksCalibration_[0].zeroStates[i]);
708    }
709  }
710
711  // ###### Key Events ######
712
713  /**
714    @brief Event handler for the keyPressed Event.
715    @param e Event information
716  */
717  bool InputManager::keyPressed(const OIS::KeyEvent &e)
718  {
719    // check whether the key already is in the list (can happen when focus was lost)
720    unsigned int iKey = 0;
721    while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key)
722      iKey++;
723    if (iKey == keysDown_.size())
724      keysDown_.push_back(Key(e));
725
726    // update modifiers
727    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
728      keyboardModifiers_ |= KeyboardModifier::Alt;   // alt key
729    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
730      keyboardModifiers_ |= KeyboardModifier::Ctrl;  // ctrl key
731    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
732      keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
733
734    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
735      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
736
737    return true;
738  }
739
740  /**
741    @brief Event handler for the keyReleased Event.
742    @param e Event information
743  */
744  bool InputManager::keyReleased(const OIS::KeyEvent &e)
745  {
746    // remove the key from the keysDown_ list
747    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
748    {
749      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
750      {
751        keysDown_.erase(keysDown_.begin() + iKey);
752        break;
753      }
754    }
755
756    // update modifiers
757    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
758      keyboardModifiers_ &= ~KeyboardModifier::Alt;   // alt key
759    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
760      keyboardModifiers_ &= ~KeyboardModifier::Ctrl;  // ctrl key
761    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
762      keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
763
764    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
765      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
766
767    return true;
768  }
769
770
771  // ###### Mouse Events ######
772
773  /**
774    @brief Event handler for the mouseMoved Event.
775    @param e Event information
776  */
777  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
778  {
779    // check for actual moved event
780    if (e.state.X.rel != 0 || e.state.Y.rel != 0)
781    {
782      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
783        activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs),
784            IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height));
785    }
786
787    // check for mouse scrolled event
788    if (e.state.Z.rel != 0)
789    {
790      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
791        activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
792    }
793
794    return true;
795  }
796
797  /**
798    @brief Event handler for the mousePressed Event.
799    @param e Event information
800    @param id The ID of the mouse button
801  */
802  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
803  {
804    // check whether the button already is in the list (can happen when focus was lost)
805    unsigned int iButton = 0;
806    while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id)
807      iButton++;
808    if (iButton == mouseButtonsDown_.size())
809      mouseButtonsDown_.push_back((MouseButton::Enum)id);
810
811    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
812      activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id);
813
814    return true;
815  }
816
817  /**
818    @brief Event handler for the mouseReleased Event.
819    @param e Event information
820    @param id The ID of the mouse button
821  */
822  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
823  {
824    // remove the button from the keysDown_ list
825    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
826    {
827      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
828      {
829        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
830        break;
831      }
832    }
833
834    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
835      activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id);
836
837    return true;
838  }
839
840
841  // ###### Joy Stick Events ######
842
843  inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg)
844  {
845    // use the device to identify which one called the method
846    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
847    unsigned int iJoyStick = 0;
848    while (joySticks_[iJoyStick] != joyStick)
849    {
850      iJoyStick++;
851      if (iJoyStick == joySticksSize_)
852      {
853        CCOUT(3) << "Unknown joystick fired an event. This means there is a bug somewhere! Aborting." << std::endl;
854        abort();
855      }
856    }
857    return iJoyStick;
858  }
859
860  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
861  {
862    unsigned int iJoyStick = _getJoystick(arg);
863
864    // check whether the button already is in the list (can happen when focus was lost)
865    std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
866    unsigned int iButton = 0;
867    while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
868      iButton++;
869    if (iButton == buttonsDown.size())
870      buttonsDown.push_back(button);
871
872    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
873      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button);
874
875    return true;
876  }
877
878  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
879  {
880    unsigned int iJoyStick = _getJoystick(arg);
881
882    // remove the button from the joyStickButtonsDown_ list
883    std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
884    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
885    {
886      if (buttonsDown[iButton] == button)
887      {
888        buttonsDown.erase(buttonsDown.begin() + iButton);
889        break;
890      }
891    }
892
893    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
894      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button);
895
896    return true;
897  }
898
899  void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value)
900  {
901    if (state_ == IS_CALIBRATE)
902    {
903      if (value > marginalsMax_[axis])
904        marginalsMax_[axis] = value;
905      if (value < marginalsMin_[axis])
906        marginalsMin_[axis] = value;
907    }
908    else
909    {
910      float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis];
911      if (fValue > 0.0f)
912        fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis];
913      else
914        fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis];
915
916      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
917        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue);
918    }
919  }
920
921  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
922  {
923    //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000)
924    //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;}
925
926    unsigned int iJoyStick = _getJoystick(arg);
927
928    // keep in mind that the first 8 axes are reserved for the sliders
929    _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs);
930
931    return true;
932  }
933
934  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
935  {
936    //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000)
937    //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;}
938    //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl;
939
940    unsigned int iJoyStick = _getJoystick(arg);
941
942    if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
943      _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX);
944    else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
945      _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
946
947    return true;
948  }
949
950  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
951  {
952    unsigned int iJoyStick = _getJoystick(arg);
953
954    // translate the POV into 8 simple buttons
955    int lastState = povStates_[iJoyStick][id];
956    if (lastState & OIS::Pov::North)
957      buttonReleased(arg, 32 + id * 4 + 0);
958    if (lastState & OIS::Pov::South)
959      buttonReleased(arg, 32 + id * 4 + 1);
960    if (lastState & OIS::Pov::East)
961      buttonReleased(arg, 32 + id * 4 + 2);
962    if (lastState & OIS::Pov::West)
963      buttonReleased(arg, 32 + id * 4 + 3);
964
965    povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
966
967    int currentState = povStates_[iJoyStick][id];
968    if (currentState & OIS::Pov::North)
969      buttonPressed(arg, 32 + id * 4 + 0);
970    if (currentState & OIS::Pov::South)
971      buttonPressed(arg, 32 + id * 4 + 1);
972    if (currentState & OIS::Pov::East)
973      buttonPressed(arg, 32 + id * 4 + 2);
974    if (currentState & OIS::Pov::West)
975      buttonPressed(arg, 32 + id * 4 + 3);
976
977    return true;
978  }
979
980  /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
981  {
982    unsigned int iJoyStick = _getJoystick(arg);
983
984    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
985      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
986
987    return true;
988  }*/
989
990
991  // ################################
992  // ### Static Interface Methods ###
993  // ################################
994  // ################################
995
996  std::string InputManager::bindingCommmandString_s = "";
997
998  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
999    bool createKeyboard, bool createMouse, bool createJoySticks)
1000  {
1001    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
1002          createKeyboard, createMouse, createJoySticks);
1003  }
1004
1005  bool InputManager::initialiseKeyboard()
1006  {
1007    return _getSingleton()._initialiseKeyboard();
1008  }
1009
1010  bool InputManager::initialiseMouse()
1011  {
1012    return _getSingleton()._initialiseMouse();
1013  }
1014
1015  bool InputManager::initialiseJoySticks()
1016  {
1017    return _getSingleton()._initialiseJoySticks();
1018  }
1019
1020  int InputManager::numberOfKeyboards()
1021  {
1022    if (_getSingleton().keyboard_ != 0)
1023      return 1;
1024    else
1025      return 0;
1026  }
1027
1028  int InputManager::numberOfMice()
1029  {
1030    if (_getSingleton().mouse_ != 0)
1031      return 1;
1032    else
1033      return 0;
1034  }
1035
1036  int InputManager::numberOfJoySticks()
1037  {
1038    return _getSingleton().joySticksSize_;
1039  }
1040
1041  /*bool InputManager::isKeyDown(KeyCode::Enum key)
1042  {
1043    if (_getSingleton().keyboard_)
1044      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
1045    else
1046      return false;
1047  }*/
1048
1049  /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
1050  {
1051    if (_getSingleton().keyboard_)
1052      return isModifierDown(modifier);
1053    else
1054      return false;
1055  }*/
1056
1057  /*const MouseState InputManager::getMouseState()
1058  {
1059    if (_getSingleton().mouse_)
1060      return _getSingleton().mouse_->getMouseState();
1061    else
1062      return MouseState();
1063  }*/
1064
1065  /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
1066  {
1067    if (ID < _getSingleton().joySticksSize_)
1068      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
1069    else
1070      return JoyStickState();
1071  }*/
1072
1073  void InputManager::destroy()
1074  {
1075    _getSingleton()._destroy();
1076  }
1077
1078  void InputManager::destroyKeyboard()
1079  {
1080    return _getSingleton()._destroyKeyboard();
1081  }
1082
1083  void InputManager::destroyMouse()
1084  {
1085    return _getSingleton()._destroyMouse();
1086  }
1087
1088  void InputManager::destroyJoySticks()
1089  {
1090    return _getSingleton()._destroyJoySticks();
1091  }
1092
1093
1094  /**
1095    @brief Adjusts the mouse window metrics.
1096    This method has to be called every time the size of the window changes.
1097    @param width The new width of the render window
1098    @param height the new height of the render window
1099  */
1100  void InputManager::setWindowExtents(const int width, const int height)
1101  {
1102    if (_getSingleton().mouse_)
1103    {
1104      // Set mouse region (if window resizes, we should alter this to reflect as well)
1105      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
1106      mouseState.width  = width;
1107      mouseState.height = height;
1108    }
1109  }
1110
1111  /**
1112    @brief Sets the input mode to either GUI, inGame or Buffer
1113    @param mode The new input mode
1114    @remark Only has an affect if the mode actually changes
1115  */
1116  void InputManager::setInputState(const InputState state)
1117  {
1118    _getSingleton().stateRequest_ = state;
1119  }
1120
1121  /**
1122    @brief Returns the current input handling method
1123    @return The current input mode.
1124  */
1125  InputManager::InputState InputManager::getInputState()
1126  {
1127    return _getSingleton().state_;
1128  }
1129
1130  void InputManager::storeKeyStroke(const std::string& name)
1131  {
1132    setInputState(IS_NODETECT);
1133    COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl;
1134    CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false);
1135  }
1136
1137  void InputManager::keyBind(const std::string& command)
1138  {
1139    bindingCommmandString_s = command;
1140    setInputState(IS_DETECT);
1141    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
1142  }
1143
1144  void InputManager::calibrate()
1145  {
1146    _getSingleton().setInputState(IS_CALIBRATE);
1147  }
1148
1149  void InputManager::tick(float dt)
1150  {
1151    _getSingleton()._tick(dt);
1152  }
1153
1154  // ###### KeyHandler ######
1155
1156  /**
1157    @brief Adds a new key handler.
1158    @param handler Pointer to the handler object.
1159    @param name Unique name of the handler.
1160    @return True if added, false if name already existed.
1161  */
1162  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
1163  {
1164    if (!handler)
1165      return false;
1166    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
1167    {
1168      _getSingleton().keyHandlers_[name] = handler;
1169      return true;
1170    }
1171    else
1172      return false;
1173  }
1174
1175  /**
1176    @brief Removes a Key handler from the list.
1177    @param name Unique name of the handler.
1178    @return True if removal was successful, false if name was not found.
1179  */
1180  bool InputManager::removeKeyHandler(const std::string &name)
1181  {
1182    disableKeyHandler(name);
1183    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
1184    if (it != _getSingleton().keyHandlers_.end())
1185    {
1186      _getSingleton().keyHandlers_.erase(it);
1187      return true;
1188    }
1189    else
1190      return false;
1191  }
1192
1193  /**
1194    @brief Returns the pointer to a handler.
1195    @param name Unique name of the handler.
1196    @return Pointer to the instance, 0 if name was not found.
1197  */
1198  KeyHandler* InputManager::getKeyHandler(const std::string& name)
1199  {
1200    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
1201    if (it != _getSingleton().keyHandlers_.end())
1202    {
1203      return (*it).second;
1204    }
1205    else
1206      return 0;
1207  }
1208
1209  /**
1210    @brief Enables a specific key handler that has already been added.
1211    @param name Unique name of the handler.
1212    @return False if name was not found, true otherwise.
1213  */
1214  bool InputManager::enableKeyHandler(const std::string& name)
1215  {
1216    // get pointer from the map with all stored handlers
1217    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1218    if (mapIt == _getSingleton().keyHandlers_.end())
1219      return false;
1220    // see whether the handler already is in the list
1221    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1222          it != _getSingleton().activeKeyHandlers_.end(); it++)
1223    {
1224      if ((*it) == (*mapIt).second)
1225      {
1226        return true;
1227      }
1228    }
1229    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
1230    _getSingleton().stateRequest_ = IS_CUSTOM;
1231    _getSingleton()._updateTickables();
1232    return true;
1233  }
1234
1235  /**
1236    @brief Disables a specific key handler.
1237    @param name Unique name of the handler.
1238    @return False if name was not found, true otherwise.
1239  */
1240  bool InputManager::disableKeyHandler(const std::string &name)
1241  {
1242    // get pointer from the map with all stored handlers
1243    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1244    if (mapIt == _getSingleton().keyHandlers_.end())
1245      return false;
1246    // look for the handler in the list
1247    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1248          it != _getSingleton().activeKeyHandlers_.end(); it++)
1249    {
1250      if ((*it) == (*mapIt).second)
1251      {
1252        _getSingleton().activeKeyHandlers_.erase(it);
1253        _getSingleton().stateRequest_ = IS_CUSTOM;
1254        _getSingleton()._updateTickables();
1255        return true;
1256      }
1257    }
1258    return true;
1259  }
1260
1261  /**
1262    @brief Checks whether a key handler is active
1263    @param name Unique name of the handler.
1264    @return False if key handler is not active or doesn't exist, true otherwise.
1265  */
1266  bool InputManager::isKeyHandlerActive(const std::string& name)
1267  {
1268    // get pointer from the map with all stored handlers
1269    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1270    if (mapIt == _getSingleton().keyHandlers_.end())
1271      return false;
1272    // see whether the handler already is in the list
1273    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1274          it != _getSingleton().activeKeyHandlers_.end(); it++)
1275    {
1276      if ((*it) == (*mapIt).second)
1277        return true;
1278    }
1279    return false;
1280  }
1281
1282
1283  // ###### MouseHandler ######
1284  /**
1285    @brief Adds a new mouse handler.
1286    @param handler Pointer to the handler object.
1287    @param name Unique name of the handler.
1288    @return True if added, false if name already existed.
1289  */
1290  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
1291  {
1292    if (!handler)
1293      return false;
1294    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
1295    {
1296      _getSingleton().mouseHandlers_[name] = handler;
1297      return true;
1298    }
1299    else
1300      return false;
1301  }
1302
1303  /**
1304    @brief Removes a Mouse handler from the list.
1305    @param name Unique name of the handler.
1306    @return True if removal was successful, false if name was not found.
1307  */
1308  bool InputManager::removeMouseHandler(const std::string &name)
1309  {
1310    disableMouseHandler(name);
1311    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1312    if (it != _getSingleton().mouseHandlers_.end())
1313    {
1314      _getSingleton().mouseHandlers_.erase(it);
1315      return true;
1316    }
1317    else
1318      return false;
1319  }
1320
1321  /**
1322    @brief Returns the pointer to a handler.
1323    @param name Unique name of the handler.
1324    @return Pointer to the instance, 0 if name was not found.
1325  */
1326  MouseHandler* InputManager::getMouseHandler(const std::string& name)
1327  {
1328    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1329    if (it != _getSingleton().mouseHandlers_.end())
1330    {
1331      return (*it).second;
1332    }
1333    else
1334      return 0;
1335  }
1336
1337  /**
1338    @brief Enables a specific mouse handler that has already been added.
1339    @param name Unique name of the handler.
1340    @return False if name was not found, true otherwise.
1341  */
1342  bool InputManager::enableMouseHandler(const std::string& name)
1343  {
1344    // get pointer from the map with all stored handlers
1345    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1346    if (mapIt == _getSingleton().mouseHandlers_.end())
1347      return false;
1348    // see whether the handler already is in the list
1349    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1350          it != _getSingleton().activeMouseHandlers_.end(); it++)
1351    {
1352      if ((*it) == (*mapIt).second)
1353      {
1354        return true;
1355      }
1356    }
1357    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1358    _getSingleton().stateRequest_ = IS_CUSTOM;
1359    _getSingleton()._updateTickables();
1360    return true;
1361  }
1362
1363  /**
1364    @brief Disables a specific mouse handler.
1365    @param name Unique name of the handler.
1366    @return False if name was not found, true otherwise.
1367  */
1368  bool InputManager::disableMouseHandler(const std::string &name)
1369  {
1370    // get pointer from the map with all stored handlers
1371    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1372    if (mapIt == _getSingleton().mouseHandlers_.end())
1373      return false;
1374    // look for the handler in the list
1375    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1376          it != _getSingleton().activeMouseHandlers_.end(); it++)
1377    {
1378      if ((*it) == (*mapIt).second)
1379      {
1380        _getSingleton().activeMouseHandlers_.erase(it);
1381        _getSingleton().stateRequest_ = IS_CUSTOM;
1382        _getSingleton()._updateTickables();
1383        return true;
1384      }
1385    }
1386    return true;
1387  }
1388
1389  /**
1390    @brief Checks whether a mouse handler is active
1391    @param name Unique name of the handler.
1392    @return False if key handler is not active or doesn't exist, true otherwise.
1393  */
1394  bool InputManager::isMouseHandlerActive(const std::string& name)
1395  {
1396    // get pointer from the map with all stored handlers
1397    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1398    if (mapIt == _getSingleton().mouseHandlers_.end())
1399      return false;
1400    // see whether the handler already is in the list
1401    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1402          it != _getSingleton().activeMouseHandlers_.end(); it++)
1403    {
1404      if ((*it) == (*mapIt).second)
1405        return true;
1406    }
1407    return false;
1408  }
1409
1410
1411  // ###### JoyStickHandler ######
1412
1413  /**
1414    @brief Adds a new joy stick handler.
1415    @param handler Pointer to the handler object.
1416    @param name Unique name of the handler.
1417    @return True if added, false if name already existed.
1418  */
1419  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1420  {
1421    if (!handler)
1422      return false;
1423    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1424    {
1425      _getSingleton().joyStickHandlers_[name] = handler;
1426      return true;
1427    }
1428    else
1429      return false;
1430  }
1431
1432  /**
1433    @brief Removes a JoyStick handler from the list.
1434    @param name Unique name of the handler.
1435    @return True if removal was successful, false if name was not found.
1436  */
1437  bool InputManager::removeJoyStickHandler(const std::string &name)
1438  {
1439    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1440          itstick != _getSingleton().joySticks_.end(); itstick++)
1441      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1442
1443    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1444    if (it != _getSingleton().joyStickHandlers_.end())
1445    {
1446      _getSingleton().joyStickHandlers_.erase(it);
1447      return true;
1448    }
1449    else
1450      return false;
1451  }
1452
1453  /**
1454    @brief Returns the pointer to a handler.
1455    @param name Unique name of the handler.
1456    @return Pointer to the instance, 0 if name was not found.
1457  */
1458  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1459  {
1460    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1461    if (it != _getSingleton().joyStickHandlers_.end())
1462    {
1463      return (*it).second;
1464    }
1465    else
1466      return 0;
1467  }
1468
1469  /**
1470    @brief Enables a specific joy stick handler that has already been added.
1471    @param name Unique name of the handler.
1472    @return False if name or id was not found, true otherwise.
1473  */
1474  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1475  {
1476    // get handler pointer from the map with all stored handlers
1477    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1478    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1479      return false;
1480
1481    // check for existence of the ID
1482    if (ID >= _getSingleton().joySticksSize_)
1483      return false;
1484
1485    // see whether the handler already is in the list
1486    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1487          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1488    {
1489      if ((*it) == (*handlerIt).second)
1490      {
1491        return true;
1492      }
1493    }
1494    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1495    _getSingleton().stateRequest_ = IS_CUSTOM;
1496    _getSingleton()._updateTickables();
1497    return true;
1498  }
1499
1500  /**
1501    @brief Disables a specific joy stick handler.
1502    @param name Unique name of the handler.
1503    @return False if name or id was not found, true otherwise.
1504  */
1505  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1506  {
1507    // get handler pointer from the map with all stored handlers
1508    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1509    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1510      return false;
1511
1512    // check for existence of the ID
1513    if (ID >= _getSingleton().joySticksSize_)
1514      return false;
1515
1516    // look for the handler in the list
1517    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1518          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1519    {
1520      if ((*it) == (*handlerIt).second)
1521      {
1522        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1523        _getSingleton().stateRequest_ = IS_CUSTOM;
1524        _getSingleton()._updateTickables();
1525        return true;
1526      }
1527    }
1528    return true;
1529  }
1530
1531  /**
1532    @brief Checks whether a joy stick handler is active
1533    @param name Unique name of the handler.
1534    @return False if key handler is not active or doesn't exist, true otherwise.
1535  */
1536  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1537  {
1538    // get handler pointer from the map with all stored handlers
1539    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1540    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1541      return false;
1542
1543    // check for existence of the ID
1544    if (ID >= _getSingleton().joySticksSize_)
1545      return false;
1546
1547    // see whether the handler already is in the list
1548    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1549          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1550    {
1551      if ((*it) == (*handlerIt).second)
1552        return true;
1553    }
1554    return false;
1555  }
1556
1557}
Note: See TracBrowser for help on using the repository browser.