Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 12, 2009, 4:12:04 PM (16 years ago)
Author:
rgrieder
Message:

Added a few more generic parts to the input library:

  • Created Mouse and Keyboard to join JoyStick and provided them with a templated base class (InputDeviceTemplated) that does most of the work (reduces quite some redundancy)
  • Created InputPrereqs.h from InputInterfaces.h and destroyed the latter
  • Exported InputHandler to its own file and replaced KeyHandler, MouseHandler and JoyStickHandler with the single InputHandler.
  • Deleted the SimpleInputState: There is only one class now which fulfills all our needs.

In general there is now less code and the code itself has more 'pluses'. However I haven't really thrown away any feature at all.

File:
1 edited

Legend:

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

    r3270 r3274  
    2626 *
    2727 */
    28 
    29 /**
    30 @file
    31 @brief
    32     Implementation of the JoyStick wrapper class.
    33 */
    3428
    3529#include "JoyStick.h"
     
    5448    void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue);
    5549
    56     JoyStick::JoyStick(const std::vector<InputState*>& states, unsigned int id)
    57         : id_(id)
    58         , bCalibrating_(false)
    59         , inputStates_(states)
     50    JoyStick::JoyStick(unsigned int id)
     51        : super(id)
    6052    {
    6153        RegisterRootObject(JoyStick);
    6254        this->setConfigValues();
    63 
    64         OIS::InputManager* system = InputManager::getInstance().getInputSystem();
    65         oisJoyStick_ = static_cast<OIS::JoyStick*>(system->createInputObject(OIS::OISJoyStick, true));
    66         oisJoyStick_->setEventCallback(this);
     55        // Initialise POV and Slider states
     56        this->clearBuffersImpl();
    6757
    6858        idString_ = "JoyStick_";
    69         std::string name = oisJoyStick_->vendor();
     59        std::string name = oisDevice_->vendor();
    7060        replaceCharacters(name, ' ', '_');
    7161        idString_ += name + "_";
    72         idString_ += multi_cast<std::string>(oisJoyStick_->getNumberOfComponents(OIS::OIS_Button))  + "_";
    73         idString_ += multi_cast<std::string>(oisJoyStick_->getNumberOfComponents(OIS::OIS_Axis))    + "_";
    74         idString_ += multi_cast<std::string>(oisJoyStick_->getNumberOfComponents(OIS::OIS_Slider))  + "_";
    75         idString_ += multi_cast<std::string>(oisJoyStick_->getNumberOfComponents(OIS::OIS_POV));
    76         //idString_ += multi_cast<std::string>(oisJoyStick_->getNumberOfComponents(OIS::OIS_Vector3));
     62        idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Button))  + "_";
     63        idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis))    + "_";
     64        idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Slider))  + "_";
     65        idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_POV));
     66        //idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3));
    7767
    7868        if (InputManager::getInstance().checkJoyStickID(idString_) == false)
    7969        {
    8070            // Make the ID unique for this execution time.
    81             idString_ += "_" + multi_cast<std::string>(id_);
     71            idString_ += "_" + multi_cast<std::string>(this->getDeviceID());
    8272        }
    8373
     
    8575
    8676        // Load calibration
    87         size_t axes = sliderAxes_s + static_cast<size_t>(oisJoyStick_->getNumberOfComponents(OIS::OIS_Axis));
     77        size_t axes = sliderAxes_s + static_cast<size_t>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis));
    8878        loadCalibration(configMinValues_,  idString_, "MinValue",  axes,  -32768);
    8979        loadCalibration(configMaxValues_,  idString_, "MaxValue",  axes,   32768);
     
    9282    }
    9383
    94     JoyStick::~JoyStick()
    95     {
    96         try
    97         {
    98             OIS::InputManager* system = InputManager::getInstance().getInputSystem();
    99             system->destroyInputObject(oisJoyStick_);
    100         }
    101         catch (...)
    102         {
    103             COUT(1) << "Joy stick destruction failed! Potential resource leak!" << std::endl;
    104         }
    105     }
    106 
    107     /**
    108     @brief
    109         Callback for the joy stick calibration config file. @see setConfigValues.
    110     */
     84    //!< Callback for the joy stick calibration config file.
    11185    void JoyStick::calibrationFileCallback()
    11286    {
     
    11488    }
    11589
    116     /**
    117     @brief
    118         Sets the configurable values.
    119     */
    12090    void JoyStick::setConfigValues()
    12191    {
     
    140110        // fill the rest with default values
    141111        for (unsigned int i = configValueVectorSize; i < size; ++i)
    142         {
    143112            list[i] = defaultValue;
    144         }
    145     }
    146 
    147     void JoyStick::startCalibration()
    148     {
    149         bCalibrating_ = true;
    150 
     113    }
     114
     115    //! Called by InputDevice when calibration mode has started
     116    void JoyStick::calibrationStarted()
     117    {
    151118        // Set initial values
    152119        BOOST_FOREACH(int& minVal, configMinValues_)
     
    158125    }
    159126
    160     void JoyStick::stopCalibration()
     127    //! Called by InputDevice when calibration mode has stopped
     128    void JoyStick::calibrationStopped()
    161129    {
    162130        // Get the middle positions now
     
    164132        for (unsigned int i = 0; i < sliderAxes_s/2; ++i)
    165133        {
    166             configZeroValues_[iAxis++] = oisJoyStick_->getJoyStickState().mSliders[i].abX;
    167             configZeroValues_[iAxis++] = oisJoyStick_->getJoyStickState().mSliders[i].abY;
    168         }
    169         // Note: joyStickMiddleValues_[iJoyStick] was already correctly resised in loadCalibration()
    170         assert(oisJoyStick_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s);
     134            configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abX;
     135            configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abY;
     136        }
     137        // Note: joyStickZeroValues_[iJoyStick] was already correctly resised in loadCalibration()
     138        assert(oisDevice_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s);
    171139        for (unsigned int i = 0; i < configZeroValues_.size() - sliderAxes_s; ++i)
    172             configZeroValues_[iAxis++] = oisJoyStick_->getJoyStickState().mAxes[i].abs;
     140            configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mAxes[i].abs;
    173141
    174142        for (unsigned int i = 0; i < configMinValues_.size(); ++i)
     
    192160
    193161        this->evaluateCalibration();
    194 
    195         bCalibrating_ = false;
    196     }
    197 
     162    }
     163
     164    //! Evaluates the accumulated values during calibration
    198165    void JoyStick::evaluateCalibration()
    199166    {
     
    206173    }
    207174
    208     void JoyStick::clearBuffer()
    209     {
    210         pressedButtons_.clear();
     175    // TODO: What do we really need to reset here?
     176    void JoyStick::clearBuffersImpl()
     177    {
    211178        for (int j = 0; j < 4; ++j)
    212179        {
     
    217184    }
    218185
    219 
    220     // ###### Events ######
    221 
    222     void JoyStick::capture()
    223     {
    224         oisJoyStick_->capture();
    225 
    226         // call all the handlers for the held joy stick button events
    227         for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++)
    228         {
    229             BOOST_FOREACH(InputState* state, inputStates_)
    230                 state->joyStickButtonHeld(id_, pressedButtons_[iButton]);
    231         }
    232     }
    233 
    234     bool JoyStick::buttonPressed(const OIS::JoyStickEvent &arg, int button)
    235     {
    236         // check whether the button already is in the list (can happen when focus was lost)
    237         unsigned int iButton = 0;
    238         while (iButton < pressedButtons_.size() && pressedButtons_[iButton] != button)
    239             iButton++;
    240         if (iButton == pressedButtons_.size())
    241             pressedButtons_.push_back(static_cast<JoyStickButtonCode::ByEnum>(button));
    242 
    243         BOOST_FOREACH(InputState* state, inputStates_)
    244             state->joyStickButtonPressed(id_, static_cast<JoyStickButtonCode::ByEnum>(button));
    245 
    246         return true;
    247     }
    248 
    249     bool JoyStick::buttonReleased(const OIS::JoyStickEvent &arg, int button)
    250     {
    251         // remove the button from the pressedButtons_ list
    252         for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++)
    253         {
    254             if (static_cast<int>(pressedButtons_[iButton]) == button)
    255             {
    256                 pressedButtons_.erase(pressedButtons_.begin() + iButton);
    257                 break;
    258             }
    259         }
    260 
    261         BOOST_FOREACH(InputState* state, inputStates_)
    262             state->joyStickButtonPressed(id_, static_cast<JoyStickButtonCode::ByEnum>(button));
    263 
    264         return true;
    265     }
    266 
    267     /**
    268     @brief
    269         Calls the states for a particular axis with our enumeration.
    270         Used by OIS sliders and OIS axes.
    271     */
     186    //! Generic method to forward axis events
    272187    void JoyStick::fireAxis(int axis, int value)
    273188    {
    274         if (bCalibrating_)
     189        if (this->isCalibrating())
    275190        {
    276191            if (value < configMinValues_[axis])
     
    288203
    289204            BOOST_FOREACH(InputState* state, inputStates_)
    290                 state->joyStickAxisMoved(id_, axis, fValue);
    291         }
    292     }
    293 
     205                state->joyStickAxisMoved(this->getDeviceID(), axis, fValue);
     206        }
     207    }
     208
     209    //! OIS joy stick axis event handler
    294210    bool JoyStick::axisMoved(const OIS::JoyStickEvent &arg, int axis)
    295211    {
     
    300216    }
    301217
     218    //! A Slider always has an X and an Y direction!
    302219    bool JoyStick::sliderMoved(const OIS::JoyStickEvent &arg, int id)
    303220    {
     
    310227    }
    311228
     229    //! A POV is the big button that can point in all directions (but only in one at once)
    312230    bool JoyStick::povMoved(const OIS::JoyStickEvent &arg, int id)
    313231    {
Note: See TracChangeset for help on using the changeset viewer.