| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/Action> |
|---|
| 16 | |
|---|
| 17 | using namespace osgAnimation; |
|---|
| 18 | |
|---|
| 19 | Action::Action() |
|---|
| 20 | { |
|---|
| 21 | _numberFrame = 25; |
|---|
| 22 | _fps = 25; |
|---|
| 23 | _speed = 1.0; |
|---|
| 24 | _loop = 1; |
|---|
| 25 | } |
|---|
| 26 | Action::Action(const Action&,const osg::CopyOp&) {} |
|---|
| 27 | Action::Callback* Action::getFrameCallback(unsigned int frame) |
|---|
| 28 | { |
|---|
| 29 | if (_framesCallback.find(frame) != _framesCallback.end()) |
|---|
| 30 | { |
|---|
| 31 | return _framesCallback[frame].get(); |
|---|
| 32 | } |
|---|
| 33 | return 0; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | void Action::removeCallback(Callback* cb) |
|---|
| 37 | { |
|---|
| 38 | std::vector<unsigned int> keyToRemove; |
|---|
| 39 | for (FrameCallback::iterator it = _framesCallback.begin(); it != _framesCallback.end(); ++it) |
|---|
| 40 | { |
|---|
| 41 | if (it->second.get()) |
|---|
| 42 | { |
|---|
| 43 | if (it->second.get() == cb) |
|---|
| 44 | { |
|---|
| 45 | it->second = it->second->getNestedCallback(); |
|---|
| 46 | if (!it->second.valid()) |
|---|
| 47 | keyToRemove.push_back(it->first); |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | it->second->removeCallback(cb); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | for (std::vector<unsigned int>::iterator it = keyToRemove.begin(); it != keyToRemove.end(); ++it) |
|---|
| 56 | _framesCallback.erase(*it); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | Action::Callback* Action::getFrameCallback(double time) |
|---|
| 60 | { |
|---|
| 61 | unsigned int frame = static_cast<unsigned int>(floor(time * _fps)); |
|---|
| 62 | return getFrameCallback(frame); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | bool osgAnimation::Action::evaluateFrame(unsigned int frame, unsigned int& resultframe, unsigned int& nbloop ) |
|---|
| 66 | { |
|---|
| 67 | unsigned int nbFrames = getNumFrames(); |
|---|
| 68 | if (!nbFrames) { |
|---|
| 69 | nbFrames = 1; |
|---|
| 70 | osg::notify(osg::WARN) << "osgAnimation::Action::evaluateFrame your action " << getName() << " has 0 frames, it seems like an error in the data" << std::endl; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | nbloop = frame / nbFrames; |
|---|
| 74 | resultframe = frame; |
|---|
| 75 | |
|---|
| 76 | if (frame > nbFrames-1) |
|---|
| 77 | { |
|---|
| 78 | if (!getLoop()) |
|---|
| 79 | resultframe = frame % nbFrames; |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | if (nbloop >= getLoop()) |
|---|
| 83 | return false; |
|---|
| 84 | else |
|---|
| 85 | resultframe = frame % nbFrames; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|