Sequence 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #ifndef OSG_SEQUENCE
  14. #define OSG_SEQUENCE 1
  15. #include <osg/Group>
  16. namespace osg
  17. {
  18. /** Sequence is a Group node which allows automatic, time based
  19. switching between children.
  20. */
  21. class OSG_EXPORT Sequence : public Group
  22. {
  23. public :
  24. Sequence();
  25. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  26. Sequence(const Sequence&, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  27. META_Node(osg, Sequence);
  28. virtual void traverse(NodeVisitor& nv);
  29. // the relationship between the _frameTime vector and the _children
  30. // vector is a bit of a mess. This is how it was in previous versions,
  31. // and there's no way out of it if upward compatibility needs to be
  32. // maintained. New code should set defaultTime and use addChild, and
  33. // not mess with the setTime method
  34. using osg::Group::addChild;
  35. using osg::Group::insertChild;
  36. using osg::Group::removeChild;
  37. virtual bool addChild( Node *child);
  38. virtual bool addChild( Node *child, double t);
  39. template<class T> bool addChild( const ref_ptr<T>& child, double t) { return addChild(child.get(), t); }
  40. virtual bool insertChild( unsigned int index, Node *child);
  41. virtual bool insertChild( unsigned int index, Node *child, double t);
  42. template<class T> bool insertChild( unsigned int index, const ref_ptr<T>& child, double t) { return insertChild(index, child.get(), t); }
  43. virtual bool removeChild( Node *child );
  44. virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
  45. /** value is which child node is to be displayed */
  46. void setValue(int value) { _value = value ; }
  47. int getValue() const { return _value; }
  48. /** Set time in seconds for child. */
  49. void setTime(unsigned int frame, double t);
  50. /** Get time for child. */
  51. double getTime(unsigned int frame) const;
  52. /** Set the time list for children. */
  53. void setTimeList(const std::vector<double>& timeList) { _frameTime = timeList; }
  54. /** Get the time list for children. */
  55. const std::vector<double>& getTimeList() const { return _frameTime; }
  56. /** Set default time in seconds for new child.
  57. if t<0, t=0 */
  58. void setDefaultTime(double t) {_defaultTime = (t<0.?0.:t);}
  59. /** Get default time in seconds for new child. */
  60. double getDefaultTime(void) const {return _defaultTime;};
  61. /** Set time of last frame of last loop, in seconds.
  62. if t<= 0, then ignored */
  63. void setLastFrameTime(double t) {_lastFrameTime = (t<0.?0.:t);}
  64. /** Get last frame time in seconds */
  65. double getLastFrameTime(void) const {return _lastFrameTime;};
  66. /** Get number of frames */
  67. inline unsigned int getNumFrames() const { return _frameTime.size(); }
  68. /** Interval modes. 'Loop' repeats frames 1-N; 'swing' repeats 1->N, (N-1)->1. */
  69. enum LoopMode
  70. {
  71. LOOP,
  72. SWING
  73. };
  74. /** Set sequence mode. */
  75. void setLoopMode(LoopMode mode) { _loopMode = mode; _value = -1; }
  76. /** Get sequence mode. */
  77. LoopMode getLoopMode() const { return _loopMode; }
  78. /** Set interval beginning. */
  79. void setBegin(int begin) { _begin = begin; _value = -1; }
  80. /** Get interval beginning. */
  81. int getBegin() const { return _begin; }
  82. /** Set interval ending. */
  83. void setEnd(int end) { _end = end; _value = -1; }
  84. /** Get interval ending. */
  85. int getEnd() const { return _end; }
  86. /** Set sequence mode & interval (range of children to be displayed). */
  87. void setInterval(LoopMode mode, int begin, int end);
  88. /** Get sequence mode & interval. */
  89. inline void getInterval(LoopMode& mode, int& begin, int& end) const
  90. {
  91. mode = _loopMode;
  92. begin = _begin;
  93. end = _end;
  94. }
  95. /** Set speed. */
  96. void setSpeed(float speed) { _speed = speed; }
  97. /** Get speed. */
  98. float getSpeed() const { return _speed; }
  99. /** Set number of repeats. */
  100. void setNumRepeats(int nreps) { _nreps = (nreps<0?-1:nreps); _nrepsRemain = _nreps; }
  101. /** Get number of repeats. */
  102. int getNumRepeats() const { return _nreps; }
  103. /** Set duration: speed-up & number of repeats */
  104. void setDuration(float speed, int nreps = -1);
  105. /** Get duration & number of repeats. */
  106. inline void getDuration(float& speed, int& nreps) const
  107. {
  108. speed = _speed;
  109. nreps = _nreps;
  110. }
  111. /** Sequence modes. */
  112. enum SequenceMode
  113. {
  114. START,
  115. STOP,
  116. PAUSE,
  117. RESUME
  118. };
  119. /** Set sequence mode. Start/stop & pause/resume. */
  120. void setMode(SequenceMode mode);
  121. /** Get sequence mode. */
  122. inline SequenceMode getMode() const { return _mode; }
  123. /** If false (default), frames will not be sync'd to frameTime. If
  124. true, frames will be sync'd to frameTime. */
  125. void setSync(bool sync) { _sync = sync; }
  126. /** Get sync value */
  127. bool getSync() const { return _sync; }
  128. /** If true, show no child nodes after stopping */
  129. void setClearOnStop(bool clearOnStop) { _clearOnStop = clearOnStop; }
  130. /** Get whether to show no child nodes after stopping */
  131. bool getClearOnStop() const { return _clearOnStop; }
  132. protected :
  133. virtual ~Sequence() {}
  134. // get next _value in sequence
  135. int _getNextValue(void) ;
  136. // update local variables
  137. void _update(void) ;
  138. // init to -1 to mean "restart"
  139. int _value;
  140. // current time, set by traverse
  141. double _now ;
  142. // time this frame started. init to -1.0f- means get current time
  143. double _start;
  144. // a vector of frame times, one per value
  145. std::vector<double> _frameTime;
  146. // the total time for one sequence, from BEGIN to END
  147. double _totalTime ;
  148. // true if _totalTime needs to be recalculated because setTime or
  149. // setInterval was invoked, or a new child was added
  150. bool _resetTotalTime ;
  151. // store "loop mde", either LOOP or SWING
  152. // init to LOOP- set by setInterval
  153. LoopMode _loopMode;
  154. // first and last "values" to sequence through
  155. // begin inits to 0
  156. // end inits to -1- means to init to number of values
  157. int _begin, _end;
  158. // multiplier of real-time clock- set to N to go N times faster
  159. // init to 0- going nowhere
  160. float _speed;
  161. // _nreps: how many times to repeat- default param is -1, repeat forever
  162. // init to 0, no repetitions
  163. // _nrepsRemain: set to nreps and counts down every traversal,
  164. // stopping when it gets to zero. init to 0
  165. int _nreps, _nrepsRemain;
  166. // frame step (are we stepping forward or backward?)
  167. int _step;
  168. // default frame time for newly created frames or children- default is 1.
  169. // set by setDefaultTime
  170. double _defaultTime ;
  171. // special time to display last frame of last loop
  172. // <= zero means to not do anything special
  173. double _lastFrameTime ;
  174. // save the actual time of the last frame, and what value was stored
  175. double _saveRealLastFrameTime ;
  176. unsigned int _saveRealLastFrameValue ;
  177. // the current mode
  178. SequenceMode _mode;
  179. // the current sync value
  180. bool _sync ;
  181. // the current clearOnStop value
  182. bool _clearOnStop ;
  183. };
  184. }
  185. #endif