summaryrefslogtreecommitdiff
path: root/game/client/tf/vgui/tf_warinfopanel.cpp
blob: 2458bffc71daf4180db5f893c2c1d3d85f8376bf (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
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
#include "cbase.h"
#include "tf_warinfopanel.h"
#include "econ_controls.h"
#include "econ_item_inventory.h"
#include "vgui_avatarimage.h"
#include "vgui_controls/ProgressBar.h"
#include <vgui_controls/HTML.h>
#include "c_tf_player.h"
#include "econ_ui.h"
#include "tf_asyncpanel.h"
#include "item_model_panel.h"
#include "tf_wardata.h"
#include "iclientmode.h"
#include <vgui_controls/AnimationController.h>
#include "vgui/ISystem.h"

#ifdef STAGING_ONLY
ConVar tf_war_override_active_state( "tf_war_override_active_state", "-1" );
#endif

bool IsWarActive( war_definition_index_t nDefIndex )
{
#ifdef STAGING_ONLY
	if ( tf_war_override_active_state.GetInt() != -1 )
	{
		if ( tf_war_override_active_state.GetInt() == 0 )
			return false;

		return true;
	}
#endif

	const CWarDefinition* pWarDef = GetItemSchema()->GetWarDefinitionByIndex( nDefIndex );
	Assert( pWarDef );
	if ( !pWarDef )
		return false;

	return pWarDef->IsActive();
}

template < typename T >
void SetNestedDialogVariable( Panel *pRoot, const char *pszPanel, const char *pszVariable, T val )
{
	EditablePanel *pPanel = pRoot->FindControl<EditablePanel>( pszPanel, true );
	if ( pPanel )
	{
		pPanel->SetDialogVariable( pszVariable, val );
	}
}

DECLARE_BUILD_FACTORY( CWarStandingPanel );
CWarStandingPanel::CWarStandingPanel( Panel* pParent, const char* pszPanelName )
	: BaseClass( pParent, pszPanelName )
	, m_flLastUpdateTime( 0.f )
	, m_pTeam0ProgressBar( NULL )
	, m_pTeam1ProgressBar( NULL )
	, m_bNeedsLerp( false )
{
	ListenForGameEvent( "global_war_data_updated" );
}

void CWarStandingPanel::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );
	LoadControlSettings( "Resource/UI/econ/WarStandingPanel.res"  );

	for( int i=0; i<2; ++i )
	{
		m_Scores[i].m_pTeamProgressBar = FindControl< ContinuousProgressBar >( CFmtStr( "Team%dProgressBar", i ), true );
		m_Scores[i].m_pContainerPanel  = FindControl< EditablePanel >( CFmtStr( "Team%dContainer", i ), true );
		m_Scores[i].m_pScoreLabel	   = FindControl< CExLabel >( CFmtStr( "Team%dScore", i ), true );
		m_Scores[i].m_pTeamLabel	   = FindControl< CExLabel >( CFmtStr( "Team%dName", i ), true );
	}
}

void CWarStandingPanel::ApplySettings( KeyValues *inResourceData ) 
{
	BaseClass::ApplySettings( inResourceData );

	m_strWarName = inResourceData->GetString( "war_name", NULL );
}

void CWarStandingPanel::OnThink()
{
	BaseClass::OnThink();

	if ( Plat_FloatTime() - m_flLastUpdateTime > 30.f )
	{
		InvalidateLayout();
	}

	if ( m_bNeedsLerp )
	{
		int nTotal = 0;
		if ( m_Scores[ 0 ].m_nNewScore != 0 || m_Scores[ 1 ].m_nNewScore != 0 )
		{
			nTotal = m_Scores[ 0 ].m_nNewScore + m_Scores[ 1 ].m_nNewScore;
		}

		float flPercent = GetPercentAnimated();

		for( int i=0; i<2; ++i )
		{
			// Lerp flPercent from [0,1] -> [StartScore,EndScore]
			float flCurrentScore = RemapValClamped( flPercent, 0.f, 1.f, (float)m_Scores[i].m_nLastScore, (float)m_Scores[i].m_nNewScore );
			
			float flProgressPercent = 0.f;
			if ( nTotal != 0 )
			{
				flProgressPercent = flCurrentScore / (float)nTotal;
			}

			m_Scores[i].m_pTeamProgressBar->SetProgress( flProgressPercent );
			m_Scores[i].m_pContainerPanel->SetDialogVariable( CFmtStr( "team%dscore", i ), CFmtStr( "%.0f%%", flProgressPercent * 100.f ) );

			int nScoreXpos = RemapValClamped( flProgressPercent
											, 0.f
											, 1.f
											, m_Scores[i].m_pTeamProgressBar->GetXPos()
											, m_Scores[i].m_pTeamProgressBar->GetXPos() + m_Scores[i].m_pTeamProgressBar->GetWide() );

			m_Scores[i].m_pScoreLabel->SetPos( nScoreXpos, m_Scores[i].m_pScoreLabel->GetYPos() );
		}

		if ( flPercent == 1.f )
		{
			m_bNeedsLerp = false;
		}
	}
}

