Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 20, 2008, 6:49:24 PM (16 years ago)
Author:
rgrieder
Message:

Finally! The InputManager is now working like I imagined it to. And it's even easier to use it as well.
A little explanation: Every time you change something about the input distribution, it is a change of 'state' represented by the class 'InputState'.
That can be for instance: "console", "game", "gui", etc. Every state has a name and a priority which describes who comes first. Now if one state doesn't handle mouse input or instance, then the one with the next lower priority gets it. To prevent that, you can add the 'EmptyHandler' to the state with setMouseHandler.
InputState is just an abstract base class. There are two classes implementing it: SimpleInputState and ExtendedInputState. The latter allows for multiple input handlers for one single device.

Basically, what you need to know is what you see in Orxonox.cc, InGameConsole.cc and Shell.cc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/core/input/InputManager.cc

    r1630 r1637  
    3838#include <limits.h>
    3939
     40#include "ois/OISException.h"
     41#include "ois/OISInputManager.h"
     42
    4043#include "core/CoreIncludes.h"
    4144#include "core/ConfigValueIncludes.h"
     
    4346#include "core/CommandExecutor.h"
    4447#include "core/ConsoleCommand.h"
    45 #include "core/Shell.h"               // hack!
    4648
    4749#include "InputBuffer.h"
     
    4951#include "KeyDetector.h"
    5052#include "CalibratorCallback.h"
    51 
    52 #include "src/ois/OISException.h"
    53 #include "src/ois/OISInputManager.h"
     53#include "InputState.h"
     54#include "SimpleInputState.h"
     55#include "ExtendedInputState.h"
    5456
    5557namespace orxonox
     
    5961    SetConsoleCommandShortcut(InputManager, calibrate);
    6062
     63    using namespace InputDevice;
     64
    6165    // ###############################
    6266    // ###    Internal Methods     ###
     
    7478        , mouse_(0)
    7579        , 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)
     80        , devicesNum_(0)
     81        , stateDetector_(0)
     82        , stateCalibrator_(0)
     83        , stateEmpty_(0)
    8384        , keyboardModifiers_(0)
     85        , bCalibrating_(false)
    8486    {
    8587        RegisterRootObject(InputManager);
     
    9294        A reference to the only instance of the InputManager
    9395    */
    94     InputManager& InputManager::_getSingleton()
     96    InputManager& InputManager::_getInstance()
    9597    {
    9698        static InputManager theOnlyInstance;
     
    106108        _destroy();
    107109    }
     110
     111
     112    // ############################################################
     113    // #####                  Initialisation                  #####
     114    // ##########                                        ##########
     115    // ############################################################
    108116
    109117    /**
     
    121129                                   bool createKeyboard, bool createMouse, bool createJoySticks)
    122130    {
    123         if (state_ == IS_UNINIT)
     131        if (inputSystem_ == 0)
    124132        {
    125133            CCOUT(3) << "Initialising Input System..." << std::endl;
     
    160168                _initialiseJoySticks();
    161169
     170            // set all the std::vector list sizes now that the devices have been created
     171            _redimensionLists();
     172
    162173            // Set mouse/joystick region
    163174            if (mouse_)
     
    166177            }
    167178
    168             state_ = IS_NONE;
    169179            CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
    170180
    171181            // 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");
     182            //buffer_ = new InputBuffer();
     183            //addKeyHandler(buffer_, "buffer");
     184            //Shell::getInstance().setInputBuffer(buffer_);
    190185
    191186            setConfigValues();
     187
     188            stateEmpty_ = createSimpleInputState("empty", -1);
     189            stateEmpty_->setHandler(new EmptyHandler());
     190            activeStates_[stateEmpty_->getPriority()] = stateEmpty_;
     191
     192            stateDetector_ = createSimpleInputState("detector", 101);
     193            KeyDetector* temp = new KeyDetector();
     194            temp->loadBindings("storeKeyStroke");
     195            stateDetector_->setHandler(temp);
     196
     197            stateCalibrator_ = createSimpleInputState("calibrator", 100);
     198            stateCalibrator_->setHandler(new EmptyHandler());
     199            InputBuffer* buffer = new InputBuffer();
     200            buffer->registerListener(this, &InputManager::_completeCalibration, '\r', true);
     201            stateCalibrator_->setKeyHandler(buffer);
     202
     203            _updateActiveStates();
    192204
    193205            CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
     
    232244        catch (OIS::Exception ex)
    233245        {
    234             // TODO: Test this output regarding formatting
    235246            CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
    236247                << "OIS error message: \"" << ex.eText << "\"" << std::endl;
     
    300311                    OIS::JoyStick* stig = static_cast<OIS::JoyStick*>
    301312                        (inputSystem_->createInputObject(OIS::OISJoyStick, true));
     313                    CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
    302314                    joySticks_.push_back(stig);
    303315                    // register our listener in OIS.
    304316                    stig->setEventCallback(this);
    305                     CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
    306317                    success = true;
    307318                }
     
    318329            return false;
    319330        }
     331        return success;
     332    }
     333
     334    void InputManager::_redimensionLists()
     335    {
    320336        joySticksSize_ = joySticks_.size();
    321         activeJoyStickHandlers_.resize(joySticksSize_);
    322         joyStickButtonsDown_.resize(joySticksSize_);
    323         povStates_.resize(joySticksSize_);
    324         sliderStates_.resize(joySticksSize_);
     337        devicesNum_ = 2 + joySticksSize_;
     338        joyStickButtonsDown_ .resize(joySticksSize_);
     339        povStates_           .resize(joySticksSize_);
     340        sliderStates_        .resize(joySticksSize_);
    325341        joySticksCalibration_.resize(joySticksSize_);
     342
    326343        for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++)
    327344        {
     
    334351            }
    335352        }
    336         return success;
     353
     354        // state management
     355        activeStatesTop_.resize(devicesNum_);
     356
     357        // inform all registered states
     358        for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin();
     359            it != inputStatesByPriority_.end(); ++it)
     360            (*it).second->setNumOfJoySticks(joySticksSize_);
    337361    }
    338362
     
    392416    }
    393417
     418
     419    // ############################################################
     420    // #####                    Destruction                   #####
     421    // ##########                                        ##########
     422    // ############################################################
     423
    394424    /**
    395425    @brief
     
    398428    void InputManager::_destroy()
    399429    {
    400         if (state_ != IS_UNINIT)
     430        if (inputSystem_)
    401431        {
    402432            CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
    403433
    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 
     434            // kick all active states 'nicely'
     435            for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin();
     436                rit != activeStates_.rend(); ++rit)
     437                (*rit).second->onLeave();
     438            activeStates_.clear();
     439
     440            // destroy our own states
     441            stateEmpty_->removeAndDestroyAllHandlers();
     442            stateCalibrator_->removeAndDestroyAllHandlers();
     443            stateDetector_->removeAndDestroyAllHandlers();
     444            _destroyState(stateEmpty_);
     445            _destroyState(stateCalibrator_);
     446            _destroyState(stateDetector_);
     447            stateEmpty_ = 0;
     448            stateCalibrator_ = 0;
     449            stateDetector_ = 0;
     450
     451            // we don't remove the other states yet because the singleton might still exist.
     452            // So people can still removeAndDestroy their states
     453            //inputStatesByName_.clear();
     454            //inputStatesByPriority_.clear();
     455
     456            // destroy the devices
    420457            _destroyKeyboard();
    421458            _destroyMouse();
    422459            _destroyJoySticks();
    423460
    424             activeHandlers_.clear();
    425 
    426             // inputSystem_ can never be 0, or else the code is mistaken
     461            _redimensionLists();
     462
    427463            OIS::InputManager::destroyInputSystem(inputSystem_);
    428464            inputSystem_ = 0;
    429465
    430             state_ = IS_UNINIT;
    431466            CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
    432467        }
     
    442477            inputSystem_->destroyInputObject(keyboard_);
    443478        keyboard_ = 0;
    444         activeKeyHandlers_.clear();
    445479        keysDown_.clear();
    446480        CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
     
    456490            inputSystem_->destroyInputObject(mouse_);
    457491        mouse_ = 0;
    458         activeMouseHandlers_.clear();
    459492        mouseButtonsDown_.clear();
    460493        CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
     
    475508
    476509            joySticks_.clear();
    477             joySticksSize_ = 0;
    478             activeJoyStickHandlers_.clear();
    479             joyStickButtonsDown_.clear();
    480             povStates_.clear();
    481             sliderStates_.clear();
    482             joySticksCalibration_.clear();
    483510        }
    484511        CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
    485512    }
    486513
    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.
     514    void InputManager::_destroyState(InputState* state)
     515    {
     516        assert(state);
     517        inputStatesByPriority_.erase(state->getPriority());
     518        inputStatesByName_.erase(state->getName());
     519        delete state;
     520    }
     521
     522
     523    // ############################################################
     524    // #####                  Runtime Methods                 #####
     525    // ##########                                        ##########
     526    // ############################################################
     527
     528    /**
     529    @brief
     530        Updates the InputManager. Tick is called by the Core class.
    531531    @param dt
    532532        Delta time
     
    534534    void InputManager::_tick(float dt)
    535535    {
    536         if (state_ == IS_UNINIT)
     536        if (inputSystem_ == 0)
    537537            return;
    538538
    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             }
     539        // check for states to leave (don't use unsigned int!)
     540        for (int i = stateLeaveRequests_.size() - 1; i >= 0; --i)
     541        {
     542            stateLeaveRequests_[i]->onLeave();
     543            // just to be sure that the state actually is registered
     544            assert(inputStatesByName_.find(stateLeaveRequests_[i]->getName()) != inputStatesByName_.end());
     545           
     546            activeStates_.erase(stateLeaveRequests_[i]->getPriority());
     547            _updateActiveStates();
     548            stateLeaveRequests_.pop_back();
     549        }
     550
     551
     552        // check for states to enter (don't use unsigned int!)
     553        for (int i = stateEnterRequests_.size() - 1; i >= 0; --i)
     554        {
     555            // just to be sure that the state actually is registered
     556            assert(inputStatesByName_.find(stateEnterRequests_[i]->getName()) != inputStatesByName_.end());
     557           
     558            activeStates_[stateEnterRequests_[i]->getPriority()] = stateEnterRequests_[i];
     559            _updateActiveStates();
     560            stateEnterRequests_[i]->onEnter();
     561            stateEnterRequests_.pop_back();
    652562        }
    653563
    654564        // Capture all the input. This calls the event handlers in InputManager.
     565        if (keyboard_)
     566            keyboard_->capture();
    655567        if (mouse_)
    656568            mouse_->capture();
    657         if (keyboard_)
    658             keyboard_->capture();
    659569        for (unsigned  int i = 0; i < joySticksSize_; i++)
    660570            joySticks_[i]->capture();
    661571
    662         if (state_ != IS_CALIBRATE)
     572        if (!bCalibrating_)
    663573        {
    664574            // call all the handlers for the held key events
    665575            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_));
     576                activeStatesTop_[Keyboard]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_));
    668577
    669578            // call all the handlers for the held mouse button events
    670579            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]);
     580                activeStatesTop_[Mouse]->mouseButtonHeld(mouseButtonsDown_[iButton]);
    673581
    674582            // call all the handlers for the held joy stick button events
    675583            for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
    676584                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);
     585                    activeStatesTop_[JoyStick0 + iJoyStick]
     586                        ->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
     587
     588            // tick the handlers for each active handler
     589            for (unsigned int i = 0; i < devicesNum_; ++i)
     590                activeStatesTop_[i]->tickInput(dt, i);
     591
     592            // tick the handler with a general tick afterwards
     593            for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i)
     594                activeStatesTicked_[i]->tickInput(dt);
     595        }
     596    }
     597
     598    void InputManager::_updateActiveStates()
     599    {
     600        for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it)
     601            for (unsigned int i = 0; i < devicesNum_; ++i)
     602                if ((*it).second->isInputDeviceEnabled(i))
     603                    activeStatesTop_[i] = (*it).second;
     604
     605        // update tickables (every state will only appear once)
     606        // Using a std::set to avoid duplicates
     607        std::set<InputState*> tempSet;
     608        for (unsigned int i = 0; i < devicesNum_; ++i)
     609            tempSet.insert(activeStatesTop_[i]);
     610
     611        // copy the content of the set back to the actual vector
     612        activeStatesTicked_.clear();
     613        for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it)
     614            activeStatesTicked_.push_back(*it);
    687615    }
    688616
     
    744672            cont->set(i, joySticksCalibration_[0].zeroStates[i]);
    745673        }
    746     }
     674
     675        // restore old input state
     676        requestLeaveState("calibrator");
     677    }
     678
     679
     680    // ############################################################
     681    // #####                    OIS events                    #####
     682    // ##########                                        ##########
     683    // ############################################################
    747684
    748685    // ###### Key Events ######
     
    771708            keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
    772709
    773         for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
    774             activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
     710        activeStatesTop_[Keyboard]->keyPressed(KeyEvent(e, keyboardModifiers_));
    775711
    776712        return true;
     
    803739            keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
    804740
    805         for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
    806             activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
     741        activeStatesTop_[Keyboard]->keyReleased(KeyEvent(e, keyboardModifiers_));
    807742
    808743        return true;
     
    823758        if (e.state.X.rel != 0 || e.state.Y.rel != 0)
    824759        {
    825             for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
    826             {
    827                 activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs),
     760            activeStatesTop_[Mouse]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs),
    828761                    IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height));
    829             }
    830762        }
    831763
     
    833765        if (e.state.Z.rel != 0)
    834766        {
    835             for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
    836                 activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
     767            activeStatesTop_[Mouse]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
    837768        }
    838769
     
    857788            mouseButtonsDown_.push_back((MouseButton::Enum)id);
    858789
    859         for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
    860             activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id);
     790        activeStatesTop_[Mouse]->mouseButtonPressed((MouseButton::Enum)id);
    861791
    862792        return true;
     
    883813        }
    884814
    885         for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
    886             activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id);
     815        activeStatesTop_[Mouse]->mouseButtonReleased((MouseButton::Enum)id);
    887816
    888817        return true;
     
    898827        unsigned int iJoyStick = 0;
    899828        while (joySticks_[iJoyStick] != joyStick)
    900         {
    901829            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         }
     830        // assert: Unknown joystick fired an event.
     831        assert(iJoyStick != joySticksSize_);
    908832        return iJoyStick;
    909833    }
     
    921845            buttonsDown.push_back(button);
    922846
    923         for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
    924             activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button);
     847        activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, button);
    925848
    926849        return true;
     
    942865        }
    943866
    944         for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
    945             activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button);
     867        activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, button);
    946868
    947869        return true;
     
    950872    void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value)
    951873    {
    952         if (state_ == IS_CALIBRATE)
     874        if (bCalibrating_)
    953875        {
    954876            if (value > marginalsMax_[axis])
     
    965887                fValue *= joySticksCalibration_[iJoyStick].negativeCoeff[axis];
    966888
    967             for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
    968                 activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis, fValue);
     889            activeStatesTop_[2 + iJoyStick]->joyStickAxisMoved(iJoyStick, axis, fValue);
    969890        }
    970891    }
     
    972893    bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
    973894    {
    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 
    977895        unsigned int iJoyStick = _getJoystick(arg);
    978896
     
    985903    bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
    986904    {
    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 
    991905        unsigned int iJoyStick = _getJoystick(arg);
    992906
     
    1029943    }
    1030944
    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     // ################################
     945
     946    // ############################################################
     947    // #####            Static Interface Methods              #####
     948    // ##########                                        ##########
     949    // ############################################################
    1049950
    1050951    std::string InputManager::bindingCommmandString_s = "";
     
    1053954        bool createKeyboard, bool createMouse, bool createJoySticks)
    1054955    {
    1055         return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
     956        return _getInstance()._initialise(windowHnd, windowWidth, windowHeight,
    1056957            createKeyboard, createMouse, createJoySticks);
    1057958    }
     
    1059960    bool InputManager::initialiseKeyboard()
    1060961    {
    1061         return _getSingleton()._initialiseKeyboard();
     962        return _getInstance()._initialiseKeyboard();
    1062963    }
    1063964
    1064965    bool InputManager::initialiseMouse()
    1065966    {
    1066         return _getSingleton()._initialiseMouse();
     967        return _getInstance()._initialiseMouse();
    1067968    }
    1068969
    1069970    bool InputManager::initialiseJoySticks()
    1070971    {
    1071         return _getSingleton()._initialiseJoySticks();
     972        return _getInstance()._initialiseJoySticks();
    1072973    }
    1073974
    1074975    int InputManager::numberOfKeyboards()
    1075976    {
    1076         if (_getSingleton().keyboard_ != 0)
     977        if (_getInstance().keyboard_ != 0)
    1077978            return 1;
    1078979        else
     
    1082983    int InputManager::numberOfMice()
    1083984    {
    1084         if (_getSingleton().mouse_ != 0)
     985        if (_getInstance().mouse_ != 0)
    1085986            return 1;
    1086987        else
     
    1090991    int InputManager::numberOfJoySticks()
    1091992    {
    1092         return _getSingleton().joySticksSize_;
     993        return _getInstance().joySticksSize_;
    1093994    }
    1094995
    1095996    /*bool InputManager::isKeyDown(KeyCode::Enum key)
    1096997    {
    1097     if (_getSingleton().keyboard_)
    1098         return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
     998    if (_getInstance().keyboard_)
     999        return _getInstance().keyboard_->isKeyDown((OIS::KeyCode)key);
    10991000    else
    11001001        return false;
     
    11031004    /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
    11041005    {
    1105     if (_getSingleton().keyboard_)
     1006    if (_getInstance().keyboard_)
    11061007        return isModifierDown(modifier);
    11071008    else
     
    11111012    /*const MouseState InputManager::getMouseState()
    11121013    {
    1113     if (_getSingleton().mouse_)
    1114         return _getSingleton().mouse_->getMouseState();
     1014    if (_getInstance().mouse_)
     1015        return _getInstance().mouse_->getMouseState();
    11151016    else
    11161017        return MouseState();
     
    11191020    /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
    11201021    {
    1121     if (ID < _getSingleton().joySticksSize_)
    1122         return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
     1022    if (ID < _getInstance().joySticksSize_)
     1023        return JoyStickState(_getInstance().joySticks_[ID]->getJoyStickState(), ID);
    11231024    else
    11241025        return JoyStickState();
     
    11271028    void InputManager::destroy()
    11281029    {
    1129         _getSingleton()._destroy();
     1030        _getInstance()._destroy();
    11301031    }
    11311032
    11321033    void InputManager::destroyKeyboard()
    11331034    {
    1134         return _getSingleton()._destroyKeyboard();
     1035        return _getInstance()._destroyKeyboard();
    11351036    }
    11361037
    11371038    void InputManager::destroyMouse()
    11381039    {
    1139         return _getSingleton()._destroyMouse();
     1040        return _getInstance()._destroyMouse();
    11401041    }
    11411042
    11421043    void InputManager::destroyJoySticks()
    11431044    {
    1144         return _getSingleton()._destroyJoySticks();
     1045        return _getInstance()._destroyJoySticks();
    11451046    }
    11461047
     
    11571058    void InputManager::setWindowExtents(const int width, const int height)
    11581059    {
    1159         if (_getSingleton().mouse_)
     1060        if (_getInstance().mouse_)
    11601061        {
    11611062            // Set mouse region (if window resizes, we should alter this to reflect as well)
    1162             const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
     1063            const OIS::MouseState &mouseState = _getInstance().mouse_->getMouseState();
    11631064            mouseState.width  = width;
    11641065            mouseState.height = height;
     
    11661067    }
    11671068
    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 
    11921069    void InputManager::storeKeyStroke(const std::string& name)
    11931070    {
    1194         setInputState(IS_NODETECT);
     1071        requestLeaveState("detector");
    11951072        COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl;
    11961073        CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false);
     
    12001077    {
    12011078        bindingCommmandString_s = command;
    1202         setInputState(IS_DETECT);
     1079        requestEnterState("detector");
    12031080        COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
    12041081    }
     
    12061083    void InputManager::calibrate()
    12071084    {
    1208         _getSingleton().setInputState(IS_CALIBRATE);
     1085        requestEnterState("calibrator");
    12091086    }
    12101087
    12111088    void InputManager::tick(float dt)
    12121089    {
    1213         _getSingleton()._tick(dt);
    1214     }
    1215 
    1216     // ###### KeyHandler ######
     1090        _getInstance()._tick(dt);
     1091    }
     1092
     1093    // ###### InputStates ######
    12171094
    12181095    /**
     
    12261103        True if added, false if name already existed.
    12271104    */
    1228     bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
    1229     {
    1230         if (!handler)
     1105    bool InputManager::_configureInputState(InputState* state, const std::string& name, int priority)
     1106    {
     1107        if (name == "")
    12311108            return false;
    1232         if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
    1233         {
    1234             _getSingleton().keyHandlers_[name] = handler;
    1235             return true;
     1109        if (_getInstance().inputStatesByName_.find(name) == _getInstance().inputStatesByName_.end())
     1110        {
     1111            if (_getInstance().inputStatesByPriority_.find(priority)
     1112                == _getInstance().inputStatesByPriority_.end())
     1113            {
     1114                _getInstance().inputStatesByName_[name] = state;
     1115                _getInstance().inputStatesByPriority_[priority] = state;
     1116                state->setNumOfJoySticks(numberOfJoySticks());
     1117                state->setName(name);
     1118                state->setPriority(priority);
     1119                return true;
     1120            }
     1121            else
     1122            {
     1123                COUT(2) << "Warning: Could not add an InputState with the same priority '"
     1124                    << priority << "'." << std::endl;
     1125                return false;
     1126            }
    12361127        }
    12371128        else
     1129        {
     1130            COUT(2) << "Warning: Could not add an InputState with the same name '" << name << "'." << std::endl;
    12381131            return false;
     1132        }
     1133    }
     1134
     1135    SimpleInputState* InputManager::createSimpleInputState(const std::string &name, int priority)
     1136    {
     1137        SimpleInputState* state = new SimpleInputState();
     1138        if (_getInstance()._configureInputState(state, name, priority))
     1139            return state;
     1140        else
     1141        {
     1142            delete state;
     1143            return 0;
     1144        }
     1145    }
     1146
     1147    ExtendedInputState* InputManager::createExtendedInputState(const std::string &name, int priority)
     1148    {
     1149        ExtendedInputState* state = new ExtendedInputState();
     1150        if (_getInstance()._configureInputState(state, name, priority))
     1151            return state;
     1152        else
     1153        {
     1154            delete state;
     1155            return 0;
     1156        }
    12391157    }
    12401158
     
    12471165        True if removal was successful, false if name was not found.
    12481166    */
    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);
     1167    bool InputManager::destroyState(const std::string& name)
     1168    {
     1169        if (name == "empty" || name == "calibrator" || name == "detector")
     1170        {
     1171            COUT(2) << "InputManager: Removing the '" << name << "' state is not allowed!" << std::endl;
     1172            return false;
     1173        }
     1174        std::map<std::string, InputState*>::iterator it = _getInstance().inputStatesByName_.find(name);
     1175        if (it != _getInstance().inputStatesByName_.end())
     1176        {
     1177            _getInstance()._destroyState((*it).second);
    12561178            return true;
    12571179        }
    1258         else
    1259             return false;
     1180        return false;
    12601181    }
    12611182
     
    12681189        Pointer to the instance, 0 if name was not found.
    12691190    */
    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())
     1191    InputState* InputManager::getState(const std::string& name)
     1192    {
     1193        std::map<std::string, InputState*>::iterator it = _getInstance().inputStatesByName_.find(name);
     1194        if (it != _getInstance().inputStatesByName_.end())
    12741195            return (*it).second;
    12751196        else
    12761197            return 0;
     1198    }
     1199
     1200    /**
     1201    @brief
     1202        Returns the current input handling method
     1203    @return
     1204        The current input mode.
     1205    */
     1206    InputState* InputManager::getCurrentState()
     1207    {
     1208        return (*_getInstance().activeStates_.rbegin()).second;
    12771209    }
    12781210
     
    12851217        False if name was not found, true otherwise.
    12861218    */
    1287     bool InputManager::enableKeyHandler(const std::string& name)
     1219    bool InputManager::requestEnterState(const std::string& name)
    12881220    {
    12891221        // 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)
     1222        std::map<std::string, InputState*>::const_iterator it = _getInstance().inputStatesByName_.find(name);
     1223        if (it != _getInstance().inputStatesByName_.end())
     1224        {
     1225            _getInstance().stateEnterRequests_.push_back((*it).second);
     1226            return true;
     1227        }
     1228        return false;
     1229    }
     1230
     1231    bool InputManager::requestLeaveState(const std::string& name)
    13171232    {
    13181233        // 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;
     1234        std::map<std::string, InputState*>::const_iterator it = _getInstance().inputStatesByName_.find(name);
     1235        if (it != _getInstance().inputStatesByName_.end())
     1236        {
     1237            _getInstance().stateLeaveRequests_.push_back((*it).second);
     1238            return true;
    13571239        }
    13581240        return false;
    13591241    }
    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 
    16741242}
Note: See TracChangeset for help on using the changeset viewer.