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.

440 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "vgui_basebudgetpanel.h"
  7. #include "vgui_controls/Label.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. CBaseBudgetPanel::CBaseBudgetPanel( vgui::Panel *pParent, const char *pElementName )
  11. : vgui::Panel( pParent, pElementName )
  12. {
  13. m_BudgetHistoryOffset = 0;
  14. SetProportional( false );
  15. SetKeyBoardInputEnabled( false );
  16. SetMouseInputEnabled( false );
  17. SetVisible( true );
  18. m_pBudgetHistoryPanel = NULL;
  19. m_pBudgetBarGraphPanel = NULL;
  20. SetZPos( 1001 );
  21. m_bDedicated = false;
  22. }
  23. CBaseBudgetPanel::~CBaseBudgetPanel()
  24. {
  25. }
  26. float CBaseBudgetPanel::GetBudgetGroupPercent( float value )
  27. {
  28. if ( m_ConfigData.m_flBarGraphRange == 0.0f )
  29. return 1.0f;
  30. return value / m_ConfigData.m_flBarGraphRange;
  31. }
  32. const double *CBaseBudgetPanel::GetBudgetGroupData( int &nGroups, int &nSamplesPerGroup, int &nSampleOffset ) const
  33. {
  34. nGroups = m_ConfigData.m_BudgetGroupInfo.Count();
  35. nSamplesPerGroup = BUDGET_HISTORY_COUNT;
  36. nSampleOffset = m_BudgetHistoryOffset;
  37. if( m_BudgetGroupTimes.Count() == 0 )
  38. {
  39. return NULL;
  40. }
  41. else
  42. {
  43. return &m_BudgetGroupTimes[0].m_Time[0];
  44. }
  45. }
  46. void CBaseBudgetPanel::ClearTimesForAllGroupsForThisFrame( void )
  47. {
  48. int i;
  49. for( i = 0; i < m_ConfigData.m_BudgetGroupInfo.Count(); i++ )
  50. {
  51. m_BudgetGroupTimes[i].m_Time[m_BudgetHistoryOffset] = 0.0;
  52. }
  53. }
  54. void CBaseBudgetPanel::ClearAllTimesForGroup( int groupID )
  55. {
  56. int i;
  57. for( i = 0; i < BUDGET_HISTORY_COUNT; i++ )
  58. {
  59. m_BudgetGroupTimes[groupID].m_Time[i] = 0.0;
  60. }
  61. }
  62. void CBaseBudgetPanel::OnConfigDataChanged( const CBudgetPanelConfigData &data )
  63. {
  64. int oldNumGroups = m_ConfigData.m_BudgetGroupInfo.Count();
  65. // Copy in the config data and rebuild everything.
  66. Rebuild( data );
  67. if ( m_ConfigData.m_BudgetGroupInfo.Count() > m_BudgetGroupTimes.Count() )
  68. {
  69. m_BudgetGroupTimes.EnsureCount( m_ConfigData.m_BudgetGroupInfo.Count() );
  70. for ( int i = oldNumGroups; i < m_ConfigData.m_BudgetGroupInfo.Count(); i++ )
  71. {
  72. ClearAllTimesForGroup( i );
  73. }
  74. }
  75. else
  76. {
  77. m_BudgetGroupTimes.SetSize( m_ConfigData.m_BudgetGroupInfo.Count() );
  78. for ( int i = 0; i < m_BudgetGroupTimes.Count(); i++ )
  79. {
  80. ClearAllTimesForGroup( i );
  81. }
  82. }
  83. InvalidateLayout( false, true );
  84. }
  85. void CBaseBudgetPanel::ResetAll()
  86. {
  87. m_ConfigData.m_BudgetGroupInfo.Purge();
  88. for ( int i=0; i < m_GraphLabels.Count(); i++ )
  89. m_GraphLabels[i]->MarkForDeletion();
  90. m_GraphLabels.Purge();
  91. for ( int i=0; i < m_TimeLabels.Count(); i++ )
  92. m_TimeLabels[i]->MarkForDeletion();
  93. m_TimeLabels.Purge();
  94. }
  95. void CBaseBudgetPanel::Rebuild( const CBudgetPanelConfigData &data )
  96. {
  97. int oldNumBudgetGroups = m_ConfigData.m_BudgetGroupInfo.Count();
  98. int oldNumHistoryLabels = m_ConfigData.m_HistoryLabelValues.Count();
  99. int oldNumTimeLabels = m_TimeLabels.Count();
  100. // Copy the new config in.
  101. m_ConfigData = data;
  102. int nParentWidth, nParentHeight;
  103. GetParent()->GetSize( nParentWidth, nParentHeight );
  104. if ( m_ConfigData.m_Width > nParentWidth )
  105. {
  106. m_ConfigData.m_Width = nParentWidth;
  107. }
  108. if ( m_ConfigData.m_Height > nParentHeight )
  109. {
  110. m_ConfigData.m_Height = nParentHeight;
  111. }
  112. if ( m_ConfigData.m_xCoord + m_ConfigData.m_Width > nParentWidth )
  113. {
  114. m_ConfigData.m_xCoord = nParentWidth - m_ConfigData.m_Width;
  115. }
  116. if ( m_ConfigData.m_yCoord + m_ConfigData.m_Height > nParentHeight )
  117. {
  118. m_ConfigData.m_yCoord = nParentHeight - m_ConfigData.m_Height;
  119. }
  120. // Recreate the history and bar graph panels.
  121. if( m_pBudgetHistoryPanel )
  122. {
  123. m_pBudgetHistoryPanel->MarkForDeletion();
  124. }
  125. m_pBudgetHistoryPanel = new CBudgetHistoryPanel( this, "FrametimeHistory" );
  126. if( m_pBudgetBarGraphPanel )
  127. {
  128. m_pBudgetBarGraphPanel->MarkForDeletion();
  129. }
  130. m_pBudgetBarGraphPanel = new CBudgetBarGraphPanel( this, "BudgetBarGraph" );
  131. // Create any new labels we need.
  132. int i;
  133. if ( m_ConfigData.m_BudgetGroupInfo.Count() > m_GraphLabels.Count() )
  134. {
  135. m_GraphLabels.EnsureCount( m_ConfigData.m_BudgetGroupInfo.Count() );
  136. for( i = oldNumBudgetGroups; i < m_ConfigData.m_BudgetGroupInfo.Count(); i++ )
  137. {
  138. const char *pBudgetGroupName = m_ConfigData.m_BudgetGroupInfo[i].m_Name.String();
  139. m_GraphLabels[i] = new vgui::Label( this, pBudgetGroupName, pBudgetGroupName );
  140. }
  141. }
  142. else
  143. {
  144. while ( m_GraphLabels.Count() > m_ConfigData.m_BudgetGroupInfo.Count() )
  145. {
  146. m_GraphLabels[m_GraphLabels.Count()-1]->MarkForDeletion();
  147. m_GraphLabels.Remove( m_GraphLabels.Count()-1 );
  148. }
  149. }
  150. Assert( m_GraphLabels.Count() == m_ConfigData.m_BudgetGroupInfo.Count() );
  151. // Create new history labels.
  152. if ( m_ConfigData.m_HistoryLabelValues.Count() > m_HistoryLabels.Count() )
  153. {
  154. m_HistoryLabels.EnsureCount( m_ConfigData.m_HistoryLabelValues.Count() );
  155. for ( i=oldNumHistoryLabels; i < m_HistoryLabels.Count(); i++ )
  156. {
  157. m_HistoryLabels[i] = new vgui::Label( this, "history label", "history label" );
  158. }
  159. }
  160. else
  161. {
  162. while ( m_HistoryLabels.Count() > m_ConfigData.m_HistoryLabelValues.Count() )
  163. {
  164. m_HistoryLabels[m_HistoryLabels.Count()-1]->MarkForDeletion();
  165. m_HistoryLabels.Remove( m_HistoryLabels.Count()-1 );
  166. }
  167. }
  168. SetHistoryLabelText();
  169. // Note: the time lines still use milliseconds for the computations about where to draw them,
  170. // but each BudgetGroupDataType_t has its own scale.
  171. int nTimeLabels = m_ConfigData.m_flBarGraphRange + data.m_flTimeLabelInterval;
  172. if ( data.m_flTimeLabelInterval != 0.0f )
  173. {
  174. nTimeLabels /= data.m_flTimeLabelInterval;
  175. }
  176. if ( nTimeLabels > m_TimeLabels.Count() )
  177. {
  178. m_TimeLabels.EnsureCount( nTimeLabels );
  179. for( i = oldNumTimeLabels; i < m_TimeLabels.Count(); i++ )
  180. {
  181. char name[1024];
  182. Q_snprintf( name, sizeof( name ), "time_label_%d", i );
  183. m_TimeLabels[i] = new vgui::Label( this, name, "TEXT NOT SET YET" );
  184. }
  185. }
  186. else
  187. {
  188. while ( m_TimeLabels.Count() > nTimeLabels )
  189. {
  190. m_TimeLabels[m_TimeLabels.Count()-1]->MarkForDeletion();
  191. m_TimeLabels.Remove( m_TimeLabels.Count()-1 );
  192. }
  193. }
  194. SetTimeLabelText();
  195. }
  196. void CBaseBudgetPanel::UpdateWindowGeometry()
  197. {
  198. if( m_ConfigData.m_Width > BUDGET_HISTORY_COUNT )
  199. {
  200. m_ConfigData.m_Width = BUDGET_HISTORY_COUNT;
  201. }
  202. SetPos( m_ConfigData.m_xCoord, m_ConfigData.m_yCoord );
  203. SetSize( m_ConfigData.m_Width, m_ConfigData.m_Height );
  204. }
  205. void CBaseBudgetPanel::PerformLayout()
  206. {
  207. if ( !m_pBudgetHistoryPanel || !m_pBudgetBarGraphPanel )
  208. return;
  209. int maxFPSLabelWidth = 0;
  210. int i;
  211. for( i = 0; i < m_HistoryLabels.Count(); i++ )
  212. {
  213. int labelWidth, labelHeight;
  214. m_HistoryLabels[i]->GetContentSize( labelWidth, labelHeight );
  215. if( labelWidth > maxFPSLabelWidth )
  216. {
  217. maxFPSLabelWidth = labelWidth;
  218. }
  219. }
  220. m_pBudgetHistoryPanel->SetRange( 0, m_ConfigData.m_flHistoryRange );
  221. float bottomOfHistoryPercentage = m_ConfigData.m_flBottomOfHistoryFraction;
  222. UpdateWindowGeometry();
  223. int x, y, totalWidth, totalHeight;
  224. int totalHeightMinusTimeLabels;
  225. GetPos( x, y );
  226. GetSize( totalWidth, totalHeight );
  227. int maxTimeLabelHeight = 0;
  228. for( i = 0; i < m_TimeLabels.Count(); i++ )
  229. {
  230. int labelWidth, labelHeight;
  231. m_TimeLabels[i]->GetContentSize( labelWidth, labelHeight );
  232. maxTimeLabelHeight = MAX( maxTimeLabelHeight, labelHeight );
  233. }
  234. totalHeightMinusTimeLabels = totalHeight - maxTimeLabelHeight;
  235. m_pBudgetHistoryPanel->SetPos( 0, 0 );
  236. int budgetHistoryHeight = totalHeightMinusTimeLabels * bottomOfHistoryPercentage;
  237. m_pBudgetHistoryPanel->SetSize( totalWidth - maxFPSLabelWidth,
  238. budgetHistoryHeight );
  239. int maxLabelWidth = 0;
  240. for( i = 0; i < m_GraphLabels.Count(); i++ )
  241. {
  242. int width, height;
  243. m_GraphLabels[i]->GetContentSize( width, height );
  244. if( maxLabelWidth < width )
  245. {
  246. maxLabelWidth = width;
  247. }
  248. }
  249. m_pBudgetBarGraphPanel->SetPos( maxLabelWidth,
  250. totalHeightMinusTimeLabels * bottomOfHistoryPercentage );
  251. m_pBudgetBarGraphPanel->SetSize( totalWidth - maxLabelWidth,
  252. totalHeightMinusTimeLabels * ( 1 - bottomOfHistoryPercentage ) );
  253. for( i = 0; i < m_GraphLabels.Count(); i++ )
  254. {
  255. m_GraphLabels[i]->SetPos( 0,
  256. ( bottomOfHistoryPercentage * totalHeightMinusTimeLabels ) +
  257. ( i * totalHeightMinusTimeLabels *
  258. ( 1 - bottomOfHistoryPercentage ) ) / m_ConfigData.m_BudgetGroupInfo.Count() );
  259. // fudge height by 1 for rounding
  260. m_GraphLabels[i]->SetSize( maxLabelWidth, 1 + ( totalHeightMinusTimeLabels *
  261. ( 1 - bottomOfHistoryPercentage ) ) / m_ConfigData.m_BudgetGroupInfo.Count() );
  262. m_GraphLabels[i]->SetContentAlignment( vgui::Label::a_east );
  263. }
  264. // Note: the time lines still use milliseconds for the computations about where to draw them,
  265. // but each BudgetGroupDataType_t has its own scale.
  266. float fRange = m_ConfigData.m_flBarGraphRange;
  267. for( i = 0; i < m_TimeLabels.Count(); i++ )
  268. {
  269. int labelWidth, labelHeight;
  270. m_TimeLabels[i]->GetContentSize( labelWidth, labelHeight );
  271. int x = maxLabelWidth + ( i * m_ConfigData.m_flTimeLabelInterval ) / fRange * ( totalWidth - maxLabelWidth );
  272. m_TimeLabels[i]->SetPos( x - ( labelWidth * 0.5 ), totalHeight - labelHeight );
  273. m_TimeLabels[i]->SetSize( labelWidth, labelHeight );
  274. m_TimeLabels[i]->SetContentAlignment( vgui::Label::a_east );
  275. }
  276. // position the fps labels
  277. fRange = m_ConfigData.m_flHistoryRange;
  278. for( i = 0; i < m_HistoryLabels.Count(); i++ )
  279. {
  280. int labelWidth, labelHeight;
  281. m_HistoryLabels[i]->GetContentSize( labelWidth, labelHeight );
  282. float y = (fRange != 0) ? budgetHistoryHeight * m_ConfigData.m_HistoryLabelValues[i] / ( float )fRange : 0.0f;
  283. int top = ( int )( budgetHistoryHeight - y - 1 - labelHeight * 0.5f );
  284. m_HistoryLabels[i]->SetPos( totalWidth - maxFPSLabelWidth, top );
  285. m_HistoryLabels[i]->SetSize( labelWidth, labelHeight );
  286. m_HistoryLabels[i]->SetContentAlignment( vgui::Label::a_east );
  287. }
  288. }
  289. void CBaseBudgetPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  290. {
  291. BaseClass::ApplySchemeSettings( pScheme );
  292. int i;
  293. for( i = 0; i < m_ConfigData.m_BudgetGroupInfo.Count(); i++ )
  294. {
  295. m_GraphLabels[i]->SetFgColor( m_ConfigData.m_BudgetGroupInfo[i].m_Color );
  296. m_GraphLabels[i]->SetBgColor( Color( 0, 0, 0, 255 ) );
  297. m_GraphLabels[i]->SetPaintBackgroundEnabled( false );
  298. m_GraphLabels[i]->SetFont( pScheme->GetFont( "BudgetLabel", IsProportional() ) );
  299. if ( m_bDedicated )
  300. {
  301. m_GraphLabels[i]->SetBgColor( pScheme->GetColor( "ControlBG", Color( 0, 0, 0, 255 ) ) );
  302. }
  303. }
  304. for( i = 0; i < m_TimeLabels.Count(); i++ )
  305. {
  306. int red, green, blue, alpha;
  307. red = green = blue = alpha = 255;
  308. m_TimeLabels[i]->SetFgColor( Color( red, green, blue, alpha ) );
  309. m_TimeLabels[i]->SetBgColor( Color( 0, 0, 0, 255 ) );
  310. m_TimeLabels[i]->SetPaintBackgroundEnabled( false );
  311. m_TimeLabels[i]->SetFont( pScheme->GetFont( "BudgetLabel", IsProportional() ) );
  312. if ( m_bDedicated )
  313. {
  314. m_TimeLabels[i]->SetBgColor( pScheme->GetColor( "ControlBG", Color( 0, 0, 0, 255 ) ) );
  315. }
  316. }
  317. for( i = 0; i < m_HistoryLabels.Count(); i++ )
  318. {
  319. int red, green, blue, alpha;
  320. red = green = blue = alpha = 255;
  321. m_HistoryLabels[i]->SetFgColor( Color( red, green, blue, alpha ) );
  322. m_HistoryLabels[i]->SetBgColor( Color( 0, 0, 0, 255 ) );
  323. m_HistoryLabels[i]->SetPaintBackgroundEnabled( false );
  324. m_HistoryLabels[i]->SetFont( pScheme->GetFont( "BudgetLabel", IsProportional() ) );
  325. if ( m_bDedicated )
  326. {
  327. m_HistoryLabels[i]->SetBgColor( pScheme->GetColor( "ControlBG", Color( 0, 0, 0, 255 ) ) );
  328. }
  329. }
  330. m_hFont = pScheme->GetFont( "DefaultFixed" );
  331. if ( m_bDedicated )
  332. {
  333. SetBgColor( pScheme->GetColor( "ControlBG", Color( 0, 0, 0, 255 ) ) );
  334. }
  335. SetPaintBackgroundEnabled( true );
  336. }
  337. void CBaseBudgetPanel::PaintBackground()
  338. {
  339. if ( m_bDedicated )
  340. {
  341. m_pBudgetBarGraphPanel->SetBgColor( GetBgColor() );
  342. }
  343. else
  344. {
  345. SetBgColor( Color( 0, 0, 0, m_ConfigData.m_flBackgroundAlpha ) );
  346. }
  347. BaseClass::PaintBackground();
  348. }
  349. void CBaseBudgetPanel::Paint()
  350. {
  351. m_pBudgetHistoryPanel->SetData( &m_BudgetGroupTimes[0].m_Time[0], GetNumCachedBudgetGroups(), BUDGET_HISTORY_COUNT, m_BudgetHistoryOffset );
  352. BaseClass::Paint();
  353. }
  354. void CBaseBudgetPanel::MarkForFullRepaint()
  355. {
  356. Repaint();
  357. m_pBudgetHistoryPanel->Repaint();
  358. m_pBudgetBarGraphPanel->Repaint();
  359. }
  360. //-----------------------------------------------------------------------------
  361. // Purpose:
  362. //-----------------------------------------------------------------------------
  363. void CBaseBudgetPanel::GetGraphLabelScreenSpaceTopAndBottom( int id, int &top, int &bottom )
  364. {
  365. int x = 0;
  366. int y = 0;
  367. m_GraphLabels[id]->LocalToScreen( x, y );
  368. top = y;
  369. bottom = top + m_GraphLabels[id]->GetTall();
  370. }