| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgGA/StandardManipulator> |
|---|
| 20 | #include <osgViewer/View> |
|---|
| 21 | |
|---|
| 22 | using namespace osg; |
|---|
| 23 | using namespace osgGA; |
|---|
| 24 | using namespace osgUtil; |
|---|
| 25 | |
|---|
| 26 | int StandardManipulator::numRelativeFlagsAllocated = 0; |
|---|
| 27 | |
|---|
| 28 | int StandardManipulator::allocateRelativeFlag() |
|---|
| 29 | { |
|---|
| 30 | return numRelativeFlagsAllocated++; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | StandardManipulator::StandardManipulator( int flags ) |
|---|
| 36 | : inherited(), |
|---|
| 37 | _thrown( false ), |
|---|
| 38 | _allowThrow( true ), |
|---|
| 39 | _mouseCenterX(0.0f), _mouseCenterY(0.0f), |
|---|
| 40 | _delta_frame_time(0.01), _last_frame_time(0.0), |
|---|
| 41 | _modelSize( 0. ), |
|---|
| 42 | _verticalAxisFixed( true ), |
|---|
| 43 | _flags( flags ), |
|---|
| 44 | _relativeFlags( 0 ) |
|---|
| 45 | { |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | StandardManipulator::StandardManipulator( const StandardManipulator& uim, const CopyOp& copyOp ) |
|---|
| 51 | : inherited( uim, copyOp ), |
|---|
| 52 | _thrown( uim._thrown ), |
|---|
| 53 | _allowThrow( uim._allowThrow ), |
|---|
| 54 | _mouseCenterX(0.0f), _mouseCenterY(0.0f), |
|---|
| 55 | _ga_t1( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t1.get() ) ) ), |
|---|
| 56 | _ga_t0( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t0.get() ) ) ), |
|---|
| 57 | _delta_frame_time(0.01), _last_frame_time(0.0), |
|---|
| 58 | _modelSize( uim._modelSize ), |
|---|
| 59 | _verticalAxisFixed( uim._verticalAxisFixed ), |
|---|
| 60 | _flags( uim._flags ), |
|---|
| 61 | _relativeFlags( uim._relativeFlags ) |
|---|
| 62 | { |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | void StandardManipulator::setNode( Node* node ) |
|---|
| 71 | { |
|---|
| 72 | _node = node; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if( _node.get() ) |
|---|
| 76 | { |
|---|
| 77 | const BoundingSphere& boundingSphere = _node->getBound(); |
|---|
| 78 | _modelSize = boundingSphere.radius(); |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | _modelSize = 0.; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | if( getAutoComputeHomePosition() ) |
|---|
| 87 | computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 ); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | const Node* StandardManipulator::getNode() const |
|---|
| 93 | { |
|---|
| 94 | return _node.get(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | Node* StandardManipulator::getNode() |
|---|
| 100 | { |
|---|
| 101 | return _node.get(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | void StandardManipulator::setVerticalAxisFixed( bool value ) |
|---|
| 111 | { |
|---|
| 112 | _verticalAxisFixed = value; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | void StandardManipulator::setAnimationTime( const double t ) |
|---|
| 118 | { |
|---|
| 119 | if( t <= 0. ) |
|---|
| 120 | { |
|---|
| 121 | finishAnimation(); |
|---|
| 122 | _animationData = NULL; |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if( !_animationData ) |
|---|
| 127 | allocAnimationData(); |
|---|
| 128 | |
|---|
| 129 | _animationData->_animationTime = t; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | double StandardManipulator::getAnimationTime() const |
|---|
| 135 | { |
|---|
| 136 | if( _animationData ) |
|---|
| 137 | return _animationData->_animationTime; |
|---|
| 138 | else |
|---|
| 139 | return 0.; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | bool StandardManipulator::isAnimating() const |
|---|
| 145 | { |
|---|
| 146 | if( _animationData ) |
|---|
| 147 | return _animationData->_isAnimating; |
|---|
| 148 | else |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | void StandardManipulator::finishAnimation() |
|---|
| 155 | { |
|---|
| 156 | if( !isAnimating() ) |
|---|
| 157 | return; |
|---|
| 158 | |
|---|
| 159 | applyAnimationStep( 1., _animationData->_phase ); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | void StandardManipulator::home( double ) |
|---|
| 173 | { |
|---|
| 174 | if( getAutoComputeHomePosition() ) |
|---|
| 175 | computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 ); |
|---|
| 176 | |
|---|
| 177 | _thrown = false; |
|---|
| 178 | setTransformation( _homeEye, _homeCenter, _homeUp ); |
|---|
| 179 | flushMouseEventStack(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | void StandardManipulator::home( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 193 | { |
|---|
| 194 | if( getAutoComputeHomePosition() ) |
|---|
| 195 | { |
|---|
| 196 | const Camera *camera = us.asView() ? us.asView()->getCamera() : NULL; |
|---|
| 197 | computeHomePosition( camera, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 ); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | _thrown = false; |
|---|
| 201 | setTransformation( _homeEye, _homeCenter, _homeUp ); |
|---|
| 202 | |
|---|
| 203 | us.requestRedraw(); |
|---|
| 204 | us.requestContinuousUpdate( false ); |
|---|
| 205 | flushMouseEventStack(); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | void StandardManipulator::init( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 211 | { |
|---|
| 212 | flushMouseEventStack(); |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | _thrown = false; |
|---|
| 216 | us.requestContinuousUpdate(false); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | bool StandardManipulator::handle( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 222 | { |
|---|
| 223 | switch( ea.getEventType() ) |
|---|
| 224 | { |
|---|
| 225 | |
|---|
| 226 | case GUIEventAdapter::FRAME: |
|---|
| 227 | return handleFrame( ea, us ); |
|---|
| 228 | |
|---|
| 229 | case GUIEventAdapter::RESIZE: |
|---|
| 230 | return handleResize( ea, us ); |
|---|
| 231 | |
|---|
| 232 | default: |
|---|
| 233 | break; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | if( ea.getHandled() ) |
|---|
| 237 | return false; |
|---|
| 238 | |
|---|
| 239 | switch( ea.getEventType() ) |
|---|
| 240 | { |
|---|
| 241 | case GUIEventAdapter::MOVE: |
|---|
| 242 | return handleMouseMove( ea, us ); |
|---|
| 243 | |
|---|
| 244 | case GUIEventAdapter::DRAG: |
|---|
| 245 | return handleMouseDrag( ea, us ); |
|---|
| 246 | |
|---|
| 247 | case GUIEventAdapter::PUSH: |
|---|
| 248 | return handleMousePush( ea, us ); |
|---|
| 249 | |
|---|
| 250 | case GUIEventAdapter::RELEASE: |
|---|
| 251 | return handleMouseRelease( ea, us ); |
|---|
| 252 | |
|---|
| 253 | case GUIEventAdapter::KEYDOWN: |
|---|
| 254 | return handleKeyDown( ea, us ); |
|---|
| 255 | |
|---|
| 256 | case GUIEventAdapter::KEYUP: |
|---|
| 257 | return handleKeyUp( ea, us ); |
|---|
| 258 | |
|---|
| 259 | case GUIEventAdapter::SCROLL: |
|---|
| 260 | if( _flags & PROCESS_MOUSE_WHEEL ) |
|---|
| 261 | return handleMouseWheel( ea, us ); |
|---|
| 262 | else |
|---|
| 263 | return false; |
|---|
| 264 | |
|---|
| 265 | default: |
|---|
| 266 | return false; |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | bool StandardManipulator::handleFrame( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 273 | { |
|---|
| 274 | double current_frame_time = ea.getTime(); |
|---|
| 275 | |
|---|
| 276 | _delta_frame_time = current_frame_time - _last_frame_time; |
|---|
| 277 | _last_frame_time = current_frame_time; |
|---|
| 278 | |
|---|
| 279 | if( _thrown && performMovement() ) |
|---|
| 280 | { |
|---|
| 281 | us.requestRedraw(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | if( _animationData && _animationData->_isAnimating ) |
|---|
| 285 | { |
|---|
| 286 | performAnimationMovement( ea, us ); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | return false; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | bool StandardManipulator::handleResize( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 294 | { |
|---|
| 295 | init( ea, us ); |
|---|
| 296 | us.requestRedraw(); |
|---|
| 297 | |
|---|
| 298 | return true; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | bool StandardManipulator::handleMouseMove( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 304 | { |
|---|
| 305 | return false; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | bool StandardManipulator::handleMouseDrag( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 311 | { |
|---|
| 312 | addMouseEvent( ea ); |
|---|
| 313 | |
|---|
| 314 | if( performMovement() ) |
|---|
| 315 | us.requestRedraw(); |
|---|
| 316 | |
|---|
| 317 | us.requestContinuousUpdate( false ); |
|---|
| 318 | _thrown = false; |
|---|
| 319 | |
|---|
| 320 | return true; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | bool StandardManipulator::handleMousePush( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 326 | { |
|---|
| 327 | flushMouseEventStack(); |
|---|
| 328 | addMouseEvent( ea ); |
|---|
| 329 | |
|---|
| 330 | if( performMovement() ) |
|---|
| 331 | us.requestRedraw(); |
|---|
| 332 | |
|---|
| 333 | us.requestContinuousUpdate( false ); |
|---|
| 334 | _thrown = false; |
|---|
| 335 | |
|---|
| 336 | return true; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | bool StandardManipulator::handleMouseRelease( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 342 | { |
|---|
| 343 | if( ea.getButtonMask() == 0 ) |
|---|
| 344 | { |
|---|
| 345 | |
|---|
| 346 | double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() - _ga_t0->getTime()) : DBL_MAX; |
|---|
| 347 | if( timeSinceLastRecordEvent > 0.02 ) |
|---|
| 348 | flushMouseEventStack(); |
|---|
| 349 | |
|---|
| 350 | if( isMouseMoving() ) |
|---|
| 351 | { |
|---|
| 352 | |
|---|
| 353 | if( performMovement() && _allowThrow ) |
|---|
| 354 | { |
|---|
| 355 | us.requestRedraw(); |
|---|
| 356 | us.requestContinuousUpdate( true ); |
|---|
| 357 | _thrown = true; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | return true; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | flushMouseEventStack(); |
|---|
| 365 | addMouseEvent( ea ); |
|---|
| 366 | if( performMovement() ) |
|---|
| 367 | us.requestRedraw(); |
|---|
| 368 | us.requestContinuousUpdate( false ); |
|---|
| 369 | _thrown = false; |
|---|
| 370 | |
|---|
| 371 | return true; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | bool StandardManipulator::handleKeyDown( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 377 | { |
|---|
| 378 | if( ea.getKey() == GUIEventAdapter::KEY_Space ) |
|---|
| 379 | { |
|---|
| 380 | flushMouseEventStack(); |
|---|
| 381 | _thrown = false; |
|---|
| 382 | home(ea,us); |
|---|
| 383 | return true; |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | return false; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | bool StandardManipulator::handleKeyUp( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 392 | { |
|---|
| 393 | return false; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | bool StandardManipulator::handleMouseWheel( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 399 | { |
|---|
| 400 | return false; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | void StandardManipulator::getUsage( ApplicationUsage& usage ) const |
|---|
| 406 | { |
|---|
| 407 | usage.addKeyboardMouseBinding( getManipulatorName() + ": Space", "Reset the viewing position to home" ); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | bool StandardManipulator::performMovement() |
|---|
| 413 | { |
|---|
| 414 | |
|---|
| 415 | if( _ga_t0.get() == NULL || _ga_t1.get() == NULL ) |
|---|
| 416 | return false; |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | double eventTimeDelta = _ga_t0->getTime() - _ga_t1->getTime(); |
|---|
| 420 | if( eventTimeDelta < 0. ) |
|---|
| 421 | { |
|---|
| 422 | OSG_WARN << "Manipulator warning: eventTimeDelta = " << eventTimeDelta << std::endl; |
|---|
| 423 | eventTimeDelta = 0.; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | float dx = _ga_t0->getXnormalized() - _ga_t1->getXnormalized(); |
|---|
| 428 | float dy = _ga_t0->getYnormalized() - _ga_t1->getYnormalized(); |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | if( dx == 0. && dy == 0. ) |
|---|
| 432 | return false; |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | unsigned int buttonMask = _ga_t1->getButtonMask(); |
|---|
| 437 | if( buttonMask == GUIEventAdapter::LEFT_MOUSE_BUTTON ) |
|---|
| 438 | { |
|---|
| 439 | return performMovementLeftMouseButton( eventTimeDelta, dx, dy ); |
|---|
| 440 | } |
|---|
| 441 | else if( buttonMask == GUIEventAdapter::MIDDLE_MOUSE_BUTTON || |
|---|
| 442 | buttonMask == (GUIEventAdapter::LEFT_MOUSE_BUTTON | GUIEventAdapter::RIGHT_MOUSE_BUTTON) ) |
|---|
| 443 | { |
|---|
| 444 | return performMovementMiddleMouseButton( eventTimeDelta, dx, dy ); |
|---|
| 445 | } |
|---|
| 446 | else if( buttonMask == GUIEventAdapter::RIGHT_MOUSE_BUTTON ) |
|---|
| 447 | { |
|---|
| 448 | return performMovementRightMouseButton( eventTimeDelta, dx, dy ); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | return false; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | bool StandardManipulator::performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy ) |
|---|
| 458 | { |
|---|
| 459 | return false; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | bool StandardManipulator::performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy ) |
|---|
| 467 | { |
|---|
| 468 | return false; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | bool StandardManipulator::performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy ) |
|---|
| 475 | { |
|---|
| 476 | return false; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | bool StandardManipulator::handleMouseDeltaMovement( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 482 | { |
|---|
| 483 | float dx = ea.getX() - _mouseCenterX; |
|---|
| 484 | float dy = ea.getY() - _mouseCenterY; |
|---|
| 485 | |
|---|
| 486 | if( dx == 0.f && dy == 0.f ) |
|---|
| 487 | return false; |
|---|
| 488 | |
|---|
| 489 | addMouseEvent( ea ); |
|---|
| 490 | centerMousePointer( ea, us ); |
|---|
| 491 | |
|---|
| 492 | return performMouseDeltaMovement( dx, dy ); |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | bool StandardManipulator::performMouseDeltaMovement( const float dx, const float dy ) |
|---|
| 498 | { |
|---|
| 499 | return false; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | bool StandardManipulator::performAnimationMovement( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 505 | { |
|---|
| 506 | double f = (ea.getTime() - _animationData->_startTime) / _animationData->_animationTime; |
|---|
| 507 | if( f >= 1. ) |
|---|
| 508 | { |
|---|
| 509 | f = 1.; |
|---|
| 510 | _animationData->_isAnimating = false; |
|---|
| 511 | if( !_thrown ) |
|---|
| 512 | us.requestContinuousUpdate( false ); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | applyAnimationStep( f, _animationData->_phase ); |
|---|
| 516 | |
|---|
| 517 | _animationData->_phase = f; |
|---|
| 518 | us.requestRedraw(); |
|---|
| 519 | |
|---|
| 520 | return _animationData->_isAnimating; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | void StandardManipulator::applyAnimationStep( const double currentProgress, const double prevProgress ) |
|---|
| 526 | { |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | void StandardManipulator::centerMousePointer( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 532 | { |
|---|
| 533 | _mouseCenterX = (ea.getXmin() + ea.getXmax()) / 2.0f; |
|---|
| 534 | _mouseCenterY = (ea.getYmin() + ea.getYmax()) / 2.0f; |
|---|
| 535 | us.requestWarpPointer( _mouseCenterX, _mouseCenterY ); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | void StandardManipulator::addMouseEvent( const GUIEventAdapter& ea ) |
|---|
| 541 | { |
|---|
| 542 | _ga_t1 = _ga_t0; |
|---|
| 543 | _ga_t0 = &ea; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | void StandardManipulator::flushMouseEventStack() |
|---|
| 549 | { |
|---|
| 550 | _ga_t1 = NULL; |
|---|
| 551 | _ga_t0 = NULL; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | bool StandardManipulator::isMouseMoving() const |
|---|
| 558 | { |
|---|
| 559 | if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false; |
|---|
| 560 | |
|---|
| 561 | static const float velocity = 0.1f; |
|---|
| 562 | |
|---|
| 563 | float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized(); |
|---|
| 564 | float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized(); |
|---|
| 565 | float len = sqrtf(dx*dx+dy*dy); |
|---|
| 566 | float dt = _ga_t0->getTime()-_ga_t1->getTime(); |
|---|
| 567 | |
|---|
| 568 | return (len>dt*velocity); |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | void StandardManipulator::setAllowThrow( bool allowThrow ) |
|---|
| 576 | { |
|---|
| 577 | _allowThrow = allowThrow; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | float StandardManipulator::getThrowScale( const double eventTimeDelta ) const |
|---|
| 587 | { |
|---|
| 588 | if( _thrown ) return float( _delta_frame_time / eventTimeDelta ); |
|---|
| 589 | else return 1.f; |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | void StandardManipulator::rotateYawPitch( Quat& rotation, const double yaw, const double pitch, |
|---|
| 599 | const Vec3d& localUp ) |
|---|
| 600 | { |
|---|
| 601 | bool verticalAxisFixed = (localUp != Vec3d( 0.,0.,0. )); |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | if( verticalAxisFixed ) |
|---|
| 605 | fixVerticalAxis( rotation, localUp, true ); |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | Quat rotateYaw( -yaw, verticalAxisFixed ? localUp : rotation * Vec3d( 0.,1.,0. ) ); |
|---|
| 609 | Quat rotatePitch; |
|---|
| 610 | Quat newRotation; |
|---|
| 611 | Vec3d cameraRight( rotation * Vec3d( 1.,0.,0. ) ); |
|---|
| 612 | |
|---|
| 613 | double my_dy = pitch; |
|---|
| 614 | int i = 0; |
|---|
| 615 | |
|---|
| 616 | do { |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | rotatePitch.makeRotate( my_dy, cameraRight ); |
|---|
| 620 | newRotation = rotation * rotateYaw * rotatePitch; |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | if( verticalAxisFixed ) |
|---|
| 624 | fixVerticalAxis( newRotation, localUp, false ); |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | Vec3d newCameraUp = newRotation * Vec3d( 0.,1.,0. ); |
|---|
| 628 | if( newCameraUp * localUp > 0. ) |
|---|
| 629 | { |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | rotation = newRotation; |
|---|
| 633 | return; |
|---|
| 634 | |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | my_dy /= 2.; |
|---|
| 638 | if( ++i == 20 ) |
|---|
| 639 | { |
|---|
| 640 | rotation = rotation * rotateYaw; |
|---|
| 641 | return; |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | } while( true ); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | void StandardManipulator::fixVerticalAxis( Vec3d& eye, Quat& rotation, bool disallowFlipOver ) |
|---|
| 660 | { |
|---|
| 661 | CoordinateFrame coordinateFrame = getCoordinateFrame( eye ); |
|---|
| 662 | Vec3d localUp = getUpVector( coordinateFrame ); |
|---|
| 663 | |
|---|
| 664 | fixVerticalAxis( rotation, localUp, disallowFlipOver ); |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | void StandardManipulator::fixVerticalAxis( Quat& rotation, const Vec3d& localUp, bool disallowFlipOver ) |
|---|
| 678 | { |
|---|
| 679 | |
|---|
| 680 | Vec3d cameraUp = rotation * Vec3d( 0.,1.,0. ); |
|---|
| 681 | Vec3d cameraRight = rotation * Vec3d( 1.,0.,0. ); |
|---|
| 682 | Vec3d cameraForward = rotation * Vec3d( 0.,0.,-1. ); |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | Vec3d newCameraRight1 = cameraForward ^ localUp; |
|---|
| 686 | Vec3d newCameraRight2 = cameraUp ^ localUp; |
|---|
| 687 | Vec3d newCameraRight = (newCameraRight1.length2() > newCameraRight2.length2()) ? |
|---|
| 688 | newCameraRight1 : newCameraRight2; |
|---|
| 689 | if( newCameraRight * cameraRight < 0. ) |
|---|
| 690 | newCameraRight = -newCameraRight; |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | Quat rotationVerticalAxisCorrection; |
|---|
| 694 | rotationVerticalAxisCorrection.makeRotate( cameraRight, newCameraRight ); |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | rotation *= rotationVerticalAxisCorrection; |
|---|
| 698 | |
|---|
| 699 | if( disallowFlipOver ) |
|---|
| 700 | { |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | Vec3d newCameraUp = newCameraRight ^ cameraForward; |
|---|
| 704 | if( newCameraUp * localUp < 0. ) |
|---|
| 705 | rotation = Quat( PI, Vec3d( 0.,0.,1. ) ) * rotation; |
|---|
| 706 | |
|---|
| 707 | } |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | void StandardManipulator::fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp, |
|---|
| 722 | const osg::Vec3d& localUp, bool disallowFlipOver ) |
|---|
| 723 | { |
|---|
| 724 | |
|---|
| 725 | osg::Vec3d right1 = forward ^ localUp; |
|---|
| 726 | osg::Vec3d right2 = up ^ localUp; |
|---|
| 727 | osg::Vec3d right = (right1.length2() > right2.length2()) ? right1 : right2; |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | osg::Vec3d updatedUp = right ^ forward; |
|---|
| 731 | if( updatedUp.normalize() >= 0. ) |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | newUp = updatedUp; |
|---|
| 735 | |
|---|
| 736 | else { |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | OSG_WARN << "StandardManipulator::fixVerticalAxis warning: Can not update vertical axis." << std::endl; |
|---|
| 740 | newUp = up; |
|---|
| 741 | |
|---|
| 742 | } |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | bool StandardManipulator::setCenterByMousePointerIntersection( const GUIEventAdapter& ea, GUIActionAdapter& us ) |
|---|
| 751 | { |
|---|
| 752 | osg::View* view = us.asView(); |
|---|
| 753 | if( !view ) |
|---|
| 754 | return false; |
|---|
| 755 | |
|---|
| 756 | Camera *camera = view->getCamera(); |
|---|
| 757 | if( !camera ) |
|---|
| 758 | return false; |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | float x = ( ea.getX() - ea.getXmin() ) / ( ea.getXmax() - ea.getXmin() ); |
|---|
| 762 | float y = ( ea.getY() - ea.getYmin() ) / ( ea.getYmax() - ea.getYmin() ); |
|---|
| 763 | LineSegmentIntersector::CoordinateFrame cf; |
|---|
| 764 | Viewport *vp = camera->getViewport(); |
|---|
| 765 | if( vp ) { |
|---|
| 766 | cf = Intersector::WINDOW; |
|---|
| 767 | x *= vp->width(); |
|---|
| 768 | y *= vp->height(); |
|---|
| 769 | } else |
|---|
| 770 | cf = Intersector::PROJECTION; |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | ref_ptr< LineSegmentIntersector > picker = new LineSegmentIntersector( cf, x, y ); |
|---|
| 774 | IntersectionVisitor iv( picker.get() ); |
|---|
| 775 | camera->accept( iv ); |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | if( !picker->containsIntersections() ) |
|---|
| 779 | return false; |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | LineSegmentIntersector::Intersections& intersections = picker->getIntersections(); |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | osg::Vec3d eye, oldCenter, up; |
|---|
| 786 | getTransformation( eye, oldCenter, up ); |
|---|
| 787 | |
|---|
| 788 | |
|---|
| 789 | osg::Vec3d newCenter = (*intersections.begin()).getWorldIntersectPoint(); |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | if( getVerticalAxisFixed() ) |
|---|
| 793 | { |
|---|
| 794 | |
|---|
| 795 | CoordinateFrame coordinateFrame = getCoordinateFrame( newCenter ); |
|---|
| 796 | Vec3d localUp = getUpVector( coordinateFrame ); |
|---|
| 797 | |
|---|
| 798 | fixVerticalAxis( newCenter - eye, up, up, localUp, true ); |
|---|
| 799 | |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | setTransformation( eye, newCenter, up ); |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | centerMousePointer( ea, us ); |
|---|
| 818 | |
|---|
| 819 | return true; |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | bool StandardManipulator::startAnimationByMousePointerIntersection( |
|---|
| 829 | const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us ) |
|---|
| 830 | { |
|---|
| 831 | return false; |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | StandardManipulator::AnimationData::AnimationData() |
|---|
| 836 | :_isAnimating( false ) |
|---|
| 837 | { |
|---|
| 838 | } |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | void StandardManipulator::AnimationData::start( const double startTime ) |
|---|
| 842 | { |
|---|
| 843 | _isAnimating = true; |
|---|
| 844 | _startTime = startTime; |
|---|
| 845 | _phase = 0.; |
|---|
| 846 | } |
|---|