Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1735 was 1735, checked in by scheusso, 16 years ago

network branch merged into trunk

  • Property svn:eol-style set to native
File size: 49.1 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    else
726      return true;
727
728    // update modifiers
729    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
730      keyboardModifiers_ |= KeyboardModifier::Alt;   // alt key
731    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
732      keyboardModifiers_ |= KeyboardModifier::Ctrl;  // ctrl key
733    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
734      keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
735
736    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
737      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
738
739    return true;
740  }
741
742  /**
743    @brief Event handler for the keyReleased Event.
744    @param e Event information
745  */
746  bool InputManager::keyReleased(const OIS::KeyEvent &e)
747  {
748    // remove the key from the keysDown_ list
749    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
750    {
751      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
752      {
753        keysDown_.erase(keysDown_.begin() + iKey);
754        break;
755      }
756    }
757
758    // update modifiers
759    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
760      keyboardModifiers_ &= ~KeyboardModifier::Alt;   // alt key
761    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
762      keyboardModifiers_ &= ~KeyboardModifier::Ctrl;  // ctrl key
763    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
764      keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
765
766    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
767      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
768
769    return true;
770  }
771
772
773  // ###### Mouse Events ######
774
775  /**
776    @brief Event handler for the mouseMoved Event.
777    @param e Event information
778  */
779  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
780  {
781    // check for actual moved event
782    if (e.state.X.rel != 0 || e.state.Y.rel != 0)
783    {
784      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
785        activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs),
786            IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height));
787    }
788
789    // check for mouse scrolled event
790    if (e.state.Z.rel != 0)
791    {
792      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
793        activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
794    }
795
796    return true;
797  }
798
799  /**
800    @brief Event handler for the mousePressed Event.
801    @param e Event information
802    @param id The ID of the mouse button
803  */
804  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
805  {
806    // check whether the button already is in the list (can happen when focus was lost)
807    unsigned int iButton = 0;
808    while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id)
809      iButton++;
810    if (iButton == mouseButtonsDown_.size())
811      mouseButtonsDown_.push_back((MouseButton::Enum)id);
812
813    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
814      activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id);
815
816    return true;
817  }
818
819  /**
820    @brief Event handler for the mouseReleased Event.
821    @param e Event information
822    @param id The ID of the mouse button
823  */
824  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
825  {
826    // remove the button from the keysDown_ list
827    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
828    {
829      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
830      {
831        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
832        break;
833      }
834    }
835
836    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
837      activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id);
838
839    return true;
840  }
841
842
843  // ###### Joy Stick Events ######
844
845  inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg)
846  {
847    // use the device to identify which one called the method
848    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
849    unsigned int iJoyStick = 0;
850    while (joySticks_[iJoyStick] != joyStick)
851    {
852      iJoyStick++;
853      if (iJoyStick == joySticksSize_)
854      {
855        CCOUT(3) << "Unknown joystick fired an event. This means there is a bug somewhere! Aborting." << std::endl;
856        abort();
857      }
858    }
859    return iJoyStick;
860  }
861
862  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
863  {
864    unsigned int iJoyStick = _getJoystick(arg);
865
866    // check whether the button already is in the list (can happen when focus was lost)
867    std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
868    unsigned int iButton = 0;
869    while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
870      iButton++;
871    if (iButton == buttonsDown.size())
872      buttonsDown.push_back(button);
873
874    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
875      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button);
876
877    return true;
878  }
879
880  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
881  {
882    unsigned int iJoyStick = _getJoystick(arg);
883
884    // remove the button from the joyStickButtonsDown_ list
885    std::vector<int>& buttonsDown = joyStickButtonsDown_[iJoyStick];
886    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
887    {
888      if (buttonsDown[iButton] == button)
889      {
890        buttonsDown.erase(buttonsDown.begin() + iButton);
891        break;
892      }
893    }
894
895    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
896      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button);
897
898    return true;
899  }
900
901  void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value)
902  {
903    if (state_ == IS_CALIBRATE)
904    {
905      if (value > marginalsMax_[axis])
906        marginalsMax_[axis] = value;
907      if (value < marginalsMin_[axis])
908        marginalsMin_[axis] = value;
909    }
910    else
911    {
912      float fValue = value - joySticksCalibration_[iJoyStick].zeroStates[axis];
913      if (fValue > 0.0f)
914        fValue *= joySticksCalibration_[iJoyStick].positiveCoeff[axis];
915      else
916        fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis];
917
918      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
919        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue);
920    }
921  }
922
923  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
924  {
925    //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000)
926    //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;}
927
928    unsigned int iJoyStick = _getJoystick(arg);
929
930    // keep in mind that the first 8 axes are reserved for the sliders
931    _fireAxis(iJoyStick, axis + 8, arg.state.mAxes[axis].abs);
932
933    return true;
934  }
935
936  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
937  {
938    //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000)
939    //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;}
940    //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl;
941
942    unsigned int iJoyStick = _getJoystick(arg);
943
944    if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
945      _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX);
946    else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
947      _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
948
949    return true;
950  }
951
952  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
953  {
954    unsigned int iJoyStick = _getJoystick(arg);
955
956    // translate the POV into 8 simple buttons
957    int lastState = povStates_[iJoyStick][id];
958    if (lastState & OIS::Pov::North)
959      buttonReleased(arg, 32 + id * 4 + 0);
960    if (lastState & OIS::Pov::South)
961      buttonReleased(arg, 32 + id * 4 + 1);
962    if (lastState & OIS::Pov::East)
963      buttonReleased(arg, 32 + id * 4 + 2);
964    if (lastState & OIS::Pov::West)
965      buttonReleased(arg, 32 + id * 4 + 3);
966
967    povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
968
969    int currentState = povStates_[iJoyStick][id];
970    if (currentState & OIS::Pov::North)
971      buttonPressed(arg, 32 + id * 4 + 0);
972    if (currentState & OIS::Pov::South)
973      buttonPressed(arg, 32 + id * 4 + 1);
974    if (currentState & OIS::Pov::East)
975      buttonPressed(arg, 32 + id * 4 + 2);
976    if (currentState & OIS::Pov::West)
977      buttonPressed(arg, 32 + id * 4 + 3);
978
979    return true;
980  }
981
982  /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
983  {
984    unsigned int iJoyStick = _getJoystick(arg);
985
986    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
987      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
988
989    return true;
990  }*/
991
992
993  // ################################
994  // ### Static Interface Methods ###
995  // ################################
996  // ################################
997
998  std::string InputManager::bindingCommmandString_s = "";
999
1000  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
1001    bool createKeyboard, bool createMouse, bool createJoySticks)
1002  {
1003    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
1004          createKeyboard, createMouse, createJoySticks);
1005  }
1006
1007  bool InputManager::initialiseKeyboard()
1008  {
1009    return _getSingleton()._initialiseKeyboard();
1010  }
1011
1012  bool InputManager::initialiseMouse()
1013  {
1014    return _getSingleton()._initialiseMouse();
1015  }
1016
1017  bool InputManager::initialiseJoySticks()
1018  {
1019    return _getSingleton()._initialiseJoySticks();
1020  }
1021
1022  int InputManager::numberOfKeyboards()
1023  {
1024    if (_getSingleton().keyboard_ != 0)
1025      return 1;
1026    else
1027      return 0;
1028  }
1029
1030  int InputManager::numberOfMice()
1031  {
1032    if (_getSingleton().mouse_ != 0)
1033      return 1;
1034    else
1035      return 0;
1036  }
1037
1038  int InputManager::numberOfJoySticks()
1039  {
1040    return _getSingleton().joySticksSize_;
1041  }
1042
1043  /*bool InputManager::isKeyDown(KeyCode::Enum key)
1044  {
1045    if (_getSingleton().keyboard_)
1046      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
1047    else
1048      return false;
1049  }*/
1050
1051  /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
1052  {
1053    if (_getSingleton().keyboard_)
1054      return isModifierDown(modifier);
1055    else
1056      return false;
1057  }*/
1058
1059  /*const MouseState InputManager::getMouseState()
1060  {
1061    if (_getSingleton().mouse_)
1062      return _getSingleton().mouse_->getMouseState();
1063    else
1064      return MouseState();
1065  }*/
1066
1067  /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
1068  {
1069    if (ID < _getSingleton().joySticksSize_)
1070      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
1071    else
1072      return JoyStickState();
1073  }*/
1074
1075  void InputManager::destroy()
1076  {
1077    _getSingleton()._destroy();
1078  }
1079
1080  void InputManager::destroyKeyboard()
1081  {
1082    return _getSingleton()._destroyKeyboard();
1083  }
1084
1085  void InputManager::destroyMouse()
1086  {
1087    return _getSingleton()._destroyMouse();
1088  }
1089
1090  void InputManager::destroyJoySticks()
1091  {
1092    return _getSingleton()._destroyJoySticks();
1093  }
1094
1095
1096  /**
1097    @brief Adjusts the mouse window metrics.
1098    This method has to be called every time the size of the window changes.
1099    @param width The new width of the render window
1100    @param height the new height of the render window
1101  */
1102  void InputManager::setWindowExtents(const int width, const int height)
1103  {
1104    if (_getSingleton().mouse_)
1105    {
1106      // Set mouse region (if window resizes, we should alter this to reflect as well)
1107      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
1108      mouseState.width  = width;
1109      mouseState.height = height;
1110    }
1111  }
1112
1113  /**
1114    @brief Sets the input mode to either GUI, inGame or Buffer
1115    @param mode The new input mode
1116    @remark Only has an affect if the mode actually changes
1117  */
1118  void InputManager::setInputState(const InputState state)
1119  {
1120    _getSingleton().stateRequest_ = state;
1121  }
1122
1123  /**
1124    @brief Returns the current input handling method
1125    @return The current input mode.
1126  */
1127  InputManager::InputState InputManager::getInputState()
1128  {
1129    return _getSingleton().state_;
1130  }
1131
1132  void InputManager::storeKeyStroke(const std::string& name)
1133  {
1134    setInputState(IS_NODETECT);
1135    COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl;
1136    CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false);
1137  }
1138
1139  void InputManager::keyBind(const std::string& command)
1140  {
1141    bindingCommmandString_s = command;
1142    setInputState(IS_DETECT);
1143    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
1144  }
1145
1146  void InputManager::calibrate()
1147  {
1148    _getSingleton().setInputState(IS_CALIBRATE);
1149  }
1150
1151  void InputManager::tick(float dt)
1152  {
1153    _getSingleton()._tick(dt);
1154  }
1155
1156  // ###### KeyHandler ######
1157
1158  /**
1159    @brief Adds a new key handler.
1160    @param handler Pointer to the handler object.
1161    @param name Unique name of the handler.
1162    @return True if added, false if name already existed.
1163  */
1164  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
1165  {
1166    if (!handler)
1167      return false;
1168    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
1169    {
1170      _getSingleton().keyHandlers_[name] = handler;
1171      return true;
1172    }
1173    else
1174      return false;
1175  }
1176
1177  /**
1178    @brief Removes a Key handler from the list.
1179    @param name Unique name of the handler.
1180    @return True if removal was successful, false if name was not found.
1181  */
1182  bool InputManager::removeKeyHandler(const std::string &name)
1183  {
1184    disableKeyHandler(name);
1185    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
1186    if (it != _getSingleton().keyHandlers_.end())
1187    {
1188      _getSingleton().keyHandlers_.erase(it);
1189      return true;
1190    }
1191    else
1192      return false;
1193  }
1194
1195  /**
1196    @brief Returns the pointer to a handler.
1197    @param name Unique name of the handler.
1198    @return Pointer to the instance, 0 if name was not found.
1199  */
1200  KeyHandler* InputManager::getKeyHandler(const std::string& name)
1201  {
1202    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
1203    if (it != _getSingleton().keyHandlers_.end())
1204    {
1205      return (*it).second;
1206    }
1207    else
1208      return 0;
1209  }
1210
1211  /**
1212    @brief Enables a specific key handler that has already been added.
1213    @param name Unique name of the handler.
1214    @return False if name was not found, true otherwise.
1215  */
1216  bool InputManager::enableKeyHandler(const std::string& name)
1217  {
1218    // get pointer from the map with all stored handlers
1219    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1220    if (mapIt == _getSingleton().keyHandlers_.end())
1221      return false;
1222    // see whether the handler already is in the list
1223    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1224          it != _getSingleton().activeKeyHandlers_.end(); it++)
1225    {
1226      if ((*it) == (*mapIt).second)
1227      {
1228        return true;
1229      }
1230    }
1231    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
1232    _getSingleton().stateRequest_ = IS_CUSTOM;
1233    _getSingleton()._updateTickables();
1234    return true;
1235  }
1236
1237  /**
1238    @brief Disables a specific key handler.
1239    @param name Unique name of the handler.
1240    @return False if name was not found, true otherwise.
1241  */
1242  bool InputManager::disableKeyHandler(const std::string &name)
1243  {
1244    // get pointer from the map with all stored handlers
1245    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1246    if (mapIt == _getSingleton().keyHandlers_.end())
1247      return false;
1248    // look for the handler in the list
1249    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1250          it != _getSingleton().activeKeyHandlers_.end(); it++)
1251    {
1252      if ((*it) == (*mapIt).second)
1253      {
1254        _getSingleton().activeKeyHandlers_.erase(it);
1255        _getSingleton().stateRequest_ = IS_CUSTOM;
1256        _getSingleton()._updateTickables();
1257        return true;
1258      }
1259    }
1260    return true;
1261  }
1262
1263  /**
1264    @brief Checks whether a key handler is active
1265    @param name Unique name of the handler.
1266    @return False if key handler is not active or doesn't exist, true otherwise.
1267  */
1268  bool InputManager::isKeyHandlerActive(const std::string& name)
1269  {
1270    // get pointer from the map with all stored handlers
1271    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
1272    if (mapIt == _getSingleton().keyHandlers_.end())
1273      return false;
1274    // see whether the handler already is in the list
1275    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
1276          it != _getSingleton().activeKeyHandlers_.end(); it++)
1277    {
1278      if ((*it) == (*mapIt).second)
1279        return true;
1280    }
1281    return false;
1282  }
1283
1284
1285  // ###### MouseHandler ######
1286  /**
1287    @brief Adds a new mouse handler.
1288    @param handler Pointer to the handler object.
1289    @param name Unique name of the handler.
1290    @return True if added, false if name already existed.
1291  */
1292  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
1293  {
1294    if (!handler)
1295      return false;
1296    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
1297    {
1298      _getSingleton().mouseHandlers_[name] = handler;
1299      return true;
1300    }
1301    else
1302      return false;
1303  }
1304
1305  /**
1306    @brief Removes a Mouse handler from the list.
1307    @param name Unique name of the handler.
1308    @return True if removal was successful, false if name was not found.
1309  */
1310  bool InputManager::removeMouseHandler(const std::string &name)
1311  {
1312    disableMouseHandler(name);
1313    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1314    if (it != _getSingleton().mouseHandlers_.end())
1315    {
1316      _getSingleton().mouseHandlers_.erase(it);
1317      return true;
1318    }
1319    else
1320      return false;
1321  }
1322
1323  /**
1324    @brief Returns the pointer to a handler.
1325    @param name Unique name of the handler.
1326    @return Pointer to the instance, 0 if name was not found.
1327  */
1328  MouseHandler* InputManager::getMouseHandler(const std::string& name)
1329  {
1330    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1331    if (it != _getSingleton().mouseHandlers_.end())
1332    {
1333      return (*it).second;
1334    }
1335    else
1336      return 0;
1337  }
1338
1339  /**
1340    @brief Enables a specific mouse handler that has already been added.
1341    @param name Unique name of the handler.
1342    @return False if name was not found, true otherwise.
1343  */
1344  bool InputManager::enableMouseHandler(const std::string& name)
1345  {
1346    // get pointer from the map with all stored handlers
1347    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1348    if (mapIt == _getSingleton().mouseHandlers_.end())
1349      return false;
1350    // see whether the handler already is in the list
1351    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1352          it != _getSingleton().activeMouseHandlers_.end(); it++)
1353    {
1354      if ((*it) == (*mapIt).second)
1355      {
1356        return true;
1357      }
1358    }
1359    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1360    _getSingleton().stateRequest_ = IS_CUSTOM;
1361    _getSingleton()._updateTickables();
1362    return true;
1363  }
1364
1365  /**
1366    @brief Disables a specific mouse handler.
1367    @param name Unique name of the handler.
1368    @return False if name was not found, true otherwise.
1369  */
1370  bool InputManager::disableMouseHandler(const std::string &name)
1371  {
1372    // get pointer from the map with all stored handlers
1373    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1374    if (mapIt == _getSingleton().mouseHandlers_.end())
1375      return false;
1376    // look for the handler in the list
1377    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1378          it != _getSingleton().activeMouseHandlers_.end(); it++)
1379    {
1380      if ((*it) == (*mapIt).second)
1381      {
1382        _getSingleton().activeMouseHandlers_.erase(it);
1383        _getSingleton().stateRequest_ = IS_CUSTOM;
1384        _getSingleton()._updateTickables();
1385        return true;
1386      }
1387    }
1388    return true;
1389  }
1390
1391  /**
1392    @brief Checks whether a mouse handler is active
1393    @param name Unique name of the handler.
1394    @return False if key handler is not active or doesn't exist, true otherwise.
1395  */
1396  bool InputManager::isMouseHandlerActive(const std::string& name)
1397  {
1398    // get pointer from the map with all stored handlers
1399    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1400    if (mapIt == _getSingleton().mouseHandlers_.end())
1401      return false;
1402    // see whether the handler already is in the list
1403    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1404          it != _getSingleton().activeMouseHandlers_.end(); it++)
1405    {
1406      if ((*it) == (*mapIt).second)
1407        return true;
1408    }
1409    return false;
1410  }
1411
1412
1413  // ###### JoyStickHandler ######
1414
1415  /**
1416    @brief Adds a new joy stick handler.
1417    @param handler Pointer to the handler object.
1418    @param name Unique name of the handler.
1419    @return True if added, false if name already existed.
1420  */
1421  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1422  {
1423    if (!handler)
1424      return false;
1425    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1426    {
1427      _getSingleton().joyStickHandlers_[name] = handler;
1428      return true;
1429    }
1430    else
1431      return false;
1432  }
1433
1434  /**
1435    @brief Removes a JoyStick handler from the list.
1436    @param name Unique name of the handler.
1437    @return True if removal was successful, false if name was not found.
1438  */
1439  bool InputManager::removeJoyStickHandler(const std::string &name)
1440  {
1441    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1442          itstick != _getSingleton().joySticks_.end(); itstick++)
1443      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1444
1445    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1446    if (it != _getSingleton().joyStickHandlers_.end())
1447    {
1448      _getSingleton().joyStickHandlers_.erase(it);
1449      return true;
1450    }
1451    else
1452      return false;
1453  }
1454
1455  /**
1456    @brief Returns the pointer to a handler.
1457    @param name Unique name of the handler.
1458    @return Pointer to the instance, 0 if name was not found.
1459  */
1460  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1461  {
1462    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1463    if (it != _getSingleton().joyStickHandlers_.end())
1464    {
1465      return (*it).second;
1466    }
1467    else
1468      return 0;
1469  }
1470
1471  /**
1472    @brief Enables a specific joy stick handler that has already been added.
1473    @param name Unique name of the handler.
1474    @return False if name or id was not found, true otherwise.
1475  */
1476  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1477  {
1478    // get handler pointer from the map with all stored handlers
1479    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1480    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1481      return false;
1482
1483    // check for existence of the ID
1484    if (ID >= _getSingleton().joySticksSize_)
1485      return false;
1486
1487    // see whether the handler already is in the list
1488    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1489          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1490    {
1491      if ((*it) == (*handlerIt).second)
1492      {
1493        return true;
1494      }
1495    }
1496    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1497    _getSingleton().stateRequest_ = IS_CUSTOM;
1498    _getSingleton()._updateTickables();
1499    return true;
1500  }
1501
1502  /**
1503    @brief Disables a specific joy stick handler.
1504    @param name Unique name of the handler.
1505    @return False if name or id was not found, true otherwise.
1506  */
1507  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1508  {
1509    // get handler pointer from the map with all stored handlers
1510    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1511    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1512      return false;
1513
1514    // check for existence of the ID
1515    if (ID >= _getSingleton().joySticksSize_)
1516      return false;
1517
1518    // look for the handler in the list
1519    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1520          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1521    {
1522      if ((*it) == (*handlerIt).second)
1523      {
1524        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1525        _getSingleton().stateRequest_ = IS_CUSTOM;
1526        _getSingleton()._updateTickables();
1527        return true;
1528      }
1529    }
1530    return true;
1531  }
1532
1533  /**
1534    @brief Checks whether a joy stick handler is active
1535    @param name Unique name of the handler.
1536    @return False if key handler is not active or doesn't exist, true otherwise.
1537  */
1538  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1539  {
1540    // get handler pointer from the map with all stored handlers
1541    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1542    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1543      return false;
1544
1545    // check for existence of the ID
1546    if (ID >= _getSingleton().joySticksSize_)
1547      return false;
1548
1549    // see whether the handler already is in the list
1550    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1551          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1552    {
1553      if ((*it) == (*handlerIt).second)
1554        return true;
1555    }
1556    return false;
1557  }
1558
1559}
Note: See TracBrowser for help on using the repository browser.