sequenceNode.I 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Filename: sequenceNode.I
  2. // Created by: drose (06Mar02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: SequenceNode::CData::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. INLINE SequenceNode::CData::
  24. CData() {
  25. _cycle_rate = 0.0f;
  26. _frame_offset = 0.0f;
  27. _start_time = 0.0f;
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: SequenceNode::CData::Copy Constructor
  31. // Access: Public
  32. // Description:
  33. ////////////////////////////////////////////////////////////////////
  34. INLINE SequenceNode::CData::
  35. CData(const SequenceNode::CData &copy) :
  36. _cycle_rate(copy._cycle_rate),
  37. _frame_offset(copy._frame_offset),
  38. _start_time(copy._start_time)
  39. {
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: SequenceNode::Constructor
  43. // Access: Published
  44. // Description:
  45. ////////////////////////////////////////////////////////////////////
  46. INLINE SequenceNode::
  47. SequenceNode(float cycle_rate, const string &name) :
  48. SelectiveChildNode(name)
  49. {
  50. CDWriter cdata(_cycler);
  51. cdata->_cycle_rate = cycle_rate;
  52. cdata->_frame_offset = 0.0f;
  53. float now = ClockObject::get_global_clock()->get_frame_time();
  54. cdata->_start_time = now;
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function: SequenceNode::set_cycle_rate
  58. // Access: Published
  59. // Description: Sets the rate of cycling for the children of the
  60. // SequenceNode, in cycles per second.
  61. ////////////////////////////////////////////////////////////////////
  62. INLINE void SequenceNode::
  63. set_cycle_rate(float cycle_rate) {
  64. // Do some fussing so we keep the same frame visible while we
  65. // change this.
  66. CDWriter cdata(_cycler);
  67. float now = ClockObject::get_global_clock()->get_frame_time();
  68. cdata->_frame_offset = calc_frame(now);
  69. cdata->_start_time = now;
  70. cdata->_cycle_rate = cycle_rate;
  71. }
  72. ////////////////////////////////////////////////////////////////////
  73. // Function: SequenceNode::get_cycle_rate
  74. // Access: Published
  75. // Description: Returns the rate of cycling for the children of the
  76. // SequenceNode, in cycles per second.
  77. ////////////////////////////////////////////////////////////////////
  78. INLINE float SequenceNode::
  79. get_cycle_rate() const {
  80. CDReader cdata(_cycler);
  81. return cdata->_cycle_rate;
  82. }
  83. ////////////////////////////////////////////////////////////////////
  84. // Function: SequenceNode::set_visible_child
  85. // Access: Published
  86. // Description: Sets the particular child that this SequenceNode will
  87. // display this frame. Future frames will proceed from
  88. // here.
  89. ////////////////////////////////////////////////////////////////////
  90. INLINE void SequenceNode::
  91. set_visible_child(int index) {
  92. int num_children = get_num_children();
  93. if (num_children != 0) {
  94. CDWriter cdata(_cycler);
  95. float now = ClockObject::get_global_clock()->get_frame_time();
  96. cdata->_frame_offset = (index - (now - cdata->_start_time) * cdata->_cycle_rate);
  97. }
  98. }
  99. ////////////////////////////////////////////////////////////////////
  100. // Function: SequenceNode::calc_frame
  101. // Access: Private
  102. // Description: Returns the floating-point frame number at the
  103. // indicated time.
  104. ////////////////////////////////////////////////////////////////////
  105. INLINE float SequenceNode::
  106. calc_frame(float now) const {
  107. CDReader cdata(_cycler);
  108. return (now - cdata->_start_time) * cdata->_cycle_rate + cdata->_frame_offset;
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: SequenceNode::calc_frame
  112. // Access: Private
  113. // Description: Returns the floating-point frame number at the
  114. // current time.
  115. ////////////////////////////////////////////////////////////////////
  116. INLINE float SequenceNode::
  117. calc_frame() const {
  118. return calc_frame(ClockObject::get_global_clock()->get_frame_time());
  119. }