summaryrefslogtreecommitdiff
path: root/game/client/tf2/minimap_trace.cpp
blob: c75afa3d9927e3fcb3cc520caf54fad2197f50f8 (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: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "hud.h"
#include "minimap_trace.h"
#include "c_basetfplayer.h"
#include "mapdata.h"
#include "model_types.h"
#include "clientmode_tfnormal.h"
#include <vgui/IVGui.h>
#include "vgui_bitmapimage.h"
#include <KeyValues.h>
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "shareddefs.h"
#include "engine/ivmodelinfo.h"

//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CMinimapTracePanel::CMinimapTracePanel( vgui::Panel *parent, const char *panelName)
	: BaseClass( parent, "CMinimapTracePanel" )
{
	m_pEntity = NULL;
	SetPaintBackgroundEnabled( false );
	m_bCanScale = true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CMinimapTracePanel::~CMinimapTracePanel()
{
}

//-----------------------------------------------------------------------------
// Initialization
//-----------------------------------------------------------------------------
bool CMinimapTracePanel::Init( KeyValues* pKeyValues, MinimapInitData_t* pInitData )
{
	// NOTE: Can't use a EHANDLE here because the EHANDLE for pEntity is set up
	// when AddEntity is called; this gets called before that happens
	m_pEntity = pInitData->m_pEntity;
	m_vecPosition = pInitData->m_vecPosition;

	int w, h;
	if (!ParseCoord(pKeyValues, "offset", m_OffsetX, m_OffsetY))
		return false;

	if (!ParseCoord(pKeyValues, "size", w, h ))
		return false;

	// Lifetime of the minimap trace
	float flLifeTime = pKeyValues->GetFloat("lifetime", -1.0f);
	if (flLifeTime > 0.0f)
		m_flDeletionTime = gpGlobals->curtime + flLifeTime;
	else
		m_flDeletionTime = -1.0f;

	// Optional parameters
	m_bVisibleWhenZoomedIn = (pKeyValues->GetInt( "detail", 0 ) == 0 );
	m_bClampToMap = (pKeyValues->GetInt( "clamp", 0 ) != 0);
	m_bClipToMap = (pKeyValues->GetInt( "noclip", 0 ) == 0);
	m_bCanScale = (pKeyValues->GetInt( "noscale", 0 ) == 0);
	m_bVisible = true;

	m_SizeW = w;
	m_SizeH = h;

	SetSize( w, h );

	return true;
}


//-----------------------------------------------------------------------------
// Entity accessor
//-----------------------------------------------------------------------------
C_BaseEntity* CMinimapTracePanel::GetEntity()
{ 
	return m_pEntity; 
}

void CMinimapTracePanel::SetEntity( C_BaseEntity* pEntity )
{
	m_pEntity = pEntity;
}

//-----------------------------------------------------------------------------
// Sets the position of the trace in world space
//-----------------------------------------------------------------------------
void CMinimapTracePanel::SetPosition( const Vector &vecPosition )
{
	m_vecPosition = vecPosition;
}


//-----------------------------------------------------------------------------
// Computes the entity position
//-----------------------------------------------------------------------------
bool CMinimapTracePanel::GetEntityPosition( float &x, float &y )
{
	C_BaseEntity *pEntity = GetEntity();

	Vector pos;
	if (!pEntity)
	{
		pos = m_vecPosition;
	}
	else
	{
		if (pEntity == C_BaseTFPlayer::GetLocalPlayer())
		{
			// Use the predicted position...
			pos = pEntity->GetAbsOrigin();
		}
		else if ( !pEntity->GetModel() || modelinfo->GetModelType( pEntity->GetModel() ) != mod_brush )
		{  
			// If it's not a brush model, use the origin
			pos = pEntity->GetRenderOrigin();
		}
		else
		{
			Vector mins, maxs;
			pEntity->GetRenderBounds( mins, maxs );
			pos = (mins + maxs) * 0.5f;
		}

		// Position is an offset when there's an entity
		pos += m_vecPosition;
	}

	return CMinimapPanel::MinimapPanel()->WorldToMinimap( m_bClampToMap ? MINIMAP_CLIP : MINIMAP_NOCLIP, pos, x, y );
}


//-----------------------------------------------------------------------------
// Causes the minimap panel to not be visible
//-----------------------------------------------------------------------------
void CMinimapTracePanel::SetTraceVisibility( bool bVisible )
{
	m_bVisible = bVisible;
}


//-----------------------------------------------------------------------------
// Call this before rendering to see if the entity should be rendered
// and to get the rendering position
//-----------------------------------------------------------------------------
bool CMinimapTracePanel::ComputeVisibility( )
{
	if (!m_bVisible)
		return false;

	C_BaseEntity* pEntity = GetEntity();
	
	// No entity? We must be a positional trace
	if (!pEntity)
		return true;

	// Don't draw if it's not in the PVS
	if ( pEntity->IsDormant() )
		return false;

	// Check visible to tactical
	if( !MapData().IsEntityVisibleToTactical(pEntity) )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// called when we're ticked...
//-----------------------------------------------------------------------------
void CMinimapTracePanel::OnTick()
{
	// Don't do anything when not in a game
	if ( !engine->IsInGame() )
	{
		SetVisible( false );
		return;
	}

	// Update our current position
	float sx, sy;
	bool onMap = GetEntityPosition( sx, sy );

	int ofsx, ofsy;
	ofsx = m_OffsetX;
	ofsy = m_OffsetY;

	if ( m_bCanScale )
	{
		int w, h;
		GetSize( w, h );

		float scale = CMinimapPanel::MinimapPanel()->GetTrueZoom();
		if ( 1 || scale != 1.0f )
		{
			ofsx *= scale;
			ofsy *= scale;

			int sizew = m_SizeW * scale;
			int sizeh = m_SizeH * scale;

			SetPos( (int)(sx + ofsx + 0.5f), (int)(sy + ofsy + 0.5f));
			SetSize( sizew, sizeh );
		}
		else
		{
			SetPos( (int)(sx + ofsx + 0.5f), (int)(sy + ofsy + 0.5f));
		}
	}
	else
	{
		SetPos( (int)(sx + ofsx + 0.5f), (int)(sy + ofsy + 0.5f));
	}

	// Update our visibility
	if (!onMap)
		SetVisible( false );
	else
		SetVisible( ComputeVisibility( ) );

	if ((m_flDeletionTime >= 0.0f) && (gpGlobals->curtime >= m_flDeletionTime))
	{
		MarkForDeletion();
	}
}


//-----------------------------------------------------------------------------
// Computes a panel alpha based on zoom level...
//-----------------------------------------------------------------------------
float CMinimapTracePanel::ComputePanelAlpha()
{
	if (m_bVisibleWhenZoomedIn)
		return 1.0f;

	int a;
	if (!CMinimapPanel::MinimapPanel())
		return 0.0f;

	if (!CMinimapPanel::MinimapPanel()->ShouldDrawZoomDetails( a ))
		return 0.0f;

	return a / 255.0f;
}


//-----------------------------------------------------------------------------
//
// A standard minimap panel that displays a bitmap
//
//-----------------------------------------------------------------------------
DECLARE_MINIMAP_FACTORY( CMinimapTraceBitmapPanel, "minimap_image_panel" );


//-----------------------------------------------------------------------------
// Sets the bitmap and size
//-----------------------------------------------------------------------------
bool CMinimapTraceBitmapPanel::Init( KeyValues* pKeyValues, MinimapInitData_t* pInitData )
{
	if (!BaseClass::Init(pKeyValues, pInitData))
		return false;

	return m_Image.Init( GetVPanel(), pKeyValues );
}


//-----------------------------------------------------------------------------
// Performs the rendering...
//-----------------------------------------------------------------------------
void CMinimapTraceBitmapPanel::Paint(  )
{
	if ( gHUD.IsHidden( HIDEHUD_MISCSTATUS ) )
		return;

	if (!m_bClipToMap)
		g_pMatSystemSurface->DisableClipping( true );
	m_Image.DoPaint( GetVPanel(), 0, ComputePanelAlpha() );
	g_pMatSystemSurface->DisableClipping( false );
}



//-----------------------------------------------------------------------------
//
// A standard minimap renderable that displays a bitmap that changes when team changes
//
//-----------------------------------------------------------------------------
DECLARE_MINIMAP_FACTORY( CMinimapTraceTeamBitmapPanel, "minimap_team_image_panel" );


//-----------------------------------------------------------------------------
// Sets the bitmap and size
//-----------------------------------------------------------------------------
bool CMinimapTraceTeamBitmapPanel::Init( KeyValues* pKeyValues, MinimapInitData_t* pInitData )
{
	if (!BaseClass::Init(pKeyValues, pInitData))
		return false;

	return m_TeamImage.Init( this, pKeyValues, pInitData->m_pEntity );
}


//-----------------------------------------------------------------------------
// Performs the rendering...
//-----------------------------------------------------------------------------
void CMinimapTraceTeamBitmapPanel::Paint( )
{
	if ( gHUD.IsHidden( HIDEHUD_MISCSTATUS ) )
		return;

	if (!m_bClipToMap)
		g_pMatSystemSurface->DisableClipping( true );
	m_TeamImage.SetAlpha( ComputePanelAlpha() );
	m_TeamImage.Paint();
	g_pMatSystemSurface->DisableClipping( false );
}