summaryrefslogtreecommitdiff
path: root/game/client/cstrike/VGUI/base_stats_page.cpp
blob: ab27865b63cfc240632d4f60a48efa33e22fec1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//
//=============================================================================//

#include "cbase.h"
#include "tier3/tier3.h"
#include "vgui/ILocalize.h"
#include "lifetime_stats_page.h"
#include <vgui_controls/SectionedListPanel.h>
#include "cs_client_gamestats.h" 
#include "filesystem.h"
#include "cs_weapon_parse.h"
#include "buy_presets/buy_presets.h"
#include "../vgui_controls/ScrollBar.h"
#include "stat_card.h"

using namespace vgui;

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


KeyValues *g_pPreloadedCSBaseStatGroupLayout = NULL;

//-----------------------------------------------------------------------------
// Purpose: creates child panels, passes down name to pick up any settings from res files.
//-----------------------------------------------------------------------------
CBaseStatsPage::CBaseStatsPage(vgui::Panel *parent, const char *name) : BaseClass(parent, "CSBaseStatsDialog")
{
	vgui::IScheme *pScheme = scheme()->GetIScheme( GetScheme() );

	m_listItemFont = pScheme->GetFont( "StatsPageText", IsProportional() );

	m_statsList = new SectionedListPanel( this, "StatsList" );
	m_statsList->SetClickable(false);
	m_statsList->SetDrawHeaders(false);

	m_bottomBar = new ImagePanel(this, "BottomBar");

	m_pGroupsList = new vgui::PanelListPanel( this, "listpanel_groups" );
	m_pGroupsList->SetFirstColumnWidth( 0 );

	SetBounds(0, 0, 900, 780);
	SetMinimumSize( 256, 780 );

	SetBgColor(GetSchemeColor("ListPanel.BgColor", GetBgColor(), pScheme));

	m_pStatCard = new StatCard(this, "ignored");

	ListenForGameEvent( "player_stats_updated" );

	m_bStatsDirty = true;
}

CBaseStatsPage::~CBaseStatsPage()
{
	delete m_statsList;
}


void CBaseStatsPage::MoveToFront()
{
    UpdateStatsData();
	m_pStatCard->UpdateInfo();
}

void CBaseStatsPage::UpdateStatsData()
{
	// Hide the group list scrollbar
	if (m_pGroupsList->GetScrollbar())
	{
		m_pGroupsList->GetScrollbar()->SetWide(0);
	}

	UpdateGroupPanels();
	RepopulateStats();

	m_bStatsDirty = false;
}

//-----------------------------------------------------------------------------
// Purpose: Loads settings from statsdialog.res in hl2/resource/ui/
//-----------------------------------------------------------------------------
void CBaseStatsPage::ApplySchemeSettings( vgui::IScheme *pScheme )
{
    BaseClass::ApplySchemeSettings( pScheme );
    LoadControlSettings("resource/ui/CSBaseStatsDialog.res");

	m_statsList->SetClickable(false);
	m_statsList->SetDrawHeaders(false);

	m_statsList->SetVerticalScrollbar(true);

	SetBgColor(Color(86,86,86,255));

	//Remove any pre-existing sections and add then fresh (this can happen on a resolution change)
	m_statsList->RemoveAllSections();

	m_statsList->AddSection( 0, "Players");

	m_statsList->SetFontSection(0, m_listItemFont);

	m_pGroupsList->SetBgColor(Color(86,86,86,255));
	m_statsList->SetBgColor(Color(52,52,52,255));
}

void CBaseStatsPage::SetActiveStatGroup (CBaseStatGroupPanel* groupPanel)
{
	for (int i = 0; i < m_pGroupsList->GetItemCount(); i++)
	{
		CBaseStatGroupPanel *pPanel = (CBaseStatGroupPanel*)m_pGroupsList->GetItemPanel(i);
		if ( pPanel )
		{
			if ( pPanel != groupPanel )
			{
				pPanel->SetGroupActive( false );
			}
			else
			{
				pPanel->SetGroupActive( true );
			}
		}
	}
} 

