Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/input/InputManager.h @ 2814

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

InputManager upgrade number one: InputState priorities are managed automatically. However you can still use the enum in InputManager.h and get yourself a high priority.
High priorities are only to be used for special cases like calibrator, console or detector.
All other states are simply pushed onto the normal stack.

  • 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, InputStatePriority priority = InputStatePriority::Dynamic);
133
134        InputState* getState       (const std::string& name);
135        InputState* getCurrentState();
136        ExtendedInputState* getMasterInputState() { return this->stateMaster_; }
137        bool requestDestroyState   (const std::string& name);
138        bool requestEnterState     (const std::string& name);
139        bool requestLeaveState     (const std::string& name);
140
141        void update(const Clock& time);
142
143        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
144        static InputManager* getInstancePtr() { return singletonRef_s; }
145
146        // console commands
147        static void calibrate();
148        static void reload(bool joyStickSupport = true);
149
150    public: // variables
151        static EmptyHandler                 EMPTY_HANDLER;
152        static const unsigned int           sliderAxes = 8;
153
154    private: // functions
155        // don't mess with a Singleton
156        InputManager (const InputManager&);
157
158        // Intenal methods
159        void _initialiseKeyboard();
160        void _initialiseMouse();
161        void _initialiseJoySticks();
162        void _configureJoySticks();
163
164        void _loadCalibration();
165        void _startCalibration();
166        void _completeCalibration();
167        void _evaluateCalibration();
168
169        void _destroyKeyboard();
170        void _destroyMouse();
171        void _destroyJoySticks();
172        void _destroyState(InputState* state);
173        void _clearBuffers();
174
175        void _reload(bool joyStickSupport);
176
177        void _fireAxis(unsigned int iJoyStick, int axis, int value);
178        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
179
180        void _updateActiveStates();
181        bool _configureInputState(InputState* state, const std::string& name, int priority);
182
183        // input events
184        bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
185        bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
186        bool mouseMoved    (const OIS::MouseEvent    &arg);
187        bool keyPressed    (const OIS::KeyEvent      &arg);
188        bool keyReleased   (const OIS::KeyEvent      &arg);
189        bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
190        bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
191        bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
192        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
193        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
194        // don't remove that! Or else add OIS as dependency library to orxonox.
195        bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
196
197        void setConfigValues();
198        void _calibrationFileCallback();
199
200    private: // variables
201        OIS::InputManager*                  inputSystem_;          //!< OIS input manager
202        OIS::Keyboard*                      keyboard_;             //!< OIS mouse
203        OIS::Mouse*                         mouse_;                //!< OIS keyboard
204        std::vector<OIS::JoyStick*>         joySticks_;            //!< OIS joy sticks
205        unsigned int                        joySticksSize_;
206        std::vector<std::string>            joyStickIDs_;          //!< Execution unique identification strings for the joy sticks
207        unsigned int                        devicesNum_;
208        size_t                              windowHnd_;            //!< Render window handle
209        InputManagerState                   internalState_;        //!< Current internal state
210
211        // some internally handled states and handlers
212        SimpleInputState*                   stateEmpty_;
213        ExtendedInputState*                 stateMaster_;          //!< Always active master input state
214        KeyDetector*                        keyDetector_;          //!< KeyDetector instance
215        InputBuffer*                        calibratorCallbackBuffer_;
216
217        std::map<std::string, InputState*>  inputStatesByName_;
218
219        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
220        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
221        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
222
223        std::map<int, InputState*>          activeStates_;
224        std::vector<InputState*>            activeStatesTop_;      //!< Current input states for joy stick events.
225        std::vector<InputState*>            activeStatesTicked_;   //!< Current input states for joy stick events.
226
227        // joystick calibration
228        std::vector<std::vector<int> >      joyStickMinValues_;
229        std::vector<std::vector<int> >      joyStickMaxValues_;
230        std::vector<std::vector<int> >      joyStickMiddleValues_;
231        std::vector<ConfigValueContainer*>  calibrationConfigValueContainers_;
232        std::vector<JoyStickCalibration>    joyStickCalibrations_; 
233
234        unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers.
235        std::vector<POVStates>              povStates_;            //!< Keeps track of the joy stick POV states.
236        std::vector<SliderStates>           sliderStates_;         //!< Keeps track of the possibly two slider axes.
237
238        std::vector<Key>                    keysDown_;
239        std::vector<MouseButtonCode::ByEnum>      mouseButtonsDown_;
240        std::vector<std::vector<JoyStickButtonCode::ByEnum> >  joyStickButtonsDown_;
241
242        // ConfigValues
243        std::string                         calibrationFilename_;  //!< Joy stick calibration ini filename
244
245        static InputManager*                singletonRef_s;
246    };
247
248    /**
249    @brief
250        Creates a new InputState by type, name and priority.
251       
252        You will have to use this method because the
253        c'tors and d'tors are private.
254    @remarks
255        The InputManager will take care of the state completely. That also
256        means it gets deleted when the InputManager is destroyed!
257    @param name
258        Name of the InputState when referenced as string
259    @param priority
260        Priority matters when multiple states are active. You can specify any
261        number, but 1 - 99 is preferred (99 means high).
262    */
263    template <class T>
264    T* InputManager::createInputState(const std::string& name, InputStatePriority priority)
265    {
266        T* state = new T;
267        if (_configureInputState(state, name, priority))
268            return state;
269        else
270        {
271            delete state;
272            return 0;
273        }
274    }
275}
276
277#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.