void CWarStandingPanel::PerformLayout()
{
	BaseClass::PerformLayout();

	if ( GetPercentAnimated() == 1.f )
	{
		m_flLastUpdateTime = Plat_FloatTime();
	}

	const CWarDefinition* pWarDef = GetItemSchema()->GetWarDefinitionByName( m_strWarName );
	Assert( pWarDef->GetSides().Count() == 2 ); // Needs to be 2 sides to the war for this panel to work!
	CWarData *pWarData = GetLocalPlayerWarData( pWarDef->GetDefIndex() );
	war_side_t nAffiliation = INVALID_WAR_SIDE;
	if ( pWarData )
	{
		nAffiliation = pWarData->Obj().affiliation();
	}

	FOR_EACH_MAP_FAST( pWarDef->GetSides(), i )
	{
		uint64 nScore = nScore = GetWarData().GetGlobalSideScore( pWarDef->GetDefIndex(), pWarDef->GetSide( i )->m_nSideIndex );
				
		m_Scores[ i ].m_nLastScore = m_Scores[ i ].m_nNewScore;
		m_Scores[ i ].m_nNewScore = nScore;

		if ( m_Scores[ i ].m_pTeamLabel )
		{
			m_Scores[ i ].m_pTeamLabel->SetText( pWarDef->GetSide( i )->m_pszLocalizedName );
		}

		// Check if there's a change to show
		m_bNeedsLerp |= m_Scores[ i ].m_nLastScore != m_Scores[ i ].m_nNewScore;

		if ( !m_bNeedsLerp )
		{
			m_Scores[i].m_pContainerPanel->SetDialogVariable( CFmtStr( "team%dscore", i ), CFmtStr( "%.0f%%", m_Scores[i].m_pTeamProgressBar->GetProgress() * 100.f ) );
		}

		// Set the "(Your side)" labels
		SetControlVisible( CFmtStr( "Team%dYourSide", i ), i == nAffiliation, true );
	}
}

float CWarStandingPanel::GetPercentAnimated() const
{
	float flTimeSinceLastUpdate = Plat_FloatTime() - m_flLastUpdateTime;
	float flPercent = Bias( RemapValClamped( flTimeSinceLastUpdate, 0.f, 1.2f, 0.f, 1.f ), 0.8f );
	return flPercent;
}

void CWarStandingPanel::FireGameEvent( IGameEvent *event )
{
	if ( FStrEq( event->GetName(), "global_war_data_updated" ) )
	{
		InvalidateLayout();
	}
}


DECLARE_BUILD_FACTORY( CWarLandingPanel );

CWarLandingPanel::CWarLandingPanel( Panel *pParent, const char *pszPanelName )
	: BaseClass( pParent, pszPanelName )
	, m_flChoseTeamTime( 0.f )
	, m_nLastKnownSide( INVALID_WAR_SIDE )
	, m_nPendingSide( INVALID_WAR_SIDE )
	, m_eJoiningState( NO_ACTION )
{
}

void CWarLandingPanel::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	KeyValues* pKVConditions = new KeyValues( "conditions" );

	if ( IsWarActive( PYRO_VS_HEAVY_WAR_DEF_INDEX ) )
	{
		pKVConditions->AddSubKey( new KeyValues( "if_war_active" ) );
	}
	else
	{
		pKVConditions->AddSubKey( new KeyValues( "if_war_over" ) );
	}

	LoadControlSettings( "Resource/UI/econ/WarJoinPanel.res", NULL, NULL, pKVConditions );
}

