Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1630 was 1630, checked in by rgrieder, 16 years ago

converted input system to 4 spaces/tab

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