2 * Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
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.
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.
15#ifndef OSGANIMATION_ACTION_H
16#define OSGANIMATION_ACTION_H
18#include <osgAnimation/Export>
19#include <osgAnimation/Animation>
20#include <osgAnimation/ActionVisitor>
21#include <osgAnimation/FrameAction>
24#define META_Action(library,name) \
25 virtual osg::Object* cloneType() const { return new name (); } \
26 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
27 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
28 virtual const char* className() const { return #name; } \
29 virtual const char* libraryName() const { return #library; } \
30 virtual void accept(osgAnimation::ActionVisitor& nv) { nv.apply(*this); } \
36 class OSGANIMATION_EXPORT Action : public osg::Object
40 class Callback : public osg::Object
44 Callback(const Callback& nc,const osg::CopyOp& copyop) :
45 osg::Object(nc, copyop),
46 _nestedCallback(nc._nestedCallback) {}
48 META_Object(osgAnimation,Callback);
50 virtual void operator()(Action* /*action*/, osgAnimation::ActionVisitor* /*nv*/) {}
52 Callback* getNestedCallback() { return _nestedCallback.get(); }
53 void addNestedCallback(Callback* callback)
56 if (_nestedCallback.valid())
57 _nestedCallback->addNestedCallback(callback);
59 _nestedCallback = callback;
63 void removeCallback(Callback* cb)
68 if (_nestedCallback.get() == cb)
69 _nestedCallback = _nestedCallback->getNestedCallback();
70 else if (_nestedCallback.valid())
71 _nestedCallback->removeCallback(cb);
75 osg::ref_ptr<Callback> _nestedCallback;
79 typedef std::map<unsigned int, osg::ref_ptr<Callback> > FrameCallback;
81 META_Action(osgAnimation, Action);
84 Action(const Action&,const osg::CopyOp&);
86 void setCallback(double when, Callback* callback)
88 setCallback(static_cast<unsigned int>(floor(when*_fps)), callback);
91 void setCallback(unsigned int frame, Callback* callback)
93 if (_framesCallback[frame].valid())
94 _framesCallback[frame]->addNestedCallback(callback);
96 _framesCallback[frame] = callback;
98 Callback* getCallback(unsigned int frame)
100 if (_framesCallback.find(frame) == _framesCallback.end())
102 return _framesCallback[frame].get();
105 void removeCallback(Callback*);
107 Callback* getFrameCallback(unsigned int frame);
108 Callback* getFrameCallback(double time);
109 unsigned int getFramesPerSecond() const { return _fps; }
111 void setNumFrames(unsigned int numFrames) { _numberFrame = numFrames;}
112 void setDuration(double duration) { _numberFrame = static_cast<unsigned int>(floor(duration * _fps)); }
113 unsigned int getNumFrames() const { return _numberFrame;}
114 double getDuration() const { return _numberFrame * 1.0 / _fps; }
116 // 0 means infinite else it's the number of loop
117 virtual void setLoop(unsigned int nb) { _loop = nb; }
118 virtual unsigned int getLoop() const { return _loop;}
120 // get the number of loop, the frame relative to loop.
121 // return true if in range, and false if out of range.
122 bool evaluateFrame(unsigned int frame, unsigned int& resultframe, unsigned int& nbloop );
123 virtual void traverse(ActionVisitor& /*visitor*/) {}
124 //virtual void evaluate(unsigned int frame);
127 FrameCallback _framesCallback;
131 unsigned int _numberFrame;