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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Draws achievement progress bars on the HUD
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "hudelement.h"
#include "hud_macros.h"
#include <game_controls/baseviewport.h>
#include <vgui_controls/Controls.h>
#include <vgui_controls/Panel.h>
#include <vgui_controls/ImagePanel.h>
#include <vgui_controls/TextImage.h>
#include "vgui/ILocalize.h"
#include "hud_baseachievement_tracker.h"
#include "iachievementmgr.h"
#include "baseachievement.h"
#include "iclientmode.h"
#include "cdll_int.h"
#include <vgui/ISurface.h>
#include <vgui_controls/AnimationController.h>
#include "fmtstr.h"
#include "engine/IEngineSound.h"
//=============================================================================
// HPE_BEGIN
// [dwenger] Necessary for HUD Achievement display
//=============================================================================
#include "cdll_client_int.h"
#include "steam/isteamuserstats.h"
#include "steam/steam_api.h"
//=============================================================================
// HPE_END
//=============================================================================
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
#define PROGRESS_BAR_NEEDS_UPDATE -1
#define UNKNOWN_ACHIEVEMENT_ID -1
void TrackerDescriptionChanged( IConVar *var, const char *pOldString, float flOldValue )
{
static int s_iTimesChanged = 0;
if ( s_iTimesChanged > 0 )
{
engine->ClientCmd_Unrestricted( "hud_reloadscheme" );
}
s_iTimesChanged++;
}
ConVar hud_achievement_description("hud_achievement_description", "1", FCVAR_ARCHIVE, "Show full descriptions of achievements on the HUD", TrackerDescriptionChanged );
#ifdef CSTRIKE_DLL
ConVar hud_achievement_count("hud_achievement_count", "5", FCVAR_ARCHIVE, "Max number of achievements that can be shown on the HUD" );
#else
ConVar hud_achievement_count("hud_achievement_count", "8", FCVAR_ARCHIVE, "Max number of achievements that can be shown on the HUD" );
#endif
ConVar hud_achievement_glowtime("hud_achievement_glowtime", "2.5", FCVAR_NONE, "Duration of glow effect around incremented achievements" );
ConVar hud_achievement_tracker("hud_achievement_tracker", "1", FCVAR_NONE, "Show or hide the achievement tracker" );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudBaseAchievementTracker::CHudBaseAchievementTracker( const char *pElementName ) :
CHudElement( pElementName ), BaseClass( NULL, "HudAchievementTracker" )
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
for ( int i=0; i < GetMaxAchievementsShown(); i++ )
{
CAchievementTrackerItem *pNewItem = CreateAchievementPanel();
pNewItem->SetAchievement( NULL );
m_AchievementItem.AddToTail( pNewItem );
}
m_flNextThink = 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudBaseAchievementTracker::Reset()
{
m_flNextThink = gpGlobals->curtime + 0.05f;
}
void CHudBaseAchievementTracker::LevelInit()
{
// clear out tracker items and floating numbers on level change
for ( int i = 0; i < GetChildCount(); i++ )
{
GetChild( i )->SetVisible( false );
GetChild( i )->MarkForDeletion();
}
m_AchievementItem.Purge();
for ( int i=0; i < GetMaxAchievementsShown(); i++ )
{
CAchievementTrackerItem *pNewItem = CreateAchievementPanel();
pNewItem->SetAchievement( NULL );
m_AchievementItem.AddToTail( pNewItem );
}
CHudElement::LevelInit();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudBaseAchievementTracker::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CHudBaseAchievementTracker::ShouldDraw()
{
if ( engine->IsPlayingDemo() )
return false;
return CHudElement::ShouldDraw();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudBaseAchievementTracker::OnThink()
{
if ( m_flNextThink < gpGlobals->curtime )
{
UpdateAchievementItems();
m_flNextThink = gpGlobals->curtime + 0.5f;
}
}
//-----------------------------------------------------------------------------
// Purpose: Max number of achievements shown on the HUD
//-----------------------------------------------------------------------------
int CHudBaseAchievementTracker::GetMaxAchievementsShown()
{
return hud_achievement_count.GetInt();
}
bool CHudBaseAchievementTracker::ShouldShowAchievement( IAchievement *pAchievement )
{
return ( hud_achievement_tracker.GetBool() && pAchievement && pAchievement->ShouldShowOnHUD() && !pAchievement->IsAchieved() );
}
CAchievementTrackerItem* CHudBaseAchievementTracker::CreateAchievementPanel()
{
return new CAchievementTrackerItem( this, "HudAchievementTrackerItem" );
}
//-----------------------------------------------------------------------------
// Purpose: create panels for each achievement the player wants shown on the HUD and assign achievements to each one
//-----------------------------------------------------------------------------
void CHudBaseAchievementTracker::UpdateAchievementItems()
{
IAchievementMgr *pAchievementMgr = engine->GetAchievementMgr();
if ( !pAchievementMgr )
return;
int iCount = pAchievementMgr->GetAchievementCount();
int iShown = 0;
for ( int i = 0; i < iCount; ++i )
{
IAchievement* pCur = pAchievementMgr->GetAchievementByIndex( i );
if ( !ShouldShowAchievement( pCur ) )
{
// don't remove achievements that are still glowing (typically a just completed achievement)
if ( pCur && m_AchievementItem.Count() > iShown && m_AchievementItem[iShown]->GetAchievementID() == pCur->GetAchievementID()
&& m_AchievementItem[iShown]->GetGlow() > 0 )
{
iShown++;
}
continue;
}
if ( m_AchievementItem.Count() < iShown+1 )
{
CAchievementTrackerItem *pNewItem = CreateAchievementPanel();
SETUP_PANEL( pNewItem );
m_AchievementItem.AddToTail( pNewItem );
}
m_AchievementItem[iShown]->SetAchievement( pCur );
m_AchievementItem[iShown]->SetSlot( iShown );
m_AchievementItem[iShown]->SetVisible( true );
iShown++;
if ( iShown >= GetMaxAchievementsShown() )
break;
}
// hide any extra panels we may have created from when the list was longer
if ( iShown < m_AchievementItem.Count() )
{
for ( int i = m_AchievementItem.Count() - 1; i >= iShown ; i-- )
{
m_AchievementItem[i]->SetVisible( false );
m_AchievementItem[i]->SetAchievement( NULL );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Layout all child panels vertically
//-----------------------------------------------------------------------------
void CHudBaseAchievementTracker::PerformLayout()
{
// make sure all children are laid out first
for ( int i=0; i < m_AchievementItem.Count(); i++ )
{
m_AchievementItem[i]->InvalidateLayout( true );
}
int iCurrentY = 0;
int x, y;
for ( int i=0; i< m_AchievementItem.Count(); i++ )
{
m_AchievementItem[i]->GetPos( x, y );
m_AchievementItem[i]->SetPos( x, iCurrentY );
iCurrentY += m_AchievementItem[i]->GetTall() + m_iItemPadding;
}
}
CAchievementTrackerItem* CHudBaseAchievementTracker::GetAchievementPanel( int i )
{
if ( i < 0 || i >= m_AchievementItem.Count() )
return NULL;
return m_AchievementItem[i];
}
//-----------------------------------------------------------------------------
// Purpose: The child panels
//-----------------------------------------------------------------------------
CAchievementTrackerItem::CAchievementTrackerItem( vgui::Panel* pParent, const char *pElementName ) :
BaseClass( pParent, pElementName )
{
m_pAchievementNameGlow = new vgui::Label( this, "AchievementNameGlow", "" );
m_pAchievementName = new vgui::Label( this, "AchievementName", "" );
m_pAchievementDesc = new vgui::Label( this, "AchievementDesc", "" );
m_pProgressBarBackground = SETUP_PANEL( new ImagePanel( this, "ProgressBarBG" ) );
m_pProgressBar = SETUP_PANEL( new ImagePanel( this, "ProgressBar" ) );
m_iAchievementID = UNKNOWN_ACHIEVEMENT_ID;
m_iLastPaintedAchievementID = UNKNOWN_ACHIEVEMENT_ID;
m_iAccumulatedIncrement = 0;
m_flShowIncrementsTime = 0;
m_iLastCount = 0;
m_iPadding = 1;
}
CAchievementTrackerItem::~CAchievementTrackerItem()
{
if ( m_pAchievementName )
{
m_pAchievementName->MarkForDeletion();
m_pAchievementName = NULL;
}
if ( m_pAchievementNameGlow )
{
m_pAchievementNameGlow->MarkForDeletion();
m_pAchievementNameGlow = NULL;
}
if ( m_pAchievementDesc )
{
m_pAchievementDesc->MarkForDeletion();
m_pAchievementDesc = NULL;
}
if ( m_pProgressBarBackground )
{
m_pProgressBarBackground->MarkForDeletion();
m_pProgressBarBackground = NULL;
}
if ( m_pProgressBar )
{
m_pProgressBar->MarkForDeletion();
m_pProgressBar = NULL;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
LoadControlSettings( "resource/UI/HudAchievementTrackerItem.res" );
m_pAchievementDesc->SetVisible( hud_achievement_description.GetBool() );
m_iLastPaintedAchievementID = UNKNOWN_ACHIEVEMENT_ID;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::PerformLayout()
{
BaseClass::PerformLayout();
int x, y, w, t;
//=============================================================================
// HPE_BEGIN
// [dwenger] Necessary for HUD Achievement display
//=============================================================================
m_pAchievementName->GetContentSize( w, t ); // needed in order to load up font for the name
m_pAchievementNameGlow->GetContentSize( w, t ); // needed in order to load up font for the glow
//=============================================================================
// HPE_END
//=============================================================================
if ( hud_achievement_description.GetBool() )
{
m_pAchievementDesc->GetContentSize( w, t );
m_pAchievementDesc->SetTall( t );
m_pAchievementDesc->GetBounds( x, y, w, t );
}
else
{
m_pAchievementName->GetBounds( x, y, w, t );
}
if ( m_pProgressBarBackground->IsVisible() )
{
// put progress bar after description
int bx, by;
m_pProgressBarBackground->GetPos( bx, by );
m_pProgressBarBackground->SetPos( bx, y + t + m_iPadding );
SetTall( y + t + m_pProgressBarBackground->GetTall() + m_iPadding * 2 );
}
else
{
SetTall( y + t + m_iPadding );
}
m_iLastProgressBarCount = m_iLastProgressBarGoal = PROGRESS_BAR_NEEDS_UPDATE;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::SetAchievement( IAchievement* pAchievement )
{
if ( !pAchievement )
{
m_iAchievementID = UNKNOWN_ACHIEVEMENT_ID;
m_iLastCount = 0;
return;
}
if ( m_iAchievementID != pAchievement->GetAchievementID() )
{
m_iAchievementID = pAchievement->GetAchievementID();
m_iLastCount = pAchievement->GetCount();
UpdateAchievementDisplay();
}
}
void CAchievementTrackerItem::OnThink()
{
UpdateAchievementDisplay();
}
//-----------------------------------------------------------------------------
// Purpose: Make sure our labels and progress bar are up to date
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::UpdateAchievementDisplay()
{
IAchievementMgr *pAchievementMgr = engine->GetAchievementMgr();
if ( !pAchievementMgr )
return;
CBaseAchievement* pAchievement = pAchievementMgr->GetAchievementByID( m_iAchievementID );
if ( !pAchievement )
return;
if ( m_iAchievementID != m_iLastPaintedAchievementID )
{
// need to update labels
//=============================================================================
// HPE_BEGIN
// [dwenger] Necessary for HUD Achievement display
//=============================================================================
m_pAchievementName->SetText( ACHIEVEMENT_LOCALIZED_NAME( pAchievement ) );
m_pAchievementNameGlow->SetText( ACHIEVEMENT_LOCALIZED_NAME( pAchievement ) );
m_pAchievementDesc->SetText( ACHIEVEMENT_LOCALIZED_DESC( pAchievement ) );
//=============================================================================
// HPE_END
//=============================================================================
m_pProgressBarBackground->SetVisible( pAchievement->GetGoal() > 1 );
m_pProgressBar->SetVisible( pAchievement->GetGoal() > 1 );
m_iLastPaintedAchievementID = m_iAchievementID;
m_flGlow = 0.0f;
m_flGlowTime = 0.0f;
m_iAccumulatedIncrement = 0;
//InvalidateLayout( true );
GetParent()->InvalidateLayout( true );
}
if ( m_iAccumulatedIncrement > 0 && gpGlobals->curtime > m_flShowIncrementsTime )
{
ShowAccumulatedIncrements();
}
if ( pAchievement->GetCount() != m_iLastProgressBarCount || pAchievement->GetGoal() != m_iLastProgressBarGoal )
{
// need to update progress bar
float flProgress = float ( pAchievement->GetCount() ) / float( pAchievement->GetGoal() );
int x, y, w, t;
m_pProgressBarBackground->GetBounds( x, y, w, t );
m_pProgressBar->SetBounds( x, y, w * flProgress, t );
m_iLastProgressBarCount = pAchievement->GetCount();
m_iLastProgressBarGoal = pAchievement->GetGoal();
AchievementIncremented( pAchievement->GetCount() );
}
if ( gpGlobals->curtime < m_flGlowTime )
{
m_flGlow = MIN( 1.0f, m_flGlow + gpGlobals->frametime * 5.0f );
}
else
{
m_flGlow = MAX( 0.0f, m_flGlow - gpGlobals->frametime * 5.0f );
}
m_pAchievementNameGlow->SetAlpha( m_flGlow * 255.0f );
}
//-----------------------------------------------------------------------------
// Purpose: Achievement count has gone up, make it flash
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::AchievementIncremented( int iNewCount )
{
int iIncrement = iNewCount - m_iLastCount;
m_iLastCount = iNewCount;
if ( iIncrement <= 0 )
return;
if ( m_iLastProgressBarGoal > 1500 )
{
// for achievements with very high counts, accumulate increments so we don't have too many +1s on screen
// also don't play sounds as these achievements tend to increment constantly
if ( m_flShowIncrementsTime < gpGlobals->curtime )
{
m_flShowIncrementsTime = gpGlobals->curtime + 2.0f;
}
m_iAccumulatedIncrement += iIncrement;
}
else
{
m_flGlowTime = gpGlobals->curtime + hud_achievement_glowtime.GetFloat();
// create a floating +X to scroll up alongside this achievement
if ( m_pProgressBarBackground->IsVisible() )
{
int px, py;
GetPos( px, py );
int x, y, w, t;
m_pProgressBarBackground->GetBounds( x, y, w, t );
x += w;
new CFloatingAchievementNumber( iIncrement, px + x, py + y + ( t * 0.5f ), FN_DIR_RIGHT, GetParent() );
}
CLocalPlayerFilter filter;
C_BaseEntity::EmitSound( filter, SOUND_FROM_LOCAL_PLAYER, "Hud.AchievementIncremented" );
}
}
//-----------------------------------------------------------------------------
// Purpose: Show all the increments we've accumulated
//-----------------------------------------------------------------------------
void CAchievementTrackerItem::ShowAccumulatedIncrements()
{
int px, py;
GetPos( px, py );
int x, y, w, t;
m_pProgressBarBackground->GetBounds( x, y, w, t );
x += w;
new CFloatingAchievementNumber( m_iAccumulatedIncrement, px + x, py + y + ( t * 0.5f ), FN_DIR_RIGHT, GetParent() );
m_iAccumulatedIncrement = 0;
m_flGlowTime = gpGlobals->curtime + hud_achievement_glowtime.GetFloat();
}
//-----------------------------------------------------------------------------
// Purpose: Floating numbers showing how much achievement progress bars have gone up
//-----------------------------------------------------------------------------
CFloatingAchievementNumber::CFloatingAchievementNumber( int iProgress, int x, int y, floating_number_directions iDir, vgui::Panel* pParent )
: BaseClass( pParent, "FloatingAchievementNumber" )
{
m_iStartX = x;
m_iStartY = y;
m_iProgress = iProgress;
m_fStartTime = gpGlobals->curtime;
m_iDirection = iDir;
char szLabel[64];
Q_snprintf( szLabel, sizeof( szLabel ), "+%d", iProgress );
m_pNumberLabel = new vgui::Label( this, "FloatingNumberLabel", szLabel );
}
CFloatingAchievementNumber::~CFloatingAchievementNumber()
{
if ( m_pNumberLabel )
{
m_pNumberLabel->MarkForDeletion();
m_pNumberLabel = NULL;
}
}
void CFloatingAchievementNumber::ApplySchemeSettings( vgui::IScheme *scheme )
{
BaseClass::ApplySchemeSettings( scheme );
LoadControlSettings( "resource/UI/HudAchievementFloatingNumber.res" );
int fontHeight = surface()->GetFontTall( m_pNumberLabel->GetFont() );
SetPos( m_iStartX, m_iStartY - ( fontHeight * 0.5f ) );
m_pNumberLabel->SetAlpha( 0 );
GetAnimationController()->RunAnimationCommand( m_pNumberLabel, "alpha", 255, 0, 0.3, vgui::AnimationController::INTERPOLATOR_LINEAR );
switch ( m_iDirection )
{
default:
case FN_DIR_UP:
vgui::GetAnimationController()->RunAnimationCommand( this, "ypos", m_iStartY - m_iScrollDistance, 0, 2.0f, vgui::AnimationController::INTERPOLATOR_LINEAR );
break;
case FN_DIR_DOWN:
vgui::GetAnimationController()->RunAnimationCommand( this, "ypos", m_iStartY + m_iScrollDistance, 0, 2.0f, vgui::AnimationController::INTERPOLATOR_LINEAR );
break;
case FN_DIR_LEFT:
vgui::GetAnimationController()->RunAnimationCommand( this, "xpos", m_iStartX - m_iScrollDistance, 0, 2.0f, vgui::AnimationController::INTERPOLATOR_LINEAR );
break;
case FN_DIR_RIGHT:
vgui::GetAnimationController()->RunAnimationCommand( this, "xpos", m_iStartX + m_iScrollDistance, 0, 2.0f, vgui::AnimationController::INTERPOLATOR_LINEAR );
break;
}
}
//-----------------------------------------------------------------------------
// Purpose: Delete panel when floating number has faded out
//-----------------------------------------------------------------------------
void CFloatingAchievementNumber::OnThink()
{
if ( gpGlobals->curtime > m_fStartTime + 1.0f )
{
if ( m_pNumberLabel->GetAlpha() >= 255 )
{
m_pNumberLabel->SetAlpha( 254 );
GetAnimationController()->RunAnimationCommand( m_pNumberLabel, "alpha", 0, 0.0, 1.0f, vgui::AnimationController::INTERPOLATOR_LINEAR );
}
else if ( m_pNumberLabel->GetAlpha() <= 0 )
{
MarkForDeletion();
SetVisible( false );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Debug command to make one of the achievement panels flash as though it just went up
//-----------------------------------------------------------------------------
class CHudAchievementTracker;
void cc_TrackerAnim_f( const CCommand &args )
{
CHudBaseAchievementTracker *pTracker = ( CHudBaseAchievementTracker * )GET_HUDELEMENT( CHudAchievementTracker );
if ( !pTracker )
return;
CAchievementTrackerItem *pItem = pTracker->GetAchievementPanel( atoi(args[1]) );
if ( !pItem )
return;
pItem->AchievementIncremented( pItem->GetLastCount() + RandomInt(1, 1) );
}
ConCommand cc_TrackerAnim( "TrackerAnim", cc_TrackerAnim_f, "Test animation of the achievement tracker. Parameter is achievement number on HUD to flash", FCVAR_NONE );
|