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.

219 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hudelement.h"
  8. #include <vgui_controls/Panel.h>
  9. #include <vgui/ISurface.h>
  10. #include "clientmode.h"
  11. #include "c_tf_player.h"
  12. #include "tf_hud_crosshair.h"
  13. #include "hud_crosshair.h"
  14. #include "materialsystem/imaterial.h"
  15. #include "materialsystem/imesh.h"
  16. #include "materialsystem/imaterialvar.h"
  17. #include "VGuiMatSurface/IMatSystemSurface.h"
  18. #include "tf_logic_halloween_2014.h"
  19. #include "tf_gamerules.h"
  20. #include "mathlib/mathlib.h"
  21. ConVar cl_crosshair_red( "cl_crosshair_red", "200", FCVAR_ARCHIVE );
  22. ConVar cl_crosshair_green( "cl_crosshair_green", "200", FCVAR_ARCHIVE );
  23. ConVar cl_crosshair_blue( "cl_crosshair_blue", "200", FCVAR_ARCHIVE );
  24. ConVar cl_crosshair_file( "cl_crosshair_file", "", FCVAR_ARCHIVE );
  25. ConVar cl_crosshair_scale( "cl_crosshair_scale", "32.0", FCVAR_ARCHIVE );
  26. using namespace vgui;
  27. // Everything else is expecting to find "CHudCrosshair"
  28. DECLARE_NAMED_HUDELEMENT( CHudTFCrosshair, CHudCrosshair );
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. CHudTFCrosshair::CHudTFCrosshair( const char *pName ) :
  33. CHudCrosshair ( pName )
  34. {
  35. m_szPreviousCrosshair[0] = '\0';
  36. m_iCrosshairTextureID = -1;
  37. m_flTimeToHideUntil = -1.f;
  38. ListenForGameEvent( "restart_timer_time" );
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. CHudTFCrosshair::~CHudTFCrosshair( void )
  44. {
  45. if ( vgui::surface() && m_iCrosshairTextureID != -1 )
  46. {
  47. vgui::surface()->DestroyTextureID( m_iCrosshairTextureID );
  48. m_iCrosshairTextureID = -1;
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. bool CHudTFCrosshair::ShouldDraw( void )
  55. {
  56. // turn off for the minigames
  57. if ( CTFMinigameLogic::GetMinigameLogic() && CTFMinigameLogic::GetMinigameLogic()->GetActiveMinigame() )
  58. return false;
  59. if ( TFGameRules() && TFGameRules()->ShowMatchSummary() )
  60. return false;
  61. // turn off if the local player is a ghost
  62. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  63. if ( pPlayer )
  64. {
  65. if ( pPlayer->m_Shared.InCond( TF_COND_HALLOWEEN_GHOST_MODE ) )
  66. return false;
  67. if ( pPlayer->IsTaunting() )
  68. return false;
  69. }
  70. if ( m_flTimeToHideUntil > gpGlobals->curtime )
  71. return false;
  72. return BaseClass::ShouldDraw();
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose:
  76. //-----------------------------------------------------------------------------
  77. void CHudTFCrosshair::LevelShutdown( void )
  78. {
  79. m_szPreviousCrosshair[0] = '\0';
  80. if ( m_pCrosshairMaterial )
  81. {
  82. delete m_pCrosshairMaterial;
  83. m_pCrosshairMaterial = NULL;
  84. }
  85. m_flTimeToHideUntil = -1.f;
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose:
  89. //-----------------------------------------------------------------------------
  90. void CHudTFCrosshair::Init()
  91. {
  92. if ( m_iCrosshairTextureID == -1 )
  93. {
  94. m_iCrosshairTextureID = vgui::surface()->CreateNewTextureID();
  95. }
  96. m_flTimeToHideUntil = -1.f;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. //-----------------------------------------------------------------------------
  101. void CHudTFCrosshair::FireGameEvent( IGameEvent * event )
  102. {
  103. if ( FStrEq( "restart_timer_time", event->GetName() ) )
  104. {
  105. if ( TFGameRules() && TFGameRules()->IsCompetitiveMode() )
  106. {
  107. int nTime = event->GetInt( "time" );
  108. if ( ( nTime <= 10 ) && ( nTime > 0 ) )
  109. {
  110. m_flTimeToHideUntil = gpGlobals->curtime + nTime;
  111. return;
  112. }
  113. }
  114. }
  115. m_flTimeToHideUntil = -1.f;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose:
  119. //-----------------------------------------------------------------------------
  120. void CHudTFCrosshair::Paint()
  121. {
  122. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  123. if( !pPlayer )
  124. return;
  125. const char *crosshairfile = cl_crosshair_file.GetString();
  126. if ( ( crosshairfile == NULL ) || ( Q_stricmp( m_szPreviousCrosshair, crosshairfile ) != 0 ) )
  127. {
  128. char buf[256];
  129. Q_snprintf( buf, sizeof(buf), "vgui/crosshairs/%s", crosshairfile );
  130. if ( m_iCrosshairTextureID != -1 )
  131. {
  132. vgui::surface()->DrawSetTextureFile( m_iCrosshairTextureID, buf, true, false );
  133. }
  134. if ( m_pCrosshairMaterial )
  135. {
  136. delete m_pCrosshairMaterial;
  137. }
  138. m_pCrosshairMaterial = vgui::surface()->DrawGetTextureMatInfoFactory( m_iCrosshairTextureID );
  139. if ( !m_pCrosshairMaterial )
  140. return;
  141. // save the name to compare with the cvar in the future
  142. Q_strncpy( m_szPreviousCrosshair, crosshairfile, sizeof(m_szPreviousCrosshair) );
  143. }
  144. if ( m_szPreviousCrosshair[0] == '\0' )
  145. {
  146. return BaseClass::Paint();
  147. }
  148. // This is somewhat cut'n'paste from CHudCrosshair::Paint(). Would be nice to unify them some more.
  149. float x, y;
  150. bool bBehindCamera;
  151. GetDrawPosition ( &x, &y, &bBehindCamera );
  152. if( bBehindCamera )
  153. return;
  154. float flWeaponScale = 1.f;
  155. int iTextureW = 32;
  156. int iTextureH = 32;
  157. C_BaseCombatWeapon *pWeapon = pPlayer->GetActiveWeapon();
  158. if ( pWeapon )
  159. {
  160. pWeapon->GetWeaponCrosshairScale( flWeaponScale );
  161. }
  162. float flPlayerScale = 1.0f;
  163. #ifdef TF_CLIENT_DLL
  164. Color clr( cl_crosshair_red.GetInt(), cl_crosshair_green.GetInt(), cl_crosshair_blue.GetInt(), 255 );
  165. flPlayerScale = cl_crosshair_scale.GetFloat() / 32.0f; // the player can change the scale in the options/multiplayer tab
  166. #else
  167. Color clr = m_clrCrosshair;
  168. #endif
  169. float flWidth = flWeaponScale * flPlayerScale * (float)iTextureW;
  170. float flHeight = flWeaponScale * flPlayerScale * (float)iTextureH;
  171. int iWidth = (int)( flWidth + 0.5f );
  172. int iHeight = (int)( flHeight + 0.5f );
  173. int iX = (int)( x + 0.5f );
  174. int iY = (int)( y + 0.5f );
  175. vgui::ISurface *pSurf = vgui::surface();
  176. pSurf->DrawSetColor( clr );
  177. pSurf->DrawSetTexture( m_iCrosshairTextureID );
  178. pSurf->DrawTexturedRect( iX-iWidth, iY-iHeight, iX+iWidth, iY+iHeight );
  179. pSurf->DrawSetTexture(0);
  180. }