summaryrefslogtreecommitdiff
path: root/gameui/matchmaking/sessionbrowserdialog.cpp
blob: f29b1775a8ec2e3cb0a8db93b11d8e3b8ba9bd52 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Present a list of sessions from which the player can choose a game to join.
//
//=============================================================================//

#include "sessionbrowserdialog.h"
#include "engine/imatchmaking.h"
#include "EngineInterface.h"
#include "vgui_controls/ImagePanel.h"
#include "vgui_controls/Label.h"
#include "KeyValues.h"
#include "vgui/ISurface.h"

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

CSessionBrowserDialog *g_pBrowserDialog;

//--------------------------------
// CSessionBrowserDialog
//--------------------------------
CSessionBrowserDialog::CSessionBrowserDialog( vgui::Panel *pParent, KeyValues *pDialogKeys ) : BaseClass( pParent, "" )
{
	g_pBrowserDialog = this;
	m_pDialogKeys = pDialogKeys;

	SetDeleteSelfOnClose( true );
}

CSessionBrowserDialog::~CSessionBrowserDialog()
{
	m_pScenarioInfos.PurgeAndDeleteElements();
}

//---------------------------------------------------------------------
// Purpose: Center the dialog on the screen
//---------------------------------------------------------------------
void CSessionBrowserDialog::PerformLayout()
{
	BaseClass::PerformLayout();

	MoveToCenterOfScreen();
	UpdateScenarioDisplay();
}

//---------------------------------------------------------------------
// Purpose: Parse session properties and contexts from the resource file
//---------------------------------------------------------------------
void CSessionBrowserDialog::ApplySettings( KeyValues *pResourceData )
{
	BaseClass::ApplySettings( pResourceData );

	KeyValues *pScenarios = pResourceData->FindKey( "ScenarioInfoPanels" );
	if ( pScenarios )
	{
		for ( KeyValues *pScenario = pScenarios->GetFirstSubKey(); pScenario != NULL; pScenario = pScenario->GetNextKey() )
		{
			CScenarioInfoPanel *pScenarioInfo = new CScenarioInfoPanel( this, "ScenarioInfoPanel" );
			SETUP_PANEL( pScenarioInfo );
			pScenarioInfo->m_pTitle->SetText( pScenario->GetString( "title" ) );
			pScenarioInfo->m_pSubtitle->SetText( pScenario->GetString( "subtitle" ) );
			pScenarioInfo->m_pMapImage->SetImage( pScenario->GetString( "image" ) );

			int nTall = pScenario->GetInt( "tall", -1 );
			if ( nTall > 0 )
			{
				pScenarioInfo->SetTall( nTall );
			}

			int nXPos = pScenario->GetInt( "xpos", -1 );
			if ( nXPos >= 0 )
			{
				int x, y;
				pScenarioInfo->GetPos( x, y );
				pScenarioInfo->SetPos( nXPos, y );
			}

			int nDescOneYpos = pScenario->GetInt( "descOneY", -1 );
			if ( nDescOneYpos > 0 )
			{
				int x, y;
				pScenarioInfo->m_pDescOne->GetPos( x, y );
				pScenarioInfo->m_pDescOne->SetPos( x, nDescOneYpos );
			}

			int nDescTwoYpos = pScenario->GetInt( "descTwoY", -1 );
			if ( nDescTwoYpos > 0 )
			{
				int x, y;
				pScenarioInfo->m_pDescTwo->GetPos( x, y );
				pScenarioInfo->m_pDescTwo->SetPos( x, nDescTwoYpos );
			}

			m_pScenarioInfos.AddToTail( pScenarioInfo );
		}
	}
}

//---------------------------------------------------------------------
// Purpose: Set up colors and other such stuff
//---------------------------------------------------------------------
void CSessionBrowserDialog::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	for ( int i = 0; i < m_pScenarioInfos.Count(); ++i )
	{
		m_pScenarioInfos[i]->SetBgColor( pScheme->GetColor( "TanDark", Color( 0, 0, 0, 255 ) ) );
	}
}

//---------------------------------------------------------------------
// Purpose: Info about a session, sent from matchmaking
//---------------------------------------------------------------------
void CSessionBrowserDialog::SessionSearchResult( int searchIdx, void *pHostData, XSESSION_SEARCHRESULT *pResult, int ping )
{
	char *pPing;
	switch ( ping )
	{
	case 0:
		pPing = "#TF_Icon_Ping_Green";
		break;
	case 1:
		pPing = "#TF_Icon_Ping_Yellow";
		break;
	case 2:
	default:
		pPing = "#TF_Icon_Ping_Red";
		break;
	}

	int nScenarioId = 0;
	uint nContextId = m_pDialogKeys->GetInt( "scenario", -1 );
	for ( uint i = 0; i < pResult->cContexts; ++i )
	{
		if ( pResult->pContexts[i].dwContextId == nContextId )
		{
			nScenarioId = pResult->pContexts[i].dwValue;
			break;
		}
	}
 
	hostData_s *pData = (hostData_s*)pHostData;

	int filledSlots = pResult->dwFilledPublicSlots + pResult->dwFilledPrivateSlots;
	int totalSlots = filledSlots + pResult->dwOpenPublicSlots + pResult->dwOpenPrivateSlots;

	m_GameStates.AddToTail( pData->gameState );
	m_GameTimes.AddToTail( pData->gameTime );
	m_XUIDs.AddToTail( pData->xuid );

	char szSlots[16] = {0};
	Q_snprintf( szSlots, sizeof( szSlots ), "%d/%d", filledSlots, totalSlots );

	int ct = 0;
	const char *ppStrings[4];
	ppStrings[ct++] = pData->hostName;
	ppStrings[ct++] = szSlots;
	ppStrings[ct++] = pData->scenario;
	if ( ping != -1 )
	{
		ppStrings[ct++] = pPing;
	}
	m_Menu.AddSectionedItem( ppStrings, ct );

	m_ScenarioIndices.AddToTail( nScenarioId );
	m_SearchIndices.AddToTail( searchIdx );

	if ( m_Menu.GetItemCount() == 1 )
	{
		m_Menu.SetFocus( 0 );
	}

	UpdateScenarioDisplay();
}

