Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3196 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
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 */
28
29/**
30@file
31@brief
32    Implementation of a little Input handler that distributes everything
33    coming from OIS.
34*/
35
36#ifndef _InputManager_H__
37#define _InputManager_H__
38
39#include "core/CorePrereqs.h"
40
41#include <map>
42#include <set>
43#include <string>
44#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"
53
54namespace orxonox
55{
56    /**
57    @brief
58        Helper class to realise a vector<int[4]>
59    */
60    class POVStates
61    {
62    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
110        {
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
118        };
119
120        InputManager ();
121        ~InputManager();
122
123        void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true);
124
125        void reloadInputSystem(bool joyStickSupport = true);
126
127        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);
134        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
164    private: // functions
165        // don't mess with a Singleton
166        InputManager (const InputManager&);
167
168        // 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();
209
210    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
220
221        // some internally handled states and handlers
222        SimpleInputState*                   stateEmpty_;
223        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;
255    };
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    }
284}
285
286#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.