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.

359 lines
9.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //
  4. //=============================================================================//
  5. #include "cbase.h"
  6. #include "tier3/tier3.h"
  7. #include "vgui/ILocalize.h"
  8. #include "lifetime_stats_page.h"
  9. #include <vgui_controls/SectionedListPanel.h>
  10. #include "cs_client_gamestats.h"
  11. #include "filesystem.h"
  12. #include "cs_weapon_parse.h"
  13. #include "buy_presets/buy_presets.h"
  14. #include "../vgui_controls/ScrollBar.h"
  15. #include "stat_card.h"
  16. using namespace vgui;
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. KeyValues *g_pPreloadedCSBaseStatGroupLayout = NULL;
  20. //-----------------------------------------------------------------------------
  21. // Purpose: creates child panels, passes down name to pick up any settings from res files.
  22. //-----------------------------------------------------------------------------
  23. CBaseStatsPage::CBaseStatsPage(vgui::Panel *parent, const char *name) : BaseClass(parent, "CSBaseStatsDialog")
  24. {
  25. vgui::IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  26. m_listItemFont = pScheme->GetFont( "StatsPageText", IsProportional() );
  27. m_statsList = new SectionedListPanel( this, "StatsList" );
  28. m_statsList->SetClickable(false);
  29. m_statsList->SetDrawHeaders(false);
  30. m_bottomBar = new ImagePanel(this, "BottomBar");
  31. m_pGroupsList = new vgui::PanelListPanel( this, "listpanel_groups" );
  32. m_pGroupsList->SetFirstColumnWidth( 0 );
  33. SetBounds(0, 0, 900, 780);
  34. SetMinimumSize( 256, 780 );
  35. SetBgColor(GetSchemeColor("ListPanel.BgColor", GetBgColor(), pScheme));
  36. m_pStatCard = new StatCard(this, "ignored");
  37. ListenForGameEvent( "player_stats_updated" );
  38. m_bStatsDirty = true;
  39. }
  40. CBaseStatsPage::~CBaseStatsPage()
  41. {
  42. delete m_statsList;
  43. }
  44. void CBaseStatsPage::MoveToFront()
  45. {
  46. UpdateStatsData();
  47. m_pStatCard->UpdateInfo();
  48. }
  49. void CBaseStatsPage::UpdateStatsData()
  50. {
  51. // Hide the group list scrollbar
  52. if (m_pGroupsList->GetScrollbar())
  53. {
  54. m_pGroupsList->GetScrollbar()->SetWide(0);
  55. }
  56. UpdateGroupPanels();
  57. RepopulateStats();
  58. m_bStatsDirty = false;
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Loads settings from statsdialog.res in hl2/resource/ui/
  62. //-----------------------------------------------------------------------------
  63. void CBaseStatsPage::ApplySchemeSettings( vgui::IScheme *pScheme )
  64. {
  65. BaseClass::ApplySchemeSettings( pScheme );
  66. LoadControlSettings("resource/ui/CSBaseStatsDialog.res");
  67. m_statsList->SetClickable(false);
  68. m_statsList->SetDrawHeaders(false);
  69. m_statsList->SetVerticalScrollbar(true);
  70. SetBgColor(Color(86,86,86,255));
  71. //Remove any pre-existing sections and add then fresh (this can happen on a resolution change)
  72. m_statsList->RemoveAllSections();
  73. m_statsList->AddSection( 0, "Players");
  74. m_statsList->SetFontSection(0, m_listItemFont);
  75. m_pGroupsList->SetBgColor(Color(86,86,86,255));
  76. m_statsList->SetBgColor(Color(52,52,52,255));
  77. }
  78. void CBaseStatsPage::SetActiveStatGroup (CBaseStatGroupPanel* groupPanel)
  79. {
  80. for (int i = 0; i < m_pGroupsList->GetItemCount(); i++)
  81. {
  82. CBaseStatGroupPanel *pPanel = (CBaseStatGroupPanel*)m_pGroupsList->GetItemPanel(i);
  83. if ( pPanel )
  84. {
  85. if ( pPanel != groupPanel )
  86. {
  87. pPanel->SetGroupActive( false );
  88. }
  89. else
  90. {
  91. pPanel->SetGroupActive( true );
  92. }
  93. }
  94. }
  95. }
  96. void CBaseStatsPage::UpdateGroupPanels()
  97. {
  98. int iGroupCount = m_pGroupsList->GetItemCount();
  99. vgui::IScheme *pGroupScheme = scheme()->GetIScheme( GetScheme() );
  100. for ( int i = 0; i < iGroupCount; i++ )
  101. {
  102. CBaseStatGroupPanel *pPanel = (CBaseStatGroupPanel*)m_pGroupsList->GetItemPanel(i);
  103. if ( pPanel )
  104. {
  105. pPanel->Update( pGroupScheme );
  106. }
  107. }
  108. }
  109. void CBaseStatsPage::OnSizeChanged(int newWide, int newTall)
  110. {
  111. BaseClass::OnSizeChanged(newWide, newTall);
  112. if (m_statsList)
  113. {
  114. int labelX, labelY, listX, listY, listWide, listTall;
  115. m_statsList->GetBounds(listX, listY, listWide, listTall);
  116. if (m_bottomBar)
  117. {
  118. m_bottomBar->GetPos(labelX, labelY);
  119. m_bottomBar->SetPos(labelX, listY + listTall);
  120. }
  121. }
  122. }
  123. const wchar_t* CBaseStatsPage::TranslateWeaponKillIDToAlias( int statKillID )
  124. {
  125. CSWeaponID weaponIDIndex = WEAPON_MAX;
  126. for ( int i = 0; WeaponName_StatId_Table[i].killStatId != CSSTAT_UNDEFINED; ++i )
  127. {
  128. if( WeaponName_StatId_Table[i].killStatId == statKillID )
  129. {
  130. weaponIDIndex = WeaponName_StatId_Table[i].weaponId;
  131. break;
  132. }
  133. }
  134. if (weaponIDIndex == WEAPON_MAX)
  135. {
  136. return NULL;
  137. }
  138. else
  139. {
  140. return WeaponIDToDisplayName(weaponIDIndex);
  141. }
  142. }
  143. const wchar_t* CBaseStatsPage::LocalizeTagOrUseDefault( const char* tag, const wchar_t* def )
  144. {
  145. const wchar_t* result = g_pVGuiLocalize->Find( tag );
  146. if ( !result )
  147. result = def ? def : L"\0";
  148. return result;
  149. }
  150. CBaseStatGroupPanel* CBaseStatsPage::AddGroup( const wchar_t* name, const char* title_tag, const wchar_t* def )
  151. {
  152. CBaseStatGroupPanel* newGroup = new CBaseStatGroupPanel( m_pGroupsList, this, "StatGroupPanel", 0 );
  153. newGroup->SetGroupInfo( name, LocalizeTagOrUseDefault( title_tag, def ) );
  154. newGroup->SetGroupActive( false );
  155. m_pGroupsList->AddItem( NULL, newGroup );
  156. return newGroup;
  157. }
  158. void CBaseStatsPage::FireGameEvent( IGameEvent * event )
  159. {
  160. const char *type = event->GetName();
  161. if ( 0 == Q_strcmp( type, "player_stats_updated" ) )
  162. m_bStatsDirty = true;
  163. }
  164. void CBaseStatsPage::OnThink()
  165. {
  166. if ( m_bStatsDirty )
  167. UpdateStatsData();
  168. }
  169. CBaseStatGroupPanel::CBaseStatGroupPanel( vgui::PanelListPanel *parent, CBaseStatsPage *owner, const char* name, int iListItemID ) : BaseClass( parent, name )
  170. {
  171. m_pParent = parent;
  172. m_pOwner = owner;
  173. m_pSchemeSettings = NULL;
  174. m_pGroupIcon = SETUP_PANEL(new vgui::ImagePanel( this, "GroupIcon" ));
  175. m_pBaseStatGroupLabel = new vgui::Label( this, "GroupName", "name" );
  176. m_pGroupButton = new CBaseStatGroupButton(this, "GroupButton", "" );
  177. m_pGroupButton->SetPos( 0, 0 );
  178. m_pGroupButton->SetZPos( 20 );
  179. m_pGroupButton->SetWide( 256 );
  180. m_pGroupButton->SetTall( 64 );
  181. SetMouseInputEnabled( true );
  182. parent->SetMouseInputEnabled( true );
  183. m_bActiveButton = false;
  184. }
  185. CBaseStatGroupPanel::~CBaseStatGroupPanel()
  186. {
  187. delete m_pBaseStatGroupLabel;
  188. delete m_pGroupIcon;
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose: Sets the parameter pIconPanel to display the specified achievement's icon file.
  192. //-----------------------------------------------------------------------------
  193. bool CBaseStatGroupPanel::LoadIcon( const char* pFilename)
  194. {
  195. char imagePath[_MAX_PATH];
  196. Q_strncpy( imagePath, "achievements\\", sizeof(imagePath) );
  197. Q_strncat( imagePath, pFilename, sizeof(imagePath), COPY_ALL_CHARACTERS );
  198. Q_strncat( imagePath, ".vtf", sizeof(imagePath), COPY_ALL_CHARACTERS );
  199. char checkFile[_MAX_PATH];
  200. Q_snprintf( checkFile, sizeof(checkFile), "materials\\vgui\\%s", imagePath );
  201. if ( !g_pFullFileSystem->FileExists( checkFile ) )
  202. {
  203. Q_snprintf( imagePath, sizeof(imagePath), "hud\\icon_locked.vtf" );
  204. }
  205. m_pGroupIcon->SetShouldScaleImage( true );
  206. m_pGroupIcon->SetImage( imagePath );
  207. m_pGroupIcon->SetVisible( true );
  208. return m_pGroupIcon->IsVisible();
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose: Loads settings from hl2/resource/ui/achievementitem.res
  212. // Sets display info for this achievement item.
  213. //-----------------------------------------------------------------------------
  214. void CBaseStatGroupPanel::ApplySchemeSettings( vgui::IScheme* pScheme )
  215. {
  216. if ( !g_pPreloadedCSBaseStatGroupLayout )
  217. {
  218. PreloadResourceFile();
  219. }
  220. LoadControlSettings( "", NULL, g_pPreloadedCSBaseStatGroupLayout );
  221. m_pSchemeSettings = pScheme;
  222. BaseClass::ApplySchemeSettings( pScheme );
  223. }
  224. void CBaseStatGroupPanel::Update( vgui::IScheme* pScheme )
  225. {
  226. if ( m_pSchemeSettings )
  227. {
  228. // Set group name text
  229. m_pBaseStatGroupLabel->SetText( m_pGroupTitle );
  230. m_pBaseStatGroupLabel->SetFgColor(Color(157, 194, 80, 255));
  231. if ( !m_bActiveButton )
  232. {
  233. LoadIcon( "achievement-btn-up" );
  234. }
  235. else
  236. {
  237. LoadIcon( "achievement-btn-select" );
  238. }
  239. }
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Purpose:
  243. //-----------------------------------------------------------------------------
  244. void CBaseStatGroupPanel::PreloadResourceFile( void )
  245. {
  246. const char *controlResourceName = "resource/ui/StatGroup.res";
  247. g_pPreloadedCSBaseStatGroupLayout = new KeyValues(controlResourceName);
  248. g_pPreloadedCSBaseStatGroupLayout->LoadFromFile(g_pFullFileSystem, controlResourceName);
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Purpose: Assigns a name and achievement id bounds for an achievement group.
  252. //-----------------------------------------------------------------------------
  253. void CBaseStatGroupPanel::SetGroupInfo ( const wchar_t* name, const wchar_t* title)
  254. {
  255. // Store away the group name
  256. short _textLen = (short)wcslen(name) + 1;
  257. m_pGroupName = new wchar_t[_textLen];
  258. Q_memcpy( m_pGroupName, name, _textLen * sizeof(wchar_t) );
  259. _textLen = (short)wcslen(title) + 1;
  260. m_pGroupTitle = new wchar_t[_textLen];
  261. Q_memcpy( m_pGroupTitle, title, _textLen * sizeof(wchar_t) );
  262. }
  263. CBaseStatGroupButton::CBaseStatGroupButton( vgui::Panel *pParent, const char *pName, const char *pText ) :
  264. BaseClass( pParent, pName, pText )
  265. {
  266. }
  267. //-----------------------------------------------------------------------------
  268. // Purpose: Handle the case where the user presses an achievement group button.
  269. //-----------------------------------------------------------------------------
  270. void CBaseStatGroupButton::DoClick( void )
  271. {
  272. // Process when a group button is hit
  273. CBaseStatGroupPanel* pParent = static_cast<CBaseStatGroupPanel*>(GetParent());
  274. if (pParent)
  275. {
  276. CBaseStatsPage* pBaseStatsPage = static_cast<CBaseStatsPage*>(pParent->GetOwner());
  277. if (pBaseStatsPage)
  278. {
  279. pBaseStatsPage->SetActiveStatGroup( pParent );
  280. pBaseStatsPage->UpdateStatsData();
  281. }
  282. }
  283. }