root/OpenSceneGraph/trunk/include/osgAnimation/Action @ 10344

Revision 10344, 7.3 kB (checked in by cedricpinson, 6 years ago)

From Cedric Pinson, split timeline classes in differents files, cleanup and add a statshandler to visualize current action in timeline

Line 
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&) :
45                _nestedCallback(nc._nestedCallback) {}
46
47            META_Object(osgAnimation,Callback);
48       
49            virtual void operator()(Action* action, osgAnimation::ActionVisitor* nv) {}
50           
51            Callback* getNestedCallback() { return _nestedCallback.get(); }
52            void addNestedCallback(Callback* callback)
53            {
54                if (_nestedCallback.valid())
55                    _nestedCallback->addNestedCallback(callback);
56                else
57                    _nestedCallback = callback;
58            }
59
60        protected:
61            osg::ref_ptr<Callback> _nestedCallback;
62        };
63
64
65        typedef std::map<unsigned int, osg::ref_ptr<Callback> > FrameCallback;
66
67        META_Action(osgAnimation, Action);
68       
69        Action();
70        Action(const Action&,const osg::CopyOp&);
71
72        void setCallback(double when, Callback* callback)
73        {
74            setCallback(static_cast<unsigned int>(floor(when*_fps)), callback);
75        }
76
77        void setCallback(unsigned int frame, Callback* callback)
78        {
79            if (_framesCallback[frame].valid())
80                _framesCallback[frame]->addNestedCallback(callback);
81            else
82                _framesCallback[frame] = callback;
83        }
84        Callback* getCallback(unsigned int frame)
85        {
86            if (_framesCallback.find(frame) == _framesCallback.end())
87                return 0;
88            return _framesCallback[frame].get();
89        }
90       
91        Callback* getFrameCallback(unsigned int frame);
92        Callback* getFrameCallback(double time);
93        unsigned int getFramesPerSecond() const { return _fps; }
94
95        void setNumFrames(unsigned int numFrames) { _numberFrame = numFrames;}
96        void setDuration(double duration) { _numberFrame = static_cast<unsigned int>(floor(duration * _fps)); }
97        unsigned int getNumFrames() const { return _numberFrame;}
98        double getDuration() const { return _numberFrame * 1.0 / _fps; }
99
100        // 0 means infini else it's the number of loop
101        virtual void setLoop(int nb) { _loop = nb; }
102        virtual unsigned int getLoop() const { return _loop;}
103
104        // get the number of loop, the frame relative to loop.
105        // return true if in range, and false if out of range.
106        bool evaluateFrame(unsigned int frame, unsigned int& resultframe, unsigned int& nbloop );
107        virtual void traverse(ActionVisitor& visitor) {}
108        //virtual void evaluate(unsigned int frame);
109
110    protected:
111        FrameCallback _framesCallback;
112
113        double _speed;
114        unsigned int _fps;
115        unsigned int _numberFrame;
116        unsigned int _loop;
117
118        enum Status
119        {
120            Play,
121            Stop
122        };
123       
124        Status _state;
125    };
126
127
128
129
130    // blend in from 0 to weight in duration
131    class BlendIn : public Action
132    {
133        double _weight;
134        osg::ref_ptr<Animation> _animation;
135
136    public:
137        META_Action(osgAnimation, BlendIn);
138        BlendIn() : _weight(0) {}
139        BlendIn(const BlendIn& a, const osg::CopyOp& c) : Action(a,c) { _weight = a._weight; _animation = a._animation;}
140        BlendIn(Animation* animation, double duration, double weight);
141        double getWeight() const { return _weight;}
142        Animation* getAnimation() { return _animation.get(); }
143        void computeWeight(unsigned int frame);
144    };
145
146    // blend in from 0 to weight in duration
147    class BlendOut : public Action
148    {
149        double _weight;
150        osg::ref_ptr<Animation> _animation;
151    public:
152        META_Action(osgAnimation, BlendOut);
153        BlendOut() : _weight(0) {}
154        BlendOut(const BlendOut& a, const osg::CopyOp& c) : Action(a,c) { _weight = a._weight; _animation = a._animation;}
155        BlendOut(Animation* animation, double duration);
156        Animation* getAnimation() { return _animation.get(); }
157        double getWeight() const { return _weight;}
158        void computeWeight(unsigned int frame);
159    };
160
161
162    class ActionAnimation : public Action
163    {
164    public:
165        META_Action(osgAnimation, ActionAnimation);
166        ActionAnimation() {}
167        ActionAnimation(const ActionAnimation& a, const osg::CopyOp& c) : Action(a,c) { _animation = a._animation;}
168        ActionAnimation(Animation* animation);
169        void updateAnimation(unsigned int frame);
170        Animation* getAnimation() { return _animation.get(); }
171
172    protected:
173        osg::ref_ptr<Animation> _animation;
174    };
175
176
177    // encapsulate animation with blend in blend out for classic usage
178    class StripAnimation : public Action
179    {
180    public:
181        META_Action(osgAnimation, StripAnimation);
182        StripAnimation() {}
183        StripAnimation(const StripAnimation& a, const osg::CopyOp& c);
184        StripAnimation(Animation* animation, double blendInDuration = 0.0, double blendOutDuration = 0.0, double blendInWeightTarget = 1.0  );
185        ActionAnimation* getActionAnimation() { return _animation.get(); }
186        BlendIn* getBlendIn() { return _blendIn.get(); }
187        BlendOut* getBlendOut() { return _blendOut.second.get(); }
188        const ActionAnimation* getActionAnimation() const { return _animation.get(); }
189        const BlendIn* getBlendIn() const { return _blendIn.get(); }
190        const BlendOut* getBlendOut() const { return _blendOut.second.get(); }
191        unsigned int getBlendOutStartFrame() const { return _blendOut.first; }
192       
193        unsigned int getLoop() const { return _animation->getLoop(); }
194        void setLoop(unsigned int loop);
195        void computeWeightAndUpdateAnimation(unsigned int frame);
196
197    protected:
198        typedef std::pair<unsigned int, osg::ref_ptr<BlendOut> > FrameBlendOut;
199        osg::ref_ptr<BlendIn> _blendIn;
200        FrameBlendOut _blendOut;
201        osg::ref_ptr<ActionAnimation> _animation;
202    };
203
204
205
206}
207
208#endif
Note: See TracBrowser for help on using the browser.