Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/input/InputManager.h @ 1881

Last change on this file since 1881 was 1881, checked in by rgrieder, 16 years ago

Changed initialisation of internally handled InputStates and InputHandlers.

  • Property svn:eol-style set to native
File size: 9.4 KB
RevLine 
[918]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1502]3 *                    > www.orxonox.net <
[918]4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
[973]28
[918]29/**
[1755]30@file
31@brief
32    Implementation of a little Input handler that distributes everything
33    coming from OIS.
34*/
[973]35
36#ifndef _InputManager_H__
37#define _InputManager_H__
38
[1519]39#include "core/CorePrereqs.h"
[1062]40
[1219]41#include <map>
[1293]42#include <vector>
[1755]43#include <stack>
[1349]44#include "util/Math.h"
[1524]45#include "core/OrxonoxClass.h"
[1293]46#include "InputInterfaces.h"
[973]47
48namespace orxonox
49{
[1755]50    /**
51    @brief
52        Helper class to realise a vector<int[4]>
53    */
54    class POVStates
55    {
56    public:
57        int& operator[](unsigned int index) { return povStates[index]; }
58        int povStates[4];
59    };
[1349]60
[1219]61    /**
[1755]62    @brief
63        Helper class to realise a vector< {int[4], int[4]} >
[1219]64    */
[1755]65    class SliderStates
[1219]66    {
[1755]67    public:
68        IntVector2 sliderStates[4];
[1219]69    };
[973]70
[1755]71    struct JoyStickCalibration
72    {
73        int zeroStates[24];
74        float positiveCoeff[24];
75        float negativeCoeff[24];
76    };
[1502]77
[1755]78    /**
79    @brief
80        Captures and distributes mouse and keyboard input.
81    */
82    class _CoreExport InputManager
83        : public OrxonoxClass,
84        public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
85    {
86        // --> setConfigValues is private
87        friend class ClassIdentifier<InputManager>;
88        // let Core class use tick(.)
89        friend class Core;
[1066]90
[1755]91    public:
92        enum InputManagerState
93        {
94            Uninitialised    = 0x00,
95            OISReady         = 0x01,
96            InternalsReady   = 0x02,
97            Ticking          = 0x04,
98            Calibrating      = 0x08,
99            ReloadRequest    = 0x10,
100            JoyStickSupport  = 0x20 // used with ReloadRequest to store a bool
101        };
[1219]102
[1755]103        InputManager ();
104        ~InputManager();
[1293]105
[1755]106        void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true);
[1219]107
[1755]108        void reloadInputSystem(bool joyStickSupport = true);
[1219]109
[1878]110        void clearBuffers();
111
[1755]112        int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
113        int  numberOfMice()      { return mouse_    ? 1 : 0; }
114        int  numberOfJoySticks() { return joySticksSize_; }
[1502]115
[1755]116        void setWindowExtents(const int width, const int height);
[1502]117
[1755]118        template <class T>
[1878]119        T* createInputState(const std::string& name, int priority);
[1524]120
[1755]121        InputState* getState       (const std::string& name);
122        InputState* getCurrentState();
[1788]123        ExtendedInputState* getMasterInputState() { return this->stateMaster_; }
[1755]124        bool requestDestroyState   (const std::string& name);
125        bool requestEnterState     (const std::string& name);
126        bool requestLeaveState     (const std::string& name);
[1219]127
[1755]128        void tick(float dt);
[1219]129
[1755]130        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
131        static InputManager* getInstancePtr() { return singletonRef_s; }
[1219]132
[1878]133        // console commands
[1755]134        static void storeKeyStroke(const std::string& name);
135        static void keyBind(const std::string& command);
136        static void calibrate();
137        static void reload(bool joyStickSupport = true);
[973]138
[1878]139    public: // variables
140        static EmptyHandler                 EMPTY_HANDLER;
141
[1755]142    private: // functions
143        // don't mess with a Singleton
144        InputManager (const InputManager&);
[973]145
[1755]146        // Intenal methods
147        void _initialiseKeyboard();
148        void _initialiseMouse();
149        void _initialiseJoySticks();
150        void _redimensionLists();
[973]151
[1755]152        void _destroyKeyboard();
153        void _destroyMouse();
154        void _destroyJoySticks();
155        void _destroyState(InputState* state);
156        void _clearBuffers();
[1349]157
[1755]158        void _reload(bool joyStickSupport);
[1502]159
[1755]160        void _completeCalibration();
[1502]161
[1755]162        void _fireAxis(unsigned int iJoyStick, int axis, int value);
163        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
[1502]164
[1755]165        void _updateActiveStates();
166        bool _configureInputState(InputState* state, const std::string& name, int priority);
[1219]167
[1755]168        // input events
169        bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
170        bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
171        bool mouseMoved    (const OIS::MouseEvent    &arg);
172        bool keyPressed    (const OIS::KeyEvent      &arg);
173        bool keyReleased   (const OIS::KeyEvent      &arg);
174        bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
175        bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
176        bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
177        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
178        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
179        // don't remove that! Or else add OIS as dependency library to orxonox.
180        bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
[1219]181
[1755]182        void setConfigValues();
[1219]183
[1755]184    private: // variables
185        OIS::InputManager*                  inputSystem_;          //!< OIS input manager
186        OIS::Keyboard*                      keyboard_;             //!< OIS mouse
187        OIS::Mouse*                         mouse_;                //!< OIS keyboard
188        std::vector<OIS::JoyStick*>         joySticks_;            //!< OIS joy sticks
189        unsigned int                        joySticksSize_;
190        unsigned int                        devicesNum_;
191        size_t                              windowHnd_;            //!< Render window handle
192        InputManagerState                   internalState_;        //!< Current internal state
[1219]193
[1878]194        // some internally handled states and handlers
[1755]195        SimpleInputState*                   stateEmpty_;
[1788]196        ExtendedInputState*                 stateMaster_;          //!< Always active master input state
[1881]197        KeyDetector*                        keyDetector_;        //!< KeyDetector instance
198        InputBuffer*                        calibratorCallbackBuffer_;
[1502]199
[1755]200        std::map<std::string, InputState*>  inputStatesByName_;
201        std::map<int, InputState*>          inputStatesByPriority_;
[1219]202
[1755]203        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
204        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
205        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
[1502]206
[1755]207        std::map<int, InputState*>          activeStates_;
208        std::vector<InputState*>            activeStatesTop_;      //!< Current input states for joy stick events.
209        std::vector<InputState*>            activeStatesTicked_;   //!< Current input states for joy stick events.
[1349]210
[1755]211        // joystick calibration
212        //std::vector<int> marginalsMaxConfig_;
213        //std::vector<int> marginalsMinConfig_;
214        int                                 marginalsMax_[24];
215        int                                 marginalsMin_[24];
216        bool                                bCalibrated_;
217        bool                                bCalibrating_;
[1219]218
[1755]219        unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers.
220        std::vector<POVStates>              povStates_;            //!< Keeps track of the joy stick POV states.
221        std::vector<SliderStates>           sliderStates_;         //!< Keeps track of the possibly two slider axes.
222        std::vector<JoyStickCalibration>    joySticksCalibration_; 
[1219]223
[1755]224        std::vector<Key>                    keysDown_;
225        std::vector<MouseButton::Enum>      mouseButtonsDown_;
226        std::vector<std::vector<JoyStickButton::Enum> >  joyStickButtonsDown_;
[1219]227
[1755]228        static std::string                  bindingCommmandString_s;
229        static InputManager*                singletonRef_s;
230    };
[1878]231
232    /**
233    @brief
234        Creates a new InputState by type, name and priority.
235       
236        You will have to use this method because the
237        c'tors and d'tors are private.
238    @remarks
239        The InputManager will take care of the state completely. That also
240        means it gets deleted when the InputManager is destroyed!
241    @param name
242        Name of the InputState when referenced as string
243    @param priority
244        Priority matters when multiple states are active. You can specify any
245        number, but 1 - 99 is preferred (99 means high).
246    */
247    template <class T>
248    T* InputManager::createInputState(const std::string& name, int priority)
249    {
250        T* state = new T;
251        if (_configureInputState(state, name, priority))
252            return state;
253        else
254        {
255            delete state;
256            return 0;
257        }
258    }
[973]259}
260
261#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.