Sequence 7.96 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
 *
 * 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 OSG_SEQUENCE
#define OSG_SEQUENCE 1

#include <osg/Group>

namespace osg 
{
  
/** Sequence is a Group node which allows automatic, time based
switching between children.
*/
class OSG_EXPORT Sequence : public Group
{
    public :

        Sequence();

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        Sequence(const Sequence&, const CopyOp& copyop=CopyOp::SHALLOW_COPY);

        META_Node(osg, Sequence);

        virtual void traverse(NodeVisitor& nv);

        // the relationship between the _frameTime vector and the _children
        // vector is a bit of a mess.  This is how it was in previous versions,
        // and there's no way out of it if upward compatibility needs to be
        // maintained.  New code should set defaultTime and use addChild, and
        // not mess with the setTime method

        virtual bool addChild( Node *child);

        virtual bool addChild( Node *child, double t);

        virtual bool insertChild( unsigned int index, Node *child);

        virtual bool insertChild( unsigned int index, Node *child, double t);

        virtual bool removeChild( Node *child );

        virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);


        /** value is which child node is to be displayed */
        void setValue(int value) { _value = value ; }
        int getValue() const { return _value; }

        /** Set time in seconds for child. */
        void setTime(unsigned int frame, double t);

        /** Get time for child. */
        double getTime(unsigned int frame) const;
        
        /** Set the time list for children. */
        void setTimeList(const std::vector<double>& timeList) { _frameTime = timeList; }
        
        /** Get the time list for children. */
        const std::vector<double>& getTimeList() const { return _frameTime; }

        /** Set default time in seconds for new child. 
            if t<0, t=0 */
        void setDefaultTime(double t) {_defaultTime = (t<0.?0.:t);}

        /** Get default time in seconds for new child. */
        double getDefaultTime(void) const {return _defaultTime;};

        /** Set time of last frame of last loop, in seconds.  
            if t<= 0, then ignored */
        void setLastFrameTime(double t) {_lastFrameTime = (t<0.?0.:t);}

        /** Get last frame time in seconds */
        double getLastFrameTime(void) const {return _lastFrameTime;};

        /** Get number of frames */
        inline unsigned int getNumFrames() const { return _frameTime.size(); }

        /** Interval modes. 'Loop' repeats frames 1-N; 'swing' repeats 1->N, (N-1)->1. */
        enum LoopMode
        {
            LOOP,
            SWING
        };
        
        /** Set sequence mode. */
        void setLoopMode(LoopMode mode) { _loopMode = mode; _value = -1; }
        
        /** Get sequence mode. */
        LoopMode getLoopMode() const { return _loopMode; }
        
        /** Set interval beginning. */
        void setBegin(int begin) { _begin = begin; _value = -1; }
        
        /** Get interval beginning. */
        int getBegin() const { return _begin; }
        
        /** Set interval ending. */
        void setEnd(int end) { _end = end; _value = -1; }
        
        /** Get interval ending. */
        int getEnd() const { return _end; }

        /** Set sequence mode & interval (range of children to be displayed). */
        void setInterval(LoopMode mode, int begin, int end);

        /** Get sequence mode & interval. */
        inline void getInterval(LoopMode& mode, int& begin, int& end) const
        {
            mode = _loopMode;
            begin = _begin;
            end = _end;
        }
        
        /** Set speed. */
        void setSpeed(float speed) { _speed = speed; }
        
        /** Get speed. */
        float getSpeed() const { return _speed; }
        
        /** Set number of repeats. */
        void setNumRepeats(int nreps) { _nreps = (nreps<0?-1:nreps); _nrepsRemain = _nreps; }
        
        /** Get number of repeats. */
        int getNumRepeats() const { return _nreps; }

        /** Set duration: speed-up & number of repeats */
        void setDuration(float speed, int nreps = -1);

        /** Get duration & number of repeats. */
        inline void getDuration(float& speed, int& nreps) const
        {
            speed = _speed;
            nreps = _nreps;
        }

        /** Sequence modes. */
        enum SequenceMode
        {
            START,
            STOP,
            PAUSE,
            RESUME
        };

        /** Set sequence mode. Start/stop & pause/resume. */
        void setMode(SequenceMode mode);

        /** Get sequence mode. */
        inline SequenceMode getMode() const { return _mode; }

        /** If false (default), frames will not be sync'd to frameTime.  If
            true, frames will be sync'd to frameTime. */
        void setSync(bool sync) { _sync = sync; }

        /** Get sync value */
        bool getSync() const { return _sync; }

        /** If true, show no child nodes after stopping */
        void setClearOnStop(bool clearOnStop) { _clearOnStop = clearOnStop; }

        /** Get whether to show no child nodes after stopping */
        bool getClearOnStop() const { return _clearOnStop; }

    protected :

        virtual ~Sequence() {}

        // get next _value in sequence
        int _getNextValue(void) ;

        // update local variables
        void _update(void) ;

        // init to -1 to mean "restart"
        int _value;

        // current time, set by traverse
        double _now ;

        // time this frame started.  init to -1.0f- means get current time
        double _start;

        // a vector of frame times, one per value
        std::vector<double> _frameTime;

        // the total time for one sequence, from BEGIN to END
        double _totalTime ;

        // true if _totalTime needs to be recalculated because setTime or
        // setInterval was invoked, or a new child was added
        bool _resetTotalTime ;

        // store "loop mde", either LOOP or SWING
        // init to LOOP- set by setInterval
        LoopMode _loopMode;

        // first and last "values" to sequence through
        // begin inits to 0
        // end inits to -1- means to init to number of values
        int _begin, _end;

        // multiplier of real-time clock- set to N to go N times faster
        // init to 0- going nowhere
        float _speed;

        // _nreps: how many times to repeat- default param is -1, repeat forever
        // init to 0, no repetitions
        // _nrepsRemain: set to nreps and counts down every traversal, 
        // stopping when it gets to zero.  init to 0
        int _nreps, _nrepsRemain;

        // frame step (are we stepping forward or backward?)
        int _step;

        // default frame time for newly created frames or children- default is 1.
        // set by setDefaultTime
        double _defaultTime ;

        // special time to display last frame of last loop
        // <= zero means to not do anything special
        double _lastFrameTime ;

        // save the actual time of the last frame, and what value was stored
        double _saveRealLastFrameTime ;
        unsigned int _saveRealLastFrameValue ;

        // the current mode
        SequenceMode _mode;

        // the current sync value
        bool _sync ;

        // the current clearOnStop value
        bool _clearOnStop ;

};
  
}

#endif