aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/client/hl2/hud_locator.cpp
blob: 2b8dd880b3c06a2072ad955c61713e0aa5f6182d (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Hud locator element, helps direct the player to objects in the world
//
//=============================================================================//

#include "cbase.h"
#include "hudelement.h"
#include "hud_numericdisplay.h"
#include <vgui_controls/Panel.h>
#include "hud.h"
#include "hud_suitpower.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include "c_basehlplayer.h"

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

#define LOCATOR_MATERIAL_JALOPY			"vgui/icons/icon_jalopy"
#define LOCATOR_MATERIAL_BIG_TICK		"vgui/icons/tick_long"
#define LOCATOR_MATERIAL_SMALL_TICK		"vgui/icons/tick_short"

ConVar hud_locator_alpha( "hud_locator_alpha", "230" );
ConVar hud_locator_fov("hud_locator_fov", "350" );

//-----------------------------------------------------------------------------
// Purpose: Shows positions of objects relative to the player.
//-----------------------------------------------------------------------------
class CHudLocator : public CHudElement, public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CHudLocator, vgui::Panel );

public:
	CHudLocator( const char *pElementName );
	virtual ~CHudLocator( void );

	virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
	void VidInit( void );
	bool ShouldDraw();

protected:
	void FillRect( int x, int y, int w, int h );
	float LocatorXPositionForYawDiff( float yawDiff );
	void DrawGraduations( float flYawPlayerFacing );
	virtual void Paint();

private:
	void Reset( void );

	int m_textureID_IconJalopy;
	int m_textureID_IconBigTick;
	int m_textureID_IconSmallTick;

	Vector			m_vecLocation;
};	

using namespace vgui;

#ifdef HL2_EPISODIC
DECLARE_HUDELEMENT( CHudLocator );
#endif 

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

	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );

	m_textureID_IconJalopy = -1;
	m_textureID_IconSmallTick = -1;
	m_textureID_IconBigTick = -1;
}

