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 "CorePrereqs.h" |
---|
39 | |
---|
40 | #include <map> |
---|
41 | #include <vector> |
---|
42 | |
---|
43 | #include "ois/OIS.h" |
---|
44 | #include "util/Math.h" |
---|
45 | #include "Tickable.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 TickableReal, |
---|
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 bool addKeyHandler (KeyHandler* handler, const std::string& name); |
---|
146 | static bool removeKeyHandler (const std::string& name); |
---|
147 | static KeyHandler* getKeyHandler (const std::string& name); |
---|
148 | static bool enableKeyHandler (const std::string& name); |
---|
149 | static bool disableKeyHandler (const std::string& name); |
---|
150 | static bool isKeyHandlerActive (const std::string& name); |
---|
151 | |
---|
152 | static bool addMouseHandler (MouseHandler* handler, const std::string& name); |
---|
153 | static bool removeMouseHandler (const std::string& name); |
---|
154 | static MouseHandler* getMouseHandler (const std::string& name); |
---|
155 | static bool enableMouseHandler (const std::string& name); |
---|
156 | static bool disableMouseHandler (const std::string& name); |
---|
157 | static bool isMouseHandlerActive (const std::string& name); |
---|
158 | |
---|
159 | static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); |
---|
160 | static bool removeJoyStickHandler (const std::string& name); |
---|
161 | static JoyStickHandler* getJoyStickHandler(const std::string& name); |
---|
162 | static bool enableJoyStickHandler (const std::string& name, unsigned int id); |
---|
163 | static bool disableJoyStickHandler (const std::string& name, unsigned int id); |
---|
164 | static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); |
---|
165 | |
---|
166 | private: // functions |
---|
167 | // don't mess with a Singleton |
---|
168 | InputManager (); |
---|
169 | InputManager (const InputManager&); |
---|
170 | ~InputManager(); |
---|
171 | |
---|
172 | // Intenal methods |
---|
173 | bool _initialise(const size_t, int, int, bool, bool, bool); |
---|
174 | bool _initialiseKeyboard(); |
---|
175 | bool _initialiseMouse(); |
---|
176 | bool _initialiseJoySticks(); |
---|
177 | |
---|
178 | void _destroy(); |
---|
179 | void _destroyKeyboard(); |
---|
180 | void _destroyMouse(); |
---|
181 | void _destroyJoySticks(); |
---|
182 | |
---|
183 | void _updateTickables(); |
---|
184 | |
---|
185 | void _saveState(); |
---|
186 | void _restoreState(); |
---|
187 | |
---|
188 | void _completeCalibration(); |
---|
189 | |
---|
190 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
191 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
192 | |
---|
193 | void tick(float dt); |
---|
194 | |
---|
195 | // input events |
---|
196 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
197 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
198 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
199 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
200 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
201 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
202 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
203 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
204 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
205 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
206 | //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); |
---|
207 | |
---|
208 | static InputManager& _getSingleton(); |
---|
209 | static InputManager* _getSingletonPtr() { return &_getSingleton(); } |
---|
210 | |
---|
211 | private: // variables |
---|
212 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
213 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
214 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
215 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
216 | unsigned int joySticksSize_; |
---|
217 | |
---|
218 | KeyBinder* keyBinder_; //!< KeyBinder instance |
---|
219 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
220 | InputBuffer* buffer_; //!< InputBuffer instance |
---|
221 | CalibratorCallback* calibratorCallback_; |
---|
222 | |
---|
223 | InputState state_; |
---|
224 | InputState stateRequest_; |
---|
225 | InputState savedState_; |
---|
226 | unsigned int keyboardModifiers_; |
---|
227 | StoredState savedHandlers_; |
---|
228 | |
---|
229 | // joystick calibration |
---|
230 | //std::vector<int> marginalsMaxConfig_; |
---|
231 | //std::vector<int> marginalsMinConfig_; |
---|
232 | int marginalsMax_[24]; |
---|
233 | int marginalsMin_[24]; |
---|
234 | bool bCalibrated_; |
---|
235 | |
---|
236 | //! Keeps track of the joy stick POV states |
---|
237 | std::vector<POVStates> povStates_; |
---|
238 | //! Keeps track of the possibly two slider axes |
---|
239 | std::vector<SliderStates> sliderStates_; |
---|
240 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
241 | |
---|
242 | std::map<std::string, KeyHandler*> keyHandlers_; |
---|
243 | std::map<std::string, MouseHandler*> mouseHandlers_; |
---|
244 | std::map<std::string, JoyStickHandler*> joyStickHandlers_; |
---|
245 | |
---|
246 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
247 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
248 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
249 | std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; |
---|
250 | |
---|
251 | std::vector<Key> keysDown_; |
---|
252 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
253 | std::vector<std::vector<int> > joyStickButtonsDown_; |
---|
254 | |
---|
255 | static std::string bindingCommmandString_s; |
---|
256 | }; |
---|
257 | |
---|
258 | } |
---|
259 | |
---|
260 | #endif /* _InputManager_H__ */ |
---|