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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "c_tf_passtime_logic.h"
#include "tf_hud_passtime_ball_offscreen_arrow.h"
#include "iclientmode.h"
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/IVGui.h>
#include "tf_controls.h"
#include "view.h"
#include "ivieweffects.h"
#include "viewrender.h"
#include "prediction.h"
#include "tf_gamerules.h"
#include "c_tf_player.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define PANEL_WIDE (XRES(48))
#define PANEL_TALL (XRES(48))
#define PANEL_ARROW_WIDE (XRES(24))
#define PANEL_ARROW_TALL (XRES(24))
//-----------------------------------------------------------------------------
static const char *GetBallImageForTeam( int iTeam )
{
switch( iTeam )
{
case TF_TEAM_RED: return "../passtime/hud/passtime_ball_offscreen_red";
case TF_TEAM_BLUE: return "../passtime/hud/passtime_ball_offscreen_blue";
default: return "../passtime/hud/passtime_ball";
};
}
//=============================================================================
// CTFHudPasstimeOffscreenArrow
//=============================================================================
//-----------------------------------------------------------------------------
CTFHudPasstimeOffscreenArrow::CTFHudPasstimeOffscreenArrow( Panel *parent, const char *name )
: EditablePanel( parent, name )
, m_pArrowMaterial( 0 )
, m_pImage( 0 )
{
}
//-----------------------------------------------------------------------------
CTFHudPasstimeOffscreenArrow::~CTFHudPasstimeOffscreenArrow()
{
if ( m_pArrowMaterial )
{
m_pArrowMaterial->DecrementReferenceCount();
}
}
//-----------------------------------------------------------------------------
void CTFHudPasstimeOffscreenArrow::ApplySchemeSettings( vgui::IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
LoadControlSettings( "resource/UI/HudPasstimeOffscreenArrow.res" );
SetBounds( 0,0, PANEL_WIDE, PANEL_TALL );
SetVisible( true );
if ( m_pArrowMaterial )
{
m_pArrowMaterial->DecrementReferenceCount();
}
m_pArrowMaterial = materials->FindMaterial( "HUD/medic_arrow", TEXTURE_GROUP_VGUI );
m_pArrowMaterial->IncrementReferenceCount();
m_pImage = FindControl<vgui::ImagePanel>( "Image" );
if ( m_pImage )
{
m_pImage->SetVisible( true );
m_pImage->SetSize( PANEL_WIDE, PANEL_TALL );
m_pImage->SetPos( (GetWide() - m_pImage->GetWide()) * 0.5, (GetTall() - m_pImage->GetTall()) * 0.5 );
}
}
//-----------------------------------------------------------------------------
// TODO what's the difference between Paint and PaintBackground?
extern int HudTransform( const Vector& point, Vector& screen );
void CTFHudPasstimeOffscreenArrow::PaintBackground()
{
// BaseClass::PaintBackground();
if ( !g_pPasstimeLogic )
{
return;
}
//
// Let subclasses determine where to point and how to look
//
C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
C_BaseEntity *pEnt = PreparePaint( m_pImage, pLocalPlayer );
//
// Check if everything is valid, friendly, and visible.
//
{
if ( !pEnt || !pLocalPlayer )
{
SetAlpha( 0 );
return;
}
const bool bTargetVisible = (pEnt->GetEffects() & EF_NODRAW) != 0;
const bool bLocalPlayerTarget = (pEnt == pLocalPlayer);
if ( bTargetVisible || bLocalPlayerTarget )
{
SetAlpha( 0 );
return;
}
}
//
// Determine if the arrow should even be visible
// HudTransform works ok as long as the point is on-screen, otherwise it's useless.
// Calling HudTransform here does a lot of redundant work, but I don't care right now.
//
Vector vecEntPos = pEnt->WorldSpaceCenter();
{
Vector vecActualProjection;
const bool bBehind = HudTransform( vecEntPos, vecActualProjection );
const bool bCenterOfScreen =
(vecActualProjection.x < 1.0f) && (vecActualProjection.x > -1.0f)
&& (vecActualProjection.y < 1.0f) && (vecActualProjection.y > -1.0f);
if ( !bBehind && bCenterOfScreen )
{
SetAlpha( 0 );
return;
}
}
// Definitely visible
//
// Move the target into view space.
// The screen is in the y/z plane.
//
Vector vecLocalTarget;
{
VMatrix mWorldToView( SetupMatrixIdentity() );
const VMatrix mTemp( SetupMatrixOrgAngles( CurrentViewOrigin(), CurrentViewAngles() ) );
MatrixInverseTR( mTemp, mWorldToView );
Vector3DMultiplyPosition( mWorldToView, vecEntPos, vecLocalTarget );
}
//
// Calc the direction in viewspace from the view origin to the target.
// Since all we want is a direction on-screen that goes toward the object, we
// don't need to actually project it. it's transformed to view space to get a direction.
// vecHudDir is always a point on the exact edge of the screen.
//
float flArrowAngle;
{
const Vector vecHudDir = Vector( -vecLocalTarget.y / engine->GetScreenAspectRatio(), -vecLocalTarget.z, 0 ).Normalized();
flArrowAngle = atan2( vecHudDir.y, vecHudDir.x );
// put it in the range 0 to 2PI
if ( flArrowAngle > (M_PI_F * 2.0f) )
flArrowAngle -= (M_PI_F * 2.0f);
else if ( flArrowAngle < 0 )
flArrowAngle += (M_PI_F * 2.0f);
}
//
// Build the mesh for the arrow, because vgui can't rotate
// TODO probably don't need to build this mesh every frame
// The code that draws the arrow mesh has to use the current vgui
// position or there will be a one-frame position lag between the two.
//
{
int iX = PANEL_ARROW_WIDE / 2;
int iY = PANEL_ARROW_TALL / 2;
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->Bind( m_pArrowMaterial );
IMesh* pMesh = pRenderContext->GetDynamicMesh( true );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
meshBuilder.Position3f( -iX, -iY, 0 );
meshBuilder.TexCoord2f( 0, 0, 0 );
meshBuilder.Color4ub( 255, 255, 255, GetAlpha() );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( iX, -iY, 0 );
meshBuilder.TexCoord2f( 0, 1, 0 );
meshBuilder.Color4ub( 255, 255, 255, GetAlpha() );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( iX, iY, 0 );
meshBuilder.TexCoord2f( 0, 1, 1 );
meshBuilder.Color4ub( 255, 255, 255, GetAlpha() );
meshBuilder.AdvanceVertex();
meshBuilder.Position3f( -iX, iY, 0 );
meshBuilder.TexCoord2f( 0, 0, 1 );
meshBuilder.Color4ub( 255, 255, 255, GetAlpha() );
meshBuilder.AdvanceVertex();
meshBuilder.End();
// Determine a scale factor based on how close the player is to the ball. Scale is between 1-2.
Vector vecPlayerPos = pLocalPlayer->GetNetworkOrigin();
float flDistanceToTargetSq = (float)(vecEntPos.DistToSqr( vecPlayerPos ));
float flMaxDistanceSq = 1000000.0f;
flDistanceToTargetSq = Min( flDistanceToTargetSq, flMaxDistanceSq );
float flDistanceScale = (flDistanceToTargetSq / flMaxDistanceSq) + 1.0f;
GetPos( iX, iY );
iX += GetWide() / 2;
iY += GetTall() / 2;
pRenderContext->PushMatrix();
pRenderContext->Translate( iX, iY, 0 );
pRenderContext->Rotate( RAD2DEG( flArrowAngle ), 0, 0, 1 );
pRenderContext->Scale( flDistanceScale, 1.0f, 1.0f );
pRenderContext->Translate( GetWide() / 2, 0, 0 );
pMesh->Draw();
pRenderContext->PopMatrix();
}
//
// Given the angle, calc a position on the screen for the indicator.
// This finds the point at which vecHudDir intersects with the edges of the screen.
// Roughly.
//
Vector vecHudPoint;
{
Assert( flArrowAngle >= 0 && flArrowAngle < (M_PI_F * 2.0f) );
vecHudPoint.y = tanf( flArrowAngle );
if ( fabsf( vecHudPoint.y ) <= 1 )
{
// point will be either along the left or right edge.
// y coord is between top and bottom edges, decide whether it lies on
// the left or right side of the screen.
const float fHalfPi = M_PI_F / 2.0f;
if ( (flArrowAngle < fHalfPi) || (flArrowAngle > (M_PI_F + fHalfPi)) )
vecHudPoint.Init( 1, vecHudPoint.y );
else
vecHudPoint.Init( -1, -vecHudPoint.y );
}
else
{
// point will be either along the top or bottom edge.
// y coord is out of bounds, clamp it and compute x coord
if ( flArrowAngle < M_PI_F )
vecHudPoint.Init( (1 / vecHudPoint.y), 1 );
else
vecHudPoint.Init( (-1 / vecHudPoint.y), -1 );
}
vecHudPoint.x *= 0.8f; // bring toward center.
vecHudPoint.y *= 0.5f;
}
//
// Convert to hud coordinates and reposition the vgui control
//
{
const int iX = (0.5f * ( 1.0f + vecHudPoint.x ) * ScreenWidth()) - (GetWide() / 2);
const int iY = (0.5f * ( 1.0f + vecHudPoint.y ) * ScreenHeight()) - (GetTall() / 2);
#ifdef DEBUG
// This block avoids calling SetPos with invalid values while debugging,
// because otherwise Paint will never be called again.
const int min = 8;
const int maxx = ScreenWidth() - 8;
const int maxy = ScreenHeight() - 8;
if ( iX > min && iX < maxx && iY > min && iY < maxy )
#endif
SetPos( iX, iY );
}
}
//=============================================================================
// CTFHudPasstimeBallOffscreenArrow
//=============================================================================
//-----------------------------------------------------------------------------
CTFHudPasstimeBallOffscreenArrow::CTFHudPasstimeBallOffscreenArrow( vgui::Panel *pParent )
: BaseClass( pParent, "PasstimeBallOffscreenArrow" )
{
}
//-----------------------------------------------------------------------------
C_BaseEntity *CTFHudPasstimeBallOffscreenArrow::PreparePaint(
vgui::ImagePanel *pImage, C_TFPlayer *pLocalPlayer )
{
if ( !g_pPasstimeLogic )
{
return NULL;
}
C_PasstimeBall *pBall = g_pPasstimeLogic->GetBall();
C_BaseEntity *pTarget = 0;
bool bHomingActive = false;
bool bHaveTarget = g_pPasstimeLogic->GetBallReticleTarget( &pTarget, &bHomingActive );
if ( !pImage || !pBall || !pLocalPlayer || !bHaveTarget )
{
return NULL;
}
if ( pLocalPlayer->m_Shared.IsTargetedForPasstimePass() || bHomingActive )
{
SetAlpha( (int)( (fmodf( gpGlobals->curtime * 3.0f, 1.0f )) * 255 ) );
}
else
{
SetAlpha( 128 );
}
if ( pImage )
{
// setimage will ignore redundant calls
if ( pTarget )
{
pImage->SetImage( GetBallImageForTeam( pTarget->GetTeamNumber() ) );
}
else
{
pImage->SetImage( "../passtime/hud/passtime_ball" );
}
}
return pTarget;
}
//=============================================================================
// CTFHudPasstimePlayerOffscreenArrow
//=============================================================================
//-----------------------------------------------------------------------------
CTFHudPasstimePlayerOffscreenArrow::CTFHudPasstimePlayerOffscreenArrow( vgui::Panel *pParent, int iPlayerIndex )
: BaseClass( pParent, "PasstimePlayerOffscreenArrow" )
, m_iPlayerIndex( iPlayerIndex )
{
}
//-----------------------------------------------------------------------------
C_BaseEntity *CTFHudPasstimePlayerOffscreenArrow::PreparePaint(
vgui::ImagePanel *pImage, C_TFPlayer *pLocalPlayer )
{
if ( !pImage || (!pLocalPlayer->m_Shared.HasPasstimeBall() && !pLocalPlayer->IsObserver()) )
{
return NULL;
}
C_TFPlayer *pPlayer = ToTFPlayer( UTIL_PlayerByIndex( m_iPlayerIndex ) );
if ( !pPlayer || (pPlayer == pLocalPlayer) || (pPlayer->m_Shared.AskForBallTime() < gpGlobals->curtime) )
{
return NULL;
}
SetAlpha( 128 );
pImage->SetImage( "../passtime/hud/passtime_pass_to_me_prompt" );
return pPlayer;
}
|