Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3286 for code/branches


Ignore:
Timestamp:
Jul 13, 2009, 8:54:43 PM (15 years ago)
Author:
rgrieder
Message:

Added and adjusted documentation for the devices.
Also removed one last ugliness where the InputManager was called from within a device (which should not even know about the InputManager).

Location:
code/branches/core4/src/core/input
Files:
12 edited

Legend:

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

    r3285 r3286  
    2929/**
    3030@file
    31 @brief
     31@brief
     32    Implementation of InputDevice and InputDeviceTemplated
    3233*/
    3334
     
    3839
    3940#include <vector>
    40 #include <boost/foreach.hpp>
    4141#include <ois/OISInputManager.h>
    4242
    4343#include "util/Debug.h"
    4444#include "core/Clock.h"
    45 // TODO: Try to remove this
    46 #include "InputManager.h"
    4745#include "InputState.h"
    4846
     
    5149    /**
    5250    @brief
     51        Abstract base class for all input devices (mouse, keyboard and joy sticks).
     52
     53        It provides common virtual functions to be used by the InputManager.
    5354    */
    5455    class InputDevice
    5556    {
    56         friend class InputManager;
    57 
    5857    public:
    59         InputDevice(unsigned int id) : bCalibrating_(false), id_(id) { }
     58        //! Only resets the members
     59        InputDevice(unsigned int id) : bCalibrating_(false), deviceID_(id) { }
    6060        virtual ~InputDevice() { }
    61         virtual std::string getClassName() = 0;
     61        //! Returns the device class (derived) name as string
     62        virtual std::string getClassName() const = 0;
     63        //! Updates the device which should in turn distribute events
    6264        virtual void update(const Clock& time) = 0;
     65        //! Clear all button related buffers
    6366        virtual void clearBuffers() = 0;
    6467
     68        //! Start calibrating (only useful for joy sticks)
    6569        void startCalibration()
    6670        {
     
    6973        }
    7074
     75        //! Stop calibrating and evaluate the data (only useful for joy sticks)
    7176        void stopCalibration()
    7277        {
     
    7580        }
    7681
    77         unsigned int getDeviceID() { return this->id_; }
     82        //! Returns a reference to the internal input state vector. Use with care!
     83        std::vector<InputState*>& getStateListRef() { return this->inputStates_; }
     84        //! Returns the ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard)
     85        unsigned int getDeviceID() const { return this->deviceID_; }
     86        //! Tells whether the device is in calibration mode
     87        bool isCalibrating() const { return bCalibrating_; }
    7888
    7989    protected:
     90        //! To be ovrridden by the subclass
    8091        virtual void calibrationStarted() { }
     92        //! To be ovrridden by the subclass
    8193        virtual void calibrationStopped() { }
    82         bool isCalibrating() { return bCalibrating_; }
    83 
    84         // InputState handling
     94
     95        //! List of all input states that receive events from this device
    8596        std::vector<InputState*> inputStates_;
    8697
    8798    private:
    88         InputDevice(const InputDevice& rhs);
    89 
    90         std::vector<InputState*>& getStateListRef()
    91         {
    92             return this->inputStates_;
    93         }
    94 
    95         bool bCalibrating_;
    96         const unsigned int id_;
     99        InputDevice(const InputDevice& rhs); //!< Don't use!
     100
     101        bool bCalibrating_;                  //!< Whether the device is in calibration mode
     102        const unsigned int deviceID_;        //!< ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard)
    97103    };
    98104
    99105    /**
    100106    @brief
     107        Heavily templated base class for all three input devices.
     108
     109        The purpose of this class is not to provide an interface but rather
     110        to reduce code redundancy. This concerns device creation and destruction
     111        as well as common code for button events (press, release, hold).
     112
     113        In order to derive from this class you have to supply it with a struct
     114        as template parameter that contains the necessary type traits.
    101115    */
    102116    template <class Traits>
     
    110124
    111125    public:
    112         InputDeviceTemplated(unsigned int id)
     126        //! Creates the OIS device
     127        InputDeviceTemplated(unsigned int id, OIS::InputManager* oisInputManager)
    113128            : InputDevice(id)
    114         {
    115             OIS::InputManager* system = InputManager::getInstance().getOISInputManager();
    116             oisDevice_ = static_cast<OISDeviceClass*>(system->createInputObject(OISDeviceValue, true));
     129            , oisInputManager_(oisInputManager)
     130        {
     131            oisDevice_ = static_cast<OISDeviceClass*>(oisInputManager_->createInputObject(OISDeviceValue, true));
     132            // Note: after the static_cast here, the casted this pointer becomes
     133            //       invalid right until the subclass has been constructed!
    117134            oisDevice_->setEventCallback(static_cast<DeviceClass*>(this));
    118135            COUT(4) << "Instantiated a " << this->getClassName() << std::endl;
    119136        }
    120137
     138        //! Destroys the OIS device
    121139        virtual ~InputDeviceTemplated()
    122140        {
    123141            try
    124142            {
    125                 InputManager::getInstance().getOISInputManager()->destroyInputObject(oisDevice_);
     143                oisInputManager_->destroyInputObject(oisDevice_);
    126144            }
    127145            catch (...)
     
    131149        }
    132150
    133         OISDeviceClass* getOISDevice()
    134         {
    135             return this->oisDevice_;
    136         }
    137 
    138         std::string getClassName()
    139         {
    140             return DeviceClass::getClassNameImpl();
    141         }
    142 
     151        //! Captures OIS events (which then get distributed to the derived class) and creates the button held events
    143152        void update(const Clock& time)
    144153        {
    145154            oisDevice_->capture();
    146155
    147             if (!this->isCalibrating())
    148             {
    149                 // Call all the states with the held button event
    150                 for (unsigned int iB = 0; iB < pressedButtons_.size(); ++iB)
    151                     for (unsigned int iS = 0; iS < inputStates_.size(); ++iS)
    152                         inputStates_[iS]->buttonEvent<ButtonEvent::THold, Traits>(
    153                             this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(pressedButtons_[iB]));
    154 
    155                 // Call states with device update events
    156                 for (unsigned int i = 0; i < inputStates_.size(); ++i)
    157                     inputStates_[i]->update(time.getDeltaTime(), this->getDeviceID());
    158             }
     156            // Call all the states with the held button event
     157            for (unsigned int iB = 0; iB < pressedButtons_.size(); ++iB)
     158                for (unsigned int iS = 0; iS < inputStates_.size(); ++iS)
     159                    inputStates_[iS]->buttonEvent<ButtonEvent::THold, Traits>(
     160                        this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(pressedButtons_[iB]));
     161
     162            // Call states with device update events
     163            for (unsigned int i = 0; i < inputStates_.size(); ++i)
     164                inputStates_[i]->update(time.getDeltaTime(), this->getDeviceID());
    159165
    160166            static_cast<DeviceClass*>(this)->updateImpl(time);
    161167        }
    162168
     169        //! Clears the list of pressed buttons and calls the derived class's method
    163170        void clearBuffers()
    164171        {
     
    167174        }
    168175
     176        // Returns a pointer to the OIS device
     177        OISDeviceClass* getOISDevice()   { return this->oisDevice_; }
     178        // Returns the name of the derived class as string
     179        std::string getClassName() const { return DeviceClass::getClassNameImpl(); }
     180
    169181    protected:
     182        //! Common code for all button pressed events (updates pressed buttons list and calls the input states)
    170183        void buttonPressed(ButtonTypeParam button)
    171184        {
     
    184197        }
    185198
     199        //! Common code for all button released events (updates pressed buttons list and calls the input states)
    186200        void buttonReleased(ButtonTypeParam button)
    187201        {
     
    201215        }
    202216
     217        //! Managed pointer to the OIS device
    203218        OISDeviceClass* oisDevice_;
    204219
    205220    private:
    206         void clearBuffersImpl() { } //!< Fallback dummy function for static polymorphism
    207         void updateImpl(const Clock& time) { } //!< Fallback dummy function for static polymorphism
     221        //!< Fallback dummy function for static polymorphism
     222        void clearBuffersImpl() { }
     223        //!< Fallback dummy function for static polymorphism
     224        void updateImpl(const Clock& time) { }
    208225        //!< Fallback dummy function for static polymorphism
    209226        ButtonType& getButtonEventArg(ButtonType& button) { return button; }
    210227
    211         std::vector<ButtonType> pressedButtons_;
     228        std::vector<ButtonType> pressedButtons_; //!< List of all buttons that are currently pressed down
     229        OIS::InputManager* oisInputManager_;     //!< Pointer to the OIS InputManager that can create and destroy devices
    212230    };
    213231}
  • code/branches/core4/src/core/input/InputHandler.h

    r3285 r3286  
    2626 *
    2727 */
    28 
    29 /**
    30 @file
    31 @brief
    32     Declarations of various interface classes for the input management.
    33 */
    3428
    3529#ifndef _InputHandler_H__
     
    10094    /**
    10195    @brief
     96        Base class for all input handlers like KeyBinder, InputBuffer, etc.
     97
     98        Derive from this class if you wish to receive input events.
     99        But keep in mind that this is pointless wihtout first having an InputState.
     100    @note
     101        The definitions for the button events with the weird arguments are simply
     102        to avoid redunant code in the input devices.
    102103    */
    103104    class _CoreExport InputHandler
     
    140141        virtual void allDevicesUpdated(float dt) { }
    141142
     143        //! Use this input handler if you want to occupy a device in an input state.
    142144        static InputHandler EMPTY;
    143145    };
  • code/branches/core4/src/core/input/InputManager.cc

    r3281 r3286  
    201201
    202202            if (oisInputManager_->getNumberOfDevices(OIS::OISKeyboard) > 0)
    203                 devices_[InputDeviceEnumerator::Keyboard] = new Keyboard(InputDeviceEnumerator::Keyboard);
     203                devices_[InputDeviceEnumerator::Keyboard] = new Keyboard(InputDeviceEnumerator::Keyboard, oisInputManager_);
    204204            else
    205205                ThrowException(InitialisationFailed, "InputManager: No keyboard found, cannot proceed!");
     
    232232            try
    233233            {
    234                 devices_[InputDeviceEnumerator::Mouse] = new Mouse(InputDeviceEnumerator::Mouse, windowWidth, windowHeight);
     234                devices_[InputDeviceEnumerator::Mouse] = new Mouse(InputDeviceEnumerator::Mouse, oisInputManager_, windowWidth, windowHeight);
    235235            }
    236236            catch (const OIS::Exception& ex)
     
    251251            try
    252252            {
    253                 devices_.push_back(new JoyStick(InputDeviceEnumerator::FirstJoyStick + i));
     253                devices_.push_back(new JoyStick(InputDeviceEnumerator::FirstJoyStick + i, oisInputManager_));
    254254            }
    255255            catch (std::exception ex)
     
    262262        for (ObjectList<JoyStickQuantityListener>::iterator it = ObjectList<JoyStickQuantityListener>::begin(); it; ++it)
    263263            it->JoyStickQuantityChanged(devices_.size() - InputDeviceEnumerator::FirstJoyStick);
    264     }
    265 
    266     bool InputManager::checkJoyStickID(const std::string& idString) const
    267     {
    268         for (unsigned int i = InputDeviceEnumerator::FirstJoyStick; i < devices_.size(); ++i)
    269             if (static_cast<JoyStick*>(devices_[i])->getIDString() == idString)
    270                 return false;
    271         return true;
    272264    }
    273265
     
    565557        // Clear buffers to prevent button hold events
    566558        this->clearBuffers();
     559
     560        COUT(0) << "Calibration has been stored." << std::endl;
    567561    }
    568562
  • code/branches/core4/src/core/input/InputManager.h

    r3281 r3286  
    161161        //! Sets the the name of the command used by the KeyDetector as callback.
    162162        void setKeyDetectorCallback(const std::string& command);
    163         /**
    164         @brief
    165             Checks whether there is already a joy stick with the given ID string.
    166         @return
    167             Returns true if ID is ok (unique), false otherwise.
    168         */
    169         bool checkJoyStickID(const std::string& idString) const;
    170163        //! Returns the number of joy stick that have been created since the c'tor or last call to reload().
    171164        unsigned int getJoyStickQuantity() const
  • code/branches/core4/src/core/input/InputPrereqs.h

    r3285 r3286  
    3030@file
    3131@brief
    32     Declarations of various interface classes for the input management.
     32    Declarations of all key/button/axis code enumeration and string literals
     33    and an input device enumeration.
    3334*/
    3435
     
    5253        const unsigned int numberOfKeys = 0xEE; // 238
    5354
    54         // note: KeyCode comments were directly copied from OISKeyboard.h
     55        //! Key codes as enumeration
    5556        enum ByEnum
    5657        {
     
    202203        };
    203204       
    204         // Names as string. Has no real linkage!
     205        //! Key codes as strings
    205206        const char* const ByString[] =
    206207        {
     
    307308        const unsigned int numberOfButtons = 8;
    308309
     310        //! Mouse button codes as enumeration
    309311        enum ByEnum
    310312        {
     
    319321        };
    320322
    321         // Names as string. Has no real linkage!
     323        // Mouse button codes as strings
    322324        const char* const ByString[] =
    323325        {
     
    337339        const unsigned int numberOfAxes = 2;
    338340
     341        // Mouse axis codes as enumeration
    339342        enum ByEnum
    340343        {
     
    343346        };
    344347
    345         // Names as string. Has no real linkage!
     348        // Mouse axis codes as strings
    346349        const char* const ByString[] =
    347350        {
     
    356359        const unsigned int numberOfButtons = 64;
    357360
     361        // Joy stick button codes as enumeration
    358362        enum ByEnum
    359363        {
     
    380384        };
    381385
    382         // Names as string. Has no real linkage!
     386        // Joy stick button codes as strings
    383387        const char* const ByString[] =
    384388        {
     
    406410        const unsigned int numberOfAxes = 24;
    407411
     412        // Joy stick axis codes as enumeration
    408413        enum ByEnum
    409414        {
     
    416421        };
    417422
    418         // Names as string. Has no real linkage!
     423        // Joy stick axis codes as strings
    419424        const char* const ByString[] =
    420425        {
     
    435440    namespace InputDeviceEnumerator
    436441    {
     442        //! Used to access the devices in an array
    437443        enum Value
    438444        {
  • code/branches/core4/src/core/input/JoyStick.cc

    r3274 r3286  
    3030
    3131#include <ois/OISJoyStick.h>
    32 #include <ois/OISInputManager.h>
    3332#include <boost/foreach.hpp>
    3433
     
    3837#include "util/Convert.h"
    3938#include "InputState.h"
    40 #include "InputManager.h"
    4139
    4240namespace orxonox
    4341{
    44     /**
    45     @brief
    46         Helper function that loads the config value vector of one coefficient
    47     */
     42    //! Helper function that loads the config value vector of one coefficient
    4843    void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue);
    4944
    50     JoyStick::JoyStick(unsigned int id)
    51         : super(id)
     45    std::vector<std::string> JoyStick::idStrings_s;
     46
     47    JoyStick::JoyStick(unsigned int id, OIS::InputManager* oisInputManager)
     48        : super(id, oisInputManager)
    5249    {
    5350        RegisterRootObject(JoyStick);
     
    6663        //idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3));
    6764
    68         if (InputManager::getInstance().checkJoyStickID(idString_) == false)
    69         {
    70             // Make the ID unique for this execution time.
    71             idString_ += "_" + multi_cast<std::string>(this->getDeviceID());
     65
     66        BOOST_FOREACH(std::string& idString, idStrings_s)
     67        {
     68            if (idString_ == idString)
     69            {
     70                // Make the ID unique for this execution time.
     71                idString_ += "_" + multi_cast<std::string>(this->getDeviceID());
     72                break;
     73            }
    7274        }
    7375
     
    8284    }
    8385
    84     //!< Callback for the joy stick calibration config file.
     86    //! Callback for the joy stick calibration config file.
    8587    void JoyStick::calibrationFileCallback()
    8688    {
     
    173175    }
    174176
    175     // TODO: What do we really need to reset here?
     177    //! Resets the pov states
    176178    void JoyStick::clearBuffersImpl()
    177179    {
    178180        for (int j = 0; j < 4; ++j)
    179         {
    180181            povStates_[j] = 0;
    181             sliderStates_[j][0] = 0;
    182             sliderStates_[j][1] = 0;
    183         }
    184182    }
    185183
  • code/branches/core4/src/core/input/JoyStick.h

    r3285 r3286  
    3838namespace orxonox
    3939{
     40    //! Template parameter collection for the base class
    4041    struct JoyStickTraits
    4142    {
     
    6667    public:
    6768        //! Assigns a generated ID string and loads the calibration (if present)
    68         JoyStick(unsigned int id);
     69        JoyStick(unsigned int id, OIS::InputManager* oisInputManager);
    6970        ~JoyStick() { }
    7071        void setConfigValues();
    7172
    72         //! Returns the generated (from the number of knobs and the device name) ID string
     73        //! Returns the ID string generated from the number of knobs and the device name
    7374        const std::string& getIDString() const { return this->idString_; }
    7475
     
    99100        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
    100101        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
    101         //!< OIS event handler (don't remove that because of OIS version issues!)
     102        //! OIS event handler (don't remove that because of OIS version issues!)
    102103        bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
    103104
     105        //! Returns the class name as string
    104106        static std::string getClassNameImpl() { return "JoyStick"; }
    105107
     
    120122        std::string calibrationFilename_;     //!< Joy stick calibration ini filename
    121123
     124        //! Contains a list of all ID strings to avoid duplicates
     125        static std::vector<std::string> idStrings_s;
     126
    122127        //!< Maximum number of slider axes
    123128        static const unsigned int sliderAxes_s = 8;
  • code/branches/core4/src/core/input/JoyStickQuantityListener.cc

    r3274 r3286  
    2727 */
    2828
    29 /**
    30 @file
    31 @brief
    32     Implementation of the JoyStickQuantityListener class.
    33 */
    34 
    3529#include "JoyStickQuantityListener.h"
    3630#include "core/CoreIncludes.h"
  • code/branches/core4/src/core/input/JoyStickQuantityListener.h

    r3274 r3286  
    4040namespace orxonox
    4141{
     42    //! Derive from this class to get informed when joy sticks get added/removed
    4243    class _CoreExport JoyStickQuantityListener : virtual public OrxonoxClass
    4344    {
     
    4647        virtual ~JoyStickQuantityListener() { }
    4748
     49        //! Called when joy sticks get added/removed
    4850        virtual void JoyStickQuantityChanged(unsigned int value) = 0;
    4951    };
  • code/branches/core4/src/core/input/Keyboard.h

    r3285 r3286  
    3131
    3232#include "InputPrereqs.h"
     33
    3334#include "InputHandler.h"
    3435#include "InputDevice.h"
     
    3637namespace orxonox
    3738{
     39    //! Template parameter collection for the base class
    3840    struct KeyboardTraits
    3941    {
     
    6163
    6264    public:
    63         Keyboard(unsigned int id) : super(id), modifiers_(0) { }
     65        //! Only resets the keyboard modifiers. Initialising is done in the base class.
     66        Keyboard(unsigned int id, OIS::InputManager* oisInputManager) : super(id, oisInputManager), modifiers_(0) { }
    6467        ~Keyboard() { }
    6568
    6669    private:
    67         // TODO: Do we need to reset the modifiers?
    68         void clearBuffersImpl() { }
     70        //! Resets the keyboard modifiers
     71        void clearBuffersImpl() { this->modifiers_ = 0; }
    6972        //! Translates the KeyHandle to a KeyEvent
    70         KeyEvent& getButtonEventArg(KeyEvent& button) { button.setModifiers(modifiers_); return button; }
     73        KeyEvent& getButtonEventArg(KeyEvent& button)
     74        {
     75            button.setModifiers(modifiers_);
     76            return button;
     77        }
    7178
    7279        bool keyPressed(const OIS::KeyEvent& arg);
    7380        bool keyReleased(const OIS::KeyEvent& arg);
    7481
     82        //! Returns the class name as string
    7583        static std::string getClassNameImpl() { return "Keyboard"; }
    7684
  • code/branches/core4/src/core/input/Mouse.cc

    r3276 r3286  
    3030
    3131#include <ois/OISMouse.h>
    32 #include <boost/foreach.hpp>
    3332#include "InputState.h"
    3433#include "core/ConsoleCommand.h"
     
    4140namespace orxonox
    4241{
    43     Mouse::Mouse(unsigned int id, unsigned int windowWidth, unsigned int windowHeight)
    44         : super(id)
     42    Mouse::Mouse(unsigned int id, OIS::InputManager* oisInputManager, unsigned int windowWidth, unsigned int windowHeight)
     43        : super(id, oisInputManager)
    4544    {
    4645        this->setMouseClipping(windowWidth, windowHeight);
     
    7473            IntVector2 rel(e.state.X.rel, e.state.Y.rel);
    7574            IntVector2 clippingSize(e.state.width, e.state.height);
    76             BOOST_FOREACH(InputState* state, inputStates_)
    77                 state->mouseMoved(abs, rel, clippingSize);
     75            for (unsigned int i = 0; i < inputStates_.size(); ++i)
     76                inputStates_[i]->mouseMoved(abs, rel, clippingSize);
    7877        }
    7978
     
    8180        if (e.state.Z.rel != 0)
    8281        {
    83             BOOST_FOREACH(InputState* state, inputStates_)
    84                 state->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
     82            for (unsigned int i = 0; i < inputStates_.size(); ++i)
     83                inputStates_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
    8584        }
    8685
  • code/branches/core4/src/core/input/Mouse.h

    r3285 r3286  
    3535namespace orxonox
    3636{
     37    //! Template parameter collection for the base class
    3738    struct MouseTraits
    3839    {
     
    5859
    5960    public:
    60         Mouse(unsigned int id, unsigned int windowWidth, unsigned int windowHeight);
     61        //! Only sets the clipping size. Initialising is done in the base class.
     62        Mouse(unsigned int id, OIS::InputManager* oisInputManager, unsigned int windowWidth, unsigned int windowHeight);
    6163        ~Mouse() { }
    6264
     
    6870        */
    6971        void setMouseClipping(unsigned int width, unsigned int height);
     72        // Returns the width of the mouse window
    7073        unsigned int getClippingWidth() const;
     74        // Returns the height of the mouse window
    7175        unsigned int getClippingHeight() const;
    7276
     
    7781#ifdef ORXONOX_PLATFORM_LINUX
    7882        // HACK!
     83        // TODO: Make this a feature rather than a hack
    7984        static void grabMouse();
    8085        static void ungrabMouse();
     
    8287
    8388    private:
    84         // TODO: Do we need to reset the mouse position?
    85         void clearBuffersImpl() { }
    86 
    8789        //! OIS event handler
    8890        bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
     
    101103        bool mouseMoved(const OIS::MouseEvent &arg);
    102104
     105        // Returns the class name as string
    103106        static std::string getClassNameImpl() { return "Mouse"; }
    104107
Note: See TracChangeset for help on using the changeset viewer.