Action 4.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/*  -*-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&) :
                _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 infini 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