Counter Strike : Global Offensive Source Code
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.

267 lines
6.6 KiB

  1. //===== Copyright � 1996-2005, 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 "HUD/sfhudflashinterface.h"
  18. #include <vgui/ISurface.h>
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. using namespace vgui;
  22. //For progress bar orientations
  23. const int CHud::HUDPB_HORIZONTAL = 0;
  24. const int CHud::HUDPB_VERTICAL = 1;
  25. const int CHud::HUDPB_HORIZONTAL_INV = 2;
  26. // Called when a ConVar changes value
  27. static void FovChanged_Callback( IConVar *pConVar, const char *pOldString, float flOldValue )
  28. {
  29. ConVarRef var( pConVar );
  30. if ( engine->IsInGame() )
  31. {
  32. engine->ServerCmd( VarArgs( "fov %f\n", var.GetFloat() ) );
  33. }
  34. }
  35. static ConVar fov_watcher( "_fov", "0", 0, "Automates fov command to server.", FovChanged_Callback );
  36. void CHud::DoElementThink( CHudElement* pElement, vgui::Panel* pPanel )
  37. {
  38. bool visible = true;
  39. //if we are globally disabling the HUD (and the pElement obeys global hiding), override the ShouldDraw check
  40. if ( m_iDisabledCount > 0 && !pElement->GetIgnoreGlobalHudDisable() )
  41. {
  42. visible = false;
  43. }
  44. else
  45. {
  46. visible = pElement->ShouldDraw();
  47. }
  48. pElement->SetActive( visible );
  49. pElement->Think();
  50. if ( pPanel && pPanel->IsVisible() != visible )
  51. {
  52. pPanel->SetVisible( visible );
  53. }
  54. bool bProcessInput = visible;
  55. if ( bProcessInput )
  56. {
  57. if ( SFHudFlashInterface *pHudFlashInterface = dynamic_cast< SFHudFlashInterface * >( pElement ) )
  58. {
  59. if ( !pHudFlashInterface->FlashAPIIsValid() && !pHudFlashInterface->ShouldProcessInputBeforeFlashApiReady() )
  60. bProcessInput = false;
  61. }
  62. }
  63. if ( bProcessInput )
  64. {
  65. pElement->ProcessInput();
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose: Think
  70. //-----------------------------------------------------------------------------
  71. void CHud::Think( void )
  72. {
  73. // Determine the visibility of all hud elements
  74. CUtlVector< CHudElement * > & list = GetHudList();
  75. CUtlVector< vgui::Panel * > & hudPanelList = GetHudPanelList();
  76. int c = list.Count();
  77. Assert( c == hudPanelList.Count() );
  78. m_bEngineIsInGame = engine->IsInGame() && ( engine->IsLevelMainMenuBackground() == false );
  79. for ( int i = 0; i < c; ++i )
  80. {
  81. CHudElement* pElement = list[i];
  82. if ( !pElement->m_bWantLateUpdate )
  83. {
  84. DoElementThink( pElement, hudPanelList[ i ] );
  85. }
  86. }
  87. // Let the active weapon at the keybits
  88. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  89. if ( pPlayer )
  90. {
  91. C_BaseCombatWeapon *pWeapon = pPlayer->GetActiveWeapon();
  92. if ( pWeapon )
  93. {
  94. pWeapon->HandleInput();
  95. }
  96. }
  97. if ( ( m_flScreenShotTime > 0 ) && ( m_flScreenShotTime < gpGlobals->curtime ) )
  98. {
  99. if ( !IsGameConsole() )
  100. {
  101. engine->ClientCmd( "screenshot" );
  102. }
  103. m_flScreenShotTime = -1;
  104. }
  105. }
  106. void CHud::OnTimeJump(void)
  107. {
  108. CUtlVector< CHudElement * > & list = GetHudList();
  109. int c = list.Count();
  110. for ( int i = 0; i < c; ++i )
  111. {
  112. CHudElement* pElement = list[i];
  113. if ( !pElement->m_bWantLateUpdate )
  114. {
  115. pElement->OnTimeJump();
  116. }
  117. }
  118. }
  119. void CHud::LateThink( void )
  120. {
  121. SNPROF("LateThink");
  122. CUtlVector< CHudElement * > & list = GetHudList();
  123. CUtlVector< vgui::Panel * > & hudPanelList = GetHudPanelList();
  124. int c = list.Count();
  125. Assert( c == hudPanelList.Count() );
  126. m_bEngineIsInGame = engine->IsInGame() && ( engine->IsLevelMainMenuBackground() == false );
  127. for ( int i = 0; i < c; ++i )
  128. {
  129. CHudElement* pElement = list[i];
  130. if ( pElement->m_bWantLateUpdate )
  131. {
  132. DoElementThink( pElement, hudPanelList[ i ] );
  133. }
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose: The percentage passed in is expected and clamped to 0.0f to 1.0f
  138. // Input : x -
  139. // y -
  140. // width -
  141. // height -
  142. // percentage -
  143. // clr -
  144. // type -
  145. //-----------------------------------------------------------------------------
  146. void CHud::DrawProgressBar( int x, int y, int width, int height, float percentage, Color& clr, unsigned char type )
  147. {
  148. //Clamp our percentage
  149. percentage = MIN( 1.0f, percentage );
  150. percentage = MAX( 0.0f, percentage );
  151. Color lowColor = clr;
  152. lowColor[ 0 ] /= 2;
  153. lowColor[ 1 ] /= 2;
  154. lowColor[ 2 ] /= 2;
  155. //Draw a vertical progress bar
  156. if ( type == HUDPB_VERTICAL )
  157. {
  158. int barOfs = height * percentage;
  159. surface()->DrawSetColor( lowColor );
  160. surface()->DrawFilledRect( x, y, x + width, y + barOfs );
  161. surface()->DrawSetColor( clr );
  162. surface()->DrawFilledRect( x, y + barOfs, x + width, y + height );
  163. }
  164. else if ( type == HUDPB_HORIZONTAL )
  165. {
  166. int barOfs = width * percentage;
  167. surface()->DrawSetColor( lowColor );
  168. surface()->DrawFilledRect( x, y, x + barOfs, y + height );
  169. surface()->DrawSetColor( clr );
  170. surface()->DrawFilledRect( x + barOfs, y, x + width, y + height );
  171. }
  172. else if ( type == HUDPB_HORIZONTAL_INV )
  173. {
  174. int barOfs = width * percentage;
  175. surface()->DrawSetColor( clr );
  176. surface()->DrawFilledRect( x, y, x + barOfs, y + height );
  177. surface()->DrawSetColor( lowColor );
  178. surface()->DrawFilledRect( x + barOfs, y, x + width, y + height );
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Purpose: The percentage passed in is expected and clamped to 0.0f to 1.0f
  183. // Input : x -
  184. // y -
  185. // *icon -
  186. // percentage -
  187. // clr -
  188. // type -
  189. //-----------------------------------------------------------------------------
  190. void CHud::DrawIconProgressBar( int x, int y, CHudTexture *icon, CHudTexture *icon2, float percentage, Color& clr, int type )
  191. {
  192. if ( icon == NULL )
  193. return;
  194. //Clamp our percentage
  195. percentage = MIN( 1.0f, percentage );
  196. percentage = MAX( 0.0f, percentage );
  197. int height = icon->Height();
  198. int width = icon->Width();
  199. //Draw a vertical progress bar
  200. if ( type == HUDPB_VERTICAL )
  201. {
  202. int barOfs = height * percentage;
  203. icon2->DrawSelfCropped(
  204. x, y, // Pos
  205. 0, 0, width, barOfs, // Cropped subrect
  206. clr );
  207. icon->DrawSelfCropped(
  208. x, y + barOfs,
  209. 0, barOfs, width, height - barOfs, // Cropped subrect
  210. clr );
  211. }
  212. else if ( type == HUDPB_HORIZONTAL )
  213. {
  214. int barOfs = width * percentage;
  215. icon2->DrawSelfCropped(
  216. x, y, // Pos
  217. 0, 0, barOfs, height, // Cropped subrect
  218. clr );
  219. icon->DrawSelfCropped(
  220. x + barOfs, y,
  221. barOfs, 0, width - barOfs, height, // Cropped subrect
  222. clr );
  223. }
  224. }