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

Revision 13041, 4.0 kB (checked in by robert, 2 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/AnimationManagerBase>
16#include <osgAnimation/LinkVisitor>
17#include <algorithm>
18
19using namespace osgAnimation;
20
21AnimationManagerBase::~AnimationManagerBase() {}
22
23AnimationManagerBase::AnimationManagerBase()
24{
25    _needToLink = false;
26    _automaticLink = true;
27}
28
29void AnimationManagerBase::clearTargets()
30{
31    for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); ++it)
32        (*it).get()->reset();
33}
34
35void AnimationManagerBase::dirty()
36{
37    _needToLink = true;
38}
39
40void AnimationManagerBase::setAutomaticLink(bool state) { _automaticLink = state; }
41bool AnimationManagerBase::getAutomaticLink() const { return _automaticLink; }
42
43void AnimationManagerBase::operator()(osg::Node* node, osg::NodeVisitor* nv)
44{
45    if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
46    {
47        if (needToLink())
48        {
49            /** manager need to link, it means that an animation has been added
50                so we need to relink all item animated with all animations.
51                We apply the linker visitor on the manager node to affect
52                all its children.
53                But it should not be done here, it should be done in the
54                update of AnimationManager
55            */
56            link(node);
57        }
58        const osg::FrameStamp* fs = nv->getFrameStamp();
59        update(fs->getSimulationTime());
60    }
61    traverse(node,nv);
62}
63
64
65AnimationManagerBase::AnimationManagerBase(const AnimationManagerBase& b, const osg::CopyOp& copyop) : osg::NodeCallback(b,copyop)
66{
67    const AnimationList& animationList = b.getAnimationList();
68    for (AnimationList::const_iterator it = animationList.begin();
69         it != animationList.end();
70         ++it)
71    {
72        Animation* animation = dynamic_cast<osgAnimation::Animation*>(it->get()->clone(copyop));
73        _animations.push_back(animation);
74    }
75    _needToLink = true;
76    _automaticLink = b._automaticLink;
77    buildTargetReference();
78}
79
80void AnimationManagerBase::buildTargetReference()
81{
82    _targets.clear();
83    for( AnimationList::iterator iterAnim = _animations.begin(); iterAnim != _animations.end(); ++iterAnim )
84    {
85        Animation* anim = (*iterAnim).get();
86        for (ChannelList::iterator it = anim->getChannels().begin();
87             it != anim->getChannels().end();
88             ++it)
89            _targets.insert((*it)->getTarget());
90    }
91}
92
93
94void AnimationManagerBase::registerAnimation (Animation* animation)
95{
96    _needToLink = true;
97    _animations.push_back(animation);
98    buildTargetReference();
99}
100
101void AnimationManagerBase::unregisterAnimation (Animation* animation)
102{
103    AnimationList::iterator it = std::find(_animations.begin(), _animations.end(), animation);
104    if (it != _animations.end())
105    {
106        _animations.erase(it);
107    }
108    buildTargetReference();
109}
110
111bool AnimationManagerBase::needToLink() const { return _needToLink && getAutomaticLink(); }
112
113
114void AnimationManagerBase::setLinkVisitor(LinkVisitor* visitor)
115{
116    _linker = visitor;
117}
118
119LinkVisitor* AnimationManagerBase::getOrCreateLinkVisitor()
120{
121    if (!_linker.valid())
122        _linker = new LinkVisitor;
123    return _linker.get();
124}
125
126void AnimationManagerBase::link(osg::Node* subgraph)
127{
128    LinkVisitor* linker = getOrCreateLinkVisitor();
129    linker->getAnimationList().clear();
130    linker->getAnimationList() = _animations;
131
132    subgraph->accept(*linker);
133    _needToLink = false;
134    buildTargetReference();
135}
Note: See TracBrowser for help on using the browser.