OpenWalnut  1.4.0
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         if( zoomDelta != 0.0 )
00081         {
00082             m_zoom *= 1.0 + zoomDelta;
00083             us.requestRedraw();
00084         }
00085     }
00086     else
00087     {
00088         if( ea.getHandled() )
00089         {
00090             return true;
00091         }
00092 
00093         switch( ea.getScrollingMotion() )
00094         {
00095             case osgGA::GUIEventAdapter::SCROLL_UP:
00096                 zoomDelta = 0.05;
00097                 break;
00098             case osgGA::GUIEventAdapter::SCROLL_DOWN:
00099                 zoomDelta = -0.05;
00100                 break;
00101             case osgGA::GUIEventAdapter::SCROLL_2D:
00102                 zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
00103                 break;
00104                 // case osgGA::GUIEventAdapter::SCROLL_LEFT:
00105                 // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
00106                 // case osgGA::GUIEventAdapter::SCROLL_NONE:
00107             default:
00108                 // do nothing
00109                 zoomDelta = 0.0;
00110                 break;
00111         }
00112     }
00113 
00114     if( zoomDelta != 0.0 )
00115     {
00116         m_zoom *= 1.0 + zoomDelta;
00117         us.requestRedraw();
00118     }
00119 
00120     us.requestContinuousUpdate( false );
00121     return true;
00122 }
00123 
00124 bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
00125 {
00126     _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
00127 
00128     if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
00129     {
00130         // We set the scene to the manipulator home position if the scene
00131         // requests to do so. See WGEScene for more details.
00132         home( 0 );
00133         return true;
00134     }
00135     else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL || ea.getKey() == 45 ||  ea.getKey() == 43 )
00136     {
00137         return zoom( ea, us );
00138     }
00139     // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
00140     // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
00141     else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
00142     {
00143         if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
00144         {
00145             return true;
00146         }
00147         else if(  ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
00148         {
00149             return true;
00150         }
00151         else
00152         {
00153             return TrackballManipulator::handle( ea, us );
00154         }
00155     }
00156     else
00157     {
00158         return TrackballManipulator::handle( ea, us );
00159     }
00160 }
00161 
00162 void WGEZoomTrackballManipulator::setPaintMode( int mode )
00163 {
00164     m_paintMode = mode;
00165 }
00166 
00167 void WGEZoomTrackballManipulator::setThrow( bool allowThrow )
00168 {
00169     m_allowThrow = allowThrow;
00170 }
00171 
00172 bool WGEZoomTrackballManipulator::getThrow() const
00173 {
00174     return m_allowThrow;
00175 }
00176