Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp3/src/core/input/InputManager.h @ 3014

Last change on this file since 3014 was 3014, checked in by scheusso, 15 years ago

console command grabMouse / ungrabMouse now working (used for a presentation hack)

  • Property svn:eol-style set to native
File size: 10.1 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 <vector>
43#include <stack>
44#include "util/Math.h"
45#include "util/OrxEnum.h"
46#include "core/OrxonoxClass.h"
47#include "InputInterfaces.h"
48
49namespace orxonox
50{
51    /**
52    @brief
53        Helper class to realise a vector<int[4]>
54    */
55    class POVStates
56    {
57    public:
58        int& operator[](unsigned int index) { return povStates[index]; }
59        int povStates[4];
60    };
61
62    /**
63    @brief
64        Helper class to realise a vector< {int[4], int[4]} >
65    */
66    class SliderStates
67    {
68    public:
69        IntVector2 sliderStates[4];
70    };
71
72    struct JoyStickCalibration
73    {
74        int middleValue[24];
75        float positiveCoeff[24];
76        float negativeCoeff[24];
77    };
78
79    struct InputStatePriority : OrxEnum<InputStatePriority>
80    {
81        OrxEnumConstructors(InputStatePriority);
82
83        static const int Empty        = -1;
84        static const int Dynamic      = 0;
85
86        static const int HighPriority = 1000;
87        static const int Console      = HighPriority + 0;
88        static const int Calibrator   = HighPriority + 1;
89        static const int Detector     = HighPriority + 2;
90    };
91
92    /**
93    @brief
94        Captures and distributes mouse and keyboard input.
95    */
96    class _CoreExport InputManager
97        : public OrxonoxClass,
98        public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
99    {
100        // --> setConfigValues is private
101        friend class ClassIdentifier<InputManager>;
102
103    public:
104        enum InputManagerState
105        {
106            Uninitialised    = 0x00,
107            OISReady         = 0x01,
108            InternalsReady   = 0x02,
109            Ticking          = 0x04,
110            Calibrating      = 0x08,
111            ReloadRequest    = 0x10,
112            JoyStickSupport  = 0x20 // used with ReloadRequest to store a bool
113        };
114
115        InputManager ();
116        ~InputManager();
117
118        void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true);
119
120        void reloadInputSystem(bool joyStickSupport = true);
121
122        void clearBuffers();
123
124        unsigned int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
125        unsigned int  numberOfMice()      { return mouse_    ? 1 : 0; }
126        unsigned int  numberOfJoySticks() { return joySticksSize_; }
127
128        void setWindowExtents(const int width, const int height);
129        void setKeyDetectorCallback(const std::string& command);
130
131        template <class T>
132        T* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
133
134        InputState* getState       (const std::string& name);
135        InputState* getCurrentState();
136        bool requestDestroyState   (const std::string& name);
137        bool requestEnterState     (const std::string& name);
138        bool requestLeaveState     (const std::string& name);
139
140#ifdef ORXONOX_PLATFORM_LINUX
141        // HACK!
142        static void grabMouse();
143        static void ungrabMouse();
144#endif
145
146        void update(const Clock& time);
147
148        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
149        static InputManager* getInstancePtr() { return singletonRef_s; }
150
151        // console commands
152        static void calibrate();
153        static void reload(bool joyStickSupport = true);
154
155    public: // variables
156        static EmptyHandler                 EMPTY_HANDLER;
157        static const unsigned int           sliderAxes = 8;
158
159    private: // functions
160        // don't mess with a Singleton
161        InputManager (const InputManager&);
162
163        // Intenal methods
164        void _initialiseKeyboard();
165        void _initialiseMouse();
166        void _initialiseJoySticks();
167        void _configureJoySticks();
168
169        void _loadCalibration();
170        void _startCalibration();
171        void _completeCalibration();
172        void _evaluateCalibration();
173
174        void _destroyKeyboard();
175        void _destroyMouse();
176        void _destroyJoySticks();
177        void _destroyState(InputState* state);
178        void _clearBuffers();
179
180        void _reload(bool joyStickSupport);
181
182        void _fireAxis(unsigned int iJoyStick, int axis, int value);
183        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
184
185        void _updateActiveStates();
186        bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority);
187
188        // input events
189        bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
190        bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
191        bool mouseMoved    (const OIS::MouseEvent    &arg);
192        bool keyPressed    (const OIS::KeyEvent      &arg);
193        bool keyReleased   (const OIS::KeyEvent      &arg);
194        bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
195        bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
196        bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
197        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
198        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
199        // don't remove that! Or else add OIS as dependency library to orxonox.
200        bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
201
202        void setConfigValues();
203        void _calibrationFileCallback();
204
205    private: // variables
206        OIS::InputManager*                  inputSystem_;          //!< OIS input manager
207        OIS::Keyboard*                      keyboard_;             //!< OIS mouse
208        OIS::Mouse*                         mouse_;                //!< OIS keyboard
209        std::vector<OIS::JoyStick*>         joySticks_;            //!< OIS joy sticks
210        unsigned int                        joySticksSize_;
211        std::vector<std::string>            joyStickIDs_;          //!< Execution unique identification strings for the joy sticks
212        unsigned int                        devicesNum_;
213        size_t                              windowHnd_;            //!< Render window handle
214        InputManagerState                   internalState_;        //!< Current internal state
215
216        // some internally handled states and handlers
217        SimpleInputState*                   stateEmpty_;
218        KeyDetector*                        keyDetector_;          //!< KeyDetector instance
219        InputBuffer*                        calibratorCallbackBuffer_;
220
221        std::map<std::string, InputState*>  inputStatesByName_;
222
223        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
224        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
225        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
226
227        std::map<int, InputState*>          activeStates_;
228        std::vector<std::vector<InputState*> > activeStatesTriggered_;
229        std::vector<InputState*>            activeStatesTicked_;
230
231        // joystick calibration
232        std::vector<std::vector<int> >      joyStickMinValues_;
233        std::vector<std::vector<int> >      joyStickMaxValues_;
234        std::vector<std::vector<int> >      joyStickMiddleValues_;
235        std::vector<ConfigValueContainer*>  calibrationConfigValueContainers_;
236        std::vector<JoyStickCalibration>    joyStickCalibrations_; 
237
238        unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers.
239        std::vector<POVStates>              povStates_;            //!< Keeps track of the joy stick POV states.
240        std::vector<SliderStates>           sliderStates_;         //!< Keeps track of the possibly two slider axes.
241
242        std::vector<Key>                    keysDown_;
243        std::vector<MouseButtonCode::ByEnum>      mouseButtonsDown_;
244        std::vector<std::vector<JoyStickButtonCode::ByEnum> >  joyStickButtonsDown_;
245
246        // ConfigValues
247        std::string                         calibrationFilename_;  //!< Joy stick calibration ini filename
248
249        static InputManager*                singletonRef_s;
250    };
251
252    /**
253    @brief
254        Creates a new InputState by type, name and priority.
255       
256        You will have to use this method because the
257        c'tors and d'tors are private.
258    @remarks
259        The InputManager will take care of the state completely. That also
260        means it gets deleted when the InputManager is destroyed!
261    @param name
262        Name of the InputState when referenced as string
263    @param priority
264        Priority matters when multiple states are active. You can specify any
265        number, but 1 - 99 is preferred (99 means high).
266    */
267    template <class T>
268    T* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority)
269    {
270        T* state = new T;
271        if (_configureInputState(state, name, bAlwaysGetsInput, bTransparent, priority))
272            return state;
273        else
274        {
275            delete state;
276            return 0;
277        }
278    }
279}
280
281#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.