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.

297 lines
9.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "gamerect.h"
  8. // To handle scaling
  9. #include "materialsystem/imaterialsystem.h"
  10. #include "animdata.h"
  11. #include "Color.h"
  12. #include "gameuisystemmgr.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. BEGIN_DMXELEMENT_UNPACK ( CGameRect )
  16. DMXELEMENT_UNPACK_FIELD_UTLSTRING( "name", "", m_pName )
  17. DMXELEMENT_UNPACK_FIELD( "center", "0 0", Vector2D, m_Geometry.m_Center )
  18. DMXELEMENT_UNPACK_FIELD( "scale", "0 0", Vector2D, m_Geometry.m_Scale )
  19. DMXELEMENT_UNPACK_FIELD( "rotation", "0", float, m_Geometry.m_Rotation )
  20. DMXELEMENT_UNPACK_FIELD( "maintainaspectratio", "0", bool, m_Geometry.m_bMaintainAspectRatio )
  21. DMXELEMENT_UNPACK_FIELD( "sublayertype", "0", int, m_Geometry.m_Sublayer )
  22. DMXELEMENT_UNPACK_FIELD( "visible", "1", bool, m_Geometry.m_bVisible )
  23. DMXELEMENT_UNPACK_FIELD( "initialstate", "-1", int, m_CurrentState )
  24. DMXELEMENT_UNPACK_FIELD( "horizgradient", "0", bool, m_Geometry.m_bHorizontalGradient )
  25. DMXELEMENT_UNPACK_FIELD( "color", "255 255 255 255", Color, m_Geometry.m_Color )
  26. DMXELEMENT_UNPACK_FIELD( "topcolor", "255 255 255 255", Color, m_Geometry.m_TopColor )
  27. DMXELEMENT_UNPACK_FIELD( "bottomcolor", "255 255 255 255", Color, m_Geometry.m_BottomColor )
  28. // color is gotten from log.
  29. // sheet seq number is gotten from log.
  30. END_DMXELEMENT_UNPACK( CGameRect, s_GameRectUnpack )
  31. //-----------------------------------------------------------------------------
  32. // Constructor, destructor
  33. //-----------------------------------------------------------------------------
  34. CGameRect::CGameRect( const char *pName )
  35. {
  36. m_Geometry.m_SheetSequenceNumber = 0; // FIXME, not updating seq numbers yet.
  37. m_bCanAcceptInput = false;
  38. // DME default values.
  39. m_pName = pName;
  40. m_Geometry.m_Center.x = 0;
  41. m_Geometry.m_Center.y = 0;
  42. m_Geometry.m_Scale.x = 0;
  43. m_Geometry.m_Scale.y = 0;
  44. m_Geometry.m_Rotation = 0;
  45. m_Geometry.m_bMaintainAspectRatio = 0;
  46. m_Geometry.m_Sublayer = 0;
  47. m_Geometry.m_bVisible = true;
  48. m_CurrentState = -1;
  49. m_Geometry.m_bHorizontalGradient = false;
  50. m_Geometry.m_Color.r = 255;
  51. m_Geometry.m_Color.g = 255;
  52. m_Geometry.m_Color.b = 255;
  53. m_Geometry.m_Color.a = 255;
  54. m_Geometry.m_TopColor.r = 255;
  55. m_Geometry.m_TopColor.g = 255;
  56. m_Geometry.m_TopColor.b = 255;
  57. m_Geometry.m_TopColor.a = 255;
  58. m_Geometry.m_BottomColor.r = 255;
  59. m_Geometry.m_BottomColor.g = 255;
  60. m_Geometry.m_BottomColor.b = 255;
  61. m_Geometry.m_BottomColor.a = 255;
  62. m_Geometry.m_RelativePositions.AddToTail( Vector2D( -.5, -.5 ) );
  63. m_Geometry.m_RelativePositions.AddToTail( Vector2D( .5, -.5 ) );
  64. m_Geometry.m_RelativePositions.AddToTail( Vector2D( .5, .5 ) );
  65. m_Geometry.m_RelativePositions.AddToTail( Vector2D( -.5, .5 ) );
  66. m_Geometry.m_TextureCoords.AddToTail( Vector2D( 0.0, 0.0 ) );
  67. m_Geometry.m_TextureCoords.AddToTail( Vector2D( 1.0, 0.0 ) );
  68. m_Geometry.m_TextureCoords.AddToTail( Vector2D( 1.0, 1.0 ) );
  69. m_Geometry.m_TextureCoords.AddToTail( Vector2D( 0.0, 1.0 ) );
  70. SetupVertexColors();
  71. CTriangle triangle;
  72. triangle.m_PointIndex[0] = 0;
  73. triangle.m_PointIndex[1] = 1;
  74. triangle.m_PointIndex[2] = 2;
  75. m_Geometry.m_Triangles.AddToTail( triangle );
  76. triangle.m_PointIndex[0] = 0;
  77. triangle.m_PointIndex[1] = 2;
  78. triangle.m_PointIndex[2] = 3;
  79. m_Geometry.m_Triangles.AddToTail( triangle );
  80. }
  81. CGameRect::~CGameRect()
  82. {
  83. }
  84. //-----------------------------------------------------------------------------
  85. //
  86. //-----------------------------------------------------------------------------
  87. bool CGameRect::Unserialize( CDmxElement *pGraphic )
  88. {
  89. pGraphic->UnpackIntoStructure( this, s_GameRectUnpack );
  90. // GEOMETRY
  91. CDmxAttribute *pRelativePositions = pGraphic->GetAttribute( "relativepositions" );
  92. if ( !pRelativePositions || pRelativePositions->GetType() != AT_VECTOR2_ARRAY )
  93. {
  94. return false;
  95. }
  96. const CUtlVector< Vector2D > &relpositions = pRelativePositions->GetArray< Vector2D >( );
  97. int nCount = relpositions.Count();
  98. m_Geometry.m_RelativePositions.RemoveAll();
  99. for ( int i = 0; i < nCount; ++i )
  100. {
  101. m_Geometry.m_RelativePositions.AddToTail( Vector2D( relpositions[i].x, relpositions[i].y ) );
  102. }
  103. CDmxAttribute *pTexCoords = pGraphic->GetAttribute( "texcoords" );
  104. if ( !pTexCoords || pTexCoords->GetType() != AT_VECTOR2_ARRAY )
  105. {
  106. return false;
  107. }
  108. const CUtlVector< Vector2D > &texcoords = pTexCoords->GetArray< Vector2D >( );
  109. nCount = texcoords.Count();
  110. m_Geometry.m_TextureCoords.RemoveAll();
  111. for ( int i = 0; i < nCount; ++i )
  112. {
  113. m_Geometry.m_TextureCoords.AddToTail( Vector2D( texcoords[i].x, texcoords[i].y ) );
  114. }
  115. SetupVertexColors();
  116. CDmxAttribute *pTriangles = pGraphic->GetAttribute( "triangles" );
  117. if ( !pTriangles || pTriangles->GetType() != AT_ELEMENT_ARRAY )
  118. {
  119. return false;
  120. }
  121. const CUtlVector< CDmxElement * > &triangles = pTriangles->GetArray< CDmxElement * >( );
  122. nCount = triangles.Count();
  123. m_Geometry.m_Triangles.RemoveAll();
  124. for ( int i = 0; i < nCount; ++i )
  125. {
  126. CDmxAttribute *pPoints = triangles[i]->GetAttribute( "positionindexes" );
  127. const CUtlVector< int > &points = pPoints->GetArray< int >( );
  128. CTriangle triangle;
  129. triangle.m_PointIndex[0] = points[0];
  130. triangle.m_PointIndex[1] = points[1];
  131. triangle.m_PointIndex[2] = points[2];
  132. m_Geometry.m_Triangles.AddToTail( triangle );
  133. }
  134. // ANIMSTATES
  135. CDmxAttribute *pImageAnims = pGraphic->GetAttribute( "imageanims" );
  136. if ( !pImageAnims || pImageAnims->GetType() != AT_ELEMENT_ARRAY )
  137. {
  138. return false;
  139. }
  140. const CUtlVector< CDmxElement * > &imageanims = pImageAnims->GetArray< CDmxElement * >( );
  141. nCount = imageanims.Count();
  142. for ( int i = 0; i < nCount; ++i )
  143. {
  144. CAnimData *pAnimData = new CAnimData;
  145. if ( !pAnimData->Unserialize( imageanims[i] ) )
  146. {
  147. delete pAnimData;
  148. return false;
  149. }
  150. m_Anims.AddToTail( pAnimData );
  151. }
  152. // Ok the initial state is 0, which is (usually ) default.
  153. // default could be aliased to another state though so if it is fix the initial state here.
  154. // default might also not be the state that is 0 so this sets the graphic's initial
  155. // state to be the default one.
  156. SetState( "default" );
  157. return true;
  158. }
  159. //-----------------------------------------------------------------------------
  160. //
  161. //-----------------------------------------------------------------------------
  162. void CGameRect::UpdateGeometry()
  163. {
  164. if ( m_CurrentState == -1 )
  165. return;
  166. Assert( m_CurrentState < m_Anims.Count() );
  167. DmeTime_t flAnimTime = GetAnimationTimePassed();
  168. // Update texture
  169. m_Geometry.m_SheetSequenceNumber = m_Anims[ m_CurrentState ]->m_TextureAnimSheetSeqNumber;
  170. m_Geometry.m_AnimationRate = m_Anims[ m_CurrentState ]->m_AnimationRate;
  171. // Update color
  172. m_Anims[ m_CurrentState ]->m_ColorAnim.GetValue( flAnimTime, &m_Geometry.m_Color );
  173. // Update center location
  174. m_Anims[ m_CurrentState ]->m_CenterPosAnim.GetValue( flAnimTime, &m_Geometry.m_Center );
  175. // Update scale
  176. m_Anims[ m_CurrentState ]->m_ScaleAnim.GetValue( flAnimTime, &m_Geometry.m_Scale );
  177. // Update rotation
  178. m_Anims[ m_CurrentState ]->m_RotationAnim.GetValue( flAnimTime, &m_Geometry.m_Rotation );
  179. }
  180. //-----------------------------------------------------------------------------
  181. //
  182. //-----------------------------------------------------------------------------
  183. void CGameRect::UpdateRenderData( color32 parentColor, CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  184. {
  185. if ( !m_Geometry.m_bVisible )
  186. return;
  187. m_Geometry.SetResultantColor( parentColor );
  188. m_Geometry.UpdateRenderData( renderGeometryLists, firstListIndex );
  189. // Now transform our array of positions into local graphic coord system.
  190. int nCount = m_Geometry.m_RelativePositions.Count();
  191. m_ScreenPositions.RemoveAll();
  192. for ( int i = 0; i < nCount; ++i )
  193. {
  194. // Position
  195. Vector relativePosition( m_Geometry.m_RelativePositions[i].x, m_Geometry.m_RelativePositions[i].y, 0 );
  196. Vector screenpos;
  197. VectorTransform( relativePosition, m_Geometry.m_RenderToScreen, screenpos );
  198. m_ScreenPositions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );
  199. }
  200. }
  201. //-----------------------------------------------------------------------------
  202. //
  203. //-----------------------------------------------------------------------------
  204. void CGameRect::SetupVertexColors()
  205. {
  206. m_Geometry.m_VertexColors.RemoveAll();
  207. // Create 4 vertex colors for this rect.
  208. color32 c;
  209. c.r = 255;
  210. c.g = 255;
  211. c.b = 255;
  212. c.a = 255;
  213. m_Geometry.m_VertexColors.AddToTail( c );
  214. m_Geometry.m_VertexColors.AddToTail( c );
  215. m_Geometry.m_VertexColors.AddToTail( c );
  216. m_Geometry.m_VertexColors.AddToTail( c );
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Determine if x,y is inside the graphic.
  220. //-----------------------------------------------------------------------------
  221. bool CGameRect::HitTest( int x, int y )
  222. {
  223. if ( !m_Geometry.m_bVisible )
  224. return false;
  225. if ( m_ScreenPositions.Count() == 0 )
  226. return false;
  227. for ( int i = 0; i < m_Geometry.GetTriangleCount(); ++i )
  228. {
  229. if ( PointTriangleHitTest(
  230. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[0] ],
  231. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[1] ],
  232. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[2] ],
  233. Vector2D( x, y ) ) )
  234. {
  235. //Msg( "%d, %d hit\n", x, y );
  236. return true;
  237. }
  238. }
  239. return false;
  240. }