openscenegraph
Action
Go to the documentation of this file.
1/* -*-c++-*-
2 * Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
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.
8 *
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.
13*/
14
15#ifndef OSGANIMATION_ACTION_H
16#define OSGANIMATION_ACTION_H
17
18#include <osgAnimation/Export>
19#include <osgAnimation/Animation>
20#include <osgAnimation/ActionVisitor>
21#include <osgAnimation/FrameAction>
22#include <iostream>
23
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); } \
31
32
33namespace osgAnimation
34{
35
36 class OSGANIMATION_EXPORT Action : public osg::Object
37 {
38 public:
39
40 class Callback : public osg::Object
41 {
42 public:
43 Callback(){}
44 Callback(const Callback& nc,const osg::CopyOp& copyop) :
45 osg::Object(nc, copyop),
46 _nestedCallback(nc._nestedCallback) {}
47
48 META_Object(osgAnimation,Callback);
49
50 virtual void operator()(Action* /*action*/, osgAnimation::ActionVisitor* /*nv*/) {}
51
52 Callback* getNestedCallback() { return _nestedCallback.get(); }
53 void addNestedCallback(Callback* callback)
54 {
55 if (callback) {
56 if (_nestedCallback.valid())
57 _nestedCallback->addNestedCallback(callback);
58 else
59 _nestedCallback = callback;
60 }
61 }
62
63 void removeCallback(Callback* cb)
64 {
65 if (!cb)
66 return;
67
68 if (_nestedCallback.get() == cb)
69 _nestedCallback = _nestedCallback->getNestedCallback();
70 else if (_nestedCallback.valid())
71 _nestedCallback->removeCallback(cb);
72 }
73
74 protected:
75 osg::ref_ptr<Callback> _nestedCallback;
76 };
77
78
79 typedef std::map<unsigned int, osg::ref_ptr<Callback> > FrameCallback;
80
81 META_Action(osgAnimation, Action);
82
83 Action();
84 Action(const Action&,const osg::CopyOp&);
85
86 void setCallback(double when, Callback* callback)
87 {
88 setCallback(static_cast<unsigned int>(floor(when*_fps)), callback);
89 }
90
91 void setCallback(unsigned int frame, Callback* callback)
92 {
93 if (_framesCallback[frame].valid())
94 _framesCallback[frame]->addNestedCallback(callback);
95 else
96 _framesCallback[frame] = callback;
97 }
98 Callback* getCallback(unsigned int frame)
99 {
100 if (_framesCallback.find(frame) == _framesCallback.end())
101 return 0;
102 return _framesCallback[frame].get();
103 }
104
105 void removeCallback(Callback*);
106
107 Callback* getFrameCallback(unsigned int frame);
108 Callback* getFrameCallback(double time);
109 unsigned int getFramesPerSecond() const { return _fps; }
110
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; }
115
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;}
119
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);
125
126 protected:
127 FrameCallback _framesCallback;
128
129 double _speed;
130 unsigned int _fps;
131 unsigned int _numberFrame;
132 unsigned int _loop;
133
134 enum Status
135 {
136 Play,
137 Stop
138 };
139
140 Status _state;
141 };
142
143
144
145
146}
147
148#endif