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.

219 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "cbase.h"
  5. #if defined( REPLAY_ENABLED )
  6. #include "replaybrowsermovieplayerpanel.h"
  7. #include "vgui/IVGui.h"
  8. #include "vgui/IInput.h"
  9. #include "engine/IEngineSound.h"
  10. #include "iclientmode.h"
  11. //-----------------------------------------------------------------------------
  12. using namespace vgui;
  13. //-----------------------------------------------------------------------------
  14. CMoviePlayerPanel::CMoviePlayerPanel( Panel *pParent, const char *pName, const char *pMovieFilename )
  15. : CReplayBasePanel( pParent, pName ),
  16. m_flCurFrame( 0.0f ),
  17. m_flLastTime( 0.0f ),
  18. m_nLastMouseXPos( 0 ),
  19. m_bPlaying( false ),
  20. m_bLooping( false ),
  21. m_bFullscreen( false ),
  22. m_bMouseOverScrub( false ),
  23. m_pOldParent( NULL ),
  24. m_pVideoMaterial( NULL )
  25. {
  26. if ( g_pVideo )
  27. {
  28. m_pVideoMaterial = g_pVideo->CreateVideoMaterial( pMovieFilename, pMovieFilename, "GAME" );
  29. if ( m_pVideoMaterial )
  30. {
  31. m_pMaterial = m_pVideoMaterial->GetMaterial();
  32. m_pMaterial->AddRef();
  33. m_nNumFrames = m_pVideoMaterial->GetFrameCount();
  34. }
  35. }
  36. ivgui()->AddTickSignal( GetVPanel(), 0 );
  37. }
  38. CMoviePlayerPanel::~CMoviePlayerPanel()
  39. {
  40. FreeMaterial();
  41. ivgui()->RemoveTickSignal( GetVPanel() );
  42. }
  43. void CMoviePlayerPanel::PerformLayout()
  44. {
  45. BaseClass::PerformLayout();
  46. GetPosRelativeToAncestor( NULL, m_nGlobalPos[0], m_nGlobalPos[1] );
  47. if ( m_bFullscreen )
  48. {
  49. // Cache parent
  50. m_pOldParent = GetParent();
  51. GetBounds( m_aOldBounds[0], m_aOldBounds[1], m_aOldBounds[2], m_aOldBounds[3] );
  52. // Adjust parent for fullscreen mode
  53. SetParent( g_pClientMode->GetViewport() );
  54. // Adjust bounds for fullscreen
  55. SetBounds( 0, 0, ScreenWidth(), ScreenHeight() );
  56. }
  57. else if ( m_pOldParent )
  58. {
  59. // Restore old parent/bounds
  60. SetParent( m_pOldParent );
  61. SetBounds( m_aOldBounds[0], m_aOldBounds[1], m_aOldBounds[2], m_aOldBounds[3] );
  62. }
  63. }
  64. void CMoviePlayerPanel::OnMousePressed( MouseCode code )
  65. {
  66. // ToggleFullscreen();
  67. }
  68. void CMoviePlayerPanel::SetScrubOnMouseOverMode( bool bOn )
  69. {
  70. if ( bOn )
  71. {
  72. m_bPlaying = false;
  73. }
  74. m_bMouseOverScrub = bOn;
  75. }
  76. void CMoviePlayerPanel::Play()
  77. {
  78. m_bPlaying = true;
  79. m_flLastTime = gpGlobals->realtime;
  80. enginesound->NotifyBeginMoviePlayback();
  81. }
  82. void CMoviePlayerPanel::FreeMaterial()
  83. {
  84. if ( m_pVideoMaterial )
  85. {
  86. if ( g_pVideo )
  87. {
  88. g_pVideo->DestroyVideoMaterial( m_pVideoMaterial );
  89. }
  90. m_pVideoMaterial = NULL;
  91. }
  92. if ( m_pMaterial )
  93. {
  94. m_pMaterial->Release();
  95. m_pMaterial = NULL;
  96. }
  97. }
  98. void CMoviePlayerPanel::OnTick()
  99. {
  100. if ( !IsEnabled() )
  101. return;
  102. if ( m_bMouseOverScrub )
  103. {
  104. int nMouseX, nMouseY;
  105. input()->GetCursorPos( nMouseX, nMouseY );
  106. if ( IsWithin( nMouseX, nMouseY ) &&
  107. nMouseX != m_nLastMouseXPos )
  108. {
  109. float flPercent = (float)( nMouseX - m_nGlobalPos[0] ) / GetWide();
  110. m_flCurFrame = flPercent * ( m_nNumFrames - 1 );
  111. m_nLastMouseXPos = nMouseX;
  112. }
  113. }
  114. else if ( m_bPlaying )
  115. {
  116. float flElapsed = gpGlobals->realtime - m_flLastTime;
  117. m_flLastTime = gpGlobals->realtime;
  118. m_flCurFrame += flElapsed * m_pVideoMaterial->GetVideoFrameRate().GetFPS();
  119. // Loop if necessary
  120. if ( m_flCurFrame >= m_nNumFrames )
  121. {
  122. if ( m_bLooping )
  123. {
  124. m_flCurFrame = m_flCurFrame - m_nNumFrames;
  125. }
  126. else
  127. {
  128. // Don't go past last frame
  129. m_flCurFrame = m_nNumFrames - 1;
  130. }
  131. }
  132. }
  133. m_pVideoMaterial->SetFrame( m_flCurFrame );
  134. // Msg( "frame: %f / %i\n", m_flCurFrame, m_nNumFrames );
  135. }
  136. void CMoviePlayerPanel::Paint()
  137. {
  138. if ( m_pVideoMaterial == NULL )
  139. return;
  140. // Get panel position/dimensions
  141. int x,y;
  142. int w,h;
  143. GetPosRelativeToAncestor( NULL, x, y );
  144. GetSize( w,h );
  145. CMatRenderContextPtr pRenderContext( materials );
  146. pRenderContext->Bind( m_pMaterial );
  147. IMesh* pMesh = pRenderContext->GetDynamicMesh( true );
  148. float flMinU = 0.0f, flMinV = 0.0f;
  149. float flMaxU, flMaxV;
  150. m_pVideoMaterial->GetVideoTexCoordRange( &flMaxU, &flMaxV );
  151. CMeshBuilder meshBuilder;
  152. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  153. meshBuilder.Position3f( x, y, 0.0f );
  154. meshBuilder.TexCoord2f( 0, flMinU, flMinV );
  155. meshBuilder.Color4ub( 255, 255, 255, 255 );
  156. meshBuilder.AdvanceVertex();
  157. meshBuilder.Position3f( x + w, y, 0.0f );
  158. meshBuilder.TexCoord2f( 0, flMaxU, flMinV );
  159. meshBuilder.Color4ub( 255, 255, 255, 255 );
  160. meshBuilder.AdvanceVertex();
  161. meshBuilder.Position3f( x + w, y + h, 0.0f );
  162. meshBuilder.TexCoord2f( 0, flMaxU, flMaxV );
  163. meshBuilder.Color4ub( 255, 255, 255, 255 );
  164. meshBuilder.AdvanceVertex();
  165. meshBuilder.Position3f( x, y + h, 0.0f );
  166. meshBuilder.TexCoord2f( 0, flMinU, flMaxV );
  167. meshBuilder.Color4ub( 255, 255, 255, 255 );
  168. meshBuilder.AdvanceVertex();
  169. meshBuilder.End();
  170. pMesh->Draw();
  171. }
  172. void CMoviePlayerPanel::ToggleFullscreen()
  173. {
  174. m_bFullscreen = !m_bFullscreen;
  175. InvalidateLayout( false, false );
  176. }
  177. #endif