void CWarLandingPanel::ApplySettings( KeyValues *inResourceData )
{
	BaseClass::ApplySettings( inResourceData );

	m_strSceneAnimName = inResourceData->GetString( "scene_anim_name", NULL );
	Assert( m_strSceneAnimName.Length() );
}

void CWarLandingPanel::OnCommand( const char *pCommand )
{
	if ( FStrEq( pCommand, "close" ) )
	{
		if ( m_eJoiningState == NO_ACTION )
		{
			SetVisible( false );
		}
	}
	else if ( V_strncasecmp( pCommand, "join_war", V_strlen( "join_war" ) ) == 0 )
	{
		int nSide = V_atoi( &pCommand[ V_strlen( "join_war" ) ] );

		const CWarDefinition* pWarDef = GetItemSchema()->GetWarDefinitionByIndex( PYRO_VS_HEAVY_WAR_DEF_INDEX );

		if ( !IsWarActive( PYRO_VS_HEAVY_WAR_DEF_INDEX ) )
		{
			DevMsg( "War is inactive" );
			return;
		}

		if ( !pWarDef->IsValidSide( nSide ) )
		{
			DevMsg( "%d is not a valid side", nSide );
			return;
		}
	
		m_nPendingSide = nSide;
		Assert( m_eJoiningState == NO_ACTION );
		m_eJoiningState = CONFIRM_SIDE_SELECTION;
		
		UpdateUIState();
		return;
	}
	else if ( FStrEq( pCommand, "confirm_team" ) )
	{
		if ( !IsWarActive( PYRO_VS_HEAVY_WAR_DEF_INDEX ) )
		{
			DevMsg( "War is inactive" );
			return;
		}

		// Join the war!
		GCSDK::CProtoBufMsg< CGCMsgGC_War_JoinWar > msg( k_EMsgGC_War_JoinWar );

		msg.Body().set_affiliation( m_nPendingSide );
		msg.Body().set_war_id( PYRO_VS_HEAVY_WAR_DEF_INDEX );

		GCClientSystem()->BSendMessage( msg );
		m_flChoseTeamTime = Plat_FloatTime();
		m_eJoiningState = ATTEMPTING_TO_JOIN_AND_WAITING_FOR_RESPONSE;

		UpdateUIState();
		return;
	}
	else if ( FStrEq( pCommand, "dismiss_joining_result" ) )
	{
		m_eJoiningState = NO_ACTION;
		m_nPendingSide = INVALID_WAR_SIDE;
		UpdateUIState();

		return;
	}
	else if ( FStrEq( "view_update_comic", pCommand ) )
	{
		const char* pszComicURL = "http://www.teamfortress.com/theshowdown/";

		if ( steamapicontext && steamapicontext->SteamFriends() && steamapicontext->SteamUtils() && steamapicontext->SteamUtils()->IsOverlayEnabled() )
		{
			steamapicontext->SteamFriends()->ActivateGameOverlayToWebPage( pszComicURL );
		}
		else
		{
			vgui::system()->ShellExecute( "open", pszComicURL );
		}

		return;
	}

	BaseClass::OnCommand( pCommand );
}

void CWarLandingPanel::OnThink()
{
	BaseClass::OnThink();
	
	if ( ( Plat_FloatTime() - m_flChoseTeamTime ) > 5.f && m_eJoiningState == ATTEMPTING_TO_JOIN_AND_WAITING_FOR_RESPONSE )
	{
		m_eJoiningState = FAILED_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION;
		UpdateUIState();
	}
}

#ifdef STAGING_ONLY
// 12345?  Yea, well, we probably will never have 12,345 sides to a war
ConVar tf_fake_war_side( "tf_fake_war_side", "12345" );
#endif

void CWarLandingPanel::PerformLayout()
{
	BaseClass::PerformLayout();

#ifdef STAGING_ONLY
	if ( tf_fake_war_side.GetInt() != 12345 )
	{
		m_nLastKnownSide = tf_fake_war_side.GetInt();
	}
#endif

	UpdateUIState();
}


