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.

210 lines
5.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. //
  9. // hud_redraw.cpp
  10. //
  11. #include "cbase.h"
  12. #include "hudelement.h"
  13. #include "iclientmode.h"
  14. #include "itextmessage.h"
  15. #include "vgui_basepanel.h"
  16. #include "hud_crosshair.h"
  17. #include <vgui/ISurface.h>
  18. #if defined( REPLAY_ENABLED )
  19. #include "replay/ienginereplay.h"
  20. #endif
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. using namespace vgui;
  24. //For progress bar orientations
  25. const int CHud::HUDPB_HORIZONTAL = 0;
  26. const int CHud::HUDPB_VERTICAL = 1;
  27. const int CHud::HUDPB_HORIZONTAL_INV = 2;
  28. // Called when a ConVar changes value
  29. static void FovChanged_Callback( IConVar *pConVar, const char *pOldString, float flOldValue )
  30. {
  31. ConVarRef var( pConVar );
  32. if ( engine->IsInGame() )
  33. {
  34. engine->ServerCmd( VarArgs( "fov %f\n", var.GetFloat() ) );
  35. }
  36. }
  37. static ConVar fov_watcher( "_fov", "0", 0, "Automates fov command to server.", FovChanged_Callback );
  38. //-----------------------------------------------------------------------------
  39. // Purpose: Think
  40. //-----------------------------------------------------------------------------
  41. void CHud::Think(void)
  42. {
  43. #if defined( REPLAY_ENABLED )
  44. // Don't draw this
  45. extern IEngineClientReplay *g_EngineClientReplay;
  46. const bool bPlayingReplay = g_pEngineClientReplay && g_pEngineClientReplay->IsPlayingReplayDemo();
  47. #endif
  48. // Determine the visibility of all hud elements
  49. for ( int i = 0; i < m_HudList.Size(); i++ )
  50. {
  51. // Visible?
  52. bool visible = m_HudList[i]->ShouldDraw();
  53. #if defined( REPLAY_ENABLED )
  54. visible = visible && !bPlayingReplay;
  55. #endif
  56. m_HudList[i]->SetActive( visible );
  57. // If it's a vgui panel, hide/show as appropriate
  58. vgui::Panel *pPanel = dynamic_cast<vgui::Panel*>(m_HudList[i]);
  59. if ( pPanel && pPanel->IsVisible() != visible )
  60. {
  61. pPanel->SetVisible( visible );
  62. }
  63. else if ( !pPanel )
  64. {
  65. // All HUD elements should now derive from vgui!!!
  66. Assert( 0 );
  67. }
  68. if ( visible )
  69. {
  70. m_HudList[i]->ProcessInput();
  71. }
  72. }
  73. // Let the active weapon at the keybits
  74. C_BaseCombatWeapon *pWeapon = GetActiveWeapon();
  75. if ( pWeapon )
  76. {
  77. pWeapon->HandleInput();
  78. }
  79. if ( ( m_flScreenShotTime > 0 ) && ( m_flScreenShotTime < gpGlobals->curtime ) )
  80. {
  81. if ( !IsX360() )
  82. {
  83. engine->ClientCmd( "screenshot" );
  84. }
  85. m_flScreenShotTime = -1;
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose: The percentage passed in is expected and clamped to 0.0f to 1.0f
  90. // Input : x -
  91. // y -
  92. // width -
  93. // height -
  94. // percentage -
  95. // clr -
  96. // type -
  97. //-----------------------------------------------------------------------------
  98. void CHud::DrawProgressBar( int x, int y, int width, int height, float percentage, Color& clr, unsigned char type )
  99. {
  100. //Clamp our percentage
  101. percentage = MIN( 1.0f, percentage );
  102. percentage = MAX( 0.0f, percentage );
  103. Color lowColor = clr;
  104. lowColor[ 0 ] /= 2;
  105. lowColor[ 1 ] /= 2;
  106. lowColor[ 2 ] /= 2;
  107. //Draw a vertical progress bar
  108. if ( type == HUDPB_VERTICAL )
  109. {
  110. int barOfs = height * percentage;
  111. surface()->DrawSetColor( lowColor );
  112. surface()->DrawFilledRect( x, y, x + width, y + barOfs );
  113. surface()->DrawSetColor( clr );
  114. surface()->DrawFilledRect( x, y + barOfs, x + width, y + height );
  115. }
  116. else if ( type == HUDPB_HORIZONTAL )
  117. {
  118. int barOfs = width * percentage;
  119. surface()->DrawSetColor( lowColor );
  120. surface()->DrawFilledRect( x, y, x + barOfs, y + height );
  121. surface()->DrawSetColor( clr );
  122. surface()->DrawFilledRect( x + barOfs, y, x + width, y + height );
  123. }
  124. else if ( type == HUDPB_HORIZONTAL_INV )
  125. {
  126. int barOfs = width * percentage;
  127. surface()->DrawSetColor( clr );
  128. surface()->DrawFilledRect( x, y, x + barOfs, y + height );
  129. surface()->DrawSetColor( lowColor );
  130. surface()->DrawFilledRect( x + barOfs, y, x + width, y + height );
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose: The percentage passed in is expected and clamped to 0.0f to 1.0f
  135. // Input : x -
  136. // y -
  137. // *icon -
  138. // percentage -
  139. // clr -
  140. // type -
  141. //-----------------------------------------------------------------------------
  142. void CHud::DrawIconProgressBar( int x, int y, CHudTexture *icon, CHudTexture *icon2, float percentage, Color& clr, int type )
  143. {
  144. if ( icon == NULL )
  145. return;
  146. //Clamp our percentage
  147. percentage = MIN( 1.0f, percentage );
  148. percentage = MAX( 0.0f, percentage );
  149. int height = icon->Height();
  150. int width = icon->Width();
  151. //Draw a vertical progress bar
  152. if ( type == HUDPB_VERTICAL )
  153. {
  154. int barOfs = height * percentage;
  155. icon2->DrawSelfCropped(
  156. x, y, // Pos
  157. 0, 0, width, barOfs, // Cropped subrect
  158. clr );
  159. icon->DrawSelfCropped(
  160. x, y + barOfs,
  161. 0, barOfs, width, height - barOfs, // Cropped subrect
  162. clr );
  163. }
  164. else if ( type == HUDPB_HORIZONTAL )
  165. {
  166. int barOfs = width * percentage;
  167. icon2->DrawSelfCropped(
  168. x, y, // Pos
  169. 0, 0, barOfs, height, // Cropped subrect
  170. clr );
  171. icon->DrawSelfCropped(
  172. x + barOfs, y,
  173. barOfs, 0, width - barOfs, height, // Cropped subrect
  174. clr );
  175. }
  176. }