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.

224 lines
5.8 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 "iclientmode.h"
  11. #include "hl1_hud_numbers.h"
  12. #include <vgui/ISurface.h>
  13. #include <vgui_controls/Panel.h>
  14. #include "ihudlcd.h"
  15. #define MIN_ALPHA 100
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Displays current ammunition level
  18. //-----------------------------------------------------------------------------
  19. class CHudAmmo : public CHudElement, public CHL1HudNumbers
  20. {
  21. DECLARE_CLASS_SIMPLE( CHudAmmo, CHL1HudNumbers );
  22. public:
  23. CHudAmmo( const char *pElementName );
  24. void Init( void );
  25. void VidInit( void );
  26. void OnThink( void );
  27. private:
  28. void Paint( void );
  29. void ApplySchemeSettings(vgui::IScheme *pScheme);
  30. private:
  31. CHandle<C_BaseCombatWeapon> m_hLastPickedUpWeapon;
  32. float m_flFade;
  33. };
  34. DECLARE_HUDELEMENT( CHudAmmo );
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Constructor
  37. //-----------------------------------------------------------------------------
  38. CHudAmmo::CHudAmmo( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudAmmo")
  39. {
  40. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT | HIDEHUD_WEAPONSELECTION );
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. void CHudAmmo::Init( void )
  46. {
  47. m_hLastPickedUpWeapon = NULL;
  48. m_flFade = 0.0;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. //-----------------------------------------------------------------------------
  53. void CHudAmmo::VidInit( void )
  54. {
  55. Reset();
  56. BaseClass::VidInit();
  57. }
  58. void CHudAmmo::OnThink( void )
  59. {
  60. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  61. if ( !pPlayer )
  62. return;
  63. // find and display our current selection
  64. C_BaseCombatWeapon *pActiveWeapon = GetActiveWeapon();
  65. if ( !pActiveWeapon )
  66. {
  67. m_hLastPickedUpWeapon = NULL;
  68. return;
  69. }
  70. if ( m_hLastPickedUpWeapon != pActiveWeapon )
  71. {
  72. m_flFade = 200.0f;
  73. m_hLastPickedUpWeapon = pActiveWeapon;
  74. }
  75. }
  76. void CHudAmmo::Paint( void )
  77. {
  78. int r, g, b, a, nUnused;
  79. int x, y;
  80. Color clrAmmo;
  81. if (!ShouldDraw())
  82. return;
  83. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  84. C_BaseCombatWeapon *pActiveWeapon = GetActiveWeapon();
  85. // find and display our current selection
  86. if ( !pPlayer || !pActiveWeapon )
  87. {
  88. hudlcd->SetGlobalStat( "(ammo_primary)", "n/a" );
  89. hudlcd->SetGlobalStat( "(ammo_secondary)", "n/a" );
  90. return;
  91. }
  92. hudlcd->SetGlobalStat( "(weapon_print_name)", pActiveWeapon ? pActiveWeapon->GetPrintName() : " " );
  93. hudlcd->SetGlobalStat( "(weapon_name)", pActiveWeapon ? pActiveWeapon->GetName() : " " );
  94. if ( ( pActiveWeapon->GetPrimaryAmmoType() == -1 ) && ( pActiveWeapon->GetSecondaryAmmoType() == -1 ) )
  95. return;
  96. int nFontWidth = GetNumberFontWidth();
  97. int nFontHeight = GetNumberFontHeight();
  98. a = (int)MAX( MIN_ALPHA, m_flFade );
  99. if ( m_flFade > 0 )
  100. m_flFade -= ( gpGlobals->frametime * 20 );
  101. (gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
  102. clrAmmo.SetColor( r, g, b, a );
  103. int nHudElemWidth, nHudElemHeight;
  104. GetSize( nHudElemWidth, nHudElemHeight );
  105. // Does this weapon have a clip?
  106. y = nHudElemHeight - ( nFontHeight * 1.5 );
  107. // Does weapon have any ammo at all?
  108. if ( pActiveWeapon->GetPrimaryAmmoType() != -1 )
  109. {
  110. CHudTexture *icon_ammo = gWR.GetAmmoIconFromWeapon( pActiveWeapon->GetPrimaryAmmoType() );
  111. if ( !icon_ammo )
  112. {
  113. return;
  114. }
  115. int nIconWidth = icon_ammo->Width();
  116. if ( pActiveWeapon->UsesClipsForAmmo1() )
  117. {
  118. // room for the number and the '|' and the current ammo
  119. x = nHudElemWidth - (8 * nFontWidth) - nIconWidth;
  120. x = DrawHudNumber( x, y, pActiveWeapon->Clip1(), clrAmmo );
  121. int nBarWidth = nFontWidth / 10;
  122. x += nFontWidth / 2;
  123. (gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
  124. clrAmmo.SetColor( r, g, b, a );
  125. // draw the | bar
  126. clrAmmo.SetColor( r, g, b, a );
  127. vgui::surface()->DrawSetColor( clrAmmo );
  128. vgui::surface()->DrawFilledRect( x, y, x + nBarWidth, y + nFontHeight );
  129. x += nBarWidth + nFontWidth / 2;
  130. x = DrawHudNumber( x, y, pPlayer->GetAmmoCount( pActiveWeapon->GetPrimaryAmmoType() ), clrAmmo );
  131. }
  132. else
  133. {
  134. // SPR_Draw a bullets only line
  135. x = nHudElemWidth - 4 * nFontWidth - nIconWidth;
  136. x = DrawHudNumber( x, y, pPlayer->GetAmmoCount( pActiveWeapon->GetPrimaryAmmoType() ), clrAmmo );
  137. }
  138. // Draw the ammo Icon
  139. icon_ammo->DrawSelf( x, y, clrAmmo );
  140. hudlcd->SetGlobalStat( "(ammo_primary)", VarArgs( "%d", pPlayer->GetAmmoCount( pActiveWeapon->GetPrimaryAmmoType() ) ) );
  141. }
  142. else
  143. {
  144. hudlcd->SetGlobalStat( "(ammo_primary)", "n/a" );
  145. }
  146. // Does weapon have seconday ammo?
  147. if ( pActiveWeapon->GetSecondaryAmmoType() != -1 )
  148. {
  149. CHudTexture *icon_ammo = gWR.GetAmmoIconFromWeapon( pActiveWeapon->GetSecondaryAmmoType() );
  150. if ( !icon_ammo )
  151. {
  152. return;
  153. }
  154. int nIconWidth = icon_ammo->Width();
  155. // Do we have secondary ammo?
  156. if ( pPlayer->GetAmmoCount( pActiveWeapon->GetSecondaryAmmoType() ) > 0 )
  157. {
  158. y -= ( nFontHeight * 1.25 );
  159. x = nHudElemWidth - 4 * nFontWidth - nIconWidth;
  160. x = DrawHudNumber( x, y, pPlayer->GetAmmoCount( pActiveWeapon->GetSecondaryAmmoType() ), clrAmmo );
  161. // Draw the ammo Icon
  162. icon_ammo->DrawSelf( x, y, clrAmmo );
  163. }
  164. hudlcd->SetGlobalStat( "(ammo_secondary)", VarArgs( "%d", pPlayer->GetAmmoCount( pActiveWeapon->GetSecondaryAmmoType() ) ) );
  165. }
  166. else
  167. {
  168. hudlcd->SetGlobalStat( "(ammo_secondary)", "n/a" );
  169. }
  170. }
  171. void CHudAmmo::ApplySchemeSettings(vgui::IScheme *pScheme)
  172. {
  173. BaseClass::ApplySchemeSettings(pScheme);
  174. SetPaintBackgroundEnabled(false);
  175. }