openscenegraph
Sequence
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12 */
13
14#ifndef OSG_SEQUENCE
15#define OSG_SEQUENCE 1
16
17#include <osg/Group>
18
19namespace osg
20{
21
22/** Sequence is a Group node which allows automatic, time based
23switching between children.
24*/
25class OSG_EXPORT Sequence : public Group
26{
27 public :
28
29 Sequence();
30
31 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
32 Sequence(const Sequence&, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
33
34 META_Node(osg, Sequence);
35
36 virtual void traverse(NodeVisitor& nv);
37
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
43
44 using osg::Group::addChild;
45 using osg::Group::insertChild;
46 using osg::Group::removeChild;
47
48 virtual bool addChild( Node *child);
49
50 virtual bool addChild( Node *child, double t);
51
52 template<class T> bool addChild( const ref_ptr<T>& child, double t) { return addChild(child.get(), t); }
53
54 virtual bool insertChild( unsigned int index, Node *child);
55
56 virtual bool insertChild( unsigned int index, Node *child, double t);
57
58 template<class T> bool insertChild( unsigned int index, const ref_ptr<T>& child, double t) { return insertChild(index, child.get(), t); }
59
60 virtual bool removeChild( Node *child );
61
62 virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
63
64
65 /** value is which child node is to be displayed */
66 void setValue(int value) { _value = value ; }
67 int getValue() const { return _value; }
68
69 /** Set time in seconds for child. */
70 void setTime(unsigned int frame, double t);
71
72 /** Get time for child. */
73 double getTime(unsigned int frame) const;
74
75 /** Set the time list for children. */
76 void setTimeList(const std::vector<double>& timeList) { _frameTime = timeList; }
77
78 /** Get the time list for children. */
79 const std::vector<double>& getTimeList() const { return _frameTime; }
80
81 /** Set default time in seconds for new child.
82 if t<0, t=0 */
83 void setDefaultTime(double t) {_defaultTime = (t<0.?0.:t);}
84
85 /** Get default time in seconds for new child. */
86 double getDefaultTime(void) const {return _defaultTime;};
87
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);}
91
92 /** Get last frame time in seconds */
93 double getLastFrameTime(void) const {return _lastFrameTime;};
94
95 /** Get number of frames */
96 inline unsigned int getNumFrames() const { return _frameTime.size(); }
97
98 /** Interval modes. 'Loop' repeats frames 1-N; 'swing' repeats 1->N, (N-1)->1. */
99 enum LoopMode
100 {
101 LOOP,
102 SWING
103 };
104
105 /** Set sequence mode. */
106 void setLoopMode(LoopMode mode) { _loopMode = mode; _value = -1; }
107
108 /** Get sequence mode. */
109 LoopMode getLoopMode() const { return _loopMode; }
110
111 /** Set interval beginning. */
112 void setBegin(int begin) { _begin = begin; _value = -1; }
113
114 /** Get interval beginning. */
115 int getBegin() const { return _begin; }
116
117 /** Set interval ending. */
118 void setEnd(int end) { _end = end; _value = -1; }
119
120 /** Get interval ending. */
121 int getEnd() const { return _end; }
122
123 /** Set sequence mode & interval (range of children to be displayed). */
124 void setInterval(LoopMode mode, int begin, int end);
125
126 /** Get sequence mode & interval. */
127 inline void getInterval(LoopMode& mode, int& begin, int& end) const
128 {
129 mode = _loopMode;
130 begin = _begin;
131 end = _end;
132 }
133
134 /** Set speed. */
135 void setSpeed(float speed) { _speed = speed; }
136
137 /** Get speed. */
138 float getSpeed() const { return _speed; }
139
140 /** Set number of repeats. */
141 void setNumRepeats(int nreps) { _nreps = (nreps<0?-1:nreps); _nrepsRemain = _nreps; }
142
143 /** Get number of repeats. */
144 int getNumRepeats() const { return _nreps; }
145
146 /** Set duration: speed-up & number of repeats */
147 void setDuration(float speed, int nreps = -1);
148
149 /** Get duration & number of repeats. */
150 inline void getDuration(float& speed, int& nreps) const
151 {
152 speed = _speed;
153 nreps = _nreps;
154 }
155
156 /** Sequence modes. */
157 enum SequenceMode
158 {
159 START,
160 STOP,
161 PAUSE,
162 RESUME
163 };
164
165 /** Set sequence mode. Start/stop & pause/resume. */
166 void setMode(SequenceMode mode);
167
168 /** Get sequence mode. */
169 inline SequenceMode getMode() const { return _mode; }
170
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; }
174
175 /** Get sync value */
176 bool getSync() const { return _sync; }
177
178 /** If true, show no child nodes after stopping */
179 void setClearOnStop(bool clearOnStop) { _clearOnStop = clearOnStop; }
180
181 /** Get whether to show no child nodes after stopping */
182 bool getClearOnStop() const { return _clearOnStop; }
183
184 protected :
185
186 virtual ~Sequence() {}
187
188 // get next _value in sequence
189 int _getNextValue(void) ;
190
191 // update local variables
192 void _update(void) ;
193
194 // init to -1 to mean "restart"
195 int _value;
196
197 // current time, set by traverse
198 double _now ;
199
200 // time this frame started. init to -1.0f- means get current time
201 double _start;
202
203 // a vector of frame times, one per value
204 std::vector<double> _frameTime;
205
206 // the total time for one sequence, from BEGIN to END
207 double _totalTime ;
208
209 // true if _totalTime needs to be recalculated because setTime or
210 // setInterval was invoked, or a new child was added
211 bool _resetTotalTime ;
212
213 // store "loop mde", either LOOP or SWING
214 // init to LOOP- set by setInterval
215 LoopMode _loopMode;
216
217 // first and last "values" to sequence through
218 // begin inits to 0
219 // end inits to -1- means to init to number of values
220 int _begin, _end;
221
222 // multiplier of real-time clock- set to N to go N times faster
223 // init to 0- going nowhere
224 float _speed;
225
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;
231
232 // frame step (are we stepping forward or backward?)
233 int _step;
234
235 // default frame time for newly created frames or children- default is 1.
236 // set by setDefaultTime
237 double _defaultTime ;
238
239 // special time to display last frame of last loop
240 // <= zero means to not do anything special
241 double _lastFrameTime ;
242
243 // save the actual time of the last frame, and what value was stored
244 double _saveRealLastFrameTime ;
245 unsigned int _saveRealLastFrameValue ;
246
247 // the current mode
248 SequenceMode _mode;
249
250 // the current sync value
251 bool _sync ;
252
253 // the current clearOnStop value
254 bool _clearOnStop ;
255
256};
257
258}
259
260#endif