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.

172 lines
4.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a panel which is rendered image on top of an entity
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "teambitmapimage.h"
  10. #include <KeyValues.h>
  11. #include "vgui_BitmapImage.h"
  12. #include "PanelMetaClassMgr.h"
  13. #include "vguimatsurface/IMatSystemSurface.h"
  14. #include <vgui_controls/Panel.h>
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. //-----------------------------------------------------------------------------
  18. // A multiplexer bitmap that chooses a bitmap based on team
  19. //-----------------------------------------------------------------------------
  20. CTeamBitmapImage::CTeamBitmapImage() : m_Alpha(1.0f)
  21. {
  22. memset( m_ppImage, 0, BITMAP_COUNT * sizeof(BitmapImage*) );
  23. m_pEntity = NULL;
  24. m_bRelativeTeams = 0;
  25. }
  26. CTeamBitmapImage::~CTeamBitmapImage()
  27. {
  28. int i;
  29. for ( i = 0; i < BITMAP_COUNT; ++i )
  30. {
  31. if (m_ppImage[i])
  32. delete m_ppImage[i];
  33. }
  34. }
  35. //-----------------------------------------------------------------------------
  36. // initialization
  37. //-----------------------------------------------------------------------------
  38. bool CTeamBitmapImage::Init( vgui::Panel *pParent, KeyValues* pInitData, C_BaseEntity* pEntity )
  39. {
  40. static char *pRelativeTeamNames[BITMAP_COUNT] =
  41. {
  42. "NoTeam",
  43. "MyTeam",
  44. "EnemyTeam",
  45. };
  46. static char *pAbsoluteTeamNames[BITMAP_COUNT] =
  47. {
  48. "Team0",
  49. "Team1",
  50. "Team2",
  51. };
  52. m_pEntity = pEntity;
  53. m_bRelativeTeams = (pInitData->GetInt( "relativeteam" ) != 0);
  54. char **ppTeamNames = m_bRelativeTeams ? pRelativeTeamNames : pAbsoluteTeamNames;
  55. int i;
  56. for ( i = 0 ; i < BITMAP_COUNT; ++i )
  57. {
  58. // Default to null
  59. m_ppImage[i] = NULL;
  60. // Look for team section
  61. KeyValues *pTeamKV = pInitData->FindKey( ppTeamNames[i] );
  62. if ( !pTeamKV )
  63. continue;
  64. char const* pClassImage = pTeamKV->GetString( "material" );
  65. if ( !pClassImage || !pClassImage[ 0 ] )
  66. return false;
  67. // modulation color
  68. Color color;
  69. if (!ParseRGBA( pTeamKV, "color", color ))
  70. color.SetColor( 255, 255, 255, 255 );
  71. // hook in the bitmap
  72. m_ppImage[i] = new BitmapImage( pParent->GetVPanel(), pClassImage );
  73. m_ppImage[i]->SetColor( color );
  74. }
  75. return true;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Alpha modulate...
  79. //-----------------------------------------------------------------------------
  80. void CTeamBitmapImage::SetAlpha( float alpha )
  81. {
  82. m_Alpha = clamp( alpha, 0.0f, 1.0f );
  83. }
  84. //-----------------------------------------------------------------------------
  85. // draw
  86. //-----------------------------------------------------------------------------
  87. void CTeamBitmapImage::Paint( float yaw /*= 0.0f*/ )
  88. {
  89. if (m_Alpha == 0.0f)
  90. return;
  91. int team = 0;
  92. if (m_bRelativeTeams)
  93. {
  94. if (GetEntity())
  95. {
  96. if (GetEntity()->GetTeamNumber() != 0)
  97. {
  98. team = GetEntity()->InLocalTeam() ? 1 : 2;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. if (GetEntity())
  105. team = GetEntity()->GetTeamNumber();
  106. }
  107. // Paint the image for the current team
  108. if (m_ppImage[team])
  109. {
  110. // Modulate the color based on the alpha....
  111. Color color = m_ppImage[team]->GetColor();
  112. int alpha = color[3];
  113. color[3] = (alpha * m_Alpha);
  114. m_ppImage[team]->SetColor( color );
  115. if ( yaw != 0.0f )
  116. {
  117. g_pMatSystemSurface->DisableClipping( true );
  118. m_ppImage[team]->DoPaint( m_ppImage[team]->GetRenderSizePanel(), yaw );
  119. g_pMatSystemSurface->DisableClipping( false );
  120. }
  121. else
  122. {
  123. // Paint
  124. m_ppImage[team]->Paint();
  125. }
  126. // restore previous color
  127. color[3] = alpha;
  128. m_ppImage[team]->SetColor( color );
  129. }
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Helper method to initialize a team image from KeyValues data..
  133. //-----------------------------------------------------------------------------
  134. bool InitializeTeamImage( KeyValues *pInitData, const char* pSectionName, vgui::Panel *pParent, C_BaseEntity *pEntity, CTeamBitmapImage* pTeamImage )
  135. {
  136. KeyValues *pTeamImageSection = pInitData;
  137. if (pSectionName)
  138. {
  139. pTeamImageSection = pInitData->FindKey( pSectionName );
  140. if ( !pTeamImageSection )
  141. return false;
  142. }
  143. return pTeamImage->Init( pParent, pTeamImageSection, pEntity );
  144. }