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