Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 8, 2009, 12:58:47 AM (16 years ago)
Author:
dafrick
Message:

Reverted to revision 2906 (because I'm too stupid to merge correctly, 2nd try will follow shortly. ;))

Location:
code/branches/questsystem5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/questsystem5

  • code/branches/questsystem5/src/core/input/InputManager.cc

    r2907 r2908  
    4343
    4444#include "util/Exception.h"
    45 #include "core/Clock.h"
    4645#include "core/CoreIncludes.h"
    4746#include "core/ConfigValueIncludes.h"
     
    109108        , internalState_(Uninitialised)
    110109        , stateEmpty_(0)
     110        , stateMaster_(0)
    111111        , keyDetector_(0)
    112112        , calibratorCallbackBuffer_(0)
     
    173173            //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
    174174            //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
    175 #if defined ORXONOX_PLATFORM_LINUX
     175#if defined OIS_LINUX_PLATFORM
    176176            paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
    177177            paramList.insert(std::make_pair(std::string("x11_mouse_grab"), "true"));
     
    218218
    219219            // Lowest priority empty InputState
    220             stateEmpty_ = createInputState<SimpleInputState>("empty", false, false, InputStatePriority::Empty);
     220            stateEmpty_ = createInputState<SimpleInputState>("empty", -1);
    221221            stateEmpty_->setHandler(&EMPTY_HANDLER);
    222222            activeStates_[stateEmpty_->getPriority()] = stateEmpty_;
    223223
     224            // Always active master InputState
     225            stateMaster_ = new ExtendedInputState();
     226            stateMaster_->setName("master");
     227            stateMaster_->setNumOfJoySticks(joySticksSize_);
     228
    224229            // KeyDetector to evaluate a pressed key's name
    225             SimpleInputState* detector = createInputState<SimpleInputState>("detector", false, false, InputStatePriority::Detector);
     230            SimpleInputState* detector = createInputState<SimpleInputState>("detector", 101);
    226231            keyDetector_ = new KeyDetector();
    227232            detector->setHandler(keyDetector_);
    228233
    229234            // Joy stick calibration helper callback
    230             SimpleInputState* calibrator = createInputState<SimpleInputState>("calibrator", false, false, InputStatePriority::Calibrator);
     235            SimpleInputState* calibrator = createInputState<SimpleInputState>("calibrator", 100);
    231236            calibrator->setHandler(&EMPTY_HANDLER);
    232237            calibratorCallbackBuffer_ = new InputBuffer();
     
    419424
    420425        // state management
    421         activeStatesTriggered_.resize(devicesNum_);
     426        activeStatesTop_.resize(devicesNum_);
    422427
    423428        // inform all states
    424         for (std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.begin();
    425             it != inputStatesByName_.end(); ++it)
     429        for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin();
     430            it != inputStatesByPriority_.end(); ++it)
    426431        {
    427432            it->second->setNumOfJoySticks(joySticksSize_);
    428433        }
     434        // inform master state
     435        if (stateMaster_)
     436            this->stateMaster_->setNumOfJoySticks(joySticksSize_);
    429437
    430438        // inform all JoyStick Device Number Listeners
     
    446454        }
    447455    }
    448 
     456   
    449457    void InputManager::_startCalibration()
    450458    {
     
    541549                // destroy the empty InputState
    542550                _destroyState(this->stateEmpty_);
     551                // destroy the master input state. This might trigger a memory leak
     552                // because the user has forgotten to destroy the KeyBinder or any Handler!
     553                delete stateMaster_;
    543554
    544555                // destroy all user InputStates
    545                 while (inputStatesByName_.size() > 0)
    546                     _destroyState((*inputStatesByName_.rbegin()).second);
     556                while (inputStatesByPriority_.size() > 0)
     557                    _destroyState((*inputStatesByPriority_.rbegin()).second);
    547558
    548559                // destroy the devices
     
    628639            _updateActiveStates();
    629640        }
     641        inputStatesByPriority_.erase(state->getPriority());
    630642        inputStatesByName_.erase(state->getName());
    631643        delete state;
     
    658670    @brief
    659671        Public interface. Only reloads immediately if the call stack doesn't
    660         include the update() method.
     672        include the tick() method.
    661673    @param joyStickSupport
    662674        Whether or not to initialise joy sticks as well.
     
    730742    @brief
    731743        Updates the states and the InputState situation.
    732     @param time
    733         Clock holding the current time.
    734     */
    735     void InputManager::update(const Clock& time)
     744    @param dt
     745        Delta time
     746    */
     747    void InputManager::tick(float dt)
    736748    {
    737749        if (internalState_ == Uninitialised)
     
    747759        if (!stateLeaveRequests_.empty())
    748760        {
    749             for (std::set<InputState*>::iterator it = stateLeaveRequests_.begin();
    750                 it != stateLeaveRequests_.end(); ++it)
    751             {
    752                 (*it)->onLeave();
     761            for (std::set<InputState*>::reverse_iterator rit = stateLeaveRequests_.rbegin();
     762                rit != stateLeaveRequests_.rend(); ++rit)
     763            {
     764                (*rit)->onLeave();
    753765                // just to be sure that the state actually is registered
    754                 assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
    755 
    756                 activeStates_.erase((*it)->getPriority());
    757                 if ((*it)->getPriority() < InputStatePriority::HighPriority)
    758                     (*it)->setPriority(0);
     766                assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end());
     767
     768                activeStates_.erase((*rit)->getPriority());
    759769                _updateActiveStates();
    760770            }
     
    765775        if (!stateEnterRequests_.empty())
    766776        {
    767             for (std::set<InputState*>::const_iterator it = stateEnterRequests_.begin();
    768                 it != stateEnterRequests_.end(); ++it)
     777            for (std::set<InputState*>::reverse_iterator rit = stateEnterRequests_.rbegin();
     778                rit != stateEnterRequests_.rend(); ++rit)
    769779            {
    770780                // just to be sure that the state actually is registered
    771                 assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
    772 
    773                 if ((*it)->getPriority() == 0)
    774                 {
    775                     // Get smallest possible priority between 1 and maxStateStackSize_s
    776 #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator
    777                     for(std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin();
    778                         rit != activeStates_.rend(); ++rit)
    779 #else
    780                     for(std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin();
    781                         rit != activeStates_.rend(); ++rit)
    782 #endif
    783                     {
    784                         if (rit->first < InputStatePriority::HighPriority)
    785                         {
    786                             (*it)->setPriority(rit->first + 1);
    787                             break;
    788                         }
    789                     }
    790                     // In case no normal handler was on the stack
    791                     if ((*it)->getPriority() == 0)
    792                         (*it)->setPriority(1);
    793                 }
    794                 activeStates_[(*it)->getPriority()] = (*it);
     781                assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end());
     782
     783                activeStates_[(*rit)->getPriority()] = (*rit);
    795784                _updateActiveStates();
    796                 (*it)->onEnter();
     785                (*rit)->onEnter();
    797786            }
    798787            stateEnterRequests_.clear();
     
    802791        if (!stateDestroyRequests_.empty())
    803792        {
    804             for (std::set<InputState*>::iterator it = stateDestroyRequests_.begin();
    805                 it != stateDestroyRequests_.end(); ++it)
    806             {
    807                 _destroyState((*it));
     793            for (std::set<InputState*>::reverse_iterator rit = stateDestroyRequests_.rbegin();
     794                rit != stateDestroyRequests_.rend(); ++rit)
     795            {
     796                _destroyState((*rit));
    808797            }
    809798            stateDestroyRequests_.clear();
     
    823812            _updateActiveStates();
    824813
    825         // mark that we now start capturing and distributing input
     814        // mark that we capture and distribute input
    826815        internalState_ |= Ticking;
    827816
     
    840829            {
    841830                KeyEvent kEvt(keysDown_[iKey], keyboardModifiers_);
    842 
    843                 for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState)
    844                     activeStatesTriggered_[Keyboard][iState]->keyHeld(kEvt);
     831                activeStatesTop_[Keyboard]->keyHeld(kEvt);
     832                stateMaster_->keyHeld(kEvt);
    845833            }
    846834
     
    848836            for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
    849837            {
    850                 for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState)
    851                     activeStatesTriggered_[Mouse][iState]->mouseButtonHeld(mouseButtonsDown_[iButton]);
     838                activeStatesTop_[Mouse]->mouseButtonHeld(mouseButtonsDown_[iButton]);
     839                stateMaster_->mouseButtonHeld(mouseButtonsDown_[iButton]);
    852840            }
    853841
     
    856844                for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
    857845                {
    858                     for (unsigned int iState = 0; iState < activeStatesTriggered_[JoyStick0 + iJoyStick].size(); ++iState)
    859                         activeStatesTriggered_[JoyStick0 + iJoyStick][iState]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
     846                    activeStatesTop_[JoyStick0 + iJoyStick]
     847                        ->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
     848                    stateMaster_->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
    860849                }
    861850
    862             // update the handlers for each active handler
     851            // tick the handlers for each active handler
    863852            for (unsigned int i = 0; i < devicesNum_; ++i)
    864853            {
    865                 for (unsigned int iState = 0; iState < activeStatesTriggered_[i].size(); ++iState)
    866                     activeStatesTriggered_[i][iState]->updateInput(time.getDeltaTime(), i);
    867             }
    868 
    869             // update the handler with a general tick afterwards
     854                activeStatesTop_[i]->tickInput(dt, i);
     855                if (stateMaster_->isInputDeviceEnabled(i))
     856                    stateMaster_->tickInput(dt, i);
     857            }
     858
     859            // tick the handler with a general tick afterwards
    870860            for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i)
    871                 activeStatesTicked_[i]->updateInput(time.getDeltaTime());
     861                activeStatesTicked_[i]->tickInput(dt);
     862            stateMaster_->tickInput(dt);
    872863        }
    873864
     
    878869    @brief
    879870        Updates the currently active states (according to activeStates_) for each device.
    880         Also, a list of all active states (no duplicates!) is compiled for the general update().
     871        Also, a list of all active states (no duplicates!) is compiled for the general tick.
    881872    */
    882873    void InputManager::_updateActiveStates()
    883874    {
    884         for (unsigned int i = 0; i < devicesNum_; ++i)
    885         {
    886             bool occupied = false;
    887             activeStatesTriggered_[i].clear();
    888 #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator
    889             for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit)
    890             {
    891 #else
    892             for (std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit)
    893             {
    894 #endif
    895                 if (rit->second->isInputDeviceEnabled(i) && (!occupied || rit->second->bAlwaysGetsInput_))
    896                 {
    897                     activeStatesTriggered_[i].push_back(rit->second);
    898                     if (!rit->second->bTransparent_)
    899                         occupied = true;
    900                 }
    901             }
    902         }
     875        for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it)
     876            for (unsigned int i = 0; i < devicesNum_; ++i)
     877                if (it->second->isInputDeviceEnabled(i))
     878                    activeStatesTop_[i] = it->second;
    903879
    904880        // update tickables (every state will only appear once)
     
    906882        std::set<InputState*> tempSet;
    907883        for (unsigned int i = 0; i < devicesNum_; ++i)
    908             for (unsigned int iState = 0; iState < activeStatesTriggered_[i].size(); ++iState)
    909                 tempSet.insert(activeStatesTriggered_[i][iState]);
    910 
    911         // copy the content of the std::set back to the actual vector
     884            tempSet.insert(activeStatesTop_[i]);
     885
     886        // copy the content of the set back to the actual vector
    912887        activeStatesTicked_.clear();
    913888        for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it)
     
    967942
    968943        KeyEvent kEvt(e, keyboardModifiers_);
    969         for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState)
    970             activeStatesTriggered_[Keyboard][iState]->keyPressed(kEvt);
     944        activeStatesTop_[Keyboard]->keyPressed(kEvt);
     945        stateMaster_->keyPressed(kEvt);
    971946
    972947        return true;
     
    1000975
    1001976        KeyEvent kEvt(e, keyboardModifiers_);
    1002         for (unsigned int iState = 0; iState < activeStatesTriggered_[Keyboard].size(); ++iState)
    1003             activeStatesTriggered_[Keyboard][iState]->keyReleased(kEvt);
     977        activeStatesTop_[Keyboard]->keyReleased(kEvt);
     978        stateMaster_->keyReleased(kEvt);
    1004979
    1005980        return true;
     
    1023998            IntVector2 rel(e.state.X.rel, e.state.Y.rel);
    1024999            IntVector2 clippingSize(e.state.width, e.state.height);
    1025             for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState)
    1026                 activeStatesTriggered_[Mouse][iState]->mouseMoved(abs, rel, clippingSize);
     1000            activeStatesTop_[Mouse]->mouseMoved(abs, rel, clippingSize);
     1001            stateMaster_->mouseMoved(abs, rel, clippingSize);
    10271002        }
    10281003
     
    10301005        if (e.state.Z.rel != 0)
    10311006        {
    1032             for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState)
    1033                 activeStatesTriggered_[Mouse][iState]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
     1007            activeStatesTop_[Mouse]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
     1008            stateMaster_->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
    10341009        }
    10351010
     
    10541029            mouseButtonsDown_.push_back((MouseButtonCode::ByEnum)id);
    10551030
    1056         for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState)
    1057             activeStatesTriggered_[Mouse][iState]->mouseButtonPressed((MouseButtonCode::ByEnum)id);
     1031        activeStatesTop_[Mouse]->mouseButtonPressed((MouseButtonCode::ByEnum)id);
     1032        stateMaster_->mouseButtonPressed((MouseButtonCode::ByEnum)id);
    10581033
    10591034        return true;
     
    10801055        }
    10811056
    1082         for (unsigned int iState = 0; iState < activeStatesTriggered_[Mouse].size(); ++iState)
    1083             activeStatesTriggered_[Mouse][iState]->mouseButtonReleased((MouseButtonCode::ByEnum)id);
     1057        activeStatesTop_[Mouse]->mouseButtonReleased((MouseButtonCode::ByEnum)id);
     1058        stateMaster_->mouseButtonReleased((MouseButtonCode::ByEnum)id);
    10841059
    10851060        return true;
     
    11171092            buttonsDown.push_back((JoyStickButtonCode::ByEnum)button);
    11181093
    1119         for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1120             activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button);
     1094        activeStatesTop_[2 + iJoyStick]->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button);
     1095        stateMaster_->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button);
    11211096
    11221097        return true;
     
    11381113        }
    11391114
    1140         for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1141             activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button);
     1115        activeStatesTop_[2 + iJoyStick]->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button);
     1116        stateMaster_->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button);
    11421117
    11431118        return true;
     
    11661141                fValue *= joyStickCalibrations_[iJoyStick].negativeCoeff[axis];
    11671142
    1168             for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1169                 activeStatesTriggered_[2 + iJoyStick][iState]->joyStickAxisMoved(iJoyStick, axis, fValue);
     1143            activeStatesTop_[2 + iJoyStick]->joyStickAxisMoved(iJoyStick, axis, fValue);
     1144            stateMaster_->joyStickAxisMoved(iJoyStick, axis, fValue);
    11701145        }
    11711146    }
     
    12701245        Unique name of the handler.
    12711246    @param priority
    1272         Determines which InputState gets the input. Higher is better.
    1273         Use 0 to handle it implicitely by the order of activation.
    1274         Otherwise numbers larger than maxStateStackSize_s have to be used!
     1247        Unique integer number. Higher means more prioritised.
    12751248    @return
    12761249        True if added, false if name or priority already existed.
    12771250    */
    1278     bool InputManager::_configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority)
     1251    bool InputManager::_configureInputState(InputState* state, const std::string& name, int priority)
    12791252    {
    12801253        if (name == "")
     
    12841257        if (inputStatesByName_.find(name) == inputStatesByName_.end())
    12851258        {
    1286             if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
    1287             {
    1288                 // Make sure we don't add two high priority states with the same priority
    1289                 for (std::map<std::string, InputState*>::const_iterator it = this->inputStatesByName_.begin();
    1290                     it != this->inputStatesByName_.end(); ++it)
    1291                 {
    1292                     if (it->second->getPriority() == priority)
    1293                     {
    1294                         COUT(2) << "Warning: Could not add an InputState with the same priority '"
    1295                             << priority << "' != 0." << std::endl;
    1296                         return false;
    1297                     }
    1298                 }
    1299             }
    1300             inputStatesByName_[name] = state;
    1301             state->setNumOfJoySticks(numberOfJoySticks());
    1302             state->setName(name);
    1303             state->bAlwaysGetsInput_ = bAlwaysGetsInput;
    1304             state->bTransparent_ = bTransparent;
    1305             if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
     1259            if (inputStatesByPriority_.find(priority)
     1260                == inputStatesByPriority_.end())
     1261            {
     1262                inputStatesByName_[name] = state;
     1263                inputStatesByPriority_[priority] = state;
     1264                state->setNumOfJoySticks(numberOfJoySticks());
     1265                state->setName(name);
    13061266                state->setPriority(priority);
    1307             return true;
     1267                return true;
     1268            }
     1269            else
     1270            {
     1271                COUT(2) << "Warning: Could not add an InputState with the same priority '"
     1272                    << priority << "'." << std::endl;
     1273                return false;
     1274            }
    13081275        }
    13091276        else
     
    13231290    @remarks
    13241291        You can't remove the internal states "empty", "calibrator" and "detector".
    1325         The removal process is being postponed if InputManager::update() is currently running.
     1292        The removal process is being postponed if InputManager::tick() is currently running.
    13261293    */
    13271294    bool InputManager::requestDestroyState(const std::string& name)
     
    14041371                {
    14051372                    // not scheduled for destruction
    1406                     // prevents a state being added multiple times
     1373                    // set prevents a state being added multiple times
    14071374                    stateEnterRequests_.insert(it->second);
    14081375                    return true;
     
    14231390    bool InputManager::requestLeaveState(const std::string& name)
    14241391    {
    1425         if (name == "empty")
    1426         {
    1427             COUT(2) << "InputManager: Leaving the empty state is not allowed!" << std::endl;
    1428             return false;
    1429         }
    14301392        // get pointer from the map with all stored handlers
    14311393        std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name);
Note: See TracChangeset for help on using the changeset viewer.