root/OpenSceneGraph/trunk/src/osgAnimation/BasicAnimationManager.cpp @ 13041

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

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15#include <osgAnimation/BasicAnimationManager>
16#include <osgAnimation/LinkVisitor>
17
18using namespace osgAnimation;
19
20BasicAnimationManager::BasicAnimationManager()
21: _lastUpdate(0.0)
22{
23}
24
25BasicAnimationManager::BasicAnimationManager(const AnimationManagerBase& b, const osg::CopyOp& copyop)
26: AnimationManagerBase(b,copyop)
27, _lastUpdate(0.0)
28{
29}
30
31BasicAnimationManager::~BasicAnimationManager()
32{
33}
34
35void BasicAnimationManager::stopAll()
36{
37    // loop over all playing animation
38    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
39    {
40        AnimationList& list = iterAnim->second;
41        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
42            (*it)->resetTargets();
43    }
44    _animationsPlaying.clear();
45}
46
47void BasicAnimationManager::playAnimation(Animation* pAnimation, int priority, float weight)
48{
49    if (!findAnimation(pAnimation))
50        return;
51
52    if ( isPlaying(pAnimation) )
53        stopAnimation(pAnimation);
54
55    _animationsPlaying[priority].push_back(pAnimation);
56    // for debug
57    //std::cout << "player Animation " << pAnimation->getName() << " at " << _lastUpdate << std::endl;
58    pAnimation->setStartTime(_lastUpdate);
59    pAnimation->setWeight(weight);
60}
61
62bool BasicAnimationManager::stopAnimation(Animation* pAnimation)
63{
64    // search though the layer and remove animation
65    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
66    {
67        AnimationList& list = iterAnim->second;
68        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
69            if( (*it) == pAnimation )
70            {
71                (*it)->resetTargets();
72                list.erase(it);
73                return true;
74            }
75    }
76    return false;
77}
78
79
80void BasicAnimationManager::update (double time)
81{
82    _lastUpdate = time; // keep time of last update
83
84    // could filtered with an active flag
85    for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); ++it)
86        (*it).get()->reset();
87
88    // update from high priority to low priority
89    for( AnimationLayers::reverse_iterator iterAnim = _animationsPlaying.rbegin(); iterAnim != _animationsPlaying.rend(); ++iterAnim )
90    {
91        // update all animation
92        std::vector<int> toremove;
93        int priority = iterAnim->first;
94        AnimationList& list = iterAnim->second;
95        for (unsigned int i = 0; i < list.size(); i++)
96        {
97            if (! list[i]->update(time, priority))
98            {
99                // debug
100                // std::cout << list[i]->getName() << " finished at " << time << std::endl;
101                toremove.push_back(i);
102            } else
103            {
104                // debug
105                //std::cout << list[i]->getName() << " updated" << std::endl;
106            }
107        }
108
109        // remove finished animation
110        while (!toremove.empty())
111        {
112            list.erase(list.begin() + toremove.back());
113            toremove.pop_back();
114        }
115    }
116}
117
118
119bool BasicAnimationManager::findAnimation(Animation* pAnimation)
120{
121    for( AnimationList::const_iterator iterAnim = _animations.begin(); iterAnim != _animations.end(); ++iterAnim )
122    {
123        if ( (*iterAnim) == pAnimation )
124            return true;
125    }
126    return false;
127}
128
129
130bool BasicAnimationManager::isPlaying(Animation* pAnimation)
131{
132    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
133    {
134        AnimationList& list = iterAnim->second;
135        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
136            if ( (*it) == pAnimation )
137                return true;
138    }
139    return false;
140}
141
142bool BasicAnimationManager::isPlaying(const std::string& name)
143{
144    // loop over all playing animation
145    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
146    {
147        AnimationList& list = iterAnim->second;
148        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
149            if ( (*it)->getName() == name )
150                return true;
151    }
152    return false;
153}
Note: See TracBrowser for help on using the browser.