WGEZoomTrackballManipulator.cpp

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include "WGEZoomTrackballManipulator.h"
00026 #include "WGraphicsEngine.h"
00027 
00028 WGEZoomTrackballManipulator::WGEZoomTrackballManipulator():
00029     TrackballManipulator(),
00030     m_zoom( 1.0 ),
00031     m_allowThrow( false ),
00032     m_paintMode( 0 )
00033 {
00034     setTrackballSize( .3 ); // changes the effect of a mouse move for rotation
00035 }
00036 
00037 void WGEZoomTrackballManipulator::setByMatrix( const osg::Matrixd& matrix )
00038 {
00039     m_zoom = 1.0 / matrix.getScale()[0];
00040 
00041     // The zoom needs to be undone before forwarding the matrix.
00042     TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) ) * matrix );
00043 }
00044 
00045 osg::Matrixd WGEZoomTrackballManipulator::getMatrix() const
00046 {
00047     return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) * TrackballManipulator::getMatrix();
00048 }
00049 
00050 osg::Matrixd WGEZoomTrackballManipulator::getMatrixWithoutZoom() const
00051 {
00052     return TrackballManipulator::getMatrix();
00053 }
00054 
00055 osg::Matrixd WGEZoomTrackballManipulator::getInverseMatrix() const
00056 {
00057     return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale( m_zoom, m_zoom, m_zoom );
00058 }
00059 
00060 void WGEZoomTrackballManipulator::home( double /* currentTime */ )
00061 {
00062     m_zoom = 1.0;
00063     TrackballManipulator::home( 0 /* currentTime */ );
00064 }
00065 
00066 bool WGEZoomTrackballManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
00067 {
00068     double zoomDelta = 0.0;
00069 
00070     if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
00071     {
00072         if( ea.getKey() == 45 ) // -
00073         {
00074             zoomDelta = -0.05;
00075         }
00076         if( ea.getKey() == 43 ) // +
00077         {
00078             zoomDelta = 0.05;
00079         }
00080 
00081         if(zoomDelta != 0.0)
00082         {
00083             m_zoom *= 1.0 + zoomDelta;
00084             us.requestRedraw();
00085         }
00086     }
00087     else
00088     {
00089         switch( ea.getScrollingMotion() )
00090         {
00091             case osgGA::GUIEventAdapter::SCROLL_UP:
00092                 zoomDelta = 0.05;
00093                 break;
00094             case osgGA::GUIEventAdapter::SCROLL_DOWN:
00095                 zoomDelta = -0.05;
00096                 break;
00097             case osgGA::GUIEventAdapter::SCROLL_2D:
00098                 zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
00099                 break;
00100                 // case osgGA::GUIEventAdapter::SCROLL_LEFT:
00101                 // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
00102                 // case osgGA::GUIEventAdapter::SCROLL_NONE:
00103             default:
00104                 // do nothing
00105                 zoomDelta = 0.0;
00106                 break;
00107         }
00108     }
00109 
00110     if(zoomDelta != 0.0)
00111     {
00112         m_zoom *= 1.0 + zoomDelta;
00113         us.requestRedraw();
00114     }
00115 
00116     us.requestContinuousUpdate( false );
00117     return true;
00118 }
00119 
00120 bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
00121 {
00122     _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
00123 
00124     if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
00125     {
00126         // We set the scene to the manipulator home position if the scene
00127         // requests to do so. See WGEScene for more details.
00128         home( 0 );
00129         return true;
00130     }
00131     else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL || ea.getKey() == 45 ||  ea.getKey() == 43 )
00132     {
00133         return zoom( ea, us );
00134     }
00135     // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
00136     // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
00137     else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
00138     {
00139         if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
00140         {
00141             return true;
00142         }
00143         else if(  ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
00144         {
00145             return true;
00146         }
00147         else
00148         {
00149             return TrackballManipulator::handle( ea, us );
00150         }
00151     }
00152     else
00153     {
00154         return TrackballManipulator::handle( ea, us );
00155     }
00156 }
00157 
00158 void WGEZoomTrackballManipulator::setPaintMode( int mode )
00159 {
00160     m_paintMode = mode;
00161 }
00162 
00163 void WGEZoomTrackballManipulator::setThrow( bool allowThrow )
00164 {
00165     m_allowThrow = allowThrow;
00166 }
00167 
00168 bool WGEZoomTrackballManipulator::getThrow() const
00169 {
00170     return m_allowThrow;
00171 }
00172 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends