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.

193 lines
5.4 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_dod_player.h"
  11. #include "c_playerresource.h"
  12. #include "vgui_entitypanel.h"
  13. #include "iclientmode.h"
  14. #include "vgui/ILocalize.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. #define PLAYER_HINT_DISTANCE 150
  18. #define PLAYER_HINT_DISTANCE_SQ (PLAYER_HINT_DISTANCE*PLAYER_HINT_DISTANCE)
  19. static ConVar hud_centerid( "hud_centerid", "1", FCVAR_ARCHIVE );
  20. //-----------------------------------------------------------------------------
  21. // Purpose:
  22. //-----------------------------------------------------------------------------
  23. class CTargetID : public CHudElement, public vgui::Panel
  24. {
  25. DECLARE_CLASS_SIMPLE( CTargetID, vgui::Panel );
  26. public:
  27. CTargetID( const char *pElementName );
  28. void Init( void );
  29. virtual void ApplySchemeSettings( vgui::IScheme *scheme );
  30. virtual void Paint( void );
  31. void VidInit( void );
  32. private:
  33. vgui::HFont m_hFont;
  34. int m_iLastEntIndex;
  35. float m_flLastChangeTime;
  36. };
  37. DECLARE_HUDELEMENT( CTargetID );
  38. using namespace vgui;
  39. //-----------------------------------------------------------------------------
  40. // Purpose:
  41. //-----------------------------------------------------------------------------
  42. CTargetID::CTargetID( const char *pElementName ) :
  43. CHudElement( pElementName ), BaseClass( NULL, "TargetID" )
  44. {
  45. vgui::Panel *pParent = g_pClientMode->GetViewport();
  46. SetParent( pParent );
  47. m_hFont = g_hFontTrebuchet24;
  48. m_flLastChangeTime = 0;
  49. m_iLastEntIndex = 0;
  50. SetHiddenBits( HIDEHUD_MISCSTATUS );
  51. RegisterForRenderGroup( "winpanel" );
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Setup
  55. //-----------------------------------------------------------------------------
  56. void CTargetID::Init( void )
  57. {
  58. };
  59. void CTargetID::ApplySchemeSettings( vgui::IScheme *scheme )
  60. {
  61. BaseClass::ApplySchemeSettings( scheme );
  62. m_hFont = scheme->GetFont( "TargetID", IsProportional() );
  63. SetPaintBackgroundEnabled( false );
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: clear out string etc between levels
  67. //-----------------------------------------------------------------------------
  68. void CTargetID::VidInit()
  69. {
  70. CHudElement::VidInit();
  71. m_flLastChangeTime = 0;
  72. m_iLastEntIndex = 0;
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Draw function for the element
  76. //-----------------------------------------------------------------------------
  77. void CTargetID::Paint()
  78. {
  79. #define MAX_ID_STRING 256
  80. wchar_t sIDString[ MAX_ID_STRING ];
  81. sIDString[0] = 0;
  82. C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
  83. if ( !pPlayer )
  84. return;
  85. // Get our target's ent index
  86. int iEntIndex = pPlayer->GetIDTarget();
  87. // Didn't find one?
  88. if ( !iEntIndex )
  89. {
  90. // Check to see if we should clear our ID
  91. if ( m_flLastChangeTime && (gpGlobals->curtime > (m_flLastChangeTime + 0.5)) )
  92. {
  93. m_flLastChangeTime = 0;
  94. sIDString[0] = 0;
  95. m_iLastEntIndex = 0;
  96. }
  97. else
  98. {
  99. // Keep re-using the old one
  100. iEntIndex = m_iLastEntIndex;
  101. }
  102. }
  103. else
  104. {
  105. m_flLastChangeTime = gpGlobals->curtime;
  106. }
  107. // Is this an entindex sent by the server?
  108. if ( iEntIndex )
  109. {
  110. C_BasePlayer *pPlayer = static_cast<C_BasePlayer*>(cl_entitylist->GetEnt( iEntIndex ));
  111. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  112. C_DODPlayer *pLocalDODPlayer = C_DODPlayer::GetLocalDODPlayer();
  113. wchar_t wszPlayerName[ MAX_PLAYER_NAME_LENGTH ];
  114. wchar_t wszHealthText[ 10 ];
  115. // Some entities we always want to check, cause the text may change
  116. // even while we're looking at it
  117. if ( IsPlayerIndex( iEntIndex ) && pPlayer->InSameTeam(pLocalPlayer) )
  118. {
  119. // Construct the wide char name string
  120. g_pVGuiLocalize->ConvertANSIToUnicode( pPlayer->GetPlayerName(), wszPlayerName, sizeof(wszPlayerName) );
  121. _snwprintf( wszHealthText, ARRAYSIZE(wszHealthText) - 1, L"%.0f", (float)pPlayer->GetHealth() );
  122. wszHealthText[ ARRAYSIZE(wszHealthText)-1 ] = '\0';
  123. // Construct the string to display
  124. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), L"%s1 (%s2)", 2, wszPlayerName, wszHealthText );
  125. if ( sIDString[0] )
  126. {
  127. int wide, tall;
  128. int ypos = YRES(260);
  129. int xpos = XRES(10);
  130. vgui::surface()->GetTextSize( m_hFont, sIDString, wide, tall );
  131. if( hud_centerid.GetInt() == 0 )
  132. {
  133. ypos = YRES(420);
  134. }
  135. else
  136. {
  137. xpos = (ScreenWidth() - wide) / 2;
  138. }
  139. vgui::surface()->DrawSetTextFont( m_hFont );
  140. // draw a black dropshadow ( the default one looks horrible )
  141. vgui::surface()->DrawSetTextPos( xpos+1, ypos+1 );
  142. vgui::surface()->DrawSetTextColor( Color(0,0,0,255) );
  143. vgui::surface()->DrawPrintText( sIDString, wcslen(sIDString) );
  144. vgui::surface()->DrawSetTextPos( xpos, ypos );
  145. vgui::surface()->DrawSetTextColor( g_PR->GetTeamColor( pPlayer->GetTeamNumber() ) );
  146. vgui::surface()->DrawPrintText( sIDString, wcslen(sIDString) );
  147. }
  148. pLocalDODPlayer->HintMessage( HINT_FRIEND_SEEN );
  149. }
  150. else if( IsPlayerIndex( iEntIndex ) &&
  151. pPlayer->GetTeamNumber() != TEAM_SPECTATOR &&
  152. pPlayer->GetTeamNumber() != TEAM_UNASSIGNED &&
  153. pPlayer->IsAlive() )
  154. {
  155. // must not be in the same team, enemy
  156. pLocalDODPlayer->HintMessage( HINT_ENEMY_SEEN );
  157. }
  158. }
  159. }