| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/LinkVisitor> |
|---|
| 16 | #include <osgAnimation/AnimationUpdateCallback> |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | #include <osg/Geode> |
|---|
| 19 | |
|---|
| 20 | using namespace osgAnimation; |
|---|
| 21 | |
|---|
| 22 | LinkVisitor::LinkVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 23 | { |
|---|
| 24 | _nbLinkedTarget = 0; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | void LinkVisitor::reset() |
|---|
| 28 | { |
|---|
| 29 | _nbLinkedTarget = 0; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | AnimationList& LinkVisitor::getAnimationList() |
|---|
| 33 | { |
|---|
| 34 | return _animations; |
|---|
| 35 | } |
|---|
| 36 | void LinkVisitor::link(AnimationUpdateCallbackBase* cb) |
|---|
| 37 | { |
|---|
| 38 | int result = 0; |
|---|
| 39 | for (int i = 0; i < (int)_animations.size(); i++) |
|---|
| 40 | { |
|---|
| 41 | result += cb->link(_animations[i].get()); |
|---|
| 42 | _nbLinkedTarget += result; |
|---|
| 43 | } |
|---|
| 44 | OSG_NOTICE << "LinkVisitor links " << result << " for \"" << cb->getName() << '"' << std::endl; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | void LinkVisitor::handle_stateset(osg::StateSet* stateset) |
|---|
| 48 | { |
|---|
| 49 | if (!stateset) |
|---|
| 50 | return; |
|---|
| 51 | osg::StateSet::AttributeList& attr = stateset->getAttributeList(); |
|---|
| 52 | for (osg::StateSet::AttributeList::iterator it = attr.begin(); it != attr.end(); ++it) |
|---|
| 53 | { |
|---|
| 54 | osg::StateAttribute* sattr = it->second.first.get(); |
|---|
| 55 | osgAnimation::AnimationUpdateCallbackBase* cb = dynamic_cast<osgAnimation::AnimationUpdateCallbackBase*>(sattr->getUpdateCallback()); |
|---|
| 56 | if (cb) |
|---|
| 57 | link(cb); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void LinkVisitor::apply(osg::Node& node) |
|---|
| 62 | { |
|---|
| 63 | osg::StateSet* st = node.getStateSet(); |
|---|
| 64 | if (st) |
|---|
| 65 | handle_stateset(st); |
|---|
| 66 | |
|---|
| 67 | osg::NodeCallback* cb = node.getUpdateCallback(); |
|---|
| 68 | while (cb) |
|---|
| 69 | { |
|---|
| 70 | osgAnimation::AnimationUpdateCallbackBase* cba = dynamic_cast<osgAnimation::AnimationUpdateCallbackBase*>(cb); |
|---|
| 71 | if (cba) |
|---|
| 72 | link(cba); |
|---|
| 73 | cb = cb->getNestedCallback(); |
|---|
| 74 | } |
|---|
| 75 | traverse(node); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void LinkVisitor::apply(osg::Geode& node) |
|---|
| 79 | { |
|---|
| 80 | for (unsigned int i = 0; i < node.getNumDrawables(); i++) |
|---|
| 81 | { |
|---|
| 82 | osg::Drawable* drawable = node.getDrawable(i); |
|---|
| 83 | if (drawable && drawable->getStateSet()) |
|---|
| 84 | handle_stateset(drawable->getStateSet()); |
|---|
| 85 | } |
|---|
| 86 | apply(static_cast<osg::Node&>(node)); |
|---|
| 87 | } |
|---|