summaryrefslogtreecommitdiff
path: root/game/client/dod/VGUI/dodclassmenu.cpp
blob: a32d1af24674b9f149c2f72c224cfb8cfe4c08f3 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "dodclassmenu.h"

#include <KeyValues.h>
#include <filesystem.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/RichText.h>
#include <vgui/IVGui.h>

#include "hud.h" // for gEngfuncs
#include "c_dod_player.h"
#include "c_dod_team.h"

#include "imagemouseoverbutton.h"

#include "dodmouseoverpanelbutton.h"
#include "dodrandombutton.h"
#include "IconPanel.h"

#include "IGameUIFuncs.h" // for key bindings

extern IGameUIFuncs *gameuifuncs; // for key binding details

using namespace vgui;

extern ConVar hud_classautokill;
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
Panel *CDODClassInfoPanel::CreateControlByName( const char *controlName )
{
	if( !Q_stricmp( "ProgressBar", controlName ) )
	{
		return new CDODProgressBar(this);
	}
	else if ( !Q_stricmp( "CIconPanel", controlName ) )
	{
		return new CIconPanel(this, "icon_panel");
	}
	else
	{
		return BaseClass::CreateControlByName( controlName );
	}
}

void CDODClassInfoPanel::ApplySchemeSettings( IScheme *pScheme )
{
	RichText *pClassInfo = dynamic_cast<RichText*>(FindChildByName("classInfo"));

	if ( pClassInfo )
	{
		pClassInfo->SetBorder(pScheme->GetBorder("NoBorder"));
		pClassInfo->SetBgColor(pScheme->GetColor("Blank", Color(0,0,0,0)));
	}

	BaseClass::ApplySchemeSettings( pScheme );
}

CDODClassMenu::CDODClassMenu(IViewPort *pViewPort) : CClassMenu(pViewPort)
{
	m_mouseoverButtons.RemoveAll();
	m_iClassMenuKey = BUTTON_CODE_INVALID;
	m_pInitialButton = NULL;

	m_pBackground = SETUP_PANEL( new CDODMenuBackground( this ) );

	m_pClassInfoPanel = new CDODClassInfoPanel( this, "ClassInfoPanel" );
	
	vgui::ivgui()->AddTickSignal( GetVPanel() );

	m_iActivePlayerClass = -1;
	m_iLastPlayerClassCount = -1;

	m_pClassNumLabel[0] = new Label( this, "class_1_num", "" );
	m_pClassNumLabel[1] = new Label( this, "class_2_num", "" );
	m_pClassNumLabel[2] = new Label( this, "class_3_num", "" );
	m_pClassNumLabel[3] = new Label( this, "class_4_num", "" );
	m_pClassNumLabel[4] = new Label( this, "class_5_num", "" );
	m_pClassNumLabel[5] = new Label( this, "class_6_num", "" );

	m_pClassFullLabel[0] = new Label( this, "class_1_full", "" );
	m_pClassFullLabel[1] = new Label( this, "class_2_full", "" );
	m_pClassFullLabel[2] = new Label( this, "class_3_full", "" );
	m_pClassFullLabel[3] = new Label( this, "class_4_full", "" );
	m_pClassFullLabel[4] = new Label( this, "class_5_full", "" );
	m_pClassFullLabel[5] = new Label( this, "class_6_full", "" );

	m_pSuicideOption = new CheckButton( this, "suicide_option", "Sky is blue?" );
}

void CDODClassMenu::ShowPanel( bool bShow )
{
	if ( bShow )
	{
		engine->CheckPoint( "ClassMenu" );

		m_iClassMenuKey = gameuifuncs->GetButtonCodeForBind( "changeclass" );

		m_pSuicideOption->SetSelected( hud_classautokill.GetBool() );
	}

	for( int i = 0; i< GetChildCount(); i++ ) 
	{
		CImageMouseOverButton<CDODClassInfoPanel> *button =
			dynamic_cast<CImageMouseOverButton<CDODClassInfoPanel> *>(GetChild(i));

		if ( button )
		{
			if( button == m_pInitialButton && bShow == true )
				button->ShowPage();
			else
				button->HidePage();
		}
	}

	CDODRandomButton<CDODClassInfoPanel> *pRandom =
		dynamic_cast<CDODRandomButton<CDODClassInfoPanel> *>( FindChildByName("random") );

	if ( pRandom )
		pRandom->HidePage();

	// recalc position of checkbox, since it doesn't do right alignment
	m_pSuicideOption->SizeToContents();

	int x, y, wide, tall; 
	m_pSuicideOption->GetBounds( x, y, wide, tall );

	int parentW, parentH;
	GetSize( parentW, parentH );

	x = parentW / 2;	// - wide;
	m_pSuicideOption->SetPos( x, y );

	BaseClass::ShowPanel( bShow );
}

void CDODClassMenu::OnKeyCodePressed( KeyCode code )
{
#ifdef REFRESH_CLASSMENU_TOOL

	if ( code == KEY_PAD_MULTIPLY )
	{
		OnRefreshClassMenu();
	}
#endif

	if ( m_iClassMenuKey != BUTTON_CODE_INVALID && m_iClassMenuKey == code )
	{
		ShowPanel( false );
	}
	else
	{
		BaseClass::OnKeyCodePressed( code );
	}
}

