Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef ANIMATIONLAYER_H
  7. #define ANIMATIONLAYER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "rangecheckedvar.h"
  12. #include "lerp_functions.h"
  13. #include "networkvar.h"
  14. class C_AnimationLayer
  15. {
  16. public:
  17. // This allows the datatables to access private members.
  18. ALLOW_DATATABLES_PRIVATE_ACCESS();
  19. C_AnimationLayer();
  20. void Reset();
  21. void SetOrder( int order );
  22. public:
  23. bool IsActive( void );
  24. CRangeCheckedVar<int, -1, 65535, 0> m_nSequence;
  25. CRangeCheckedVar<float, -2, 2, 0> m_flPrevCycle;
  26. CRangeCheckedVar<float, -5, 5, 0> m_flWeight;
  27. int m_nOrder;
  28. // used for automatic crossfades between sequence changes
  29. CRangeCheckedVar<float, -50, 50, 1> m_flPlaybackRate;
  30. CRangeCheckedVar<float, -2, 2, 0> m_flCycle;
  31. float GetFadeout( float flCurTime );
  32. void BlendWeight();
  33. float m_flLayerAnimtime;
  34. float m_flLayerFadeOuttime;
  35. float m_flBlendIn;
  36. float m_flBlendOut;
  37. bool m_bClientBlend;
  38. };
  39. #ifdef CLIENT_DLL
  40. #define CAnimationLayer C_AnimationLayer
  41. #endif
  42. inline C_AnimationLayer::C_AnimationLayer()
  43. {
  44. Reset();
  45. }
  46. inline void C_AnimationLayer::Reset()
  47. {
  48. m_nSequence = 0;
  49. m_flPrevCycle = 0;
  50. m_flWeight = 0;
  51. m_flPlaybackRate = 0;
  52. m_flCycle = 0;
  53. m_flLayerAnimtime = 0;
  54. m_flLayerFadeOuttime = 0;
  55. m_flBlendIn = 0;
  56. m_flBlendOut = 0;
  57. m_bClientBlend = false;
  58. }
  59. inline void C_AnimationLayer::SetOrder( int order )
  60. {
  61. m_nOrder = order;
  62. }
  63. inline float C_AnimationLayer::GetFadeout( float flCurTime )
  64. {
  65. float s;
  66. if (m_flLayerFadeOuttime <= 0.0f)
  67. {
  68. s = 0;
  69. }
  70. else
  71. {
  72. // blend in over 0.2 seconds
  73. s = 1.0 - (flCurTime - m_flLayerAnimtime) / m_flLayerFadeOuttime;
  74. if (s > 0 && s <= 1.0)
  75. {
  76. // do a nice spline curve
  77. s = 3 * s * s - 2 * s * s * s;
  78. }
  79. else if ( s > 1.0f )
  80. {
  81. // Shouldn't happen, but maybe curtime is behind animtime?
  82. s = 1.0f;
  83. }
  84. }
  85. return s;
  86. }
  87. inline C_AnimationLayer LoopingLerp( float flPercent, C_AnimationLayer& from, C_AnimationLayer& to )
  88. {
  89. C_AnimationLayer output;
  90. output.m_nSequence = to.m_nSequence;
  91. output.m_flCycle = LoopingLerp( flPercent, (float)from.m_flCycle, (float)to.m_flCycle );
  92. output.m_flPrevCycle = to.m_flPrevCycle;
  93. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  94. output.m_nOrder = to.m_nOrder;
  95. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  96. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  97. return output;
  98. }
  99. inline C_AnimationLayer Lerp( float flPercent, const C_AnimationLayer& from, const C_AnimationLayer& to )
  100. {
  101. C_AnimationLayer output;
  102. output.m_nSequence = to.m_nSequence;
  103. output.m_flCycle = Lerp( flPercent, from.m_flCycle, to.m_flCycle );
  104. output.m_flPrevCycle = to.m_flPrevCycle;
  105. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  106. output.m_nOrder = to.m_nOrder;
  107. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  108. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  109. return output;
  110. }
  111. inline C_AnimationLayer LoopingLerp_Hermite( float flPercent, C_AnimationLayer& prev, C_AnimationLayer& from, C_AnimationLayer& to )
  112. {
  113. C_AnimationLayer output;
  114. output.m_nSequence = to.m_nSequence;
  115. output.m_flCycle = LoopingLerp_Hermite( flPercent, (float)prev.m_flCycle, (float)from.m_flCycle, (float)to.m_flCycle );
  116. output.m_flPrevCycle = to.m_flPrevCycle;
  117. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  118. output.m_nOrder = to.m_nOrder;
  119. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  120. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  121. return output;
  122. }
  123. // YWB: Specialization for interpolating euler angles via quaternions...
  124. inline C_AnimationLayer Lerp_Hermite( float flPercent, const C_AnimationLayer& prev, const C_AnimationLayer& from, const C_AnimationLayer& to )
  125. {
  126. C_AnimationLayer output;
  127. output.m_nSequence = to.m_nSequence;
  128. output.m_flCycle = Lerp_Hermite( flPercent, prev.m_flCycle, from.m_flCycle, to.m_flCycle );
  129. output.m_flPrevCycle = to.m_flPrevCycle;
  130. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  131. output.m_nOrder = to.m_nOrder;
  132. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  133. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  134. return output;
  135. }
  136. inline void Lerp_Clamp( C_AnimationLayer &val )
  137. {
  138. Lerp_Clamp( val.m_nSequence );
  139. Lerp_Clamp( val.m_flCycle );
  140. Lerp_Clamp( val.m_flPrevCycle );
  141. Lerp_Clamp( val.m_flWeight );
  142. Lerp_Clamp( val.m_nOrder );
  143. Lerp_Clamp( val.m_flLayerAnimtime );
  144. Lerp_Clamp( val.m_flLayerFadeOuttime );
  145. }
  146. inline void C_AnimationLayer::BlendWeight()
  147. {
  148. if ( !m_bClientBlend )
  149. return;
  150. m_flWeight = 1;
  151. // blend in?
  152. if ( m_flBlendIn != 0.0f )
  153. {
  154. if (m_flCycle < m_flBlendIn)
  155. {
  156. m_flWeight = m_flCycle / m_flBlendIn;
  157. }
  158. }
  159. // blend out?
  160. if ( m_flBlendOut != 0.0f )
  161. {
  162. if (m_flCycle > 1.0 - m_flBlendOut)
  163. {
  164. m_flWeight = (1.0 - m_flCycle) / m_flBlendOut;
  165. }
  166. }
  167. m_flWeight = 3.0 * m_flWeight * m_flWeight - 2.0 * m_flWeight * m_flWeight * m_flWeight;
  168. if (m_nSequence == 0)
  169. m_flWeight = 0;
  170. }
  171. #endif // ANIMATIONLAYER_H