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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Singleton dialog that generates and presents the entity report.
//
//===========================================================================//
#include "particlesystemdefinitionbrowser.h"
#include "tier1/KeyValues.h"
#include "tier1/utlbuffer.h"
#include "iregistry.h"
#include "vgui/ivgui.h"
#include "vgui_controls/listpanel.h"
#include "vgui_controls/inputdialog.h"
#include "vgui_controls/messagebox.h"
#include "petdoc.h"
#include "pettool.h"
#include "datamodel/dmelement.h"
#include "vgui/keycode.h"
#include "dme_controls/dmecontrols_utils.h"
#include "dme_controls/particlesystempanel.h"
#include "filesystem.h"
#include "vgui_controls/FileOpenDialog.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui;
//-----------------------------------------------------------------------------
// Sort by particle system definition name
//-----------------------------------------------------------------------------
static int __cdecl ParticleSystemNameSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
{
const char *string1 = item1.kv->GetString("name");
const char *string2 = item2.kv->GetString("name");
return Q_stricmp( string1, string2 );
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CParticleSystemDefinitionBrowser::CParticleSystemDefinitionBrowser( CPetDoc *pDoc, vgui::Panel* pParent, const char *pName )
: BaseClass( pParent, pName ), m_pDoc( pDoc )
{
SetKeyBoardInputEnabled( true );
SetPaintBackgroundEnabled( true );
m_pParticleSystemsDefinitions = new vgui::ListPanel( this, "ParticleSystems" );
m_pParticleSystemsDefinitions->AddColumnHeader( 0, "name", "Name", 52, ListPanel::COLUMN_RESIZEWITHWINDOW );
m_pParticleSystemsDefinitions->SetColumnSortable( 0, true );
m_pParticleSystemsDefinitions->SetEmptyListText( "No Particle System Definitions" );
m_pParticleSystemsDefinitions->AddActionSignalTarget( this );
m_pParticleSystemsDefinitions->SetSortFunc( 0, ParticleSystemNameSortFunc );
m_pParticleSystemsDefinitions->SetSortColumn( 0 );
LoadControlSettingsAndUserConfig( "resource/particlesystemdefinitionbrowser.res" );
UpdateParticleSystemList();
}
CParticleSystemDefinitionBrowser::~CParticleSystemDefinitionBrowser()
{
SaveUserConfig();
}
//-----------------------------------------------------------------------------
// Gets the ith selected particle system
//-----------------------------------------------------------------------------
CDmeParticleSystemDefinition* CParticleSystemDefinitionBrowser::GetSelectedParticleSystem( int i )
{
int iSel = m_pParticleSystemsDefinitions->GetSelectedItem( i );
KeyValues *kv = m_pParticleSystemsDefinitions->GetItem( iSel );
return GetElementKeyValue< CDmeParticleSystemDefinition >( kv, "particleSystem" );
}
//-----------------------------------------------------------------------------
// Purpose: Deletes the marked objects.
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::DeleteParticleSystems()
{
int iSel = m_pParticleSystemsDefinitions->GetSelectedItem( 0 );
int nRow = m_pParticleSystemsDefinitions->GetItemCurrentRow( iSel ) - 1;
{
// This is undoable
CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Delete Particle Systems", "Delete Particle Systems" );
//
// Build a list of objects to delete.
//
CUtlVector< CDmeParticleSystemDefinition* > itemsToDelete;
int nCount = m_pParticleSystemsDefinitions->GetSelectedItemsCount();
for (int i = 0; i < nCount; i++)
{
CDmeParticleSystemDefinition *pParticleSystem = GetSelectedParticleSystem( i );
if ( pParticleSystem )
{
itemsToDelete.AddToTail( pParticleSystem );
}
}
nCount = itemsToDelete.Count();
for ( int i = 0; i < nCount; ++i )
{
m_pDoc->DeleteParticleSystemDefinition( itemsToDelete[i] );
}
}
// Update the list box selection.
if ( m_pParticleSystemsDefinitions->GetItemCount() > 0 )
{
if ( nRow < 0 )
{
nRow = 0;
}
else if ( nRow >= m_pParticleSystemsDefinitions->GetItemCount() )
{
nRow = m_pParticleSystemsDefinitions->GetItemCount() - 1;
}
iSel = m_pParticleSystemsDefinitions->GetItemIDFromRow( nRow );
m_pParticleSystemsDefinitions->SetSingleSelectedItem( iSel );
}
else
{
m_pParticleSystemsDefinitions->ClearSelectedItems();
}
}
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::LoadKVSection( CDmeParticleSystemDefinition *pNew, KeyValues *pOverridesKv, ParticleFunctionType_t eType )
{
// Operator KV
KeyValues *pOperator = pOverridesKv->FindKey( GetParticleFunctionTypeName(eType), NULL );
if ( !pOperator )
return;
// Function
FOR_EACH_TRUE_SUBKEY( pOperator, pFunctionBlock )
{
int iFunction = pNew->FindFunction( eType, pFunctionBlock->GetName() );
if ( iFunction >= 0 )
{
CDmeParticleFunction *pDmeFunction = pNew->GetParticleFunction( eType, iFunction );
// Elements
FOR_EACH_SUBKEY( pFunctionBlock, pAttributeItem )
{
CDmAttribute *pAttribute = pDmeFunction->GetAttribute( pAttributeItem->GetName() );
if ( !pAttribute )
{
Warning( "Unable to Find Attribute [%s] in Function [%s] in Operator [%s] for Definition [%s]\n", pAttributeItem->GetName(), pFunctionBlock->GetName(), GetParticleFunctionTypeName(eType), pNew->GetName() );
}
else
{
pAttribute->SetValueFromString( pAttributeItem->GetString() );
}
}
}
else
{
Warning( "Function [%s] not found under Operator [%s] for Definition [%s]\n", pFunctionBlock->GetName(), GetParticleFunctionTypeName(eType), pNew->GetName() );
}
}
}
//-----------------------------------------------------------------------------
// Given a KV, create, add and return an effect
//-----------------------------------------------------------------------------
CDmeParticleSystemDefinition* CParticleSystemDefinitionBrowser::CreateParticleFromKV( KeyValues *pKeyValue )
{
CDmeParticleSystemDefinition* pBaseParticleDef = NULL;
// Get the Base Particle Effect Def
const char* pBaseParticleName = pKeyValue->GetString( "base_effect", "" );
for ( int i = 0; i < m_pParticleSystemsDefinitions->GetItemCount(); ++i )
{
KeyValues *kv = m_pParticleSystemsDefinitions->GetItem( i );
if ( !V_strcmp( kv->GetString( "name", "" ), pBaseParticleName ) )
{
//
pBaseParticleDef = GetElementKeyValue< CDmeParticleSystemDefinition >( kv, "particleSystem" );
break;
}
}
// Base Particle could not be found, end;
if ( !pBaseParticleDef )
{
Warning( "Unable to to find base particle system [%s]", pBaseParticleName );
return NULL;
}
// Create a Copy of the Base Effect
const char *pszNewParticleName = pKeyValue->GetName();
//CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Copy Particle System", "Copy Particle System" );
CDmeParticleSystemDefinition *pNew = CastElement<CDmeParticleSystemDefinition>( pBaseParticleDef->Copy() );
pNew->SetName( pszNewParticleName );
// Overrides
//
//"properties"
KeyValues *pProperties = pKeyValue->FindKey( "Properties", NULL );
if ( pProperties )
{
FOR_EACH_SUBKEY( pProperties, pProperty )
{
CDmAttribute *pAttribute = pNew->GetAttribute( pProperty->GetName() );
if ( !pAttribute )
{
Warning( "Unable to Find Attribute [%s] in Function [%s]\n", pProperty->GetName(), "Properties" );
}
else
{
pAttribute->SetValueFromString( pProperty->GetString() );
}
}
}
LoadKVSection( pNew, pKeyValue, FUNCTION_RENDERER );
LoadKVSection( pNew, pKeyValue, FUNCTION_OPERATOR );
LoadKVSection( pNew, pKeyValue, FUNCTION_INITIALIZER );
LoadKVSection( pNew, pKeyValue, FUNCTION_EMITTER );
LoadKVSection( pNew, pKeyValue, FUNCTION_FORCEGENERATOR );
LoadKVSection( pNew, pKeyValue, FUNCTION_CONSTRAINT );
// Remove copied children
int iChildrenCount = pNew->GetParticleFunctionCount( FUNCTION_CHILDREN );
for ( int i = iChildrenCount - 1; i >= 0; i-- )
{
pNew->RemoveFunction( FUNCTION_CHILDREN, i );
}
// Search Children
KeyValues *pChildren = pKeyValue->FindKey( "Children", NULL );
if ( pChildren )
{
FOR_EACH_TRUE_SUBKEY( pChildren, pChild )
{
// each Child is its own effect so we need to add it and return it
CDmeParticleSystemDefinition* pChildEffect = CreateParticleFromKV( pChild );
if ( pChildEffect )
{
pNew->AddChild( pChildEffect );
}
}
}
m_pDoc->ReplaceParticleSystemDefinition( pNew );
m_pDoc->UpdateAllParticleSystems();
return pNew;
}
//-----------------------------------------------------------------------------
// Create from KV
void CParticleSystemDefinitionBrowser::CreateParticleSystemsFromKV( const char *pFileName )
{
//
//const char * pFileName = "particles\\_weapon_prefab_override_kv.txt";
CUtlBuffer bufRawData;
bool bReadFileOK = g_pFullFileSystem->ReadFile( pFileName, "MOD", bufRawData );
if ( !bReadFileOK )
{
Warning( "Unable to Open KV file [%s]\n", pFileName );
return;
}
// Wrap it with a text buffer reader
CUtlBuffer bufText( bufRawData.Base(), bufRawData.TellPut(), CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER );
KeyValues *pBaseKeyValue = NULL;
pBaseKeyValue = new KeyValues( "CCreateParticlesFromKV" );
if ( !pBaseKeyValue->LoadFromBuffer( NULL, bufText ) )
{
Warning( "Unable to Read KV file [%s]\n", pFileName );
pBaseKeyValue->deleteThis();
return;
}
FOR_EACH_TRUE_SUBKEY( pBaseKeyValue, pKVOver )
{
CreateParticleFromKV( pKVOver );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::OnKeyCodeTyped( vgui::KeyCode code )
{
if ( code == KEY_DELETE )
{
DeleteParticleSystems();
}
else
{
BaseClass::OnKeyCodeTyped( code );
}
}
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::OnFileSelected(const char *fullpath)
{
CreateParticleSystemsFromKV( fullpath );
}
//-----------------------------------------------------------------------------
// Called when the selection changes
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::UpdateParticleSystemSelection()
{
if ( m_pParticleSystemsDefinitions->GetSelectedItemsCount() == 1 )
{
CDmeParticleSystemDefinition *pParticleSystem = GetSelectedParticleSystem( 0 );
g_pPetTool->SetCurrentParticleSystem( pParticleSystem, false );
}
else
{
g_pPetTool->SetCurrentParticleSystem( NULL, false );
}
}
//-----------------------------------------------------------------------------
// Item selection/deselection
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::OnItemSelected( void )
{
UpdateParticleSystemSelection();
}
void CParticleSystemDefinitionBrowser::OnItemDeselected( void )
{
UpdateParticleSystemSelection();
}
//-----------------------------------------------------------------------------
// Select a particular node
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::SelectParticleSystem( CDmeParticleSystemDefinition *pFind )
{
m_pParticleSystemsDefinitions->ClearSelectedItems();
for ( int nItemID = m_pParticleSystemsDefinitions->FirstItem(); nItemID != m_pParticleSystemsDefinitions->InvalidItemID(); nItemID = m_pParticleSystemsDefinitions->NextItem( nItemID ) )
{
KeyValues *kv = m_pParticleSystemsDefinitions->GetItem( nItemID );
CDmeParticleSystemDefinition *pParticleSystem = GetElementKeyValue<CDmeParticleSystemDefinition>( kv, "particleSystem" );
if ( pParticleSystem == pFind )
{
m_pParticleSystemsDefinitions->AddSelectedItem( nItemID );
break;
}
}
}
//-----------------------------------------------------------------------------
// Called when buttons are clicked
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::OnInputCompleted( KeyValues *pKeyValues )
{
const char *pText = pKeyValues->GetString( "text", NULL );
if ( m_pDoc->IsParticleSystemDefined( pText ) )
{
char pBuf[1024];
Q_snprintf( pBuf, sizeof(pBuf), "Particle System \"%s\" already exists!\n", pText );
vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Duplicate Particle System Name!\n", pBuf, g_pPetTool->GetRootPanel() );
pMessageBox->DoModal( );
return;
}
if ( pKeyValues->FindKey( "create" ) )
{
CDmeParticleSystemDefinition *pParticleSystem = m_pDoc->AddNewParticleSystemDefinition( pText );
g_pPetTool->SetCurrentParticleSystem( pParticleSystem );
}
else if ( pKeyValues->FindKey( "copy" ) )
{
int nCount = m_pParticleSystemsDefinitions->GetSelectedItemsCount();
if ( nCount )
{
CDmeParticleSystemDefinition *pParticleSystem = GetSelectedParticleSystem( 0 );
CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Copy Particle System",
"Copy Particle System" );
CDmeParticleSystemDefinition * pNew =
CastElement<CDmeParticleSystemDefinition>( pParticleSystem->Copy( ) );
pNew->SetName( pText );
m_pDoc->AddNewParticleSystemDefinition( pNew, guard );
}
}
}
//-----------------------------------------------------------------------------
// Copy to clipboard
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::CopyToClipboard( )
{
int nCount = m_pParticleSystemsDefinitions->GetSelectedItemsCount();
CUtlVector< KeyValues * > list;
CUtlRBTree< CDmeParticleSystemDefinition* > defs( 0, 0, DefLessFunc( CDmeParticleSystemDefinition* ) );
for ( int i = 0; i < nCount; ++i )
{
CDmeParticleSystemDefinition *pParticleSystem = GetSelectedParticleSystem( i );
CUtlBuffer buf( 0, 0, CUtlBuffer::TEXT_BUFFER );
if ( g_pDataModel->Serialize( buf, "keyvalues2", "pcf", pParticleSystem->GetHandle() ) )
{
KeyValues *pData = new KeyValues( "Clipboard" );
pData->SetString( "pcf", (char*)buf.Base() );
list.AddToTail( pData );
}
}
if ( list.Count() )
{
g_pDataModel->SetClipboardData( list );
}
}
//-----------------------------------------------------------------------------
// Paste from clipboard
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::ReplaceDef_r( CUndoScopeGuard& guard, CDmeParticleSystemDefinition *pDef )
{
if ( !pDef )
return;
m_pDoc->ReplaceParticleSystemDefinition( pDef );
int nChildCount = pDef->GetParticleFunctionCount( FUNCTION_CHILDREN );
for ( int i = 0; i < nChildCount; ++i )
{
CDmeParticleChild *pChildFunction = static_cast< CDmeParticleChild* >( pDef->GetParticleFunction( FUNCTION_CHILDREN, i ) );
CDmeParticleSystemDefinition* pChild = pChildFunction->m_Child;
ReplaceDef_r( guard, pChild );
}
}
void CParticleSystemDefinitionBrowser::PasteFromClipboard( )
{
// This is undoable
CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Paste From Clipboard", "Paste From Clipboard" );
bool bRefreshAll = false;
CUtlVector< KeyValues * > list;
g_pDataModel->GetClipboardData( list );
int nItems = list.Count();
for ( int i = 0; i < nItems; ++i )
{
const char *pData = list[i]->GetString( "pcf" );
if ( !pData )
continue;
int nLen = Q_strlen( pData );
CUtlBuffer buf( pData, nLen, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::READ_ONLY );
DmElementHandle_t hRoot;
if ( !g_pDataModel->Unserialize( buf, "keyvalues2", "pcf", NULL, "paste", CR_FORCE_COPY, hRoot ) )
continue;
CDmeParticleSystemDefinition *pDef = GetElement<CDmeParticleSystemDefinition>( hRoot );
if ( !pDef )
continue;
ReplaceDef_r( guard, pDef );
bRefreshAll = true;
}
guard.Release();
if ( bRefreshAll )
{
m_pDoc->UpdateAllParticleSystems();
}
}
//-----------------------------------------------------------------------------
// Called when buttons are clicked
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::OnCommand( const char *pCommand )
{
if ( !Q_stricmp( pCommand, "create" ) )
{
vgui::InputDialog *pInputDialog = new vgui::InputDialog( g_pPetTool->GetRootPanel(), "Enter Particle System Name", "Name:", "" );
pInputDialog->SetSmallCaption( true );
pInputDialog->SetMultiline( false );
pInputDialog->AddActionSignalTarget( this );
pInputDialog->DoModal( new KeyValues("create") );
return;
}
if ( !Q_stricmp( pCommand, "copy" ) )
{
vgui::InputDialog *pInputDialog = new vgui::InputDialog( g_pPetTool->GetRootPanel(), "Enter Particle System Name", "Name:", "" );
pInputDialog->SetSmallCaption( true );
pInputDialog->SetMultiline( false );
pInputDialog->AddActionSignalTarget( this );
pInputDialog->DoModal( new KeyValues("copy") );
return;
}
if ( !Q_stricmp( pCommand, "Create From KeyValue" ) )
{
vgui::FileOpenDialog *pDialog = new vgui::FileOpenDialog( g_pPetTool->GetRootPanel(), "Select KV File", vgui::FOD_OPEN );
pDialog->SetTitle( "Choose KeyValue File", true );
pDialog->AddFilter( "*.txt", "KeyValue File (*.txt)", true );
pDialog->AddActionSignalTarget( this );
char szParticlesDir[MAX_PATH];
pDialog->SetStartDirectory( g_pFullFileSystem->RelativePathToFullPath( "particles", "MOD", szParticlesDir, sizeof(szParticlesDir) ) );
pDialog->DoModal( new KeyValues( "Create From KeyValue" ) );
return;
}
if ( !Q_stricmp( pCommand, "delete" ) )
{
DeleteParticleSystems();
return;
}
if ( !Q_stricmp( pCommand, "Save" ) )
{
g_pPetTool->Save();
return;
}
if ( !Q_stricmp( pCommand, "SaveAndTest" ) )
{
g_pPetTool->SaveAndTest();
return;
}
BaseClass::OnCommand( pCommand );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CParticleSystemDefinitionBrowser::UpdateParticleSystemList(void)
{
const CDmrParticleSystemList particleSystemList = m_pDoc->GetParticleSystemDefinitionList();
if ( !particleSystemList.IsValid() )
return;
// Maintain selection if possible
CUtlVector< CUtlString > selectedItems;
int nCount = m_pParticleSystemsDefinitions->GetSelectedItemsCount();
for ( int i = 0; i < nCount; ++i )
{
CDmeParticleSystemDefinition *pParticleSystem = GetSelectedParticleSystem( i );
if ( pParticleSystem )
{
selectedItems.AddToTail( pParticleSystem->GetName() );
}
}
m_pParticleSystemsDefinitions->RemoveAll();
int nSelectedItemCount = selectedItems.Count();
nCount = particleSystemList.Count();
for ( int i = 0; i < nCount; ++i )
{
CDmeParticleSystemDefinition *pParticleSystem = particleSystemList[i];
if ( !pParticleSystem )
continue;
const char *pName = pParticleSystem->GetName();
if ( !pName || !pName[0] )
{
pName = "<no name>";
}
KeyValues *kv = new KeyValues( "node" );
kv->SetString( "name", pName );
SetElementKeyValue( kv, "particleSystem", pParticleSystem );
int nItemID = m_pParticleSystemsDefinitions->AddItem( kv, 0, false, false );
for ( int j = 0; j < nSelectedItemCount; ++j )
{
if ( Q_stricmp( selectedItems[j], pName ) )
continue;
m_pParticleSystemsDefinitions->AddSelectedItem( nItemID );
selectedItems.FastRemove(j);
--nSelectedItemCount;
break;
}
}
m_pParticleSystemsDefinitions->SortList();
}
|