25 #include "WGEZoomTrackballManipulator.h"
26 #include "WGraphicsEngine.h"
29 TrackballManipulator( 0x02 ),
31 m_allowThrow( false ),
34 setTrackballSize( .3 );
39 m_zoom = 1.0 / matrix.getScale()[0];
42 TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 /
m_zoom, 1.0 /
m_zoom, 1.0 /
m_zoom ) ) * matrix );
47 return osg::Matrixd::scale( 1.0 /
m_zoom, 1.0 /
m_zoom, 1.0 /
m_zoom ) * TrackballManipulator::getMatrix();
52 return TrackballManipulator::getMatrix();
57 return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale(
m_zoom,
m_zoom,
m_zoom );
63 TrackballManipulator::home( 0 );
68 double zoomDelta = 0.0;
70 if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
72 if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus )
76 if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
80 if( zoomDelta != 0.0 )
93 switch( ea.getScrollingMotion() )
95 case osgGA::GUIEventAdapter::SCROLL_UP:
98 case osgGA::GUIEventAdapter::SCROLL_DOWN:
101 case osgGA::GUIEventAdapter::SCROLL_2D:
102 zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
114 if( zoomDelta != 0.0 )
116 m_zoom *= 1.0 + zoomDelta;
120 us.requestContinuousUpdate(
false );
135 else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL
136 || ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus
137 || ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
139 return zoom( ea, us );
143 else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
145 if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
149 else if( ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && (
m_paintMode == 1 ) )
155 return TrackballManipulator::handle( ea, us );
160 return TrackballManipulator::handle( ea, us );
183 osg::BoundingSphere boundingSphere;
188 getNode()->accept( cbVisitor );
193 boundingSphere.expandBy( bb );
197 boundingSphere = getNode()->getBound();
202 boundingSphere = getNode()->getBound();
205 m_radius = osg::maximum( (
double ) boundingSphere.radius(), 1e-6 );
208 setHomePosition( boundingSphere.center() + osg::Vec3d( 0.0, -
m_radius, 0.0 ),
209 boundingSphere.center(),
210 osg::Vec3d( 0.0, 0.0, 1.0 ),
211 _autoComputeHomePosition );
224 double radiusFac[] = {
234 osg::Vec3 center = _homeCenter;
235 osg::Matrix transf = camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
236 double fac = INFINITY;
237 for(
int i = 0; i < 8; i++ )
239 osg::Vec3d vec = osg::Vec3( center.x() +
m_radius * radiusFac[ i * 3 ],
240 center.y() +
m_radius * radiusFac[ i * 3 + 1 ],
241 center.z() +
m_radius * radiusFac[ i * 3 + 2 ] );
242 osg::Vec3d screenCoords = vec * transf;
244 double facX = abs( camera->getViewport()->width() / screenCoords.x() );
245 double facY = abs( camera->getViewport()->height() / screenCoords.y() );
247 fac = fmin( fac, facX );
248 fac = fmin( fac, facY );
258 float scale = -0.5 * ( _distance /
m_zoom ) * getThrowScale( eventTimeDelta );
259 panModel( dx * scale, dy * scale );
bool getThrow() const
Checks whether throwing is active.
double m_zoom
Zoom factor.
void setThrow(bool allowThrow=true)
En-/Disables throwing.
virtual bool performMovementMiddleMouseButton(const double eventTimeDelta, const double dx, const double dy)
Performs the middle mouse button click.
virtual void computeHomePosition(const osg::Camera *camera, bool useBoundingBox)
Computes the home position.
void fitToScreen(const osg::Camera *camera)
Zooms and centers to fit screen.
bool zoom(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handles events related to zooming.
bool m_allowThrow
Do we want the auto-rotation thingy?
virtual osg::Matrixd getInverseMatrix() const
Get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model...
WGEZoomTrackballManipulator()
Default constructor.
double m_radius
The radius of the bounding sphere.
virtual void setByMatrix(const osg::Matrixd &matrix)
Set the position of the manipulator using a 4x4 matrix.
virtual osg::Matrixd getMatrix() const
Get the position of the manipulator as 4x4 matrix.
int m_paintMode
paint mode
virtual void home(double currentTime)
Move the camera to the default position.
virtual osg::Matrixd getMatrixWithoutZoom() const
Get the manipulator only containing rotation and translation.
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handle events, return true if handled, false otherwise.
void setPaintMode(int mode)
setter for paint mode when set to something different from 0, a left drag should move the scene
A visitor that ignores specific nodes that are irrelevant for the trackball.
osg::BoundingBox & getBoundingBox()
Gets the bounding box.
static std::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.