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.

820 lines
24 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "gamelayer.h"
  8. #include "gamerect.h"
  9. #include "tier1/keyvalues.h"
  10. #include "materialsystem/imaterial.h"
  11. #include "materialsystem/imaterialvar.h"
  12. #include "tier1/utlbuffer.h"
  13. #include "tier2/tier2.h"
  14. #include "filesystem.h"
  15. #include "rendersystem/irendercontext.h"
  16. #include "tier2/fileutils.h"
  17. #include "gameuisystemsurface.h"
  18. #include "gameuidefinition.h"
  19. #include "tier1/fmtstr.h"
  20. #include "gamerect.h"
  21. #include "gametext.h"
  22. #include "hitarea.h"
  23. #include "graphicgroup.h"
  24. #include "dynamicrect.h"
  25. #include "gameuiscript.h"
  26. #include "gameuisystemmgr.h"
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. BEGIN_DMXELEMENT_UNPACK ( CGameLayer )
  30. DMXELEMENT_UNPACK_FIELD_UTLSTRING( "name", "", m_pName )
  31. END_DMXELEMENT_UNPACK( CGameLayer, s_GameLayerUnpack )
  32. //-----------------------------------------------------------------------------
  33. // Constructor
  34. //-----------------------------------------------------------------------------
  35. CGameLayer::CGameLayer( SublayerTypes_t layerType )
  36. {
  37. m_pName = "";
  38. m_LayerType = layerType;
  39. m_pTextureName = "";
  40. m_hTexture = RENDER_TEXTURE_HANDLE_INVALID;
  41. m_Material = NULL;
  42. m_Sheet = NULL;
  43. m_SheetSymbol = UTL_INVAL_SYMBOL;
  44. m_bSheetSymbolCached = false;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Destructor
  48. //-----------------------------------------------------------------------------
  49. CGameLayer::~CGameLayer()
  50. {
  51. if ( m_Material.IsValid() )
  52. {
  53. m_Material.Shutdown( true );
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. //
  58. //-----------------------------------------------------------------------------
  59. void CGameLayer::Shutdown()
  60. {
  61. int nGraphicsCount = m_LayerGraphics.Count();
  62. for ( int j = 0; j < nGraphicsCount; ++j )
  63. {
  64. Assert( !m_LayerGraphics[j]->IsGroup() );
  65. delete m_LayerGraphics[j];
  66. m_LayerGraphics[j] = NULL;
  67. }
  68. m_LayerGraphics.RemoveAll();
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Load data from dmx file
  72. //-----------------------------------------------------------------------------
  73. bool CGameLayer::Unserialize( CDmxElement *pLayer, CUtlDict< CGameGraphic *, int > &unserializedGraphicMapping )
  74. {
  75. pLayer->UnpackIntoStructure( this, s_GameLayerUnpack );
  76. // Static graphics
  77. if ( m_LayerType == SUBLAYER_STATIC )
  78. {
  79. CDmxAttribute *pGraphics = pLayer->GetAttribute( "staticGraphics" );
  80. const CUtlVector< CDmxElement * > &graphics = pGraphics->GetArray< CDmxElement * >( );
  81. int nCount = graphics.Count();
  82. for ( int i = 0; i < nCount; ++i )
  83. {
  84. // What kind of graphic is this?
  85. const char *pType = graphics[i]->GetValueString( "classtype" );
  86. if ( !Q_stricmp( pType, "CDmeRectGeometry" ) )
  87. {
  88. CGameRect *pGraphic = new CGameRect( "" );
  89. if ( !pGraphic->Unserialize( graphics[i] ) )
  90. {
  91. delete pGraphic;
  92. return false;
  93. }
  94. m_LayerGraphics.AddToTail( pGraphic );
  95. char pBuf[255];
  96. UniqueIdToString( graphics[i]->GetId(), pBuf, 255 );
  97. unserializedGraphicMapping.Insert( pBuf, pGraphic );
  98. }
  99. else if ( !Q_stricmp( pType, "CDmeHitAreaGeometry" ) )
  100. {
  101. CHitArea *pGraphic = new CHitArea( "" );
  102. if ( !pGraphic->Unserialize( graphics[i] ) )
  103. {
  104. delete pGraphic;
  105. return false;
  106. }
  107. m_LayerGraphics.AddToTail( pGraphic );
  108. char pBuf[255];
  109. UniqueIdToString( graphics[i]->GetId(), pBuf, 255 );
  110. unserializedGraphicMapping.Insert( pBuf, pGraphic );
  111. }
  112. else
  113. {
  114. Warning( "CGameUIDefinition: Warning unknown static graphic type\n" );
  115. }
  116. }
  117. CDmxAttribute *pTextureName = pLayer->GetAttribute( "statictexture" );
  118. if ( !pTextureName || pTextureName->GetType() != AT_STRING )
  119. {
  120. return false;
  121. }
  122. char textureName[128];
  123. pTextureName->GetValueAsString( textureName, 128 );
  124. InitSheetTexture( textureName );
  125. }
  126. // Dynamic graphics
  127. else if ( m_LayerType == SUBLAYER_DYNAMIC )
  128. {
  129. CDmxAttribute *pGraphicsAttr = pLayer->GetAttribute( "dynamicGraphics" );
  130. const CUtlVector< CDmxElement * > &graphics = pGraphicsAttr->GetArray< CDmxElement * >( );
  131. for ( int i = 0; i < graphics.Count(); ++i )
  132. {
  133. // What kind of graphic is this?
  134. const char *pType = graphics[i]->GetValueString( "classtype" );
  135. if ( !Q_stricmp( pType, "CDmeDynamicRectGeometry" ) )
  136. {
  137. CDynamicRect *pGraphic = new CDynamicRect( "" );
  138. if ( !pGraphic->Unserialize( graphics[i] ) )
  139. {
  140. delete pGraphic;
  141. return false;
  142. }
  143. m_LayerGraphics.AddToTail( pGraphic );
  144. char pBuf[255];
  145. UniqueIdToString( graphics[i]->GetId(), pBuf, 255 );
  146. unserializedGraphicMapping.Insert( pBuf, pGraphic );
  147. }
  148. else
  149. {
  150. Warning( "CGameUIDefinition: Warning unknown static graphic type\n" );
  151. }
  152. }
  153. }
  154. // Font graphics
  155. else if ( m_LayerType == SUBLAYER_FONT )
  156. {
  157. CDmxAttribute *pGraphics = pLayer->GetAttribute( "fontGraphics" );
  158. const CUtlVector< CDmxElement * > &graphics = pGraphics->GetArray< CDmxElement * >( );
  159. int nCount = graphics.Count();
  160. for ( int i = 0; i < nCount; ++i )
  161. {
  162. // What kind of graphic is this?
  163. const char *pType = graphics[i]->GetValueString( "classtype" );
  164. if ( !Q_stricmp( pType, "CDmeTextGeometry" ) )
  165. {
  166. CGameText *pGraphic = new CGameText( "" );
  167. if ( !pGraphic->Unserialize( graphics[i] ) )
  168. {
  169. delete pGraphic;
  170. return false;
  171. }
  172. m_LayerGraphics.AddToTail( pGraphic );
  173. char pBuf[255];
  174. UniqueIdToString( graphics[i]->GetId(), pBuf, 255 );
  175. GameGraphicMap_t graphicMapEntry;
  176. graphicMapEntry.m_Id = graphics[i]->GetId();
  177. graphicMapEntry.pGraphic = pGraphic;
  178. unserializedGraphicMapping.Insert( pBuf, pGraphic );
  179. }
  180. else
  181. {
  182. Warning( "CGameUIDefinition: Warning unknown font graphic type in gui file.\n" );
  183. }
  184. }
  185. }
  186. else
  187. {
  188. Assert(0); // Unknown layer type!!
  189. }
  190. return true;
  191. }
  192. //-----------------------------------------------------------------------------
  193. //
  194. //-----------------------------------------------------------------------------
  195. bool CGameLayer::InitSheetTexture()
  196. {
  197. Assert( m_pTextureName );
  198. return InitSheetTexture( m_pTextureName );
  199. }
  200. //-----------------------------------------------------------------------------
  201. // FIXME: Must go into rendersystem at some point
  202. //-----------------------------------------------------------------------------
  203. static HRenderTexture CreateTextureFromVTFFile( const char *pFileName, CSheet **ppSheet )
  204. {
  205. static intp s_UniqueID = 0x4000000;
  206. *ppSheet = NULL;
  207. char pTemp[MAX_PATH];
  208. Q_ComposeFileName( "materials/", pFileName, pTemp, sizeof(pTemp) );
  209. Q_SetExtension( pTemp, "vtf", sizeof(pTemp) );
  210. pFileName = pTemp;
  211. char pTemp360[MAX_PATH];
  212. if ( PLATFORM_EXT[0] )
  213. {
  214. CreatePlatformFilename( pFileName, pTemp360, sizeof(pTemp360) );
  215. pFileName = pTemp360;
  216. }
  217. CUtlBuffer fBuf;
  218. if ( !g_pFullFileSystem->ReadFile( pFileName, "GAME", fBuf ) )
  219. {
  220. Warning( "Unable to read texture file %s\n", pFileName );
  221. return RENDER_TEXTURE_HANDLE_INVALID;
  222. }
  223. IVTFTexture *pVtfTexture = CreateVTFTexture();
  224. #if !defined( _GAMECONSOLE )
  225. pVtfTexture->Unserialize( fBuf );
  226. #else
  227. pVtfTexture->UnserializeFromBuffer( fBuf, true, false, false, 0 );
  228. #endif
  229. // we are only going to load the high mip
  230. // pCurData is pointing at pixel data now
  231. TextureHeader_t spec;
  232. memset( &spec, 0, sizeof(TextureHeader_t) );
  233. spec.m_nWidth = pVtfTexture->Width();
  234. spec.m_nHeight = pVtfTexture->Height();
  235. spec.m_nNumMipLevels = 1; //pVtfTexture->MipCount();
  236. spec.m_nDepth = pVtfTexture->Depth();
  237. spec.m_nImageFormat = pVtfTexture->Format();
  238. //spec.m_vecAverageColor.Init( pVtfTexture->Reflectivity().x, pVtfTexture->Reflectivity().y, pVtfTexture->Reflectivity().z, 1 );
  239. char pResourceName[16];
  240. Q_snprintf( pResourceName, sizeof(pResourceName), "%d", s_UniqueID );
  241. HRenderTexture hRet = g_pRenderDevice->FindOrCreateTexture( "gamelayer", pResourceName, &spec );
  242. ResourceAddRef( hRet );
  243. s_UniqueID++;
  244. IRenderContext *pCtx = g_pRenderDevice->GetRenderContext( );
  245. pCtx->SetTextureData( hRet, &spec, pVtfTexture->ImageData( 0, 0, 0 ), pVtfTexture->ComputeTotalSize(), pVtfTexture->IsPreTiled() );
  246. pCtx->Submit( );
  247. g_pRenderDevice->ReleaseRenderContext( pCtx );
  248. ResourceRelease( hRet );
  249. size_t nSheetByteCount;
  250. const void *pSheetData = pVtfTexture->GetResourceData( VTF_RSRC_SHEET, &nSheetByteCount );
  251. if ( pSheetData )
  252. {
  253. // expand compact sheet into fatter runtime form
  254. CUtlBuffer bufLoad( pSheetData, nSheetByteCount, CUtlBuffer::READ_ONLY );
  255. *ppSheet = new CSheet( bufLoad );
  256. }
  257. DestroyVTFTexture( pVtfTexture );
  258. return hRet;
  259. }
  260. //-----------------------------------------------------------------------------
  261. //
  262. //-----------------------------------------------------------------------------
  263. bool CGameLayer::InitSheetTexture( const char *pBaseTextureName )
  264. {
  265. m_pTextureName = pBaseTextureName;
  266. if ( !g_pRenderDevice )
  267. {
  268. // Material names must be different for each menu.
  269. CFmtStr materialName;
  270. switch ( m_LayerType )
  271. {
  272. case SUBLAYER_STATIC:
  273. materialName.sprintf( "statictx_%s", pBaseTextureName );
  274. break;
  275. case SUBLAYER_DYNAMIC:
  276. materialName.sprintf( "dynamictx_%s", pBaseTextureName );
  277. break;
  278. case SUBLAYER_FONT:
  279. materialName.sprintf( "fonttx_%s", pBaseTextureName );
  280. break;
  281. default:
  282. Assert(0);
  283. break;
  284. };
  285. KeyValues *pVMTKeyValues = new KeyValues( "GameControls" );
  286. pVMTKeyValues->SetString( "$basetexture", pBaseTextureName );
  287. m_Material.Init( materialName, TEXTURE_GROUP_OTHER, pVMTKeyValues );
  288. m_Material->Refresh();
  289. m_Sheet.Set( LoadSheet( m_Material ) );
  290. }
  291. else
  292. {
  293. CUtlString materialName;
  294. switch ( m_LayerType )
  295. {
  296. case SUBLAYER_STATIC:
  297. CSheet *pSheet;
  298. m_hTexture = CreateTextureFromVTFFile( pBaseTextureName, &pSheet );
  299. m_Sheet.Set( pSheet );
  300. break;
  301. case SUBLAYER_DYNAMIC:
  302. break;
  303. case SUBLAYER_FONT:
  304. break;
  305. default:
  306. Assert(0);
  307. break;
  308. };
  309. }
  310. return true;
  311. }
  312. //-----------------------------------------------------------------------------
  313. // UI sheets
  314. //-----------------------------------------------------------------------------
  315. CSheet *CGameLayer::LoadSheet( IMaterial *pMaterial )
  316. {
  317. if ( !pMaterial )
  318. return NULL;
  319. bool bFoundVar = false;
  320. IMaterialVar *pVar = pMaterial->FindVar( "$basetexture", &bFoundVar, true );
  321. if ( bFoundVar && pVar && pVar->IsDefined() )
  322. {
  323. ITexture *pTex = pVar->GetTextureValue();
  324. if ( pTex && !pTex->IsError() )
  325. {
  326. return LoadSheet( pTex->GetName(), pTex );
  327. }
  328. }
  329. return NULL;
  330. }
  331. void CGameLayer::GetSheetTextureSize( int &nWidth, int &nHeight )
  332. {
  333. if ( !m_Material )
  334. return;
  335. bool bFoundVar = false;
  336. IMaterialVar *pVar = m_Material->FindVar( "$basetexture", &bFoundVar, true );
  337. if ( bFoundVar && pVar && pVar->IsDefined() )
  338. {
  339. ITexture *pTex = pVar->GetTextureValue();
  340. if ( pTex && !pTex->IsError() )
  341. {
  342. nWidth = pTex->GetActualWidth();
  343. nHeight = pTex->GetActualHeight();
  344. }
  345. }
  346. }
  347. //--------------------------------------------------------------------------------
  348. // UI sheets
  349. //--------------------------------------------------------------------------------
  350. CSheet *CGameLayer::LoadSheet( char const *pszFname, ITexture *pTexture )
  351. {
  352. //if ( !m_bShouldLoadSheets )
  353. // return NULL;
  354. //if ( m_SheetList.Defined( pszFname ) )
  355. // return m_SheetList[ pszFname ];
  356. CSheet *pNewSheet = NULL;
  357. // get compact sheet representation held by texture
  358. size_t numBytes;
  359. void const *pSheet = pTexture->GetResourceData( VTF_RSRC_SHEET, &numBytes );
  360. if ( pSheet )
  361. {
  362. // expand compact sheet into fatter runtime form
  363. CUtlBuffer bufLoad( pSheet, numBytes, CUtlBuffer::READ_ONLY );
  364. pNewSheet = new CSheet( bufLoad );
  365. }
  366. //m_SheetList[ pszFname ] = pNewSheet;
  367. return pNewSheet;
  368. }
  369. //--------------------------------------------------------------------------------
  370. // Add a graphic to the list.
  371. //--------------------------------------------------------------------------------
  372. int CGameLayer::AddGraphic( CGameGraphic *pGraphic )
  373. {
  374. return m_LayerGraphics.AddToTail( pGraphic );
  375. }
  376. //--------------------------------------------------------------------------------
  377. // Remove a graphic from the list.
  378. // Returns true if it was found and removed.
  379. //--------------------------------------------------------------------------------
  380. bool CGameLayer::RemoveGraphic( CGameGraphic *pGraphic )
  381. {
  382. return m_LayerGraphics.FindAndRemove( pGraphic );
  383. }
  384. //--------------------------------------------------------------------------------
  385. // Clear out the graphics list
  386. //--------------------------------------------------------------------------------
  387. void CGameLayer::ClearGraphics()
  388. {
  389. m_LayerGraphics.RemoveAll();
  390. }
  391. //--------------------------------------------------------------------------------
  392. // Clear out the graphics list
  393. //--------------------------------------------------------------------------------
  394. bool CGameLayer::HasGraphic( CGameGraphic *pGraphic )
  395. {
  396. int nCount = m_LayerGraphics.Count();
  397. for ( int i = 0; i < nCount; ++i )
  398. {
  399. if ( pGraphic == m_LayerGraphics[i] )
  400. return true;
  401. }
  402. return false;
  403. }
  404. //-----------------------------------------------------------------------------
  405. //
  406. //-----------------------------------------------------------------------------
  407. IMaterial *CGameLayer::GetMaterial()
  408. {
  409. // FIXME
  410. // NOTE: This has to be this way to ensure we don't load every freaking material @ startup
  411. //Assert( IsPrecached() );
  412. //if ( !IsPrecached() )
  413. // return NULL;
  414. return m_Material;
  415. }
  416. //-----------------------------------------------------------------------------
  417. //
  418. //-----------------------------------------------------------------------------
  419. void CGameLayer::InvalidateSheetSymbol()
  420. {
  421. m_bSheetSymbolCached = false;
  422. }
  423. //-----------------------------------------------------------------------------
  424. //
  425. //-----------------------------------------------------------------------------
  426. void CGameLayer::CacheSheetSymbol( CUtlSymbol sheetSymbol )
  427. {
  428. m_SheetSymbol = sheetSymbol;
  429. m_bSheetSymbolCached = true;
  430. }
  431. //-----------------------------------------------------------------------------
  432. //
  433. //-----------------------------------------------------------------------------
  434. CUtlSymbol CGameLayer::GetSheetSymbol() const
  435. {
  436. Assert( IsSheetSymbolCached() );
  437. return m_SheetSymbol;
  438. }
  439. //-----------------------------------------------------------------------------
  440. // Start playing animations
  441. //-----------------------------------------------------------------------------
  442. void CGameLayer::StartPlaying()
  443. {
  444. int nCount = m_LayerGraphics.Count();
  445. for ( int i = 0; i < nCount; ++i )
  446. {
  447. m_LayerGraphics[i]->StartPlaying();
  448. }
  449. }
  450. //-----------------------------------------------------------------------------
  451. // Stop playing animations
  452. //-----------------------------------------------------------------------------
  453. void CGameLayer::StopPlaying()
  454. {
  455. int nCount = m_LayerGraphics.Count();
  456. for ( int i = 0; i < nCount; ++i )
  457. {
  458. m_LayerGraphics[i]->StopPlaying();
  459. }
  460. }
  461. //-----------------------------------------------------------------------------
  462. // Move to the next available animation state
  463. //-----------------------------------------------------------------------------
  464. void CGameLayer::AdvanceState()
  465. {
  466. int nCount = m_LayerGraphics.Count();
  467. for ( int i = 0; i < nCount; ++i )
  468. {
  469. m_LayerGraphics[i]->AdvanceState();
  470. }
  471. }
  472. //-----------------------------------------------------------------------------
  473. // Set all graphics to "default" state.
  474. //-----------------------------------------------------------------------------
  475. void CGameLayer::InitAnims()
  476. {
  477. int nCount = m_LayerGraphics.Count();
  478. for ( int i = 0; i < nCount; ++i )
  479. {
  480. m_LayerGraphics[i]->SetState( "default" );
  481. }
  482. }
  483. //-----------------------------------------------------------------------------
  484. //
  485. //-----------------------------------------------------------------------------
  486. void CGameLayer::UpdateGeometry()
  487. {
  488. int nGraphicsCount = m_LayerGraphics.Count();
  489. if ( nGraphicsCount == 0 )
  490. {
  491. return;
  492. }
  493. for ( int i = 0; i < nGraphicsCount; ++i )
  494. {
  495. m_LayerGraphics[i]->UpdateGeometry();
  496. }
  497. }
  498. //-----------------------------------------------------------------------------
  499. //
  500. //-----------------------------------------------------------------------------
  501. void CGameLayer::UpdateRenderTransforms( const StageRenderInfo_t &stageRenderInfo )
  502. {
  503. int nGraphicsCount = m_LayerGraphics.Count();
  504. if ( nGraphicsCount == 0 )
  505. {
  506. return;
  507. }
  508. for ( int i = 0; i < nGraphicsCount; ++i )
  509. {
  510. m_LayerGraphics[i]->UpdateRenderTransforms( stageRenderInfo );
  511. }
  512. }
  513. //-----------------------------------------------------------------------------
  514. //
  515. //-----------------------------------------------------------------------------
  516. void CGameLayer::UpdateRenderData( CGameUIDefinition &gameUIDef, color32 parentColor, CUtlVector< LayerRenderLists_t > &renderLists )
  517. {
  518. bool bDrawExtents = false;
  519. int nGraphicsCount = m_LayerGraphics.Count();
  520. if ( nGraphicsCount == 0 )
  521. {
  522. return;
  523. }
  524. // Basic algorithm is one renderList per material.
  525. // Static graphics are sorted so that they all use the same sheet texture.
  526. // Font graphics are sorted by render order. Their rendergeometry contains the font ID to use
  527. // Dynamic graphics could add renderlists, since the material can change.
  528. // Dynamic graphics will keep adding to the same renderlist until the material's don't match.
  529. // This adds draw calls but preserves render order.
  530. if ( m_LayerType == SUBLAYER_DYNAMIC )
  531. {
  532. int listIndex = -1;
  533. int layerIndex = -1;
  534. for ( int i = 0; i < nGraphicsCount; ++i )
  535. {
  536. // Groups should never be in lists of layer graphics.
  537. Assert( !m_LayerGraphics[i]->IsGroup() );
  538. CGraphicGroup *pGroup = m_LayerGraphics[i]->GetGroup();
  539. Assert( pGroup );
  540. // Only dynamic graphics are allowed in dynamic sublayers.
  541. Assert( m_LayerGraphics[i]->IsDynamic() );
  542. // Get the material this graphic uses.
  543. const char *pAlias = m_LayerGraphics[i]->GetMaterialAlias();
  544. IMaterial *pAliasMaterial = g_pGameUISystemMgrImpl->GetImageAliasMaterial( pAlias );
  545. // Is the material this graphic uses the same as the one in our current renderlist?
  546. if ( i == 0 || renderLists[layerIndex].m_pMaterial != pAliasMaterial )
  547. {
  548. // Our material has changed. Add a new renderlist for this material.
  549. layerIndex = renderLists.AddToTail();
  550. listIndex = renderLists[layerIndex].m_RenderGeometryLists.AddToTail();
  551. renderLists[layerIndex].m_LayerType = m_LayerType;
  552. renderLists[layerIndex].m_pMaterial = pAliasMaterial;
  553. renderLists[layerIndex].m_pSheet = NULL;
  554. renderLists[layerIndex].m_hTexture = m_hTexture;
  555. }
  556. CUtlVector< RenderGeometryList_t > &renderGeometryLists = renderLists[layerIndex].m_RenderGeometryLists;
  557. Assert( layerIndex != -1 );
  558. Assert( listIndex != -1 );
  559. m_LayerGraphics[i]->UpdateRenderData( pGroup->GetResultantColor(), renderGeometryLists, listIndex );
  560. }
  561. }
  562. else if ( m_LayerType == SUBLAYER_STATIC )
  563. {
  564. int listIndex = -1;
  565. int layerIndex = -1;
  566. layerIndex = renderLists.AddToTail();
  567. listIndex = renderLists[layerIndex].m_RenderGeometryLists.AddToTail();
  568. renderLists[layerIndex].m_LayerType = m_LayerType;
  569. renderLists[layerIndex].m_pMaterial = m_Material;
  570. renderLists[layerIndex].m_pSheet = m_Sheet;
  571. renderLists[layerIndex].m_hTexture = m_hTexture;
  572. CUtlVector< RenderGeometryList_t > &renderGeometryLists = renderLists[layerIndex].m_RenderGeometryLists;
  573. Assert( layerIndex != -1 );
  574. Assert( listIndex != -1 );
  575. for ( int i = 0; i < nGraphicsCount; ++i )
  576. {
  577. // Groups should never be in lists of layer graphics.
  578. Assert( !m_LayerGraphics[i]->IsGroup() );
  579. CGraphicGroup *pGroup = m_LayerGraphics[i]->GetGroup();
  580. Assert( pGroup );
  581. m_LayerGraphics[i]->UpdateRenderData( pGroup->GetResultantColor(), renderGeometryLists, listIndex );
  582. }
  583. if ( bDrawExtents )
  584. {
  585. for ( int i = 0; i < nGraphicsCount; ++i )
  586. {
  587. if ( !m_LayerGraphics[i]->IsGroup() )
  588. {
  589. m_LayerGraphics[i]->DrawExtents( renderGeometryLists, listIndex );
  590. }
  591. }
  592. }
  593. }
  594. else if ( m_LayerType == SUBLAYER_FONT )
  595. {
  596. int listIndex = -1;
  597. int layerIndex = -1;
  598. layerIndex = renderLists.AddToTail();
  599. listIndex = renderLists[layerIndex].m_RenderGeometryLists.AddToTail();
  600. renderLists[layerIndex].m_LayerType = m_LayerType;
  601. renderLists[layerIndex].m_pMaterial = m_Material;
  602. renderLists[layerIndex].m_pSheet = m_Sheet;
  603. renderLists[layerIndex].m_hTexture = m_hTexture;
  604. CUtlVector< RenderGeometryList_t > &renderGeometryLists = renderLists[layerIndex].m_RenderGeometryLists;
  605. Assert( layerIndex != -1 );
  606. Assert( listIndex != -1 );
  607. for ( int i = 0; i < nGraphicsCount; ++i )
  608. {
  609. // Groups should never be in lists of layer graphics.
  610. Assert( !m_LayerGraphics[i]->IsGroup() );
  611. CGraphicGroup *pGroup = m_LayerGraphics[i]->GetGroup();
  612. Assert( pGroup );
  613. m_LayerGraphics[i]->UpdateRenderData( pGroup->GetResultantColor(), renderGeometryLists, listIndex );
  614. }
  615. if ( bDrawExtents )
  616. {
  617. // Find the static material and sheet
  618. for ( int i = 0; i < renderLists.Count(); ++i )
  619. {
  620. if ( renderLists[i].m_LayerType == SUBLAYER_STATIC )
  621. {
  622. // Make another layer for rendering extents
  623. // Only do this if there is a static texture around to use for rendering.
  624. layerIndex = renderLists.AddToTail();
  625. listIndex = renderLists[layerIndex].m_RenderGeometryLists.AddToTail();
  626. renderLists[layerIndex].m_LayerType = SUBLAYER_STATIC; // must be static since these render as triangles.
  627. renderLists[layerIndex].m_pMaterial = renderLists[i].m_pMaterial;
  628. renderLists[layerIndex].m_pSheet = renderLists[i].m_pSheet;
  629. renderLists[layerIndex].m_hTexture = renderLists[i].m_hTexture;
  630. CUtlVector< RenderGeometryList_t > &renderGeometryLists2 = renderLists[layerIndex].m_RenderGeometryLists;
  631. Assert( layerIndex != -1 );
  632. Assert( listIndex != -1 );
  633. for ( int i = 0; i < nGraphicsCount; ++i )
  634. {
  635. CGameText *pText = dynamic_cast<CGameText *>( m_LayerGraphics[i] );
  636. Assert( pText );
  637. pText->DrawExtents( renderGeometryLists2, listIndex );
  638. }
  639. break;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. //-----------------------------------------------------------------------------
  646. // Given a position, return the front most graphic under it.
  647. //-----------------------------------------------------------------------------
  648. CGameGraphic *CGameLayer::GetGraphic( int x, int y )
  649. {
  650. int nGraphicCount = m_LayerGraphics.Count();
  651. for ( int i = nGraphicCount-1; i >= 0; --i )
  652. {
  653. CGameGraphic *pGraphic = m_LayerGraphics[i];
  654. if ( pGraphic->HitTest( x, y ) )
  655. return pGraphic;
  656. }
  657. return NULL;
  658. }
  659. //-----------------------------------------------------------------------------
  660. // Given a position, return the front most graphic that can take input under it.
  661. //-----------------------------------------------------------------------------
  662. CGameGraphic *CGameLayer::GetMouseFocus( int x, int y )
  663. {
  664. int nGraphicCount = m_LayerGraphics.Count();
  665. for ( int i = nGraphicCount-1; i >= 0; --i )
  666. {
  667. CGameGraphic *pGraphic = m_LayerGraphics[i];
  668. if ( pGraphic->CanAcceptInput() && pGraphic->HitTest( x, y ) )
  669. return pGraphic;
  670. }
  671. return NULL;
  672. }
  673. //-----------------------------------------------------------------------------
  674. // Given a position, return the front most graphic that can take input under it.
  675. //-----------------------------------------------------------------------------
  676. CGameGraphic *CGameLayer::GetNextFocus( bool &bGetNext, CGameGraphic *pCurrentGraphic )
  677. {
  678. int nGraphicCount = m_LayerGraphics.Count();
  679. for ( int i = 0; i < nGraphicCount; ++i )
  680. {
  681. CGameGraphic *pGraphic = m_LayerGraphics[i];
  682. if ( bGetNext && pGraphic->CanAcceptInput() )
  683. {
  684. return pGraphic;
  685. }
  686. if ( pCurrentGraphic == pGraphic )
  687. {
  688. bGetNext = true;
  689. }
  690. }
  691. return NULL;
  692. }
  693. //-----------------------------------------------------------------------------
  694. // Given a name of a graphic, find it in this layer
  695. //-----------------------------------------------------------------------------
  696. CGameGraphic *CGameLayer::FindGraphicByName( const char *pName )
  697. {
  698. int nGraphicCount = m_LayerGraphics.Count();
  699. for ( int i = 0; i < nGraphicCount; ++i )
  700. {
  701. CGameGraphic *pGraphic = m_LayerGraphics[i]->FindGraphicByName( pName );
  702. if ( pGraphic )
  703. {
  704. // Match.
  705. return pGraphic;
  706. }
  707. }
  708. return NULL;
  709. }