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.

494 lines
14 KiB

  1. //========= Copyright � 1996-2009, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Allows movies to be played as a VGUI screen in the world
  4. //
  5. //=====================================================================================//
  6. #include "cbase.h"
  7. #include "EnvMessage.h"
  8. #include "fmtstr.h"
  9. #include "vguiscreen.h"
  10. #include "filesystem.h"
  11. // NOTE: This has to be the last file included!
  12. #include "tier0/memdbgon.h"
  13. class CMovieDisplay : public CBaseEntity
  14. {
  15. public:
  16. DECLARE_CLASS( CMovieDisplay, CBaseEntity );
  17. DECLARE_DATADESC();
  18. DECLARE_SERVERCLASS();
  19. CMovieDisplay()
  20. : m_bForcePrecache( false )
  21. {
  22. }
  23. virtual ~CMovieDisplay();
  24. virtual bool KeyValue( const char *szKeyName, const char *szValue );
  25. virtual int UpdateTransmitState();
  26. virtual void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways );
  27. virtual void Spawn( void );
  28. virtual void Precache( void );
  29. virtual void OnRestore( void );
  30. void ScreenVisible( bool bVisible );
  31. void Disable( void );
  32. void Enable( void );
  33. void InputDisable( inputdata_t &inputdata );
  34. void InputEnable( inputdata_t &inputdata );
  35. void InputSetDisplayText( inputdata_t &inputdata );
  36. void InputTakeOverAsMaster( inputdata_t &inputdata );
  37. void InputSetMovie( inputdata_t &inputdata );
  38. void InputSetUseCustomUVs( inputdata_t &inputdata );
  39. void InputSetUMin( inputdata_t &inputdata );
  40. void InputSetVMin( inputdata_t &inputdata );
  41. void InputSetUMax( inputdata_t &inputdata );
  42. void InputSetVMax( inputdata_t &inputdata );
  43. private:
  44. // Control panel
  45. void GetControlPanelInfo( int nPanelIndex, const char *&pPanelName );
  46. void GetControlPanelClassName( int nPanelIndex, const char *&pPanelName );
  47. void SpawnControlPanels( void );
  48. void RestoreControlPanels( void );
  49. private:
  50. CNetworkVar( bool, m_bEnabled );
  51. CNetworkVar( bool, m_bLooping );
  52. CNetworkVar( bool, m_bStretchToFill );
  53. CNetworkVar( bool, m_bForcedSlave );
  54. bool m_bForcePrecache;
  55. CNetworkVar( bool, m_bUseCustomUVs );
  56. CNetworkVar( float, m_flUMin );
  57. CNetworkVar( float, m_flUMax );
  58. CNetworkVar( float, m_flVMin );
  59. CNetworkVar( float, m_flVMax );
  60. CNetworkString( m_szDisplayText, 128 );
  61. // Filename of the movie to play
  62. CNetworkString( m_szMovieFilename, 128 );
  63. string_t m_strMovieFilename;
  64. // "Group" name. Screens of the same group name will play the same movie at the same time
  65. // Effectively this lets multiple screens tune to the same "channel" in the world
  66. CNetworkString( m_szGroupName, 128 );
  67. string_t m_strGroupName;
  68. int m_iScreenWidth;
  69. int m_iScreenHeight;
  70. bool m_bDoFullTransmit;
  71. CHandle<CVGuiScreen> m_hScreen;
  72. };
  73. LINK_ENTITY_TO_CLASS( vgui_movie_display, CMovieDisplay );
  74. //-----------------------------------------------------------------------------
  75. // Save/load
  76. //-----------------------------------------------------------------------------
  77. BEGIN_DATADESC( CMovieDisplay )
  78. DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
  79. DEFINE_AUTO_ARRAY_KEYFIELD( m_szDisplayText, FIELD_CHARACTER, "displaytext" ),
  80. DEFINE_AUTO_ARRAY( m_szMovieFilename, FIELD_CHARACTER ),
  81. DEFINE_KEYFIELD( m_strMovieFilename, FIELD_STRING, "moviefilename" ),
  82. DEFINE_AUTO_ARRAY( m_szGroupName, FIELD_CHARACTER ),
  83. DEFINE_KEYFIELD( m_strGroupName, FIELD_STRING, "groupname" ),
  84. DEFINE_KEYFIELD( m_iScreenWidth, FIELD_INTEGER, "width" ),
  85. DEFINE_KEYFIELD( m_iScreenHeight, FIELD_INTEGER, "height" ),
  86. DEFINE_KEYFIELD( m_bLooping, FIELD_BOOLEAN, "looping" ),
  87. DEFINE_KEYFIELD( m_bStretchToFill, FIELD_BOOLEAN, "stretch" ),
  88. DEFINE_KEYFIELD( m_bForcedSlave, FIELD_BOOLEAN, "forcedslave" ),
  89. DEFINE_KEYFIELD( m_bForcePrecache, FIELD_BOOLEAN, "forceprecache" ),
  90. DEFINE_FIELD( m_bUseCustomUVs, FIELD_BOOLEAN ),
  91. DEFINE_FIELD( m_flUMin, FIELD_FLOAT ),
  92. DEFINE_FIELD( m_flUMax, FIELD_FLOAT ),
  93. DEFINE_FIELD( m_flVMin, FIELD_FLOAT ),
  94. DEFINE_FIELD( m_flVMax, FIELD_FLOAT ),
  95. DEFINE_FIELD( m_bDoFullTransmit, FIELD_BOOLEAN ),
  96. DEFINE_FIELD( m_hScreen, FIELD_EHANDLE ),
  97. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  98. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  99. DEFINE_INPUTFUNC( FIELD_STRING, "SetDisplayText", InputSetDisplayText ),
  100. DEFINE_INPUTFUNC( FIELD_STRING, "SetMovie", InputSetMovie ),
  101. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "SetUseCustomUVs", InputSetUseCustomUVs ),
  102. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetUMin", InputSetUMin ),
  103. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetUMax", InputSetUMax ),
  104. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetVMin", InputSetVMin ),
  105. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetVMax", InputSetVMax ),
  106. DEFINE_INPUTFUNC( FIELD_VOID, "TakeOverAsMaster", InputTakeOverAsMaster ),
  107. END_DATADESC()
  108. IMPLEMENT_SERVERCLASS_ST( CMovieDisplay, DT_MovieDisplay )
  109. SendPropBool( SENDINFO( m_bEnabled ) ),
  110. SendPropBool( SENDINFO( m_bLooping ) ),
  111. SendPropString( SENDINFO( m_szMovieFilename ) ),
  112. SendPropString( SENDINFO( m_szGroupName ) ),
  113. SendPropBool( SENDINFO( m_bStretchToFill ) ),
  114. SendPropBool( SENDINFO( m_bForcedSlave ) ),
  115. SendPropBool( SENDINFO( m_bUseCustomUVs ) ),
  116. SendPropFloat( SENDINFO( m_flUMin ) ),
  117. SendPropFloat( SENDINFO( m_flUMax ) ),
  118. SendPropFloat( SENDINFO( m_flVMin ) ),
  119. SendPropFloat( SENDINFO( m_flVMax ) ),
  120. END_SEND_TABLE()
  121. CMovieDisplay::~CMovieDisplay()
  122. {
  123. DestroyVGuiScreen( m_hScreen.Get() );
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Read in Hammer data
  127. //-----------------------------------------------------------------------------
  128. bool CMovieDisplay::KeyValue( const char *szKeyName, const char *szValue )
  129. {
  130. // NOTE: Have to do these separate because they set two values instead of one
  131. if( FStrEq( szKeyName, "angles" ) )
  132. {
  133. Assert( GetMoveParent() == NULL );
  134. QAngle angles;
  135. UTIL_StringToVector( angles.Base(), szValue );
  136. // Because the vgui screen basis is strange (z is front, y is up, x is right)
  137. // we need to rotate the typical basis before applying it
  138. VMatrix mat, rotation, tmp;
  139. MatrixFromAngles( angles, mat );
  140. MatrixBuildRotationAboutAxis( rotation, Vector( 0, 1, 0 ), 90 );
  141. MatrixMultiply( mat, rotation, tmp );
  142. MatrixBuildRotateZ( rotation, 90 );
  143. MatrixMultiply( tmp, rotation, mat );
  144. MatrixToAngles( mat, angles );
  145. SetAbsAngles( angles );
  146. return true;
  147. }
  148. return BaseClass::KeyValue( szKeyName, szValue );
  149. }
  150. //-----------------------------------------------------------------------------
  151. //
  152. //-----------------------------------------------------------------------------
  153. int CMovieDisplay::UpdateTransmitState()
  154. {
  155. if ( m_bDoFullTransmit )
  156. {
  157. m_bDoFullTransmit = false;
  158. return SetTransmitState( FL_EDICT_ALWAYS );
  159. }
  160. return SetTransmitState( FL_EDICT_FULLCHECK );
  161. }
  162. //-----------------------------------------------------------------------------
  163. //
  164. //-----------------------------------------------------------------------------
  165. void CMovieDisplay::SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways )
  166. {
  167. // Are we already marked for transmission?
  168. if ( pInfo->m_pTransmitEdict->Get( entindex() ) )
  169. return;
  170. BaseClass::SetTransmit( pInfo, bAlways );
  171. // Force our screen to be sent too.
  172. m_hScreen->SetTransmit( pInfo, bAlways );
  173. }
  174. //-----------------------------------------------------------------------------
  175. //
  176. //-----------------------------------------------------------------------------
  177. void CMovieDisplay::Spawn( void )
  178. {
  179. // Move the strings into a networkable form
  180. Q_strcpy( m_szMovieFilename.GetForModify(), m_strMovieFilename.ToCStr() );
  181. Q_strcpy( m_szGroupName.GetForModify(), m_strGroupName.ToCStr() );
  182. Precache();
  183. BaseClass::Spawn();
  184. m_bEnabled = false;
  185. SpawnControlPanels();
  186. ScreenVisible( m_bEnabled );
  187. m_bDoFullTransmit = true;
  188. m_bUseCustomUVs = false;
  189. m_flUMin = 0;
  190. m_flUMax = 1;
  191. m_flVMin = 0;
  192. m_flVMax = 1;
  193. }
  194. //-----------------------------------------------------------------------------
  195. //
  196. //-----------------------------------------------------------------------------
  197. void CMovieDisplay::Precache( void )
  198. {
  199. BaseClass::Precache();
  200. PrecacheVGuiScreen( "movie_display_screen" );
  201. if ( m_bForcePrecache )
  202. {
  203. DevMsg( "Precaching vgui_movie_display %s with movie %s\n", m_iName->ToCStr(), m_szMovieFilename.Get() );
  204. PrecacheMovie( m_szMovieFilename );
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. //
  209. //-----------------------------------------------------------------------------
  210. void CMovieDisplay::OnRestore( void )
  211. {
  212. BaseClass::OnRestore();
  213. m_bDoFullTransmit = true;
  214. RestoreControlPanels();
  215. ScreenVisible( m_bEnabled );
  216. }
  217. //-----------------------------------------------------------------------------
  218. //
  219. //-----------------------------------------------------------------------------
  220. void CMovieDisplay::ScreenVisible( bool bVisible )
  221. {
  222. // Set its active state
  223. m_hScreen->SetActive( bVisible );
  224. if ( bVisible )
  225. {
  226. m_hScreen->RemoveEffects( EF_NODRAW );
  227. }
  228. else
  229. {
  230. m_hScreen->AddEffects( EF_NODRAW );
  231. }
  232. }
  233. //-----------------------------------------------------------------------------
  234. //
  235. //-----------------------------------------------------------------------------
  236. void CMovieDisplay::Disable( void )
  237. {
  238. if ( !m_bEnabled )
  239. return;
  240. m_bEnabled = false;
  241. ScreenVisible( false );
  242. }
  243. //-----------------------------------------------------------------------------
  244. //
  245. //-----------------------------------------------------------------------------
  246. void CMovieDisplay::Enable( void )
  247. {
  248. if ( m_bEnabled )
  249. return;
  250. m_bEnabled = true;
  251. ScreenVisible( true );
  252. }
  253. //-----------------------------------------------------------------------------
  254. //
  255. //-----------------------------------------------------------------------------
  256. void CMovieDisplay::InputDisable( inputdata_t &inputdata )
  257. {
  258. Disable();
  259. }
  260. //-----------------------------------------------------------------------------
  261. //
  262. //-----------------------------------------------------------------------------
  263. void CMovieDisplay::InputEnable( inputdata_t &inputdata )
  264. {
  265. Enable();
  266. }
  267. //-----------------------------------------------------------------------------
  268. //
  269. //-----------------------------------------------------------------------------
  270. void CMovieDisplay::InputSetUseCustomUVs( inputdata_t &inputdata )
  271. {
  272. m_bUseCustomUVs = inputdata.value.Bool();
  273. }
  274. //-----------------------------------------------------------------------------
  275. //
  276. //-----------------------------------------------------------------------------
  277. void CMovieDisplay::InputSetUMin( inputdata_t &inputdata )
  278. {
  279. m_flUMin = inputdata.value.Float();
  280. }
  281. //-----------------------------------------------------------------------------
  282. //
  283. //-----------------------------------------------------------------------------
  284. void CMovieDisplay::InputSetUMax( inputdata_t &inputdata )
  285. {
  286. m_flUMax = inputdata.value.Float();
  287. }
  288. //-----------------------------------------------------------------------------
  289. //
  290. //-----------------------------------------------------------------------------
  291. void CMovieDisplay::InputSetVMin( inputdata_t &inputdata )
  292. {
  293. m_flVMin = inputdata.value.Float();
  294. }
  295. //-----------------------------------------------------------------------------
  296. //
  297. //-----------------------------------------------------------------------------
  298. void CMovieDisplay::InputSetVMax( inputdata_t &inputdata )
  299. {
  300. m_flVMax = inputdata.value.Float();
  301. }
  302. //-----------------------------------------------------------------------------
  303. //
  304. //-----------------------------------------------------------------------------
  305. void CMovieDisplay::InputTakeOverAsMaster( inputdata_t &inputdata )
  306. {
  307. Enable();
  308. EntityMessageBegin( this );
  309. WRITE_BYTE( 0 );
  310. MessageEnd();
  311. }
  312. //-----------------------------------------------------------------------------
  313. //
  314. //-----------------------------------------------------------------------------
  315. void CMovieDisplay::InputSetDisplayText( inputdata_t &inputdata )
  316. {
  317. Q_strcpy( m_szDisplayText.GetForModify(), inputdata.value.String() );
  318. }
  319. //-----------------------------------------------------------------------------
  320. //
  321. //-----------------------------------------------------------------------------
  322. void CMovieDisplay::InputSetMovie( inputdata_t &inputdata )
  323. {
  324. Q_strncpy( m_szMovieFilename.GetForModify(), inputdata.value.String(), 128 );
  325. }
  326. //-----------------------------------------------------------------------------
  327. //
  328. //-----------------------------------------------------------------------------
  329. void CMovieDisplay::GetControlPanelInfo( int nPanelIndex, const char *&pPanelName )
  330. {
  331. pPanelName = "movie_display_screen";
  332. }
  333. //-----------------------------------------------------------------------------
  334. //
  335. //-----------------------------------------------------------------------------
  336. void CMovieDisplay::GetControlPanelClassName( int nPanelIndex, const char *&pPanelName )
  337. {
  338. pPanelName = "vgui_screen";
  339. }
  340. //-----------------------------------------------------------------------------
  341. // This is called by the base object when it's time to spawn the control panels
  342. //-----------------------------------------------------------------------------
  343. void CMovieDisplay::SpawnControlPanels()
  344. {
  345. int nPanel;
  346. for ( nPanel = 0; true; ++nPanel )
  347. {
  348. const char *pScreenName;
  349. GetControlPanelInfo( nPanel, pScreenName );
  350. if (!pScreenName)
  351. continue;
  352. const char *pScreenClassname;
  353. GetControlPanelClassName( nPanel, pScreenClassname );
  354. if ( !pScreenClassname )
  355. continue;
  356. float flWidth = m_iScreenWidth;
  357. float flHeight = m_iScreenHeight;
  358. CVGuiScreen *pScreen = CreateVGuiScreen( pScreenClassname, pScreenName, this, this, 0 );
  359. pScreen->ChangeTeam( GetTeamNumber() );
  360. pScreen->SetActualSize( flWidth, flHeight );
  361. pScreen->SetActive( true );
  362. pScreen->MakeVisibleOnlyToTeammates( false );
  363. pScreen->SetTransparency( true );
  364. m_hScreen = pScreen;
  365. return;
  366. }
  367. }
  368. //-----------------------------------------------------------------------------
  369. //
  370. //-----------------------------------------------------------------------------
  371. void CMovieDisplay::RestoreControlPanels( void )
  372. {
  373. int nPanel;
  374. for ( nPanel = 0; true; ++nPanel )
  375. {
  376. const char *pScreenName;
  377. GetControlPanelInfo( nPanel, pScreenName );
  378. if (!pScreenName)
  379. continue;
  380. const char *pScreenClassname;
  381. GetControlPanelClassName( nPanel, pScreenClassname );
  382. if ( !pScreenClassname )
  383. continue;
  384. CVGuiScreen *pScreen = (CVGuiScreen *)gEntList.FindEntityByClassname( NULL, pScreenClassname );
  385. while ( ( pScreen && pScreen->GetOwnerEntity() != this ) || Q_strcmp( pScreen->GetPanelName(), pScreenName ) != 0 )
  386. {
  387. pScreen = (CVGuiScreen *)gEntList.FindEntityByClassname( pScreen, pScreenClassname );
  388. }
  389. if ( pScreen )
  390. {
  391. m_hScreen = pScreen;
  392. m_hScreen->SetActive( true );
  393. }
  394. return;
  395. }
  396. }