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 "core/OrxonoxClass.h" |
---|
46 | #include "InputInterfaces.h" |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | /** |
---|
51 | @brief |
---|
52 | Helper class to realise a vector<int[4]> |
---|
53 | */ |
---|
54 | class POVStates |
---|
55 | { |
---|
56 | public: |
---|
57 | int& operator[](unsigned int index) { return povStates[index]; } |
---|
58 | int povStates[4]; |
---|
59 | }; |
---|
60 | |
---|
61 | /** |
---|
62 | @brief |
---|
63 | Helper class to realise a vector< {int[4], int[4]} > |
---|
64 | */ |
---|
65 | class SliderStates |
---|
66 | { |
---|
67 | public: |
---|
68 | IntVector2 sliderStates[4]; |
---|
69 | }; |
---|
70 | |
---|
71 | struct JoyStickCalibration |
---|
72 | { |
---|
73 | int zeroStates[24]; |
---|
74 | float positiveCoeff[24]; |
---|
75 | float negativeCoeff[24]; |
---|
76 | }; |
---|
77 | |
---|
78 | /** |
---|
79 | @brief |
---|
80 | Captures and distributes mouse and keyboard input. |
---|
81 | */ |
---|
82 | class _CoreExport InputManager |
---|
83 | : public OrxonoxClass, |
---|
84 | public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener |
---|
85 | { |
---|
86 | // --> setConfigValues is private |
---|
87 | friend class ClassIdentifier<InputManager>; |
---|
88 | // let Core class use tick(.) |
---|
89 | friend class Core; |
---|
90 | |
---|
91 | public: |
---|
92 | enum InputManagerState |
---|
93 | { |
---|
94 | Uninitialised = 0x00, |
---|
95 | OISReady = 0x01, |
---|
96 | InternalsReady = 0x02, |
---|
97 | Ticking = 0x04, |
---|
98 | Calibrating = 0x08, |
---|
99 | ReloadRequest = 0x10, |
---|
100 | JoyStickSupport = 0x20 // used with ReloadRequest to store a bool |
---|
101 | }; |
---|
102 | |
---|
103 | InputManager (); |
---|
104 | ~InputManager(); |
---|
105 | |
---|
106 | void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); |
---|
107 | |
---|
108 | void reloadInputSystem(bool joyStickSupport = true); |
---|
109 | |
---|
110 | void clearBuffers(); |
---|
111 | |
---|
112 | unsigned int numberOfKeyboards() { return keyboard_ ? 1 : 0; } |
---|
113 | unsigned int numberOfMice() { return mouse_ ? 1 : 0; } |
---|
114 | unsigned int numberOfJoySticks() { return joySticksSize_; } |
---|
115 | |
---|
116 | void setWindowExtents(const int width, const int height); |
---|
117 | void setKeyDetectorCallback(const std::string& command); |
---|
118 | |
---|
119 | template <class T> |
---|
120 | T* createInputState(const std::string& name, int priority); |
---|
121 | |
---|
122 | InputState* getState (const std::string& name); |
---|
123 | InputState* getCurrentState(); |
---|
124 | ExtendedInputState* getMasterInputState() { return this->stateMaster_; } |
---|
125 | bool requestDestroyState (const std::string& name); |
---|
126 | bool requestEnterState (const std::string& name); |
---|
127 | bool requestLeaveState (const std::string& name); |
---|
128 | |
---|
129 | void tick(float dt); |
---|
130 | |
---|
131 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
132 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
133 | |
---|
134 | // console commands |
---|
135 | static void calibrate(); |
---|
136 | static void reload(bool joyStickSupport = true); |
---|
137 | |
---|
138 | public: // variables |
---|
139 | static EmptyHandler EMPTY_HANDLER; |
---|
140 | |
---|
141 | private: // functions |
---|
142 | // don't mess with a Singleton |
---|
143 | InputManager (const InputManager&); |
---|
144 | |
---|
145 | // Intenal methods |
---|
146 | void _initialiseKeyboard(); |
---|
147 | void _initialiseMouse(); |
---|
148 | void _initialiseJoySticks(); |
---|
149 | void _configureNumberOfJoySticks(); |
---|
150 | |
---|
151 | void _destroyKeyboard(); |
---|
152 | void _destroyMouse(); |
---|
153 | void _destroyJoySticks(); |
---|
154 | void _destroyState(InputState* state); |
---|
155 | void _clearBuffers(); |
---|
156 | |
---|
157 | void _reload(bool joyStickSupport); |
---|
158 | |
---|
159 | void _completeCalibration(); |
---|
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, 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 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
174 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
175 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
176 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
177 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
178 | // don't remove that! Or else add OIS as dependency library to orxonox. |
---|
179 | bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } |
---|
180 | |
---|
181 | void setConfigValues(); |
---|
182 | |
---|
183 | private: // variables |
---|
184 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
185 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
186 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
187 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
188 | unsigned int joySticksSize_; |
---|
189 | unsigned int devicesNum_; |
---|
190 | size_t windowHnd_; //!< Render window handle |
---|
191 | InputManagerState internalState_; //!< Current internal state |
---|
192 | |
---|
193 | // some internally handled states and handlers |
---|
194 | SimpleInputState* stateEmpty_; |
---|
195 | ExtendedInputState* stateMaster_; //!< Always active master input state |
---|
196 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
197 | InputBuffer* calibratorCallbackBuffer_; |
---|
198 | |
---|
199 | std::map<std::string, InputState*> inputStatesByName_; |
---|
200 | std::map<int, InputState*> inputStatesByPriority_; |
---|
201 | |
---|
202 | std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state |
---|
203 | std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state |
---|
204 | std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state |
---|
205 | |
---|
206 | std::map<int, InputState*> activeStates_; |
---|
207 | std::vector<InputState*> activeStatesTop_; //!< Current input states for joy stick events. |
---|
208 | std::vector<InputState*> activeStatesTicked_; //!< Current input states for joy stick events. |
---|
209 | |
---|
210 | // joystick calibration |
---|
211 | //std::vector<int> marginalsMaxConfig_; |
---|
212 | //std::vector<int> marginalsMinConfig_; |
---|
213 | int marginalsMax_[24]; |
---|
214 | int marginalsMin_[24]; |
---|
215 | bool bCalibrated_; |
---|
216 | bool bCalibrating_; |
---|
217 | |
---|
218 | unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. |
---|
219 | std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. |
---|
220 | std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. |
---|
221 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
222 | |
---|
223 | std::vector<Key> keysDown_; |
---|
224 | std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_; |
---|
225 | std::vector<std::vector<JoyStickButtonCode::ByEnum> > joyStickButtonsDown_; |
---|
226 | |
---|
227 | static std::string bindingCommmandString_s; |
---|
228 | static InputManager* singletonRef_s; |
---|
229 | }; |
---|
230 | |
---|
231 | /** |
---|
232 | @brief |
---|
233 | Creates a new InputState by type, name and priority. |
---|
234 | |
---|
235 | You will have to use this method because the |
---|
236 | c'tors and d'tors are private. |
---|
237 | @remarks |
---|
238 | The InputManager will take care of the state completely. That also |
---|
239 | means it gets deleted when the InputManager is destroyed! |
---|
240 | @param name |
---|
241 | Name of the InputState when referenced as string |
---|
242 | @param priority |
---|
243 | Priority matters when multiple states are active. You can specify any |
---|
244 | number, but 1 - 99 is preferred (99 means high). |
---|
245 | */ |
---|
246 | template <class T> |
---|
247 | T* InputManager::createInputState(const std::string& name, int priority) |
---|
248 | { |
---|
249 | T* state = new T; |
---|
250 | if (_configureInputState(state, name, priority)) |
---|
251 | return state; |
---|
252 | else |
---|
253 | { |
---|
254 | delete state; |
---|
255 | return 0; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | #endif /* _InputManager_H__ */ |
---|