OpenWalnut  1.5.0dev
WGEAnimationManipulator.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEANIMATIONMANIPULATOR_H
26 #define WGEANIMATIONMANIPULATOR_H
27 
28 #include <osg/Version>
29 
30 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
31 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
32 #if( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
33  ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
34  #include <osgGA/CameraManipulator>
35  namespace osgGA
36  {
37  typedef CameraManipulator MatrixManipulator;
38  }
39 #else
40  #include <osgGA/MatrixManipulator>
41 #endif
42 
43 #include "../../common/WTimer.h"
44 #include "../../common/WRealtimeTimer.h"
45 
46 
47 
48 /**
49  * New OSG manipulator: AnimationManipulator. Can play back animation paths (not osg::AnimationPath),
50  */
51 class WGEAnimationManipulator: public osgGA::MatrixManipulator
52 {
53 public:
54  /**
55  * Convenience typedef
56  */
57  typedef osg::ref_ptr< WGEAnimationManipulator > RefPtr;
58 
59  /**
60  * Convenience typedef
61  */
62  typedef osg::ref_ptr< const WGEAnimationManipulator > ConstRefPtr;
63 
64  /**
65  * Constructs a animation path manipulator using a realtime timer if not specified.
66  *
67  * \param timer the timer type
68  */
70 
71  /**
72  * Destructor.
73  */
74  virtual ~WGEAnimationManipulator();
75 
76  /**
77  * Sets the current matrix of this animation manipulator. This most probably gets overwritten in the next frame by the current animation
78  * matrix.
79  *
80  * \param matrix the matrix to set
81  */
82  virtual void setByMatrix( const osg::Matrixd& matrix );
83 
84  /**
85  * Sets the current inverse matrix of this animation manipulator. This is, in most cases, the modelview matrix.
86  * This most probably gets overwritten in the next frame by the current animation
87  * matrix.
88  *
89  * \param matrix the matrix to set
90  */
91  virtual void setByInverseMatrix( const osg::Matrixd& matrix );
92 
93  /**
94  * Gets the current animation matrix for the current time-step.
95  *
96  * \return the matrix.
97  */
98  virtual osg::Matrixd getMatrix() const;
99 
100  /**
101  * Gets the current inverse animation matrix for the current time-step. In most cases, this should be used as modelview matrix.
102  *
103  * \return the matrix
104  */
105  virtual osg::Matrixd getInverseMatrix() const;
106 
107  /**
108  * Handles incoming events send by the event dispatcher of the view.
109  *
110  * \param ea event adapter
111  * \param us action adapter allowing interaction with the event dispatcher
112  *
113  * \return true if the event was handled
114  */
115  virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
116 
117  /**
118  * Initializes this manipulator. This simply calls home( 0 ).
119  *
120  * \param ea event adapter
121  * \param us action adapter allowing interaction with the event dispatcher
122  */
123  virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
124 
125  /**
126  * Sets the manipulator back to its default.
127  *
128  * \param ea event adapter
129  * \param us action adapter allowing interaction with the event dispatcher
130  */
131  virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
132 
133  /**
134  * Sets the animation callback to a certain time.
135  *
136  * \param currentTime the time to which the manipulator should be set.
137  */
138  virtual void home( double currentTime );
139 
140  /**
141  * Allows to switch the timer type. It continues animation at the current timer position.
142  *
143  * \param timer the timer
144  */
145  virtual void setTimer( WTimer::ConstSPtr timer );
146 
147 private:
148  /**
149  * The view matrix. Gets modified on a per-frame basis.
150  */
151  osg::Matrixd m_matrix;
152 
153  /**
154  * This timer keeps track of the current animation-time.
155  */
157 
158  /**
159  * If home() is called, the homeOffsetTime stores the timers current value.
160  */
162 
163  /**
164  * This method updates m_matrix per frame according to time elapsed.
165  */
166  void handleFrame();
167 };
168 
169 #endif // WGEANIMATIONMANIPULATOR_H
New OSG manipulator: AnimationManipulator.
osg::ref_ptr< WGEAnimationManipulator > RefPtr
Convenience typedef.
virtual void setByMatrix(const osg::Matrixd &matrix)
Sets the current matrix of this animation manipulator.
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handles incoming events send by the event dispatcher of the view.
void handleFrame()
This method updates m_matrix per frame according to time elapsed.
virtual osg::Matrixd getMatrix() const
Gets the current animation matrix for the current time-step.
virtual ~WGEAnimationManipulator()
Destructor.
osg::ref_ptr< const WGEAnimationManipulator > ConstRefPtr
Convenience typedef.
double m_homeOffsetTime
If home() is called, the homeOffsetTime stores the timers current value.
virtual void init(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Initializes this manipulator.
WTimer::ConstSPtr m_timer
This timer keeps track of the current animation-time.
virtual void setTimer(WTimer::ConstSPtr timer)
Allows to switch the timer type.
osg::Matrixd m_matrix
The view matrix.
virtual void setByInverseMatrix(const osg::Matrixd &matrix)
Sets the current inverse matrix of this animation manipulator.
virtual osg::Matrixd getInverseMatrix() const
Gets the current inverse animation matrix for the current time-step.
virtual void home(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Sets the manipulator back to its default.
WGEAnimationManipulator(WTimer::ConstSPtr timer=WTimer::ConstSPtr(new WRealtimeTimer()))
Constructs a animation path manipulator using a realtime timer if not specified.
Realtime timing.
std::shared_ptr< const WTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
Definition: WTimer.h:47