void CBaseStatsPage::UpdateGroupPanels()
{
	int iGroupCount = m_pGroupsList->GetItemCount();
	vgui::IScheme *pGroupScheme = scheme()->GetIScheme( GetScheme() );

	for ( int i = 0; i < iGroupCount; i++ )
	{
		CBaseStatGroupPanel *pPanel = (CBaseStatGroupPanel*)m_pGroupsList->GetItemPanel(i);
		if ( pPanel )
		{
			pPanel->Update( pGroupScheme );
		}
	}
}



void CBaseStatsPage::OnSizeChanged(int newWide, int newTall)
{
	BaseClass::OnSizeChanged(newWide, newTall);

	if (m_statsList)
	{
		int labelX, labelY, listX, listY, listWide, listTall;
		m_statsList->GetBounds(listX, listY, listWide, listTall);

		if (m_bottomBar)
		{
			m_bottomBar->GetPos(labelX, labelY);
			m_bottomBar->SetPos(labelX, listY + listTall);
		}
	}
}

const wchar_t* CBaseStatsPage::TranslateWeaponKillIDToAlias( int statKillID )
{
	CSWeaponID weaponIDIndex = WEAPON_MAX;
	for ( int i = 0; WeaponName_StatId_Table[i].killStatId != CSSTAT_UNDEFINED; ++i )
	{
		if( WeaponName_StatId_Table[i].killStatId == statKillID )
		{
			weaponIDIndex = WeaponName_StatId_Table[i].weaponId;
			break;
		}
	}

	if (weaponIDIndex == WEAPON_MAX)
	{
		return NULL;
	}
	else
	{
		return WeaponIDToDisplayName(weaponIDIndex);
	}
}

const wchar_t* CBaseStatsPage::LocalizeTagOrUseDefault( const char* tag, const wchar_t* def )
{
	const wchar_t* result = g_pVGuiLocalize->Find( tag );

	if ( !result )
		result = def ? def : L"\0";

	return result;
}

CBaseStatGroupPanel* CBaseStatsPage::AddGroup( const wchar_t* name, const char* title_tag, const wchar_t* def )
{
	CBaseStatGroupPanel* newGroup = new CBaseStatGroupPanel( m_pGroupsList, this, "StatGroupPanel", 0 );
	newGroup->SetGroupInfo( name, LocalizeTagOrUseDefault( title_tag, def ) );
	newGroup->SetGroupActive( false );

	m_pGroupsList->AddItem( NULL, newGroup );

	return newGroup;
}

void CBaseStatsPage::FireGameEvent( IGameEvent * event )
{
	const char *type = event->GetName();

	if ( 0 == Q_strcmp( type, "player_stats_updated" ) )
		m_bStatsDirty = true;
}

void CBaseStatsPage::OnThink()
{
	if ( m_bStatsDirty )
		UpdateStatsData();
}

CBaseStatGroupPanel::CBaseStatGroupPanel( vgui::PanelListPanel *parent, CBaseStatsPage *owner, const char* name, int iListItemID ) : BaseClass( parent, name )
{
	m_pParent = parent;
	m_pOwner = owner;
	m_pSchemeSettings = NULL;

	m_pGroupIcon = SETUP_PANEL(new vgui::ImagePanel( this, "GroupIcon" ));
	m_pBaseStatGroupLabel = new vgui::Label( this, "GroupName", "name" );
	m_pGroupButton = new CBaseStatGroupButton(this, "GroupButton", "" );
	m_pGroupButton->SetPos( 0, 0 );
	m_pGroupButton->SetZPos( 20 );
	m_pGroupButton->SetWide( 256 );
	m_pGroupButton->SetTall( 64 );
	SetMouseInputEnabled( true );
	parent->SetMouseInputEnabled( true );

	m_bActiveButton = false;
}

CBaseStatGroupPanel::~CBaseStatGroupPanel()
{
	delete m_pBaseStatGroupLabel;
	delete m_pGroupIcon;
}


