openscenegraph
StandardManipulator
Go to the documentation of this file.
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
19#ifndef OSGGA_CAMERA_MANIPULATOR
20#define OSGGA_CAMERA_MANIPULATOR 1
21
22#include <osgGA/CameraManipulator>
23
24
25namespace osgGA {
26
27
28/** StandardManipulator class provides basic functionality
29 for user controlled manipulation.*/
30class OSGGA_EXPORT StandardManipulator : public CameraManipulator
31{
32 typedef CameraManipulator inherited;
33
34 public:
35
36 // flags
37 enum UserInteractionFlags
38 {
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
44 };
45
46 StandardManipulator( int flags = DEFAULT_SETTINGS );
47 StandardManipulator( const StandardManipulator& m,
48 const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
49
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"; }
53
54 /** Sets manipulator by eye position and eye orientation.*/
55 virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation ) = 0;
56
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;
59
60 /** Gets manipulator's eye position and eye orientation.*/
61 virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const = 0;
62
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;
65
66 virtual void setNode( osg::Node* );
67 virtual const osg::Node* getNode() const;
68 virtual osg::Node* getNode();
69
70 virtual void setVerticalAxisFixed( bool value );
71 inline bool getVerticalAxisFixed() const;
72 inline bool getAllowThrow() const;
73 virtual void setAllowThrow( bool allowThrow );
74
75 virtual void setAnimationTime( const double t );
76 double getAnimationTime() const;
77 bool isAnimating() const;
78 virtual void finishAnimation();
79
80 virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
81 virtual void home( double );
82
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;
86
87 protected:
88
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 );
99
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 );
107
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 );
113
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 );
122
123 // mouse state
124 bool _thrown;
125 bool _allowThrow;
126 float _mouseCenterX, _mouseCenterY;
127
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;
131
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.
135 */
136 double _delta_frame_time;
137
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.
140 */
141 double _last_frame_time;
142
143 // scene data
144 osg::ref_ptr< osg::Node > _node;
145 double _modelSize;
146 bool _verticalAxisFixed;
147
148 // animation stuff
149 class OSGGA_EXPORT AnimationData : public osg::Referenced {
150 public:
151 double _animationTime;
152 bool _isAnimating;
153 double _startTime;
154 double _phase;
155
156 AnimationData();
157 void start( const double startTime );
158 };
159 osg::ref_ptr< AnimationData > _animationData;
160 virtual void allocAnimationData() { _animationData = new AnimationData(); }
161
162 // flags
163 int _flags;
164
165 // flags indicating that a value is relative to model size
166 int _relativeFlags;
167 inline bool getRelativeFlag( int index ) const;
168 inline void setRelativeFlag( int index, bool value );
169 static int numRelativeFlagsAllocated;
170 static int allocateRelativeFlag();
171};
172
173
174//
175// inline methods
176//
177
178inline bool StandardManipulator::getRelativeFlag( int index ) const
179{
180 return ( _relativeFlags & (0x01 << index) ) != 0;
181}
182
183inline void StandardManipulator::setRelativeFlag( int index, bool value )
184{
185 if( value ) _relativeFlags |= (0x01 << index);
186 else _relativeFlags &= ~(0x01 << index);
187}
188
189/// Returns whether manipulator preserves camera's "UP" vector.
190inline bool StandardManipulator::getVerticalAxisFixed() const
191{
192 return _verticalAxisFixed;
193}
194
195/// Returns true if the camera can be thrown, false otherwise. It defaults to true.
196inline bool StandardManipulator::getAllowThrow() const
197{
198 return _allowThrow;
199}
200
201
202}
203
204#endif /* OSGGA_CAMERA_MANIPULATOR */