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

Revision 13041, 5.2 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) 2009 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/Action>
16#include <osgAnimation/ActionBlendIn>
17#include <osgAnimation/ActionBlendOut>
18#include <osgAnimation/ActionStripAnimation>
19#include <osgAnimation/ActionAnimation>
20#include <osgAnimation/ActionVisitor>
21#include <osgAnimation/Timeline>
22
23using namespace osgAnimation;
24
25ActionVisitor::ActionVisitor()
26{
27    _currentLayer = 0;
28}
29void ActionVisitor::pushFrameActionOnStack(const FrameAction& fa) { _stackFrameAction.push_back(fa); }
30void ActionVisitor::popFrameAction() { _stackFrameAction.pop_back(); }
31void ActionVisitor::pushTimelineOnStack(Timeline* tm) { _stackTimeline.push_back(tm); }
32void ActionVisitor::popTimeline() { _stackTimeline.pop_back(); }
33void ActionVisitor::apply(Action& action) { traverse(action); }
34void ActionVisitor::apply(Timeline& tm) { tm.traverse(*this); }
35void ActionVisitor::apply(ActionBlendIn& action) { apply(static_cast<Action&>(action));}
36void ActionVisitor::apply(ActionBlendOut& action) { apply(static_cast<Action&>(action)); }
37void ActionVisitor::apply(ActionAnimation& action) { apply(static_cast<Action&>(action)); }
38void ActionVisitor::apply(ActionStripAnimation& action) { apply(static_cast<Action&>(action)); }
39void ActionVisitor::traverse(Action& action)
40{
41    action.traverse(*this);
42}
43
44Timeline* ActionVisitor::getCurrentTimeline()
45{
46    if (_stackTimeline.empty())
47        return 0;
48    return _stackTimeline.back();
49}
50
51UpdateActionVisitor::UpdateActionVisitor()
52{
53    _frame = 0;
54    _currentAnimationPriority = 0;
55}
56
57
58void UpdateActionVisitor::apply(Timeline& tm)
59{
60    _currentAnimationPriority = 0;
61
62    tm.setEvaluating(true);
63
64    tm.traverse(*this);
65
66    tm.setEvaluating(false);
67
68    tm.setLastFrameEvaluated(_frame);
69}
70
71bool UpdateActionVisitor::isActive(Action& action) const
72{
73    FrameAction fa = _stackFrameAction.back();
74    if (_frame < fa.first)
75        return false;
76    if (!fa.second.valid())
77        return false;
78
79    unsigned int f = getLocalFrame();
80    unsigned int frameInAction;
81    unsigned int loopDone;
82    return action.evaluateFrame(f, frameInAction, loopDone);
83}
84
85unsigned int UpdateActionVisitor::getLocalFrame() const
86{
87    return _frame - _stackFrameAction.back().first;
88}
89
90void UpdateActionVisitor::apply(Action& action)
91{
92    if (isActive(action))
93    {
94        unsigned int frame = getLocalFrame();
95
96        unsigned int frameInAction;
97        unsigned int loopDone;
98        bool result = action.evaluateFrame(frame, frameInAction, loopDone);
99        if (!result)
100        {
101            OSG_DEBUG << action.getName() << " Action frame " << frameInAction  << " finished" << std::endl;
102            return;
103        }
104        OSG_DEBUG << action.getName() << " Action frame " << frame  << " relative to loop " << frameInAction  << " no loop " << loopDone<< std::endl;
105
106        frame = frameInAction;
107        Action::Callback* cb = action.getFrameCallback(frame);
108        while (cb)
109        {
110            OSG_DEBUG << action.getName() << " evaluate callback " << cb->getName() << " at " << frame << std::endl;
111            (*cb)(&action, this);
112            cb = cb->getNestedCallback();
113        }
114    }
115}
116
117void UpdateActionVisitor::apply(ActionBlendIn& action)
118{
119    if (isActive(action))
120    {
121        unsigned int frame = getLocalFrame();
122        apply(static_cast<Action&>(action));
123        action.computeWeight(frame);
124    }
125}
126
127void UpdateActionVisitor::apply(ActionBlendOut& action)
128{
129    if (isActive(action))
130    {
131        unsigned int frame = getLocalFrame();
132        apply(static_cast<Action&>(action));
133        action.computeWeight(frame);
134    }
135}
136
137void UpdateActionVisitor::apply(ActionAnimation& action)
138{
139    if (isActive(action))
140    {
141        unsigned int frame = getLocalFrame();
142        apply(static_cast<Action&>(action));
143                int pri = static_cast<int>(_currentAnimationPriority);
144                _currentAnimationPriority++;
145        action.updateAnimation(frame, -pri);
146    }
147}
148
149void UpdateActionVisitor::apply(ActionStripAnimation& action)
150{
151    if (isActive(action))
152    {
153        apply(static_cast<Action&>(action));
154        action.traverse(*this);
155    }
156}
157
158
159
160ClearActionVisitor::ClearActionVisitor(ClearType type) : _clearType(type)
161{
162}
163
164void ClearActionVisitor::apply(Timeline& tm)
165{
166    _remove.clear();
167    tm.traverse(*this);
168    for (int i = 0; i < (int)_remove.size(); i++)
169        tm.removeAction(_remove[i].get());
170}
171void ClearActionVisitor::apply(Action& action)
172{
173    FrameAction fa = _stackFrameAction.back();
174    switch( _clearType) {
175    case BEFORE_FRAME:
176        if (_frame > fa.first)
177            _remove.push_back(&action);
178        break;
179    case AFTER_FRAME:
180        if (_frame - fa.first > action.getNumFrames())
181            _remove.push_back(&action);
182        break;
183    }
184}
Note: See TracBrowser for help on using the browser.