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.

203 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "hudelement.h"
  9. #include "hud_macros.h"
  10. #include "hud_numericdisplay.h"
  11. #include "iclientmode.h"
  12. #include "c_cs_player.h"
  13. #include "VGuiMatSurface/IMatSystemSurface.h"
  14. #include "materialsystem/imaterial.h"
  15. #include "materialsystem/imesh.h"
  16. #include "materialsystem/imaterialvar.h"
  17. #include <vgui/IScheme.h>
  18. #include <vgui/ISurface.h>
  19. #include <KeyValues.h>
  20. #include <vgui_controls/AnimationController.h>
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Draws the zoom screen
  25. //-----------------------------------------------------------------------------
  26. class CHudScope : public vgui::Panel, public CHudElement
  27. {
  28. DECLARE_CLASS_SIMPLE( CHudScope, vgui::Panel );
  29. public:
  30. CHudScope( const char *pElementName );
  31. void Init( void );
  32. void LevelInit( void );
  33. protected:
  34. virtual void ApplySchemeSettings(vgui::IScheme *scheme);
  35. virtual void Paint( void );
  36. private:
  37. CMaterialReference m_ScopeMaterial;
  38. CMaterialReference m_DustOverlayMaterial;
  39. int m_iScopeArcTexture;
  40. int m_iScopeDustTexture;
  41. };
  42. DECLARE_HUDELEMENT_DEPTH( CHudScope, 70 );
  43. using namespace vgui;
  44. //-----------------------------------------------------------------------------
  45. // Purpose: Constructor
  46. //-----------------------------------------------------------------------------
  47. CHudScope::CHudScope( const char *pElementName ) : CHudElement(pElementName), BaseClass(NULL, "HudScope")
  48. {
  49. vgui::Panel *pParent = g_pClientMode->GetViewport();
  50. SetParent( pParent );
  51. SetHiddenBits( HIDEHUD_PLAYERDEAD );
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: standard hud element init function
  55. //-----------------------------------------------------------------------------
  56. void CHudScope::Init( void )
  57. {
  58. m_iScopeArcTexture = vgui::surface()->CreateNewTextureID();
  59. vgui::surface()->DrawSetTextureFile(m_iScopeArcTexture, "sprites/scope_arc", true, false);
  60. m_iScopeDustTexture = vgui::surface()->CreateNewTextureID();
  61. vgui::surface()->DrawSetTextureFile(m_iScopeDustTexture, "overlays/scope_lens", true, false);
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: standard hud element init function
  65. //-----------------------------------------------------------------------------
  66. void CHudScope::LevelInit( void )
  67. {
  68. Init();
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose: sets scheme colors
  72. //-----------------------------------------------------------------------------
  73. void CHudScope::ApplySchemeSettings( vgui::IScheme *scheme )
  74. {
  75. BaseClass::ApplySchemeSettings(scheme);
  76. SetPaintBackgroundEnabled(false);
  77. SetPaintBorderEnabled(false);
  78. int screenWide, screenTall;
  79. GetHudSize(screenWide, screenTall);
  80. SetBounds(0, 0, screenWide, screenTall);
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: draws the zoom effect
  84. //-----------------------------------------------------------------------------
  85. void CHudScope::Paint( void )
  86. {
  87. C_CSPlayer *pPlayer = dynamic_cast<C_CSPlayer *>(C_BasePlayer::GetLocalPlayer());
  88. if ( pPlayer == NULL )
  89. return;
  90. CWeaponCSBase *pWeapon = pPlayer->GetActiveCSWeapon();
  91. if( !pWeapon )
  92. return;
  93. Assert( m_iScopeArcTexture );
  94. Assert( m_iScopeDustTexture );
  95. // see if we're zoomed with a sniper rifle
  96. if( pPlayer->GetFOV() != pPlayer->GetDefaultFOV() &&
  97. pWeapon->GetCSWpnData().m_WeaponType == WEAPONTYPE_SNIPER_RIFLE )
  98. {
  99. int screenWide, screenTall;
  100. GetHudSize(screenWide, screenTall);
  101. // calculate the bounds in which we should draw the scope
  102. int inset = screenTall / 16;
  103. int y1 = inset;
  104. int x1 = (screenWide - screenTall) / 2 + inset;
  105. int y2 = screenTall - inset;
  106. int x2 = screenWide - x1;
  107. int x = screenWide / 2;
  108. int y = screenTall / 2;
  109. float uv1 = 0.5f / 256.0f, uv2 = 1.0f - uv1;
  110. vgui::Vertex_t vert[4];
  111. Vector2D uv11( uv1, uv1 );
  112. Vector2D uv12( uv1, uv2 );
  113. Vector2D uv21( uv2, uv1 );
  114. Vector2D uv22( uv2, uv2 );
  115. int xMod = ( screenWide / 2 );
  116. int yMod = ( screenTall / 2 );
  117. int iMiddleX = (screenWide / 2 );
  118. int iMiddleY = (screenTall / 2 );
  119. vgui::surface()->DrawSetTexture( m_iScopeDustTexture );
  120. vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
  121. vert[0].Init( Vector2D( iMiddleX + xMod, iMiddleY + yMod ), uv21 );
  122. vert[1].Init( Vector2D( iMiddleX - xMod, iMiddleY + yMod ), uv11 );
  123. vert[2].Init( Vector2D( iMiddleX - xMod, iMiddleY - yMod ), uv12 );
  124. vert[3].Init( Vector2D( iMiddleX + xMod, iMiddleY - yMod ), uv22 );
  125. vgui::surface()->DrawTexturedPolygon( 4, vert );
  126. vgui::surface()->DrawSetColor(0,0,0,255);
  127. //Draw the reticle with primitives
  128. vgui::surface()->DrawLine( 0, y, screenWide, y );
  129. vgui::surface()->DrawLine( x, 0, x, screenTall );
  130. //Draw the outline
  131. vgui::surface()->DrawSetTexture(m_iScopeArcTexture);
  132. // bottom right
  133. vert[0].Init( Vector2D( x, y ), uv11 );
  134. vert[1].Init( Vector2D( x2, y ), uv21 );
  135. vert[2].Init( Vector2D( x2, y2 ), uv22 );
  136. vert[3].Init( Vector2D( x, y2 ), uv12 );
  137. vgui::surface()->DrawTexturedPolygon( 4, vert );
  138. // top right
  139. vert[0].Init( Vector2D( x - 1, y1 ), uv12 );
  140. vert[1].Init( Vector2D ( x2, y1 ), uv22 );
  141. vert[2].Init( Vector2D( x2, y + 1 ), uv21 );
  142. vert[3].Init( Vector2D( x - 1, y + 1 ), uv11 );
  143. vgui::surface()->DrawTexturedPolygon(4, vert);
  144. // bottom left
  145. vert[0].Init( Vector2D( x1, y ), uv21 );
  146. vert[1].Init( Vector2D( x, y ), uv11 );
  147. vert[2].Init( Vector2D( x, y2 ), uv12 );
  148. vert[3].Init( Vector2D( x1, y2), uv22 );
  149. vgui::surface()->DrawTexturedPolygon(4, vert);
  150. // top left
  151. vert[0].Init( Vector2D( x1, y1 ), uv22 );
  152. vert[1].Init( Vector2D( x, y1 ), uv12 );
  153. vert[2].Init( Vector2D( x, y ), uv11 );
  154. vert[3].Init( Vector2D( x1, y ), uv21 );
  155. vgui::surface()->DrawTexturedPolygon(4, vert);
  156. vgui::surface()->DrawFilledRect(0, 0, screenWide, y1); // top
  157. vgui::surface()->DrawFilledRect(0, y2, screenWide, screenTall); // bottom
  158. vgui::surface()->DrawFilledRect(0, y1, x1, screenTall); // left
  159. vgui::surface()->DrawFilledRect(x2, y1, screenWide, screenTall); // right
  160. }
  161. }