Team Fortress 2 Source Code as on 22/4/2020
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.

222 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: HUD Target ID element
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "hud.h"
  9. #include "hudelement.h"
  10. #include "c_hl2mp_player.h"
  11. #include "c_playerresource.h"
  12. #include "vgui_entitypanel.h"
  13. #include "iclientmode.h"
  14. #include "vgui/ILocalize.h"
  15. #include "hl2mp_gamerules.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. #define PLAYER_HINT_DISTANCE 150
  19. #define PLAYER_HINT_DISTANCE_SQ (PLAYER_HINT_DISTANCE*PLAYER_HINT_DISTANCE)
  20. static ConVar hud_centerid( "hud_centerid", "1" );
  21. static ConVar hud_showtargetid( "hud_showtargetid", "1" );
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. class CTargetID : public CHudElement, public vgui::Panel
  26. {
  27. DECLARE_CLASS_SIMPLE( CTargetID, vgui::Panel );
  28. public:
  29. CTargetID( const char *pElementName );
  30. void Init( void );
  31. virtual void ApplySchemeSettings( vgui::IScheme *scheme );
  32. virtual void Paint( void );
  33. void VidInit( void );
  34. private:
  35. Color GetColorForTargetTeam( int iTeamNumber );
  36. vgui::HFont m_hFont;
  37. int m_iLastEntIndex;
  38. float m_flLastChangeTime;
  39. };
  40. DECLARE_HUDELEMENT( CTargetID );
  41. using namespace vgui;
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. CTargetID::CTargetID( const char *pElementName ) :
  46. CHudElement( pElementName ), BaseClass( NULL, "TargetID" )
  47. {
  48. vgui::Panel *pParent = g_pClientMode->GetViewport();
  49. SetParent( pParent );
  50. m_hFont = g_hFontTrebuchet24;
  51. m_flLastChangeTime = 0;
  52. m_iLastEntIndex = 0;
  53. SetHiddenBits( HIDEHUD_MISCSTATUS );
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose: Setup
  57. //-----------------------------------------------------------------------------
  58. void CTargetID::Init( void )
  59. {
  60. };
  61. void CTargetID::ApplySchemeSettings( vgui::IScheme *scheme )
  62. {
  63. BaseClass::ApplySchemeSettings( scheme );
  64. m_hFont = scheme->GetFont( "TargetID", IsProportional() );
  65. SetPaintBackgroundEnabled( false );
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: clear out string etc between levels
  69. //-----------------------------------------------------------------------------
  70. void CTargetID::VidInit()
  71. {
  72. CHudElement::VidInit();
  73. m_flLastChangeTime = 0;
  74. m_iLastEntIndex = 0;
  75. }
  76. Color CTargetID::GetColorForTargetTeam( int iTeamNumber )
  77. {
  78. return GameResources()->GetTeamColor( iTeamNumber );
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Draw function for the element
  82. //-----------------------------------------------------------------------------
  83. void CTargetID::Paint()
  84. {
  85. #define MAX_ID_STRING 256
  86. wchar_t sIDString[ MAX_ID_STRING ];
  87. sIDString[0] = 0;
  88. C_HL2MP_Player *pPlayer = C_HL2MP_Player::GetLocalHL2MPPlayer();
  89. if ( !pPlayer )
  90. return;
  91. Color c;
  92. // Get our target's ent index
  93. int iEntIndex = pPlayer->GetIDTarget();
  94. // Didn't find one?
  95. if ( !iEntIndex )
  96. {
  97. // Check to see if we should clear our ID
  98. if ( m_flLastChangeTime && (gpGlobals->curtime > (m_flLastChangeTime + 0.5)) )
  99. {
  100. m_flLastChangeTime = 0;
  101. sIDString[0] = 0;
  102. m_iLastEntIndex = 0;
  103. }
  104. else
  105. {
  106. // Keep re-using the old one
  107. iEntIndex = m_iLastEntIndex;
  108. }
  109. }
  110. else
  111. {
  112. m_flLastChangeTime = gpGlobals->curtime;
  113. }
  114. // Is this an entindex sent by the server?
  115. if ( iEntIndex )
  116. {
  117. C_BasePlayer *pPlayer = static_cast<C_BasePlayer*>(cl_entitylist->GetEnt( iEntIndex ));
  118. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  119. const char *printFormatString = NULL;
  120. wchar_t wszPlayerName[ MAX_PLAYER_NAME_LENGTH ];
  121. wchar_t wszHealthText[ 10 ];
  122. bool bShowHealth = false;
  123. bool bShowPlayerName = false;
  124. // Some entities we always want to check, cause the text may change
  125. // even while we're looking at it
  126. // Is it a player?
  127. if ( IsPlayerIndex( iEntIndex ) )
  128. {
  129. c = GetColorForTargetTeam( pPlayer->GetTeamNumber() );
  130. bShowPlayerName = true;
  131. g_pVGuiLocalize->ConvertANSIToUnicode( pPlayer->GetPlayerName(), wszPlayerName, sizeof(wszPlayerName) );
  132. if ( HL2MPRules()->IsTeamplay() == true && pPlayer->InSameTeam(pLocalPlayer) )
  133. {
  134. printFormatString = "#Playerid_sameteam";
  135. bShowHealth = true;
  136. }
  137. else
  138. {
  139. printFormatString = "#Playerid_diffteam";
  140. }
  141. if ( bShowHealth )
  142. {
  143. _snwprintf( wszHealthText, ARRAYSIZE(wszHealthText) - 1, L"%.0f%%", ((float)pPlayer->GetHealth() / (float)pPlayer->GetMaxHealth() ) );
  144. wszHealthText[ ARRAYSIZE(wszHealthText)-1 ] = '\0';
  145. }
  146. }
  147. if ( printFormatString )
  148. {
  149. if ( bShowPlayerName && bShowHealth )
  150. {
  151. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 2, wszPlayerName, wszHealthText );
  152. }
  153. else if ( bShowPlayerName )
  154. {
  155. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 1, wszPlayerName );
  156. }
  157. else if ( bShowHealth )
  158. {
  159. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 1, wszHealthText );
  160. }
  161. else
  162. {
  163. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 0 );
  164. }
  165. }
  166. if ( sIDString[0] )
  167. {
  168. int wide, tall;
  169. int ypos = YRES(260);
  170. int xpos = XRES(10);
  171. vgui::surface()->GetTextSize( m_hFont, sIDString, wide, tall );
  172. if( hud_centerid.GetInt() == 0 )
  173. {
  174. ypos = YRES(420);
  175. }
  176. else
  177. {
  178. xpos = (ScreenWidth() - wide) / 2;
  179. }
  180. vgui::surface()->DrawSetTextFont( m_hFont );
  181. vgui::surface()->DrawSetTextPos( xpos, ypos );
  182. vgui::surface()->DrawSetTextColor( c );
  183. vgui::surface()->DrawPrintText( sIDString, wcslen(sIDString) );
  184. }
  185. }
  186. }