1 | #include "InputManager.h" |
---|
2 | |
---|
3 | InputManager *InputManager::mInputManager; |
---|
4 | |
---|
5 | InputManager::InputManager( void ) : |
---|
6 | mMouse( 0 ), mKeyboard( 0 ), mInputSystem( 0 ) {} |
---|
7 | |
---|
8 | InputManager::~InputManager( void ) { |
---|
9 | if( mInputSystem ) { |
---|
10 | if( mMouse ) { |
---|
11 | mInputSystem->destroyInputObject( mMouse ); |
---|
12 | mMouse = 0; |
---|
13 | } |
---|
14 | |
---|
15 | if( mKeyboard ) { |
---|
16 | mInputSystem->destroyInputObject( mKeyboard ); |
---|
17 | mKeyboard = 0; |
---|
18 | } |
---|
19 | |
---|
20 | if( mJoysticks.size() > 0 ) { |
---|
21 | itJoystick = mJoysticks.begin(); |
---|
22 | itJoystickEnd = mJoysticks.end(); |
---|
23 | for(; itJoystick != itJoystickEnd; ++itJoystick ) { |
---|
24 | mInputSystem->destroyInputObject( *itJoystick ); |
---|
25 | } |
---|
26 | |
---|
27 | mJoysticks.clear(); |
---|
28 | } |
---|
29 | |
---|
30 | // If you use OIS1.0RC1 or above, uncomment this line |
---|
31 | // and comment the line below it |
---|
32 | mInputSystem->destroyInputSystem( mInputSystem ); |
---|
33 | //mInputSystem->destroyInputSystem(); |
---|
34 | mInputSystem = 0; |
---|
35 | |
---|
36 | // Clear Listeners |
---|
37 | mKeyListeners.clear(); |
---|
38 | mMouseListeners.clear(); |
---|
39 | mJoystickListeners.clear(); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | void InputManager::initialise( Ogre::RenderWindow *renderWindow ) { |
---|
44 | if( !mInputSystem ) { |
---|
45 | // Setup basic variables |
---|
46 | OIS::ParamList paramList; |
---|
47 | size_t windowHnd = 0; |
---|
48 | std::ostringstream windowHndStr; |
---|
49 | |
---|
50 | // Get window handle |
---|
51 | renderWindow->getCustomAttribute( "WINDOW", &windowHnd ); |
---|
52 | |
---|
53 | // Fill parameter list |
---|
54 | windowHndStr << (unsigned int) windowHnd; |
---|
55 | paramList.insert( std::make_pair( std::string( "WINDOW" ), windowHndStr.str() ) ); |
---|
56 | |
---|
57 | // Create inputsystem |
---|
58 | mInputSystem = OIS::InputManager::createInputSystem( paramList ); |
---|
59 | |
---|
60 | // If possible create a buffered keyboard |
---|
61 | // (note: if below line doesn't compile, try: if (mInputSystem->getNumberOfDevices(OIS::OISKeyboard) > 0) { |
---|
62 | if( mInputSystem->numKeyboards() > 0 ) { |
---|
63 | // if (mInputSystem->getNumberOfDevices(OIS::OISKeyboard) > 0) { |
---|
64 | mKeyboard = static_cast<OIS::Keyboard*>( mInputSystem->createInputObject( OIS::OISKeyboard, true ) ); |
---|
65 | mKeyboard->setEventCallback( this ); |
---|
66 | } |
---|
67 | |
---|
68 | // If possible create a buffered mouse |
---|
69 | // (note: if below line doesn't compile, try: if (mInputSystem->getNumberOfDevices(OIS::OISMouse) > 0) { |
---|
70 | if( mInputSystem->numMice() > 0 ) { |
---|
71 | // if (mInputSystem->getNumberOfDevices(OIS::OISMouse) > 0) { |
---|
72 | mMouse = static_cast<OIS::Mouse*>( mInputSystem->createInputObject( OIS::OISMouse, true ) ); |
---|
73 | mMouse->setEventCallback( this ); |
---|
74 | |
---|
75 | // Get window size |
---|
76 | unsigned int width, height, depth; |
---|
77 | int left, top; |
---|
78 | renderWindow->getMetrics( width, height, depth, left, top ); |
---|
79 | |
---|
80 | // Set mouse region |
---|
81 | this->setWindowExtents( width, height ); |
---|
82 | } |
---|
83 | |
---|
84 | // If possible create all joysticks in buffered mode |
---|
85 | // (note: if below line doesn't compile, try: if (mInputSystem->getNumberOfDevices(OIS::OISJoyStick) > 0) { |
---|
86 | if( mInputSystem->numJoySticks() > 0 ) { |
---|
87 | // if (mInputSystem->getNumberOfDevices(OIS::OISJoyStick) > 0) { |
---|
88 | mJoysticks.resize( mInputSystem->numJoySticks() ); |
---|
89 | // mJoysticks.resize( mInputSystem->getNumberOfDevices(OIS::OISJoyStick) ); |
---|
90 | |
---|
91 | itJoystick = mJoysticks.begin(); |
---|
92 | itJoystickEnd = mJoysticks.end(); |
---|
93 | for(; itJoystick != itJoystickEnd; ++itJoystick ) { |
---|
94 | (*itJoystick) = static_cast<OIS::JoyStick*>( mInputSystem->createInputObject( OIS::OISJoyStick, true ) ); |
---|
95 | (*itJoystick)->setEventCallback( this ); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void InputManager::capture( void ) { |
---|
102 | // Need to capture / update each device every frame |
---|
103 | if( mMouse ) { |
---|
104 | mMouse->capture(); |
---|
105 | } |
---|
106 | |
---|
107 | if( mKeyboard ) { |
---|
108 | mKeyboard->capture(); |
---|
109 | } |
---|
110 | |
---|
111 | if( mJoysticks.size() > 0 ) { |
---|
112 | itJoystick = mJoysticks.begin(); |
---|
113 | itJoystickEnd = mJoysticks.end(); |
---|
114 | for(; itJoystick != itJoystickEnd; ++itJoystick ) { |
---|
115 | (*itJoystick)->capture(); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void InputManager::addKeyListener( OIS::KeyListener *keyListener, const std::string& instanceName ) { |
---|
121 | if( mKeyboard ) { |
---|
122 | // Check for duplicate items |
---|
123 | itKeyListener = mKeyListeners.find( instanceName ); |
---|
124 | if( itKeyListener == mKeyListeners.end() ) { |
---|
125 | mKeyListeners[ instanceName ] = keyListener; |
---|
126 | } |
---|
127 | else { |
---|
128 | // Duplicate Item |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void InputManager::addMouseListener( OIS::MouseListener *mouseListener, const std::string& instanceName ) { |
---|
134 | if( mMouse ) { |
---|
135 | // Check for duplicate items |
---|
136 | itMouseListener = mMouseListeners.find( instanceName ); |
---|
137 | if( itMouseListener == mMouseListeners.end() ) { |
---|
138 | mMouseListeners[ instanceName ] = mouseListener; |
---|
139 | } |
---|
140 | else { |
---|
141 | // Duplicate Item |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | void InputManager::addJoystickListener( OIS::JoyStickListener *joystickListener, const std::string& instanceName ) { |
---|
147 | if( mJoysticks.size() > 0 ) { |
---|
148 | // Check for duplicate items |
---|
149 | itJoystickListener = mJoystickListeners.find( instanceName ); |
---|
150 | if( itJoystickListener == mJoystickListeners.end() ) { |
---|
151 | mJoystickListeners[ instanceName ] = joystickListener; |
---|
152 | } |
---|
153 | else { |
---|
154 | // Duplicate Item |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | void InputManager::removeKeyListener( const std::string& instanceName ) { |
---|
160 | // Check if item exists |
---|
161 | itKeyListener = mKeyListeners.find( instanceName ); |
---|
162 | if( itKeyListener != mKeyListeners.end() ) { |
---|
163 | mKeyListeners.erase( itKeyListener ); |
---|
164 | } |
---|
165 | else { |
---|
166 | // Doesn't Exist |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void InputManager::removeMouseListener( const std::string& instanceName ) { |
---|
171 | // Check if item exists |
---|
172 | itMouseListener = mMouseListeners.find( instanceName ); |
---|
173 | if( itMouseListener != mMouseListeners.end() ) { |
---|
174 | mMouseListeners.erase( itMouseListener ); |
---|
175 | } |
---|
176 | else { |
---|
177 | // Doesn't Exist |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | void InputManager::removeJoystickListener( const std::string& instanceName ) { |
---|
182 | // Check if item exists |
---|
183 | itJoystickListener = mJoystickListeners.find( instanceName ); |
---|
184 | if( itJoystickListener != mJoystickListeners.end() ) { |
---|
185 | mJoystickListeners.erase( itJoystickListener ); |
---|
186 | } |
---|
187 | else { |
---|
188 | // Doesn't Exist |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | void InputManager::removeKeyListener( OIS::KeyListener *keyListener ) { |
---|
193 | itKeyListener = mKeyListeners.begin(); |
---|
194 | itKeyListenerEnd = mKeyListeners.end(); |
---|
195 | for(; itKeyListener != itKeyListenerEnd; ++itKeyListener ) { |
---|
196 | if( itKeyListener->second == keyListener ) { |
---|
197 | mKeyListeners.erase( itKeyListener ); |
---|
198 | break; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | void InputManager::removeMouseListener( OIS::MouseListener *mouseListener ) { |
---|
204 | itMouseListener = mMouseListeners.begin(); |
---|
205 | itMouseListenerEnd = mMouseListeners.end(); |
---|
206 | for(; itMouseListener != itMouseListenerEnd; ++itMouseListener ) { |
---|
207 | if( itMouseListener->second == mouseListener ) { |
---|
208 | mMouseListeners.erase( itMouseListener ); |
---|
209 | break; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | void InputManager::removeJoystickListener( OIS::JoyStickListener *joystickListener ) { |
---|
215 | itJoystickListener = mJoystickListeners.begin(); |
---|
216 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
217 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
218 | if( itJoystickListener->second == joystickListener ) { |
---|
219 | mJoystickListeners.erase( itJoystickListener ); |
---|
220 | break; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | void InputManager::removeAllListeners( void ) { |
---|
226 | mKeyListeners.clear(); |
---|
227 | mMouseListeners.clear(); |
---|
228 | mJoystickListeners.clear(); |
---|
229 | } |
---|
230 | |
---|
231 | void InputManager::removeAllKeyListeners( void ) { |
---|
232 | mKeyListeners.clear(); |
---|
233 | } |
---|
234 | |
---|
235 | void InputManager::removeAllMouseListeners( void ) { |
---|
236 | mMouseListeners.clear(); |
---|
237 | } |
---|
238 | |
---|
239 | void InputManager::removeAllJoystickListeners( void ) { |
---|
240 | mJoystickListeners.clear(); |
---|
241 | } |
---|
242 | |
---|
243 | void InputManager::setWindowExtents( int width, int height ) { |
---|
244 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
245 | const OIS::MouseState &mouseState = mMouse->getMouseState(); |
---|
246 | mouseState.width = width; |
---|
247 | mouseState.height = height; |
---|
248 | } |
---|
249 | |
---|
250 | OIS::Mouse* InputManager::getMouse( void ) { |
---|
251 | return mMouse; |
---|
252 | } |
---|
253 | |
---|
254 | OIS::Keyboard* InputManager::getKeyboard( void ) { |
---|
255 | return mKeyboard; |
---|
256 | } |
---|
257 | |
---|
258 | OIS::JoyStick* InputManager::getJoystick( unsigned int index ) { |
---|
259 | // Make sure it's a valid index |
---|
260 | if( index < mJoysticks.size() ) { |
---|
261 | return mJoysticks[ index ]; |
---|
262 | } |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | int InputManager::getNumOfJoysticks( void ) { |
---|
268 | // Cast to keep compiler happy ^^ |
---|
269 | return (int) mJoysticks.size(); |
---|
270 | } |
---|
271 | |
---|
272 | bool InputManager::keyPressed( const OIS::KeyEvent &e ) { |
---|
273 | itKeyListener = mKeyListeners.begin(); |
---|
274 | itKeyListenerEnd = mKeyListeners.end(); |
---|
275 | for(; itKeyListener != itKeyListenerEnd; ++itKeyListener ) { |
---|
276 | if(!itKeyListener->second->keyPressed( e )) |
---|
277 | break; |
---|
278 | } |
---|
279 | |
---|
280 | return true; |
---|
281 | } |
---|
282 | |
---|
283 | bool InputManager::keyReleased( const OIS::KeyEvent &e ) { |
---|
284 | itKeyListener = mKeyListeners.begin(); |
---|
285 | itKeyListenerEnd = mKeyListeners.end(); |
---|
286 | for(; itKeyListener != itKeyListenerEnd; ++itKeyListener ) { |
---|
287 | if(!itKeyListener->second->keyReleased( e )) |
---|
288 | break; |
---|
289 | } |
---|
290 | |
---|
291 | return true; |
---|
292 | } |
---|
293 | |
---|
294 | bool InputManager::mouseMoved( const OIS::MouseEvent &e ) { |
---|
295 | itMouseListener = mMouseListeners.begin(); |
---|
296 | itMouseListenerEnd = mMouseListeners.end(); |
---|
297 | for(; itMouseListener != itMouseListenerEnd; ++itMouseListener ) { |
---|
298 | if(!itMouseListener->second->mouseMoved( e )) |
---|
299 | break; |
---|
300 | } |
---|
301 | |
---|
302 | return true; |
---|
303 | } |
---|
304 | |
---|
305 | bool InputManager::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id ) { |
---|
306 | itMouseListener = mMouseListeners.begin(); |
---|
307 | itMouseListenerEnd = mMouseListeners.end(); |
---|
308 | for(; itMouseListener != itMouseListenerEnd; ++itMouseListener ) { |
---|
309 | if(!itMouseListener->second->mousePressed( e, id )) |
---|
310 | break; |
---|
311 | } |
---|
312 | |
---|
313 | return true; |
---|
314 | } |
---|
315 | |
---|
316 | bool InputManager::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id ) { |
---|
317 | itMouseListener = mMouseListeners.begin(); |
---|
318 | itMouseListenerEnd = mMouseListeners.end(); |
---|
319 | for(; itMouseListener != itMouseListenerEnd; ++itMouseListener ) { |
---|
320 | if(!itMouseListener->second->mouseReleased( e, id )) |
---|
321 | break; |
---|
322 | } |
---|
323 | |
---|
324 | return true; |
---|
325 | } |
---|
326 | |
---|
327 | bool InputManager::povMoved( const OIS::JoyStickEvent &e, int pov ) { |
---|
328 | itJoystickListener = mJoystickListeners.begin(); |
---|
329 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
330 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
331 | if(!itJoystickListener->second->povMoved( e, pov )) |
---|
332 | break; |
---|
333 | } |
---|
334 | |
---|
335 | return true; |
---|
336 | } |
---|
337 | |
---|
338 | bool InputManager::axisMoved( const OIS::JoyStickEvent &e, int axis ) { |
---|
339 | itJoystickListener = mJoystickListeners.begin(); |
---|
340 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
341 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
342 | if(!itJoystickListener->second->axisMoved( e, axis )) |
---|
343 | break; |
---|
344 | } |
---|
345 | |
---|
346 | return true; |
---|
347 | } |
---|
348 | |
---|
349 | bool InputManager::sliderMoved( const OIS::JoyStickEvent &e, int sliderID ) { |
---|
350 | itJoystickListener = mJoystickListeners.begin(); |
---|
351 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
352 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
353 | if(!itJoystickListener->second->sliderMoved( e, sliderID )) |
---|
354 | break; |
---|
355 | } |
---|
356 | |
---|
357 | return true; |
---|
358 | } |
---|
359 | |
---|
360 | bool InputManager::buttonPressed( const OIS::JoyStickEvent &e, int button ) { |
---|
361 | itJoystickListener = mJoystickListeners.begin(); |
---|
362 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
363 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
364 | if(!itJoystickListener->second->buttonPressed( e, button )) |
---|
365 | break; |
---|
366 | } |
---|
367 | |
---|
368 | return true; |
---|
369 | } |
---|
370 | |
---|
371 | bool InputManager::buttonReleased( const OIS::JoyStickEvent &e, int button ) { |
---|
372 | itJoystickListener = mJoystickListeners.begin(); |
---|
373 | itJoystickListenerEnd = mJoystickListeners.end(); |
---|
374 | for(; itJoystickListener != itJoystickListenerEnd; ++itJoystickListener ) { |
---|
375 | if(!itJoystickListener->second->buttonReleased( e, button )) |
---|
376 | break; |
---|
377 | } |
---|
378 | |
---|
379 | return true; |
---|
380 | } |
---|
381 | |
---|
382 | InputManager* InputManager::getSingletonPtr( void ) { |
---|
383 | if( !mInputManager ) { |
---|
384 | mInputManager = new InputManager(); |
---|
385 | } |
---|
386 | |
---|
387 | return mInputManager; |
---|
388 | } |
---|