1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
22/** Sequence is a Group node which allows automatic, time based
23switching between children.
25class OSG_EXPORT Sequence : public Group
31 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
32 Sequence(const Sequence&, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
34 META_Node(osg, Sequence);
36 virtual void traverse(NodeVisitor& nv);
38 // the relationship between the _frameTime vector and the _children
39 // vector is a bit of a mess. This is how it was in previous versions,
40 // and there's no way out of it if upward compatibility needs to be
41 // maintained. New code should set defaultTime and use addChild, and
42 // not mess with the setTime method
44 using osg::Group::addChild;
45 using osg::Group::insertChild;
46 using osg::Group::removeChild;
48 virtual bool addChild( Node *child);
50 virtual bool addChild( Node *child, double t);
52 template<class T> bool addChild( const ref_ptr<T>& child, double t) { return addChild(child.get(), t); }
54 virtual bool insertChild( unsigned int index, Node *child);
56 virtual bool insertChild( unsigned int index, Node *child, double t);
58 template<class T> bool insertChild( unsigned int index, const ref_ptr<T>& child, double t) { return insertChild(index, child.get(), t); }
60 virtual bool removeChild( Node *child );
62 virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
65 /** value is which child node is to be displayed */
66 void setValue(int value) { _value = value ; }
67 int getValue() const { return _value; }
69 /** Set time in seconds for child. */
70 void setTime(unsigned int frame, double t);
72 /** Get time for child. */
73 double getTime(unsigned int frame) const;
75 /** Set the time list for children. */
76 void setTimeList(const std::vector<double>& timeList) { _frameTime = timeList; }
78 /** Get the time list for children. */
79 const std::vector<double>& getTimeList() const { return _frameTime; }
81 /** Set default time in seconds for new child.
83 void setDefaultTime(double t) {_defaultTime = (t<0.?0.:t);}
85 /** Get default time in seconds for new child. */
86 double getDefaultTime(void) const {return _defaultTime;};
88 /** Set time of last frame of last loop, in seconds.
89 if t<= 0, then ignored */
90 void setLastFrameTime(double t) {_lastFrameTime = (t<0.?0.:t);}
92 /** Get last frame time in seconds */
93 double getLastFrameTime(void) const {return _lastFrameTime;};
95 /** Get number of frames */
96 inline unsigned int getNumFrames() const { return _frameTime.size(); }
98 /** Interval modes. 'Loop' repeats frames 1-N; 'swing' repeats 1->N, (N-1)->1. */
105 /** Set sequence mode. */
106 void setLoopMode(LoopMode mode) { _loopMode = mode; _value = -1; }
108 /** Get sequence mode. */
109 LoopMode getLoopMode() const { return _loopMode; }
111 /** Set interval beginning. */
112 void setBegin(int begin) { _begin = begin; _value = -1; }
114 /** Get interval beginning. */
115 int getBegin() const { return _begin; }
117 /** Set interval ending. */
118 void setEnd(int end) { _end = end; _value = -1; }
120 /** Get interval ending. */
121 int getEnd() const { return _end; }
123 /** Set sequence mode & interval (range of children to be displayed). */
124 void setInterval(LoopMode mode, int begin, int end);
126 /** Get sequence mode & interval. */
127 inline void getInterval(LoopMode& mode, int& begin, int& end) const
135 void setSpeed(float speed) { _speed = speed; }
138 float getSpeed() const { return _speed; }
140 /** Set number of repeats. */
141 void setNumRepeats(int nreps) { _nreps = (nreps<0?-1:nreps); _nrepsRemain = _nreps; }
143 /** Get number of repeats. */
144 int getNumRepeats() const { return _nreps; }
146 /** Set duration: speed-up & number of repeats */
147 void setDuration(float speed, int nreps = -1);
149 /** Get duration & number of repeats. */
150 inline void getDuration(float& speed, int& nreps) const
156 /** Sequence modes. */
165 /** Set sequence mode. Start/stop & pause/resume. */
166 void setMode(SequenceMode mode);
168 /** Get sequence mode. */
169 inline SequenceMode getMode() const { return _mode; }
171 /** If false (default), frames will not be sync'd to frameTime. If
172 true, frames will be sync'd to frameTime. */
173 void setSync(bool sync) { _sync = sync; }
175 /** Get sync value */
176 bool getSync() const { return _sync; }
178 /** If true, show no child nodes after stopping */
179 void setClearOnStop(bool clearOnStop) { _clearOnStop = clearOnStop; }
181 /** Get whether to show no child nodes after stopping */
182 bool getClearOnStop() const { return _clearOnStop; }
186 virtual ~Sequence() {}
188 // get next _value in sequence
189 int _getNextValue(void) ;
191 // update local variables
194 // init to -1 to mean "restart"
197 // current time, set by traverse
200 // time this frame started. init to -1.0f- means get current time
203 // a vector of frame times, one per value
204 std::vector<double> _frameTime;
206 // the total time for one sequence, from BEGIN to END
209 // true if _totalTime needs to be recalculated because setTime or
210 // setInterval was invoked, or a new child was added
211 bool _resetTotalTime ;
213 // store "loop mde", either LOOP or SWING
214 // init to LOOP- set by setInterval
217 // first and last "values" to sequence through
219 // end inits to -1- means to init to number of values
222 // multiplier of real-time clock- set to N to go N times faster
223 // init to 0- going nowhere
226 // _nreps: how many times to repeat- default param is -1, repeat forever
227 // init to 0, no repetitions
228 // _nrepsRemain: set to nreps and counts down every traversal,
229 // stopping when it gets to zero. init to 0
230 int _nreps, _nrepsRemain;
232 // frame step (are we stepping forward or backward?)
235 // default frame time for newly created frames or children- default is 1.
236 // set by setDefaultTime
237 double _defaultTime ;
239 // special time to display last frame of last loop
240 // <= zero means to not do anything special
241 double _lastFrameTime ;
243 // save the actual time of the last frame, and what value was stored
244 double _saveRealLastFrameTime ;
245 unsigned int _saveRealLastFrameValue ;
250 // the current sync value
253 // the current clearOnStop value