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.

259 lines
6.4 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 "hud_savestatus.h"
  9. #include "iclientmode.h"
  10. #include "view.h"
  11. #include "vgui_controls/Controls.h"
  12. #include "vgui_controls/EditablePanel.h"
  13. #include "engineinterface.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. // Actually must access console primary user id
  17. #if defined( _GAMECONSOLE ) && defined( XBX_GetPrimaryUserId )
  18. #undef XBX_GetPrimaryUserId
  19. #endif
  20. using namespace vgui;
  21. DECLARE_HUDELEMENT( CHudSaveStatus );
  22. #define MIN_HOLD_TIME 1.0f
  23. #define FADE_OUT_TIME 1.0f
  24. CHudSaveStatus::CHudSaveStatus( const char *pElementName ) :
  25. CHudElement( pElementName ), BaseClass( NULL, "HudSaveStatus" )
  26. {
  27. vgui::Panel *pParent = GetClientMode()->GetViewport();
  28. SetParent( pParent );
  29. SetScheme( "basemodui_scheme" );
  30. SetProportional( true );
  31. m_pSavingIcon = NULL;
  32. m_pSavingLabel = NULL;
  33. m_pSavedLabel = NULL;
  34. m_flLastAnimTime = 0;
  35. m_flFadeOutTime = 0;
  36. m_flSaveStartedTime = 0;
  37. m_bNeedsDraw = false;
  38. m_bIsSteamProfileSave = false;
  39. }
  40. void CHudSaveStatus::ApplySchemeSettings( IScheme *pScheme )
  41. {
  42. BaseClass::ApplySchemeSettings( pScheme );
  43. LoadControlSettings( "resource/ui/hud_saving.res" );
  44. m_pSavingIcon = dynamic_cast< vgui::ImagePanel* >( FindChildByName( "SavingIcon" ) );
  45. m_pSavingLabel = dynamic_cast< vgui::Label* >( FindChildByName( "SavingLabel" ) );
  46. if ( m_pSavingLabel )
  47. {
  48. m_pSavingLabel->SetVisible( false );
  49. }
  50. m_pSavedLabel = dynamic_cast< vgui::Label* >( FindChildByName( "SavedLabel" ) );
  51. if ( m_pSavedLabel )
  52. {
  53. m_pSavedLabel->SetVisible( false );
  54. }
  55. // need to keep state consistent, ApplySchemeSettings()
  56. // will get fired after ShouldDraw() in Split Screen
  57. SetSavingLabels( !m_bIsSteamProfileSave );
  58. SetPaintBackgroundEnabled( false );
  59. SetVisible( false );
  60. }
  61. void CHudSaveStatus::SetSavingLabels( bool bIsGameSave )
  62. {
  63. const char *pSavingLabel = bIsGameSave ? "#SFUI_Hud_SavingGame" : "#SFUI_Hud_SavingProfile";
  64. const char *pSavedLabel = bIsGameSave ? "#SFUI_Hud_GameSaved" : "#SFUI_Hud_ProfileSaved";
  65. if ( m_pSavingLabel )
  66. {
  67. m_pSavingLabel->SetText( pSavingLabel );
  68. }
  69. if ( m_pSavedLabel )
  70. {
  71. m_pSavedLabel->SetText( pSavedLabel );
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Save CPU cycles by letting the HUD system early cull
  76. // costly traversal. Called per frame, return true if thinking and
  77. // painting need to occur.
  78. //-----------------------------------------------------------------------------
  79. bool CHudSaveStatus::ShouldDraw()
  80. {
  81. #ifdef _GAMECONSOLE
  82. ASSERT_LOCAL_PLAYER_RESOLVABLE();
  83. int nSlot = GET_ACTIVE_SPLITSCREEN_SLOT();
  84. if ( XBX_GetUserId( nSlot ) != ( int )XBX_GetPrimaryUserId() )
  85. {
  86. return false;
  87. }
  88. #endif
  89. bool bNeedsDraw = false;
  90. if ( m_flSaveStartedTime && Plat_FloatTime() < m_flSaveStartedTime + MIN_HOLD_TIME )
  91. {
  92. // force the element to draw
  93. bNeedsDraw = true;
  94. }
  95. // notify on autosaves, except autosavedangerous
  96. // autosavedangerous notifications are suppressed until marked safe, and might get discarded
  97. if ( engine->IsSaveInProgress() &&
  98. !engine->IsAutoSaveDangerousInProgress() &&
  99. !( IsPC() && engine->IsAutoSaveInProgress() ) )
  100. {
  101. bool bPrimaryUserIsGuest = false;
  102. #if defined( _GAMECONSOLE )
  103. bPrimaryUserIsGuest = ( XBX_GetPrimaryUserIsGuest() != 0 );
  104. #endif
  105. int iController = XBX_GetActiveUserId();
  106. DWORD nStorageDevice = XBX_GetStorageDeviceId( iController );
  107. bool bHasStorageDevice = ( XBX_DescribeStorageDevice( nStorageDevice ) != 0 );
  108. // a guest has no storage, the in-memory saves aren't shown to prevent player confusion as they have no access to the save
  109. if ( !bPrimaryUserIsGuest && bHasStorageDevice )
  110. {
  111. bNeedsDraw = true;
  112. }
  113. }
  114. #if defined( _PS3 )
  115. bool bIsSteamProfileSave = false;
  116. bool bPS3SaveUtilBusy = ps3saveuiapi->IsSaveUtilBusy();
  117. if ( bPS3SaveUtilBusy )
  118. {
  119. uint32 nOpTag = ps3saveuiapi->GetCurrentOpTag();
  120. if ( nOpTag == kSAVE_TAG_WRITE_STEAMINFO )
  121. {
  122. bNeedsDraw = true;
  123. bIsSteamProfileSave = true;
  124. }
  125. }
  126. if ( bPS3SaveUtilBusy && ( m_bIsSteamProfileSave != bIsSteamProfileSave ) )
  127. {
  128. // change to correct label
  129. m_bIsSteamProfileSave = bIsSteamProfileSave;
  130. SetSavingLabels( !m_bIsSteamProfileSave );
  131. }
  132. #endif
  133. if ( m_bNeedsDraw != bNeedsDraw )
  134. {
  135. // transition change
  136. m_bNeedsDraw = bNeedsDraw;
  137. if ( m_bNeedsDraw && !m_flSaveStartedTime )
  138. {
  139. // ensure we hold the start state for a minimum
  140. m_flSaveStartedTime = Plat_FloatTime();
  141. }
  142. else if ( !m_bNeedsDraw )
  143. {
  144. // clear for next time
  145. m_flSaveStartedTime = 0;
  146. }
  147. // the labels go from Saving...
  148. // to Saved.
  149. if ( m_pSavingLabel )
  150. {
  151. m_pSavingLabel->SetVisible( m_bNeedsDraw );
  152. }
  153. if ( m_pSavedLabel )
  154. {
  155. m_pSavedLabel->SetVisible( !m_bNeedsDraw );
  156. }
  157. if ( m_bNeedsDraw )
  158. {
  159. // stop any prior fading
  160. m_flFadeOutTime = 0;
  161. }
  162. else if ( !m_flFadeOutTime )
  163. {
  164. // trigger the fade out
  165. m_flFadeOutTime = Plat_FloatTime();
  166. }
  167. }
  168. if ( m_flFadeOutTime && Plat_FloatTime() < m_flFadeOutTime + FADE_OUT_TIME )
  169. {
  170. // keep drawing during the fade out
  171. bNeedsDraw = true;
  172. }
  173. // respect the hud request to hide (likely a pause)
  174. if ( !CHudElement::ShouldDraw() )
  175. {
  176. bNeedsDraw = false;
  177. if ( !m_bNeedsDraw && m_flFadeOutTime )
  178. {
  179. // stop any mid stream fade out that was occurring
  180. // to prevent a jarring pop if they pause/unpause
  181. // the element was going away anyways
  182. m_flFadeOutTime = 0;
  183. }
  184. }
  185. #if defined( _PS3 )
  186. if ( !bNeedsDraw && m_bIsSteamProfileSave )
  187. {
  188. m_bIsSteamProfileSave = false;
  189. SetSavingLabels( !m_bIsSteamProfileSave );
  190. }
  191. #endif
  192. return bNeedsDraw;
  193. }
  194. void CHudSaveStatus::OnThink()
  195. {
  196. if ( m_pSavingIcon )
  197. {
  198. // clock the anim at 10hz
  199. float time = Plat_FloatTime();
  200. if ( ( m_flLastAnimTime + 0.1f ) < time )
  201. {
  202. m_flLastAnimTime = time;
  203. m_pSavingIcon->SetFrame( m_pSavingIcon->GetFrame() + 1 );
  204. }
  205. }
  206. float flFade = 1.0f;
  207. if ( m_flFadeOutTime )
  208. {
  209. flFade = RemapValClamped( Plat_FloatTime(), m_flFadeOutTime, m_flFadeOutTime + FADE_OUT_TIME, 1.0f, 0.0f );
  210. }
  211. SetAlpha( flFade * 255.0f );
  212. }
  213. void CHudSaveStatus::SaveStarted()
  214. {
  215. // external event to force the hud element to draw
  216. // will automatically time out and go away
  217. if ( !m_flSaveStartedTime )
  218. {
  219. m_flSaveStartedTime = Plat_FloatTime();
  220. }
  221. }