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 | int numberOfKeyboards() { return keyboard_ ? 1 : 0; } |
---|
111 | int numberOfMice() { return mouse_ ? 1 : 0; } |
---|
112 | int numberOfJoySticks() { return joySticksSize_; } |
---|
113 | |
---|
114 | void setWindowExtents(const int width, const int height); |
---|
115 | |
---|
116 | template <class T> |
---|
117 | T* createInputState(const std::string& name, int priority) |
---|
118 | { |
---|
119 | T* state = new T; |
---|
120 | if (_configureInputState(state, name, priority)) |
---|
121 | return state; |
---|
122 | else |
---|
123 | { |
---|
124 | delete state; |
---|
125 | return 0; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | InputState* getState (const std::string& name); |
---|
130 | InputState* getCurrentState(); |
---|
131 | bool requestDestroyState (const std::string& name); |
---|
132 | bool requestEnterState (const std::string& name); |
---|
133 | bool requestLeaveState (const std::string& name); |
---|
134 | |
---|
135 | void tick(float dt); |
---|
136 | |
---|
137 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
138 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
139 | |
---|
140 | public: // console commands |
---|
141 | static void storeKeyStroke(const std::string& name); |
---|
142 | static void keyBind(const std::string& command); |
---|
143 | static void calibrate(); |
---|
144 | static void reload(bool joyStickSupport = true); |
---|
145 | |
---|
146 | private: // functions |
---|
147 | // don't mess with a Singleton |
---|
148 | InputManager (const InputManager&); |
---|
149 | |
---|
150 | // Intenal methods |
---|
151 | void _initialiseKeyboard(); |
---|
152 | void _initialiseMouse(); |
---|
153 | void _initialiseJoySticks(); |
---|
154 | void _redimensionLists(); |
---|
155 | |
---|
156 | void _destroyKeyboard(); |
---|
157 | void _destroyMouse(); |
---|
158 | void _destroyJoySticks(); |
---|
159 | void _destroyState(InputState* state); |
---|
160 | void _clearBuffers(); |
---|
161 | |
---|
162 | void _reload(bool joyStickSupport); |
---|
163 | |
---|
164 | void _completeCalibration(); |
---|
165 | |
---|
166 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
167 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
168 | |
---|
169 | void _updateActiveStates(); |
---|
170 | bool _configureInputState(InputState* state, const std::string& name, int priority); |
---|
171 | |
---|
172 | // input events |
---|
173 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
174 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
175 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
176 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
177 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
178 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
179 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
180 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
181 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
182 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
183 | // don't remove that! Or else add OIS as dependency library to orxonox. |
---|
184 | bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } |
---|
185 | |
---|
186 | void setConfigValues(); |
---|
187 | |
---|
188 | private: // variables |
---|
189 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
190 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
191 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
192 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
193 | unsigned int joySticksSize_; |
---|
194 | unsigned int devicesNum_; |
---|
195 | size_t windowHnd_; //!< Render window handle |
---|
196 | InputManagerState internalState_; //!< Current internal state |
---|
197 | |
---|
198 | // some internally handled states |
---|
199 | SimpleInputState* stateDetector_; //!< KeyDetector instance |
---|
200 | SimpleInputState* stateCalibrator_; |
---|
201 | SimpleInputState* stateEmpty_; |
---|
202 | |
---|
203 | std::map<std::string, InputState*> inputStatesByName_; |
---|
204 | std::map<int, InputState*> inputStatesByPriority_; |
---|
205 | |
---|
206 | std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state |
---|
207 | std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state |
---|
208 | std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state |
---|
209 | |
---|
210 | std::map<int, InputState*> activeStates_; |
---|
211 | std::vector<InputState*> activeStatesTop_; //!< Current input states for joy stick events. |
---|
212 | std::vector<InputState*> activeStatesTicked_; //!< Current input states for joy stick events. |
---|
213 | |
---|
214 | // joystick calibration |
---|
215 | //std::vector<int> marginalsMaxConfig_; |
---|
216 | //std::vector<int> marginalsMinConfig_; |
---|
217 | int marginalsMax_[24]; |
---|
218 | int marginalsMin_[24]; |
---|
219 | bool bCalibrated_; |
---|
220 | bool bCalibrating_; |
---|
221 | |
---|
222 | unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. |
---|
223 | std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. |
---|
224 | std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. |
---|
225 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
226 | |
---|
227 | std::vector<Key> keysDown_; |
---|
228 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
229 | std::vector<std::vector<JoyStickButton::Enum> > joyStickButtonsDown_; |
---|
230 | |
---|
231 | static std::string bindingCommmandString_s; |
---|
232 | static InputManager* singletonRef_s; |
---|
233 | }; |
---|
234 | } |
---|
235 | |
---|
236 | #endif /* _InputManager_H__ */ |
---|