void CWarLandingPanel::UpdateWarStatus( const CSteamID & steamIDOwner, const CSharedObject *pObject, ESOCacheEvent eEvent )
{
	if ( pObject->GetTypeID() != CWarData::k_nTypeID )
	{
		return;
	}

	if ( !steamapicontext || !steamapicontext->SteamUser() )
	{
		return;
	}

	// Make sure this is for us
	CSteamID steamID = steamapicontext->SteamUser()->GetSteamID();
	if ( steamIDOwner != steamID )
	{
		return;
	}

	const CWarData* pWarData = assert_cast< const CWarData* >( pObject );
	if ( pWarData->Obj().affiliation() != m_nLastKnownSide )
	{
		if ( m_eJoiningState == ATTEMPTING_TO_JOIN_AND_WAITING_FOR_RESPONSE )
		{
			m_eJoiningState = SUCCESS_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION;
		}

		m_nLastKnownSide = pWarData->Obj().affiliation();
	}

	UpdateUIState();
}

void CWarLandingPanel::SetVisible( bool bVisible )
{
	BaseClass::SetVisible( bVisible );

	EditablePanel* pSceneContainer = FindControl< EditablePanel >( "SceneContainer", true );
	if ( pSceneContainer )
	{
		g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( pSceneContainer, m_strSceneAnimName, false );
	}
}

