summaryrefslogtreecommitdiff
path: root/game/client/tf2/VGuiScreenVehicleBay.cpp
blob: cf8cce941e60622a1a15e6dcd3d2c2ab0954d2c3 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "c_vguiscreen.h"
#include "clientmode_tfbase.h"
#include <vgui/IVGui.h>
#include <vgui_controls/Controls.h>
#include <vgui_controls/Label.h>
#include "vgui_bitmapbutton.h"
#include "c_info_act.h"
#include "tf_shareddefs.h"
#include "c_basetfplayer.h"

int g_ValidVehicles[] =
{
	OBJ_WAGON,
	OBJ_BATTERING_RAM,
	OBJ_VEHICLE_TANK,
	OBJ_VEHICLE_TELEPORT_STATION,
	OBJ_WALKER_STRIDER,
	OBJ_WALKER_MINI_STRIDER

	// If you add a new vehicle here, you have to add a button for it to the screen_vehicle_bay.res file.
	// The button's name must be VehicleButton%d, where %d is it's index into this array.
	// Then add the build%d command to OnCommand at the bottom of this file.
};

#define NUM_VEHICLES	ARRAYSIZE(g_ValidVehicles)

//-----------------------------------------------------------------------------
// Vgui screen handling vehicle selection in vehicle bays
//-----------------------------------------------------------------------------
class CVehicleBayVGuiScreen : public CVGuiScreenPanel
{
	DECLARE_CLASS( CVehicleBayVGuiScreen, CVGuiScreenPanel );

public:
	CVehicleBayVGuiScreen( vgui::Panel *parent, const char *panelName );

	virtual bool Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData );
	virtual void OnTick();
	virtual void OnCommand( const char *command );

private:
	vgui::Button *m_pVehicleButtons[ NUM_VEHICLES ];
};


//-----------------------------------------------------------------------------
// Standard VGUI panel for objects 
//-----------------------------------------------------------------------------
DECLARE_VGUI_SCREEN_FACTORY( CVehicleBayVGuiScreen, "vehicle_bay_screen" );


//-----------------------------------------------------------------------------
// Constructor: 
//-----------------------------------------------------------------------------
CVehicleBayVGuiScreen::CVehicleBayVGuiScreen( vgui::Panel *parent, const char *panelName )
	: BaseClass( parent, panelName, g_hVGuiObjectScheme ) 
{
}


//-----------------------------------------------------------------------------
// Initialization 
//-----------------------------------------------------------------------------
bool CVehicleBayVGuiScreen::Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData )
{
	// Create our vehicle buttons
	for ( int i = 0; i < NUM_VEHICLES; i++ )
	{
		char ch[128];
		Q_snprintf( ch, sizeof(ch), "VehicleButton%d", i );
		m_pVehicleButtons[i] = new CBitmapButton( this, ch, "Name" );
	}

	// Load all of the controls in
	if (!BaseClass::Init(pKeyValues, pInitData))
		return false;

	// Make sure we get ticked...
	vgui::ivgui()->AddTickSignal( GetVPanel() );

	return true;
}

//-----------------------------------------------------------------------------
// Frame-based update
//-----------------------------------------------------------------------------
void CVehicleBayVGuiScreen::OnTick()
{
	BaseClass::OnTick();

	if (!GetEntity())
		return;

	C_BaseTFPlayer *pLocalPlayer = C_BaseTFPlayer::GetLocalPlayer();
	if ( !pLocalPlayer )
		return;

	int nBankResources = pLocalPlayer ? pLocalPlayer->GetBankResources() : 0;

	// Set the vehicles costs
	for ( int i = 0; i < NUM_VEHICLES; i++ )
	{
		if ( !m_pVehicleButtons[i] )
			continue;

		char buf[128];
		int iCost = CalculateObjectCost( g_ValidVehicles[i], pLocalPlayer->GetNumObjects( g_ValidVehicles[i] ), pLocalPlayer->GetTeamNumber() );
		Q_snprintf( buf, sizeof( buf ), "%s : %d", GetObjectInfo( g_ValidVehicles[i] )->m_pStatusName, iCost );
		m_pVehicleButtons[i]->SetText( buf );
		// Can't build if the game hasn't started
		if ( CurrentActIsAWaitingAct() )
		{
			m_pVehicleButtons[i]->SetEnabled( false );
		}
		else
		{
			m_pVehicleButtons[i]->SetEnabled( nBankResources >= iCost );
		}
	}
}

//-----------------------------------------------------------------------------
// Button click handlers
//-----------------------------------------------------------------------------
void CVehicleBayVGuiScreen::OnCommand( const char *command )
{
	if (!Q_strnicmp(command, "build", 5))
	{
		int iButton;
		int nCount = sscanf( command, "build%d", &iButton );
		if (nCount == 1)
		{
			char szbuf[64];
			Q_snprintf( szbuf, sizeof( szbuf ), "buildvehicle %d %d", GetEntity()->entindex(), g_ValidVehicles[iButton] );
  			engine->ClientCmd(szbuf);
			return;
		}
	}

	BaseClass::OnCommand(command);
}