| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 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 | |
|---|
| 23 | using namespace osgAnimation; |
|---|
| 24 | |
|---|
| 25 | ActionVisitor::ActionVisitor() |
|---|
| 26 | { |
|---|
| 27 | _currentLayer = 0; |
|---|
| 28 | } |
|---|
| 29 | void ActionVisitor::pushFrameActionOnStack(const FrameAction& fa) { _stackFrameAction.push_back(fa); } |
|---|
| 30 | void ActionVisitor::popFrameAction() { _stackFrameAction.pop_back(); } |
|---|
| 31 | void ActionVisitor::pushTimelineOnStack(Timeline* tm) { _stackTimeline.push_back(tm); } |
|---|
| 32 | void ActionVisitor::popTimeline() { _stackTimeline.pop_back(); } |
|---|
| 33 | void ActionVisitor::apply(Action& action) { traverse(action); } |
|---|
| 34 | void ActionVisitor::apply(Timeline& tm) { tm.traverse(*this); } |
|---|
| 35 | void ActionVisitor::apply(ActionBlendIn& action) { apply(static_cast<Action&>(action));} |
|---|
| 36 | void ActionVisitor::apply(ActionBlendOut& action) { apply(static_cast<Action&>(action)); } |
|---|
| 37 | void ActionVisitor::apply(ActionAnimation& action) { apply(static_cast<Action&>(action)); } |
|---|
| 38 | void ActionVisitor::apply(ActionStripAnimation& action) { apply(static_cast<Action&>(action)); } |
|---|
| 39 | void ActionVisitor::traverse(Action& action) |
|---|
| 40 | { |
|---|
| 41 | action.traverse(*this); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | Timeline* ActionVisitor::getCurrentTimeline() |
|---|
| 45 | { |
|---|
| 46 | if (_stackTimeline.empty()) |
|---|
| 47 | return 0; |
|---|
| 48 | return _stackTimeline.back(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | UpdateActionVisitor::UpdateActionVisitor() |
|---|
| 52 | { |
|---|
| 53 | _frame = 0; |
|---|
| 54 | _currentAnimationPriority = 0; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | void 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 | |
|---|
| 71 | bool 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 | |
|---|
| 85 | unsigned int UpdateActionVisitor::getLocalFrame() const |
|---|
| 86 | { |
|---|
| 87 | return _frame - _stackFrameAction.back().first; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void 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 | |
|---|
| 117 | void 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 | |
|---|
| 127 | void 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 | |
|---|
| 137 | void 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 | |
|---|
| 149 | void 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 | |
|---|
| 160 | ClearActionVisitor::ClearActionVisitor(ClearType type) : _clearType(type) |
|---|
| 161 | { |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void 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 | } |
|---|
| 171 | void 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 | } |
|---|