[21] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 The OGRE Team |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | You may use this sample code for anything you like, it is not covered by the |
---|
| 11 | LGPL like the rest of the engine. |
---|
| 12 | ----------------------------------------------------------------------------- |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "ColladaDemo.h" |
---|
| 16 | #include "UIHandler.h" |
---|
| 17 | |
---|
| 18 | #include "OgreRenderWindow.h" |
---|
| 19 | #include "OgreException.h" |
---|
| 20 | #include "OgreCamera.h" |
---|
| 21 | #include "OgreStringConverter.h" |
---|
| 22 | |
---|
| 23 | //-------------------------------------------------------------------------- |
---|
| 24 | // UIHandler methods that handle all input for this demo. |
---|
| 25 | //-------------------------------------------------------------------------- |
---|
| 26 | |
---|
| 27 | UIHandler::UIHandler(ColladaDemo* main) |
---|
| 28 | : mMain(main) |
---|
| 29 | , mTranslateVector(Ogre::Vector3::ZERO) |
---|
| 30 | , mStatsOn(true) |
---|
| 31 | , mNumScreenShots(0) |
---|
| 32 | , mMoveScale(0.0f) |
---|
| 33 | , mRotScale(0.0f) |
---|
| 34 | , mSpeed(MINSPEED) |
---|
| 35 | , mAvgFrameTime(0.1) |
---|
| 36 | , mSceneDetailIndex(0) |
---|
| 37 | , mMoveSpeed(100) |
---|
| 38 | , mRotateSpeed(0) |
---|
| 39 | , mSkipCount(0) |
---|
| 40 | , mUpdateFreq(50) |
---|
| 41 | , mLastMousePositionSet(false) |
---|
| 42 | , mRotX(0) |
---|
| 43 | , mRotY(0) |
---|
| 44 | , mFiltering(Ogre::TFO_BILINEAR) |
---|
| 45 | , mAniso(1) |
---|
| 46 | , mQuit(false) |
---|
| 47 | , mLMBDown(false) |
---|
| 48 | , mRMBDown(false) |
---|
| 49 | , mProcessMovement(false) |
---|
| 50 | , mUpdateMovement(false) |
---|
| 51 | , mMoveFwd(false) |
---|
| 52 | , mMoveBck(false) |
---|
| 53 | , mMoveLeft(false) |
---|
| 54 | , mMoveRight(false) |
---|
| 55 | |
---|
| 56 | { |
---|
| 57 | // using buffered input |
---|
| 58 | OIS::ParamList pl; |
---|
| 59 | size_t windowHnd = 0; |
---|
| 60 | std::ostringstream windowHndStr; |
---|
| 61 | main->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 62 | windowHndStr << windowHnd; |
---|
| 63 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 64 | |
---|
| 65 | mInputManager = OIS::InputManager::createInputSystem(windowHnd); |
---|
| 66 | |
---|
| 67 | mGuiRenderer = CEGUI::System::getSingleton().getRenderer(); |
---|
| 68 | |
---|
| 69 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true )); |
---|
| 70 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true )); |
---|
| 71 | CEGUI::Rect R=mGuiRenderer->getRect(); |
---|
| 72 | //Set Mouse Region.. if window resizes, we should alter this to reflect as well |
---|
| 73 | const OIS::MouseState &ms = mMouse->getMouseState(); |
---|
| 74 | ms.width = R.getWidth(); |
---|
| 75 | ms.height = R.getHeight(); |
---|
| 76 | |
---|
| 77 | mMouse->setEventCallback(this); |
---|
| 78 | mKeyboard->setEventCallback(this); |
---|
| 79 | // using buffered input |
---|
| 80 | /*mEventProcessor = new Ogre::EventProcessor(); |
---|
| 81 | mEventProcessor->initialise(mMain->getRenderWindow()); |
---|
| 82 | mEventProcessor->startProcessingEvents(); |
---|
| 83 | mEventProcessor->addKeyListener(this); |
---|
| 84 | mEventProcessor->addMouseMotionListener(this); |
---|
| 85 | mEventProcessor->addMouseListener(this); |
---|
| 86 | mInputDevice = mEventProcessor->getInputReader();*/ |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | mGuiAvg = CEGUI::WindowManager::getSingleton().getWindow("OPAverageFPS"); |
---|
| 90 | mGuiCurr = CEGUI::WindowManager::getSingleton().getWindow("OPCurrentFPS"); |
---|
| 91 | mGuiBest = CEGUI::WindowManager::getSingleton().getWindow("OPBestFPS"); |
---|
| 92 | mGuiWorst = CEGUI::WindowManager::getSingleton().getWindow("OPWorstFPS"); |
---|
| 93 | mGuiTris = CEGUI::WindowManager::getSingleton().getWindow("OPTriCount"); |
---|
| 94 | mGuiDbg = CEGUI::WindowManager::getSingleton().getWindow("OPDebugMsg"); |
---|
| 95 | mRoot = CEGUI::WindowManager::getSingleton().getWindow("root"); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | //-------------------------------------------------------------------------- |
---|
| 99 | UIHandler::~UIHandler() |
---|
| 100 | { |
---|
| 101 | if(mInputManager) |
---|
| 102 | { |
---|
| 103 | mInputManager->destroyInputObject(mMouse); |
---|
| 104 | mInputManager->destroyInputObject(mKeyboard); |
---|
| 105 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
| 106 | mInputManager = 0; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | //-------------------------------------------------------------------------- |
---|
| 111 | bool UIHandler::frameStarted(const Ogre::FrameEvent& evt) |
---|
| 112 | { |
---|
| 113 | mMouse->capture(); |
---|
| 114 | mKeyboard->capture(); |
---|
| 115 | if (mQuit) |
---|
| 116 | return false; |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | mSkipCount++; |
---|
| 120 | if (mSkipCount >= mUpdateFreq) |
---|
| 121 | { |
---|
| 122 | mSkipCount = 0; |
---|
| 123 | updateStats(); |
---|
| 124 | // check to see if window got deactivated because user alt-tabbed out, set it back to active so that render updates |
---|
| 125 | // updates are visible even though the window does not have focus |
---|
| 126 | if (!mMain->getRenderWindow()->isClosed() && !mMain->getRenderWindow()->isActive()) |
---|
| 127 | mMain->getRenderWindow()->setActive(true); |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | // update movement process |
---|
| 131 | if(mProcessMovement || mUpdateMovement) |
---|
| 132 | { |
---|
| 133 | mTranslateVector.x += mMoveLeft ? mAvgFrameTime * -MOVESPEED : 0; |
---|
| 134 | mTranslateVector.x += mMoveRight ? mAvgFrameTime * MOVESPEED : 0; |
---|
| 135 | mTranslateVector.z += mMoveFwd ? mAvgFrameTime * -MOVESPEED : 0; |
---|
| 136 | mTranslateVector.z += mMoveBck ? mAvgFrameTime * MOVESPEED : 0; |
---|
| 137 | |
---|
| 138 | Ogre::SceneNode* parentSceneNode = mMain->getActiveCamera()->getParentSceneNode(); |
---|
| 139 | if (parentSceneNode) |
---|
| 140 | { |
---|
| 141 | parentSceneNode->yaw(Ogre::Angle(mRotX)); |
---|
| 142 | parentSceneNode->pitch(Ogre::Angle(mRotY)); |
---|
| 143 | parentSceneNode->translate(mTranslateVector, Ogre::Node::TS_LOCAL); |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
| 147 | mMain->getActiveCamera()->yaw(Ogre::Angle(mRotX)); |
---|
| 148 | mMain->getActiveCamera()->pitch(Ogre::Angle(mRotY)); |
---|
| 149 | mMain->getActiveCamera()->moveRelative(mTranslateVector); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | mUpdateMovement = false; |
---|
| 153 | mRotX = 0; |
---|
| 154 | mRotY = 0; |
---|
| 155 | mTranslateVector = Ogre::Vector3::ZERO; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | mMain->updateAutoReload(mAvgFrameTime); |
---|
| 159 | |
---|
| 160 | return true; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | //-------------------------------------------------------------------------- |
---|
| 165 | bool UIHandler::mouseMoved (const OIS::MouseEvent &e) |
---|
| 166 | { |
---|
| 167 | CEGUI::System::getSingleton().injectMouseMove( |
---|
| 168 | e.state.X.rel, |
---|
| 169 | e.state.Y.rel); |
---|
| 170 | CEGUI::System::getSingleton().injectMouseWheelChange(e.state.Z.rel); |
---|
| 171 | return true; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | //-------------------------------------------------------------------------- |
---|
| 175 | bool UIHandler::mouseDragged (const OIS::MouseEvent &e) |
---|
| 176 | { |
---|
| 177 | return mouseMoved(e); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //-------------------------------------------------------------------------- |
---|
| 181 | bool UIHandler::keyPressed (const OIS::KeyEvent &e) |
---|
| 182 | { |
---|
| 183 | // give 'quitting' priority |
---|
| 184 | if (e.key == OIS::KC_ESCAPE) |
---|
| 185 | { |
---|
| 186 | mQuit = true; |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (e.key== OIS::KC_SYSRQ ) |
---|
| 191 | { |
---|
| 192 | char tmp[20]; |
---|
| 193 | sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots); |
---|
| 194 | mMain->getRenderWindow()->writeContentsToFile(tmp); |
---|
| 195 | // mMain->getRenderWindow()->setDebugText(Ogre::String("Wrote ") + tmp); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | // do event injection |
---|
| 199 | CEGUI::System& cegui = CEGUI::System::getSingleton(); |
---|
| 200 | |
---|
| 201 | // key down |
---|
| 202 | cegui.injectKeyDown(e.key); |
---|
| 203 | |
---|
| 204 | // now character |
---|
| 205 | cegui.injectChar(e.text); |
---|
| 206 | |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | //-------------------------------------------------------------------------- |
---|
| 211 | bool UIHandler::keyReleased (const OIS::KeyEvent &e) |
---|
| 212 | { |
---|
| 213 | CEGUI::System::getSingleton().injectKeyUp(e.key); |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | //-------------------------------------------------------------------------- |
---|
| 218 | bool UIHandler::mousePressed (const OIS::MouseEvent &e,OIS::MouseButtonID b) |
---|
| 219 | { |
---|
| 220 | CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(b)); |
---|
| 221 | return true; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | //-------------------------------------------------------------------------- |
---|
| 225 | bool UIHandler::mouseReleased (const OIS::MouseEvent &e,OIS::MouseButtonID b) |
---|
| 226 | { |
---|
| 227 | CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(b)); |
---|
| 228 | return true; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | //-------------------------------------------------------------------------- |
---|
| 232 | CEGUI::MouseButton UIHandler::convertOgreButtonToCegui(OIS::MouseButtonID b) |
---|
| 233 | { |
---|
| 234 | if(b==(OIS::MB_Left)) |
---|
| 235 | return CEGUI::LeftButton; |
---|
| 236 | if(b==(OIS::MB_Right)) |
---|
| 237 | return CEGUI::RightButton; |
---|
| 238 | if(b==(OIS::MB_Middle)) |
---|
| 239 | return CEGUI::MiddleButton; |
---|
| 240 | if(b==(OIS::MB_Button3)) |
---|
| 241 | return CEGUI::X1Button; |
---|
| 242 | return CEGUI::LeftButton; |
---|
| 243 | |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | //-------------------------------------------------------------------------- |
---|
| 247 | void UIHandler::updateStats() |
---|
| 248 | { |
---|
| 249 | static CEGUI::String currFps = "Current FPS: "; |
---|
| 250 | static CEGUI::String avgFps = "Average FPS: "; |
---|
| 251 | static CEGUI::String bestFps = "Best FPS: "; |
---|
| 252 | static CEGUI::String worstFps = "Worst FPS: "; |
---|
| 253 | static CEGUI::String tris = "Triangle Count: "; |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | const Ogre::RenderTarget::FrameStats& stats = mMain->getRenderWindow()->getStatistics(); |
---|
| 257 | |
---|
| 258 | mGuiAvg->setText(avgFps + Ogre::StringConverter::toString(stats.avgFPS)); |
---|
| 259 | mGuiCurr->setText(currFps + Ogre::StringConverter::toString(stats.lastFPS)); |
---|
| 260 | mGuiBest->setText(bestFps + Ogre::StringConverter::toString(stats.bestFPS) |
---|
| 261 | + " " + Ogre::StringConverter::toString(stats.bestFrameTime)+" ms"); |
---|
| 262 | mGuiWorst->setText(worstFps + Ogre::StringConverter::toString(stats.worstFPS) |
---|
| 263 | + " " + Ogre::StringConverter::toString(stats.worstFrameTime)+" ms"); |
---|
| 264 | |
---|
| 265 | mGuiTris->setText(tris + Ogre::StringConverter::toString(stats.triangleCount)); |
---|
| 266 | // mGuiDbg->setText(mMain->getRenderWindow()->getDebugText()); |
---|
| 267 | mAvgFrameTime = 1.0f/(stats.avgFPS + 1.0f); |
---|
| 268 | if (mAvgFrameTime > 0.1f) mAvgFrameTime = 0.1f; |
---|
| 269 | |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | //-------------------------------------------------------------------------- |
---|
| 274 | bool UIHandler::handleMouseMove(const CEGUI::EventArgs& e) |
---|
| 275 | { |
---|
| 276 | using namespace CEGUI; |
---|
| 277 | |
---|
| 278 | if( mLMBDown && !mRMBDown) |
---|
| 279 | { |
---|
| 280 | // rotate camera |
---|
| 281 | mRotX += -((const MouseEventArgs&)e).moveDelta.d_x * mAvgFrameTime * 10.0; |
---|
| 282 | mRotY += -((const MouseEventArgs&)e).moveDelta.d_y * mAvgFrameTime * 10.0; |
---|
| 283 | MouseCursor::getSingleton().setPosition( mLastMousePosition ); |
---|
| 284 | mUpdateMovement = true; |
---|
| 285 | } |
---|
| 286 | else |
---|
| 287 | { |
---|
| 288 | if( mRMBDown && !mLMBDown) |
---|
| 289 | { |
---|
| 290 | // translate camera |
---|
| 291 | mTranslateVector.x += ((const MouseEventArgs&)e).moveDelta.d_x * mAvgFrameTime * MOVESPEED; |
---|
| 292 | mTranslateVector.y += -((const MouseEventArgs&)e).moveDelta.d_y * mAvgFrameTime * MOVESPEED; |
---|
| 293 | //mTranslateVector.z = 0; |
---|
| 294 | MouseCursor::getSingleton().setPosition( mLastMousePosition ); |
---|
| 295 | mUpdateMovement = true; |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | { |
---|
| 299 | if( mRMBDown && mLMBDown) |
---|
| 300 | { |
---|
| 301 | mTranslateVector.z += (((const MouseEventArgs&)e).moveDelta.d_x + ((const MouseEventArgs&)e).moveDelta.d_y) * mAvgFrameTime * MOVESPEED; |
---|
| 302 | MouseCursor::getSingleton().setPosition( mLastMousePosition ); |
---|
| 303 | mUpdateMovement = true; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | return true; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | //-------------------------------------------------------------------------- |
---|
| 313 | bool UIHandler::handleMouseButtonUp(const CEGUI::EventArgs& e) |
---|
| 314 | { |
---|
| 315 | using namespace CEGUI; |
---|
| 316 | |
---|
| 317 | //Window* wndw = ((const WindowEventArgs&)e).window; |
---|
| 318 | if( ((const MouseEventArgs&)e).button == LeftButton ) |
---|
| 319 | { |
---|
| 320 | mLMBDown = false; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | if( ((const MouseEventArgs&)e).button == RightButton ) |
---|
| 324 | { |
---|
| 325 | mRMBDown = false; |
---|
| 326 | } |
---|
| 327 | if( !mLMBDown && !mRMBDown ) |
---|
| 328 | { |
---|
| 329 | MouseCursor::getSingleton().show(); |
---|
| 330 | if(mLastMousePositionSet) |
---|
| 331 | { |
---|
| 332 | MouseCursor::getSingleton().setPosition( mLastMousePosition ); |
---|
| 333 | mLastMousePositionSet = false; |
---|
| 334 | } |
---|
| 335 | mRoot->releaseInput(); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | return true; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | //-------------------------------------------------------------------------- |
---|
| 342 | bool UIHandler::handleMouseButtonDown(const CEGUI::EventArgs& e) |
---|
| 343 | { |
---|
| 344 | using namespace CEGUI; |
---|
| 345 | |
---|
| 346 | //Window* wndw = ((const WindowEventArgs&)e).window; |
---|
| 347 | if( ((const MouseEventArgs&)e).button == LeftButton ) |
---|
| 348 | { |
---|
| 349 | mLMBDown = true; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | if( ((const MouseEventArgs&)e).button == RightButton ) |
---|
| 353 | { |
---|
| 354 | mRMBDown = true; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | if( mLMBDown || mRMBDown ) |
---|
| 358 | { |
---|
| 359 | MouseCursor::getSingleton().hide(); |
---|
| 360 | if (!mLastMousePositionSet) |
---|
| 361 | { |
---|
| 362 | mLastMousePosition = MouseCursor::getSingleton().getPosition(); |
---|
| 363 | mLastMousePositionSet = true; |
---|
| 364 | } |
---|
| 365 | mRoot->captureInput(); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | return true; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | //-------------------------------------------------------------------------- |
---|
| 372 | bool UIHandler::handleMouseWheelEvent(const CEGUI::EventArgs& e) |
---|
| 373 | { |
---|
| 374 | using namespace CEGUI; |
---|
| 375 | mTranslateVector.z += ((const MouseEventArgs&)e).wheelChange * -5.0; |
---|
| 376 | mUpdateMovement = true; |
---|
| 377 | |
---|
| 378 | return true; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | //-------------------------------------------------------------------------- |
---|
| 382 | bool UIHandler::handleKeyDownEvent(const CEGUI::EventArgs& e) |
---|
| 383 | { |
---|
| 384 | using namespace CEGUI; |
---|
| 385 | |
---|
| 386 | CheckMovementKeys( ((const KeyEventArgs&)e).scancode , true); |
---|
| 387 | |
---|
| 388 | return true; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | //-------------------------------------------------------------------------- |
---|
| 392 | bool UIHandler::handleKeyUpEvent(const CEGUI::EventArgs& e) |
---|
| 393 | { |
---|
| 394 | using namespace CEGUI; |
---|
| 395 | CheckMovementKeys( ((const KeyEventArgs&)e).scancode, false ); |
---|
| 396 | |
---|
| 397 | return true; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | //-------------------------------------------------------------------------- |
---|
| 401 | void UIHandler::CheckMovementKeys( CEGUI::Key::Scan scancode, bool state ) |
---|
| 402 | { |
---|
| 403 | using namespace CEGUI; |
---|
| 404 | |
---|
| 405 | switch ( scancode ) |
---|
| 406 | { |
---|
| 407 | case Key::A: |
---|
| 408 | mMoveLeft = state; |
---|
| 409 | break; |
---|
| 410 | |
---|
| 411 | case Key::D: |
---|
| 412 | mMoveRight = state; |
---|
| 413 | break; |
---|
| 414 | |
---|
| 415 | case Key::S: |
---|
| 416 | mMoveBck = state; |
---|
| 417 | break; |
---|
| 418 | |
---|
| 419 | case Key::W: |
---|
| 420 | mMoveFwd = state; |
---|
| 421 | break; |
---|
| 422 | |
---|
| 423 | default: |
---|
| 424 | break; |
---|
| 425 | |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | mProcessMovement = mMoveLeft || mMoveRight || mMoveFwd || mMoveBck; |
---|
| 429 | } |
---|