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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: A panel that display particle systems
//
//=============================================================================//
#include "cbase.h"
#include <KeyValues.h>
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui_controls/EditablePanel.h>
#include "vgui/IVGui.h"
#include "tf_particlepanel.h"
#include "matsys_controls/matsyscontrols.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "tier2/renderutils.h"
#include "renderparm.h"
using namespace vgui;
CTFParticlePanel::ParticleEffect_t::ParticleEffect_t()
: m_bLoop( true )
, m_pParticleSystem( NULL )
, m_flLastTime( FLT_MAX )
, m_ParticleSystemName( NULL )
, m_bStartActivated( true )
, m_flScale( 1.f )
, m_flEndTime( FLT_MAX )
, m_nXPos( 0 )
, m_nYPos( 0 )
, m_Angles( 0.f, 0.f, 0.f )
, m_pParent( NULL )
, m_bForceStopped( false )
, m_bAutoDelete( false )
, m_bStarted( false )
{}
DECLARE_BUILD_FACTORY( CTFParticlePanel );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CTFParticlePanel::CTFParticlePanel( vgui::Panel *pParent, const char *pName )
: BaseClass( pParent, pName )
{
m_Camera.m_flZNear = 3.0f;
m_Camera.m_flZFar = 16384.0f * 1.73205080757f;
m_Camera.m_flFOV = 30.0f;
m_Camera.m_origin = Vector(0,0,0);
m_Camera.m_angles = QAngle(0,0,0);
m_pLightmapTexture.Init( "//platform/materials/debug/defaultlightmap", "editor" );
m_DefaultEnvCubemap.Init( "editor/cubemap", "editor", true );
}
CTFParticlePanel::~CTFParticlePanel()
{
m_pLightmapTexture.Shutdown();
m_DefaultEnvCubemap.Shutdown();
m_vecParticleEffects.PurgeAndDeleteElements();
}
void CTFParticlePanel::ApplySettings( KeyValues *inResourceData )
{
BaseClass::ApplySettings( inResourceData );
KeyValues *pKVParticleEffects = inResourceData->FindKey( "ParticleEffects" );
if ( pKVParticleEffects )
{
FOR_EACH_SUBKEY( pKVParticleEffects, pKVEffect )
{
m_vecParticleEffects[ m_vecParticleEffects.AddToTail() ] = new ParticleEffect_t();
ParticleEffect_t* pEffect = m_vecParticleEffects.Tail();
// get the position
int alignScreenWide = GetWide(), alignScreenTall = GetTall(); // screen dimensions used for pinning in splitscreen
int x, y;
GetPos(x, y);
const char *xstr = pKVEffect->GetString( "particle_xpos", NULL );
const char *ystr = pKVEffect->GetString( "particle_ypos", NULL );
if (xstr)
{
bool bRightAlign = false;
bool bCenterAlign = false;
// look for alignment flags
if (xstr[0] == 'r' || xstr[0] == 'R')
{
bRightAlign = true;
xstr++;
}
else if (xstr[0] == 'c' || xstr[0] == 'C')
{
bCenterAlign = true;
xstr++;
}
// get the value
x = atoi(xstr);
// scale the x up to our screen co-ords
if ( IsProportional() )
{
x = scheme()->GetProportionalScaledValueEx(GetScheme(), x);
}
// now correct the alignment
if ( bRightAlign )
{
x = alignScreenWide - x;
}
else if ( bCenterAlign )
{
x = (alignScreenWide / 2) + x;
}
}
if (ystr)
{
bool bBottomAlign = false;
bool bCenterAlign = false;
// look for alignment flags
if (ystr[0] == 'r' || ystr[0] == 'R')
{
bBottomAlign = true;
ystr++;
}
else if (ystr[0] == 'c' || ystr[0] == 'C')
{
bCenterAlign = true;
ystr++;
}
y = atoi(ystr);
if (IsProportional())
{
// scale the y up to our screen co-ords
y = scheme()->GetProportionalScaledValueEx(GetScheme(), y);
}
// now correct the alignment
if ( bBottomAlign )
{
y = alignScreenTall - y;
}
else if ( bCenterAlign )
{
y = (alignScreenTall / 2) + y;
}
}
pEffect->m_nXPos = x;
pEffect->m_nYPos = y;
pEffect->m_flScale = pKVEffect->GetFloat( "particle_scale", 1.f );
// Scale the scale factor the same way we do the XY position coordinates
if( IsProportional() )
{
int wide, tall;
surface()->GetScreenSize( wide, tall );
int proH, proW;
surface()->GetProportionalBase( proW, proH );
double scale = (double)tall / (double)proH;
pEffect->m_flScale *= scale;
}
pEffect->m_pParent = this;
pEffect->m_bLoop = pKVEffect->GetBool( "loop", true );
pEffect->m_bStartActivated = pKVEffect->GetBool( "start_activated", true );
pEffect->SetParticleSystem( pKVEffect->GetString( "particleName" ) );
// Read angles for the particle system
{
float x1,y1,z1;
const char* pszAngles = pKVEffect->GetString( "angles" );
if( *pszAngles )
{
if( pEffect->m_pParticleSystem && sscanf( pszAngles, "%f %f %f", &x1, &y1, &z1 ) == 3 )
{
pEffect->m_Angles = QAngle( x1, y1, z1 );
Quaternion q;
AngleQuaternion( pEffect->m_Angles , q );
pEffect->m_pParticleSystem->SetControlPointOrientation( 0, q );
}
}
}
pEffect->SetControlPointValue( 0, Vector(0,0,0) );
// Read all control point values
const char* pszControlPoint = NULL;
int nControlPointNumber = 0;
do
{
pszControlPoint = pKVEffect->GetString( VarArgs("control_point%d", nControlPointNumber), "" );
if ( *pszControlPoint )
{
float x2,y2,z2;
if (sscanf(pszControlPoint, "%f %f %f", &x2, &y2, &z2 ) == 3)
{
pEffect->SetControlPointValue( nControlPointNumber, Vector( x2, y2, z2 ) );
}
}
++nControlPointNumber;
}
while( *pszControlPoint );
}
}
}
//-----------------------------------------------------------------------------
// Scheme
//-----------------------------------------------------------------------------
void CTFParticlePanel::ApplySchemeSettings( vgui::IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
SetMouseInputEnabled( false );
SetKeyBoardInputEnabled( false );
}
void CTFParticlePanel::OnCommand( const char *command )
{
if ( !Q_strnicmp( command, "start", ARRAYSIZE("start") - 1 ) )
{
// Check if they specified a particular one to turn on
const char* pszNum = command + ARRAYSIZE( "start" ) - 1;
int nIndex = m_vecParticleEffects.InvalidIndex();
if( pszNum && pszNum[0] )
{
nIndex = atoi(pszNum);
Assert( nIndex >= 0 && nIndex < m_vecParticleEffects.Count() );
}
FOR_EACH_VEC( m_vecParticleEffects, i )
{
if ( nIndex != m_vecParticleEffects.InvalidIndex() && i != nIndex )
continue;
ParticleEffect_t* pEffect = m_vecParticleEffects[ i ];
if( !pEffect->m_pParticleSystem )
{
pEffect->SetParticleSystem( pEffect->m_ParticleSystemName );
}
if( !pEffect->m_pParticleSystem )
continue;
pEffect->StartupParticleCollection();
pEffect->m_pParticleSystem->StartEmission();
pEffect->m_bForceStopped = false;
}
}
else if ( !Q_strnicmp( command, "stop", ARRAYSIZE("stop") - 1 ) )
{
// Check if they specified a specific one to turn off
const char* pszNum = command + ARRAYSIZE( "start" ) - 1;
if( pszNum && pszNum[0] )
{
int iIndex = atoi(pszNum);
Assert( iIndex >= 0 && iIndex < m_vecParticleEffects.Count() );
ParticleEffect_t* pEffect = m_vecParticleEffects[iIndex];
if( pEffect->m_pParticleSystem )
{
pEffect->m_pParticleSystem->StopEmission();
pEffect->m_bForceStopped = true;
}
}
else
{
// Turn them ALL off
FOR_EACH_VEC( m_vecParticleEffects, i )
{
ParticleEffect_t* pEffect = m_vecParticleEffects[ i ];
if( pEffect->m_pParticleSystem )
{
pEffect->m_pParticleSystem->StopEmission();
pEffect->m_bForceStopped = true;
}
}
}
}
}
void CTFParticlePanel::FireParticleEffect( const char *pszName, int xPos, int yPos, float flScale, bool bLoop, float flEndTime )
{
m_vecParticleEffects[ m_vecParticleEffects.AddToTail() ] = new ParticleEffect_t();
ParticleEffect_t* pEffect = m_vecParticleEffects.Tail();
int iParentAbsX, iParentAbsY;
vgui::ipanel()->GetAbsPos( GetParent()->GetVPanel(), iParentAbsX, iParentAbsY );
pEffect->m_pParent = this;
pEffect->m_nXPos = xPos - iParentAbsX;
pEffect->m_nYPos = yPos - iParentAbsY;
pEffect->m_flScale = flScale;
pEffect->m_bLoop = bLoop;
pEffect->m_bAutoDelete = true; // This will get automatically deleted once it stops
pEffect->m_bStartActivated = true;
pEffect->m_flEndTime = gpGlobals->curtime + flEndTime;
if( IsProportional() )
{
int wide, tall;
surface()->GetScreenSize( wide, tall );
int proH, proW;
surface()->GetProportionalBase( proW, proH );
double scale = (double)tall / (double)proH;
pEffect->m_flScale *= scale;
}
for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
{
pEffect->SetControlPointValue( i, Vector( 0, 0, 10.0f * i ) );
}
pEffect->SetParticleSystem( pszName );
}
static bool IsValidHierarchy( CParticleCollection *pCollection )
{
if ( !pCollection->IsValid() )
return false;
for( CParticleCollection *pChild = pCollection->m_Children.m_pHead; pChild; pChild = pChild->m_pNext )
{
if ( !IsValidHierarchy( pChild ) )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Simulate the particle system
//-----------------------------------------------------------------------------
void CTFParticlePanel::OnTick()
{
BaseClass::OnTick();
float flTime = engine->Time();
bool bAnyActive = false;
// Update all particles
FOR_EACH_VEC_BACK( m_vecParticleEffects, i )
{
bAnyActive |= m_vecParticleEffects[i]->Update( flTime );
// If this effect is done and should auto-delete, then now is when we delete
if( m_vecParticleEffects[i]->m_pParticleSystem == NULL && m_vecParticleEffects[i]->m_bAutoDelete )
{
delete m_vecParticleEffects[i];
m_vecParticleEffects.FastRemove( i );
}
}
if ( !bAnyActive )
{
vgui::ivgui()->RemoveTickSignal( GetVPanel() );
}
}
void CTFParticlePanel::Paint()
{
// This needs calling to reset various counters.
g_pParticleSystemMgr->SetLastSimulationTime( gpGlobals->curtime );
// No particles? Do nothing.
if( m_vecParticleEffects.Count() == 0 )
return;
int screenW, screenH;
vgui::surface()->GetScreenSize( screenW, screenH );
vgui::MatSystemSurface()->Begin3DPaint( 0, 0, screenW, screenH, false );
VMatrix view, projection;
ComputeViewMatrix( &view, m_Camera );
ComputeProjectionMatrix( &projection, m_Camera, screenW, screenH );
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->CullMode( MATERIAL_CULLMODE_CCW );
pRenderContext->SetIntRenderingParameter( INT_RENDERPARM_WRITE_DEPTH_TO_DESTALPHA, false );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadIdentity( );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadMatrix( view );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->LoadMatrix( projection );
int iXOffset, iYOffset;
vgui::ipanel()->GetAbsPos( GetVPanel(), iXOffset, iYOffset );
if ( iXOffset > 0 )
iXOffset = 0;
if ( iYOffset > 0 )
iYOffset = 0;
float flXScale = 1.f;
if ( GetWide() > screenW )
flXScale = (float)screenW / GetWide();
float flYScale = 1.f;
if ( GetTall() > screenH )
flYScale = (float)screenH / GetTall();
FOR_EACH_VEC( m_vecParticleEffects, i )
{
m_vecParticleEffects[i]->Paint( pRenderContext, iXOffset, iYOffset, flXScale, flYScale, screenW, screenH );
}
pRenderContext->CullMode( MATERIAL_CULLMODE_CW );
vgui::MatSystemSurface()->End3DPaint();
}
bool CTFParticlePanel::ParticleEffect_t::Update( float flTime )
{
if ( !m_pParticleSystem || !m_bStarted )
return false;
if ( m_flLastTime == FLT_MAX )
{
m_flLastTime = flTime;
}
float flDt = flTime - m_flLastTime;
m_flLastTime = flTime;
Quaternion q;
AngleQuaternion( m_Angles, q );
for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
{
if ( !m_pParticleSystem->ReadsControlPoint( i ) )
continue;
m_pParticleSystem->SetControlPoint( i, m_pControlPointValue[i] );
m_pParticleSystem->SetControlPointOrientation( i, q );
m_pParticleSystem->SetControlPointParent( i, i );
}
// Restart the particle system if it's finished
bool bIsInvalid = !IsValidHierarchy( m_pParticleSystem );
if ( !bIsInvalid )
{
m_pParticleSystem->Simulate( flDt, false );
}
// Past our end time?
bool bEnd = gpGlobals->curtime >= m_flEndTime;
if ( m_pParticleSystem->IsFinished() || bIsInvalid || bEnd )
{
delete m_pParticleSystem;
m_pParticleSystem = NULL;
// Loop if we're supposed to
if ( m_bLoop && m_ParticleSystemName.Length() && !m_bForceStopped )
{
m_pParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemName );
}
if ( bIsInvalid && m_pParent )
{
m_pParent->PostActionSignal( new KeyValues( "ParticleSystemReconstructed" ) );
}
m_flLastTime = FLT_MAX;
}
return m_pParticleSystem != NULL;
}
//-----------------------------------------------------------------------------
// Startup, shutdown particle collection
//-----------------------------------------------------------------------------
void CTFParticlePanel::ParticleEffect_t::StartupParticleCollection()
{
if ( m_pParticleSystem && m_pParent )
{
vgui::ivgui()->AddTickSignal( m_pParent->GetVPanel(), 0 );
}
m_flLastTime = FLT_MAX;
m_bStarted = true;
}
void CTFParticlePanel::ParticleEffect_t::ShutdownParticleCollection()
{
if ( m_pParticleSystem && m_pParent )
{
delete m_pParticleSystem;
m_pParticleSystem = NULL;
}
m_bStarted = false;
}
//-----------------------------------------------------------------------------
// Set the particle system to draw
//-----------------------------------------------------------------------------
void CTFParticlePanel::ParticleEffect_t::SetParticleSystem( const char* pszParticleSystemName )
{
ShutdownParticleCollection();
if( !g_pParticleSystemMgr->IsParticleSystemDefined( pszParticleSystemName ) )
{
AssertMsg1( false, "%s is not a valid particle system name", pszParticleSystemName );
return;
}
m_pParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( pszParticleSystemName );
m_ParticleSystemName = pszParticleSystemName;
m_pParent->PostActionSignal( new KeyValues( "ParticleSystemReconstructed" ) );
if( m_bStartActivated )
{
StartupParticleCollection();
}
}
void CTFParticlePanel::ParticleEffect_t::Paint( CMatRenderContextPtr& pRenderContext, int iXOffset, int iYOffset, float flXScale, float flYScale, int screenW, int screenH )
{
if ( !m_pParticleSystem || !m_bStarted )
return;
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
pRenderContext->Ortho( 0, 0, screenW, screenH, -9999, 9999 );
pRenderContext->Translate( flXScale * ( m_nXPos + iXOffset ), screenH - flYScale * ( m_nYPos + iYOffset ), 0.f );
pRenderContext->Scale( m_flScale, m_flScale, m_flScale );
// Render Particles
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity( );
m_pParticleSystem->Render( pRenderContext );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PopMatrix();
}
|