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.

313 lines
7.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hudelement.h"
  8. #include "sfhudmoney.h"
  9. #include "hud_macros.h"
  10. #include "cs_gamerules.h"
  11. #include "sfhudfreezepanel.h"
  12. #include "c_plantedc4.h"
  13. #include "sfhudradar.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. DECLARE_HUDELEMENT( SFHudMoney);
  17. DECLARE_HUD_MESSAGE( SFHudMoney, AdjustMoney );
  18. SFUI_BEGIN_GAME_API_DEF
  19. SFUI_DECL_METHOD( DoneAnimatingAdd ),
  20. SFUI_DECL_METHOD( DoneAnimatingSub ),
  21. SFUI_END_GAME_API_DEF( SFHudMoney, Money );
  22. extern ConVar cl_draw_only_deathnotices;
  23. SFHudMoney::SFHudMoney( const char *value ) : SFHudFlashInterface( value )
  24. {
  25. m_bAnimatingAdd = false;
  26. m_bAnimatingSub = false;
  27. m_nLastMoney = 0;
  28. m_lastEntityIndex = 0;
  29. m_hCash = NULL;
  30. m_hAddCash = NULL;
  31. m_hRemoveCash = NULL;
  32. m_hBuyZoneIcon = NULL;
  33. m_bShowBuyZoneIcon = false;
  34. m_nShiftState = -1;
  35. SetIgnoreGlobalHudDisable( true );
  36. }
  37. SFHudMoney::~SFHudMoney()
  38. {
  39. }
  40. void SFHudMoney::Init( void )
  41. {
  42. HOOK_HUD_MESSAGE( SFHudMoney, AdjustMoney );
  43. }
  44. void SFHudMoney::LevelInit( void )
  45. {
  46. if ( !FlashAPIIsValid() )
  47. {
  48. SFUI_REQUEST_ELEMENT( SF_SS_SLOT( GET_ACTIVE_SPLITSCREEN_SLOT() ), g_pScaleformUI, SFHudMoney, this, Money );
  49. }
  50. }
  51. void SFHudMoney::LevelShutdown( void )
  52. {
  53. if ( FlashAPIIsValid() )
  54. {
  55. RemoveFlashElement();
  56. }
  57. }
  58. bool SFHudMoney::ShouldDraw( void )
  59. {
  60. if ( IsTakingAFreezecamScreenshot() )
  61. return false;
  62. if ( !CSGameRules() )
  63. return false;
  64. if ( CSGameRules()->IsPlayingTraining() || !CSGameRules()->CanSpendMoneyInMap() )
  65. return false;
  66. IViewPortPanel* buyPanel = NULL;
  67. IViewPortPanel *scoreboard = NULL;
  68. if ( GetViewPortInterface() )
  69. {
  70. buyPanel = GetViewPortInterface()->FindPanelByName( PANEL_BUY );
  71. scoreboard = GetViewPortInterface()->FindPanelByName( PANEL_SCOREBOARD );
  72. }
  73. if ( CSGameRules()->GetGamePhase() == GAMEPHASE_MATCH_ENDED && scoreboard && scoreboard->IsVisible() )
  74. return false;
  75. bool bGloballyHidden = GetHud().HudDisabled() && ( !buyPanel || !buyPanel->IsVisible() );
  76. return cl_drawhud.GetBool() && !bGloballyHidden && cl_draw_only_deathnotices.GetBool() == false && CHudElement::ShouldDraw();
  77. }
  78. void SFHudMoney::SetActive( bool bActive )
  79. {
  80. Show( bActive );
  81. CHudElement::SetActive( bActive );
  82. }
  83. void SFHudMoney::Show( bool show )
  84. {
  85. if ( m_FlashAPI && show != m_bActive )
  86. {
  87. WITH_SLOT_LOCKED
  88. {
  89. if ( show )
  90. {
  91. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "showPanel", NULL, 0 );
  92. }
  93. else
  94. {
  95. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "hidePanel", NULL, 0 );
  96. }
  97. }
  98. UpdateCurrentMoneyText();
  99. }
  100. }
  101. void SFHudMoney::FlashReady( void )
  102. {
  103. m_bAnimatingAdd = false;
  104. m_bAnimatingSub = false;
  105. m_cashAdjustmentQueue.SetCount( 0 );
  106. SFVALUE root = m_pScaleformUI->Value_GetMember( m_FlashAPI, "MoneyPanel" );
  107. if ( root )
  108. {
  109. SFVALUE innerPanel = m_pScaleformUI->Value_GetMember( root, "InnerMoneyPanel" );
  110. if ( innerPanel )
  111. {
  112. m_hBuyZoneIcon = m_pScaleformUI->TextObject_MakeTextObjectFromMember( innerPanel, "BuyZoneIcon" );
  113. SFVALUE container = m_pScaleformUI->Value_GetMember( innerPanel, "CashContainer" );
  114. if ( container )
  115. {
  116. m_hCash = m_pScaleformUI->TextObject_MakeTextObjectFromMember( container, "Cash" );
  117. SFVALUE AddCash = m_pScaleformUI->Value_GetMember( container, "AddCash" );
  118. if ( AddCash )
  119. {
  120. m_hAddCash = m_pScaleformUI->TextObject_MakeTextObjectFromMember( AddCash, "AddText" );
  121. g_pScaleformUI->ReleaseValue( AddCash );
  122. }
  123. SFVALUE RemoveCash = m_pScaleformUI->Value_GetMember( container, "RemoveCash" );
  124. if ( RemoveCash )
  125. {
  126. m_hRemoveCash = m_pScaleformUI->TextObject_MakeTextObjectFromMember( RemoveCash, "RemoveText" );
  127. g_pScaleformUI->ReleaseValue( RemoveCash );
  128. }
  129. g_pScaleformUI->ReleaseValue( container );
  130. }
  131. g_pScaleformUI->ReleaseValue( innerPanel );
  132. }
  133. g_pScaleformUI->ReleaseValue( root );
  134. }
  135. if ( m_bActive )
  136. {
  137. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "showPanel", NULL, 0 );
  138. }
  139. else
  140. {
  141. m_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "hidePanel", NULL, 0 );
  142. }
  143. if ( m_hBuyZoneIcon )
  144. {
  145. m_hBuyZoneIcon->SetVisible( false );
  146. m_bShowBuyZoneIcon = false;
  147. }
  148. m_nShiftState = -1;
  149. UpdateCurrentMoneyText();
  150. }
  151. bool SFHudMoney::PreUnloadFlash( void )
  152. {
  153. SafeReleaseSFTextObject( m_hCash );
  154. SafeReleaseSFTextObject( m_hAddCash );
  155. SafeReleaseSFTextObject( m_hRemoveCash );
  156. SafeReleaseSFTextObject( m_hBuyZoneIcon );
  157. return true;
  158. }
  159. void SFHudMoney::ProcessInput( void )
  160. {
  161. C_CSPlayer* pPlayer = pPlayer = GetHudPlayer();
  162. CCSGameRules* pGameRules = CSGameRules();
  163. if ( pGameRules && pPlayer )
  164. {
  165. int entityIndex = pPlayer->entindex();
  166. if ( pPlayer->IsControllingBot() )
  167. entityIndex = pPlayer->GetControlledBotIndex();
  168. // let's always draw attention to when the player's money has changed
  169. if ( entityIndex == m_lastEntityIndex )
  170. {
  171. if ( m_nLastMoney != pPlayer->GetAccount() )
  172. {
  173. // if this is the start of the very first round, don't show the change that can happen from the warmup round to the start round
  174. if ( pGameRules->GetTotalRoundsPlayed() == 0 && pGameRules->GetRoundElapsedTime() < 1 )
  175. {
  176. UpdateCurrentMoneyText();
  177. }
  178. else
  179. {
  180. UpdateMoneyChange( pPlayer->GetAccount() - m_nLastMoney );
  181. }
  182. }
  183. }
  184. else
  185. {
  186. // we changed who we're observing, so just update it directly
  187. m_lastEntityIndex = entityIndex;
  188. UpdateCurrentMoneyText();
  189. }
  190. m_nLastMoney = pPlayer->GetAccount();
  191. bool bShowBuyZoneIcon = CSGameRules()->CanSpendMoneyInMap() &&
  192. !pGameRules->IsBuyTimeElapsed() &&
  193. pPlayer->IsInBuyZone();
  194. if ( m_hBuyZoneIcon && ( m_bShowBuyZoneIcon != bShowBuyZoneIcon ) )
  195. {
  196. m_bShowBuyZoneIcon = bShowBuyZoneIcon;
  197. WITH_SLOT_LOCKED
  198. {
  199. m_hBuyZoneIcon->SetVisible( m_bShowBuyZoneIcon );
  200. }
  201. }
  202. int nShiftState = 0;
  203. bool bRoundRadar = ( GET_HUDELEMENT( SFHudRadar ) )->m_bRound;
  204. nShiftState = ( pGameRules->IsHostageRescueMap() || !bRoundRadar ) ? 1: nShiftState;
  205. nShiftState = pPlayer->IsBuyMenuOpen() ? 2: nShiftState;
  206. if ( FlashAPIIsValid() && ( m_nShiftState != nShiftState ) )
  207. {
  208. m_nShiftState = nShiftState;
  209. WITH_SFVALUEARRAY_SLOT_LOCKED( data, 1 )
  210. {
  211. m_pScaleformUI->ValueArray_SetElement( data, 0, nShiftState );
  212. g_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "SetShift", data, 1 );
  213. }
  214. }
  215. }
  216. }
  217. bool SFHudMoney::MsgFunc_AdjustMoney( const CCSUsrMsg_AdjustMoney &msg )
  218. {
  219. return true;
  220. }
  221. void SFHudMoney::UpdateMoneyChange( int nDelta )
  222. {
  223. if ( FlashAPIIsValid() )
  224. {
  225. WITH_SFVALUEARRAY_SLOT_LOCKED( data, 1 )
  226. {
  227. m_pScaleformUI->ValueArray_SetElement( data, 0, nDelta );
  228. g_pScaleformUI->Value_InvokeWithoutReturn( m_FlashAPI, "DisplayMoneyAdjustment", data, 1 );
  229. }
  230. }
  231. if ( nDelta < 0 )
  232. {
  233. UpdateCurrentMoneyText();
  234. m_bAnimatingSub = true;
  235. }
  236. else
  237. {
  238. m_bAnimatingAdd = true;
  239. }
  240. }
  241. void SFHudMoney::DoneAnimatingAdd( SCALEFORM_CALLBACK_ARGS_DECL )
  242. {
  243. m_bAnimatingAdd = false;
  244. UpdateCurrentMoneyText();
  245. }
  246. void SFHudMoney::DoneAnimatingSub( SCALEFORM_CALLBACK_ARGS_DECL )
  247. {
  248. m_bAnimatingSub = false;
  249. }
  250. void SFHudMoney::UpdateCurrentMoneyText( void )
  251. {
  252. C_CSPlayer *pPlayer = GetHudPlayer();
  253. if ( pPlayer && m_hCash )
  254. {
  255. WITH_SLOT_LOCKED
  256. {
  257. m_hCash->SetText( CFmtStr( "$%d", pPlayer->GetAccount() ) );
  258. }
  259. }
  260. }