//-----------------------------------------------------------------
// Purpose: Show the correct scenario image and text
//-----------------------------------------------------------------
void CSessionBrowserDialog::UpdateScenarioDisplay( void )
{
	if ( !m_ScenarioIndices.Count() )
		return;

	// Check if the selected map has changed (first menu item)
	int idx = m_Menu.GetActiveItemIndex();
	for ( int i = 0; i < m_pScenarioInfos.Count(); ++i )
	{
		m_pScenarioInfos[i]->SetVisible( i == m_ScenarioIndices[idx] );
	}

	// Get the screen size
	int wide, tall;
	vgui::surface()->GetScreenSize(wide, tall);
	bool bLodef = ( tall <= 480 );

	const char *pState = "";
	switch( m_GameStates[idx] )
	{
	case 0:
		if ( bLodef )
		{
			pState = "#TF_GameState_InLobby_lodef";
		}
		else
		{
			pState = "#TF_GameState_InLobby";
		}
		break;

	case 1:
		if ( bLodef )
		{
			pState = "#TF_GameState_GameInProgress_lodef";
		}
		else
		{
			pState = "#TF_GameState_GameInProgress";
		}
		break;
	}

	char szTime[32] = {0};
	if ( m_GameTimes[idx] >= NO_TIME_LIMIT )
	{
		Q_strncpy( szTime, "#TF_NoTimeLimit", sizeof( szTime) );
	}
	else
	{
		Q_snprintf( szTime, sizeof( szTime), "%d:00", m_GameTimes[idx] );
	}

	if ( !m_pScenarioInfos.IsValidIndex( m_ScenarioIndices[idx] ) )
		return;

	CScenarioInfoPanel *pPanel = m_pScenarioInfos[ m_ScenarioIndices[idx] ];
	pPanel->m_pDescOne->SetText( pState );
	pPanel->m_pDescTwo->SetText( szTime );
}


//-----------------------------------------------------------------
// helper to swap two ints in a utlvector
//-----------------------------------------------------------------
static void Swap( CUtlVector< int > &vec, int iOne, int iTwo )
{
	int temp = vec[iOne];
	vec[iOne] = vec[iTwo];
	vec[iTwo] = temp;
}

//-----------------------------------------------------------------
// Purpose: Swap the order of two menu items
//-----------------------------------------------------------------
void CSessionBrowserDialog::SwapMenuItems( int iOne, int iTwo )
{
	Swap( m_ScenarioIndices, iOne, iTwo );
	Swap( m_SearchIndices, iOne, iTwo );
	Swap( m_GameStates, iOne, iTwo );
	Swap( m_GameTimes, iOne, iTwo );

	// swap the XUIDs, too
	XUID temp = m_XUIDs[iOne];
	m_XUIDs[iOne] = m_XUIDs[iTwo];
	m_XUIDs[iTwo] = temp;
}

//-----------------------------------------------------------------
// Purpose: Handle commands from the dialog menu
//-----------------------------------------------------------------
void CSessionBrowserDialog::OnCommand( const char *pCommand )
{
	if ( !Q_stricmp( pCommand, "SelectSession" ) )
	{
		int idx = m_SearchIndices[ m_Menu.GetActiveItemIndex() ];
		matchmaking->SelectSession( idx );
	}

	m_pParent->OnCommand( pCommand );
}

//-----------------------------------------------------------------
// Purpose: Send key presses to the dialog's menu
//-----------------------------------------------------------------
void CSessionBrowserDialog::OnKeyCodePressed( vgui::KeyCode code )
{
	switch( code )
	{
	case KEY_XBUTTON_B:
		matchmaking->KickPlayerFromSession( 0 );
		break;
	case KEY_XBUTTON_X:
#ifdef _X360
		int idx = m_Menu.GetActiveItemIndex();
		if ( m_XUIDs.IsValidIndex( idx ) )
		{
			XShowGamerCardUI( XBX_GetPrimaryUserId(), m_XUIDs[idx] );
		}
#endif
		break;
	}

	BaseClass::OnKeyCodePressed( code );

	// Selected session may have been updated
	UpdateScenarioDisplay();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CSessionBrowserDialog::OnThink()
{
	vgui::KeyCode code = m_KeyRepeat.KeyRepeated();
	if ( code )
	{
		m_Menu.HandleKeyCode( code );
		UpdateScenarioDisplay();
	}

	BaseClass::OnThink();
}