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.

335 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "movieobjects/dmetimeselection.h"
  3. #include "interpolatortypes.h"
  4. #include "datamodel/dmelementfactoryhelper.h"
  5. // #include "dme_controls/RecordingState.h"
  6. IMPLEMENT_ELEMENT_FACTORY( DmeTimeSelection, CDmeTimeSelection );
  7. void CDmeTimeSelection::OnConstruction()
  8. {
  9. m_bEnabled.InitAndSet( this, "enabled", false );
  10. m_bRelative.InitAndSet( this, "relative", false );
  11. DmeTime_t one( 1.0f );
  12. m_falloff[ 0 ].InitAndSet( this, "falloff_left", -one.GetTenthsOfMS() );
  13. m_falloff[ 1 ].InitAndSet( this, "falloff_right", one.GetTenthsOfMS() );
  14. m_hold[ 0 ].Init( this, "hold_left" );
  15. m_hold[ 1 ].Init( this, "hold_right" );
  16. m_nFalloffInterpolatorType[ 0 ].InitAndSet( this, "interpolator_left", INTERPOLATE_LINEAR_INTERP );
  17. m_nFalloffInterpolatorType[ 1 ].InitAndSet( this, "interpolator_right", INTERPOLATE_LINEAR_INTERP );
  18. m_threshold.InitAndSet( this, "threshold", 0.0005f );
  19. m_nRecordingState.InitAndSet( this, "recordingstate", 3 /*AS_PLAYBACK : HACK THIS SHOULD MOVE TO A PUBLIC HEADER*/ );
  20. }
  21. void CDmeTimeSelection::OnDestruction()
  22. {
  23. }
  24. static int g_InterpolatorTypes[] =
  25. {
  26. INTERPOLATE_LINEAR_INTERP,
  27. INTERPOLATE_EASE_IN,
  28. INTERPOLATE_EASE_OUT,
  29. INTERPOLATE_EASE_INOUT,
  30. };
  31. float CDmeTimeSelection::AdjustFactorForInterpolatorType( float factor, int side )
  32. {
  33. Vector points[ 4 ];
  34. points[ 0 ].Init();
  35. points[ 1 ].Init( 0.0, 0.0, 0.0f );
  36. points[ 2 ].Init( 1.0f, 1.0f, 0.0f );
  37. points[ 3 ].Init();
  38. Vector out;
  39. Interpolator_CurveInterpolate
  40. (
  41. GetFalloffInterpolatorType( side ),
  42. points[ 0 ], // unused
  43. points[ 1 ],
  44. points[ 2 ],
  45. points[ 3 ], // unused
  46. factor,
  47. out
  48. );
  49. return out.y; // clamp( out.y, 0.0f, 1.0f );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // per-type averaging methods
  53. //-----------------------------------------------------------------------------
  54. float CDmeTimeSelection::GetAmountForTime( DmeTime_t t, DmeTime_t curtime )
  55. {
  56. Assert( IsEnabled() );
  57. float minfrac = 0.0f;
  58. // FIXME, this is slow, we should cache this maybe?
  59. DmeTime_t times[ 4 ];
  60. times[ 0 ] = GetAbsFalloff( curtime, 0 );
  61. times[ 1 ] = GetAbsHold( curtime, 0 );
  62. times[ 2 ] = GetAbsHold( curtime, 1 );
  63. times[ 3 ] = GetAbsFalloff( curtime, 1 );
  64. Vector points[ 4 ];
  65. points[ 0 ].Init();
  66. points[ 1 ].Init( 0.0, 0.0, 0.0f );
  67. points[ 2 ].Init( 1.0f, 1.0f, 0.0f );
  68. points[ 3 ].Init();
  69. if ( t >= times[ 0 ] && t < times[ 1 ] )
  70. {
  71. float f = GetFractionOfTimeBetween( t, times[ 0 ], times[ 1 ], true );
  72. Vector out;
  73. Interpolator_CurveInterpolate
  74. (
  75. GetFalloffInterpolatorType( 0 ),
  76. points[ 0 ], // unused
  77. points[ 1 ],
  78. points[ 2 ],
  79. points[ 3 ], // unused
  80. f,
  81. out
  82. );
  83. return clamp( out.y, minfrac, 1.0f );
  84. }
  85. if ( t >= times[ 1 ] && t <= times[ 2 ] )
  86. {
  87. return 1.0f;
  88. }
  89. if ( t > times[ 2 ] && t <= times[ 3 ] )
  90. {
  91. float f = 1.0f - GetFractionOfTimeBetween( t, times[ 2 ], times[ 3 ], true );
  92. Vector out;
  93. Interpolator_CurveInterpolate
  94. (
  95. GetFalloffInterpolatorType( 1 ),
  96. points[ 0 ], // unused
  97. points[ 1 ],
  98. points[ 2 ],
  99. points[ 3 ], // unused
  100. f,
  101. out
  102. );
  103. return clamp( out.y, minfrac, 1.0f );
  104. }
  105. return minfrac;
  106. }
  107. void CDmeTimeSelection::GetAlphaForTime( DmeTime_t t, DmeTime_t curtime, byte& alpha )
  108. {
  109. Assert( IsEnabled() );
  110. byte minAlpha = 31;
  111. // FIXME, this is slow, we should cache this maybe?
  112. DmeTime_t times[ 4 ];
  113. times[ 0 ] = GetAbsFalloff( curtime, 0 );
  114. times[ 1 ] = GetAbsHold( curtime, 0 );
  115. times[ 2 ] = GetAbsHold( curtime, 1 );
  116. times[ 3 ] = GetAbsFalloff( curtime, 1 );
  117. DmeTime_t dt1, dt2;
  118. dt1 = times[ 1 ] - times[ 0 ];
  119. dt2 = times[ 3 ] - times[ 2 ];
  120. if ( dt1 > DmeTime_t( 0 ) &&
  121. t >= times[ 0 ] && t < times[ 1 ] )
  122. {
  123. float frac = GetFractionOfTime( t - times[ 0 ], dt1, false );
  124. alpha = clamp( alpha * frac, minAlpha, 255 );
  125. return;
  126. }
  127. if ( dt2 > DmeTime_t( 0 ) &&
  128. t > times[ 2 ] && t <= times[ 3 ] )
  129. {
  130. float frac = GetFractionOfTime( times[ 3 ] - t, dt2, false );
  131. alpha = clamp( alpha * frac, minAlpha, 255 );
  132. return;
  133. }
  134. if ( t < times[ 0 ] )
  135. alpha = minAlpha;
  136. else if ( t > times[ 3 ] )
  137. alpha = minAlpha;
  138. }
  139. int CDmeTimeSelection::GetFalloffInterpolatorType( int side )
  140. {
  141. return m_nFalloffInterpolatorType[ side ];
  142. }
  143. void CDmeTimeSelection::SetFalloffInterpolatorType( int side, int interpolatorType )
  144. {
  145. m_nFalloffInterpolatorType[ side ] = interpolatorType;
  146. }
  147. bool CDmeTimeSelection::IsEnabled() const
  148. {
  149. return m_bEnabled;
  150. }
  151. void CDmeTimeSelection::SetEnabled( bool state )
  152. {
  153. m_bEnabled = state;
  154. }
  155. bool CDmeTimeSelection::IsRelative() const
  156. {
  157. return m_bRelative;
  158. }
  159. void CDmeTimeSelection::SetRelative( DmeTime_t time, bool state )
  160. {
  161. bool changed = m_bRelative != state;
  162. m_bRelative = state;
  163. if ( changed )
  164. {
  165. if ( state )
  166. ConvertToRelative( time );
  167. else
  168. ConvertToAbsolute( time );
  169. }
  170. }
  171. DmeTime_t CDmeTimeSelection::GetAbsFalloff( DmeTime_t time, int side )
  172. {
  173. if ( m_bRelative )
  174. {
  175. return DmeTime_t( m_falloff[ side ] ) + time;
  176. }
  177. return DmeTime_t( m_falloff[ side ] );
  178. }
  179. DmeTime_t CDmeTimeSelection::GetAbsHold( DmeTime_t time, int side )
  180. {
  181. if ( m_bRelative )
  182. {
  183. return DmeTime_t( m_hold[ side ] ) + time;
  184. }
  185. return DmeTime_t( m_hold[ side ] );
  186. }
  187. DmeTime_t CDmeTimeSelection::GetRelativeFalloff( DmeTime_t time, int side )
  188. {
  189. if ( m_bRelative )
  190. {
  191. return DmeTime_t( m_falloff[ side ] );
  192. }
  193. return DmeTime_t( m_falloff[ side ] ) - time;
  194. }
  195. DmeTime_t CDmeTimeSelection::GetRelativeHold( DmeTime_t time, int side )
  196. {
  197. if ( m_bRelative )
  198. {
  199. return DmeTime_t( m_hold[ side ] );
  200. }
  201. return DmeTime_t( m_hold[ side ] ) - time;
  202. }
  203. void CDmeTimeSelection::ConvertToRelative( DmeTime_t time )
  204. {
  205. m_falloff[ 0 ] -= time.GetTenthsOfMS();
  206. m_falloff[ 1 ] -= time.GetTenthsOfMS();
  207. m_hold[ 0 ] -= time.GetTenthsOfMS();
  208. m_hold[ 1 ] -= time.GetTenthsOfMS();
  209. }
  210. void CDmeTimeSelection::ConvertToAbsolute( DmeTime_t time )
  211. {
  212. m_falloff[ 0 ] += time.GetTenthsOfMS();
  213. m_falloff[ 1 ] += time.GetTenthsOfMS();
  214. m_hold[ 0 ] += time.GetTenthsOfMS();
  215. m_hold[ 1 ] += time.GetTenthsOfMS();
  216. }
  217. void CDmeTimeSelection::SetAbsFalloff( DmeTime_t time, int side, DmeTime_t absfallofftime )
  218. {
  219. DmeTime_t newTime;
  220. if ( m_bRelative )
  221. {
  222. newTime = absfallofftime - time;
  223. }
  224. else
  225. {
  226. newTime = absfallofftime;
  227. }
  228. m_falloff[ side ] = newTime.GetTenthsOfMS();
  229. }
  230. void CDmeTimeSelection::SetAbsHold( DmeTime_t time, int side, DmeTime_t absholdtime )
  231. {
  232. DmeTime_t newTime;
  233. if ( m_bRelative )
  234. {
  235. newTime = absholdtime - time;
  236. }
  237. else
  238. {
  239. newTime = absholdtime;
  240. }
  241. m_hold[ side ] = newTime.GetTenthsOfMS();
  242. }
  243. void CDmeTimeSelection::CopyFrom( const CDmeTimeSelection& src )
  244. {
  245. m_bEnabled = src.m_bEnabled;
  246. m_bRelative = src.m_bRelative;
  247. m_threshold = src.m_threshold;
  248. for ( int i = 0 ; i < 2; ++i )
  249. {
  250. m_falloff[ i ] = src.m_falloff[ i ];
  251. m_hold[ i ] = src.m_hold[ i ];
  252. m_nFalloffInterpolatorType[ i ] = src.m_nFalloffInterpolatorType[ i ];
  253. }
  254. m_nRecordingState = src.m_nRecordingState;
  255. }
  256. void CDmeTimeSelection::GetCurrent( DmeTime_t pTimes[TS_TIME_COUNT] )
  257. {
  258. pTimes[TS_LEFT_FALLOFF].SetTenthsOfMS( m_falloff[ 0 ] );
  259. pTimes[TS_LEFT_HOLD].SetTenthsOfMS( m_hold[ 0 ] );
  260. pTimes[TS_RIGHT_HOLD].SetTenthsOfMS( m_hold[ 1 ] );
  261. pTimes[TS_RIGHT_FALLOFF].SetTenthsOfMS( m_falloff[ 1 ] );
  262. }
  263. void CDmeTimeSelection::SetCurrent( DmeTime_t* pTimes )
  264. {
  265. m_falloff[ 0 ] = pTimes[ TS_LEFT_FALLOFF ].GetTenthsOfMS();
  266. m_hold[ 0 ] = pTimes[ TS_LEFT_HOLD ].GetTenthsOfMS();
  267. m_hold[ 1 ] = pTimes[ TS_RIGHT_HOLD ].GetTenthsOfMS();
  268. m_falloff[ 1 ] = pTimes[ TS_RIGHT_FALLOFF ].GetTenthsOfMS();
  269. }
  270. float CDmeTimeSelection::GetThreshold()
  271. {
  272. return m_threshold;
  273. }
  274. void CDmeTimeSelection::SetRecordingState( RecordingState_t state )
  275. {
  276. m_nRecordingState = ( int )state;
  277. }
  278. RecordingState_t CDmeTimeSelection::GetRecordingState() const
  279. {
  280. return ( RecordingState_t )m_nRecordingState.Get();
  281. }