Counter Strike : Global Offensive Source Code
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.

347 lines
8.9 KiB

  1. //===== Copyright � 1996-2005, 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 "tier1/lerp_functions.h"
  13. #ifdef CLIENT_DLL
  14. class C_BaseAnimatingOverlay;
  15. #endif
  16. class C_AnimationLayer
  17. {
  18. public:
  19. // This allows the datatables to access private members.
  20. ALLOW_DATATABLES_PRIVATE_ACCESS();
  21. C_AnimationLayer();
  22. void Reset();
  23. #ifdef CLIENT_DLL
  24. void SetOwner( C_BaseAnimatingOverlay *pOverlay );
  25. C_BaseAnimatingOverlay *GetOwner() const;
  26. #endif
  27. void SetOrder( int order );
  28. bool IsActive( void );
  29. float GetFadeout( float flCurTime );
  30. void SetSequence( int nSequence );
  31. void SetCycle( float flCycle );
  32. void SetPrevCycle( float flCycle );
  33. void SetPlaybackRate( float flPlaybackRate );
  34. void SetWeight( float flWeight );
  35. void SetWeightDeltaRate( float flDelta );
  36. int GetOrder() const;
  37. int GetSequence( ) const;
  38. float GetCycle( ) const;
  39. float GetPrevCycle( ) const;
  40. float GetPlaybackRate( ) const;
  41. float GetWeight( ) const;
  42. float GetWeightDeltaRate( ) const;
  43. #ifdef CLIENT_DLL
  44. // If the weights, cycle or sequence #s changed due to interpolation then
  45. // we'll need to recompute the bbox
  46. int GetInvalidatePhysicsBits() const;
  47. void SetInvalidatePhysicsBits( int iBit ) { m_nInvalidatePhysicsBits = iBit; }
  48. #endif
  49. public:
  50. float m_flLayerAnimtime;
  51. float m_flLayerFadeOuttime;
  52. // dispatch flags
  53. CStudioHdr *m_pDispatchedStudioHdr;
  54. int m_nDispatchedSrc;
  55. int m_nDispatchedDst;
  56. private:
  57. int m_nOrder;
  58. CRangeCheckedVar<int, -1, 65535, 0> m_nSequence;
  59. CRangeCheckedVar<float, -2, 2, 0> m_flPrevCycle;
  60. CRangeCheckedVar<float, -5, 5, 0> m_flWeight;
  61. CRangeCheckedVar<float, -5, 5, 0> m_flWeightDeltaRate;
  62. // used for automatic crossfades between sequence changes
  63. CRangeCheckedVar<float, -50, 50, 1> m_flPlaybackRate;
  64. CRangeCheckedVar<float, -2, 2, 0> m_flCycle;
  65. #ifdef CLIENT_DLL
  66. C_BaseAnimatingOverlay *m_pOwner;
  67. int m_nInvalidatePhysicsBits;
  68. #endif
  69. friend class C_BaseAnimatingOverlay;
  70. friend C_AnimationLayer LoopingLerp( float flPercent, C_AnimationLayer& from, C_AnimationLayer& to );
  71. friend C_AnimationLayer Lerp( float flPercent, const C_AnimationLayer& from, const C_AnimationLayer& to );
  72. friend C_AnimationLayer LoopingLerp_Hermite( const C_AnimationLayer& current, float flPercent, C_AnimationLayer& prev, C_AnimationLayer& from, C_AnimationLayer& to );
  73. friend C_AnimationLayer Lerp_Hermite( const C_AnimationLayer& current, float flPercent, const C_AnimationLayer& prev, const C_AnimationLayer& from, const C_AnimationLayer& to );
  74. friend void Lerp_Clamp( C_AnimationLayer &val );
  75. friend int CheckForSequenceBoxChanges( const C_AnimationLayer& newLayer, const C_AnimationLayer& oldLayer );
  76. };
  77. #ifdef CLIENT_DLL
  78. #define CAnimationLayer C_AnimationLayer
  79. #endif
  80. inline C_AnimationLayer::C_AnimationLayer()
  81. {
  82. #ifdef CLIENT_DLL
  83. m_pOwner = NULL;
  84. m_nInvalidatePhysicsBits = 0;
  85. #endif
  86. m_pDispatchedStudioHdr = NULL;
  87. m_nDispatchedSrc = ACT_INVALID;
  88. m_nDispatchedDst = ACT_INVALID;
  89. Reset();
  90. }
  91. #ifdef GAME_DLL
  92. inline void C_AnimationLayer::SetSequence( int nSequence )
  93. {
  94. m_nSequence = nSequence;
  95. }
  96. inline void C_AnimationLayer::SetCycle( float flCycle )
  97. {
  98. m_flCycle = flCycle;
  99. }
  100. inline void C_AnimationLayer::SetWeight( float flWeight )
  101. {
  102. m_flWeight = flWeight;
  103. }
  104. #endif // GAME_DLL
  105. FORCEINLINE void C_AnimationLayer::SetPrevCycle( float flPrevCycle )
  106. {
  107. m_flPrevCycle = flPrevCycle;
  108. }
  109. FORCEINLINE void C_AnimationLayer::SetPlaybackRate( float flPlaybackRate )
  110. {
  111. m_flPlaybackRate = flPlaybackRate;
  112. }
  113. FORCEINLINE void C_AnimationLayer::SetWeightDeltaRate( float flDelta )
  114. {
  115. m_flWeightDeltaRate = flDelta;
  116. }
  117. FORCEINLINE int C_AnimationLayer::GetSequence( ) const
  118. {
  119. return m_nSequence;
  120. }
  121. FORCEINLINE float C_AnimationLayer::GetCycle( ) const
  122. {
  123. return m_flCycle;
  124. }
  125. FORCEINLINE float C_AnimationLayer::GetPrevCycle( ) const
  126. {
  127. return m_flPrevCycle;
  128. }
  129. FORCEINLINE float C_AnimationLayer::GetPlaybackRate( ) const
  130. {
  131. return m_flPlaybackRate;
  132. }
  133. FORCEINLINE float C_AnimationLayer::GetWeight( ) const
  134. {
  135. return m_flWeight;
  136. }
  137. FORCEINLINE float C_AnimationLayer::GetWeightDeltaRate( ) const
  138. {
  139. return m_flWeightDeltaRate;
  140. }
  141. FORCEINLINE int C_AnimationLayer::GetOrder() const
  142. {
  143. return m_nOrder;
  144. }
  145. inline float C_AnimationLayer::GetFadeout( float flCurTime )
  146. {
  147. float s;
  148. if (m_flLayerFadeOuttime <= 0.0f)
  149. {
  150. s = 0;
  151. }
  152. else
  153. {
  154. // blend in over 0.2 seconds
  155. s = 1.0 - (flCurTime - m_flLayerAnimtime) / m_flLayerFadeOuttime;
  156. if (s > 0 && s <= 1.0)
  157. {
  158. // do a nice spline curve
  159. s = 3 * s * s - 2 * s * s * s;
  160. }
  161. else if ( s > 1.0f )
  162. {
  163. // Shouldn't happen, but maybe curtime is behind animtime?
  164. s = 1.0f;
  165. }
  166. }
  167. return s;
  168. }
  169. #ifdef CLIENT_DLL
  170. FORCEINLINE int C_AnimationLayer::GetInvalidatePhysicsBits() const
  171. {
  172. return m_nInvalidatePhysicsBits;
  173. }
  174. #endif
  175. inline C_AnimationLayer LoopingLerp( float flPercent, C_AnimationLayer& from, C_AnimationLayer& to )
  176. {
  177. #ifdef CLIENT_DLL
  178. Assert( from.GetOwner() == to.GetOwner() );
  179. #endif
  180. C_AnimationLayer output;
  181. output.m_nSequence = to.m_nSequence;
  182. output.m_flCycle = LoopingLerp( flPercent, (float)from.m_flCycle, (float)to.m_flCycle );
  183. output.m_flPrevCycle = to.m_flPrevCycle;
  184. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  185. output.m_nOrder = to.m_nOrder;
  186. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  187. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  188. #ifdef CLIENT_DLL
  189. output.SetOwner( to.GetOwner() );
  190. #endif
  191. return output;
  192. }
  193. inline C_AnimationLayer Lerp( float flPercent, const C_AnimationLayer& from, const C_AnimationLayer& to )
  194. {
  195. #ifdef CLIENT_DLL
  196. Assert( from.GetOwner() == to.GetOwner() );
  197. #endif
  198. C_AnimationLayer output;
  199. output.m_nSequence = to.m_nSequence;
  200. output.m_flCycle = Lerp( flPercent, from.m_flCycle, to.m_flCycle );
  201. output.m_flPrevCycle = to.m_flPrevCycle;
  202. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  203. output.m_nOrder = to.m_nOrder;
  204. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  205. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  206. #ifdef CLIENT_DLL
  207. output.SetOwner( to.GetOwner() );
  208. #endif
  209. return output;
  210. }
  211. inline int CheckForSequenceBoxChanges( const C_AnimationLayer& newLayer, const C_AnimationLayer& oldLayer )
  212. {
  213. int nChangeFlags = 0;
  214. bool bOldIsZero = ( oldLayer.GetWeight() == 0.0f );
  215. bool bNewIsZero = ( newLayer.GetWeight() == 0.0f );
  216. if ( ( newLayer.GetSequence() != oldLayer.GetSequence() ) ||
  217. ( bNewIsZero != bOldIsZero ) )
  218. {
  219. nChangeFlags |= SEQUENCE_CHANGED | BOUNDS_CHANGED;
  220. }
  221. if ( newLayer.GetCycle() != oldLayer.GetCycle() )
  222. {
  223. nChangeFlags |= ANIMATION_CHANGED;
  224. }
  225. if ( newLayer.GetOrder() != oldLayer.GetOrder() )
  226. {
  227. nChangeFlags |= BOUNDS_CHANGED;
  228. }
  229. return nChangeFlags;
  230. }
  231. inline C_AnimationLayer LoopingLerp_Hermite( const C_AnimationLayer& current, float flPercent, C_AnimationLayer& prev, C_AnimationLayer& from, C_AnimationLayer& to )
  232. {
  233. #ifdef CLIENT_DLL
  234. Assert( prev.GetOwner() == from.GetOwner() );
  235. Assert( from.GetOwner() == to.GetOwner() );
  236. #endif
  237. C_AnimationLayer output;
  238. output.m_nSequence = to.m_nSequence;
  239. output.m_flCycle = LoopingLerp_Hermite( (float)current.m_flCycle, flPercent, (float)prev.m_flCycle, (float)from.m_flCycle, (float)to.m_flCycle );
  240. output.m_flPrevCycle = to.m_flPrevCycle;
  241. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  242. output.m_nOrder = to.m_nOrder;
  243. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  244. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  245. #ifdef CLIENT_DLL
  246. output.SetOwner( to.GetOwner() );
  247. output.m_nInvalidatePhysicsBits = CheckForSequenceBoxChanges( output, current );
  248. #endif
  249. return output;
  250. }
  251. // YWB: Specialization for interpolating euler angles via quaternions...
  252. inline C_AnimationLayer Lerp_Hermite( const C_AnimationLayer& current, float flPercent, const C_AnimationLayer& prev, const C_AnimationLayer& from, const C_AnimationLayer& to )
  253. {
  254. #ifdef CLIENT_DLL
  255. Assert( prev.GetOwner() == from.GetOwner() );
  256. Assert( from.GetOwner() == to.GetOwner() );
  257. #endif
  258. C_AnimationLayer output;
  259. output.m_nSequence = to.m_nSequence;
  260. output.m_flCycle = Lerp_Hermite( (float)current.m_flCycle, flPercent, (float)prev.m_flCycle, (float)from.m_flCycle, (float)to.m_flCycle );
  261. output.m_flPrevCycle = to.m_flPrevCycle;
  262. output.m_flWeight = Lerp( flPercent, from.m_flWeight, to.m_flWeight );
  263. output.m_nOrder = to.m_nOrder;
  264. output.m_flLayerAnimtime = to.m_flLayerAnimtime;
  265. output.m_flLayerFadeOuttime = to.m_flLayerFadeOuttime;
  266. #ifdef CLIENT_DLL
  267. output.SetOwner( to.GetOwner() );
  268. output.m_nInvalidatePhysicsBits = CheckForSequenceBoxChanges( output, current );
  269. #endif
  270. return output;
  271. }
  272. inline void Lerp_Clamp( C_AnimationLayer &val )
  273. {
  274. Lerp_Clamp( val.m_nSequence );
  275. Lerp_Clamp( val.m_flCycle );
  276. Lerp_Clamp( val.m_flPrevCycle );
  277. Lerp_Clamp( val.m_flWeight );
  278. Lerp_Clamp( val.m_nOrder );
  279. Lerp_Clamp( val.m_flLayerAnimtime );
  280. Lerp_Clamp( val.m_flLayerFadeOuttime );
  281. }
  282. #endif // ANIMATIONLAYER_H