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.

459 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "scaleformuiintegration.h"
  9. #include "tier1/keyvalues.h"
  10. #include "vgui/ILocalize.h"
  11. #include "matchmaking/imatchframework.h"
  12. #include "shaderapi/ishaderapi.h"
  13. #if !defined( NO_STEAM )
  14. #include "steam/steam_api.h"
  15. #endif
  16. #include "Render/ImageFiles/PNG_ImageFile.h"
  17. #include "Render/ImageFiles/DDS_ImageFile.h"
  18. // NOTE: This must be the last file included!!!
  19. #include "tier0/memdbgon.h"
  20. using namespace Scaleform::GFx;
  21. #if !defined( NO_STEAM )
  22. static CSteamAPIContext g_SteamAPIContext;
  23. CSteamAPIContext *steamapicontext = &g_SteamAPIContext;
  24. #endif
  25. /********************************************
  26. * This is the singleton that will be exposed to all other DLLs
  27. * through the valve dll system
  28. */
  29. ScaleformUIImpl ScaleformUIImpl::m_Instance;
  30. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( ScaleformUIImpl, IScaleformUI, SCALEFORMUI_INTERFACE_VERSION, ScaleformUIImpl::m_Instance )
  31. static const char* defSafeZone = "1.0";
  32. static const char* defHudScaling = "0.85";
  33. ConVar safezonex( "safezonex", defSafeZone, FCVAR_ARCHIVE | FCVAR_ARCHIVE_GAMECONSOLE, "The percentage of the screen width that is considered safe from overscan", true, 0.2f, true, 1.0f );
  34. ConVar safezoney( "safezoney", defSafeZone, FCVAR_ARCHIVE | FCVAR_ARCHIVE_GAMECONSOLE, "The percentage of the screen height that is considered safe from overscan", true, 0.85f, true, 1.0f );
  35. ConVar hud_scaling( "hud_scaling", defHudScaling, FCVAR_ARCHIVE, "Scales hud elements", true, .5f, true, 0.95f );
  36. void safezonechanged( IConVar *var, const char *pOldValue, float flOldValue )
  37. {
  38. ConVarRef varOption( "safezonex" );
  39. float fNewSafe = varOption.GetFloat();
  40. fNewSafe = clamp( fNewSafe, SFINST.GetEnginePtr()->GetSafeZoneXMin(), varOption.GetMax() );
  41. if ( fNewSafe != varOption.GetFloat() )
  42. varOption.SetValue( fNewSafe );
  43. SFINST.UpdateSafeZone();
  44. }
  45. // Defaults to a typically unused value, so that a fresh user profile detects the load and initializes the UI tint properly
  46. ConVar sf_ui_tint( "sf_ui_tint", "8", FCVAR_ARCHIVE, "The current tint applied to the Scaleform UI" );
  47. void ui_tint_changed( IConVar *var, const char *pOldValue, float flOldValue )
  48. {
  49. SFINST.UpdateTint();
  50. }
  51. ScaleformUIImpl::ScaleformUIImpl()
  52. {
  53. ClearMembers();
  54. }
  55. void ScaleformUIImpl::ClearMembers( void )
  56. {
  57. #if defined( USE_SDL ) || defined( OSX )
  58. m_pLauncherMgr = NULL;
  59. #endif
  60. m_pSystem = NULL;
  61. m_pLoader = NULL;
  62. m_pAllocator = NULL;
  63. m_pShaderDeviceMgr = NULL;
  64. m_pDeviceCallbacks = NULL;
  65. m_pGameUIFuncs = NULL;
  66. m_bTrySWFFirst = false;
  67. m_iScreenWidth = 0;
  68. m_iScreenHeight = 0;
  69. m_iLastMouseX = 0;
  70. m_iLastMouseY = 0;
  71. m_pDefaultAvatarImage = NULL;
  72. m_pDefaultAvatarTexture = NULL;
  73. m_pDefaultInventoryImage = NULL;
  74. m_pDefaultInventoryTexture = NULL;
  75. #ifdef USE_DEFAULT_INVENTORY_ICON_BACKGROUNDS
  76. m_defaultInventoryIcons.Init( 64 );
  77. #endif
  78. m_pDefaultChromeHTMLImage = NULL;
  79. m_pEngine = NULL;
  80. m_pGameEventManager = NULL;
  81. m_pShaderAPI = NULL;
  82. m_bPumpScaleformStats = false;
  83. m_bForcePS3 = false;
  84. m_bDenyAllInputToGame = false;
  85. m_pRenderHAL.Clear();
  86. m_pRenderer2D.Clear();
  87. m_pThreadCommandQueue = NULL;
  88. m_bIMEEnabled = false;
  89. m_iIMEFocusSlot = SF_FULL_SCREEN_SLOT;
  90. m_pDevice = NULL;
  91. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  92. m_pD3D9Stateblock = NULL;
  93. #endif
  94. m_bSingleThreaded = true;
  95. m_bClearMeshCacheQueued = false;
  96. m_fTime = 0.0f;
  97. }
  98. bool ScaleformUIImpl::Connect( CreateInterfaceFn factory )
  99. {
  100. if ( !factory )
  101. {
  102. return false;
  103. }
  104. if ( !BaseClass::Connect( factory ) )
  105. {
  106. return false;
  107. }
  108. int result;
  109. #if defined( USE_SDL )
  110. m_pLauncherMgr = (ILauncherMgr *)factory( SDLMGR_INTERFACE_VERSION, NULL);
  111. #elif defined( OSX )
  112. m_pLauncherMgr = (ILauncherMgr *)factory( COCOAMGR_INTERFACE_VERSION, NULL);
  113. #endif
  114. m_pShaderDeviceMgr = ( IShaderDeviceMgr* ) factory( SHADER_DEVICE_MGR_INTERFACE_VERSION, &result );
  115. m_pGameUIFuncs = ( IGameUIFuncs* ) factory( VENGINE_GAMEUIFUNCS_VERSION, &result );
  116. m_pEngine = ( IVEngineClient* )factory( VENGINE_CLIENT_INTERFACE_VERSION, NULL );
  117. m_pGameEventManager = ( IGameEventManager2* )factory ( INTERFACEVERSION_GAMEEVENTSMANAGER2, &result );
  118. m_pShaderAPI = ( IShaderAPI * )factory( SHADERAPI_INTERFACE_VERSION, &result );
  119. if ( !m_pShaderDeviceMgr || !m_pGameUIFuncs || !m_pEngine || !m_pGameEventManager || !m_pShaderAPI )
  120. {
  121. Warning( "ScaleformUI missing expected interface\n" );
  122. return false;
  123. }
  124. // Initialize the console variables.
  125. ConVar_Register();
  126. RefreshKeyBindings();
  127. return m_pShaderDeviceMgr != NULL;
  128. }
  129. void ScaleformUIImpl::Disconnect( void )
  130. {
  131. ShutdownRendererImpl();
  132. m_pShaderDeviceMgr = NULL;
  133. m_pGameUIFuncs = NULL;
  134. m_pEngine = NULL;
  135. m_pGameEventManager = NULL;
  136. m_pShaderAPI = NULL;
  137. BaseClass::Disconnect();
  138. }
  139. void ScaleformUIImpl::InitFonts( void )
  140. {
  141. MEM_ALLOC_CREDIT();
  142. // get the file that defines the font mapping
  143. KeyValues* pkeyValueData = new KeyValues( "english" );
  144. KeyValues::AutoDelete autodelete( pkeyValueData );
  145. // Load the config data
  146. if ( !pkeyValueData )
  147. {
  148. return;
  149. }
  150. pkeyValueData->LoadFromFile( g_pFullFileSystem, "resource/flash/fontmapping.cfg", "game" );
  151. // PC uses one font library containing all languages.
  152. const char *fontLib = pkeyValueData->GetString( "fontlib", "resource/flash/fontlib.swf" );
  153. SF::Ptr<FontLib> pFontLib = *new FontLib;
  154. m_pLoader->SetFontLib( pFontLib );
  155. SF::Ptr<MovieDef> pFontMovie = *m_pLoader->CreateMovie( fontLib );
  156. pFontLib->AddFontsFrom( pFontMovie, true );
  157. pFontMovie = *m_pLoader->CreateMovie( "resource/flash/fontlib_extra.swf" );
  158. pFontLib->AddFontsFrom( pFontMovie, true );
  159. SF::Ptr<FontMap> pFontMap = *new FontMap();
  160. // now connect each of the fonts with its exported name
  161. for ( KeyValues* piter = pkeyValueData->GetFirstTrueSubKey(); piter; piter = piter->GetNextTrueSubKey() )
  162. {
  163. const char* exportedName = piter->GetName();
  164. const char* fontName = piter->GetString( "font", "Arial" );
  165. const char* fontStyle = piter->GetString( "style", "Normal" );
  166. FontMap::MapFontFlags fontFlags = FontMap::MFF_Original;
  167. if ( !V_stricmp( fontStyle, "bold" ) )
  168. {
  169. fontFlags = FontMap::MFF_Bold;
  170. }
  171. else if ( !V_stricmp( fontStyle, "italic" ) )
  172. {
  173. fontFlags = FontMap::MFF_Bold;
  174. }
  175. else if ( !V_stricmp( fontStyle, "bolditalic" ) || !V_stricmp( fontStyle, "italicbold" ) )
  176. {
  177. fontFlags = FontMap::MFF_BoldItalic;
  178. }
  179. pFontMap->MapFont( exportedName, fontName, fontFlags );
  180. }
  181. m_pLoader->SetFontMap( pFontMap );
  182. }
  183. InitReturnVal_t ScaleformUIImpl::Init( void )
  184. {
  185. MEM_ALLOC_CREDIT();
  186. InitReturnVal_t nRetVal = BaseClass::Init();
  187. if ( nRetVal != INIT_OK )
  188. {
  189. return nRetVal;
  190. }
  191. Assert( m_pAllocator == NULL );
  192. m_pAllocator = new CScaleformSysAlloc();
  193. Assert( m_pSystem == NULL );
  194. m_pSystem = new System( m_pAllocator );
  195. Assert( m_pLoader == NULL );
  196. m_pLoader = new Loader();
  197. m_pLoader->SetLog( SF::Ptr<Log> ( *new ScaleformUILogging() ) );
  198. m_pLoader->SetFileOpener( SF::Ptr<ScaleformFileOpener> ( *new ScaleformFileOpener() ) );
  199. m_pLoader->SetTextClipboard( SF::Ptr<ScaleformClipboard> ( *new ScaleformClipboard() ) );
  200. m_pLoader->SetTranslator( SF::Ptr<ScaleformTranslatorAdapter> ( *new ScaleformTranslatorAdapter() ) );
  201. m_pLoader->SetAS2Support( SF::Ptr<AS2Support>( *new AS2Support() ) );
  202. SF::Log::SetGlobalLog( m_pLoader->GetLog() );
  203. // setup image handlers
  204. SF::Ptr<SF::GFx::ImageFileHandlerRegistry> pimgReg = *new SF::GFx::ImageFileHandlerRegistry();
  205. #ifdef SF_ENABLE_LIBJPEG
  206. pimgReg->AddHandler( &SF::Render::JPEG::FileReader::Instance );
  207. #endif
  208. #ifdef SF_ENABLE_LIBPNG
  209. pimgReg->AddHandler( &SF::Render::PNG::FileReader::Instance );
  210. #endif
  211. pimgReg->AddHandler( &SF::Render::TGA::FileReader::Instance );
  212. pimgReg->AddHandler( &SF::Render::DDS::FileReader::Instance );
  213. m_pLoader->SetImageFileHandlerRegistry( pimgReg );
  214. #if defined( PLATFORM_WINDOWS_PC )
  215. m_bTrySWFFirst = true;
  216. #elif defined( _CERT )
  217. if ( CommandLine()->FindParm( "-tryswf" ) )
  218. {
  219. m_bTrySWFFirst = true;
  220. }
  221. else
  222. {
  223. m_bTrySWFFirst = false;
  224. }
  225. #else
  226. if ( CommandLine()->FindParm( "-ignoreswf" ) )
  227. {
  228. m_bTrySWFFirst = false;
  229. }
  230. else
  231. {
  232. m_bTrySWFFirst = true;
  233. }
  234. #endif
  235. #if !defined( _CERT )
  236. if ( CommandLine()->FindParm( "-sfstats" ) )
  237. m_bPumpScaleformStats = true;
  238. #endif
  239. #if !defined( NO_STEAM )
  240. SteamAPI_InitSafe();
  241. g_SteamAPIContext.Init();
  242. #endif // NO_STEAM
  243. InitValueImpl();
  244. InitTranslationImpl();
  245. InitRendererImpl();
  246. InitMovieImpl();
  247. InitHighLevelImpl();
  248. InitMovieSlotImpl();
  249. safezonex.InstallChangeCallback( safezonechanged, false );
  250. safezoney.InstallChangeCallback( safezonechanged, false );
  251. hud_scaling.InstallChangeCallback( safezonechanged, false );
  252. sf_ui_tint.InstallChangeCallback( ui_tint_changed, false );
  253. return nRetVal;
  254. }
  255. void ScaleformUIImpl::Shutdown( void )
  256. {
  257. // On Ps3, we crash on exit here.
  258. // Should revisit to ensure it's not related to the IB/VB mesh cacheing optimisation
  259. // But for pre-cert this hack will get us much better coverage testing at this point
  260. #ifdef _PS3
  261. return;
  262. #endif
  263. if ( m_pSystem )
  264. {
  265. safezonex.RemoveChangeCallback( safezonechanged );
  266. safezoney.RemoveChangeCallback( safezonechanged );
  267. hud_scaling.RemoveChangeCallback( safezonechanged );
  268. sf_ui_tint.RemoveChangeCallback( ui_tint_changed );
  269. ShutdownTranslationImpl();
  270. ShutdownMovieSlotImpl();
  271. ShutdownHighLevelImpl();
  272. ShutdownMovieImpl();
  273. ShutdownRendererImpl();
  274. ShutdownValueImpl();
  275. m_pTranslatorAdapter = NULL;
  276. delete m_pLoader;
  277. delete m_pSystem;
  278. delete m_pAllocator;
  279. ClearMembers();
  280. ConVar_Unregister();
  281. #if !defined( NO_STEAM )
  282. g_SteamAPIContext.Clear(); // Steam API context shutdown
  283. #endif
  284. }
  285. BaseClass::Shutdown();
  286. }
  287. void* ScaleformUIImpl::QueryInterface( const char *pInterfaceName )
  288. {
  289. if ( !Q_strncmp( pInterfaceName, SCALEFORMUI_INTERFACE_VERSION, Q_strlen( SCALEFORMUI_INTERFACE_VERSION ) + 1 ) )
  290. {
  291. return ( IScaleformUI* ) &ScaleformUIImpl::m_Instance;
  292. }
  293. return BaseClass::QueryInterface( pInterfaceName );
  294. }
  295. const AppSystemInfo_t* ScaleformUIImpl::GetDependencies( void )
  296. {
  297. return BaseClass::GetDependencies();
  298. }
  299. void ScaleformUIImpl::LogPrintf( const char *format, ... )
  300. {
  301. #if !defined( _CERT )
  302. va_list al;
  303. va_start( al, format );
  304. SF::Ptr<Log> pLog = m_pLoader->GetLog();
  305. if ( pLog )
  306. {
  307. pLog->LogMessageVarg( SF::Log_Message, format, al );
  308. }
  309. pLog = NULL;
  310. #endif
  311. }
  312. void SF_VerboseToggle( void );
  313. void SF_VerboseOn( void );
  314. void SF_VerboseOff( void );
  315. static ConCommand dev_scaleform_verbose_toggle("dev_scaleform_verbose_toggle", SF_VerboseToggle, "Enable/disable Scaleform verbose mode.", FCVAR_DONTRECORD | FCVAR_DEVELOPMENTONLY );
  316. static ConCommand dev_scaleform_verbose_on("dev_scaleform_verbose_on", SF_VerboseOn, "Enable Scaleform verbose mode.", FCVAR_DONTRECORD | FCVAR_DEVELOPMENTONLY );
  317. static ConCommand dev_scaleform_verbose_off("dev_scaleform_verbose_off", SF_VerboseOff, "Disable Scaleform verbose mode.", FCVAR_DONTRECORD | FCVAR_DEVELOPMENTONLY );
  318. bool ScaleformUIImpl::GetVerbose( void )
  319. {
  320. if ( m_pLoader )
  321. {
  322. SF::Ptr<ActionControl> pActionControl = m_pLoader->GetActionControl();
  323. if ( pActionControl )
  324. {
  325. return ( ( pActionControl->GetActionFlags() & ActionControl::Action_Verbose ) != 0 );
  326. }
  327. }
  328. return false;
  329. }
  330. void ScaleformUIImpl::SetVerbose( bool bVerbose )
  331. {
  332. if ( m_pLoader )
  333. {
  334. SF::Ptr<ActionControl> pActionControl = m_pLoader->GetActionControl();
  335. if ( !pActionControl )
  336. {
  337. pActionControl = *new ActionControl();
  338. m_pLoader->SetActionControl(pActionControl);
  339. }
  340. pActionControl->SetVerboseAction( bVerbose );
  341. pActionControl->SetLogAllFilenames( true );
  342. }
  343. }
  344. void SF_VerboseToggle( void )
  345. {
  346. bool bWasVerbose = SFINST.GetVerbose();
  347. SFINST.SetVerbose( !bWasVerbose );
  348. }
  349. void SF_VerboseOn( void )
  350. {
  351. SFINST.SetVerbose( true );
  352. }
  353. void SF_VerboseOff( void )
  354. {
  355. SFINST.SetVerbose( false );
  356. }