Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 27, 2008, 10:36:53 AM (16 years ago)
Author:
rgrieder
Message:

converted input system to 4 spaces/tab

File:
1 edited

Legend:

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

    r1629 r1630  
    2828
    2929/**
    30  @file
    31  @brief Implementation of a little Input handler that distributes everything
    32         coming from OIS.
    33  */
     30@file
     31@brief
     32    Implementation of a little Input handler that distributes everything
     33    coming from OIS.
     34*/
    3435
    3536#ifndef _InputManager_H__
     
    4041#include <map>
    4142#include <vector>
    42 
    4343#include "util/Math.h"
    4444#include "core/OrxonoxClass.h"
     
    4747namespace orxonox
    4848{
    49   /**
    50   * Helper class to realise a vector<int[4]>
    51   */
    52   class POVStates
    53   {
    54   public:
    55     int operator[](unsigned int index) { return povStates[index]; }
    56     int povStates[4];
    57   };
    58 
    59   /**
    60   * Helper class to realise a vector< {int[4], int[4]} >
    61   */
    62   class SliderStates
    63   {
    64   public:
    65     IntVector2 sliderStates[4];
    66   };
    67 
    68   /**
    69   * Struct for storing a custom input state
    70   */
    71   struct StoredState
    72   {
    73     std::vector<KeyHandler*>                    activeKeyHandlers_;
    74     std::vector<MouseHandler*>                  activeMouseHandlers_;
    75     std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
    76     std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
    77   };
    78 
    79   struct JoyStickCalibration
    80   {
    81     int zeroStates[24];
    82     float positiveCoeff[24];
    83     float negativeCoeff[24];
    84   };
    85 
    86   /**
    87     @brief Captures and distributes mouse and keyboard input.
    88   */
    89   class _CoreExport InputManager
     49    /**
     50    @brief
     51        Helper class to realise a vector<int[4]>
     52    */
     53    class POVStates
     54    {
     55    public:
     56        int operator[](unsigned int index) { return povStates[index]; }
     57        int povStates[4];
     58    };
     59
     60    /**
     61    @brief
     62        Helper class to realise a vector< {int[4], int[4]} >
     63    */
     64    class SliderStates
     65    {
     66    public:
     67        IntVector2 sliderStates[4];
     68    };
     69
     70    /**
     71    @brief
     72        Struct for storing a custom input state
     73    */
     74    struct StoredState
     75    {
     76        std::vector<KeyHandler*>                    activeKeyHandlers_;
     77        std::vector<MouseHandler*>                  activeMouseHandlers_;
     78        std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
     79        std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
     80    };
     81
     82    struct JoyStickCalibration
     83    {
     84        int zeroStates[24];
     85        float positiveCoeff[24];
     86        float negativeCoeff[24];
     87    };
     88
     89    /**
     90    @brief
     91        Captures and distributes mouse and keyboard input.
     92    */
     93    class _CoreExport InputManager
    9094        : public OrxonoxClass,
    91           public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
    92   {
    93   public: // enumerations
    94     /**
    95       @brief Designates the way input is handled and redirected.
    96     */
    97     enum InputState
    98     {
    99       IS_UNINIT,    //!< InputManager has not yet been initialised.
    100       IS_NONE,      //!< Input is discarded.
    101       IS_NORMAL,    //!< Normal play state. Key and button bindings are active.
    102       IS_GUI,       //!< All OIS input events are passed to CEGUI.
    103       IS_CONSOLE,   //!< Keyboard input is redirected to the InputBuffer.
    104       IS_DETECT,    //!< All the input additionally goes to the KeyDetector
    105       IS_NODETECT,  //!< remove KeyDetector
    106       IS_NOCALIBRATE,
    107       IS_CALIBRATE,
    108       IS_CUSTOM     //!< Any possible configuration.
    109     };
    110 
    111   public: // member functions
    112     void setConfigValues();
    113 
    114   public: // static functions
    115     static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
    116           bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
    117     static bool initialiseKeyboard();
    118     static bool initialiseMouse();
    119     static bool initialiseJoySticks();
    120     static int  numberOfKeyboards();
    121     static int  numberOfMice();
    122     static int  numberOfJoySticks();
    123 
    124     static void destroy();
    125     static void destroyKeyboard();
    126     static void destroyMouse();
    127     static void destroyJoySticks();
    128 
    129     //static bool isModifierDown(KeyboardModifier::Enum modifier);
    130     //static bool isKeyDown(KeyCode::Enum key);
    131     //static const MouseState getMouseState();
    132     //static const JoyStickState getJoyStickState(unsigned int ID);
    133 
    134     static void setWindowExtents(const int width, const int height);
    135 
    136     static void setInputState(const InputState state);
    137     static InputState getInputState();
    138 
    139     static void storeKeyStroke(const std::string& name);
    140     static void keyBind(const std::string& command);
    141 
    142     static void calibrate();
    143 
    144     static void tick(float dt);
    145 
    146     static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
    147     static bool removeKeyHandler              (const std::string& name);
    148     static KeyHandler* getKeyHandler          (const std::string& name);
    149     static bool enableKeyHandler              (const std::string& name);
    150     static bool disableKeyHandler             (const std::string& name);
    151     static bool isKeyHandlerActive            (const std::string& name);
    152 
    153     static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
    154     static bool removeMouseHandler            (const std::string& name);
    155     static MouseHandler* getMouseHandler      (const std::string& name);
    156     static bool enableMouseHandler            (const std::string& name);
    157     static bool disableMouseHandler           (const std::string& name);
    158     static bool isMouseHandlerActive          (const std::string& name);
    159 
    160     static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
    161     static bool removeJoyStickHandler         (const std::string& name);
    162     static JoyStickHandler* getJoyStickHandler(const std::string& name);
    163     static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
    164     static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
    165     static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
    166 
    167   private: // functions
    168     // don't mess with a Singleton
    169     InputManager ();
    170     InputManager (const InputManager&);
    171     ~InputManager();
    172 
    173     // Intenal methods
    174     bool _initialise(const size_t, int, int, bool, bool, bool);
    175     bool _initialiseKeyboard();
    176     bool _initialiseMouse();
    177     bool _initialiseJoySticks();
    178 
    179     void _destroy();
    180     void _destroyKeyboard();
    181     void _destroyMouse();
    182     void _destroyJoySticks();
    183 
    184     void _updateTickables();
    185 
    186     void _saveState();
    187     void _restoreState();
    188 
    189     void _completeCalibration();
    190 
    191     void _fireAxis(unsigned int iJoyStick, int axis, int value);
    192     unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
    193 
    194     void _tick(float dt);
    195 
    196     // input events
    197     bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
    198     bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
    199     bool mouseMoved    (const OIS::MouseEvent    &arg);
    200     bool keyPressed    (const OIS::KeyEvent      &arg);
    201     bool keyReleased   (const OIS::KeyEvent      &arg);
    202     bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
    203     bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
    204     bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
    205     bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
    206     bool povMoved      (const OIS::JoyStickEvent &arg, int id);
    207     //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
    208 
    209     static InputManager& _getSingleton();
    210     static InputManager* _getSingletonPtr() { return &_getSingleton(); }
    211 
    212   private: // variables
    213     OIS::InputManager*                          inputSystem_;     //!< OIS input manager
    214     OIS::Keyboard*                              keyboard_;        //!< OIS mouse
    215     OIS::Mouse*                                 mouse_;           //!< OIS keyboard
    216     std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
    217     unsigned int                                joySticksSize_;
    218 
    219     KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
    220     KeyDetector*                                keyDetector_;     //!< KeyDetector instance
    221     InputBuffer*                                buffer_;          //!< InputBuffer instance
    222     CalibratorCallback*                         calibratorCallback_;
    223 
    224     InputState state_;
    225     InputState stateRequest_;
    226     InputState savedState_;
    227     unsigned int keyboardModifiers_;
    228     StoredState savedHandlers_;
    229 
    230     // joystick calibration
    231     //std::vector<int> marginalsMaxConfig_;
    232     //std::vector<int> marginalsMinConfig_;
    233     int marginalsMax_[24];
    234     int marginalsMin_[24];
    235     bool bCalibrated_;
    236 
    237     //! Keeps track of the joy stick POV states
    238     std::vector<POVStates>                      povStates_;
    239     //! Keeps track of the possibly two slider axes
    240     std::vector<SliderStates>                   sliderStates_;
    241     std::vector<JoyStickCalibration>            joySticksCalibration_;
    242 
    243     std::map<std::string, KeyHandler*>          keyHandlers_;
    244     std::map<std::string, MouseHandler*>        mouseHandlers_;
    245     std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
    246 
    247     std::vector<KeyHandler*>                    activeKeyHandlers_;
    248     std::vector<MouseHandler*>                  activeMouseHandlers_;
    249     std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
    250     std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
    251 
    252     std::vector<Key>                            keysDown_;
    253     std::vector<MouseButton::Enum>              mouseButtonsDown_;
    254     std::vector<std::vector<int> >              joyStickButtonsDown_;
    255 
    256     static std::string                          bindingCommmandString_s;
    257   };
     95        public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
     96    {
     97    public: // enumerations
     98        /**
     99        @brief
     100            Designates the way input is handled and redirected.
     101        */
     102        enum InputState
     103        {
     104            IS_UNINIT,    //!< InputManager has not yet been initialised.
     105            IS_NONE,      //!< Input is discarded.
     106            IS_NORMAL,    //!< Normal play state. Key and button bindings are active.
     107            IS_GUI,       //!< All OIS input events are passed to CEGUI.
     108            IS_CONSOLE,   //!< Keyboard input is redirected to the InputBuffer.
     109            IS_DETECT,    //!< All the input additionally goes to the KeyDetector
     110            IS_NODETECT,  //!< remove KeyDetector
     111            IS_NOCALIBRATE,
     112            IS_CALIBRATE,
     113            IS_CUSTOM     //!< Any possible configuration.
     114        };
     115
     116    public: // member functions
     117        void setConfigValues();
     118
     119    public: // static functions
     120        static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
     121                               bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
     122        static bool initialiseKeyboard();
     123        static bool initialiseMouse();
     124        static bool initialiseJoySticks();
     125        static int  numberOfKeyboards();
     126        static int  numberOfMice();
     127        static int  numberOfJoySticks();
     128
     129        static void destroy();
     130        static void destroyKeyboard();
     131        static void destroyMouse();
     132        static void destroyJoySticks();
     133
     134        //static bool isModifierDown(KeyboardModifier::Enum modifier);
     135        //static bool isKeyDown(KeyCode::Enum key);
     136        //static const MouseState getMouseState();
     137        //static const JoyStickState getJoyStickState(unsigned int ID);
     138
     139        static void setWindowExtents(const int width, const int height);
     140
     141        static void setInputState(const InputState state);
     142        static InputState getInputState();
     143
     144        static void storeKeyStroke(const std::string& name);
     145        static void keyBind(const std::string& command);
     146
     147        static void calibrate();
     148
     149        static void tick(float dt);
     150
     151        static bool addKeyHandler                 (KeyHandler* handler, const std::string& name);
     152        static bool removeKeyHandler              (const std::string& name);
     153        static KeyHandler* getKeyHandler          (const std::string& name);
     154        static bool enableKeyHandler              (const std::string& name);
     155        static bool disableKeyHandler             (const std::string& name);
     156        static bool isKeyHandlerActive            (const std::string& name);
     157
     158        static bool addMouseHandler               (MouseHandler* handler, const std::string& name);
     159        static bool removeMouseHandler            (const std::string& name);
     160        static MouseHandler* getMouseHandler      (const std::string& name);
     161        static bool enableMouseHandler            (const std::string& name);
     162        static bool disableMouseHandler           (const std::string& name);
     163        static bool isMouseHandlerActive          (const std::string& name);
     164
     165        static bool addJoyStickHandler            (JoyStickHandler* handler, const std::string& name);
     166        static bool removeJoyStickHandler         (const std::string& name);
     167        static JoyStickHandler* getJoyStickHandler(const std::string& name);
     168        static bool enableJoyStickHandler         (const std::string& name, unsigned int id);
     169        static bool disableJoyStickHandler        (const std::string& name, unsigned int id);
     170        static bool isJoyStickHandlerActive       (const std::string& name, unsigned int id);
     171
     172    private: // functions
     173        // don't mess with a Singleton
     174        InputManager ();
     175        InputManager (const InputManager&);
     176        ~InputManager();
     177
     178        // Intenal methods
     179        bool _initialise(const size_t, int, int, bool, bool, bool);
     180        bool _initialiseKeyboard();
     181        bool _initialiseMouse();
     182        bool _initialiseJoySticks();
     183
     184        void _destroy();
     185        void _destroyKeyboard();
     186        void _destroyMouse();
     187        void _destroyJoySticks();
     188
     189        void _updateTickables();
     190
     191        void _saveState();
     192        void _restoreState();
     193
     194        void _completeCalibration();
     195
     196        void _fireAxis(unsigned int iJoyStick, int axis, int value);
     197        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
     198
     199        void _tick(float dt);
     200
     201        // input events
     202        bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
     203        bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
     204        bool mouseMoved    (const OIS::MouseEvent    &arg);
     205        bool keyPressed    (const OIS::KeyEvent      &arg);
     206        bool keyReleased   (const OIS::KeyEvent      &arg);
     207        bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
     208        bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
     209        bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
     210        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
     211        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
     212        //bool vector3Moved  (const OIS::JoyStickEvent &arg, int id);
     213
     214        static InputManager& _getSingleton();
     215        static InputManager* _getSingletonPtr() { return &_getSingleton(); }
     216
     217    private: // variables
     218        OIS::InputManager*                          inputSystem_;     //!< OIS input manager
     219        OIS::Keyboard*                              keyboard_;        //!< OIS mouse
     220        OIS::Mouse*                                 mouse_;           //!< OIS keyboard
     221        std::vector<OIS::JoyStick*>                 joySticks_;       //!< OIS joy sticks
     222        unsigned int                                joySticksSize_;
     223
     224        KeyBinder*                                  keyBinder_;       //!< KeyBinder instance
     225        KeyDetector*                                keyDetector_;     //!< KeyDetector instance
     226        InputBuffer*                                buffer_;          //!< InputBuffer instance
     227        CalibratorCallback*                         calibratorCallback_;
     228
     229        InputState state_;
     230        InputState stateRequest_;
     231        InputState savedState_;
     232        unsigned int keyboardModifiers_;
     233        StoredState savedHandlers_;
     234
     235        // joystick calibration
     236        //std::vector<int> marginalsMaxConfig_;
     237        //std::vector<int> marginalsMinConfig_;
     238        int marginalsMax_[24];
     239        int marginalsMin_[24];
     240        bool bCalibrated_;
     241
     242        //! Keeps track of the joy stick POV states
     243        std::vector<POVStates>                      povStates_;
     244        //! Keeps track of the possibly two slider axes
     245        std::vector<SliderStates>                   sliderStates_;
     246        std::vector<JoyStickCalibration>            joySticksCalibration_;
     247
     248        std::map<std::string, KeyHandler*>          keyHandlers_;
     249        std::map<std::string, MouseHandler*>        mouseHandlers_;
     250        std::map<std::string, JoyStickHandler*>     joyStickHandlers_;
     251
     252        std::vector<KeyHandler*>                    activeKeyHandlers_;
     253        std::vector<MouseHandler*>                  activeMouseHandlers_;
     254        std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_;
     255        std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_;
     256
     257        std::vector<Key>                            keysDown_;
     258        std::vector<MouseButton::Enum>              mouseButtonsDown_;
     259        std::vector<std::vector<int> >              joyStickButtonsDown_;
     260
     261        static std::string                          bindingCommmandString_s;
     262    };
    258263
    259264}
Note: See TracChangeset for help on using the changeset viewer.