Action
Go to the documentation of this file.
/* -*-c++-*-
* Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGANIMATION_ACTION_H
#define OSGANIMATION_ACTION_H
#include <osgAnimation/Export>
#include <osgAnimation/Animation>
#include <osgAnimation/ActionVisitor>
#include <osgAnimation/FrameAction>
#include <iostream>
#define META_Action(library,name) \
virtual osg::Object* cloneType() const { return new name (); } \
virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
virtual const char* className() const { return #name; } \
virtual const char* libraryName() const { return #library; } \
virtual void accept(osgAnimation::ActionVisitor& nv) { nv.apply(*this); } \
namespace osgAnimation
{
class OSGANIMATION_EXPORT Action : public osg::Object
{
public:
class Callback : public osg::Object
{
public:
Callback(){}
Callback(const Callback& nc,const osg::CopyOp& copyop) :
osg::Object(nc, copyop),
_nestedCallback(nc._nestedCallback) {}
META_Object(osgAnimation,Callback);
virtual void operator()(Action* /*action*/, osgAnimation::ActionVisitor* /*nv*/) {}
Callback* getNestedCallback() { return _nestedCallback.get(); }
void addNestedCallback(Callback* callback)
{
if (callback) {
if (_nestedCallback.valid())
_nestedCallback->addNestedCallback(callback);
else
_nestedCallback = callback;
}
}
void removeCallback(Callback* cb)
{
if (!cb)
return;
if (_nestedCallback.get() == cb)
_nestedCallback = _nestedCallback->getNestedCallback();
else if (_nestedCallback.valid())
_nestedCallback->removeCallback(cb);
}
protected:
osg::ref_ptr<Callback> _nestedCallback;
};
typedef std::map<unsigned int, osg::ref_ptr<Callback> > FrameCallback;
META_Action(osgAnimation, Action);
Action();
Action(const Action&,const osg::CopyOp&);
void setCallback(double when, Callback* callback)
{
setCallback(static_cast<unsigned int>(floor(when*_fps)), callback);
}
void setCallback(unsigned int frame, Callback* callback)
{
if (_framesCallback[frame].valid())
_framesCallback[frame]->addNestedCallback(callback);
else
_framesCallback[frame] = callback;
}
Callback* getCallback(unsigned int frame)
{
if (_framesCallback.find(frame) == _framesCallback.end())
return 0;
return _framesCallback[frame].get();
}
void removeCallback(Callback*);
Callback* getFrameCallback(unsigned int frame);
Callback* getFrameCallback(double time);
unsigned int getFramesPerSecond() const { return _fps; }
void setNumFrames(unsigned int numFrames) { _numberFrame = numFrames;}
void setDuration(double duration) { _numberFrame = static_cast<unsigned int>(floor(duration * _fps)); }
unsigned int getNumFrames() const { return _numberFrame;}
double getDuration() const { return _numberFrame * 1.0 / _fps; }
// 0 means infinite else it's the number of loop
virtual void setLoop(unsigned int nb) { _loop = nb; }
virtual unsigned int getLoop() const { return _loop;}
// get the number of loop, the frame relative to loop.
// return true if in range, and false if out of range.
bool evaluateFrame(unsigned int frame, unsigned int& resultframe, unsigned int& nbloop );
virtual void traverse(ActionVisitor& /*visitor*/) {}
//virtual void evaluate(unsigned int frame);
protected:
FrameCallback _framesCallback;
double _speed;
unsigned int _fps;
unsigned int _numberFrame;
unsigned int _loop;
enum Status
{
Play,
Stop
};
Status _state;
};
}
#endif
ActionVisitor
Animation
FrameAction
osgAnimation::ActionVisitor
Definition ActionVisitor:39
osgAnimation::Action::Callback
Definition Action:41
osgAnimation::Action::Callback::getNestedCallback
Callback * getNestedCallback()
Definition Action:52
osgAnimation::Action::Callback::Callback
Callback()
Definition Action:43
osgAnimation::Action::Callback::addNestedCallback
void addNestedCallback(Callback *callback)
Definition Action:53
osgAnimation::Action::Callback::META_Object
META_Object(osgAnimation, Callback)
osgAnimation::Action::Callback::Callback
Callback(const Callback &nc, const osg::CopyOp &copyop)
Definition Action:44
osgAnimation::Action::Callback::operator()
virtual void operator()(Action *, osgAnimation::ActionVisitor *)
Definition Action:50
osgAnimation::Action::Callback::removeCallback
void removeCallback(Callback *cb)
Definition Action:63
osgAnimation::Action::Callback::_nestedCallback
osg::ref_ptr< Callback > _nestedCallback
Definition Action:75
osgAnimation::Action::getLoop
virtual unsigned int getLoop() const
Definition Action:118
osgAnimation::Action::setDuration
void setDuration(double duration)
Definition Action:112
osgAnimation::Action::setLoop
virtual void setLoop(unsigned int nb)
Definition Action:117
osgAnimation::Action::_numberFrame
unsigned int _numberFrame
Definition Action:131
osgAnimation::Action::getFramesPerSecond
unsigned int getFramesPerSecond() const
Definition Action:109
osgAnimation::Action::_speed
double _speed
Definition Action:129
osgAnimation::Action::_framesCallback
FrameCallback _framesCallback
Definition Action:127
osgAnimation::Action::getNumFrames
unsigned int getNumFrames() const
Definition Action:113
osgAnimation::Action::_fps
unsigned int _fps
Definition Action:130
osgAnimation::Action::_state
Status _state
Definition Action:140
osgAnimation::Action::META_Action
META_Action(osgAnimation, Action)
osgAnimation::Action::removeCallback
void removeCallback(Callback *)
osgAnimation::Action::getDuration
double getDuration() const
Definition Action:114
osgAnimation::Action::getCallback
Callback * getCallback(unsigned int frame)
Definition Action:98
osgAnimation::Action::_loop
unsigned int _loop
Definition Action:132
osgAnimation::Action::setCallback
void setCallback(double when, Callback *callback)
Definition Action:86
osgAnimation::Action::getFrameCallback
Callback * getFrameCallback(double time)
osgAnimation::Action::Action
Action(const Action &, const osg::CopyOp &)
osgAnimation::Action::traverse
virtual void traverse(ActionVisitor &)
Definition Action:123
osgAnimation::Action::setCallback
void setCallback(unsigned int frame, Callback *callback)
Definition Action:91
osgAnimation::Action::Action
Action()
osgAnimation::Action::getFrameCallback
Callback * getFrameCallback(unsigned int frame)
osgAnimation::Action::evaluateFrame
bool evaluateFrame(unsigned int frame, unsigned int &resultframe, unsigned int &nbloop)
osgAnimation::Action::setNumFrames
void setNumFrames(unsigned int numFrames)
Definition Action:111
osgAnimation::Action::Status
Status
Definition Action:135
osgAnimation::Action::Stop
@ Stop
Definition Action:137
osgAnimation::Action::Play
@ Play
Definition Action:136
osgAnimation::Action::FrameCallback
std::map< unsigned int, osg::ref_ptr< Callback > > FrameCallback
Definition Action:79
osg::CopyOp
Definition CopyOp:41
osg::Object
Definition Object:61
osg::Object::Object
Object()
Definition Object:69
osg::ref_ptr
Definition ref_ptr:32
osgAnimation
Definition Action:34
osg
Definition AlphaFunc:19
Export
OSGANIMATION_EXPORT
#define OSGANIMATION_EXPORT
Definition osgAnimation/Export:40