Guest User

hud_scope.cpp

a guest
Sep 26th, 2026
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include "cbase.h"
  2. #include "hudelement.h"
  3. #include "hud_macros.h"
  4. #include "iclientmode.h"
  5. #include "c_basehlplayer.h" //alternative #include "c_baseplayer.h"
  6.  
  7. #include <vgui/IScheme.h>
  8. #include <vgui_controls/Panel.h>
  9.  
  10. // memdbgon must be the last include file in a .cpp file!
  11. #include "tier0/memdbgon.h"
  12.  
  13. /**
  14. * Simple HUD element for displaying a sniper scope on screen
  15. */
  16. class CHudScope : public vgui::Panel, public CHudElement
  17. {
  18. DECLARE_CLASS_SIMPLE(CHudScope, vgui::Panel);
  19.  
  20. public:
  21. CHudScope(const char* pElementName);
  22.  
  23. void Init();
  24. void MsgFunc_ShowScope(bf_read& msg);
  25.  
  26. protected:
  27. virtual void ApplySchemeSettings(vgui::IScheme* scheme);
  28. virtual void Paint(void);
  29.  
  30. private:
  31. bool m_bShow;
  32. CHudTexture* m_pScope;
  33. };
  34.  
  35. DECLARE_HUDELEMENT(CHudScope);
  36. DECLARE_HUD_MESSAGE(CHudScope, ShowScope);
  37.  
  38. using namespace vgui;
  39.  
  40. /**
  41. * Constructor - generic HUD element initialization stuff. Make sure our 2 member variables
  42. * are instantiated.
  43. */
  44. CHudScope::CHudScope(const char* pElementName) : CHudElement(pElementName), BaseClass(NULL, "HudScope")
  45. {
  46. vgui::Panel* pParent = g_pClientMode->GetViewport();
  47. SetParent(pParent);
  48.  
  49. m_bShow = false;
  50. m_pScope = 0;
  51.  
  52. // Scope will not show when the player is dead
  53. SetHiddenBits(HIDEHUD_PLAYERDEAD);
  54.  
  55. // fix for users with diffrent screen ratio (Lodle)
  56. int screenWide, screenTall;
  57. GetHudSize(screenWide, screenTall);
  58. SetBounds(0, 0, screenWide, screenTall);
  59.  
  60. }
  61.  
  62. /**
  63. * Hook up our HUD message, and make sure we are not showing the scope
  64. */
  65. void CHudScope::Init()
  66. {
  67. HOOK_HUD_MESSAGE(CHudScope, ShowScope);
  68.  
  69. m_bShow = false;
  70. }
  71.  
  72. /**
  73. * Load in the scope material here
  74. */
  75. void CHudScope::ApplySchemeSettings(vgui::IScheme* scheme)
  76. {
  77. BaseClass::ApplySchemeSettings(scheme);
  78.  
  79. SetPaintBackgroundEnabled(false);
  80. SetPaintBorderEnabled(false);
  81.  
  82. if (!m_pScope)
  83. {
  84. m_pScope = gHUD.GetIcon("scope");
  85. }
  86. }
  87.  
  88. /**
  89. * Simple - if we want to show the scope, draw it. Otherwise don't.
  90. */
  91. void CHudScope::Paint(void)
  92. {
  93. C_BasePlayer* pPlayer = C_BasePlayer::GetLocalPlayer();
  94. if (!pPlayer)
  95. {
  96. return;
  97. }
  98.  
  99. if (m_bShow)
  100. {
  101. //Perform depth hack to prevent clips by world
  102. //materials->DepthRange( 0.0f, 0.1f );
  103.  
  104. // This will draw the scope at the origin of this HUD element, and
  105. // stretch it to the width and height of the element. As long as the
  106. // HUD element is set up to cover the entire screen, so will the scope
  107. m_pScope->DrawSelf(0, 0, GetWide(), GetTall(), Color(255, 255, 255, 255));
  108.  
  109. //Restore depth
  110. //materials->DepthRange( 0.0f, 1.0f );
  111.  
  112. // Hide the crosshair
  113. pPlayer->m_Local.m_iHideHUD |= HIDEHUD_CROSSHAIR;
  114. }
  115. else if ((pPlayer->m_Local.m_iHideHUD & HIDEHUD_CROSSHAIR) != 0)
  116. {
  117. pPlayer->m_Local.m_iHideHUD &= ~HIDEHUD_CROSSHAIR;
  118. }
  119. }
  120.  
  121.  
  122. /**
  123. * Callback for our message - set the show variable to whatever
  124. * boolean value is received in the message
  125. */
  126. void CHudScope::MsgFunc_ShowScope(bf_read& msg)
  127. {
  128. m_bShow = msg.ReadByte();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment