summaryrefslogtreecommitdiff
path: root/serverbrowser/QuickListPanel.cpp
blob: 0b08ca027103cdc3200ee10f3c4ca89028706e49 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================
#include "pch_serverbrowser.h"

using namespace vgui;


//-----------------------------------------------------------------------------
// Purpose: Invisible panel that forwards up mouse movement
//-----------------------------------------------------------------------------
class CMouseMessageForwardingPanel : public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CMouseMessageForwardingPanel, vgui::Panel );
public:
	CMouseMessageForwardingPanel( Panel *parent, const char *name );

	virtual void PerformLayout( void );
	virtual void OnMousePressed( vgui::MouseCode code );
	virtual void OnMouseDoublePressed( vgui::MouseCode code );
	virtual void OnMouseWheeled(int delta);
};

CMouseMessageForwardingPanel::CMouseMessageForwardingPanel( Panel *parent, const char *name ) : BaseClass( parent, name )
{
	// don't draw an
	SetPaintEnabled(false);
	SetPaintBackgroundEnabled(false);
	SetPaintBorderEnabled(false);
}

void CMouseMessageForwardingPanel::PerformLayout()
{
	// fill out the whole area
	int w, t;
	GetParent()->GetSize(w, t);
	SetBounds(0, 0, w, t);
}

void CMouseMessageForwardingPanel::OnMousePressed( vgui::MouseCode code )
{
	if ( GetParent() )
	{
		GetParent()->OnMousePressed( code );
	}
}

void CMouseMessageForwardingPanel::OnMouseDoublePressed( vgui::MouseCode code )
{
	if ( GetParent() )
	{
		GetParent()->OnMouseDoublePressed( code );
	}
}