CHudLocator::~CHudLocator( void )
{
	if ( vgui::surface() )
	{
		if ( m_textureID_IconJalopy != -1 )
		{
			vgui::surface()->DestroyTextureID( m_textureID_IconJalopy );
			m_textureID_IconJalopy = -1;
		}

		if ( m_textureID_IconSmallTick != -1 )
		{
			vgui::surface()->DestroyTextureID( m_textureID_IconSmallTick );
			m_textureID_IconSmallTick = -1;
		}

		if ( m_textureID_IconBigTick != -1 )
		{
			vgui::surface()->DestroyTextureID( m_textureID_IconBigTick );
			m_textureID_IconBigTick = -1;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pScheme - 
//-----------------------------------------------------------------------------
void CHudLocator::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings(pScheme);
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CHudLocator::VidInit( void )
{
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CHudLocator::ShouldDraw( void )
{
	C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
	if ( !pPlayer )
		return false;

	if( pPlayer->GetVehicle() )
		return false;

	if( pPlayer->m_HL2Local.m_vecLocatorOrigin == vec3_invalid )
		return false;
	
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Start with our background off
//-----------------------------------------------------------------------------
void CHudLocator::Reset( void )
{
	m_vecLocation = Vector( 0, 0, 0 );
}

//-----------------------------------------------------------------------------
// Purpose: Make it a bit more convenient to do a filled rect.
//-----------------------------------------------------------------------------
void CHudLocator::FillRect( int x, int y, int w, int h )
{
	int panel_x, panel_y, panel_w, panel_h;
	GetBounds( panel_x, panel_y, panel_w, panel_h );
	vgui::surface()->DrawFilledRect( x, y, x+w, y+h );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
float CHudLocator::LocatorXPositionForYawDiff( float yawDiff )
{
	float fov = hud_locator_fov.GetFloat() / 2;
	float remappedAngle = RemapVal( yawDiff, -fov, fov, -90, 90 );
	float cosine = sin(DEG2RAD(remappedAngle));
	int element_wide = GetWide();
	
	float position = (element_wide>>1) + ((element_wide>>1) * cosine);

	return position;
}

//-----------------------------------------------------------------------------
// Draw the tickmarks on the locator
//-----------------------------------------------------------------------------
#define NUM_GRADUATIONS	16.0f
void CHudLocator::DrawGraduations( float flYawPlayerFacing )
{
	int icon_wide, icon_tall;
	int xPos, yPos;
	float fov = hud_locator_fov.GetFloat() / 2;

	if( m_textureID_IconBigTick == -1 )
	{
		m_textureID_IconBigTick = vgui::surface()->CreateNewTextureID();
		vgui::surface()->DrawSetTextureFile( m_textureID_IconBigTick, LOCATOR_MATERIAL_BIG_TICK, true, false );
	}

	if( m_textureID_IconSmallTick == -1 )
	{
		m_textureID_IconSmallTick = vgui::surface()->CreateNewTextureID();
		vgui::surface()->DrawSetTextureFile( m_textureID_IconSmallTick, LOCATOR_MATERIAL_SMALL_TICK, true, false );
	}

	int element_tall = GetTall();		// Height of the VGUI element

	surface()->DrawSetColor( 255, 255, 255, 255 );

	// Tick Icons

	float angleStep = 360.0f / NUM_GRADUATIONS;
	bool tallLine = true;

	for( float angle = -180 ; angle <= 180 ; angle += angleStep )
	{
		yPos = (element_tall>>1);

		if( tallLine )
		{
			vgui::surface()->DrawSetTexture( m_textureID_IconBigTick );
			vgui::surface()->DrawGetTextureSize( m_textureID_IconBigTick, icon_wide, icon_tall );
			tallLine = false;
		}
		else
		{
			vgui::surface()->DrawSetTexture( m_textureID_IconSmallTick );
			vgui::surface()->DrawGetTextureSize( m_textureID_IconSmallTick, icon_wide, icon_tall );
			tallLine = true;
		}

		float flDiff = UTIL_AngleDiff( flYawPlayerFacing, angle );

		if( fabs(flDiff) > fov )
			continue;

		float xPosition = LocatorXPositionForYawDiff( flDiff );

		xPos = (int)xPosition;
		xPos -= (icon_wide>>1);

		vgui::surface()->DrawTexturedRect(xPos, yPos, xPos+icon_wide, yPos+icon_tall);
	}
}

//-----------------------------------------------------------------------------
// Purpose: draws the locator icons on the VGUI element.
//-----------------------------------------------------------------------------
void CHudLocator::Paint()
{
#ifdef HL2_EPISODIC

	if( m_textureID_IconJalopy == -1 )
	{
		m_textureID_IconJalopy = vgui::surface()->CreateNewTextureID();
		vgui::surface()->DrawSetTextureFile( m_textureID_IconJalopy, LOCATOR_MATERIAL_JALOPY, true, false );
	}

	int alpha = hud_locator_alpha.GetInt();

	SetAlpha( alpha );

	C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
	if ( !pPlayer )
		return;

	if( pPlayer->m_HL2Local.m_vecLocatorOrigin == vec3_origin )
		return;

	int element_tall = GetTall();		// Height of the VGUI element

	float fov = (hud_locator_fov.GetFloat()) / 2.0f;

	// Compute the relative position of objects we're tracking
	// We'll need the player's yaw for comparison.
	float flYawPlayerForward = pPlayer->EyeAngles().y;

	// Copy this value out of the member variable in case we decide to expand this
	// feature later and want to iterate some kind of list. 
	Vector vecLocation = pPlayer->m_HL2Local.m_vecLocatorOrigin;

	Vector vecToLocation = vecLocation - pPlayer->GetAbsOrigin();
	QAngle locationAngles;

	VectorAngles( vecToLocation, locationAngles );
	float yawDiff = UTIL_AngleDiff( flYawPlayerForward, locationAngles.y );
	bool bObjectInFOV = (yawDiff > -fov && yawDiff < fov);

	// Draw the icons!
	int icon_wide, icon_tall;
	int xPos, yPos;
	surface()->DrawSetColor( 255, 255, 255, 255 );

	DrawGraduations( flYawPlayerForward );
	
	if( bObjectInFOV )
	{
		// The object's location maps to a valid position along the tape, so draw an icon.
		float tapePosition = LocatorXPositionForYawDiff(yawDiff);

		// derive a scale for the locator icon
		yawDiff = fabs(yawDiff);
		float scale = 1.0f;
		scale = RemapValClamped( yawDiff, (fov/4), fov, 1.0f, 0.25f );
	
		vgui::surface()->DrawSetTexture( m_textureID_IconJalopy );
		vgui::surface()->DrawGetTextureSize( m_textureID_IconJalopy, icon_wide, icon_tall );

		float flIconWide = ((float)element_tall * 1.25f);
		float flIconTall = ((float)element_tall * 1.25f);

		// Scale the icon as desired...

		// Put back into ints
		icon_wide = (int)flIconWide;
		icon_tall = (int)flIconTall;

		icon_wide *= scale;

		//Msg("yawDiff:%f  xPos:%d  scale:%f\n", yawDiff, xPos, scale );

		// Center the icon around its position.
		xPos = (int)tapePosition;
		xPos -= (icon_wide >> 1);
		yPos = (element_tall>>1) - (icon_tall >> 1);

		//Msg("Drawing at %f %f\n", x, y );
		vgui::surface()->DrawTexturedRect(xPos, yPos, xPos+icon_wide, yPos+icon_tall);
	}

#endif // HL2_EPISODIC
}