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.

279 lines
8.6 KiB

  1. //========= Copyright � 1996-2005, 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 "c_basehlplayer.h"
  12. #include "vgui_controls/Panel.h"
  13. #include "vgui_controls/AnimationController.h"
  14. #include "vgui/ISurface.h"
  15. #include <vgui/ILocalize.h>
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. ConVar player_squad_transient_commands( "player_squad_transient_commands", "1", FCVAR_REPLICATED );
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Shows the sprint power bar
  21. //-----------------------------------------------------------------------------
  22. class CHudSquadStatus : public CHudElement, public vgui::Panel
  23. {
  24. DECLARE_CLASS_SIMPLE( CHudSquadStatus, vgui::Panel );
  25. public:
  26. explicit CHudSquadStatus( const char *pElementName );
  27. virtual void Init( void );
  28. virtual void Reset( void );
  29. virtual void OnThink( void );
  30. bool ShouldDraw();
  31. void MsgFunc_SquadMemberDied(bf_read &msg);
  32. protected:
  33. virtual void Paint();
  34. private:
  35. CPanelAnimationVar( vgui::HFont, m_hTextFont, "TextFont", "Default" );
  36. CPanelAnimationVarAliasType( float, text_xpos, "text_xpos", "8", "proportional_float" );
  37. CPanelAnimationVarAliasType( float, text_ypos, "text_ypos", "20", "proportional_float" );
  38. CPanelAnimationVar( vgui::HFont, m_hIconFont, "IconFont", "HudNumbers" );
  39. CPanelAnimationVarAliasType( float, m_flIconInsetX, "IconInsetX", "8", "proportional_float" );
  40. CPanelAnimationVarAliasType( float, m_flIconInsetY, "IconInsetY", "8", "proportional_float" );
  41. CPanelAnimationVarAliasType( float, m_flIconGap, "IconGap", "20", "proportional_float" );
  42. CPanelAnimationVar( Color, m_SquadIconColor, "SquadIconColor", "255 220 0 160" );
  43. CPanelAnimationVar( Color, m_LastMemberColor, "LastMemberColor", "255 220 0 0" );
  44. CPanelAnimationVar( Color, m_SquadTextColor, "SquadTextColor", "255 220 0 160" );
  45. int m_iSquadMembers;
  46. int m_iSquadMedics;
  47. bool m_bSquadMembersFollowing;
  48. bool m_bSquadMemberAdded;
  49. bool m_bSquadMemberJustDied;
  50. };
  51. DECLARE_HUDELEMENT( CHudSquadStatus );
  52. DECLARE_HUD_MESSAGE( CHudSquadStatus, SquadMemberDied );
  53. using namespace vgui;
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Constructor
  56. //-----------------------------------------------------------------------------
  57. CHudSquadStatus::CHudSquadStatus( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudSquadStatus" )
  58. {
  59. vgui::Panel *pParent = GetClientMode()->GetViewport();
  60. SetParent( pParent );
  61. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void CHudSquadStatus::Init( void )
  67. {
  68. HOOK_HUD_MESSAGE( CHudSquadStatus, SquadMemberDied );
  69. m_iSquadMembers = 0;
  70. m_iSquadMedics = 0;
  71. m_bSquadMemberAdded = false;
  72. m_bSquadMembersFollowing = true;
  73. m_bSquadMemberJustDied = false;
  74. SetAlpha( 0 );
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose:
  78. //-----------------------------------------------------------------------------
  79. void CHudSquadStatus::Reset( void )
  80. {
  81. Init();
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: Save CPU cycles by letting the HUD system early cull
  85. // costly traversal. Called per frame, return true if thinking and
  86. // painting need to occur.
  87. //-----------------------------------------------------------------------------
  88. bool CHudSquadStatus::ShouldDraw( void )
  89. {
  90. bool bNeedsDraw = false;
  91. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  92. if ( !pPlayer )
  93. return false;
  94. bNeedsDraw = ( pPlayer->m_HL2Local.m_iSquadMemberCount > 0 ||
  95. ( pPlayer->m_HL2Local.m_iSquadMemberCount != m_iSquadMembers ) ||
  96. ( pPlayer->m_HL2Local.m_fSquadInFollowMode != m_bSquadMembersFollowing ) ||
  97. ( m_iSquadMembers > 0 ) ||
  98. ( m_LastMemberColor[3] > 0 ) );
  99. return ( bNeedsDraw && CHudElement::ShouldDraw() );
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose: updates hud icons
  103. //-----------------------------------------------------------------------------
  104. void CHudSquadStatus::OnThink( void )
  105. {
  106. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  107. if ( !pPlayer )
  108. return;
  109. int squadMembers = pPlayer->m_HL2Local.m_iSquadMemberCount;
  110. bool following = pPlayer->m_HL2Local.m_fSquadInFollowMode;
  111. m_iSquadMedics = pPlayer->m_HL2Local.m_iSquadMedicCount;
  112. // Only update if we've changed vars
  113. if ( squadMembers == m_iSquadMembers && following == m_bSquadMembersFollowing )
  114. return;
  115. // update status display
  116. if ( squadMembers > 0)
  117. {
  118. // we have squad members, show the display
  119. GetClientMode()->GetViewportAnimationController()->RunAnimationCommand( this, "alpha", 255.0f, 0.0f, 0.4f, AnimationController::INTERPOLATOR_LINEAR);
  120. }
  121. else
  122. {
  123. // no squad members, hide the display
  124. GetClientMode()->GetViewportAnimationController()->RunAnimationCommand( this, "alpha", 0.0f, 0.0f, 0.4f, AnimationController::INTERPOLATOR_LINEAR);
  125. }
  126. if ( squadMembers > m_iSquadMembers )
  127. {
  128. // someone is added
  129. // reset the last icon color and animate
  130. m_LastMemberColor = m_SquadIconColor;
  131. m_LastMemberColor[3] = 0;
  132. m_bSquadMemberAdded = true;
  133. GetClientMode()->GetViewportAnimationController()->StartAnimationSequence( "SquadMemberAdded" );
  134. }
  135. else if ( squadMembers < m_iSquadMembers )
  136. {
  137. // someone has left
  138. // reset the last icon color and animate
  139. m_LastMemberColor = m_SquadIconColor;
  140. m_bSquadMemberAdded = false;
  141. if (m_bSquadMemberJustDied)
  142. {
  143. GetClientMode()->GetViewportAnimationController()->StartAnimationSequence( "SquadMemberDied" );
  144. m_bSquadMemberJustDied = false;
  145. }
  146. else
  147. {
  148. GetClientMode()->GetViewportAnimationController()->StartAnimationSequence( "SquadMemberLeft" );
  149. }
  150. }
  151. if ( following != m_bSquadMembersFollowing )
  152. {
  153. if ( following )
  154. {
  155. // flash the squad area to indicate they are following
  156. GetClientMode()->GetViewportAnimationController()->StartAnimationSequence( "SquadMembersFollowing" );
  157. }
  158. else
  159. {
  160. // flash the crosshair to indicate the targeted order is in effect
  161. GetClientMode()->GetViewportAnimationController()->StartAnimationSequence( "SquadMembersStationed" );
  162. }
  163. }
  164. m_iSquadMembers = squadMembers;
  165. m_bSquadMembersFollowing = following;
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose: Notification of squad member being killed
  169. //-----------------------------------------------------------------------------
  170. void CHudSquadStatus::MsgFunc_SquadMemberDied(bf_read &msg)
  171. {
  172. m_bSquadMemberJustDied = true;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: draws the power bar
  176. //-----------------------------------------------------------------------------
  177. void CHudSquadStatus::Paint()
  178. {
  179. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  180. if ( !pPlayer )
  181. return;
  182. // draw the suit power bar
  183. surface()->DrawSetTextColor( m_SquadIconColor );
  184. surface()->DrawSetTextFont( m_hIconFont );
  185. int xpos = m_flIconInsetX, ypos = m_flIconInsetY;
  186. for (int i = 0; i < m_iSquadMembers; i++)
  187. {
  188. if (m_bSquadMemberAdded && i == m_iSquadMembers - 1)
  189. {
  190. // draw the last added squad member specially
  191. surface()->DrawSetTextColor( m_LastMemberColor );
  192. }
  193. surface()->DrawSetTextPos(xpos, ypos);
  194. if (i < m_iSquadMedics)
  195. {
  196. surface()->DrawUnicodeChar('M');
  197. }
  198. else
  199. {
  200. surface()->DrawUnicodeChar('C');
  201. }
  202. xpos += m_flIconGap;
  203. }
  204. if (!m_bSquadMemberAdded && m_LastMemberColor[3])
  205. {
  206. // draw the last one in the special color
  207. surface()->DrawSetTextColor( m_LastMemberColor );
  208. surface()->DrawSetTextPos(xpos, ypos);
  209. surface()->DrawUnicodeChar('C');
  210. }
  211. // draw our squad status
  212. wchar_t *text = NULL;
  213. if (m_bSquadMembersFollowing)
  214. {
  215. text = g_pVGuiLocalize->Find("#Valve_Hud_SQUAD_FOLLOWING");
  216. if (!text)
  217. {
  218. text = L"SQUAD FOLLOWING";
  219. }
  220. }
  221. else
  222. {
  223. if ( !player_squad_transient_commands.GetBool() )
  224. {
  225. text = g_pVGuiLocalize->Find("#Valve_Hud_SQUAD_STATIONED");
  226. if (!text)
  227. {
  228. text = L"SQUAD STATIONED";
  229. }
  230. }
  231. }
  232. if (text)
  233. {
  234. surface()->DrawSetTextFont(m_hTextFont);
  235. surface()->DrawSetTextColor(m_SquadTextColor);
  236. surface()->DrawSetTextPos(text_xpos, text_ypos);
  237. surface()->DrawPrintText(text, wcslen(text));
  238. }
  239. }