OpenWalnut  1.4.0
WGEAnimationManipulator.h
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 #ifndef WGEANIMATIONMANIPULATOR_H
00026 #define WGEANIMATIONMANIPULATOR_H
00027 
00028 #include <osg/Version>
00029 
00030 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
00031 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
00032 #if ( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
00033                             ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
00034     #include <osgGA/CameraManipulator>
00035     namespace osgGA
00036     {
00037         typedef CameraManipulator MatrixManipulator;
00038     }
00039 #else
00040     #include <osgGA/MatrixManipulator>
00041 #endif
00042 
00043 #include "../../common/WTimer.h"
00044 #include "../../common/WRealtimeTimer.h"
00045 
00046 
00047 
00048 /**
00049  * New OSG manipulator: AnimationManipulator. Can play back animation paths (not osg::AnimationPath),
00050  */
00051 class WGEAnimationManipulator: public osgGA::MatrixManipulator
00052 {
00053 public:
00054     /**
00055      * Convenience typedef
00056      */
00057     typedef osg::ref_ptr< WGEAnimationManipulator > RefPtr;
00058 
00059     /**
00060      * Convenience typedef
00061      */
00062     typedef osg::ref_ptr< const WGEAnimationManipulator > ConstRefPtr;
00063 
00064     /**
00065      * Constructs a animation path manipulator using a realtime timer if not specified.
00066      *
00067      * \param timer the timer type
00068      */
00069     WGEAnimationManipulator( WTimer::ConstSPtr timer = WTimer::ConstSPtr( new WRealtimeTimer() ) );
00070 
00071     /**
00072      * Destructor.
00073      */
00074     virtual ~WGEAnimationManipulator();
00075 
00076     /**
00077      * Sets the current matrix of this animation manipulator. This most probably gets overwritten in the next frame by the current animation
00078      * matrix.
00079      *
00080      * \param matrix the matrix to set
00081      */
00082     virtual void setByMatrix( const osg::Matrixd& matrix );
00083 
00084     /**
00085      * Sets the current inverse matrix of this animation manipulator. This is, in most cases, the modelview matrix.
00086      * This most probably gets overwritten in the next frame by the current animation
00087      * matrix.
00088      *
00089      * \param matrix the matrix to set
00090      */
00091     virtual void setByInverseMatrix( const osg::Matrixd& matrix );
00092 
00093     /**
00094      * Gets the current animation matrix for the current time-step.
00095      *
00096      * \return the matrix.
00097      */
00098     virtual osg::Matrixd getMatrix() const;
00099 
00100     /**
00101      * Gets the current inverse animation matrix for the current time-step. In most cases, this should be used as modelview matrix.
00102      *
00103      * \return the matrix
00104      */
00105     virtual osg::Matrixd getInverseMatrix() const;
00106 
00107     /**
00108      * Handles incoming events send by the event dispatcher of the view.
00109      *
00110      * \param ea event adapter
00111      * \param us action adapter allowing interaction with the event dispatcher
00112      *
00113      * \return true if the event was handled
00114      */
00115     virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
00116 
00117     /**
00118      * Initializes this manipulator. This simply calls home( 0 ).
00119      *
00120      * \param ea event adapter
00121      * \param us action adapter allowing interaction with the event dispatcher
00122      */
00123     virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
00124 
00125     /**
00126      * Sets the manipulator back to its default.
00127      *
00128      * \param ea event adapter
00129      * \param us action adapter allowing interaction with the event dispatcher
00130      */
00131     virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
00132 
00133     /**
00134      * Sets the animation callback to a certain time.
00135      *
00136      * \param currentTime the time to which the manipulator should be set.
00137      */
00138     virtual void home( double currentTime );
00139 
00140     /**
00141      * Allows to switch the timer type. It continues animation at the current timer position.
00142      *
00143      * \param timer the timer
00144      */
00145     virtual void setTimer( WTimer::ConstSPtr timer );
00146 
00147 private:
00148     /**
00149      * The view matrix. Gets modified on a per-frame basis.
00150      */
00151     osg::Matrixd m_matrix;
00152 
00153     /**
00154      * This timer keeps track of the current animation-time.
00155      */
00156     WTimer::ConstSPtr m_timer;
00157 
00158     /**
00159      * If home() is called, the homeOffsetTime stores the timers current value.
00160      */
00161     double m_homeOffsetTime;
00162 
00163     /**
00164      * This method updates m_matrix per frame according to time elapsed.
00165      */
00166     void handleFrame();
00167 
00168     /**
00169      * If true, the animation is suspended.
00170      */
00171     bool m_paused;
00172 };
00173 
00174 #endif  // WGEANIMATIONMANIPULATOR_H