~saiarcot895/ubuntu/trusty/openscenegraph/armhf-support

« back to all changes in this revision

Viewing changes to OpenSceneGraph/src/osgAnimation/AnimationManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Loic Dachary (OuoU)
  • Date: 2009-03-23 14:08:20 UTC
  • mfrom: (1.1.7 upstream) (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090323140820-i4j3jozdlhyn4lre
rules prevent lib64 with -D LIB_POSTFIX="" (Closes: #517671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  -*-c++-*- 
 
2
 *  Copyright (C) 2008 Cedric Pinson <mornifle@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/AnimationManager>
 
16
#include <osgAnimation/LinkVisitor>
 
17
 
 
18
using namespace osgAnimation;
 
19
 
 
20
 
 
21
AnimationManager::~AnimationManager() {}
 
22
 
 
23
 
 
24
void AnimationManager::stopAll()
 
25
{
 
26
    // loop over all playing animation
 
27
    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim ) 
 
28
    {
 
29
        AnimationList& list = iterAnim->second;
 
30
        for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
 
31
            (*it)->resetTargets();
 
32
    }
 
33
    _animationsPlaying.clear();
 
34
}
 
35
 
 
36
AnimationManager::AnimationManager()
 
37
{
 
38
    _lastUpdate = 0; 
 
39
}
 
40
void AnimationManager::playAnimation(Animation* pAnimation, int priority, float weight)
 
41
{
 
42
    if (!findAnimation(pAnimation))
 
43
    {
 
44
        return;
 
45
    }
 
46
 
 
47
    if ( isPlaying(pAnimation) )
 
48
        stopAnimation(pAnimation);
 
49
  
 
50
    _animationsPlaying[priority].push_back(pAnimation);
 
51
    pAnimation->setStartTime(_lastUpdate);
 
52
    pAnimation->setWeight(weight);
 
53
}
 
54
 
 
55
bool AnimationManager::stopAnimation(Animation* pAnimation)
 
56
{
 
57
    // search though the layer and remove animation
 
58
    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim ) 
 
59
    {
 
60
        AnimationList& list = iterAnim->second;
 
61
        for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
 
62
            if( (*it) == pAnimation )
 
63
            {
 
64
                (*it)->resetTargets();
 
65
                list.erase(it);
 
66
                return true;
 
67
            }
 
68
    }
 
69
    return false;
 
70
}
 
71
 
 
72
 
 
73
void AnimationManager::update (double time)
 
74
{
 
75
    if (!_lastUpdate)
 
76
        _lastUpdate = time;
 
77
 
 
78
    // could filtered with an active flag
 
79
    for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
 
80
        (*it).get()->reset();
 
81
 
 
82
 
 
83
    // update from high priority to low priority
 
84
    for( AnimationLayers::reverse_iterator iterAnim = _animationsPlaying.rbegin(); iterAnim != _animationsPlaying.rend(); ++iterAnim )
 
85
    {
 
86
        // update all animation
 
87
        std::vector<int> toremove;
 
88
        AnimationList& list = iterAnim->second;
 
89
        for (unsigned int i = 0; i < list.size(); i++)
 
90
        {
 
91
            if (! list[i]->update(time))
 
92
                toremove.push_back(i);
 
93
        }
 
94
 
 
95
        // remove finished animation
 
96
        while (!toremove.empty())
 
97
        {
 
98
            list.erase(list.begin() + toremove.back());
 
99
            toremove.pop_back();
 
100
        }
 
101
    }
 
102
 
 
103
    for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
 
104
        (*it).get()->normalize();
 
105
}
 
106
 
 
107
 
 
108
bool AnimationManager::findAnimation(Animation* pAnimation) 
 
109
{
 
110
    for( AnimationList::const_iterator iterAnim = _animations.begin(); iterAnim != _animations.end(); ++iterAnim ) 
 
111
    {
 
112
        if ( (*iterAnim) == pAnimation )
 
113
            return true;
 
114
    }
 
115
    return false;
 
116
}
 
117
 
 
118
 
 
119
bool AnimationManager::isPlaying(Animation* pAnimation) 
 
120
{
 
121
    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
 
122
    {
 
123
        AnimationList& list = iterAnim->second;
 
124
        for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
 
125
            if ( (*it) == pAnimation )
 
126
                return true;
 
127
    }
 
128
    return false;
 
129
}
 
130
 
 
131
bool AnimationManager::isPlaying(const std::string& name)
 
132
{
 
133
    // loop over all playing animation
 
134
    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
 
135
    {
 
136
        AnimationList& list = iterAnim->second;
 
137
        for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
 
138
            if ( (*it)->getName() == name )
 
139
                return true;
 
140
    }
 
141
    return false;
 
142
}