Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/input/InputManager.h @ 3271

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

Extracted joy stick related code from InputManager to a new JoyStick class in order to make the InputManger less of a monster class and to apply a little bit more OO.

  • Property svn:eol-style set to native
File size: 8.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    struct InputStatePriority : OrxEnum<InputStatePriority>
57    {
58        OrxEnumConstructors(InputStatePriority);
59
60        static const int Empty        = -1;
61        static const int Dynamic      = 0;
62
63        static const int HighPriority = 1000;
64        static const int Console      = HighPriority + 0;
65        static const int Calibrator   = HighPriority + 1;
66        static const int Detector     = HighPriority + 2;
67    };
68
69    /**
70    @brief
71        Captures and distributes mouse and keyboard input.
72    */
73    class _CoreExport InputManager
74        : public OrxonoxClass,
75        public OIS::KeyListener, public OIS::MouseListener
76    {
77        // --> setConfigValues is private
78        friend class ClassIdentifier<InputManager>;
79        friend class JoyStick;
80
81    public:
82        enum InputManagerState
83        {
84            Uninitialised    = 0x00,
85            OISReady         = 0x01,
86            InternalsReady   = 0x02,
87            Ticking          = 0x04,
88            Calibrating      = 0x08,
89            ReloadRequest    = 0x10,
90        };
91
92        InputManager (size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight);
93        ~InputManager();
94
95        void initialise(size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight);
96
97        void reloadInputSystem();
98
99        void clearBuffers();
100
101        unsigned int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
102        unsigned int  numberOfMice()      { return mouse_    ? 1 : 0; }
103        unsigned int  numberOfJoySticks() { return joySticks_.size(); }
104
105        void setWindowExtents(const int width, const int height);
106        void setKeyDetectorCallback(const std::string& command);
107
108        template <class T>
109        T* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic);
110
111        InputState* getState       (const std::string& name);
112        InputState* getCurrentState();
113        bool requestDestroyState   (const std::string& name);
114        bool requestEnterState     (const std::string& name);
115        bool requestLeaveState     (const std::string& name);
116
117#ifdef ORXONOX_PLATFORM_LINUX
118        // HACK!
119        static void grabMouse();
120        static void ungrabMouse();
121#endif
122
123        void update(const Clock& time);
124
125        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
126        static InputManager* getInstancePtr() { return singletonRef_s; }
127
128        // console commands
129        static void calibrate();
130        static void reload();
131
132    public: // variables
133        static EmptyHandler                 EMPTY_HANDLER;
134
135    private: // functions for friends
136        OIS::InputManager* getInputSystem() { return this->inputSystem_; }
137        bool checkJoyStickID(const std::string&);
138
139    private: // functions
140        // don't mess with a Singleton
141        InputManager (const InputManager&);
142
143        // Intenal methods
144        void _initialiseKeyboard();
145        void _initialiseMouse(unsigned int windowWidth, unsigned int windowHeight);
146        void _initialiseJoySticks();
147        void _configureJoySticks();
148
149        void _loadCalibration();
150        void _startCalibration();
151        void _completeCalibration();
152        void _evaluateCalibration();
153
154        void _destroyKeyboard();
155        void _destroyMouse();
156        void _destroyJoySticks();
157        void _destroyState(InputState* state);
158
159        void _reload();
160
161        void _fireAxis(unsigned int iJoyStick, int axis, int value);
162        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
163
164        void _updateActiveStates();
165        bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority);
166
167        // input events
168        bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
169        bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id);
170        bool mouseMoved    (const OIS::MouseEvent    &arg);
171        bool keyPressed    (const OIS::KeyEvent      &arg);
172        bool keyReleased   (const OIS::KeyEvent      &arg);
173
174        void setConfigValues();
175        void _calibrationFileCallback();
176
177    private: // variables
178        OIS::InputManager*                  inputSystem_;          //!< OIS input manager
179        OIS::Keyboard*                      keyboard_;             //!< OIS mouse
180        OIS::Mouse*                         mouse_;                //!< OIS keyboard
181        std::vector<JoyStick*>              joySticks_;            //!< Orxonox joy sticks
182        unsigned int                        devicesNum_;
183        size_t                              windowHnd_;            //!< Render window handle
184        InputManagerState                   internalState_;        //!< Current internal state
185
186        // some internally handled states and handlers
187        SimpleInputState*                   stateEmpty_;
188        KeyDetector*                        keyDetector_;          //!< KeyDetector instance
189        InputBuffer*                        calibratorCallbackBuffer_;
190
191        std::map<std::string, InputState*>  inputStatesByName_;
192
193        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
194        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
195        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
196
197        std::map<int, InputState*>          activeStates_;
198        std::vector<std::vector<InputState*> > activeStatesTriggered_;
199        std::vector<InputState*>            activeStatesTicked_;
200
201        unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers.
202
203        std::vector<Key>                    keysDown_;
204        std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_;
205
206        static InputManager*                singletonRef_s;
207    };
208
209    /**
210    @brief
211        Creates a new InputState by type, name and priority.
212       
213        You will have to use this method because the
214        c'tors and d'tors are private.
215    @remarks
216        The InputManager will take care of the state completely. That also
217        means it gets deleted when the InputManager is destroyed!
218    @param name
219        Name of the InputState when referenced as string
220    @param priority
221        Priority matters when multiple states are active. You can specify any
222        number, but 1 - 99 is preferred (99 means high).
223    */
224    template <class T>
225    T* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority)
226    {
227        T* state = new T;
228        if (_configureInputState(state, name, bAlwaysGetsInput, bTransparent, priority))
229            return state;
230        else
231        {
232            delete state;
233            return 0;
234        }
235    }
236}
237
238#endif /* _InputManager_H__ */
Note: See TracBrowser for help on using the repository browser.