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
14 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. // NOTE: This must be the last file included!!!
  9. #include "tier0/memdbgon.h"
  10. using namespace SF::GFx;
  11. // Some utility functions to map SFUI enums to SF enums
  12. Movie::ScaleModeType ScaleformUIImpl::ScaleModeType_SFUI_to_SDK( IScaleformUI::_ScaleModeType scaleModeType )
  13. {
  14. switch ( scaleModeType )
  15. {
  16. case IScaleformUI::SM_NoScale: return Movie::SM_NoScale;
  17. case IScaleformUI::SM_ShowAll: return Movie::SM_ShowAll;
  18. case IScaleformUI::SM_ExactFit: return Movie::SM_ExactFit;
  19. case IScaleformUI::SM_NoBorder: return Movie::SM_NoBorder;
  20. default:
  21. AssertMsg( 0, "Unknown ScaleModeType\n");
  22. return Movie::SM_NoScale;
  23. }
  24. };
  25. IScaleformUI::_ScaleModeType ScaleformUIImpl::ScaleModeType_SDK_to_SFUI( Movie::ScaleModeType scaleModeType )
  26. {
  27. switch ( scaleModeType )
  28. {
  29. case Movie::SM_NoScale: return IScaleformUI::SM_NoScale;
  30. case Movie::SM_ShowAll: return IScaleformUI::SM_ShowAll;
  31. case Movie::SM_ExactFit: return IScaleformUI::SM_ExactFit;
  32. case Movie::SM_NoBorder: return IScaleformUI::SM_NoBorder;
  33. default:
  34. AssertMsg( 0, "Unknown ScaleModeType\n");
  35. return IScaleformUI::SM_NoScale;
  36. }
  37. };
  38. Value::ValueType ScaleformUIImpl::ValueType_SFUI_to_SDK( IScaleformUI::_ValueType valueType )
  39. {
  40. switch ( valueType )
  41. {
  42. case IScaleformUI::VT_Undefined : return Value::VT_Undefined ;
  43. case IScaleformUI::VT_Null : return Value::VT_Null ;
  44. case IScaleformUI::VT_Boolean : return Value::VT_Boolean ;
  45. case IScaleformUI::VT_Int : return Value::VT_Int ;
  46. case IScaleformUI::VT_UInt : return Value::VT_UInt ;
  47. case IScaleformUI::VT_Number : return Value::VT_Number ;
  48. case IScaleformUI::VT_String : return Value::VT_String ;
  49. case IScaleformUI::VT_StringW : return Value::VT_StringW ;
  50. case IScaleformUI::VT_Object : return Value::VT_Object ;
  51. case IScaleformUI::VT_Array : return Value::VT_Array ;
  52. case IScaleformUI::VT_DisplayObject : return Value::VT_DisplayObject ;
  53. case IScaleformUI::VT_Closure : return Value::VT_Closure ;
  54. case IScaleformUI::VT_ConvertBoolean : return Value::VT_ConvertBoolean ;
  55. case IScaleformUI::VT_ConvertInt : return Value::VT_ConvertInt ;
  56. case IScaleformUI::VT_ConvertUInt : return Value::VT_ConvertUInt ;
  57. case IScaleformUI::VT_ConvertNumber : return Value::VT_ConvertNumber ;
  58. case IScaleformUI::VT_ConvertString : return Value::VT_ConvertString ;
  59. case IScaleformUI::VT_ConvertStringW : return Value::VT_ConvertStringW ;
  60. default:
  61. AssertMsg( 0, "Unknown ValueType\n");
  62. return Value::VT_Undefined;
  63. }
  64. }
  65. IScaleformUI::_ValueType ScaleformUIImpl::ValueType_SDK_to_SFUI( Value::ValueType valueType )
  66. {
  67. switch ( valueType )
  68. {
  69. case Value::VT_Undefined : return IScaleformUI::VT_Undefined ;
  70. case Value::VT_Null : return IScaleformUI::VT_Null ;
  71. case Value::VT_Boolean : return IScaleformUI::VT_Boolean ;
  72. case Value::VT_Int : return IScaleformUI::VT_Int ;
  73. case Value::VT_UInt : return IScaleformUI::VT_UInt ;
  74. case Value::VT_Number : return IScaleformUI::VT_Number ;
  75. case Value::VT_String : return IScaleformUI::VT_String ;
  76. case Value::VT_StringW : return IScaleformUI::VT_StringW ;
  77. case Value::VT_Object : return IScaleformUI::VT_Object ;
  78. case Value::VT_Array : return IScaleformUI::VT_Array ;
  79. case Value::VT_DisplayObject : return IScaleformUI::VT_DisplayObject ;
  80. case Value::VT_Closure : return IScaleformUI::VT_Closure ;
  81. case Value::VT_ConvertBoolean : return IScaleformUI::VT_ConvertBoolean ;
  82. case Value::VT_ConvertInt : return IScaleformUI::VT_ConvertInt ;
  83. case Value::VT_ConvertUInt : return IScaleformUI::VT_ConvertUInt ;
  84. case Value::VT_ConvertNumber : return IScaleformUI::VT_ConvertNumber ;
  85. case Value::VT_ConvertString : return IScaleformUI::VT_ConvertString ;
  86. case Value::VT_ConvertStringW : return IScaleformUI::VT_ConvertStringW ;
  87. default:
  88. AssertMsg( 0, "Unknown ValueType\n");
  89. return IScaleformUI::VT_Undefined;
  90. }
  91. }
  92. Movie::AlignType ScaleformUIImpl::AlignType_SFUI_to_SDK( IScaleformUI::_AlignType alignType )
  93. {
  94. switch ( alignType )
  95. {
  96. case IScaleformUI::Align_Center: return Movie::Align_Center;
  97. case IScaleformUI::Align_TopCenter: return Movie::Align_TopCenter;
  98. case IScaleformUI::Align_BottomCenter: return Movie::Align_BottomCenter;
  99. case IScaleformUI::Align_CenterLeft: return Movie::Align_CenterLeft;
  100. case IScaleformUI::Align_CenterRight: return Movie::Align_CenterRight;
  101. case IScaleformUI::Align_TopLeft: return Movie::Align_TopLeft;
  102. case IScaleformUI::Align_TopRight: return Movie::Align_TopRight;
  103. case IScaleformUI::Align_BottomLeft: return Movie::Align_BottomLeft;
  104. case IScaleformUI::Align_BottomRight: return Movie::Align_BottomRight;
  105. default:
  106. AssertMsg( 0, "Unknown AlignType\n");
  107. return Movie::Align_Center;
  108. }
  109. }
  110. IScaleformUI::_AlignType ScaleformUIImpl::AlignType_SDK_to_SFUI( Movie::AlignType alignType )
  111. {
  112. switch ( alignType )
  113. {
  114. case Movie::Align_Center: return IScaleformUI::Align_Center;
  115. case Movie::Align_TopCenter: return IScaleformUI::Align_TopCenter;
  116. case Movie::Align_BottomCenter: return IScaleformUI::Align_BottomCenter;
  117. case Movie::Align_CenterLeft: return IScaleformUI::Align_CenterLeft;
  118. case Movie::Align_CenterRight: return IScaleformUI::Align_CenterRight;
  119. case Movie::Align_TopLeft: return IScaleformUI::Align_TopLeft;
  120. case Movie::Align_TopRight: return IScaleformUI::Align_TopRight;
  121. case Movie::Align_BottomLeft: return IScaleformUI::Align_BottomLeft;
  122. case Movie::Align_BottomRight: return IScaleformUI::Align_BottomRight;
  123. default:
  124. AssertMsg( 0, "Unknown AlignType\n");
  125. return IScaleformUI::Align_Center;
  126. }
  127. }
  128. Movie::HitTestType ScaleformUIImpl::HitTestType_SFUI_to_SDK( IScaleformUI::_HitTestType hitTestType )
  129. {
  130. switch ( hitTestType )
  131. {
  132. case IScaleformUI::HitTest_Bounds: return Movie::HitTest_Bounds;
  133. case IScaleformUI::HitTest_Shapes: return Movie::HitTest_Shapes;
  134. case IScaleformUI::HitTest_ButtonEvents: return Movie::HitTest_ButtonEvents;
  135. case IScaleformUI::HitTest_ShapesNoInvisible: return Movie::HitTest_ShapesNoInvisible;
  136. default:
  137. AssertMsg( 0, "Unknown HitTestType\n");
  138. return Movie::HitTest_Bounds;
  139. }
  140. }
  141. IScaleformUI::_HitTestType ScaleformUIImpl::HitTestType_SDK_to_SFUI( Movie::HitTestType hitTestType )
  142. {
  143. switch ( hitTestType )
  144. {
  145. case Movie::HitTest_Bounds: return IScaleformUI::HitTest_Bounds;
  146. case Movie::HitTest_Shapes: return IScaleformUI::HitTest_Shapes;
  147. case Movie::HitTest_ButtonEvents: return IScaleformUI::HitTest_ButtonEvents;
  148. case Movie::HitTest_ShapesNoInvisible: return IScaleformUI::HitTest_ShapesNoInvisible;
  149. default:
  150. AssertMsg( 0, "Unknown HitTestType\n");
  151. return IScaleformUI::HitTest_Bounds;
  152. }
  153. }
  154. class ScaleformMovieDefHolder: public ASUserData
  155. {
  156. public:
  157. // this is a weak pointer
  158. MovieDef* m_pMovieDef;
  159. inline ScaleformMovieDefHolder( MovieDef *pdef ) :
  160. ASUserData(), m_pMovieDef( pdef )
  161. {
  162. }
  163. virtual void OnDestroy( Movie* pmovie, void* pobject )
  164. {
  165. Release();
  166. }
  167. };
  168. void ScaleformUIImpl::InitMovieImpl( void )
  169. {
  170. }
  171. void ScaleformUIImpl::ShutdownMovieImpl( void )
  172. {
  173. #if defined( _DEBUG )
  174. if ( m_MovieViews.Count() )
  175. {
  176. LogPrintf( "Scaleform: Some movie views were not released\n" );
  177. for ( int i = 0; i < m_MovieViews.Count(); i++ )
  178. {
  179. ASUserData* pud = ( ASUserData* )m_MovieViews[i]->GetUserData();
  180. ScaleformMovieDefHolder* pDef = ( ScaleformMovieDefHolder* ) pud;
  181. if ( pDef )
  182. {
  183. LogPrintf( " %s\n", pDef->m_pMovieDef->GetFileURL() );
  184. }
  185. else
  186. {
  187. LogPrintf( " unknown slot movie\n" );
  188. }
  189. if ( pud )
  190. {
  191. pud->OnDestroy( m_MovieViews[i],NULL );
  192. }
  193. m_MovieViews[i]->SetUserData( NULL );
  194. }
  195. }
  196. if ( m_MovieDefNameCache.Count() )
  197. {
  198. LogPrintf( "Scaleform: Some movie defs were not released\n" );
  199. for ( int i = 0; i < m_MovieDefNameCache.Count(); i++ )
  200. {
  201. LogPrintf( " %s\n", m_MovieDefNameCache[i] );
  202. }
  203. }
  204. #endif
  205. }
  206. SFMOVIEDEF ScaleformUIImpl::CreateMovieDef( const char* pfilename, unsigned int loadConstants, size_t memoryArena )
  207. {
  208. // first see if we've already got the guy loaded
  209. MovieDef* presult = NULL;
  210. for ( int i = 0; i < m_MovieDefNameCache.Count(); i++ )
  211. {
  212. if ( !V_stricmp( m_MovieDefNameCache[i], pfilename ) )
  213. {
  214. presult = m_MovieDefCache[i];
  215. presult->AddRef();
  216. break;
  217. }
  218. }
  219. if ( presult == NULL )
  220. {
  221. presult = m_pLoader->CreateMovie( pfilename, loadConstants, memoryArena );
  222. m_MovieDefCache.AddToTail( presult );
  223. char* newString = new char[V_strlen( pfilename )+1];
  224. V_strcpy( newString, pfilename );
  225. m_MovieDefNameCache.AddToTail( newString );
  226. }
  227. return ToSFMOVIEDEF( presult );
  228. }
  229. void ScaleformUIImpl::ReleaseMovieDef( SFMOVIEDEF movieDef )
  230. {
  231. MovieDef* pdef = FromSFMOVIEDEF( movieDef );
  232. int refcount = pdef->GetRefCount();
  233. pdef->Release();
  234. // we actually removed this
  235. if ( refcount == 1 )
  236. {
  237. for ( int i = 0; i < m_MovieDefCache.Count(); i++ )
  238. {
  239. if ( m_MovieDefCache[i] == pdef )
  240. {
  241. delete[] m_MovieDefNameCache[i];
  242. m_MovieDefCache.FastRemove( i );
  243. m_MovieDefNameCache.FastRemove( i );
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. SFMOVIE ScaleformUIImpl::MovieDef_CreateInstance( SFMOVIEDEF movieDef, bool initFirstFrame, size_t memoryArena )
  250. {
  251. MovieDef* pDef = FromSFMOVIEDEF( movieDef );
  252. Movie* pView = pDef->CreateInstance( initFirstFrame, memoryArena, 0, m_pThreadCommandQueue );
  253. if ( pView )
  254. {
  255. #if defined( _DEBUG )
  256. // only need this so we can print the name of the movie if we don't release it
  257. ScaleformMovieDefHolder* def = new ScaleformMovieDefHolder( pDef );
  258. pView->SetUserData( def );
  259. #endif // _DEBUG
  260. m_MovieViews.AddToTail( pView );
  261. }
  262. return ToSFMOVIE( pView );
  263. }
  264. void ScaleformUIImpl::ReleaseMovieView( SFMOVIE movieView )
  265. {
  266. if ( movieView )
  267. {
  268. m_MovieViews.FindAndFastRemove( FromSFMOVIE( movieView ) );
  269. #if defined( _DEBUG )
  270. ScaleformMovieDefHolder* def = ( ScaleformMovieDefHolder* )FromSFMOVIE( movieView )->GetUserData();
  271. if ( def )
  272. {
  273. def->OnDestroy( FromSFMOVIE( movieView ), NULL );
  274. }
  275. FromSFMOVIE( movieView )->SetUserData( NULL );
  276. #endif
  277. // This needs to execute on the render thread via a command queue. It only works here
  278. // because typically when we free movies either QMS is off, or the qms is not very active
  279. // because we are loading/unloading maps
  280. Movie *pMovie = FromSFMOVIE( movieView );
  281. pMovie->ShutdownRendering( false );
  282. while ( !pMovie->IsShutdownRenderingComplete() )
  283. {
  284. if ( m_pRenderer2D->GetContextNotify() )
  285. {
  286. MovieDisplayHandle hMovieDisplay = pMovie->GetDisplayHandle();
  287. hMovieDisplay.NextCapture( m_pRenderer2D->GetContextNotify() );
  288. m_pRenderer2D->GetContextNotify()->EndFrameContextNotify();
  289. }
  290. }
  291. pMovie->Release();
  292. }
  293. }
  294. void ScaleformUIImpl::MovieView_Advance( SFMOVIE movieView, float time, unsigned int frameCatchUpCount )
  295. {
  296. FromSFMOVIE( movieView )->Advance( time, frameCatchUpCount );
  297. }
  298. void ScaleformUIImpl::MovieView_SetBackgroundAlpha( SFMOVIE movieView, float alpha )
  299. {
  300. FromSFMOVIE( movieView )->SetBackgroundAlpha( alpha );
  301. }
  302. void ScaleformUIImpl::MovieView_SetViewport( SFMOVIE movieView, int bufw, int bufh, int left, int top, int w, int h, unsigned int flags )
  303. {
  304. FromSFMOVIE( movieView )->SetViewport( bufw, bufh, left, top, w, h, flags );
  305. }
  306. void ScaleformUIImpl::MovieView_Display( SFMOVIE movieView )
  307. {
  308. MovieDisplayHandle hMovieDisplay = FromSFMOVIE( movieView )->GetDisplayHandle();
  309. if ( hMovieDisplay.NextCapture( m_pRenderer2D->GetContextNotify() ) )
  310. {
  311. m_pRenderer2D->Display( hMovieDisplay );
  312. }
  313. }
  314. /***
  315. * view alignment and scaling
  316. */
  317. void ScaleformUIImpl::MovieView_SetViewScaleMode( SFMOVIE movieView, IScaleformUI::_ScaleModeType type )
  318. {
  319. FromSFMOVIE( movieView )->SetViewScaleMode( ScaleModeType_SFUI_to_SDK( type ) );
  320. }
  321. IScaleformUI::_ScaleModeType ScaleformUIImpl::MovieView_GetViewScaleMode( SFMOVIE movieView )
  322. {
  323. return ScaleModeType_SDK_to_SFUI( FromSFMOVIE( movieView )->GetViewScaleMode() );
  324. }
  325. void ScaleformUIImpl::MovieView_SetViewAlignment( SFMOVIE movieView, IScaleformUI::_AlignType type )
  326. {
  327. FromSFMOVIE( movieView )->SetViewAlignment( AlignType_SFUI_to_SDK( type ) );
  328. }
  329. IScaleformUI::_AlignType ScaleformUIImpl::MovieView_GetViewAlignment( SFMOVIE movieView )
  330. {
  331. return AlignType_SDK_to_SFUI( FromSFMOVIE( movieView )->GetViewAlignment() );
  332. }
  333. /***************************
  334. * values
  335. */
  336. SFVALUE ScaleformUIImpl::MovieView_CreateString( SFMOVIE movieView, const char *str )
  337. {
  338. Value* pResult = FromSFVALUE( CreateGFxValue() );
  339. FromSFMOVIE( movieView )->CreateString( pResult, str );
  340. return ToSFVALUE( pResult );
  341. }
  342. SFVALUE ScaleformUIImpl::MovieView_CreateStringW( SFMOVIE movieView, const wchar_t *str )
  343. {
  344. Value* pResult = FromSFVALUE( CreateGFxValue() );
  345. FromSFMOVIE( movieView )->CreateStringW( pResult, str );
  346. return ToSFVALUE( pResult );
  347. }
  348. SFVALUE ScaleformUIImpl::MovieView_CreateObject( SFMOVIE movieView, const char* className, SFVALUEARRAY args, int numArgs )
  349. {
  350. Assert(numArgs == args.Count());
  351. if ( numArgs != args.Count() )
  352. {
  353. Warning("MovieView_CreateObject(%s) called with numArgs different than SFVALUEARRAY size\n", className);
  354. }
  355. Value* pResult = FromSFVALUE( CreateGFxValue() );
  356. FromSFMOVIE( movieView )->CreateObject( pResult, className, FromSFVALUE( args.GetValues() ), args.Count() );
  357. return ToSFVALUE( pResult );
  358. }
  359. SFVALUE ScaleformUIImpl::MovieView_CreateArray( SFMOVIE movieView, int size )
  360. {
  361. Value* pResult = FromSFVALUE( CreateGFxValue() );
  362. FromSFMOVIE( movieView )->CreateArray( pResult );
  363. if ( size != -1 )
  364. pResult->SetArraySize( size );
  365. return ToSFVALUE( pResult );
  366. }
  367. SFVALUE ScaleformUIImpl::MovieView_GetVariable( SFMOVIE movieView, const char* variablePath )
  368. {
  369. Value var;
  370. Value* pResult = NULL;
  371. bool worked = FromSFMOVIE( movieView )->GetVariable( &var, variablePath );
  372. if ( worked )
  373. {
  374. pResult = FromSFVALUE( CreateGFxValue( ToSFVALUE( &var ) ) );
  375. }
  376. return ToSFVALUE( pResult );
  377. }
  378. /***********
  379. * hit testing
  380. */
  381. bool ScaleformUIImpl::MovieView_HitTest( SFMOVIE movieView, float x, float y, IScaleformUI::_HitTestType testCond, unsigned int controllerIdx )
  382. {
  383. return FromSFMOVIE( movieView )->HitTest( x, y, HitTestType_SFUI_to_SDK(testCond), controllerIdx );
  384. }