void CMouseMessageForwardingPanel::OnMouseWheeled(int delta)
{
	if ( GetParent() )
	{
		GetParent()->OnMouseWheeled( delta );
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CQuickListPanel::CQuickListPanel( vgui::Panel* pParent, const char *pElementName ) : BaseClass( pParent, pElementName )
{
	SetParent( pParent );

	m_pListPanelParent = pParent;

	CMouseMessageForwardingPanel *panel = new CMouseMessageForwardingPanel(this, NULL);
	panel->SetZPos(3);

	m_pLatencyImage = new ImagePanel( this, "latencyimage" );
	m_pPlayerCountLabel = new Label( this, "playercount", "" );
	m_pOtherServersLabel = new Label( this, "otherservercount", "" );
	m_pServerNameLabel = new Label( this, "servername", "" );
	m_pBGroundPanel = new Panel( this, "background" );
	m_pMapImage = new ImagePanel( this, "mapimage" );
	m_pGameTypeLabel = new Label( this, "gametype", "" );
	m_pMapNameLabel = new Label( this, "mapname", "" );
	m_pLatencyLabel = new Label( this, "latencytext", "" );
	m_pReplayImage = new ImagePanel( this, "replayimage" );

	const char *pPathID = "PLATFORM";

	if ( g_pFullFileSystem->FileExists( "servers/QuickListPanel.res", "MOD" ) )
	{
		pPathID = "MOD";
	}
	
	LoadControlSettings( "servers/QuickListPanel.res", pPathID );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::ApplySchemeSettings(IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);
	
	if ( pScheme && m_pBGroundPanel )
	{
		m_pBGroundPanel->SetBgColor( pScheme->GetColor("QuickListBGDeselected", Color(255, 255, 255, 0 ) ) );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::SetRefreshing( void )
{
	if ( m_pServerNameLabel )
	{
		m_pServerNameLabel->SetText( g_pVGuiLocalize->Find("#ServerBrowser_QuickListRefreshing") );
	}

	if ( m_pPlayerCountLabel )
	{
		m_pPlayerCountLabel->SetVisible( false );
	}
	if ( m_pOtherServersLabel )
	{
		m_pOtherServersLabel->SetVisible( false );
	}

	if ( m_pLatencyImage )
	{
		m_pLatencyImage->SetVisible( false );
	}

	if ( m_pReplayImage )
	{
		m_pReplayImage->SetVisible( false );
	}

	if ( m_pLatencyLabel )
	{
		m_pLatencyLabel->SetVisible( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::SetMapName( const char *pMapName )
{
	Q_strncpy( m_szMapName, pMapName, sizeof( m_szMapName ) );

	if ( m_pMapNameLabel )
	{
		m_pMapNameLabel->SetText( pMapName );
		m_pMapNameLabel->SizeToContents();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::SetGameType( const char *pGameType )
{
	if ( strlen ( pGameType ) == 0 )
	{
		m_pGameTypeLabel->SetVisible( false );
		return;
	}

	char gametype[ 512 ];
	Q_snprintf( gametype, sizeof( gametype ), "(%s)", pGameType );

	m_pGameTypeLabel->SetText( gametype );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::SetServerInfo ( KeyValues *pKV, int iListID, int iTotalServers )
{
	if ( pKV == NULL )
		return;

	m_iListID = iListID;

	m_pServerNameLabel->SetText( pKV->GetString( "name", " " ) );

	int iPing = pKV->GetInt( "ping", 0 );

	if ( iPing <= 100 )
	{
		m_pLatencyImage->SetImage( "../vgui/icon_con_high.vmt" );
	}
	else if ( iPing <= 150 )
	{
		m_pLatencyImage->SetImage( "../vgui/icon_con_medium.vmt" );
	}
	else
	{
		m_pLatencyImage->SetImage( "../vgui/icon_con_low.vmt" );
	}

	m_pLatencyImage->SetVisible( false );

	if ( GameSupportsReplay() )
	{
		if ( pKV->GetInt( "Replay", 0 ) > 0 )
		{
			m_pReplayImage->SetVisible( true );
		}
	}

	char ping[ 512 ];
	Q_snprintf( ping, sizeof( ping ), "%d ms", iPing );

	m_pLatencyLabel->SetText( ping );
	m_pLatencyLabel->SetVisible( true );

	wchar_t players[ 512 ];
	wchar_t playercount[16];
	wchar_t *pwszPlayers = g_pVGuiLocalize->Find("#ServerBrowser_Players");

	g_pVGuiLocalize->ConvertANSIToUnicode( pKV->GetString( "players", " " ), playercount,  sizeof( playercount ) );

	_snwprintf( players, ARRAYSIZE( players ), L"%ls %ls",  playercount, pwszPlayers );
	
	m_pPlayerCountLabel->SetText( players );
	m_pPlayerCountLabel->SetVisible( true );


	// Now setup the other server count
	if ( iTotalServers == 2 )
	{
		m_pOtherServersLabel->SetText( g_pVGuiLocalize->Find("#ServerBrowser_QuickListOtherServer") );
		m_pOtherServersLabel->SetVisible( true );
	}
	else if ( iTotalServers > 2 )
	{
		wchar_t *pwszServers = g_pVGuiLocalize->Find("#ServerBrowser_QuickListOtherServers");
		_snwprintf( playercount, Q_ARRAYSIZE(playercount), L"%d", (iTotalServers-1) );
		g_pVGuiLocalize->ConstructString( players, sizeof( players ), pwszServers, 1, playercount );
		m_pOtherServersLabel->SetText( players );
		m_pOtherServersLabel->SetVisible( true );
	}
	else
	{
		m_pOtherServersLabel->SetVisible( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CQuickListPanel::SetImage( const char *pMapName )
{
	char path[ 512 ];
	Q_snprintf( path, sizeof( path ), "materials/vgui/maps/menu_thumb_%s.vmt", pMapName );

	char map[ 512 ];
	Q_snprintf( map, sizeof( map ), "maps/%s.bsp", pMapName );

	if ( g_pFullFileSystem->FileExists( map, "MOD" ) == false  )
	{
		pMapName = "default_download";
	}
	else
	{
		if ( g_pFullFileSystem->FileExists( path, "MOD" ) == false  )
		{
			pMapName = "default";
		}
	}

	if ( m_pMapImage )
	{
		char imagename[ 512 ];
		Q_snprintf( imagename, sizeof( imagename ), "..\\vgui\\maps\\menu_thumb_%s", pMapName );

		m_pMapImage->SetImage ( imagename );
		m_pMapImage->SetMouseInputEnabled( false );
	}							
}

void CQuickListPanel::OnMousePressed( vgui::MouseCode code )
{
	if ( m_pListPanelParent )
	{
		vgui::PanelListPanel *pParent = dynamic_cast < vgui::PanelListPanel *> ( m_pListPanelParent );

		if ( pParent )
		{
			pParent->SetSelectedPanel( this );
			m_pListPanelParent->CallParentFunction( new KeyValues("ItemSelected", "itemID", -1 ) );
		}

		if ( code == MOUSE_RIGHT )
		{
			m_pListPanelParent->CallParentFunction( new KeyValues("OpenContextMenu", "itemID", -1 ) );
		}

	}
}

void CQuickListPanel::OnMouseDoublePressed( vgui::MouseCode code )
{
	if ( code == MOUSE_RIGHT )
		return;

	// call the panel
	OnMousePressed( code );

	m_pListPanelParent->CallParentFunction( new KeyValues("ConnectToServer", "code", code) );
}