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.

196 lines
5.7 KiB

  1. //===== Copyright � 1996-2008, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Tip display during level loads.
  4. //
  5. //===========================================================================//
  6. #include "loadingtippanel.h"
  7. #include "filesystem.h"
  8. #include "keyvalues.h"
  9. #include "vgui/ISurface.h"
  10. #include "engineinterface.h"
  11. #include "vstdlib/random.h"
  12. #include "fmtstr.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. using namespace vgui;
  16. ConVar ui_loading_tip_refresh( "ui_loading_tip_refresh", "5", FCVAR_DEVELOPMENTONLY );
  17. ConVar ui_loading_tip_f1( "ui_loading_tip_f1", "0.05", FCVAR_DEVELOPMENTONLY );
  18. ConVar ui_loading_tip_f2( "ui_loading_tip_f2", "0.40", FCVAR_DEVELOPMENTONLY );
  19. //--------------------------------------------------------------------------------------------------------
  20. CLoadingTipPanel::CLoadingTipPanel( Panel *pParent ) : EditablePanel( pParent, "loadingtippanel" )
  21. {
  22. m_flLastTipTime = 0.f;
  23. m_iCurrentTip = 0;
  24. m_pTipIcon = NULL;
  25. m_smearColor = Color( 0, 0, 0, 255 );
  26. SetupTips();
  27. }
  28. //--------------------------------------------------------------------------------------------------------
  29. CLoadingTipPanel::~CLoadingTipPanel()
  30. {
  31. }
  32. //--------------------------------------------------------------------------------------------------------
  33. void CLoadingTipPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  34. {
  35. BaseClass::ApplySchemeSettings( pScheme );
  36. m_smearColor = pScheme->GetColor( "Frame.SmearColor", Color( 0, 0, 0, 225 ) );
  37. ReloadScheme();
  38. }
  39. //--------------------------------------------------------------------------------------------------------
  40. void CLoadingTipPanel::ReloadScheme( void )
  41. {
  42. LoadControlSettings( "Resource/UI/loadingtippanel.res" );
  43. m_pTipIcon = dynamic_cast< vgui::ImagePanel* >( FindChildByName( "TipIcon" ) );
  44. NextTip();
  45. }
  46. //--------------------------------------------------------------------------------------------------------
  47. void CLoadingTipPanel::SetupTips( void )
  48. {
  49. #ifdef _DEMO
  50. KeyValues *pKV = new KeyValues( "Tips" );
  51. KeyValues::AutoDelete autodelete( pKV );
  52. if ( !pKV->LoadFromFile( g_pFullFileSystem, "scripts/tips.txt", "GAME" ) )
  53. {
  54. AssertMsg( false, "failed to load tips!" );
  55. return;
  56. }
  57. for ( KeyValues *pKey = pKV->FindKey( "SurvivorTips" )->GetFirstSubKey(); pKey; pKey = pKey->GetNextKey() )
  58. {
  59. sTipInfo info;
  60. V_strncpy( info.szTipTitle, "", MAX_TIP_LENGTH );
  61. V_strncpy( info.szTipString, pKey->GetName(), MAX_TIP_LENGTH );
  62. V_strncpy( info.szTipImage, "achievements/ACH_SURVIVE_BRIDGE", MAX_TIP_LENGTH );
  63. m_Tips.AddToTail( info );
  64. }
  65. #else
  66. TitleAchievementsDescription_t const *desc = g_pMatchFramework->GetMatchTitle()->DescribeTitleAchievements();
  67. for ( ; desc->m_szAchievementName; ++desc )
  68. {
  69. sTipInfo info;
  70. V_snprintf( info.szTipTitle, MAX_TIP_LENGTH, "#%s_NAME", desc->m_szAchievementName );
  71. V_snprintf( info.szTipString, MAX_TIP_LENGTH, "#%s_DESC", desc->m_szAchievementName );
  72. V_snprintf( info.szTipImage, MAX_TIP_LENGTH, "achievements/%s", desc->m_szAchievementName );
  73. m_Tips.AddToTail( info );
  74. }
  75. #endif
  76. }
  77. //--------------------------------------------------------------------------------------------------------
  78. void CLoadingTipPanel::NextTip( void )
  79. {
  80. if ( !IsEnabled() )
  81. return;
  82. if ( !m_Tips.Count() )
  83. return;
  84. if ( !m_flLastTipTime )
  85. {
  86. // Initialize timer on first render
  87. m_flLastTipTime = Plat_FloatTime();
  88. return;
  89. }
  90. if ( Plat_FloatTime() - m_flLastTipTime < ui_loading_tip_refresh.GetFloat() )
  91. return;
  92. m_flLastTipTime = Plat_FloatTime();
  93. m_iCurrentTip = RandomInt( 0, m_Tips.Count() - 1 );
  94. if ( !m_Tips.IsValidIndex( m_iCurrentTip ) )
  95. return;
  96. sTipInfo info = m_Tips[m_iCurrentTip];
  97. if ( m_pTipIcon )
  98. {
  99. m_pTipIcon->SetImage( info.szTipImage );
  100. }
  101. SetControlString( "TipTitle", info.szTipTitle );
  102. SetControlString( "TipText", info.szTipString );
  103. // Set our control visible
  104. SetVisible( true );
  105. }
  106. #define TOP_BORDER_HEIGHT 21
  107. #define BOTTOM_BORDER_HEIGHT 21
  108. int CLoadingTipPanel::DrawSmearBackgroundFade( int x0, int y0, int x1, int y1 )
  109. {
  110. int wide = x1 - x0;
  111. int tall = y1 - y0;
  112. int topTall = scheme()->GetProportionalScaledValue( TOP_BORDER_HEIGHT );
  113. int bottomTall = scheme()->GetProportionalScaledValue( BOTTOM_BORDER_HEIGHT );
  114. float f1 = ui_loading_tip_f1.GetFloat();
  115. float f2 = ui_loading_tip_f2.GetFloat();
  116. topTall = 1.00f * topTall;
  117. bottomTall = 1.00f * bottomTall;
  118. int middleTall = tall - ( topTall + bottomTall );
  119. if ( middleTall < 0 )
  120. {
  121. middleTall = 0;
  122. }
  123. surface()->DrawSetColor( m_smearColor );
  124. y0 += topTall;
  125. if ( middleTall )
  126. {
  127. // middle
  128. surface()->DrawFilledRectFade( x0, y0, x0 + f1*wide, y0 + middleTall, 0, 255, true );
  129. surface()->DrawFilledRectFade( x0 + f1*wide, y0, x0 + f2*wide, y0 + middleTall, 255, 255, true );
  130. surface()->DrawFilledRectFade( x0 + f2*wide, y0, x0 + wide, y0 + middleTall, 255, 0, true );
  131. y0 += middleTall;
  132. }
  133. return topTall + middleTall + bottomTall;
  134. }
  135. //--------------------------------------------------------------------------------------------------------
  136. void CLoadingTipPanel::PaintBackground( void )
  137. {
  138. BaseClass::PaintBackground();
  139. DrawSmearBackgroundFade(
  140. 0,
  141. -scheme()->GetProportionalScaledValue( 20 ),
  142. GetWide(),
  143. GetTall() );
  144. }
  145. void PrecacheLoadingTipIcons()
  146. {
  147. TitleAchievementsDescription_t const *desc = g_pMatchFramework->GetMatchTitle()->DescribeTitleAchievements();
  148. for ( ; desc->m_szAchievementName; ++desc )
  149. {
  150. CFmtStr imageString( "vgui/achievements/%s", desc->m_szAchievementName );
  151. int nImageId = vgui::surface()->DrawGetTextureId( imageString );
  152. if ( nImageId == -1 )
  153. {
  154. nImageId = vgui::surface()->CreateNewTextureID();
  155. vgui::surface()->DrawSetTextureFile( nImageId, imageString, true, false );
  156. }
  157. }
  158. }