StandardManipulator 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. *
  13. * StandardManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
  14. * while some pieces of code were taken from OSG.
  15. * Thanks to company Cadwork (www.cadwork.ch) and
  16. * Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
  17. */
  18. #ifndef OSGGA_CAMERA_MANIPULATOR
  19. #define OSGGA_CAMERA_MANIPULATOR 1
  20. #include <osgGA/CameraManipulator>
  21. namespace osgGA {
  22. /** StandardManipulator class provides basic functionality
  23. for user controlled manipulation.*/
  24. class OSGGA_EXPORT StandardManipulator : public CameraManipulator
  25. {
  26. typedef CameraManipulator inherited;
  27. public:
  28. // flags
  29. enum UserInteractionFlags
  30. {
  31. UPDATE_MODEL_SIZE = 0x01,
  32. COMPUTE_HOME_USING_BBOX = 0x02,
  33. PROCESS_MOUSE_WHEEL = 0x04,
  34. SET_CENTER_ON_WHEEL_FORWARD_MOVEMENT = 0x08,
  35. DEFAULT_SETTINGS = UPDATE_MODEL_SIZE /*| COMPUTE_HOME_USING_BBOX*/ | PROCESS_MOUSE_WHEEL
  36. };
  37. StandardManipulator( int flags = DEFAULT_SETTINGS );
  38. StandardManipulator( const StandardManipulator& m,
  39. const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
  40. // We are not using META_Object as this is abstract class.
  41. // Use META_Object(osgGA,YourManipulator); in your descendant non-abstract classes.
  42. virtual const char* className() const { return "StandardManipulator"; }
  43. /** Sets manipulator by eye position and eye orientation.*/
  44. virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation ) = 0;
  45. /** Sets manipulator by eye position, center of rotation, and up vector.*/
  46. virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up ) = 0;
  47. /** Gets manipulator's eye position and eye orientation.*/
  48. virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const = 0;
  49. /** Gets manipulator's focal center, eye position, and up vector.*/
  50. virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const = 0;
  51. virtual void setNode( osg::Node* );
  52. virtual const osg::Node* getNode() const;
  53. virtual osg::Node* getNode();
  54. virtual void setVerticalAxisFixed( bool value );
  55. inline bool getVerticalAxisFixed() const;
  56. inline bool getAllowThrow() const;
  57. virtual void setAllowThrow( bool allowThrow );
  58. virtual void setAnimationTime( const double t );
  59. double getAnimationTime() const;
  60. bool isAnimating() const;
  61. virtual void finishAnimation();
  62. virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  63. virtual void home( double );
  64. virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  65. virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  66. virtual void getUsage( osg::ApplicationUsage& usage ) const;
  67. protected:
  68. virtual bool handleFrame( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  69. virtual bool handleResize( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  70. virtual bool handleMouseMove( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  71. virtual bool handleMouseDrag( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  72. virtual bool handleMousePush( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  73. virtual bool handleMouseRelease( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  74. virtual bool handleKeyDown( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  75. virtual bool handleKeyUp( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  76. virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  77. virtual bool handleMouseDeltaMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  78. virtual bool performMovement();
  79. virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
  80. virtual bool performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy );
  81. virtual bool performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy );
  82. virtual bool performMouseDeltaMovement( const float dx, const float dy );
  83. virtual bool performAnimationMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  84. virtual void applyAnimationStep( const double currentProgress, const double prevProgress );
  85. void addMouseEvent( const osgGA::GUIEventAdapter& ea );
  86. void flushMouseEventStack();
  87. virtual bool isMouseMoving() const;
  88. float getThrowScale( const double eventTimeDelta ) const;
  89. virtual void centerMousePointer( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  90. static void rotateYawPitch( osg::Quat& rotation, const double yaw, const double pitch,
  91. const osg::Vec3d& localUp = osg::Vec3d( 0.,0.,0.) );
  92. static void fixVerticalAxis( osg::Quat& rotation, const osg::Vec3d& localUp, bool disallowFlipOver );
  93. void fixVerticalAxis( osg::Vec3d& eye, osg::Quat& rotation, bool disallowFlipOver );
  94. static void fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp,
  95. const osg::Vec3d& localUp, bool disallowFlipOver );
  96. virtual bool setCenterByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  97. virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  98. // mouse state
  99. bool _thrown;
  100. bool _allowThrow;
  101. float _mouseCenterX, _mouseCenterY;
  102. // internal event stack comprising last two mouse events.
  103. osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t1;
  104. osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t0;
  105. /** The approximate amount of time it is currently taking to draw a frame.
  106. * This is used to compute the delta in translation/rotation during a thrown display update.
  107. * It allows us to match an delta in position/rotation independent of the rendering frame rate.
  108. */
  109. double _delta_frame_time;
  110. /** The time the last frame started.
  111. * Used when _rate_sensitive is true so that we can match display update rate to rotation/translation rate.
  112. */
  113. double _last_frame_time;
  114. // scene data
  115. osg::ref_ptr< osg::Node > _node;
  116. double _modelSize;
  117. bool _verticalAxisFixed;
  118. // animation stuff
  119. class OSGGA_EXPORT AnimationData : public osg::Referenced {
  120. public:
  121. double _animationTime;
  122. bool _isAnimating;
  123. double _startTime;
  124. double _phase;
  125. AnimationData();
  126. void start( const double startTime );
  127. };
  128. osg::ref_ptr< AnimationData > _animationData;
  129. virtual void allocAnimationData() { _animationData = new AnimationData(); }
  130. // flags
  131. int _flags;
  132. // flags indicating that a value is relative to model size
  133. int _relativeFlags;
  134. inline bool getRelativeFlag( int index ) const;
  135. inline void setRelativeFlag( int index, bool value );
  136. static int numRelativeFlagsAllocated;
  137. static int allocateRelativeFlag();
  138. };
  139. //
  140. // inline methods
  141. //
  142. inline bool StandardManipulator::getRelativeFlag( int index ) const
  143. {
  144. return ( _relativeFlags & (0x01 << index) ) != 0;
  145. }
  146. inline void StandardManipulator::setRelativeFlag( int index, bool value )
  147. {
  148. if( value ) _relativeFlags |= (0x01 << index);
  149. else _relativeFlags &= ~(0x01 << index);
  150. }
  151. /// Returns whether manipulator preserves camera's "UP" vector.
  152. inline bool StandardManipulator::getVerticalAxisFixed() const
  153. {
  154. return _verticalAxisFixed;
  155. }
  156. /// Returns true if the camera can be thrown, false otherwise. It defaults to true.
  157. inline bool StandardManipulator::getAllowThrow() const
  158. {
  159. return _allowThrow;
  160. }
  161. }
  162. #endif /* OSGGA_CAMERA_MANIPULATOR */