void CWarLandingPanel::UpdateUIState()
{
	const CWarDefinition* pWarDef = GetItemSchema()->GetWarDefinitionByIndex( PYRO_VS_HEAVY_WAR_DEF_INDEX ); // SLAM
	if ( !pWarDef )
		return;

	EditablePanel* pMainContainer = FindControl< EditablePanel >( "MainContainer" );
	if ( pMainContainer )
	{
		bool bWarActive = IsWarActive( PYRO_VS_HEAVY_WAR_DEF_INDEX );
		pMainContainer->SetControlVisible( "WarOverContainer", !bWarActive );

		bool bNeedsToJoin = m_nLastKnownSide == INVALID_WAR_SIDE;
		pMainContainer->SetControlVisible( "AffiliatedContainer", bWarActive && !bNeedsToJoin && m_eJoiningState == NO_ACTION );
		pMainContainer->SetControlVisible( "UnaffiliatedContainer", bWarActive && bNeedsToJoin && m_eJoiningState == NO_ACTION );

		bool bHasGCConnection = GCClientSystem()->BConnectedtoGC();
		pMainContainer->SetControlVisible( "JoinPyroButton", bHasGCConnection, true );
		pMainContainer->SetControlVisible( "JoinHeavyButton", bHasGCConnection, true );
		pMainContainer->SetControlVisible( "NoGContainer", !bHasGCConnection, true );
	}

	static wchar_t wszEndDateOutString[ 128 ];
	char time_buf[k_RTimeRenderBufferSize];
	CRTime timeExpire( pWarDef->GetEndDate() );
	timeExpire.SetToGMT( false );
	timeExpire.Render( time_buf );
	wchar_t wszTemp[ 1024 ];
	V_UTF8ToUnicode( time_buf, wszTemp, sizeof(wszTemp) );

	const wchar_t *wpszEndDateFormat = g_pVGuiLocalize->Find( IsWarActive( PYRO_VS_HEAVY_WAR_DEF_INDEX ) ? "#TF_War_EndFutureDate" : "#TF_War_EndPastDate" );

	g_pVGuiLocalize->ConstructString_safe( wszEndDateOutString, wpszEndDateFormat, 1, wszTemp );
	CExLabel* pSceneContainer = FindControl< CExLabel >( "EndDateLabel", true );
	if ( pSceneContainer )
	{
		pSceneContainer->SetText( wszEndDateOutString );
	}


	EditablePanel* pJoiningPopup = FindControl< EditablePanel >( "CommunicatingWithGCPopup", true );
	if ( !pJoiningPopup )
		return;

	switch ( m_eJoiningState )
	{
	case NO_ACTION:
		break;
	case CONFIRM_SIDE_SELECTION:
	{
		Assert( m_nPendingSide != INVALID_WAR_SIDE );
		const CWarDefinition::CWarSideDefinition_t* pChosenSide = pWarDef->GetSide( m_nPendingSide ); // Can be NULL
		Assert( pChosenSide );
		if ( !pChosenSide )
			return;

		static wchar_t wszOutString[ 128 ];
		const wchar_t *wpszFormat = g_pVGuiLocalize->Find( "#TF_War_ConfirmSideSelection" );
		g_pVGuiLocalize->ConstructString_safe( wszOutString, wpszFormat, 1, g_pVGuiLocalize->Find( pChosenSide->m_pszLocalizedName ) );
		
		EditablePanel* pJoining = FindControl< EditablePanel >( "ConfirmSelectionContainer", true );
		if ( pJoining )
		{
			pJoining->SetDialogVariable( "confirm_selection", wszOutString );
		}
	}
		break;
	case ATTEMPTING_TO_JOIN_AND_WAITING_FOR_RESPONSE:
	{
		Assert( m_nPendingSide != INVALID_WAR_SIDE );
		const CWarDefinition::CWarSideDefinition_t* pChosenSide = pWarDef->GetSide( m_nPendingSide ); // Can be NULL
		Assert( pChosenSide );
		if ( !pChosenSide )
			return;

		static wchar_t wszOutString[ 128 ];
		const wchar_t *wpszFormat = g_pVGuiLocalize->Find( "#TF_War_JoiningTeam" );
		g_pVGuiLocalize->ConstructString_safe( wszOutString, wpszFormat, 1, g_pVGuiLocalize->Find( pChosenSide->m_pszLocalizedName ) );
		
		EditablePanel* pJoining = FindControl< EditablePanel >( "JoiningContainer", true );
		if ( pJoining )
		{
			pJoining->SetDialogVariable( "joining_team", wszOutString );
		}
	}

		break;

	case SUCCESS_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION:
	{
		const CWarDefinition::CWarSideDefinition_t* pChosenSide = pWarDef->GetSide( m_nLastKnownSide ); // Can be NULL
		Assert( pChosenSide );
		if ( !pChosenSide )
			return;

		static wchar_t wszOutString[ 128 ];
		const wchar_t *wpszFormat = g_pVGuiLocalize->Find( "#TF_War_JoinedTeam" );
		g_pVGuiLocalize->ConstructString_safe( wszOutString, wpszFormat, 1, g_pVGuiLocalize->Find( pChosenSide->m_pszLocalizedName ) );
		EditablePanel* pJoined = FindControl< EditablePanel >( "TeamJoinedContainer", true );
		if ( pJoined )
		{
			pJoined->SetDialogVariable( "team_joined", wszOutString );
		}
	}
		break;

	case FAILED_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION:

		break;

	default:
		Assert( false );
		return;
	}

	pJoiningPopup->SetVisible( m_eJoiningState != NO_ACTION );

	pJoiningPopup->SetControlVisible( "ConfirmSelectionContainer", m_eJoiningState == CONFIRM_SIDE_SELECTION, true );
	pJoiningPopup->SetControlVisible( "JoiningContainer", m_eJoiningState == ATTEMPTING_TO_JOIN_AND_WAITING_FOR_RESPONSE, true );
	pJoiningPopup->SetControlVisible( "TeamJoinedContainer", m_eJoiningState == SUCCESS_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION, true );
	pJoiningPopup->SetControlVisible( "FailedToJoinContainer", m_eJoiningState == FAILED_RESPONSE_RECIEVED_WAITING_FOR_USER_CONFIRMATION, true );
}

#if defined( STAGING_ONLY )
CON_COMMAND( tf_war_join_side, "Join a specified war on a specified side" )
{
	if ( args.ArgC() < 2 )
		return;

	war_definition_index_t nWar = atoi( args[1] );
	const CWarDefinition* pWarDef = GetItemSchema()->GetWarDefinitionByIndex( nWar );
	if ( pWarDef == NULL)
		return;

	war_side_t nSide = atoi( args[2] );
	// Allow INVALID_WAR_SIDE for testing purposes
	if ( pWarDef->GetSide( nSide ) == NULL && nSide != INVALID_WAR_SIDE )
		return;

	// Join the war!
	GCSDK::CProtoBufMsg< CGCMsgGC_War_JoinWar > msg( k_EMsgGC_War_JoinWar );

	msg.Body().set_war_id( nWar );
	msg.Body().set_affiliation( nSide );

	GCClientSystem()->BSendMessage( msg );
}
#endif