void CDODClassMenu::Update()
{
	C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();

	if ( pPlayer && pPlayer->m_Shared.DesiredPlayerClass() == PLAYERCLASS_UNDEFINED )
	{
		SetVisibleButton( "CancelButton", false );
	}
	else
	{
		SetVisibleButton( "CancelButton", true ); 
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
Panel *CDODClassMenu::CreateControlByName( const char *controlName )
{
	if ( !Q_stricmp( "DODMouseOverPanelButton", controlName ) )
	{
		 return new CDODMouseOverButton<CDODClassInfoPanel>( this, NULL, m_pClassInfoPanel );
	}
	else if( !Q_stricmp( "DODButton", controlName ) )
	{
		return new CDODButton(this);
	}
	else if( !Q_stricmp( "DODRandomButton", controlName ) )
	{
		return new CDODRandomButton<CDODClassInfoPanel>(this, NULL, m_pClassInfoPanel );
	}
	else if ( !Q_stricmp( "ImageButton", controlName ) )
	{
		CImageMouseOverButton<CDODClassInfoPanel> *newButton = new CImageMouseOverButton<CDODClassInfoPanel>( this, NULL, m_pClassInfoPanel );

		if( !m_pInitialButton )
		{
			m_pInitialButton = newButton;
		}

		return newButton;
	}
	else if ( !Q_stricmp( "CIconPanel", controlName ) )
	{
		return new CIconPanel(this, "icon_panel");
	}
	else
	{
		return BaseClass::CreateControlByName( controlName );
	}
}

//-----------------------------------------------------------------------------
// Catch the mouseover event and set the active class
//-----------------------------------------------------------------------------
void CDODClassMenu::OnShowPage( const char *pagename )
{
	// change which class we are counting based on class name

	// turn the button name into a classname

	char buf[64];

	Q_snprintf( buf, sizeof(buf), "cls_%s", pagename );

	C_DODTeam *pTeam = dynamic_cast<C_DODTeam *>( GetGlobalTeam(GetTeamNumber()) );

	if( !pTeam )
		return;

	// Pull the index of this class via IsClassOnTeam
	if ( !pTeam->IsClassOnTeam( buf, m_iActivePlayerClass ) )
	{
		Assert( !"bad class name on class button" );
	}

	UpdateNumClassLabel();
}

//-----------------------------------------------------------------------------
// Draw nothing
//-----------------------------------------------------------------------------
void CDODClassMenu::PaintBackground( void )
{
}

//-----------------------------------------------------------------------------
// Do things that should be done often, eg number of players in the 
// selected class
//-----------------------------------------------------------------------------
void CDODClassMenu::OnTick( void )
{
	//When a player changes teams, their class and team values don't get here 
	//necessarily before the command to update the class menu. This leads to the cancel button 
	//being visible and people cancelling before they have a class. check for class == DOD_CLASS_NONE and if so
	//hide the cancel button

	if ( !IsVisible() )
		return;

	C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();

	if( pPlayer && pPlayer->m_Shared.PlayerClass() == PLAYERCLASS_UNDEFINED )
	{
		SetVisibleButton("CancelButton", false);
	}

	UpdateNumClassLabel();

	BaseClass::OnTick();
}

void CDODClassMenu::UpdateNumClassLabel( void )
{
	int iClassCount[NUM_CLASSES];
	// Initialize to zero. Was previously used uninitialized.
	int iClassLimit[NUM_CLASSES] = {};

	// count how many of this class there are
	C_DODTeam *pTeam = dynamic_cast<C_DODTeam *>( GetGlobalTeam(GetTeamNumber()) );

	if ( !pTeam )
		return;

	char buf[16];

	for( int i=0;i<NUM_CLASSES;i++ )
	{
		iClassCount[i] = pTeam->CountPlayersOfThisClass( i );

		if ( !m_pClassNumLabel[i] || !m_pClassFullLabel[i] )
			continue;

		if ( pTeam->IsClassOnTeam( i ) )
		{
			// FIXME - store pointers to these cvars
			const CDODPlayerClassInfo &pClassInfo = pTeam->GetPlayerClassInfo( i );
			ConVar *pLimitCvar = ( ConVar * )cvar->FindVar( pClassInfo.m_szLimitCvar );

			if ( pLimitCvar )
				iClassLimit[i] = MIN( 32, pLimitCvar->GetInt() );
		}	

		if ( iClassLimit[i] < 0 || iClassCount[i] < iClassLimit[i] )
			m_pClassFullLabel[i]->SetVisible( false );
		else
			m_pClassFullLabel[i]->SetVisible( true );

		if ( iClassLimit[i] > -1 )
		{
			// draw "3 / 4"
			Q_snprintf( buf, sizeof(buf), "%d / %d", iClassCount[i], iClassLimit[i] );
		}
		else
		{
			// just "3"
			Q_snprintf( buf, sizeof(buf), "x %d", iClassCount[i] );
		}

		m_pClassNumLabel[i]->SetText( buf );
	}
}

void CDODClassMenu::SetVisible( bool state )
{
	BaseClass::SetVisible( state );
}

void CDODClassMenu::OnSuicideOptionChanged( vgui::Panel *Panel )
{
	hud_classautokill.SetValue( m_pSuicideOption->IsSelected() );
}

#ifdef REFRESH_CLASSMENU_TOOL

	void CDODClassMenu::OnRefreshClassMenu( void )
	{
		for( int i = 0; i< GetChildCount(); i++ ) 
		{
			CImageMouseOverButton<CDODClassInfoPanel> *button =
				dynamic_cast<CImageMouseOverButton<CDODClassInfoPanel> *>(GetChild(i));

			if ( button )
			{
				button->RefreshClassPage();
			}
		}		
	}

#endif