Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 6, 2009, 12:03:05 PM (15 years ago)
Author:
rgrieder
Message:

Extracted joy stick related code from InputManager to a new JoyStick class in order to make the InputManger less of a monster class and to apply a little bit more OO.

Location:
code/branches/core4/src/core
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core4/src/core/CorePrereqs.h

    r3257 r3270  
    179179    class InputManager;
    180180    class InputState;
     181    class JoyStick;
    181182    class JoyStickHandler;
    182183    class MouseHandler;
  • code/branches/core4/src/core/input/CMakeLists.txt

    r3196 r3270  
    66  InputCommands.cc
    77  InputManager.cc
     8  JoyStick.cc
    89  JoyStickDeviceNumberListener.cc
    910  KeyBinder.cc
  • code/branches/core4/src/core/input/InputManager.cc

    r3255 r3270  
    4040#include <ois/OISException.h>
    4141#include <ois/OISInputManager.h>
     42#include <boost/foreach.hpp>
    4243
    4344#include "util/Convert.h"
     
    5657#include "ExtendedInputState.h"
    5758#include "JoyStickDeviceNumberListener.h"
     59#include "JoyStick.h"
    5860
    5961// HACK (include this as last, X11 seems to define some macros...)
     
    106108        and registers the class in the class hierarchy.
    107109    */
    108     InputManager::InputManager()
     110    InputManager::InputManager(size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight)
    109111        : inputSystem_(0)
    110112        , keyboard_(0)
    111113        , mouse_(0)
    112         , joySticksSize_(0)
    113114        , devicesNum_(0)
    114115        , windowHnd_(0)
     
    125126
    126127        setConfigValues();
     128
     129        initialise(windowHnd, windowWidth, windowHeight);
    127130    }
    128131
     
    133136    void InputManager::setConfigValues()
    134137    {
    135         SetConfigValue(calibrationFilename_, "joystick_calibration.ini")
    136             .description("Ini filename for the the joy stick calibration data.")
    137             .callback(this, &InputManager::_calibrationFileCallback);
    138     }
    139 
    140     /**
    141     @brief
    142         Callback for the joy stick calibration config file. @see setConfigValues.
    143     */
    144     void InputManager::_calibrationFileCallback()
    145     {
    146         ConfigFileManager::getInstance().setFilename(ConfigFileType::JoyStickCalibration, calibrationFilename_);
    147138    }
    148139
     
    157148    @param windowHeight
    158149        The height of the render window
    159     @param joyStickSupport
    160         Whether or not to load the joy sticks as well
    161     */
    162     void InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport)
     150    */
     151    void InputManager::initialise(size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight)
    163152    {
    164153        CCOUT(3) << "Initialising Input System..." << std::endl;
     
    211200            }
    212201
    213             _initialiseMouse();
    214 
    215             if (joyStickSupport)
    216                 _initialiseJoySticks();
    217             // Do this anyway to also inform when a joystick was detached.
    218             _configureJoySticks();
    219 
    220             // Set mouse/joystick region
    221             if (mouse_)
    222                 setWindowExtents(windowWidth, windowHeight);
     202            _initialiseMouse(windowWidth, windowHeight);
     203
     204            _initialiseJoySticks();
    223205
    224206            // clear all buffers
    225             _clearBuffers();
     207            clearBuffers();
    226208
    227209            internalState_ |= OISReady;
     
    298280        False if mouse stays uninitialised, true otherwise.
    299281    */
    300     void InputManager::_initialiseMouse()
     282    void InputManager::_initialiseMouse(unsigned int windowWidth, unsigned int windowHeight)
    301283    {
    302284        if (mouse_ != 0)
     
    313295                mouse_->setEventCallback(this);
    314296                CCOUT(ORX_DEBUG) << "Created OIS mouse" << std::endl;
     297
     298                // Set mouse region
     299                setWindowExtents(windowWidth, windowHeight);
    315300            }
    316301            else
     
    335320    void InputManager::_initialiseJoySticks()
    336321    {
    337         if (joySticksSize_ > 0)
     322        if (!this->joySticks_.empty())
    338323        {
    339324            CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
    340325            return;
    341326        }
    342         if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
    343         {
    344             for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
    345             {
    346                 try
    347                 {
    348                     OIS::JoyStick* stig = static_cast<OIS::JoyStick*>
    349                         (inputSystem_->createInputObject(OIS::OISJoyStick, true));
    350                     CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
    351                     joySticks_.push_back(stig);
    352                     // register our listener in OIS.
    353                     stig->setEventCallback(this);
    354                 }
    355                 catch (OIS::Exception ex)
    356                 {
    357                     CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
    358                         << "OIS error message: \"" << ex.eText << "\"" << std::endl;
    359                 }
    360             }
    361         }
    362     }
    363 
    364     /**
    365     @brief
    366         Helper function that loads the config value vector of one coefficient
    367     */
    368     void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue)
    369     {
    370         list.resize(size);
    371         unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName);
    372         if (configValueVectorSize > size)
    373             configValueVectorSize = size;
    374 
    375         for (unsigned int i = 0; i < configValueVectorSize; ++i)
    376         {
    377             list[i] = multi_cast<int>(ConfigFileManager::getInstance().getValue(
    378                 ConfigFileType::JoyStickCalibration, sectionName, valueName, i, multi_cast<std::string>(defaultValue), false));
    379         }
    380 
    381         // fill the rest with default values
    382         for (unsigned int i = configValueVectorSize; i < size; ++i)
    383         {
    384             list[i] = defaultValue;
    385         }
    386     }
    387 
    388     /**
    389     @brief
    390         Sets the size of all the different lists that are dependent on the number
    391         of joy stick devices created and loads the joy stick calibration.
    392     @remarks
    393         No matter whether there are a mouse and/or keyboard, they will always
    394         occupy 2 places in the device number dependent lists.
    395     */
    396     void InputManager::_configureJoySticks()
    397     {
    398         joySticksSize_ = joySticks_.size();
    399         devicesNum_    = 2 + joySticksSize_;
    400         joyStickIDs_         .resize(joySticksSize_);
    401         joyStickButtonsDown_ .resize(joySticksSize_);
    402         povStates_           .resize(joySticksSize_);
    403         sliderStates_        .resize(joySticksSize_);
    404         joyStickMinValues_   .resize(joySticksSize_);
    405         joyStickMaxValues_   .resize(joySticksSize_);
    406         joyStickMiddleValues_.resize(joySticksSize_);
    407         joyStickCalibrations_.resize(joySticksSize_);
    408 
    409         for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++)
    410         {
    411             // Generate some sort of execution unique id per joy stick
    412             std::string id = "JoyStick_";
    413             id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Button))  + "_";
    414             id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Axis))    + "_";
    415             id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Slider))  + "_";
    416             id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_POV))     + "_";
    417             id += multi_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Vector3)) + "_";
    418             id += joySticks_[iJoyStick]->vendor();
    419             for (unsigned int i = 0; i < iJoyStick; ++i)
    420             {
    421                 if (id == joyStickIDs_[i])
    422                 {
    423                     // Two joysticks are probably equal --> add the index as well
    424                     id += "_" + multi_cast<std::string>(iJoyStick);
    425                 }
    426             }
    427             joyStickIDs_[iJoyStick] = id;
    428 
    429             size_t axes = sliderAxes + (size_t)this->joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Axis);
    430             loadCalibration(joyStickMinValues_[iJoyStick], id, "MinValue", axes, -32768);
    431             loadCalibration(joyStickMaxValues_[iJoyStick], id, "MaxValue", axes,  32768);
    432             loadCalibration(joyStickMiddleValues_[iJoyStick], id, "MiddleValue", axes,      0);
    433         }
    434 
    435         _evaluateCalibration();
    436 
     327
     328        devicesNum_ = 2 + inputSystem_->getNumberOfDevices(OIS::OISJoyStick);
    437329        // state management
    438330        activeStatesTriggered_.resize(devicesNum_);
    439331
    440         // inform all states
    441         for (std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.begin();
    442             it != inputStatesByName_.end(); ++it)
    443         {
    444             it->second->setNumOfJoySticks(joySticksSize_);
     332        for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
     333        {
     334            try
     335            {
     336                joySticks_.push_back(new JoyStick(activeStatesTriggered_[2 + i], i));
     337            }
     338            catch (std::exception ex)
     339            {
     340                CCOUT(2) << "Warning: Failed to create joy stick: " << ex.what() << std::endl;
     341            }
    445342        }
    446343
    447344        // inform all JoyStick Device Number Listeners
    448345        for (ObjectList<JoyStickDeviceNumberListener>::iterator it = ObjectList<JoyStickDeviceNumberListener>::begin(); it; ++it)
    449             it->JoyStickDeviceNumberChanged(joySticksSize_);
    450 
    451     }
    452 
    453     void InputManager::_evaluateCalibration()
    454     {
    455         for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick)
    456         {
    457             for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); i++)
    458             {
    459                 this->joyStickCalibrations_[iJoyStick].middleValue[i] = this->joyStickMiddleValues_[iJoyStick][i];
    460                 this->joyStickCalibrations_[iJoyStick].negativeCoeff[i] = - 1.0f / (this->joyStickMinValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]);
    461                 this->joyStickCalibrations_[iJoyStick].positiveCoeff[i] =   1.0f / (this->joyStickMaxValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]);
    462             }
    463         }
     346            it->JoyStickDeviceNumberChanged(joySticks_.size());
    464347    }
    465348
    466349    void InputManager::_startCalibration()
    467350    {
    468         for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick)
    469         {
    470             // Set initial values
    471             for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); ++i)
    472                 this->joyStickMinValues_[iJoyStick][i] = INT_MAX;
    473             for (unsigned int i = 0; i < this->joyStickMaxValues_[iJoyStick].size(); ++i)
    474                 this->joyStickMaxValues_[iJoyStick][i] = INT_MIN;
    475             for (unsigned int i = 0; i < this->joyStickMiddleValues_[iJoyStick].size(); ++i)
    476                 this->joyStickMiddleValues_[iJoyStick][i] = 0;
    477         }
     351        BOOST_FOREACH(JoyStick* stick, joySticks_)
     352            stick->startCalibration();
    478353
    479354        getInstance().internalState_ |= Calibrating;
     
    483358    void InputManager::_completeCalibration()
    484359    {
    485         for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick)
    486         {
    487             // Get the middle positions now
    488             unsigned int iAxis = 0;
    489             for (unsigned int i = 0; i < sliderAxes/2; ++i)
    490             {
    491                 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abX;
    492                 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abY;
    493             }
    494             // Note: joyStickMiddleValues_[iJoyStick] was already correctly resized in _configureJoySticks()
    495             assert(joySticks_[iJoyStick]->getJoyStickState().mAxes.size() == joyStickMiddleValues_[iJoyStick].size() - sliderAxes);
    496             for (unsigned int i = 0; i < joyStickMiddleValues_[iJoyStick].size() - sliderAxes; ++i)
    497             {
    498                 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mAxes[i].abs;
    499             }
    500 
    501             for (unsigned int i = 0; i < joyStickMinValues_[iJoyStick].size(); ++i)
    502             {
    503                 // Minimum values
    504                 if (joyStickMinValues_[iJoyStick][i] == INT_MAX)
    505                     joyStickMinValues_[iJoyStick][i] = -32768;
    506                 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
    507                     this->joyStickIDs_[iJoyStick], "MinValue", i, multi_cast<std::string>(joyStickMinValues_[iJoyStick][i]), false);
    508 
    509                 // Maximum values
    510                 if (joyStickMaxValues_[iJoyStick][i] == INT_MIN)
    511                     joyStickMaxValues_[iJoyStick][i] = 32767;
    512                 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
    513                     this->joyStickIDs_[iJoyStick], "MaxValue", i, multi_cast<std::string>(joyStickMaxValues_[iJoyStick][i]), false);
    514 
    515                 // Middle values
    516                 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration,
    517                     this->joyStickIDs_[iJoyStick], "MiddleValue", i, multi_cast<std::string>(joyStickMiddleValues_[iJoyStick][i]), false);
    518             }
    519         }
    520 
    521         _evaluateCalibration();
     360        BOOST_FOREACH(JoyStick* stick, joySticks_)
     361            stick->stopCalibration();
    522362
    523363        // restore old input state
     
    625465    void InputManager::_destroyJoySticks()
    626466    {
    627         if (joySticksSize_ > 0)
    628         {
    629             assert(inputSystem_);
    630             for (unsigned int i = 0; i < joySticksSize_; i++)
    631             {
    632                 try
    633                 {
    634                     if (joySticks_[i] != 0)
    635                         inputSystem_->destroyInputObject(joySticks_[i]);
    636                 }
    637                 catch (...)
    638                 {
    639                     CCOUT(1) << "Joy stick destruction failed! Potential resource leak!" << std::endl;
    640                 }
    641             }
    642 
    643             joySticks_.clear();
    644             // don't use _configureNumberOfJoySticks(), might mess with registered handler if
    645             // downgrading from 2 to 1 joystick
    646             //_configureNumberOfJoySticks();
    647             joySticksSize_ = 0;
     467        assert(inputSystem_);
     468        while (!joySticks_.empty())
     469        {
     470            try
     471            {
     472                delete joySticks_.back();
     473            }
     474            catch (...)
     475            {
     476                CCOUT(1) << "Joy stick destruction failed! Potential resource leak!" << std::endl;
     477            }
     478            joySticks_.pop_back();
     479            devicesNum_ = 2;
    648480        }
    649481        CCOUT(4) << "Joy sticks destroyed." << std::endl;
     
    669501    }
    670502
    671     void InputManager::_clearBuffers()
    672     {
    673         keysDown_.clear();
    674         keyboardModifiers_ = 0;
    675         mouseButtonsDown_.clear();
    676         for (unsigned int i = 0; i < joySticksSize_; ++i)
    677         {
    678             joyStickButtonsDown_[i].clear();
    679             for (int j = 0; j < 4; ++j)
    680             {
    681                 sliderStates_[i].sliderStates[j].x = 0;
    682                 sliderStates_[i].sliderStates[j].y = 0;
    683                 povStates_[i][j] = 0;
    684             }
    685         }
    686     }
    687 
    688 
    689503    // ############################################################
    690504    // #####                     Reloading                    #####
     
    696510        Public interface. Only reloads immediately if the call stack doesn't
    697511        include the update() method.
    698     @param joyStickSupport
    699         Whether or not to initialise joy sticks as well.
    700     */
    701     void InputManager::reloadInputSystem(bool joyStickSupport)
     512    */
     513    void InputManager::reloadInputSystem()
    702514    {
    703515        if (internalState_ & Ticking)
     
    707519            // include an OIS method. So it would be a very bad thing to destroy it..
    708520            internalState_ |= ReloadRequest;
    709             // Misuse of internalState_: We can easily store the joyStickSupport bool.
    710             // use Uninitialised as 0 value in order to make use of the overloaded |= operator
    711             internalState_ |= joyStickSupport ? JoyStickSupport : Uninitialised;
    712521        }
    713522        else if (internalState_ & OISReady)
    714         {
    715             _reload(joyStickSupport);
    716         }
     523            _reload();
    717524        else
    718525        {
     
    726533        Internal reload method. Destroys the OIS devices and loads them again.
    727534    */
    728     void InputManager::_reload(bool joyStickSupport)
     535    void InputManager::_reload()
    729536    {
    730537        try
     
    747554
    748555            // clear all buffers containing input information
    749             _clearBuffers();
    750 
    751             initialise(windowHnd_, mouseWidth, mouseHeight, joyStickSupport);
     556            clearBuffers();
     557
     558            initialise(windowHnd_, mouseWidth, mouseHeight);
    752559
    753560            CCOUT(3) << "Reloading done." << std::endl;
     
    776583        else if (internalState_ & ReloadRequest)
    777584        {
    778             _reload(internalState_ & JoyStickSupport);
     585            _reload();
    779586            internalState_ &= ~ReloadRequest;
    780             internalState_ &= ~JoyStickSupport;
    781587        }
    782588
     
    811617                {
    812618                    // Get smallest possible priority between 1 and maxStateStackSize_s
    813 #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator
    814619                    for(std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin();
    815620                        rit != activeStates_.rend(); ++rit)
    816 #else
    817                     for(std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin();
    818                         rit != activeStates_.rend(); ++rit)
    819 #endif
    820621                    {
    821622                        if (rit->first < InputStatePriority::HighPriority)
     
    868669        if (mouse_)
    869670            mouse_->capture();
    870         for (unsigned  int i = 0; i < joySticksSize_; i++)
    871             joySticks_[i]->capture();
     671        BOOST_FOREACH(JoyStick* stick, joySticks_)
     672            stick->capture();
    872673
    873674        if (!(internalState_ & Calibrating))
     
    888689                    activeStatesTriggered_[Mouse][iState]->mouseButtonHeld(mouseButtonsDown_[iButton]);
    889690            }
    890 
    891             // call all the handlers for the held joy stick button events
    892             for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
    893                 for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
    894                 {
    895                     for (unsigned int iState = 0; iState < activeStatesTriggered_[JoyStick0 + iJoyStick].size(); ++iState)
    896                         activeStatesTriggered_[JoyStick0 + iJoyStick][iState]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
    897                 }
    898691
    899692            // update the handlers for each active handler
     
    923716            bool occupied = false;
    924717            activeStatesTriggered_[i].clear();
    925 #if defined( __MINGW32__ ) // Avoid the strange mingw-stl bug with const_reverse_iterator
    926             for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit)
    927             {
    928 #else
    929718            for (std::map<int, InputState*>::const_reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit)
    930719            {
    931 #endif
    932720                if (rit->second->isInputDeviceEnabled(i) && (!occupied || rit->second->bAlwaysGetsInput_))
    933721                {
     
    960748    void InputManager::clearBuffers()
    961749    {
    962         this->keysDown_.clear();
    963         this->mouseButtonsDown_.clear();
    964         for (unsigned int i = 0; i < this->joySticksSize_; ++i)
    965             this->joyStickButtonsDown_[i].clear();
     750        keysDown_.clear();
     751        keyboardModifiers_ = 0;
     752        mouseButtonsDown_.clear();
     753        BOOST_FOREACH(JoyStick* stick, joySticks_)
     754            stick->clearBuffer();
    966755    }
    967756
     
    1124913
    1125914
    1126     // ###### Joy Stick Events ######
    1127 
    1128     /**
    1129     @brief
    1130         Returns the joy stick ID (orxonox) according to a OIS::JoyStickEvent
    1131     */
    1132     inline unsigned int InputManager::_getJoystick(const OIS::JoyStickEvent& arg)
    1133     {
    1134         // use the device to identify which one called the method
    1135         OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
    1136         unsigned int iJoyStick = 0;
    1137         while (joySticks_[iJoyStick] != joyStick)
    1138             iJoyStick++;
    1139         // assert: Unknown joystick fired an event.
    1140         assert(iJoyStick != joySticksSize_);
    1141         return iJoyStick;
    1142     }
    1143 
    1144     bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
    1145     {
    1146         unsigned int iJoyStick = _getJoystick(arg);
    1147 
    1148         // check whether the button already is in the list (can happen when focus was lost)
    1149         std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick];
    1150         unsigned int iButton = 0;
    1151         while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
    1152             iButton++;
    1153         if (iButton == buttonsDown.size())
    1154             buttonsDown.push_back((JoyStickButtonCode::ByEnum)button);
    1155 
    1156         for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1157             activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonPressed(iJoyStick, (JoyStickButtonCode::ByEnum)button);
    1158 
    1159         return true;
    1160     }
    1161 
    1162     bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
    1163     {
    1164         unsigned int iJoyStick = _getJoystick(arg);
    1165 
    1166         // remove the button from the joyStickButtonsDown_ list
    1167         std::vector<JoyStickButtonCode::ByEnum>& buttonsDown = joyStickButtonsDown_[iJoyStick];
    1168         for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
    1169         {
    1170             if (buttonsDown[iButton] == button)
    1171             {
    1172                 buttonsDown.erase(buttonsDown.begin() + iButton);
    1173                 break;
    1174             }
    1175         }
    1176 
    1177         for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1178             activeStatesTriggered_[2 + iJoyStick][iState]->joyStickButtonReleased(iJoyStick, (JoyStickButtonCode::ByEnum)button);
    1179 
    1180         return true;
    1181     }
    1182 
    1183     /**
    1184     @brief
    1185         Calls the states for a particular axis with our enumeration.
    1186         Used by OIS sliders and OIS axes.
    1187     */
    1188     void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value)
    1189     {
    1190         if (internalState_ & Calibrating)
    1191         {
    1192             if (value < joyStickMinValues_[iJoyStick][axis])
    1193                 joyStickMinValues_[iJoyStick][axis] = value;
    1194             if (value > joyStickMaxValues_[iJoyStick][axis])
    1195                 joyStickMaxValues_[iJoyStick][axis] = value;
    1196         }
    1197         else
    1198         {
    1199             float fValue = static_cast<float>(value - joyStickCalibrations_[iJoyStick].middleValue[axis]);
    1200             if (fValue > 0.0f)
    1201                 fValue *= joyStickCalibrations_[iJoyStick].positiveCoeff[axis];
    1202             else
    1203                 fValue *= joyStickCalibrations_[iJoyStick].negativeCoeff[axis];
    1204 
    1205             for (unsigned int iState = 0; iState < activeStatesTriggered_[2 + iJoyStick].size(); ++iState)
    1206                 activeStatesTriggered_[2 + iJoyStick][iState]->joyStickAxisMoved(iJoyStick, axis, fValue);
    1207         }
    1208     }
    1209 
    1210     bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
    1211     {
    1212         unsigned int iJoyStick = _getJoystick(arg);
    1213 
    1214         // keep in mind that the first 8 axes are reserved for the sliders
    1215         _fireAxis(iJoyStick, axis + sliderAxes, arg.state.mAxes[axis].abs);
    1216 
    1217         return true;
    1218     }
    1219 
    1220     bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
    1221     {
    1222         unsigned int iJoyStick = _getJoystick(arg);
    1223 
    1224         if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
    1225             _fireAxis(iJoyStick, id * 2, arg.state.mSliders[id].abX);
    1226         else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
    1227             _fireAxis(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
    1228 
    1229         return true;
    1230     }
    1231 
    1232     bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
    1233     {
    1234         unsigned int iJoyStick = _getJoystick(arg);
    1235 
    1236         // translate the POV into 8 simple buttons
    1237 
    1238         int lastState = povStates_[iJoyStick][id];
    1239         if (lastState & OIS::Pov::North)
    1240             buttonReleased(arg, 32 + id * 4 + 0);
    1241         if (lastState & OIS::Pov::South)
    1242             buttonReleased(arg, 32 + id * 4 + 1);
    1243         if (lastState & OIS::Pov::East)
    1244             buttonReleased(arg, 32 + id * 4 + 2);
    1245         if (lastState & OIS::Pov::West)
    1246             buttonReleased(arg, 32 + id * 4 + 3);
    1247 
    1248         povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
    1249 
    1250         int currentState = povStates_[iJoyStick][id];
    1251         if (currentState & OIS::Pov::North)
    1252             buttonPressed(arg, 32 + id * 4 + 0);
    1253         if (currentState & OIS::Pov::South)
    1254             buttonPressed(arg, 32 + id * 4 + 1);
    1255         if (currentState & OIS::Pov::East)
    1256             buttonPressed(arg, 32 + id * 4 + 2);
    1257         if (currentState & OIS::Pov::West)
    1258             buttonPressed(arg, 32 + id * 4 + 3);
    1259 
     915    // ############################################################
     916    // #####                Friend functions                  #####
     917    // ##########                                        ##########
     918    // ############################################################
     919
     920    /**
     921    @brief
     922        Checks whether there is already a joy stick with the given ID string.
     923    @return
     924        Returns true if ID is ok (unique), false otherwise.
     925    */
     926    bool InputManager::checkJoyStickID(const std::string& idString)
     927    {
     928        BOOST_FOREACH(JoyStick* stick, joySticks_)
     929        {
     930            if (stick->getIDString() == idString)
     931                return false;
     932        }
    1260933        return true;
    1261934    }
     
    13361009            }
    13371010            inputStatesByName_[name] = state;
    1338             state->setNumOfJoySticks(numberOfJoySticks());
     1011            state->JoyStickDeviceNumberChanged(numberOfJoySticks());
    13391012            state->setName(name);
    13401013            state->bAlwaysGetsInput_ = bAlwaysGetsInput;
     
    15021175        Reloads the input system
    15031176    */
    1504     void InputManager::reload(bool joyStickSupport)
    1505     {
    1506         getInstance().reloadInputSystem(joyStickSupport);
     1177    void InputManager::reload()
     1178    {
     1179        getInstance().reloadInputSystem();
    15071180    }
    15081181
  • code/branches/core4/src/core/input/InputManager.h

    r3196 r3270  
    5454namespace orxonox
    5555{
    56     /**
    57     @brief
    58         Helper class to realise a vector<int[4]>
    59     */
    60     class POVStates
    61     {
    62     public:
    63         int& operator[](unsigned int index) { return povStates[index]; }
    64         int povStates[4];
    65     };
    66 
    67     /**
    68     @brief
    69         Helper class to realise a vector< {int[4], int[4]} >
    70     */
    71     class SliderStates
    72     {
    73     public:
    74         IntVector2 sliderStates[4];
    75     };
    76 
    77     struct JoyStickCalibration
    78     {
    79         int middleValue[24];
    80         float positiveCoeff[24];
    81         float negativeCoeff[24];
    82     };
    83 
    8456    struct InputStatePriority : OrxEnum<InputStatePriority>
    8557    {
     
    10173    class _CoreExport InputManager
    10274        : public OrxonoxClass,
    103         public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
     75        public OIS::KeyListener, public OIS::MouseListener
    10476    {
    10577        // --> setConfigValues is private
    10678        friend class ClassIdentifier<InputManager>;
     79        friend class JoyStick;
    10780
    10881    public:
     
    11588            Calibrating      = 0x08,
    11689            ReloadRequest    = 0x10,
    117             JoyStickSupport  = 0x20 // used with ReloadRequest to store a bool
    11890        };
    11991
    120         InputManager ();
     92        InputManager (size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight);
    12193        ~InputManager();
    12294
    123         void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true);
    124 
    125         void reloadInputSystem(bool joyStickSupport = true);
     95        void initialise(size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight);
     96
     97        void reloadInputSystem();
    12698
    12799        void clearBuffers();
     
    129101        unsigned int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
    130102        unsigned int  numberOfMice()      { return mouse_    ? 1 : 0; }
    131         unsigned int  numberOfJoySticks() { return joySticksSize_; }
     103        unsigned int  numberOfJoySticks() { return joySticks_.size(); }
    132104
    133105        void setWindowExtents(const int width, const int height);
     
    156128        // console commands
    157129        static void calibrate();
    158         static void reload(bool joyStickSupport = true);
     130        static void reload();
    159131
    160132    public: // variables
    161133        static EmptyHandler                 EMPTY_HANDLER;
    162         static const unsigned int           sliderAxes = 8;
     134
     135    private: // functions for friends
     136        OIS::InputManager* getInputSystem() { return this->inputSystem_; }
     137        bool checkJoyStickID(const std::string&);
    163138
    164139    private: // functions
     
    168143        // Intenal methods
    169144        void _initialiseKeyboard();
    170         void _initialiseMouse();
     145        void _initialiseMouse(unsigned int windowWidth, unsigned int windowHeight);
    171146        void _initialiseJoySticks();
    172147        void _configureJoySticks();
     
    181156        void _destroyJoySticks();
    182157        void _destroyState(InputState* state);
    183         void _clearBuffers();
    184 
    185         void _reload(bool joyStickSupport);
     158
     159        void _reload();
    186160
    187161        void _fireAxis(unsigned int iJoyStick, int axis, int value);
     
    197171        bool keyPressed    (const OIS::KeyEvent      &arg);
    198172        bool keyReleased   (const OIS::KeyEvent      &arg);
    199         bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
    200         bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
    201         bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
    202         bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
    203         bool povMoved      (const OIS::JoyStickEvent &arg, int id);
    204         // don't remove that! Or else add OIS as dependency library to orxonox.
    205         bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
    206173
    207174        void setConfigValues();
     
    212179        OIS::Keyboard*                      keyboard_;             //!< OIS mouse
    213180        OIS::Mouse*                         mouse_;                //!< OIS keyboard
    214         std::vector<OIS::JoyStick*>         joySticks_;            //!< OIS joy sticks
    215         unsigned int                        joySticksSize_;
    216         std::vector<std::string>            joyStickIDs_;          //!< Execution unique identification strings for the joy sticks
     181        std::vector<JoyStick*>              joySticks_;            //!< Orxonox joy sticks
    217182        unsigned int                        devicesNum_;
    218183        size_t                              windowHnd_;            //!< Render window handle
     
    234199        std::vector<InputState*>            activeStatesTicked_;
    235200
    236         // joystick calibration
    237         std::vector<std::vector<int> >      joyStickMinValues_;
    238         std::vector<std::vector<int> >      joyStickMaxValues_;
    239         std::vector<std::vector<int> >      joyStickMiddleValues_;
    240         std::vector<ConfigValueContainer*>  calibrationConfigValueContainers_;
    241         std::vector<JoyStickCalibration>    joyStickCalibrations_;
    242 
    243201        unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers.
    244         std::vector<POVStates>              povStates_;            //!< Keeps track of the joy stick POV states.
    245         std::vector<SliderStates>           sliderStates_;         //!< Keeps track of the possibly two slider axes.
    246202
    247203        std::vector<Key>                    keysDown_;
    248         std::vector<MouseButtonCode::ByEnum>      mouseButtonsDown_;
    249         std::vector<std::vector<JoyStickButtonCode::ByEnum> >  joyStickButtonsDown_;
    250 
    251         // ConfigValues
    252         std::string                         calibrationFilename_;  //!< Joy stick calibration ini filename
     204        std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_;
    253205
    254206        static InputManager*                singletonRef_s;
  • code/branches/core4/src/core/input/InputState.h

    r3196 r3270  
    4040#include <vector>
    4141#include "InputInterfaces.h"
     42#include "JoyStickDeviceNumberListener.h"
    4243
    4344namespace orxonox
    4445{
    45     class _CoreExport InputState
     46    class _CoreExport InputState : public JoyStickDeviceNumberListener
    4647    {
    4748        friend class InputManager;
     
    111112
    112113    private:
    113         void setNumOfJoySticks(unsigned int n)
     114        void JoyStickDeviceNumberChanged(unsigned int n)
    114115        {
    115116            bInputDeviceEnabled_.resize(n + 2);
  • code/branches/core4/src/core/input/SimpleInputState.cc

    r3196 r3270  
    5959        }
    6060        update();
    61     }
    62 
    63     void SimpleInputState::keyPressed(const KeyEvent& evt)
    64     {
    65         if (keyHandler_)
    66             keyHandler_->keyPressed(evt);
    6761    }
    6862
  • code/branches/core4/src/core/input/SimpleInputState.h

    r3196 r3270  
    119119    }
    120120
     121    inline void SimpleInputState::keyPressed(const KeyEvent& evt)
     122    {
     123        if (keyHandler_)
     124            keyHandler_->keyPressed(evt);
     125    }
     126
    121127    inline void SimpleInputState::keyReleased(const KeyEvent& evt)
    122128    {
Note: See TracChangeset for help on using the changeset viewer.