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.

252 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "vgui_budgetbargraphpanel.h"
  7. #include "vgui_basebudgetpanel.h"
  8. #include <vgui/ISurface.h>
  9. #include "vgui_controls/Label.h"
  10. #include "convar.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. ConVar budget_bargraph_background_alpha( "budget_bargraph_background_alpha", "128", FCVAR_ARCHIVE, "how translucent the budget panel is" );
  14. ConVar budget_peaks_window( "budget_peaks_window", "30", FCVAR_ARCHIVE, "number of frames to look at when figuring out peak frametimes" );
  15. ConVar budget_averages_window( "budget_averages_window", "30", FCVAR_ARCHIVE, "number of frames to look at when figuring out average frametimes" );
  16. ConVar budget_show_peaks( "budget_show_peaks", "1", FCVAR_ARCHIVE, "enable/disable peaks in the budget panel" );
  17. ConVar budget_show_averages( "budget_show_averages", "0", FCVAR_ARCHIVE, "enable/disable averages in the budget panel" );
  18. CBudgetBarGraphPanel::CBudgetBarGraphPanel( CBaseBudgetPanel *pParent, const char *pPanelName ) :
  19. BaseClass( pParent, pPanelName )
  20. {
  21. m_pBudgetPanel = pParent;
  22. SetProportional( false );
  23. SetKeyBoardInputEnabled( false );
  24. SetMouseInputEnabled( false );
  25. SetVisible( true );
  26. SetPaintBackgroundEnabled( true );
  27. SetBgColor( Color( 255, 0, 0, budget_bargraph_background_alpha.GetInt() ) );
  28. }
  29. CBudgetBarGraphPanel::~CBudgetBarGraphPanel()
  30. {
  31. }
  32. void CBudgetBarGraphPanel::GetBudgetGroupTopAndBottom( int id, int &top, int &bottom )
  33. {
  34. // Ask where the corresponding graph label is.
  35. m_pBudgetPanel->GetGraphLabelScreenSpaceTopAndBottom( id, top, bottom );
  36. int tall = bottom - top;
  37. int x = 0;
  38. ScreenToLocal( x, top );
  39. bottom = top + tall;
  40. }
  41. void CBudgetBarGraphPanel::DrawBarAtIndex( int id, float percent )
  42. {
  43. int panelWidth, panelHeight;
  44. GetSize( panelWidth, panelHeight );
  45. int top, bottom;
  46. GetBudgetGroupTopAndBottom( id, top, bottom );
  47. int left = 0;
  48. int right = panelWidth * percent;
  49. int red, green, blue, alpha;
  50. m_pBudgetPanel->GetConfigData().m_BudgetGroupInfo[id].m_Color.GetColor( red, green, blue, alpha );
  51. // DrawFilledRect is panel relative
  52. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  53. vgui::surface()->DrawFilledRect( left, top, right+2, bottom );
  54. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  55. vgui::surface()->DrawFilledRect( left, top+1, right+1, bottom-1 );
  56. vgui::surface()->DrawSetColor( red, green, blue, alpha );
  57. vgui::surface()->DrawFilledRect( left, top+2, right, bottom-2 );
  58. }
  59. void CBudgetBarGraphPanel::DrawTickAtIndex( int id, float percent, int red, int green, int blue, int alpha )
  60. {
  61. if( percent > 1.0f )
  62. {
  63. percent = 1.0f;
  64. }
  65. int panelWidth, panelHeight;
  66. GetSize( panelWidth, panelHeight );
  67. int top, bottom;
  68. GetBudgetGroupTopAndBottom( id, top, bottom );
  69. int right = ( int )( panelWidth * percent + 1.0f );
  70. int left = right - 2;
  71. // DrawFilledRect is panel relative
  72. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  73. vgui::surface()->DrawFilledRect( left-2, top, right+2, bottom );
  74. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  75. vgui::surface()->DrawFilledRect( left-1, top+1, right+1, bottom-1 );
  76. vgui::surface()->DrawSetColor( red, green, blue, alpha );
  77. vgui::surface()->DrawFilledRect( left, top+2, right, bottom-2 );
  78. }
  79. void CBudgetBarGraphPanel::DrawTimeLines( void )
  80. {
  81. int panelWidth, panelHeight;
  82. GetSize( panelWidth, panelHeight );
  83. int i;
  84. int left, right, top, bottom;
  85. top = 0;
  86. bottom = panelHeight;
  87. const CBudgetPanelConfigData &config = m_pBudgetPanel->GetConfigData();
  88. float flValueInterval = config.m_flTimeLabelInterval;
  89. if ( config.m_nLinesPerTimeLabel != 0.0f )
  90. {
  91. flValueInterval = config.m_flTimeLabelInterval / config.m_nLinesPerTimeLabel;
  92. }
  93. int nTotalLines = config.m_flBarGraphRange;
  94. if ( flValueInterval != 0.0f )
  95. {
  96. nTotalLines /= flValueInterval;
  97. }
  98. nTotalLines += 2;
  99. for( i = 0; i < nTotalLines; i++ )
  100. {
  101. int alpha;
  102. if( i % (config.m_nLinesPerTimeLabel*2) == 0 )
  103. {
  104. alpha = 150;
  105. }
  106. else if( i % config.m_nLinesPerTimeLabel == 0 )
  107. {
  108. alpha = 100;
  109. }
  110. else
  111. {
  112. alpha = 50;
  113. }
  114. float flTemp = ( config.m_flBarGraphRange != 0.0f ) ? ( flValueInterval / config.m_flBarGraphRange ) : flValueInterval;
  115. left = -0.5f + panelWidth * ( float )( i * flTemp );
  116. right = left + 1;
  117. vgui::surface()->DrawSetColor( 0, 0, 0, alpha );
  118. vgui::surface()->DrawFilledRect( left-1, top, right+1, bottom );
  119. vgui::surface()->DrawSetColor( 255, 255, 255, alpha );
  120. vgui::surface()->DrawFilledRect( left, top+1, right, bottom-1 );
  121. }
  122. }
  123. void CBudgetBarGraphPanel::DrawInstantaneous()
  124. {
  125. int nGroups, nSamplesPerGroup, nSampleOffset;
  126. const double *pBudgetGroupTimes = m_pBudgetPanel->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  127. if( !pBudgetGroupTimes )
  128. {
  129. return;
  130. }
  131. int i;
  132. for( i = 0; i < nGroups; i++ )
  133. {
  134. float percent = m_pBudgetPanel->GetBudgetGroupPercent( pBudgetGroupTimes[nSamplesPerGroup * i + nSampleOffset] );
  135. DrawBarAtIndex( i, percent );
  136. }
  137. }
  138. void CBudgetBarGraphPanel::DrawPeaks()
  139. {
  140. int nGroups, nSamplesPerGroup, nSampleOffset;
  141. const double *pBudgetGroupTimes = m_pBudgetPanel->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  142. if( !pBudgetGroupTimes )
  143. {
  144. return;
  145. }
  146. int numSamples = budget_peaks_window.GetInt();
  147. int i;
  148. for( i = 0; i < nGroups; i++ )
  149. {
  150. double max = 0;
  151. int j;
  152. for( j = 0; j < numSamples; j++ )
  153. {
  154. double tmp;
  155. int offset = ( nSampleOffset - j + BUDGET_HISTORY_COUNT ) % BUDGET_HISTORY_COUNT;
  156. tmp = pBudgetGroupTimes[i * nSamplesPerGroup + offset];
  157. if( tmp > max )
  158. {
  159. max = tmp;
  160. }
  161. }
  162. float percent = m_pBudgetPanel->GetBudgetGroupPercent( max );
  163. DrawTickAtIndex( i, percent, 255, 0, 0, 255 );
  164. }
  165. }
  166. void CBudgetBarGraphPanel::DrawAverages()
  167. {
  168. int nGroups, nSamplesPerGroup, nSampleOffset;
  169. const double *pBudgetGroupTimes = m_pBudgetPanel->GetBudgetGroupData( nGroups, nSamplesPerGroup, nSampleOffset );
  170. if( !pBudgetGroupTimes )
  171. {
  172. return;
  173. }
  174. int numSamples = budget_averages_window.GetInt();
  175. int i;
  176. for( i = 0; i < nGroups; i++ )
  177. {
  178. int red, green, blue, alpha;
  179. m_pBudgetPanel->GetConfigData().m_BudgetGroupInfo[i].m_Color.GetColor( red, green, blue, alpha );
  180. double sum = 0;
  181. int j;
  182. for( j = 0; j < numSamples; j++ )
  183. {
  184. int offset = ( nSampleOffset - j + BUDGET_HISTORY_COUNT ) % BUDGET_HISTORY_COUNT;
  185. sum += pBudgetGroupTimes[i * nSamplesPerGroup + offset];
  186. }
  187. sum *= ( 1.0f / numSamples );
  188. float percent = m_pBudgetPanel->GetBudgetGroupPercent( sum );
  189. DrawTickAtIndex( i, percent, red, green, blue, alpha );
  190. }
  191. }
  192. void CBudgetBarGraphPanel::Paint( void )
  193. {
  194. int width, height;
  195. GetSize( width, height );
  196. if ( !m_pBudgetPanel->IsDedicated() )
  197. {
  198. SetBgColor( Color( 255, 0, 0, budget_bargraph_background_alpha.GetInt() ) );
  199. }
  200. DrawTimeLines();
  201. DrawInstantaneous();
  202. if( budget_show_peaks.GetBool() )
  203. {
  204. DrawPeaks();
  205. }
  206. if( budget_show_averages.GetBool() )
  207. {
  208. DrawAverages();
  209. }
  210. }