root/OpenSceneGraph/trunk/src/osgGA/AnimationPathManipulator.cpp @ 13041

Revision 13041, 5.3 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgGA/AnimationPathManipulator>
2
3#include <osgDB/fstream>
4
5using namespace osgGA;
6
7AnimationPathManipulator::AnimationPathManipulator(osg::AnimationPath* animationPath)
8{
9    _printOutTimingInfo = true;
10
11    _animationPath = animationPath;
12    _timeOffset = 0.0;
13    _timeScale = 1.0;
14    _isPaused = false;
15
16    _realStartOfTimedPeriod = 0.0;
17    _animStartOfTimedPeriod = 0.0;
18    _numOfFramesSinceStartOfTimedPeriod = -1; // need to init.
19}
20
21AnimationPathManipulator::AnimationPathManipulator( const std::string& filename )
22{
23    _printOutTimingInfo = true;
24
25    _animationPath = new osg::AnimationPath;
26    _animationPath->setLoopMode(osg::AnimationPath::LOOP);
27    _timeOffset = 0.0;
28    _timeScale = 1.0;
29    _isPaused = false;
30
31
32    osgDB::ifstream in(filename.c_str());
33
34    if (!in)
35    {
36        OSG_WARN << "AnimationPathManipulator: Cannot open animation path file \"" << filename << "\".\n";
37        _valid = false;
38        return;
39    }
40
41    _animationPath->read(in);
42
43    in.close();
44
45}
46
47void AnimationPathManipulator::home(double currentTime)
48{
49    if (_animationPath.valid())
50    {
51        _timeOffset = _animationPath->getFirstTime()-currentTime;
52
53    }
54    // reset the timing of the animation.
55    _numOfFramesSinceStartOfTimedPeriod=-1;
56}
57
58void AnimationPathManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter&)
59{
60    home(ea.getTime());
61}
62
63void AnimationPathManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& aa)
64{
65    home(ea,aa);
66}
67
68bool AnimationPathManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
69{
70    if( !valid() ) return false;
71
72    switch( ea.getEventType() )
73    {
74    case GUIEventAdapter::FRAME:
75        if( _isPaused )
76        {
77            handleFrame( _pauseTime );
78        }
79        else
80        {
81            handleFrame( ea.getTime() );
82        }
83        return false;
84    case GUIEventAdapter::KEYDOWN:
85            if (ea.getKey()==' ')
86            {
87                _isPaused = false;
88
89                home(ea,us);
90                us.requestRedraw();
91                us.requestContinuousUpdate(false);
92
93                return true;
94            }
95            else if (ea.getKey()==')')
96            {
97                double time = _isPaused ? _pauseTime : ea.getTime();
98                double animationTime = (time+_timeOffset)*_timeScale;
99
100                _timeScale *= 1.1;
101
102                OSG_NOTICE<<"Animation speed = "<<_timeScale*100<<"%"<<std::endl;
103
104                // adjust timeOffset so the current animationTime does change.
105                _timeOffset = animationTime/_timeScale - time;
106
107                return true;
108            }
109            else if (ea.getKey()=='(')
110            {
111                double time = _isPaused ? _pauseTime : ea.getTime();
112                double animationTime = (time+_timeOffset)*_timeScale;
113
114                _timeScale /= 1.1;
115
116                OSG_NOTICE<<"Animation speed = "<<_timeScale*100<<"%"<<std::endl;
117
118                // adjust timeOffset so the current animationTime does change.
119                _timeOffset = animationTime/_timeScale - time;
120
121                return true;
122            }
123            else if(ea.getKey() == 'p')
124            {
125                if( _isPaused )
126                {
127                    _isPaused = false;
128                    _timeOffset -= ea.getTime() - _pauseTime;
129                }
130                else
131                {
132                    _isPaused = true;
133                    _pauseTime = ea.getTime();
134                }
135                us.requestRedraw();
136                us.requestContinuousUpdate(false);
137                return true;
138            }
139
140        break;
141        default:
142            break;
143    }
144    return false;
145}
146
147void AnimationPathManipulator::getUsage(osg::ApplicationUsage& usage) const
148{
149    usage.addKeyboardMouseBinding("AnimationPath: Space","Reset the viewing position to start of animation");
150    usage.addKeyboardMouseBinding("AnimationPath: p","Pause/resume animation.");
151    usage.addKeyboardMouseBinding("AnimationPath: (","Slow down animation speed.");
152    usage.addKeyboardMouseBinding("AnimationPath: )","Speed up animation speed.");
153}
154
155void AnimationPathManipulator::handleFrame( double time )
156{
157    osg::AnimationPath::ControlPoint cp;
158
159    double animTime = (time+_timeOffset)*_timeScale;
160    _animationPath->getInterpolatedControlPoint( animTime, cp );
161
162    if (_numOfFramesSinceStartOfTimedPeriod==-1)
163    {
164        _realStartOfTimedPeriod = time;
165        _animStartOfTimedPeriod = animTime;
166
167    }
168
169    ++_numOfFramesSinceStartOfTimedPeriod;
170
171    double animDelta = (animTime-_animStartOfTimedPeriod);
172    if (animDelta>=_animationPath->getPeriod())
173    {
174        if (_animationCompletedCallback.valid())
175        {
176            _animationCompletedCallback->completed(this);
177        }
178
179        if (_printOutTimingInfo)
180        {
181            double delta = time-_realStartOfTimedPeriod;
182            double frameRate = (double)_numOfFramesSinceStartOfTimedPeriod/delta;
183            OSG_NOTICE <<"AnimatonPath completed in "<<delta<<" seconds, completing "<<_numOfFramesSinceStartOfTimedPeriod<<" frames,"<<std::endl;
184            OSG_NOTICE <<"             average frame rate = "<<frameRate<<std::endl;
185        }
186
187        // reset counters for next loop.
188        _realStartOfTimedPeriod = time;
189        _animStartOfTimedPeriod = animTime;
190        _numOfFramesSinceStartOfTimedPeriod = 0;
191    }
192
193    cp.getMatrix( _matrix );
194}
Note: See TracBrowser for help on using the browser.