//-----------------------------------------------------------------------------
// Purpose: Sets the parameter pIconPanel to display the specified achievement's icon file.
//-----------------------------------------------------------------------------
bool CBaseStatGroupPanel::LoadIcon( const char* pFilename)
{
	char imagePath[_MAX_PATH];
	Q_strncpy( imagePath, "achievements\\", sizeof(imagePath) );
	Q_strncat( imagePath, pFilename, sizeof(imagePath), COPY_ALL_CHARACTERS );
	Q_strncat( imagePath, ".vtf", sizeof(imagePath), COPY_ALL_CHARACTERS );

	char checkFile[_MAX_PATH];
	Q_snprintf( checkFile, sizeof(checkFile), "materials\\vgui\\%s", imagePath );
	if ( !g_pFullFileSystem->FileExists( checkFile ) )
	{
		Q_snprintf( imagePath, sizeof(imagePath), "hud\\icon_locked.vtf" );
	}

	m_pGroupIcon->SetShouldScaleImage( true );
	m_pGroupIcon->SetImage( imagePath );
	m_pGroupIcon->SetVisible( true );

	return m_pGroupIcon->IsVisible();
}


//-----------------------------------------------------------------------------
// Purpose: Loads settings from hl2/resource/ui/achievementitem.res
//			Sets display info for this achievement item.
//-----------------------------------------------------------------------------
void CBaseStatGroupPanel::ApplySchemeSettings( vgui::IScheme* pScheme )
{
	if ( !g_pPreloadedCSBaseStatGroupLayout )
	{
		PreloadResourceFile();
	}

	LoadControlSettings( "", NULL, g_pPreloadedCSBaseStatGroupLayout );

	m_pSchemeSettings = pScheme;

	BaseClass::ApplySchemeSettings( pScheme );
}

void CBaseStatGroupPanel::Update( vgui::IScheme* pScheme )
{
	if ( m_pSchemeSettings )
	{

		// Set group name text
		m_pBaseStatGroupLabel->SetText( m_pGroupTitle );
		m_pBaseStatGroupLabel->SetFgColor(Color(157, 194, 80, 255));

		if ( !m_bActiveButton )
		{
			LoadIcon( "achievement-btn-up" );
		}
		else
		{
			LoadIcon( "achievement-btn-select" );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseStatGroupPanel::PreloadResourceFile( void )
{
	const char *controlResourceName = "resource/ui/StatGroup.res";

	g_pPreloadedCSBaseStatGroupLayout = new KeyValues(controlResourceName);
	g_pPreloadedCSBaseStatGroupLayout->LoadFromFile(g_pFullFileSystem, controlResourceName);
}



//-----------------------------------------------------------------------------
// Purpose: Assigns a name and achievement id bounds for an achievement group.
//-----------------------------------------------------------------------------
void CBaseStatGroupPanel::SetGroupInfo ( const wchar_t* name, const wchar_t* title)
{
	// Store away the group name
	short   _textLen = (short)wcslen(name) + 1;
	m_pGroupName = new wchar_t[_textLen];
	Q_memcpy( m_pGroupName, name, _textLen * sizeof(wchar_t) );

	_textLen = (short)wcslen(title) + 1;
	m_pGroupTitle = new wchar_t[_textLen];
	Q_memcpy( m_pGroupTitle, title, _textLen * sizeof(wchar_t) );
}


CBaseStatGroupButton::CBaseStatGroupButton( vgui::Panel *pParent, const char *pName, const char *pText ) :
BaseClass( pParent, pName, pText )
{
}

//-----------------------------------------------------------------------------
// Purpose: Handle the case where the user presses an achievement group button.
//-----------------------------------------------------------------------------
void CBaseStatGroupButton::DoClick( void )
{
	// Process when a group button is hit
	CBaseStatGroupPanel*    pParent = static_cast<CBaseStatGroupPanel*>(GetParent());

	if (pParent)
	{
		CBaseStatsPage*  pBaseStatsPage = static_cast<CBaseStatsPage*>(pParent->GetOwner());

		if (pBaseStatsPage)
		{
			pBaseStatsPage->SetActiveStatGroup( pParent );
			pBaseStatsPage->UpdateStatsData();
		}
	}
}