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.

121 lines
3.3 KiB

  1. #include "cbase.h"
  2. #include "tf_asyncpanel.h"
  3. #include "econ_controls.h"
  4. static const float k_flRequestInterval = 5.f;
  5. static const float k_flNeverRequestUpdate = -1.f;
  6. CBaseASyncPanel::CBaseASyncPanel( Panel *pParent, const char *pszPanelName )
  7. : EditablePanel( pParent, pszPanelName )
  8. , m_flLastRequestTime( 0.f )
  9. , m_flLastUpdatedTime( 0.f )
  10. , m_bDataInitialized( false )
  11. , m_bSettingsApplied( false )
  12. {
  13. ivgui()->AddTickSignal( GetVPanel(), 1.f );
  14. }
  15. void CBaseASyncPanel::ApplySchemeSettings( IScheme *pScheme )
  16. {
  17. BaseClass::ApplySchemeSettings( pScheme );
  18. m_bSettingsApplied = true;
  19. m_flLastUpdatedTime = 0.f; // Force a refresh
  20. }
  21. void CBaseASyncPanel::LoadControlSettings(const char *dialogResourceName, const char *pathID, KeyValues *pPreloadedKeyValues, KeyValues *pConditions )
  22. {
  23. m_vecLoadingPanels.Purge();
  24. m_vecPanelsToShow.Purge();
  25. BaseClass::LoadControlSettings( dialogResourceName, pathID, pPreloadedKeyValues, pConditions );
  26. }
  27. void CBaseASyncPanel::PerformLayout()
  28. {
  29. BaseClass::PerformLayout();
  30. FOR_EACH_VEC( m_vecLoadingPanels, i )
  31. {
  32. m_vecLoadingPanels[ i ]->SetVisible( true );
  33. }
  34. FOR_EACH_VEC( m_vecPanelsToShow, i )
  35. {
  36. m_vecPanelsToShow[ i ]->SetVisible( false );
  37. }
  38. }
  39. void CBaseASyncPanel::OnChildSettingsApplied( KeyValues *pInResourceData, Panel *pChild )
  40. {
  41. const char *pszAsync = pInResourceData->GetString( "asynchandling", NULL );
  42. if ( pszAsync == NULL )
  43. return;
  44. if ( FStrEq( pszAsync, "content" ) )
  45. {
  46. m_vecPanelsToShow[ m_vecPanelsToShow.AddToTail() ].Set( pChild );
  47. }
  48. else if ( FStrEq( pszAsync, "loading" ) )
  49. {
  50. m_vecLoadingPanels[ m_vecLoadingPanels.AddToTail() ].Set( pChild );
  51. }
  52. }
  53. bool CBaseASyncPanel::IsInitialized() const
  54. {
  55. return m_bDataInitialized;
  56. }
  57. void CBaseASyncPanel::PresentDataIfReady()
  58. {
  59. if ( m_bDataInitialized && m_bSettingsApplied )
  60. {
  61. FOR_EACH_VEC( m_vecLoadingPanels, i )
  62. {
  63. m_vecLoadingPanels[ i ]->SetVisible( false );
  64. }
  65. FOR_EACH_VEC( m_vecPanelsToShow, i )
  66. {
  67. m_vecPanelsToShow[ i ]->SetVisible( true );
  68. }
  69. // stop ticking. job is done.
  70. ivgui()->RemoveTickSignal( GetVPanel() );
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Checks if data is ready. If so, mark the time and hide the loading image
  75. //-----------------------------------------------------------------------------
  76. void CBaseASyncPanel::CheckForData()
  77. {
  78. m_flLastRequestTime = Plat_FloatTime();
  79. if ( CheckForData_Internal() )
  80. {
  81. m_flLastUpdatedTime = Plat_FloatTime();
  82. m_bDataInitialized = true;
  83. PresentDataIfReady();
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose: Check if we need to check for data
  88. //-----------------------------------------------------------------------------
  89. void CBaseASyncPanel::OnTick()
  90. {
  91. const float flTimeSinceUpdate = Plat_FloatTime() - m_flLastUpdatedTime;
  92. const float flTimeSinceRequest = Plat_FloatTime() - m_flLastRequestTime;
  93. // Need an update if we're beyodn the refresh delay, and our refresh delay isn't k_flNeverRequestUpdate, or if we're just not initialized
  94. const bool bNeedsUpdate = ( ( flTimeSinceUpdate > m_flRefreshDelay ) && ( m_flRefreshDelay != k_flNeverRequestUpdate ) ) || m_flLastUpdatedTime == 0.f;
  95. if ( m_bSettingsApplied && bNeedsUpdate && flTimeSinceRequest > k_flRequestInterval )
  96. {
  97. CheckForData();
  98. }
  99. }