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.

174 lines
5.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "dynamicrect.h"
  8. // To handle scaling
  9. #include "materialsystem/imaterialsystem.h"
  10. #include "animdata.h"
  11. #include "Color.h"
  12. #include "gameuisystemmgr.h"
  13. #include "gameuidefinition.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. // Class factory for scripting.
  17. class CDynamicRectClassFactory : IGameUIGraphicClassFactory
  18. {
  19. public:
  20. CDynamicRectClassFactory()
  21. {
  22. Assert( g_pGameUISystemMgrImpl );
  23. g_pGameUISystemMgrImpl->RegisterGraphicClassFactory( "rect", this );
  24. }
  25. // Returns an instance of a graphic interface (keyvalues owned by caller)
  26. virtual CGameGraphic *CreateNewGraphicClass( KeyValues *kvRequest, CGameUIDefinition *pMenu )
  27. {
  28. Assert( pMenu );
  29. CDynamicRect *pNewGraphic = NULL;
  30. const char *pName = kvRequest->GetString( "name", NULL );
  31. if ( pName )
  32. {
  33. pNewGraphic = new CDynamicRect( pName );
  34. // Rects are normally 0,0, doing this so we can see script created rects.
  35. pNewGraphic->SetScale( 100, 100 );
  36. pMenu->AddGraphicToLayer( pNewGraphic, SUBLAYER_DYNAMIC );
  37. // Now set the attributes.
  38. for ( KeyValues *arg = kvRequest->GetFirstSubKey(); arg != NULL; arg = arg->GetNextKey() )
  39. {
  40. pNewGraphic->HandleScriptCommand( arg );
  41. }
  42. }
  43. return pNewGraphic;
  44. }
  45. };
  46. static CDynamicRectClassFactory g_CDynamicRectClassFactory;
  47. BEGIN_DMXELEMENT_UNPACK ( CDynamicRect )
  48. DMXELEMENT_UNPACK_FIELD_UTLSTRING( "imagealias", "", m_ImageAlias )
  49. END_DMXELEMENT_UNPACK( CDynamicRect, s_GameDynamicRectUnpack )
  50. //-----------------------------------------------------------------------------
  51. // Constructor, destructor
  52. //-----------------------------------------------------------------------------
  53. CDynamicRect::CDynamicRect( const char *pName ) : CGameRect( pName )
  54. {
  55. // The default maps to white pixel.
  56. m_ImageAlias = "defaultImageAlias";
  57. }
  58. CDynamicRect::~CDynamicRect()
  59. {
  60. g_pGameUISystemMgrImpl->ReleaseImageAlias( m_ImageAlias );
  61. }
  62. //-----------------------------------------------------------------------------
  63. //
  64. //-----------------------------------------------------------------------------
  65. bool CDynamicRect::Unserialize( CDmxElement *pGraphic )
  66. {
  67. CGameRect::Unserialize( pGraphic );
  68. pGraphic->UnpackIntoStructure( this, s_GameDynamicRectUnpack );
  69. m_CurrentState = -1;
  70. g_pGameUISystemMgrImpl->InitImageAlias( m_ImageAlias );
  71. g_pGameUISystemMgrImpl->LoadImageAliasTexture( m_ImageAlias, "vguiedit/pixel" );
  72. return true;
  73. }
  74. //-----------------------------------------------------------------------------
  75. //
  76. //-----------------------------------------------------------------------------
  77. void CDynamicRect::UpdateRenderData( color32 parentColor, CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  78. {
  79. if ( !m_Geometry.m_bVisible )
  80. return;
  81. m_Geometry.SetResultantColor( parentColor );
  82. int i = renderGeometryLists[firstListIndex].AddToTail();
  83. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  84. // Now transform our array of positions into local graphic coord system.
  85. int nCount = m_Geometry.m_RelativePositions.Count();
  86. for ( int i = 0; i < nCount; ++i )
  87. {
  88. // Position
  89. Vector relativePosition( m_Geometry.m_RelativePositions[i].x, m_Geometry.m_RelativePositions[i].y, 0 );
  90. Vector screenpos;
  91. VectorTransform( relativePosition, m_Geometry.m_RenderToScreen, screenpos );
  92. renderGeometry.m_Positions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );;
  93. // TexCoord
  94. Vector2D sheetTexCoords;
  95. g_pGameUISystemMgrImpl->TexCoordsToSheetTexCoords( m_ImageAlias, m_Geometry.m_TextureCoords[i], sheetTexCoords );
  96. renderGeometry.m_TextureCoords.AddToTail( sheetTexCoords );
  97. // Vertex Color
  98. renderGeometry.m_VertexColors.AddToTail( m_Geometry.m_VertexColors[i] );
  99. }
  100. // Triangles
  101. nCount = m_Geometry.m_Triangles.Count();
  102. for ( int i = 0; i < nCount; ++i )
  103. {
  104. renderGeometry.m_Triangles.AddToTail( m_Geometry.m_Triangles[i] );
  105. }
  106. // Anim Info
  107. renderGeometry.m_SheetSequenceNumber = m_Geometry.m_SheetSequenceNumber;
  108. renderGeometry.m_AnimationRate = m_Geometry.m_AnimationRate;
  109. renderGeometry.m_bAnimate = m_Geometry.m_bAnimate;
  110. renderGeometry.m_AnimStartTime = m_Geometry.m_AnimStartTime;
  111. // Set the image alias. This is so we can adjust texture coords if needed if
  112. // This rect's texture gets placed in a sheet.
  113. renderGeometry.m_pImageAlias = m_ImageAlias;
  114. // Now transform our array of positions into local graphic coord system.
  115. nCount = m_Geometry.m_RelativePositions.Count();
  116. m_ScreenPositions.RemoveAll();
  117. for ( int i = 0; i < nCount; ++i )
  118. {
  119. // Position
  120. Vector relativePosition( m_Geometry.m_RelativePositions[i].x, m_Geometry.m_RelativePositions[i].y, 0 );
  121. Vector screenpos;
  122. VectorTransform( relativePosition, m_Geometry.m_RenderToScreen, screenpos );
  123. m_ScreenPositions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );
  124. }
  125. }
  126. KeyValues *CDynamicRect::HandleScriptCommand( KeyValues *args )
  127. {
  128. char const *szCommand = args->GetName();
  129. if ( !Q_stricmp( "SetAlias", szCommand ) )
  130. {
  131. g_pGameUISystemMgrImpl->ReleaseImageAlias( m_ImageAlias );
  132. m_ImageAlias = args->GetString( "alias", "defaultImageAlias" );
  133. g_pGameUISystemMgrImpl->InitImageAlias( m_ImageAlias );
  134. return NULL;
  135. }
  136. return CGameRect::HandleScriptCommand( args );
  137. }