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 Implementation of a little Input handler that distributes everything |
---|
32 | coming from OIS. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _InputManager_H__ |
---|
36 | #define _InputManager_H__ |
---|
37 | |
---|
38 | #include "core/CorePrereqs.h" |
---|
39 | |
---|
40 | #include <map> |
---|
41 | #include <vector> |
---|
42 | |
---|
43 | #include "ois/OIS.h" |
---|
44 | #include "util/Math.h" |
---|
45 | #include "core/OrxonoxClass.h" |
---|
46 | #include "InputInterfaces.h" |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | /** |
---|
51 | * Helper class to realise a vector<int[4]> |
---|
52 | */ |
---|
53 | class POVStates |
---|
54 | { |
---|
55 | public: |
---|
56 | int operator[](unsigned int index) { return povStates[index]; } |
---|
57 | int povStates[4]; |
---|
58 | }; |
---|
59 | |
---|
60 | /** |
---|
61 | * Helper class to realise a vector< {int[4], int[4]} > |
---|
62 | */ |
---|
63 | class SliderStates |
---|
64 | { |
---|
65 | public: |
---|
66 | IntVector2 sliderStates[4]; |
---|
67 | }; |
---|
68 | |
---|
69 | /** |
---|
70 | * Struct for storing a custom input state |
---|
71 | */ |
---|
72 | struct StoredState |
---|
73 | { |
---|
74 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
75 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
76 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
77 | std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; |
---|
78 | }; |
---|
79 | |
---|
80 | struct JoyStickCalibration |
---|
81 | { |
---|
82 | int zeroStates[24]; |
---|
83 | float positiveCoeff[24]; |
---|
84 | float negativeCoeff[24]; |
---|
85 | }; |
---|
86 | |
---|
87 | /** |
---|
88 | @brief Captures and distributes mouse and keyboard input. |
---|
89 | */ |
---|
90 | class _CoreExport InputManager |
---|
91 | : public OrxonoxClass, |
---|
92 | public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener |
---|
93 | { |
---|
94 | public: // enumerations |
---|
95 | /** |
---|
96 | @brief Designates the way input is handled and redirected. |
---|
97 | */ |
---|
98 | enum InputState |
---|
99 | { |
---|
100 | IS_UNINIT, //!< InputManager has not yet been initialised. |
---|
101 | IS_NONE, //!< Input is discarded. |
---|
102 | IS_NORMAL, //!< Normal play state. Key and button bindings are active. |
---|
103 | IS_GUI, //!< All OIS input events are passed to CEGUI. |
---|
104 | IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. |
---|
105 | IS_DETECT, //!< All the input additionally goes to the KeyDetector |
---|
106 | IS_NODETECT, //!< remove KeyDetector |
---|
107 | IS_NOCALIBRATE, |
---|
108 | IS_CALIBRATE, |
---|
109 | IS_CUSTOM //!< Any possible configuration. |
---|
110 | }; |
---|
111 | |
---|
112 | public: // member functions |
---|
113 | void setConfigValues(); |
---|
114 | |
---|
115 | public: // static functions |
---|
116 | static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
117 | bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); |
---|
118 | static bool initialiseKeyboard(); |
---|
119 | static bool initialiseMouse(); |
---|
120 | static bool initialiseJoySticks(); |
---|
121 | static int numberOfKeyboards(); |
---|
122 | static int numberOfMice(); |
---|
123 | static int numberOfJoySticks(); |
---|
124 | |
---|
125 | static void destroy(); |
---|
126 | static void destroyKeyboard(); |
---|
127 | static void destroyMouse(); |
---|
128 | static void destroyJoySticks(); |
---|
129 | |
---|
130 | //static bool isModifierDown(KeyboardModifier::Enum modifier); |
---|
131 | //static bool isKeyDown(KeyCode::Enum key); |
---|
132 | //static const MouseState getMouseState(); |
---|
133 | //static const JoyStickState getJoyStickState(unsigned int ID); |
---|
134 | |
---|
135 | static void setWindowExtents(const int width, const int height); |
---|
136 | |
---|
137 | static void setInputState(const InputState state); |
---|
138 | static InputState getInputState(); |
---|
139 | |
---|
140 | static void storeKeyStroke(const std::string& name); |
---|
141 | static void keyBind(const std::string& command); |
---|
142 | |
---|
143 | static void calibrate(); |
---|
144 | |
---|
145 | static void tick(float dt); |
---|
146 | |
---|
147 | static bool addKeyHandler (KeyHandler* handler, const std::string& name); |
---|
148 | static bool removeKeyHandler (const std::string& name); |
---|
149 | static KeyHandler* getKeyHandler (const std::string& name); |
---|
150 | static bool enableKeyHandler (const std::string& name); |
---|
151 | static bool disableKeyHandler (const std::string& name); |
---|
152 | static bool isKeyHandlerActive (const std::string& name); |
---|
153 | |
---|
154 | static bool addMouseHandler (MouseHandler* handler, const std::string& name); |
---|
155 | static bool removeMouseHandler (const std::string& name); |
---|
156 | static MouseHandler* getMouseHandler (const std::string& name); |
---|
157 | static bool enableMouseHandler (const std::string& name); |
---|
158 | static bool disableMouseHandler (const std::string& name); |
---|
159 | static bool isMouseHandlerActive (const std::string& name); |
---|
160 | |
---|
161 | static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); |
---|
162 | static bool removeJoyStickHandler (const std::string& name); |
---|
163 | static JoyStickHandler* getJoyStickHandler(const std::string& name); |
---|
164 | static bool enableJoyStickHandler (const std::string& name, unsigned int id); |
---|
165 | static bool disableJoyStickHandler (const std::string& name, unsigned int id); |
---|
166 | static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); |
---|
167 | |
---|
168 | private: // functions |
---|
169 | // don't mess with a Singleton |
---|
170 | InputManager (); |
---|
171 | InputManager (const InputManager&); |
---|
172 | ~InputManager(); |
---|
173 | |
---|
174 | // Intenal methods |
---|
175 | bool _initialise(const size_t, int, int, bool, bool, bool); |
---|
176 | bool _initialiseKeyboard(); |
---|
177 | bool _initialiseMouse(); |
---|
178 | bool _initialiseJoySticks(); |
---|
179 | |
---|
180 | void _destroy(); |
---|
181 | void _destroyKeyboard(); |
---|
182 | void _destroyMouse(); |
---|
183 | void _destroyJoySticks(); |
---|
184 | |
---|
185 | void _updateTickables(); |
---|
186 | |
---|
187 | void _saveState(); |
---|
188 | void _restoreState(); |
---|
189 | |
---|
190 | void _completeCalibration(); |
---|
191 | |
---|
192 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
193 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
194 | |
---|
195 | void _tick(float dt); |
---|
196 | |
---|
197 | // input events |
---|
198 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
199 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
200 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
201 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
202 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
203 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
204 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
205 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
206 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
207 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
208 | //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); |
---|
209 | |
---|
210 | static InputManager& _getSingleton(); |
---|
211 | static InputManager* _getSingletonPtr() { return &_getSingleton(); } |
---|
212 | |
---|
213 | private: // variables |
---|
214 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
215 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
216 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
217 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
218 | unsigned int joySticksSize_; |
---|
219 | |
---|
220 | KeyBinder* keyBinder_; //!< KeyBinder instance |
---|
221 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
222 | InputBuffer* buffer_; //!< InputBuffer instance |
---|
223 | CalibratorCallback* calibratorCallback_; |
---|
224 | |
---|
225 | InputState state_; |
---|
226 | InputState stateRequest_; |
---|
227 | InputState savedState_; |
---|
228 | unsigned int keyboardModifiers_; |
---|
229 | StoredState savedHandlers_; |
---|
230 | |
---|
231 | // joystick calibration |
---|
232 | //std::vector<int> marginalsMaxConfig_; |
---|
233 | //std::vector<int> marginalsMinConfig_; |
---|
234 | int marginalsMax_[24]; |
---|
235 | int marginalsMin_[24]; |
---|
236 | bool bCalibrated_; |
---|
237 | |
---|
238 | //! Keeps track of the joy stick POV states |
---|
239 | std::vector<POVStates> povStates_; |
---|
240 | //! Keeps track of the possibly two slider axes |
---|
241 | std::vector<SliderStates> sliderStates_; |
---|
242 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
243 | |
---|
244 | std::map<std::string, KeyHandler*> keyHandlers_; |
---|
245 | std::map<std::string, MouseHandler*> mouseHandlers_; |
---|
246 | std::map<std::string, JoyStickHandler*> joyStickHandlers_; |
---|
247 | |
---|
248 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
249 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
250 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
251 | std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; |
---|
252 | |
---|
253 | std::vector<Key> keysDown_; |
---|
254 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
255 | std::vector<std::vector<int> > joyStickButtonsDown_; |
---|
256 | |
---|
257 | static std::string bindingCommmandString_s; |
---|
258 | }; |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | #endif /* _InputManager_H__ */ |
---|