Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 19, 2009, 5:31:02 PM (15 years ago)
Author:
rgrieder
Message:

Merged all remaining revisions from core4 back to the trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/input/InputManager.h

    r3196 r3327  
    2727 */
    2828
    29 /**
    30 @file
    31 @brief
    32     Implementation of a little Input handler that distributes everything
    33     coming from OIS.
    34 */
    35 
    3629#ifndef _InputManager_H__
    3730#define _InputManager_H__
    3831
    39 #include "core/CorePrereqs.h"
     32#include "InputPrereqs.h"
    4033
    4134#include <map>
     
    4336#include <string>
    4437#include <vector>
    45 #include <ois/OISKeyboard.h>
    46 #include <ois/OISMouse.h>
    47 #include <ois/OISJoyStick.h>
    48 
    49 #include "util/Math.h"
    50 #include "util/OrxEnum.h"
    51 #include "core/OrxonoxClass.h"
    52 #include "InputInterfaces.h"
     38
     39#include "core/WindowEventListener.h"
     40#include "InputState.h"
    5341
    5442namespace orxonox
     
    5644    /**
    5745    @brief
    58         Helper class to realise a vector<int[4]>
     46        Manages the input devices (mouse, keyboard, joy sticks) and the input states.
     47
     48        Every input device has its own wrapper class which does the actualy input event
     49        distribution. The InputManager only creates reloads (on request) those devices.
     50
     51        The other functionality concerns handling InputStates. They act as a layer
     52        between InputHandlers (like the KeyBinder or the GUIManager) and InputDevices.
     53        InputStates are memory managed by the IputManager. You cannot create or destroy
     54        them on your own. Therefore all states get destroyed with the InputManager d'tor.
     55    @note
     56        - The actual lists containing all the InputStates for a specific device are stored
     57          in the InputDevices themselves.
     58        - The devices_ vector always has at least two elements: Keyboard (first) and mouse.
     59          You best access them intenally with InputDeviceEnumerator::Keyboard/Mouse
     60          The first joy stick is accessed with InputDeviceEnumerator::FirstJoyStick.
     61        - Keyboard construction is mandatory , mouse and joy sticks are not.
     62          If the OIS::InputManager or the Keyboard fail, an exception is thrown.
    5963    */
    60     class POVStates
     64    class _CoreExport InputManager : public WindowEventListener
    6165    {
    6266    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 
    84     struct InputStatePriority : OrxEnum<InputStatePriority>
    85     {
    86         OrxEnumConstructors(InputStatePriority);
    87 
    88         static const int Empty        = -1;
    89         static const int Dynamic      = 0;
    90 
    91         static const int HighPriority = 1000;
    92         static const int Console      = HighPriority + 0;
    93         static const int Calibrator   = HighPriority + 1;
    94         static const int Detector     = HighPriority + 2;
    95     };
    96 
    97     /**
    98     @brief
    99         Captures and distributes mouse and keyboard input.
    100     */
    101     class _CoreExport InputManager
    102         : public OrxonoxClass,
    103         public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
    104     {
    105         // --> setConfigValues is private
    106         friend class ClassIdentifier<InputManager>;
    107 
    108     public:
    109         enum InputManagerState
     67        //! Represents internal states of the InputManager.
     68        enum State
    11069        {
    111             Uninitialised    = 0x00,
    112             OISReady         = 0x01,
    113             InternalsReady   = 0x02,
    114             Ticking          = 0x04,
    115             Calibrating      = 0x08,
    116             ReloadRequest    = 0x10,
    117             JoyStickSupport  = 0x20 // used with ReloadRequest to store a bool
     70            Nothing       = 0x00,
     71            Bad           = 0x02,
     72            Ticking       = 0x04,
     73            Calibrating   = 0x08,
     74            ReloadRequest = 0x10,
    11875        };
    11976
    120         InputManager ();
     77        /**
     78        @brief
     79            Loads the devices and initialises the KeyDetector and the Calibrator.
     80           
     81            If either the OIS input system and/or the keyboard could not be created,
     82            the constructor fails with an std::exception.
     83        */
     84        InputManager(size_t windowHnd);
     85        //! Destroys all devices AND all input states!
    12186        ~InputManager();
    122 
    123         void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true);
    124 
    125         void reloadInputSystem(bool joyStickSupport = true);
    126 
     87        void setConfigValues();
     88
     89        /**
     90        @brief
     91            Updates the devices (which distribute the input events) and the input states.
     92
     93            Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request
     94            was submitted while updating, the request wil be postponed until the next update call.
     95        */
     96        void update(const Clock& time);
     97        //! Clears all input device buffers. This usually only includes the pressed button list.
    12798        void clearBuffers();
    128 
    129         unsigned int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
    130         unsigned int  numberOfMice()      { return mouse_    ? 1 : 0; }
    131         unsigned int  numberOfJoySticks() { return joySticksSize_; }
    132 
    133         void setWindowExtents(const int width, const int height);
     99        //! Starts joy stick calibration.
     100        void calibrate();
     101        /**
     102        @brief
     103            Reloads all the input devices. Use this method to initialise new joy sticks.
     104        @note
     105            Only reloads immediately if the call stack doesn't include the update() method.
     106        */
     107        void reload();
     108
     109        //-------------------------------
     110        // Input States
     111        //-------------------------------
     112        /**
     113        @brief
     114            Creates a new InputState that gets managed by the InputManager.
     115        @remarks
     116            The InputManager will take care of the state completely. That also
     117            means it gets deleted when the InputManager is destroyed!
     118        @param name
     119            Unique name of the InputState when referenced as string
     120        @param priority
     121            Priority matters when multiple states are active. You can specify any
     122            number, but 1 - 99 is preferred (99 means high priority).
     123        */
     124        InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
     125        /**
     126        @brief
     127            Returns a pointer to a InputState referenced by name.
     128        @return
     129            Returns NULL if state was not found.
     130        */
     131        InputState* getState(const std::string& name);
     132        /**
     133        @brief
     134            Activates a specific input state.
     135            It might not be actually activated if the priority is too low!
     136        @return
     137            False if name was not found, true otherwise.
     138        */
     139        bool enterState(const std::string& name);
     140        /**
     141        @brief
     142            Deactivates a specific input state.
     143        @return
     144            False if name was not found, true otherwise.
     145        */
     146        bool leaveState(const std::string& name);
     147        /**
     148        @brief
     149            Removes and destroys an input state.
     150        @return
     151            True if removal was successful, false if name was not found.
     152        @remarks
     153            - You can't remove the internal states "empty", "calibrator" and "detector".
     154            - The removal process is being postponed if InputManager::update() is currently running.
     155        */
     156        bool destroyState(const std::string& name);
     157
     158        //-------------------------------
     159        // Various getters and setters
     160        //-------------------------------
     161        //! Sets the the name of the command used by the KeyDetector as callback.
    134162        void setKeyDetectorCallback(const std::string& command);
    135 
    136         template <class T>
    137         T* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
    138 
    139         InputState* getState       (const std::string& name);
    140         InputState* getCurrentState();
    141         bool requestDestroyState   (const std::string& name);
    142         bool requestEnterState     (const std::string& name);
    143         bool requestLeaveState     (const std::string& name);
    144 
    145 #ifdef ORXONOX_PLATFORM_LINUX
    146         // HACK!
    147         static void grabMouse();
    148         static void ungrabMouse();
    149 #endif
    150 
    151         void update(const Clock& time);
    152 
    153         static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
    154         static InputManager* getInstancePtr() { return singletonRef_s; }
    155 
    156         // console commands
    157         static void calibrate();
    158         static void reload(bool joyStickSupport = true);
    159 
    160     public: // variables
    161         static EmptyHandler                 EMPTY_HANDLER;
    162         static const unsigned int           sliderAxes = 8;
     163        //! Returns the number of joy stick that have been created since the c'tor or last call to reload().
     164        unsigned int getJoyStickQuantity() const
     165            { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; }
     166        //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing!
     167        OIS::InputManager* getOISInputManager()
     168            { return this->oisInputManager_; }
     169
     170        //! Returns a reference to the singleton instance
     171        static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    163172
    164173    private: // functions
    165174        // don't mess with a Singleton
    166         InputManager (const InputManager&);
     175        InputManager(const InputManager&);
    167176
    168177        // Intenal methods
    169         void _initialiseKeyboard();
    170         void _initialiseMouse();
    171         void _initialiseJoySticks();
    172         void _configureJoySticks();
    173 
    174         void _loadCalibration();
    175         void _startCalibration();
    176         void _completeCalibration();
    177         void _evaluateCalibration();
    178 
    179         void _destroyKeyboard();
    180         void _destroyMouse();
    181         void _destroyJoySticks();
    182         void _destroyState(InputState* state);
    183         void _clearBuffers();
    184 
    185         void _reload(bool joyStickSupport);
    186 
    187         void _fireAxis(unsigned int iJoyStick, int axis, int value);
    188         unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
    189 
    190         void _updateActiveStates();
    191         bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority);
    192 
    193         // input events
    194         bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
    195         bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
    196         bool mouseMoved    (const OIS::MouseEvent    &arg);
    197         bool keyPressed    (const OIS::KeyEvent      &arg);
    198         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; }
    206 
    207         void setConfigValues();
    208         void _calibrationFileCallback();
     178        void loadDevices(size_t windowHnd);
     179        void loadMouse();
     180        void loadJoySticks();
     181        void destroyDevices();
     182
     183        void stopCalibration();
     184        void reloadInternal();
     185
     186        void destroyStateInternal(InputState* state);
     187        void updateActiveStates();
     188
     189        // From WindowEventListener
     190        void windowFocusChanged();
    209191
    210192    private: // variables
    211         OIS::InputManager*                  inputSystem_;          //!< OIS input manager
    212         OIS::Keyboard*                      keyboard_;             //!< OIS mouse
    213         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
    217         unsigned int                        devicesNum_;
    218         size_t                              windowHnd_;            //!< Render window handle
    219         InputManagerState                   internalState_;        //!< Current internal state
     193        State                               internalState_;        //!< Current internal state
     194        OIS::InputManager*                  oisInputManager_;      //!< OIS input manager
     195        std::vector<InputDevice*>           devices_;              //!< List of all input devices (keyboard, mouse, joy sticks)
     196        // TODO: Get this from the GraphicsManager during reload
     197        size_t                              windowHnd_;            //!< Render window handle (used to reload the InputManager)
    220198
    221199        // some internally handled states and handlers
    222         SimpleInputState*                   stateEmpty_;
     200        InputState*                         emptyState_;           //!< Lowest priority states (makes handling easier)
    223201        KeyDetector*                        keyDetector_;          //!< KeyDetector instance
    224         InputBuffer*                        calibratorCallbackBuffer_;
    225 
    226         std::map<std::string, InputState*>  inputStatesByName_;
    227 
    228         std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
    229         std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
    230         std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
    231 
    232         std::map<int, InputState*>          activeStates_;
    233         std::vector<std::vector<InputState*> > activeStatesTriggered_;
    234         std::vector<InputState*>            activeStatesTicked_;
    235 
    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 
    243         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.
    246 
    247         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
    253 
    254         static InputManager*                singletonRef_s;
     202        //! InputBuffer that reacts to the Enter key when calibrating the joy sticks
     203        InputBuffer*                        calibratorCallbackHandler_;
     204
     205        std::map<std::string, InputState*>  statesByName_;         //!< Contains all the created input states by name
     206        std::map<int, InputState*>          activeStates_;         //!< Contains all active input states by priority (std::map is sorted!)
     207        std::vector<InputState*>            activeStatesTicked_;   //!< Like activeStates_, but only contains the ones that currently receive events
     208
     209        std::set<InputState*>               stateEnterRequests_;   //!< Requests to enter a new state
     210        std::set<InputState*>               stateLeaveRequests_;   //!< Requests to leave a running state
     211        std::set<InputState*>               stateDestroyRequests_; //!< Requests to destroy a state
     212
     213        static InputManager*                singletonRef_s;        //!< Pointer reference to the singleton
    255214    };
    256 
    257     /**
    258     @brief
    259         Creates a new InputState by type, name and priority.
    260        
    261         You will have to use this method because the
    262         c'tors and d'tors are private.
    263     @remarks
    264         The InputManager will take care of the state completely. That also
    265         means it gets deleted when the InputManager is destroyed!
    266     @param name
    267         Name of the InputState when referenced as string
    268     @param priority
    269         Priority matters when multiple states are active. You can specify any
    270         number, but 1 - 99 is preferred (99 means high).
    271     */
    272     template <class T>
    273     T* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority)
    274     {
    275         T* state = new T;
    276         if (_configureInputState(state, name, bAlwaysGetsInput, bTransparent, priority))
    277             return state;
    278         else
    279         {
    280             delete state;
    281             return 0;
    282         }
    283     }
    284215}
    285216
Note: See TracChangeset for help on using the changeset viewer.