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.

244 lines
6.9 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 "vgui_entityimagepanel.h"
  10. #include <KeyValues.h>
  11. #include "c_BaseTFPlayer.h"
  12. #include <vgui/IVGui.h>
  13. #include "vgui_BitmapImage.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. //-----------------------------------------------------------------------------
  17. // Class factory
  18. //-----------------------------------------------------------------------------
  19. DECLARE_OVERLAY_FACTORY( CEntityImagePanel, "image" );
  20. //-----------------------------------------------------------------------------
  21. // Constructor
  22. //-----------------------------------------------------------------------------
  23. CEntityImagePanel::CEntityImagePanel( vgui::Panel *pParent, const char *panelName ) :
  24. BaseClass( pParent, panelName ), m_pImage(0)
  25. {
  26. SetPaintBackgroundEnabled( false );
  27. }
  28. CEntityImagePanel::~CEntityImagePanel()
  29. {
  30. if (m_pImage)
  31. delete m_pImage;
  32. }
  33. //-----------------------------------------------------------------------------
  34. // initialization
  35. //-----------------------------------------------------------------------------
  36. bool CEntityImagePanel::Init( KeyValues* pInitData, C_BaseEntity* pEntity )
  37. {
  38. if (!BaseClass::Init( pInitData, pEntity))
  39. return false;
  40. // modulation color
  41. if (!ParseRGBA( pInitData, "color", m_r, m_g, m_b, m_a ))
  42. return false;
  43. // get the size...
  44. int w, h;
  45. if (!ParseCoord( pInitData, "offset", m_OffsetX, m_OffsetY ))
  46. return false;
  47. if (!ParseCoord( pInitData, "size", w, h ))
  48. return false;
  49. char const* pClassImage = pInitData->GetString( "material" );
  50. if ( !pClassImage || !pClassImage[ 0 ] )
  51. return false;
  52. const char *mouseover = pInitData->GetString( "mousehint", "" );
  53. if ( mouseover && mouseover[ 0 ] )
  54. {
  55. Q_strncpy( m_szMouseOverText, mouseover, sizeof( m_szMouseOverText ) );
  56. }
  57. // hook in the bitmap
  58. m_pImage = new BitmapImage( GetVPanel(), pClassImage );
  59. // Set the size...
  60. SetSize( w, h );
  61. m_iOrgWidth = w;
  62. m_iOrgHeight = h;
  63. m_iOrgOffsetX = m_OffsetX;
  64. m_iOrgOffsetY = m_OffsetY;
  65. // we need updating
  66. vgui::ivgui()->AddTickSignal( GetVPanel() );
  67. OnTick();
  68. return true;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Should we draw?.
  72. //-----------------------------------------------------------------------------
  73. bool CEntityImagePanel::ShouldDraw()
  74. {
  75. return ( IsLocalPlayerInTactical() || m_bShowInNormal );
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Draws the puppy
  79. //-----------------------------------------------------------------------------
  80. void CEntityImagePanel::Paint( void )
  81. {
  82. // Don't draw if I'm not visible in the tactical map
  83. if ( MapData().IsEntityVisibleToTactical( GetEntity() ) == false )
  84. return;
  85. vgui::surface()->DrawSetColor( m_r, m_g, m_b, m_a );
  86. if ( !m_pImage )
  87. return;
  88. Color color;
  89. color.SetColor( m_r, m_g, m_b, m_a );
  90. m_pImage->SetColor( color );
  91. m_pImage->DoPaint( GetVPanel() );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Class factory CEntityTeamImagePanel
  95. //-----------------------------------------------------------------------------
  96. DECLARE_OVERLAY_FACTORY( CEntityTeamImagePanel, "team_image" );
  97. //-----------------------------------------------------------------------------
  98. // Purpose:
  99. // Constructor: Same as image panel, except can handle team specific colors/images
  100. // Input : pEntity -
  101. //-----------------------------------------------------------------------------
  102. CEntityTeamImagePanel::CEntityTeamImagePanel( vgui::Panel *pParent, const char *panelName ) :
  103. BaseClass( pParent, panelName )
  104. {
  105. SetPaintBackgroundEnabled( false );
  106. memset( m_Images, 0, sizeof( m_Images ) );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose:
  110. //-----------------------------------------------------------------------------
  111. CEntityTeamImagePanel::~CEntityTeamImagePanel( void )
  112. {
  113. for ( int i = 0 ; i < MAX_TEAMS; i++ )
  114. {
  115. if ( m_Images[i].m_pImage )
  116. {
  117. delete m_Images[ i ].m_pImage;
  118. }
  119. }
  120. memset( m_Images, 0, sizeof( m_Images ) );
  121. }
  122. //-----------------------------------------------------------------------------
  123. // initialization
  124. //-----------------------------------------------------------------------------
  125. bool CEntityTeamImagePanel::Init( KeyValues* pInitData, C_BaseEntity* pEntity )
  126. {
  127. if (!BaseClass::Init( pInitData, pEntity))
  128. return false;
  129. if ( pInitData->GetInt( "showinnormalmode", 0 ) )
  130. {
  131. m_bShowInNormal = true;
  132. }
  133. // get the size...
  134. int w, h;
  135. if (!ParseCoord( pInitData, "offset", m_OffsetX, m_OffsetY ))
  136. return false;
  137. if (!ParseCoord( pInitData, "size", w, h ))
  138. return false;
  139. // Set the size...
  140. SetSize( w, h );
  141. m_iOrgWidth = w;
  142. m_iOrgHeight = h;
  143. m_iOrgOffsetX = m_OffsetX;
  144. m_iOrgOffsetY = m_OffsetY;
  145. const char *mouseover = pInitData->GetString( "mousehint", "" );
  146. if ( mouseover && mouseover[ 0 ] )
  147. {
  148. Q_strncpy( m_szMouseOverText, mouseover, sizeof( m_szMouseOverText ) );
  149. }
  150. for ( int i = 0 ; i < MAX_TEAMS; i++ )
  151. {
  152. char teamname[ 32 ];
  153. Q_snprintf( teamname, sizeof( teamname ), "Team%i", i );
  154. memset( &m_Images[ i ], 0, sizeof( m_Images[ i ] ) );
  155. // Look for team section
  156. KeyValues *pTeamKV = pInitData->FindKey( teamname );
  157. if ( !pTeamKV )
  158. continue;
  159. // modulation color
  160. if (!ParseRGBA( pTeamKV, "color", m_Images[i].m_r, m_Images[i].m_g, m_Images[i].m_b, m_Images[i].m_a ))
  161. return false;
  162. char const* pClassImage = pTeamKV->GetString( "material" );
  163. if ( !pClassImage || !pClassImage[ 0 ] )
  164. return false;
  165. // hook in the bitmap
  166. m_Images[ i ].m_pImage = new BitmapImage( GetVPanel(), pClassImage );
  167. }
  168. // we need updating
  169. vgui::ivgui()->AddTickSignal( GetVPanel() );
  170. return true;
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Draws the puppy
  174. //-----------------------------------------------------------------------------
  175. void CEntityTeamImagePanel::Paint( void )
  176. {
  177. // Determine team index of underlying entity
  178. int teamnumber = GetEntity()->GetTeamNumber();
  179. if ( teamnumber < 0 || teamnumber >= MAX_TEAMS )
  180. {
  181. Assert( 0 );
  182. return;
  183. }
  184. if ( !m_Images[ teamnumber ].m_pImage )
  185. return;
  186. // Don't draw if I'm not visible in the tactical map
  187. if ( MapData().IsEntityVisibleToTactical( GetEntity() ) == false )
  188. return;
  189. ComputeAndSetSize();
  190. vgui::surface()->DrawSetColor( m_Images[ teamnumber ].m_r, m_Images[ teamnumber ].m_g, m_Images[ teamnumber ].m_b, m_Images[ teamnumber ].m_b );
  191. Color color;
  192. color.SetColor( m_Images[ teamnumber ].m_r, m_Images[ teamnumber ].m_g, m_Images[ teamnumber ].m_b, m_Images[ teamnumber ].m_b );
  193. m_Images[ teamnumber ].m_pImage->SetColor( color );
  194. m_Images[ teamnumber ].m_pImage->DoPaint( GetVPanel() );
  195. }