summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_hud_inspectpanel.cpp
blob: 404142b8285b2b88c4c285b5d1179b55884331cb (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "c_tf_player.h"
#include "iclientmode.h"
#include "ienginevgui.h"
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/IVGui.h>
#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/ProgressBar.h>
#include "tf_spectatorgui.h"
#include "hud_macros.h"
#include "c_playerresource.h"
#include "view.h"
#include "player_vs_environment/c_tf_upgrades.h"
#include "tf_hud_inspectpanel.h"

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


DECLARE_HUDELEMENT( CHudInspectPanel );

static float s_flLastInspectDownTime = 0.f;

void InspectDown()
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( pLocalPlayer == NULL )
	{
		return;
	}

	s_flLastInspectDownTime = gpGlobals->curtime;

	KeyValues *kv = new KeyValues( "+inspect_server" );
	engine->ServerCmdKeyValues( kv );

	pLocalPlayer->SetInspectTime( gpGlobals->curtime );
}
static ConCommand s_inspect_down_cmd( "+inspect", InspectDown, "", FCVAR_SERVER_CAN_EXECUTE );

void InspectUp()
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( pLocalPlayer == NULL )
	{
		return;
	}

	// quick tap on the inspect button, try to inspect players
	if ( gpGlobals->curtime - s_flLastInspectDownTime <= 0.2f )
	{
		CHudElement *pElement = gHUD.FindElement( "CHudInspectPanel" );
		if ( pElement )
		{
			((CHudInspectPanel *)pElement)->UserCmd_InspectTarget();
		}
	}

	KeyValues *kv = new KeyValues( "-inspect_server" );
	engine->ServerCmdKeyValues( kv );

	pLocalPlayer->SetInspectTime( 0.f );
}
static ConCommand s_inspect_up_cmd( "-inspect", InspectUp, "", FCVAR_SERVER_CAN_EXECUTE );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CHudInspectPanel::CHudInspectPanel( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudInspectPanel" )
{
	Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( HIDEHUD_MISCSTATUS );

	m_pItemPanel = new CItemModelPanel( this, "itempanel" ) ;
	m_iTargetItemIterator = 0;

	RegisterForRenderGroup( "mid" );
	RegisterForRenderGroup( "commentary" );
	RegisterForRenderGroup( "arena_target_id" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInspectPanel::ApplySchemeSettings( IScheme *pScheme )
{
	// load control settings...
	LoadControlSettings( "resource/UI/HudInspectPanel.res" );

	BaseClass::ApplySchemeSettings( pScheme );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInspectPanel::LockInspectRenderGroup( bool bLock )
{
	int iIndex = gHUD.LookupRenderGroupIndexByName( "inspect_panel" );
	if ( bLock )
	{
		gHUD.LockRenderGroup( iIndex );
	}
	else
	{
		gHUD.UnlockRenderGroup( iIndex );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CHudInspectPanel::ShouldDraw( void )
{
	C_TFPlayer *pLocalTFPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !CHudElement::ShouldDraw() || !pLocalTFPlayer || !pLocalTFPlayer->IsAlive() )
	{
		m_hTarget = NULL;
		SetPanelVisible( false );
		return false;
	}

	return ( m_hTarget != NULL );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInspectPanel::UserCmd_InspectTarget( void )
{
	// If we're in observer mode, we cycle items on the current observer target
	C_TFPlayer *pLocalTFPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pLocalTFPlayer )
		return;

	C_TFPlayer *pTargetPlayer = GetInspectTarget( pLocalTFPlayer );

	bool bVisible = false;
	if ( pLocalTFPlayer->IsObserver() )
	{
		CTFSpectatorGUI *pPanel = (CTFSpectatorGUI*)gViewPortInterface->FindPanelByName( PANEL_SPECGUI );
		if ( pPanel )
		{
			pPanel->ForceItemPanelCycle();
		}
	}
	// In MvM, display player upgrades
	else if ( TFGameRules() && TFGameRules()->IsMannVsMachineMode() )
	{
		CHudUpgradePanel *pUpgradePanel = GET_HUDELEMENT( CHudUpgradePanel );
		if ( pUpgradePanel )
		{
			// Close if open
			if ( pUpgradePanel->IsVisible() )
			{
				pUpgradePanel->OnCommand( "cancel" );
			}
			// Inspect a player
			else if ( pTargetPlayer && ( pTargetPlayer->GetTeamNumber() != TF_TEAM_PVE_INVADERS ) )
			{
				pUpgradePanel->InspectUpgradesForPlayer( pTargetPlayer );
			}
			// Inspect self
			else if ( !pTargetPlayer && pLocalTFPlayer  )
			{
				pUpgradePanel->InspectUpgradesForPlayer( pLocalTFPlayer );
			}
		}
	}
	else
	{
		if ( pTargetPlayer && !pTargetPlayer->IsEnemyPlayer() )
		{
			m_hTarget = pTargetPlayer;

			CEconItemView *pItem = m_hTarget->GetInspectItem( &m_iTargetItemIterator );
			if ( pItem && pItem->IsValid() )
			{
				m_pItemPanel->SetDialogVariable( "killername", g_PR->GetPlayerName( m_hTarget->entindex() ) );
				m_pItemPanel->SetItem( pItem );

				// force update description to get the correct panel size
				m_pItemPanel->UpdateDescription();
				bVisible = true;
				int x,y;
				GetPos( x, y );
				SetPos( x, ScreenHeight() - YRES( 12 ) - m_pItemPanel->GetTall() );
			}
		}
		else
		{
			m_iTargetItemIterator = 0;
		}
	}

	SetPanelVisible( bVisible );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_TFPlayer *CHudInspectPanel::GetInspectTarget( C_TFPlayer *pLocalTFPlayer )
{
	if ( !pLocalTFPlayer )
		return NULL;

	C_TFPlayer *pTargetPlayer = NULL;

	trace_t tr;
	Vector vecStart, vecEnd;
	VectorMA( MainViewOrigin(), MAX_TRACE_LENGTH, MainViewForward(), vecEnd );
	VectorMA( MainViewOrigin(), 10,   MainViewForward(), vecStart );
	UTIL_TraceLine( vecStart, vecEnd, MASK_SOLID, pLocalTFPlayer, COLLISION_GROUP_NONE, &tr );

	if ( !tr.startsolid && tr.DidHitNonWorldEntity() )
	{
		C_BaseEntity *pEntity = tr.m_pEnt;
		if ( pEntity && ( pEntity != pLocalTFPlayer ) && pEntity->IsPlayer() )
		{
			// Get the player under our crosshair
			pTargetPlayer = ToTFPlayer( pEntity );

			// Fix up if it's a spy disguised as my team
			if ( pLocalTFPlayer->m_Shared.IsSpyDisguisedAsMyTeam( pTargetPlayer ) )
			{
				// Get the player that the spy is disguised as
				C_TFPlayer *pDisguiseTarget = ToTFPlayer( pTargetPlayer->m_Shared.GetDisguiseTarget().Get() );
				if ( pDisguiseTarget && pDisguiseTarget->GetPlayerClass()->GetClassIndex() == pTargetPlayer->m_Shared.GetDisguiseClass() )
				{
					// The spy is disguised as the same class as the target, so inspect the disguise target instead
					pTargetPlayer = pDisguiseTarget;
				}
			}
		}
	}

	return pTargetPlayer;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInspectPanel::SetPanelVisible( bool bVisible )
{
	if ( m_pItemPanel->IsVisible() != bVisible )
	{
		LockInspectRenderGroup( bVisible );
		m_pItemPanel->SetVisible( bVisible );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int	CHudInspectPanel::HudElementKeyInput( int down, ButtonCode_t keynum, const char *pszCurrentBinding )
{
	if ( IsVisible() && pszCurrentBinding && pszCurrentBinding[0] )
	{
		if ( FStrEq( pszCurrentBinding, "+attack" ) )
		{
			CBasePlayer *pLocalPlayer = CBasePlayer::GetLocalPlayer();
			if ( pLocalPlayer && ( pLocalPlayer->GetTeamNumber() >= FIRST_GAME_TEAM ) && pLocalPlayer->IsAlive() )
			{
				SetPanelVisible( false );
			}
		}
	}

	return 1; // intentionally not handling the key
}