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.

183 lines
4.3 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_dod_player.h"
  12. #include "dod_hud_crosshair.h"
  13. #include "materialsystem/imaterial.h"
  14. #include "materialsystem/imesh.h"
  15. #include "materialsystem/imaterialvar.h"
  16. #include "mathlib/mathlib.h"
  17. ConVar cl_crosshair_red( "cl_crosshair_red", "200", FCVAR_ARCHIVE );
  18. ConVar cl_crosshair_green( "cl_crosshair_green", "200", FCVAR_ARCHIVE );
  19. ConVar cl_crosshair_blue( "cl_crosshair_blue", "200", FCVAR_ARCHIVE );
  20. ConVar cl_crosshair_alpha( "cl_crosshair_alpha", "200", FCVAR_ARCHIVE );
  21. ConVar cl_crosshair_file( "cl_crosshair_file", "crosshair1", FCVAR_ARCHIVE );
  22. ConVar cl_crosshair_scale( "cl_crosshair_scale", "32.0", FCVAR_ARCHIVE );
  23. ConVar cl_crosshair_approach_speed( "cl_crosshair_approach_speed", "0.015" );
  24. ConVar cl_dynamic_crosshair( "cl_dynamic_crosshair", "1", FCVAR_ARCHIVE );
  25. using namespace vgui;
  26. DECLARE_HUDELEMENT( CHudDODCrosshair );
  27. CHudDODCrosshair::CHudDODCrosshair( const char *pName ) :
  28. vgui::Panel( NULL, "HudDODCrosshair" ), CHudElement( pName )
  29. {
  30. SetParent( g_pClientMode->GetViewport() );
  31. SetHiddenBits( HIDEHUD_PLAYERDEAD );
  32. m_szPreviousCrosshair[0] = '\0';
  33. m_pFrameVar = NULL;
  34. m_flAccuracy = 0.1;
  35. }
  36. void CHudDODCrosshair::ApplySchemeSettings( IScheme *scheme )
  37. {
  38. BaseClass::ApplySchemeSettings( scheme );
  39. SetSize( ScreenWidth(), ScreenHeight() );
  40. }
  41. void CHudDODCrosshair::LevelShutdown( void )
  42. {
  43. // forces m_pFrameVar to recreate next map
  44. m_szPreviousCrosshair[0] = '\0';
  45. if ( m_pCrosshair )
  46. {
  47. delete m_pCrosshair;
  48. m_pCrosshair = NULL;
  49. }
  50. if ( m_pFrameVar )
  51. {
  52. delete m_pFrameVar;
  53. m_pFrameVar = NULL;
  54. }
  55. }
  56. void CHudDODCrosshair::Init()
  57. {
  58. m_iCrosshairTextureID = vgui::surface()->CreateNewTextureID();
  59. }
  60. bool CHudDODCrosshair::ShouldDraw()
  61. {
  62. C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
  63. if ( !pPlayer )
  64. return false;
  65. CWeaponDODBase *pWeapon = pPlayer->GetActiveDODWeapon();
  66. if ( !pWeapon )
  67. return false;
  68. return pWeapon->ShouldDrawCrosshair();
  69. }
  70. void CHudDODCrosshair::Paint()
  71. {
  72. C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
  73. if( !pPlayer )
  74. return;
  75. const char *crosshairfile = cl_crosshair_file.GetString();
  76. if ( !crosshairfile )
  77. return;
  78. if ( Q_stricmp( m_szPreviousCrosshair, crosshairfile ) != 0 )
  79. {
  80. char buf[256];
  81. Q_snprintf( buf, sizeof(buf), "vgui/crosshairs/%s", crosshairfile );
  82. vgui::surface()->DrawSetTextureFile( m_iCrosshairTextureID, buf, true, false );
  83. if ( m_pCrosshair )
  84. {
  85. delete m_pCrosshair;
  86. }
  87. m_pCrosshair = vgui::surface()->DrawGetTextureMatInfoFactory( m_iCrosshairTextureID );
  88. if ( !m_pCrosshair )
  89. return;
  90. if ( m_pFrameVar )
  91. {
  92. delete m_pFrameVar;
  93. }
  94. bool bFound = false;
  95. m_pFrameVar = m_pCrosshair->FindVarFactory( "$frame", &bFound );
  96. Assert( bFound );
  97. m_nNumFrames = m_pCrosshair->GetNumAnimationFrames();
  98. // save the name to compare with the cvar in the future
  99. Q_strncpy( m_szPreviousCrosshair, crosshairfile, sizeof(m_szPreviousCrosshair) );
  100. }
  101. if ( m_pFrameVar )
  102. {
  103. if ( cl_dynamic_crosshair.GetBool() == false )
  104. {
  105. m_pFrameVar->SetIntValue( 0 );
  106. }
  107. else
  108. {
  109. CWeaponDODBase *pWeapon = pPlayer->GetActiveDODWeapon();
  110. if ( !pWeapon )
  111. return;
  112. float accuracy = pWeapon->GetWeaponAccuracy( pPlayer->GetAbsVelocity().Length2D() );
  113. float flMin = 0.02;
  114. float flMax = 0.125;
  115. accuracy = clamp( accuracy, flMin, flMax );
  116. // approach this accuracy from our current accuracy
  117. m_flAccuracy = Approach( accuracy, m_flAccuracy, cl_crosshair_approach_speed.GetFloat() );
  118. float flFrame = RemapVal( m_flAccuracy, flMin, flMax, 0, m_nNumFrames-1 );
  119. m_pFrameVar->SetIntValue( (int)flFrame );
  120. }
  121. }
  122. Color clr( cl_crosshair_red.GetInt(), cl_crosshair_green.GetInt(), cl_crosshair_blue.GetInt(), 255 );
  123. int screenWide, screenTall;
  124. GetHudSize(screenWide, screenTall);
  125. int iX = screenWide / 2;
  126. int iY = screenTall / 2;
  127. int iWidth, iHeight;
  128. iWidth = iHeight = cl_crosshair_scale.GetInt();
  129. vgui::surface()->DrawSetColor( clr );
  130. vgui::surface()->DrawSetTexture( m_iCrosshairTextureID );
  131. vgui::surface()->DrawTexturedRect( iX-iWidth, iY-iHeight, iX+iWidth, iY+iHeight );
  132. vgui::surface()->DrawSetTexture(0);
  133. }