/
ClientScoreBoardDialog.cpp
580 lines (492 loc) · 18.3 KB
/
ClientScoreBoardDialog.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//
#include "cbase.h"
#include <stdio.h>
#include <cdll_client_int.h>
#include <cdll_util.h>
#include <globalvars_base.h>
#include <igameresources.h>
#include "IGameUIFuncs.h" // for key bindings
#include "inputsystem/iinputsystem.h"
#include "clientscoreboarddialog.h"
#include <voice_status.h>
#include <vgui/IScheme.h>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/IVGui.h>
#include <vstdlib/IKeyValuesSystem.h>
#include <KeyValues.h>
#include <vgui_controls/ImageList.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/SectionedListPanel.h>
#include <game/client/iviewport.h>
#include <igameresources.h>
#include "vgui_avatarimage.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
bool AvatarIndexLessFunc( const int &lhs, const int &rhs )
{
return lhs < rhs;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CClientScoreBoardDialog::CClientScoreBoardDialog(IViewPort *pViewPort) : EditablePanel( NULL, PANEL_SCOREBOARD )
{
m_iPlayerIndexSymbol = KeyValuesSystem()->GetSymbolForString("playerIndex");
m_nCloseKey = BUTTON_CODE_INVALID;
//memset(s_VoiceImage, 0x0, sizeof( s_VoiceImage ));
TrackerImage = 0;
m_pViewPort = pViewPort;
// initialize dialog
SetProportional(true);
SetKeyBoardInputEnabled(false);
SetMouseInputEnabled(false);
// set the scheme before any child control is created
SetScheme("ClientScheme");
m_pPlayerList = new SectionedListPanel(this, "PlayerList");
m_pPlayerList->SetVerticalScrollbar(false);
LoadControlSettings("Resource/UI/ScoreBoard.res");
m_iDesiredHeight = GetTall();
m_pPlayerList->SetVisible( false ); // hide this until we load the images in applyschemesettings
m_HLTVSpectators = 0;
m_ReplaySpectators = 0;
// update scoreboard instantly if on of these events occure
ListenForGameEvent( "hltv_status" );
ListenForGameEvent( "server_spawn" );
m_pImageList = NULL;
m_mapAvatarsToImageList.SetLessFunc( DefLessFunc( CSteamID ) );
m_mapAvatarsToImageList.RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CClientScoreBoardDialog::~CClientScoreBoardDialog()
{
if ( NULL != m_pImageList )
{
delete m_pImageList;
m_pImageList = NULL;
}
}
//-----------------------------------------------------------------------------
// Call every frame
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::OnThink()
{
BaseClass::OnThink();
// NOTE: this is necessary because of the way input works.
// If a key down message is sent to vgui, then it will get the key up message
// Sometimes the scoreboard is activated by other vgui menus,
// sometimes by console commands. In the case where it's activated by
// other vgui menus, we lose the key up message because this panel
// doesn't accept keyboard input. It *can't* accept keyboard input
// because another feature of the dialog is that if it's triggered
// from within the game, you should be able to still run around while
// the scoreboard is up. That feature is impossible if this panel accepts input.
// because if a vgui panel is up that accepts input, it prevents the engine from
// receiving that input. So, I'm stuck with a polling solution.
//
// Close key is set to non-invalid when something other than a keybind
// brings the scoreboard up, and it's set to invalid as soon as the
// dialog becomes hidden.
if ( m_nCloseKey != BUTTON_CODE_INVALID )
{
if ( !g_pInputSystem->IsButtonDown( m_nCloseKey ) )
{
m_nCloseKey = BUTTON_CODE_INVALID;
gViewPortInterface->ShowPanel( PANEL_SCOREBOARD, false );
GetClientVoiceMgr()->StopSquelchMode();
}
}
}
//-----------------------------------------------------------------------------
// Called by vgui panels that activate the client scoreboard
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::OnPollHideCode( int code )
{
m_nCloseKey = (ButtonCode_t)code;
}
//-----------------------------------------------------------------------------
// Purpose: clears everything in the scoreboard and all it's state
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::Reset()
{
// clear
m_pPlayerList->DeleteAllItems();
m_pPlayerList->RemoveAllSections();
m_iSectionId = 0;
m_fNextUpdateTime = 0;
// add all the sections
InitScoreboardSections();
}
//-----------------------------------------------------------------------------
// Purpose: adds all the team sections to the scoreboard
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::InitScoreboardSections()
{
}
//-----------------------------------------------------------------------------
// Purpose: sets up screen
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
if ( m_pImageList )
delete m_pImageList;
m_pImageList = new ImageList( false );
m_mapAvatarsToImageList.RemoveAll();
PostApplySchemeSettings( pScheme );
}
//-----------------------------------------------------------------------------
// Purpose: Does dialog-specific customization after applying scheme settings.
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::PostApplySchemeSettings( vgui::IScheme *pScheme )
{
// resize the images to our resolution
for (int i = 0; i < m_pImageList->GetImageCount(); i++ )
{
int wide, tall;
m_pImageList->GetImage(i)->GetSize(wide, tall);
m_pImageList->GetImage(i)->SetSize(scheme()->GetProportionalScaledValueEx( GetScheme(),wide), scheme()->GetProportionalScaledValueEx( GetScheme(),tall));
}
m_pPlayerList->SetImageList( m_pImageList, false );
m_pPlayerList->SetVisible( true );
// light up scoreboard a bit
SetBgColor( Color( 0,0,0,0) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::ShowPanel(bool bShow)
{
// Catch the case where we call ShowPanel before ApplySchemeSettings, eg when
// going from windowed <-> fullscreen
if ( m_pImageList == NULL )
{
InvalidateLayout( true, true );
}
if ( !bShow )
{
m_nCloseKey = BUTTON_CODE_INVALID;
}
if ( BaseClass::IsVisible() == bShow )
return;
if ( bShow )
{
Reset();
Update();
SetVisible( true );
MoveToFront();
}
else
{
BaseClass::SetVisible( false );
SetMouseInputEnabled( false );
SetKeyBoardInputEnabled( false );
}
}
void CClientScoreBoardDialog::FireGameEvent( IGameEvent *event )
{
const char * type = event->GetName();
if ( Q_strcmp(type, "hltv_status") == 0 )
{
// spectators = clients - proxies
m_HLTVSpectators = event->GetInt( "clients" );
m_HLTVSpectators -= event->GetInt( "proxies" );
}
else if ( Q_strcmp(type, "server_spawn") == 0 )
{
// We'll post the message ourselves instead of using SetControlString()
// so we don't try to translate the hostname.
const char *hostname = event->GetString( "hostname" );
Panel *control = FindChildByName( "ServerName" );
if ( control )
{
PostMessage( control, new KeyValues( "SetText", "text", hostname ) );
control->MoveToFront();
}
}
if( IsVisible() )
Update();
}
bool CClientScoreBoardDialog::NeedsUpdate( void )
{
return (m_fNextUpdateTime < gpGlobals->curtime);
}
//-----------------------------------------------------------------------------
// Purpose: Recalculate the internal scoreboard data
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::Update( void )
{
// Set the title
// Reset();
m_pPlayerList->DeleteAllItems();
FillScoreBoard();
// grow the scoreboard to fit all the players
int wide, tall;
m_pPlayerList->GetContentSize(wide, tall);
tall += GetAdditionalHeight();
wide = GetWide();
if (m_iDesiredHeight < tall)
{
SetSize(wide, tall);
m_pPlayerList->SetSize(wide, tall);
}
else
{
SetSize(wide, m_iDesiredHeight);
m_pPlayerList->SetSize(wide, m_iDesiredHeight);
}
MoveToCenterOfScreen();
// update every second
m_fNextUpdateTime = gpGlobals->curtime + 1.0f;
}
//-----------------------------------------------------------------------------
// Purpose: Sort all the teams
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::UpdateTeamInfo()
{
// TODO: work out a sorting algorithm for team display for TF2
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::UpdatePlayerInfo()
{
m_iSectionId = 0; // 0'th row is a header
int selectedRow = -1;
// walk all the players and make sure they're in the scoreboard
for ( int i = 1; i <= gpGlobals->maxClients; ++i )
{
IGameResources *gr = GameResources();
if ( gr && gr->IsConnected( i ) )
{
// add the player to the list
KeyValues *playerData = new KeyValues("data");
GetPlayerScoreInfo( i, playerData );
UpdatePlayerAvatar( i, playerData );
const char *oldName = playerData->GetString("name","");
char newName[MAX_PLAYER_NAME_LENGTH];
UTIL_MakeSafeName( oldName, newName, MAX_PLAYER_NAME_LENGTH );
playerData->SetString("name", newName);
int itemID = FindItemIDForPlayerIndex( i );
int sectionID = gr->GetTeam( i );
if ( gr->IsLocalPlayer( i ) )
{
selectedRow = itemID;
}
if (itemID == -1)
{
// add a new row
itemID = m_pPlayerList->AddItem( sectionID, playerData );
}
else
{
// modify the current row
m_pPlayerList->ModifyItem( itemID, sectionID, playerData );
}
// set the row color based on the players team
m_pPlayerList->SetItemFgColor( itemID, gr->GetTeamColor( sectionID ) );
playerData->deleteThis();
}
else
{
// remove the player
int itemID = FindItemIDForPlayerIndex( i );
if (itemID != -1)
{
m_pPlayerList->RemoveItem(itemID);
}
}
}
if ( selectedRow != -1 )
{
m_pPlayerList->SetSelectedItem(selectedRow);
}
}
//-----------------------------------------------------------------------------
// Purpose: adds the top header of the scoreboars
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::AddHeader()
{
// add the top header
m_pPlayerList->AddSection(m_iSectionId, "");
m_pPlayerList->SetSectionAlwaysVisible(m_iSectionId);
m_pPlayerList->AddColumnToSection(m_iSectionId, "name", "#PlayerName", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),NAME_WIDTH) );
m_pPlayerList->AddColumnToSection(m_iSectionId, "frags", "#PlayerScore", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),SCORE_WIDTH) );
m_pPlayerList->AddColumnToSection(m_iSectionId, "deaths", "#PlayerDeath", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),DEATH_WIDTH) );
m_pPlayerList->AddColumnToSection(m_iSectionId, "ping", "#PlayerPing", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),PING_WIDTH) );
}
//-----------------------------------------------------------------------------
// Purpose: Adds a new section to the scoreboard (i.e the team header)
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::AddSection(int teamType, int teamNumber)
{
if ( teamType == TYPE_TEAM )
{
IGameResources *gr = GameResources();
if ( !gr )
return;
// setup the team name
wchar_t *teamName = g_pVGuiLocalize->Find( gr->GetTeamName(teamNumber) );
wchar_t name[64];
wchar_t string1[1024];
if (!teamName)
{
g_pVGuiLocalize->ConvertANSIToUnicode(gr->GetTeamName(teamNumber), name, sizeof(name));
teamName = name;
}
g_pVGuiLocalize->ConstructString( string1, sizeof( string1 ), g_pVGuiLocalize->Find("#Player"), 2, teamName );
m_pPlayerList->AddSection(m_iSectionId, "", StaticPlayerSortFunc);
// Avatars are always displayed at 32x32 regardless of resolution
if ( ShowAvatars() )
{
m_pPlayerList->AddColumnToSection( m_iSectionId, "avatar", "", SectionedListPanel::COLUMN_IMAGE | SectionedListPanel::COLUMN_RIGHT, m_iAvatarWidth );
}
m_pPlayerList->AddColumnToSection(m_iSectionId, "name", string1, 0, scheme()->GetProportionalScaledValueEx( GetScheme(),NAME_WIDTH) - m_iAvatarWidth );
m_pPlayerList->AddColumnToSection(m_iSectionId, "frags", "", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),SCORE_WIDTH) );
m_pPlayerList->AddColumnToSection(m_iSectionId, "deaths", "", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),DEATH_WIDTH) );
m_pPlayerList->AddColumnToSection(m_iSectionId, "ping", "", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),PING_WIDTH) );
}
else if ( teamType == TYPE_SPECTATORS )
{
m_pPlayerList->AddSection(m_iSectionId, "");
// Avatars are always displayed at 32x32 regardless of resolution
if ( ShowAvatars() )
{
m_pPlayerList->AddColumnToSection( m_iSectionId, "avatar", "", SectionedListPanel::COLUMN_IMAGE | SectionedListPanel::COLUMN_RIGHT, m_iAvatarWidth );
}
m_pPlayerList->AddColumnToSection(m_iSectionId, "name", "#Spectators", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),NAME_WIDTH) - m_iAvatarWidth );
m_pPlayerList->AddColumnToSection(m_iSectionId, "frags", "", 0, scheme()->GetProportionalScaledValueEx( GetScheme(),SCORE_WIDTH) );
}
}
//-----------------------------------------------------------------------------
// Purpose: Used for sorting players
//-----------------------------------------------------------------------------
bool CClientScoreBoardDialog::StaticPlayerSortFunc(vgui::SectionedListPanel *list, int itemID1, int itemID2)
{
KeyValues *it1 = list->GetItemData(itemID1);
KeyValues *it2 = list->GetItemData(itemID2);
Assert(it1 && it2);
// first compare frags
int v1 = it1->GetInt("frags");
int v2 = it2->GetInt("frags");
if (v1 > v2)
return true;
else if (v1 < v2)
return false;
// next compare deaths
v1 = it1->GetInt("deaths");
v2 = it2->GetInt("deaths");
if (v1 > v2)
return false;
else if (v1 < v2)
return true;
// the same, so compare itemID's (as a sentinel value to get deterministic sorts)
return itemID1 < itemID2;
}
//-----------------------------------------------------------------------------
// Purpose: Adds a new row to the scoreboard, from the playerinfo structure
//-----------------------------------------------------------------------------
bool CClientScoreBoardDialog::GetPlayerScoreInfo(int playerIndex, KeyValues *kv)
{
IGameResources *gr = GameResources();
if (!gr )
return false;
kv->SetInt("deaths", gr->GetDeaths( playerIndex ) );
kv->SetInt("frags", gr->GetFrags( playerIndex ) );
kv->SetInt("ping", gr->GetPing( playerIndex ) ) ;
kv->SetString("name", gr->GetPlayerName( playerIndex ) );
kv->SetInt("playerIndex", playerIndex);
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::UpdatePlayerAvatar( int playerIndex, KeyValues *kv )
{
// Update their avatar
if ( kv && ShowAvatars() && steamapicontext->SteamFriends() && steamapicontext->SteamUtils() )
{
player_info_t pi;
if ( engine->GetPlayerInfo( playerIndex, &pi ) )
{
if ( pi.friendsID )
{
CSteamID steamIDForPlayer( pi.friendsID, 1, steamapicontext->SteamUtils()->GetConnectedUniverse(), k_EAccountTypeIndividual );
// See if we already have that avatar in our list
int iMapIndex = m_mapAvatarsToImageList.Find( steamIDForPlayer );
int iImageIndex;
if ( iMapIndex == m_mapAvatarsToImageList.InvalidIndex() )
{
CAvatarImage *pImage = new CAvatarImage();
pImage->SetAvatarSteamID( steamIDForPlayer );
pImage->SetAvatarSize( 32, 32 ); // Deliberately non scaling
iImageIndex = m_pImageList->AddImage( pImage );
m_mapAvatarsToImageList.Insert( steamIDForPlayer, iImageIndex );
}
else
{
iImageIndex = m_mapAvatarsToImageList[ iMapIndex ];
}
kv->SetInt( "avatar", iImageIndex );
CAvatarImage *pAvIm = (CAvatarImage *)m_pImageList->GetImage( iImageIndex );
pAvIm->UpdateFriendStatus();
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: reload the player list on the scoreboard
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::FillScoreBoard()
{
// update totals information
UpdateTeamInfo();
// update player info
UpdatePlayerInfo();
}
//-----------------------------------------------------------------------------
// Purpose: searches for the player in the scoreboard
//-----------------------------------------------------------------------------
int CClientScoreBoardDialog::FindItemIDForPlayerIndex(int playerIndex)
{
for (int i = 0; i <= m_pPlayerList->GetHighestItemID(); i++)
{
if (m_pPlayerList->IsItemIDValid(i))
{
KeyValues *kv = m_pPlayerList->GetItemData(i);
kv = kv->FindKey(m_iPlayerIndexSymbol);
if (kv && kv->GetInt() == playerIndex)
return i;
}
}
return -1;
}
//-----------------------------------------------------------------------------
// Purpose: Sets the text of a control by name
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::MoveLabelToFront(const char *textEntryName)
{
Label *entry = dynamic_cast<Label *>(FindChildByName(textEntryName));
if (entry)
{
entry->MoveToFront();
}
}
//-----------------------------------------------------------------------------
// Purpose: Center the dialog on the screen. (vgui has this method on
// Frame, but we're an EditablePanel, need to roll our own.)
//-----------------------------------------------------------------------------
void CClientScoreBoardDialog::MoveToCenterOfScreen()
{
int wx, wy, ww, wt;
surface()->GetWorkspaceBounds(wx, wy, ww, wt);
SetPos((ww - GetWide()) / 2, (wt - GetTall()) / 2);
}