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.

155 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "text_message.h"
  9. #include "hud_macros.h"
  10. #include "iclientmode.h"
  11. #include "view.h"
  12. #include "KeyValues.h"
  13. #include "vgui_controls/AnimationController.h"
  14. #include "vgui/ILocalize.h"
  15. #include "vgui/ISurface.h"
  16. #include "VGuiMatSurface/IMatSystemSurface.h"
  17. #include "materialsystem/imaterial.h"
  18. #include "materialsystem/imesh.h"
  19. #include "materialsystem/imaterialvar.h"
  20. #include "IEffects.h"
  21. #include "hudelement.h"
  22. using namespace vgui;
  23. // memdbgon must be the last include file in a .cpp file!!!
  24. #include "tier0/memdbgon.h"
  25. //-----------------------------------------------------------------------------
  26. // Purpose: HDU Damage indication
  27. //-----------------------------------------------------------------------------
  28. class CHudPoisonDamageIndicator : public CHudElement, public vgui::Panel
  29. {
  30. DECLARE_CLASS_SIMPLE( CHudPoisonDamageIndicator, vgui::Panel );
  31. public:
  32. CHudPoisonDamageIndicator( const char *pElementName );
  33. void Reset( void );
  34. virtual bool ShouldDraw( void );
  35. private:
  36. virtual void OnThink();
  37. virtual void Paint();
  38. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  39. private:
  40. CPanelAnimationVar( vgui::HFont, m_hTextFont, "TextFont", "Default" );
  41. CPanelAnimationVar( Color, m_TextColor, "TextColor", "FgColor" );
  42. CPanelAnimationVarAliasType( float, text_xpos, "text_xpos", "8", "proportional_float" );
  43. CPanelAnimationVarAliasType( float, text_ypos, "text_ypos", "8", "proportional_float" );
  44. CPanelAnimationVarAliasType( float, text_ygap, "text_ygap", "14", "proportional_float" );
  45. bool m_bDamageIndicatorVisible;
  46. };
  47. DECLARE_HUDELEMENT( CHudPoisonDamageIndicator );
  48. //-----------------------------------------------------------------------------
  49. // Purpose: Constructor
  50. //-----------------------------------------------------------------------------
  51. CHudPoisonDamageIndicator::CHudPoisonDamageIndicator( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudPoisonDamageIndicator")
  52. {
  53. vgui::Panel *pParent = g_pClientMode->GetViewport();
  54. SetParent( pParent );
  55. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. //-----------------------------------------------------------------------------
  60. void CHudPoisonDamageIndicator::Reset( void )
  61. {
  62. SetAlpha( 0 );
  63. m_bDamageIndicatorVisible = false;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: Save CPU cycles by letting the HUD system early cull
  67. // costly traversal. Called per frame, return true if thinking and
  68. // painting need to occur.
  69. //-----------------------------------------------------------------------------
  70. bool CHudPoisonDamageIndicator::ShouldDraw( void )
  71. {
  72. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  73. if ( !pPlayer )
  74. return false;
  75. bool bNeedsDraw = ( ( pPlayer->IsPoisoned() != m_bDamageIndicatorVisible ) || ( GetAlpha() > 0 ) );
  76. return ( bNeedsDraw && CHudElement::ShouldDraw() );
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: updates poison damage
  80. //-----------------------------------------------------------------------------
  81. void CHudPoisonDamageIndicator::OnThink()
  82. {
  83. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  84. if ( !pPlayer )
  85. return;
  86. // update poison damage indicator
  87. bool bShouldIndicatorBeVisible = pPlayer->IsPoisoned();
  88. if (bShouldIndicatorBeVisible == m_bDamageIndicatorVisible)
  89. return;
  90. // state change
  91. m_bDamageIndicatorVisible = bShouldIndicatorBeVisible;
  92. if (m_bDamageIndicatorVisible)
  93. {
  94. SetVisible(true);
  95. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "PoisonDamageTaken" );
  96. }
  97. else
  98. {
  99. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "PoisonDamageCured" );
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose: Paints the damage display
  104. //-----------------------------------------------------------------------------
  105. void CHudPoisonDamageIndicator::Paint()
  106. {
  107. // draw the text
  108. surface()->DrawSetTextFont( m_hTextFont );
  109. surface()->DrawSetTextColor( m_TextColor );
  110. surface()->DrawSetTextPos(text_xpos, text_ypos);
  111. int ypos = text_ypos;
  112. const wchar_t *labelText = g_pVGuiLocalize->Find("Valve_HudPoisonDamage");
  113. Assert( labelText );
  114. for (const wchar_t *wch = labelText; wch && *wch != 0; wch++)
  115. {
  116. if (*wch == '\n')
  117. {
  118. ypos += text_ygap;
  119. surface()->DrawSetTextPos(text_xpos, ypos);
  120. }
  121. else
  122. {
  123. surface()->DrawUnicodeChar(*wch);
  124. }
  125. }
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose: hud scheme settings
  129. //-----------------------------------------------------------------------------
  130. void CHudPoisonDamageIndicator::ApplySchemeSettings(vgui::IScheme *pScheme)
  131. {
  132. BaseClass::ApplySchemeSettings(pScheme);
  133. }