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.

465 lines
11 KiB

  1. #include "movieobjects/dmetimeselection.h"
  2. #include "interpolatortypes.h"
  3. #include "datamodel/dmelementfactoryhelper.h"
  4. // #include "dme_controls/RecordingState.h"
  5. float ComputeInterpolationFactor( float flFactor, int nInterpolatorType );
  6. float GetAmountForTime( DmeTime_t dmetime, const TimeSelection_t &times, const int nInterpolationTypes[ 2 ] );
  7. IMPLEMENT_ELEMENT_FACTORY( DmeTimeSelection, CDmeTimeSelection );
  8. void CDmeTimeSelection::OnConstruction()
  9. {
  10. m_bEnabled.InitAndSet( this, "enabled", false );
  11. m_bRelative.InitAndSet( this, "relative", false );
  12. DmeTime_t one( 1.0f );
  13. m_falloff[ 0 ].InitAndSet( this, "falloff_left", -one );
  14. m_falloff[ 1 ].InitAndSet( this, "falloff_right", one );
  15. m_hold[ 0 ].Init( this, "hold_left" );
  16. m_hold[ 1 ].Init( this, "hold_right" );
  17. m_nFalloffInterpolatorType[ 0 ].InitAndSet( this, "interpolator_left", INTERPOLATE_LINEAR_INTERP );
  18. m_nFalloffInterpolatorType[ 1 ].InitAndSet( this, "interpolator_right", INTERPOLATE_LINEAR_INTERP );
  19. m_threshold.InitAndSet( this, "threshold", 0.0005f );
  20. m_resampleInterval.InitAndSet( this, "resampleinterval", DmeTime_t( 100 ) ); // 10 ms
  21. m_nRecordingState.InitAndSet( this, "recordingstate", 3 /*AS_PLAYBACK : HACK THIS SHOULD MOVE TO A PUBLIC HEADER*/ );
  22. }
  23. void CDmeTimeSelection::OnDestruction()
  24. {
  25. }
  26. float CDmeTimeSelection::AdjustFactorForInterpolatorType( float factor, int side )
  27. {
  28. return ComputeInterpolationFactor( factor, GetFalloffInterpolatorType( side ) );
  29. }
  30. //-----------------------------------------------------------------------------
  31. // per-type averaging methods
  32. //-----------------------------------------------------------------------------
  33. float CDmeTimeSelection::GetAmountForTime( DmeTime_t t, DmeTime_t curtime )
  34. {
  35. Assert( IsEnabled() );
  36. TimeSelection_t times;
  37. times[ 0 ] = GetAbsFalloff( curtime, 0 );
  38. times[ 1 ] = GetAbsHold( curtime, 0 );
  39. times[ 2 ] = GetAbsHold( curtime, 1 );
  40. times[ 3 ] = GetAbsFalloff( curtime, 1 );
  41. int nInterpolatorTypes[ 2 ] = { m_nFalloffInterpolatorType[0], m_nFalloffInterpolatorType[1] };
  42. return ::GetAmountForTime( t, times, nInterpolatorTypes );
  43. }
  44. void CDmeTimeSelection::GetAlphaForTime( DmeTime_t t, DmeTime_t curtime, byte& alpha )
  45. {
  46. Assert( IsEnabled() );
  47. byte minAlpha = 31;
  48. if ( alpha <= minAlpha )
  49. return;
  50. float f = GetAmountForTime( t, curtime );
  51. alpha = ( byte )( f * ( alpha - minAlpha ) + minAlpha );
  52. alpha = clamp( alpha, minAlpha, 255 );
  53. }
  54. int CDmeTimeSelection::GetFalloffInterpolatorType( int side ) const
  55. {
  56. return m_nFalloffInterpolatorType[ side ];
  57. }
  58. void CDmeTimeSelection::SetFalloffInterpolatorType( int side, int interpolatorType )
  59. {
  60. m_nFalloffInterpolatorType[ side ] = interpolatorType;
  61. }
  62. bool CDmeTimeSelection::IsEnabled() const
  63. {
  64. return m_bEnabled;
  65. }
  66. void CDmeTimeSelection::SetEnabled( bool state )
  67. {
  68. m_bEnabled = state;
  69. }
  70. bool CDmeTimeSelection::IsRelative() const
  71. {
  72. return m_bRelative;
  73. }
  74. void CDmeTimeSelection::SetRelative( DmeTime_t time, bool state )
  75. {
  76. Assert( !IsSuspicious( true ) );
  77. bool changed = m_bRelative != state;
  78. m_bRelative = state;
  79. if ( changed )
  80. {
  81. if ( state )
  82. ConvertToRelative( time );
  83. else
  84. ConvertToAbsolute( time );
  85. }
  86. Assert( !IsSuspicious( true ) );
  87. }
  88. DmeTime_t CDmeTimeSelection::GetAbsFalloff( DmeTime_t time, int side ) const
  89. {
  90. if ( IsInfinite( side ) )
  91. {
  92. return m_falloff[ side ];
  93. }
  94. return m_bRelative ? m_falloff[ side ].Get() + time : m_falloff[ side ];
  95. }
  96. DmeTime_t CDmeTimeSelection::GetAbsHold( DmeTime_t time, int side ) const
  97. {
  98. if ( IsInfinite( side ) )
  99. {
  100. return m_hold[ side ];
  101. }
  102. return m_bRelative ? m_hold[ side ].Get() + time : m_hold[ side ];
  103. }
  104. DmeTime_t CDmeTimeSelection::GetRelativeFalloff( DmeTime_t time, int side ) const
  105. {
  106. if ( IsInfinite( side ) )
  107. {
  108. return m_falloff[ side ];
  109. }
  110. return m_bRelative ? m_falloff[ side ] : m_falloff[ side ].Get() - time;
  111. }
  112. DmeTime_t CDmeTimeSelection::GetRelativeHold( DmeTime_t time, int side ) const
  113. {
  114. if ( IsInfinite( side ) )
  115. {
  116. return m_hold[ side ];
  117. }
  118. return m_bRelative ? m_hold[ side ] : m_hold[ side ].Get() - time;
  119. }
  120. void CDmeTimeSelection::ConvertToRelative( DmeTime_t time )
  121. {
  122. Assert( !IsSuspicious( true ) );
  123. for ( int side = 0; side < 2; ++side )
  124. {
  125. if ( !IsInfinite( side ) )
  126. {
  127. m_falloff[ side ] -= time;
  128. m_hold[ side ] -= time;
  129. }
  130. }
  131. Assert( !IsSuspicious( true ) );
  132. }
  133. void CDmeTimeSelection::ConvertToAbsolute( DmeTime_t time )
  134. {
  135. Assert( !IsSuspicious( true ) );
  136. for ( int side = 0; side < 2; ++side )
  137. {
  138. if ( !IsInfinite( side ) )
  139. {
  140. m_falloff[ side ] += time;
  141. m_hold[ side ] += time;
  142. }
  143. }
  144. Assert( !IsSuspicious( true ) );
  145. }
  146. void CDmeTimeSelection::SetAbsFalloff( DmeTime_t time, int side, DmeTime_t absfallofftime )
  147. {
  148. // If going to infinite edge, don't need to remember the time delta in relative mode, so zero it
  149. if ( absfallofftime == DMETIME_MAXTIME ||
  150. absfallofftime == DMETIME_MINTIME )
  151. {
  152. time = DMETIME_ZERO;
  153. }
  154. m_falloff[ side ] = m_bRelative ? absfallofftime - time : absfallofftime;
  155. Assert( !IsSuspicious() );
  156. }
  157. void CDmeTimeSelection::SetAbsHold( DmeTime_t time, int side, DmeTime_t absholdtime )
  158. {
  159. // If going to infinite edge, don't need to remember the time delta in relative mode, so zero it
  160. if ( absholdtime == DMETIME_MAXTIME ||
  161. absholdtime == DMETIME_MINTIME )
  162. {
  163. time = DMETIME_ZERO;
  164. }
  165. m_hold[ side ] = m_bRelative ? absholdtime - time : absholdtime;
  166. Assert( !IsSuspicious() );
  167. }
  168. void CDmeTimeSelection::CopyFrom( const CDmeTimeSelection& src )
  169. {
  170. m_bEnabled = src.m_bEnabled;
  171. m_bRelative = src.m_bRelative;
  172. m_threshold = src.m_threshold;
  173. for ( int i = 0 ; i < 2; ++i )
  174. {
  175. m_falloff[ i ] = src.m_falloff[ i ];
  176. m_hold[ i ] = src.m_hold[ i ];
  177. m_nFalloffInterpolatorType[ i ] = src.m_nFalloffInterpolatorType[ i ];
  178. }
  179. Assert( !IsSuspicious( true ) );
  180. m_nRecordingState = src.m_nRecordingState;
  181. }
  182. void CDmeTimeSelection::GetAbsTimes( DmeTime_t time, DmeTime_t pTimes[TS_TIME_COUNT] ) const
  183. {
  184. if ( m_bRelative )
  185. {
  186. pTimes[TS_LEFT_FALLOFF ] = GetRelativeFalloff( time, 0 );
  187. pTimes[TS_LEFT_HOLD ] = GetRelativeHold( time, 0 );
  188. pTimes[TS_RIGHT_HOLD ] = GetRelativeHold( time, 1 );
  189. pTimes[TS_RIGHT_FALLOFF] = GetRelativeFalloff( time, 1 );
  190. return;
  191. }
  192. pTimes[TS_LEFT_FALLOFF ] = m_falloff[ 0 ];
  193. pTimes[TS_LEFT_HOLD ] = m_hold [ 0 ];
  194. pTimes[TS_RIGHT_HOLD ] = m_hold [ 1 ];
  195. pTimes[TS_RIGHT_FALLOFF] = m_falloff[ 1 ];
  196. }
  197. void CDmeTimeSelection::GetCurrent( DmeTime_t pTimes[TS_TIME_COUNT] ) const
  198. {
  199. pTimes[TS_LEFT_FALLOFF ] = m_falloff[ 0 ];
  200. pTimes[TS_LEFT_HOLD ] = m_hold [ 0 ];
  201. pTimes[TS_RIGHT_HOLD ] = m_hold [ 1 ];
  202. pTimes[TS_RIGHT_FALLOFF] = m_falloff[ 1 ];
  203. }
  204. void CDmeTimeSelection::SetCurrent( const TimeSelection_t &times )
  205. {
  206. m_falloff[ 0 ] = times[ TS_LEFT_FALLOFF ];
  207. m_hold [ 0 ] = times[ TS_LEFT_HOLD ];
  208. m_hold [ 1 ] = times[ TS_RIGHT_HOLD ];
  209. m_falloff[ 1 ] = times[ TS_RIGHT_FALLOFF ];
  210. Assert( !IsSuspicious( true ) );
  211. }
  212. float CDmeTimeSelection::GetThreshold() const
  213. {
  214. return m_threshold;
  215. }
  216. void CDmeTimeSelection::SetThreshold( float threshold )
  217. {
  218. m_threshold = threshold;
  219. }
  220. DmeTime_t CDmeTimeSelection::GetResampleInterval() const
  221. {
  222. return m_resampleInterval.Get();
  223. }
  224. void CDmeTimeSelection::SetResampleInterval( DmeTime_t resampleInterval )
  225. {
  226. m_resampleInterval.Set( resampleInterval );
  227. }
  228. void CDmeTimeSelection::SetRecordingState( RecordingState_t state )
  229. {
  230. m_nRecordingState = ( int )state;
  231. }
  232. RecordingState_t CDmeTimeSelection::GetRecordingState() const
  233. {
  234. return ( RecordingState_t )m_nRecordingState.Get();
  235. }
  236. void CDmeTimeSelection::GetTimeSelectionTimes( DmeTime_t curtime, DmeTime_t t[ TS_TIME_COUNT ] ) const
  237. {
  238. t[0] = GetAbsFalloff( curtime, 0 );
  239. t[1] = GetAbsHold ( curtime, 0 );
  240. t[2] = GetAbsHold ( curtime, 1 );
  241. t[3] = GetAbsFalloff( curtime, 1 );
  242. }
  243. void CDmeTimeSelection::SetTimeSelectionTimes( DmeTime_t curtime, DmeTime_t t[ TS_TIME_COUNT ] )
  244. {
  245. SetAbsFalloff( curtime, 0, t[0] );
  246. SetAbsHold ( curtime, 0, t[1] );
  247. SetAbsHold ( curtime, 1, t[2] );
  248. SetAbsFalloff( curtime, 1, t[3] );
  249. Assert( !IsSuspicious( true ) );
  250. }
  251. bool CDmeTimeSelection::IsInfinite( int side ) const
  252. {
  253. if ( side == 0 )
  254. {
  255. return m_hold[ side ] == DMETIME_MINTIME;
  256. }
  257. else if ( side == 1 )
  258. {
  259. return m_hold[ side ] == DMETIME_MAXTIME;
  260. }
  261. // Shouldn't get here
  262. Assert( 0 );
  263. return false;
  264. }
  265. void CDmeTimeSelection::GetInfinite( bool bInfinite[ 2 ] ) const
  266. {
  267. bInfinite[ 0 ] = IsInfinite( 0 );
  268. bInfinite[ 1 ] = IsInfinite( 1 );
  269. }
  270. bool CDmeTimeSelection::IsFullyInfinite() const
  271. {
  272. return ( m_hold[ 0 ] == DMETIME_MINTIME ) && ( m_hold[ 1 ] == DMETIME_MAXTIME );
  273. }
  274. bool CDmeTimeSelection::IsEitherInfinite() const
  275. {
  276. return ( m_hold[ 0 ] == DMETIME_MINTIME ) || ( m_hold[ 1 ] == DMETIME_MAXTIME );
  277. }
  278. void CDmeTimeSelection::SetInfinite( int side )
  279. {
  280. if ( side == 0 )
  281. {
  282. m_hold[ side ] = DMETIME_MINTIME;
  283. m_falloff[ side ] = DMETIME_MINTIME;
  284. }
  285. else if ( side == 1 )
  286. {
  287. m_hold[ side ] = DMETIME_MAXTIME;
  288. m_falloff[ side ] = DMETIME_MAXTIME;
  289. }
  290. else
  291. {
  292. Assert( 0 );
  293. }
  294. }
  295. bool CDmeTimeSelection::IsSuspicious( bool bCheckHoldAndFalloff /*= false*/ )
  296. {
  297. DmeTime_t t[ TS_TIME_COUNT ];
  298. GetAbsTimes( DMETIME_ZERO, t );
  299. DmeTime_t bounds[ 2 ] =
  300. {
  301. ( DMETIME_MINTIME + DmeTime_t( 1000.0f ) ),
  302. ( DMETIME_MAXTIME - DmeTime_t( 1000.0f ) )
  303. };
  304. for ( int i = 0; i < 4 ; ++i )
  305. {
  306. if ( t[ i ] == DMETIME_MINTIME ||
  307. t[ i ] == DMETIME_MAXTIME )
  308. continue;
  309. if ( t[ i ] < bounds[ 0 ] ||
  310. t[ i ] > bounds[ 1 ] )
  311. return true;
  312. }
  313. if ( bCheckHoldAndFalloff )
  314. {
  315. // Also check for mismatched edges if infinite
  316. bool bEdgesInfinite[ 4 ] =
  317. {
  318. t[ TS_LEFT_FALLOFF ] == DMETIME_MINTIME,
  319. t[ TS_LEFT_HOLD ] == DMETIME_MINTIME,
  320. t[ TS_RIGHT_HOLD ] == DMETIME_MAXTIME,
  321. t[ TS_RIGHT_FALLOFF ] == DMETIME_MAXTIME,
  322. };
  323. if ( ( bEdgesInfinite[ 0 ] ^ bEdgesInfinite[ 1 ] ) ||
  324. ( bEdgesInfinite[ 2 ] ^ bEdgesInfinite[ 3 ] ) )
  325. {
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. DmeTime_t CDmeTimeSelection::GetAbsTime( DmeTime_t time, int tsType ) const
  332. {
  333. switch ( tsType )
  334. {
  335. default:
  336. break;
  337. case TS_LEFT_FALLOFF:
  338. return GetAbsFalloff( time, 0 );
  339. case TS_LEFT_HOLD:
  340. return GetAbsHold( time, 0 );
  341. case TS_RIGHT_HOLD:
  342. return GetAbsHold( time, 1 );
  343. case TS_RIGHT_FALLOFF:
  344. return GetAbsFalloff( time, 1 );
  345. }
  346. Assert( 0 );
  347. return DMETIME_ZERO;
  348. }
  349. DmeTime_t CDmeTimeSelection::GetRelativeTime( DmeTime_t time, int tsType ) const
  350. {
  351. switch ( tsType )
  352. {
  353. default:
  354. break;
  355. case TS_LEFT_FALLOFF:
  356. return GetRelativeFalloff( time, 0 );
  357. case TS_LEFT_HOLD:
  358. return GetRelativeHold( time, 0 );
  359. case TS_RIGHT_HOLD:
  360. return GetRelativeHold( time, 1 );
  361. case TS_RIGHT_FALLOFF:
  362. return GetRelativeFalloff( time, 1 );
  363. }
  364. Assert( 0 );
  365. return DMETIME_ZERO;
  366. }
  367. void CDmeTimeSelection::SetAbsTime( DmeTime_t time, int tsType, DmeTime_t absTime )
  368. {
  369. switch ( tsType )
  370. {
  371. default:
  372. Assert( 0 );
  373. break;
  374. case TS_LEFT_FALLOFF:
  375. SetAbsFalloff( time, 0, absTime );
  376. break;
  377. case TS_LEFT_HOLD:
  378. SetAbsHold( time, 0, absTime );
  379. break;
  380. case TS_RIGHT_HOLD:
  381. SetAbsHold( time, 1, absTime );
  382. break;
  383. case TS_RIGHT_FALLOFF:
  384. SetAbsFalloff( time, 1, absTime );
  385. break;
  386. }
  387. Assert( !IsSuspicious() );
  388. }