1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
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.
19#ifndef OSGGA_CAMERA_MANIPULATOR
20#define OSGGA_CAMERA_MANIPULATOR 1
22#include <osgGA/CameraManipulator>
28/** StandardManipulator class provides basic functionality
29 for user controlled manipulation.*/
30class OSGGA_EXPORT StandardManipulator : public CameraManipulator
32 typedef CameraManipulator inherited;
37 enum UserInteractionFlags
39 UPDATE_MODEL_SIZE = 0x01,
40 COMPUTE_HOME_USING_BBOX = 0x02,
41 PROCESS_MOUSE_WHEEL = 0x04,
42 SET_CENTER_ON_WHEEL_FORWARD_MOVEMENT = 0x08,
43 DEFAULT_SETTINGS = UPDATE_MODEL_SIZE /*| COMPUTE_HOME_USING_BBOX*/ | PROCESS_MOUSE_WHEEL
46 StandardManipulator( int flags = DEFAULT_SETTINGS );
47 StandardManipulator( const StandardManipulator& m,
48 const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
50 // We are not using META_Object as this is abstract class.
51 // Use META_Object(osgGA,YourManipulator); in your descendant non-abstract classes.
52 virtual const char* className() const { return "StandardManipulator"; }
54 /** Sets manipulator by eye position and eye orientation.*/
55 virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation ) = 0;
57 /** Sets manipulator by eye position, center of rotation, and up vector.*/
58 virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up ) = 0;
60 /** Gets manipulator's eye position and eye orientation.*/
61 virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const = 0;
63 /** Gets manipulator's focal center, eye position, and up vector.*/
64 virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const = 0;
66 virtual void setNode( osg::Node* );
67 virtual const osg::Node* getNode() const;
68 virtual osg::Node* getNode();
70 virtual void setVerticalAxisFixed( bool value );
71 inline bool getVerticalAxisFixed() const;
72 inline bool getAllowThrow() const;
73 virtual void setAllowThrow( bool allowThrow );
75 virtual void setAnimationTime( const double t );
76 double getAnimationTime() const;
77 bool isAnimating() const;
78 virtual void finishAnimation();
80 virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
81 virtual void home( double );
83 virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
84 virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
85 virtual void getUsage( osg::ApplicationUsage& usage ) const;
89 virtual bool handleFrame( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
90 virtual bool handleResize( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
91 virtual bool handleMouseMove( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
92 virtual bool handleMouseDrag( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
93 virtual bool handleMousePush( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
94 virtual bool handleMouseRelease( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
95 virtual bool handleKeyDown( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
96 virtual bool handleKeyUp( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
97 virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
98 virtual bool handleMouseDeltaMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
100 virtual bool performMovement();
101 virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
102 virtual bool performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy );
103 virtual bool performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy );
104 virtual bool performMouseDeltaMovement( const float dx, const float dy );
105 virtual bool performAnimationMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
106 virtual void applyAnimationStep( const double currentProgress, const double prevProgress );
108 void addMouseEvent( const osgGA::GUIEventAdapter& ea );
109 void flushMouseEventStack();
110 virtual bool isMouseMoving() const;
111 float getThrowScale( const double eventTimeDelta ) const;
112 virtual void centerMousePointer( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
114 static void rotateYawPitch( osg::Quat& rotation, const double yaw, const double pitch,
115 const osg::Vec3d& localUp = osg::Vec3d( 0.,0.,0.) );
116 static void fixVerticalAxis( osg::Quat& rotation, const osg::Vec3d& localUp, bool disallowFlipOver );
117 void fixVerticalAxis( osg::Vec3d& eye, osg::Quat& rotation, bool disallowFlipOver );
118 static void fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp,
119 const osg::Vec3d& localUp, bool disallowFlipOver );
120 virtual bool setCenterByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
121 virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
126 float _mouseCenterX, _mouseCenterY;
128 // internal event stack comprising last two mouse events.
129 osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t1;
130 osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t0;
132 /** The approximate amount of time it is currently taking to draw a frame.
133 * This is used to compute the delta in translation/rotation during a thrown display update.
134 * It allows us to match an delta in position/rotation independent of the rendering frame rate.
136 double _delta_frame_time;
138 /** The time the last frame started.
139 * Used when _rate_sensitive is true so that we can match display update rate to rotation/translation rate.
141 double _last_frame_time;
144 osg::ref_ptr< osg::Node > _node;
146 bool _verticalAxisFixed;
149 class OSGGA_EXPORT AnimationData : public osg::Referenced {
151 double _animationTime;
157 void start( const double startTime );
159 osg::ref_ptr< AnimationData > _animationData;
160 virtual void allocAnimationData() { _animationData = new AnimationData(); }
165 // flags indicating that a value is relative to model size
167 inline bool getRelativeFlag( int index ) const;
168 inline void setRelativeFlag( int index, bool value );
169 static int numRelativeFlagsAllocated;
170 static int allocateRelativeFlag();
178inline bool StandardManipulator::getRelativeFlag( int index ) const
180 return ( _relativeFlags & (0x01 << index) ) != 0;
183inline void StandardManipulator::setRelativeFlag( int index, bool value )
185 if( value ) _relativeFlags |= (0x01 << index);
186 else _relativeFlags &= ~(0x01 << index);
189/// Returns whether manipulator preserves camera's "UP" vector.
190inline bool StandardManipulator::getVerticalAxisFixed() const
192 return _verticalAxisFixed;
195/// Returns true if the camera can be thrown, false otherwise. It defaults to true.
196inline bool StandardManipulator::getAllowThrow() const
204#endif /* OSGGA_CAMERA_MANIPULATOR */