forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefabsdlg.cpp
623 lines (510 loc) · 13.6 KB
/
prefabsdlg.cpp
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// PrefabsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "hammer.h"
#include "PrefabsDlg.h"
#include "Prefabs.h"
#include "Prefab3D.h"
#include "EditPrefabDlg.h"
#include "MapDoc.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
/////////////////////////////////////////////////////////////////////////////
// CPrefabsDlg dialog
CPrefabsDlg::CPrefabsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrefabsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPrefabsDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPrefabsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPrefabsDlg)
DDX_Control(pDX, IDC_OBJECTS, m_Objects);
DDX_Control(pDX, IDC_OBJECTNOTES, m_ObjectNotes);
DDX_Control(pDX, IDC_LIBRARYNOTES, m_LibraryNotes);
DDX_Control(pDX, IDC_LIBRARIES, m_Libraries);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrefabsDlg, CDialog)
//{{AFX_MSG_MAP(CPrefabsDlg)
ON_BN_CLICKED(IDC_ADDLIBRARY, OnAddlibrary)
ON_BN_CLICKED(IDC_ADDOBJECT, OnAddobject)
ON_BN_CLICKED(IDC_EDITLIBRARY, OnEditlibrary)
ON_BN_CLICKED(IDC_EDITOBJECT, OnEditobject)
ON_BN_CLICKED(IDC_EXPORTOBJECT, OnExportobject)
ON_CBN_SELCHANGE(IDC_LIBRARIES, OnSelchangeLibraries)
ON_BN_CLICKED(IDC_REMOVELIBRARY, OnRemovelibrary)
ON_BN_CLICKED(IDC_REMOVEOBJECT, OnRemoveobject)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_OBJECTS, OnItemchangedObjects)
ON_NOTIFY(LVN_ENDLABELEDIT, IDC_OBJECTS, OnEndlabeleditObjects)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_COMMAND_EX(id_EditObjectInfo, HandleEditObjectPopup)
ON_COMMAND_EX(id_EditObjectData, HandleEditObjectPopup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPrefabsDlg message handlers
void CPrefabsDlg::OnAddobject()
{
CPrefabLibrary *pLibrary = GetCurrentLibrary();
if(!pLibrary)
return; // no lib, no add
CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_NOCHANGEDIR,
"Prefab files (*.map;*.rmf;*.os)|*.map; *.rmf; *.os|"
"Game MAP files (*.map)|*.map|"
"Worldcraft RMF files (*.rmf)|*.rmf||", this);
if(dlg.DoModal() == IDCANCEL)
return; // aborted
// add all these files ..
char szDir[MAX_PATH], szFiles[2048];
memcpy(szFiles, dlg.m_ofn.lpstrFile, dlg.m_ofn.nMaxFile);
strcpy(szDir, dlg.m_ofn.lpstrFile);
BOOL bOneFile = FALSE;
char *p = szFiles + strlen(szDir) + 1;
if(!p[0])
{
bOneFile = TRUE;
p = szDir; // just one file
}
// disable caching of prefabs
CPrefab::EnableCaching(FALSE);
// get files
char szFile[MAX_PATH];
CString strFullPath;
int iItem = m_Objects.GetItemCount();
while(1)
{
strcpy(szFile, p);
if(!szFile[0])
break;
p += strlen(szFile) + 1;
if(!bOneFile)
strFullPath.Format("%s\\%s", szDir, szFile);
else
strFullPath = szFile;
// check file type
CPrefab *pPrefab = NULL;
switch(CPrefab::CheckFileType(strFullPath))
{
case CPrefab::pftUnknown:
{
continue; // no.
}
case CPrefab::pftRMF:
{
CPrefabRMF *pNew = new CPrefabRMF;
pNew->Init(strFullPath, TRUE, CPrefab::lsRMF);
pPrefab = (CPrefab *)pNew;
break;
}
case CPrefab::pftMAP:
{
CPrefabRMF *pNew = new CPrefabRMF;
pNew->Init(strFullPath, TRUE, CPrefab::lsMAP);
pPrefab = (CPrefab *)pNew;
break;
}
case CPrefab::pftScript:
{
Assert(0); // not supported yet
break;
}
}
if (!pPrefab)
{
continue;
}
// add to current library
pLibrary->Add(pPrefab);
// add to objects list
AddToObjectList(pPrefab, iItem++);
if(bOneFile)
break;
}
// now rewrite library
pLibrary->Sort();
pLibrary->Save();
CPrefab::FreeAllData(); // free memory
// re-enable prefab caching
CPrefab::EnableCaching(TRUE);
bCurLibraryModified = FALSE;
}
void CPrefabsDlg::AddToObjectList(CPrefab *pPrefab, int iItem, BOOL bReplace)
{
if(iItem == -1)
iItem = m_Objects.GetItemCount();
if(bReplace) // replace existing item
{
m_Objects.DeleteItem(iItem);
}
iItem = m_Objects.InsertItem(iItem, pPrefab->GetName(),
pPrefab->GetType() == CPrefab::pftScript ? 1 : 0);
m_Objects.SetItemData(iItem, pPrefab->GetID());
bCurLibraryModified = TRUE;
}
BOOL CPrefabsDlg::HandleEditObjectPopup(UINT nID)
{
switch(nID)
{
case id_EditObjectInfo:
EditObjectInfo();
break;
case id_EditObjectData:
EditObjectData();
break;
}
return TRUE;
}
static BOOL IsValidFilename(LPCTSTR pszString)
// check for valid windows filename. no drive/dirs allowed.
{
LPCTSTR p = pszString;
while(p[0])
{
BYTE ch = BYTE(p[0]);
++p;
if(ch > 127 || V_isalpha(ch) || V_isdigit(ch) ||
strchr(" $%`-_@~'!(){}^#&", ch))
continue;
// not one of those chars - not correct
return FALSE;
}
return TRUE;
}
void CPrefabsDlg::EditObjectInfo()
{
int iSel;
CPrefab *pPrefab = GetCurrentObject(&iSel);
CEditPrefabDlg dlg;
dlg.m_strName = pPrefab->GetName();
dlg.m_strDescript = pPrefab->GetNotes();
dlg.SetRanges(500, -1);
if(dlg.DoModal() == IDCANCEL)
return;
pPrefab->SetName(dlg.m_strName);
pPrefab->SetNotes(dlg.m_strDescript);
AddToObjectList(pPrefab, iSel, TRUE);
bCurLibraryModified = TRUE;
}
void CPrefabsDlg::EditObjectData()
{
// get application
CHammer *pApp = (CHammer*) AfxGetApp();
if(bCurLibraryModified)
{
CPrefabLibrary *pLibrary = GetCurrentLibrary();
if(pLibrary)
pLibrary->Save();
}
CMapDoc *pDoc = (CMapDoc*) pApp->pMapDocTemplate->OpenDocumentFile(NULL);
pDoc->EditPrefab3D(GetCurrentObject()->GetID());
EndDialog(IDOK);
}
void CPrefabsDlg::OnEditobject()
{
if(!GetCurrentObject()) // nothing
return;
// two stages - name/description OR data itself
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING, id_EditObjectInfo, "Name and Description");
menu.AppendMenu(MF_STRING, id_EditObjectData, "Prefab Data");
// track menu
CWnd *pButton = GetDlgItem(IDC_EDITOBJECT);
CRect r;
pButton->GetWindowRect(r);
menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, r.left, r.bottom,
this, NULL);
}
void CPrefabsDlg::OnRemoveobject()
{
// remove marked objectsss
int iIndex = m_Objects.GetNextItem(-1, LVNI_SELECTED);
BOOL bConfirmed = FALSE;
CPrefabLibrary *pLibrary = GetCurrentLibrary();
while(iIndex != -1)
{
CPrefab *pPrefab = CPrefab::FindID(m_Objects.GetItemData(iIndex));
if(pPrefab)
{
// delete it
if(!bConfirmed)
{
// do confirmation.
if(AfxMessageBox("Are you sure you want to delete these "
"items?", MB_YESNO) == IDNO)
return; // nope!
m_Objects.SetRedraw(FALSE); // no redraw while doing this
}
bConfirmed = TRUE;
// remove from lib & delete
pLibrary->Remove(pPrefab);
delete pPrefab;
// delete from list and shift index down so we keep processing
// correctly
m_Objects.DeleteItem(iIndex--);
}
// get next item
iIndex = m_Objects.GetNextItem(iIndex, LVNI_SELECTED);
}
// save library
pLibrary->Save();
m_Objects.SetRedraw(TRUE); // redraw objects
m_Objects.Invalidate();
}
void CPrefabsDlg::OnExportobject()
{
int iIndex = m_Objects.GetNextItem(-1, LVNI_SELECTED);
while(iIndex != -1)
{
CPrefab *pPrefab = CPrefab::FindID(m_Objects.GetItemData(iIndex));
if(pPrefab)
{
// export it
CString strFilename;
strFilename = pPrefab->GetName();
CFileDialog dlg(FALSE, "map", strFilename, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, "Map files|*.map;*.rmf|", this);
if(dlg.DoModal() == IDCANCEL)
return; // nevermind
strFilename = dlg.GetPathName();
int iPos = strFilename.Find('.');
DWORD dwFlags = CPrefab::lsRMF;
if(iPos != -1)
{
char *p = strFilename.GetBuffer(0);
if(!strnicmp(p+iPos+1, "map", 3))
dwFlags = CPrefab::lsMAP;
}
pPrefab->Save(strFilename, dwFlags);
}
// get next item
iIndex = m_Objects.GetNextItem(iIndex, LVNI_SELECTED);
}
}
void CPrefabsDlg::SetCurObject(int iItem)
{
iCurObject = iItem;
if(iCurObject == -1)
{
m_ObjectNotes.SetWindowText("");
return;
}
// update data..
CPrefab *pPrefab = CPrefab::FindID(m_Objects.GetItemData(iCurObject));
Assert(pPrefab);
m_ObjectNotes.SetWindowText(pPrefab->GetNotes());
}
void CPrefabsDlg::OnItemchangedObjects(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if(!(pNMListView->uChanged & LVIF_STATE))
return;
if(pNMListView->uNewState & LVIS_FOCUSED)
{
SetCurObject(pNMListView->iItem);
}
}
//
// Library mgmt
//
CPrefabLibrary *CPrefabsDlg::GetCurrentLibrary(int *piSel)
{
if(piSel)
piSel[0] = -1;
int iSel = m_Libraries.GetCurSel();
if(iSel == CB_ERR)
return NULL;
CPrefabLibrary *pLibrary = CPrefabLibrary::FindID(
m_Libraries.GetItemData(iSel));
if(piSel)
piSel[0] = iSel;
return pLibrary;
}
CPrefab *CPrefabsDlg::GetCurrentObject(int *piSel)
{
if(piSel)
piSel[0] = -1;
int iSel = iCurObject;
if(iSel == -1)
return NULL;
CPrefab *pPrefab= CPrefab::FindID(m_Objects.GetItemData(iSel));
if(piSel)
piSel[0] = iSel;
return pPrefab;
}
void CPrefabsDlg::OnSelchangeLibraries()
{
// get last library
CPrefabLibrary *pLibrary = CPrefabLibrary::FindID(
m_Libraries.GetItemData(iCurLibrary));
// save its index
if(bCurLibraryModified)
{
pLibrary->Save();
}
// update objects list
m_Objects.DeleteAllItems();
iCurLibrary = m_Libraries.GetCurSel();
bCurLibraryModified = FALSE;
pLibrary = GetCurrentLibrary();
if(!pLibrary) return;
// add objects to object list
m_Objects.SetRedraw(FALSE);
POSITION p = ENUM_START;
CPrefab *pPrefab = pLibrary->EnumPrefabs(p);
int iItem = 0;
while(pPrefab)
{
AddToObjectList(pPrefab, iItem++);
pPrefab = pLibrary->EnumPrefabs(p);
}
m_Objects.SetRedraw(TRUE);
m_Objects.Invalidate();
// set description window
m_LibraryNotes.SetWindowText(pLibrary->GetNotes());
}
static int AskAboutInvalidFilename()
{
return AfxMessageBox("That's not a valid name - some of the characters aren't\n"
"acceptable. Try using a name with only A-Z, 0-9, space,\n"
"and these characters: $%`-_@~'!(){}^#&", MB_OKCANCEL);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrefabsDlg::OnAddlibrary()
{
Again:
CEditPrefabDlg dlg;
if(dlg.DoModal() == IDCANCEL)
return;
// check name
if(!IsValidFilename(dlg.m_strName))
{
if(AskAboutInvalidFilename() == IDOK)
goto Again;
return; // nevermind.
}
CPrefabLibraryRMF *pLibrary = new CPrefabLibraryRMF;
pLibrary->SetName(dlg.m_strName);
pLibrary->SetNotes(dlg.m_strDescript);
// add to list
int iIndex = m_Libraries.AddString(pLibrary->GetName());
m_Libraries.SetItemData(iIndex, pLibrary->GetID());
m_Libraries.SetCurSel(iIndex);
OnSelchangeLibraries(); // to redraw description window
bCurLibraryModified = TRUE;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrefabsDlg::OnEditlibrary()
{
// get selection
int iSel;
CPrefabLibrary *pLibrary = GetCurrentLibrary(&iSel);
if(!pLibrary) return;
CEditPrefabDlg dlg;
dlg.m_strName = pLibrary->GetName();
dlg.m_strDescript = pLibrary->GetNotes();
Again:
if(dlg.DoModal() == IDCANCEL)
return;
// check name
if(!IsValidFilename(dlg.m_strName))
{
if(AskAboutInvalidFilename() == IDOK)
goto Again;
return; // nevermind.
}
pLibrary->SetName(dlg.m_strName);
pLibrary->SetNotes(dlg.m_strDescript);
// set in list
m_Libraries.SetRedraw(FALSE);
m_Libraries.DeleteString(iSel);
int iIndex = m_Libraries.InsertString(iSel, pLibrary->GetName());
m_Libraries.SetItemData(iIndex, pLibrary->GetID());
m_Libraries.SetRedraw(TRUE);
m_Libraries.Invalidate();
m_Libraries.SetCurSel(iSel);
OnSelchangeLibraries(); // to redraw description window
bCurLibraryModified = TRUE;
}
void CPrefabsDlg::OnRemovelibrary()
{
// get cur library
int iSel;
CPrefabLibrary *pLibrary = GetCurrentLibrary(&iSel);
if (pLibrary == NULL)
{
return;
}
if (AfxMessageBox("Are you sure you want to delete this library from your hard drive?", MB_YESNO) == IDYES)
{
pLibrary->DeleteFile();
delete pLibrary;
bCurLibraryModified = FALSE;
m_Libraries.DeleteString(iSel);
m_Libraries.SetCurSel(0);
OnSelchangeLibraries(); // to redraw description window
}
}
BOOL CPrefabsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetCurObject(-1);
// make imagelist
PrefabImages.Create(IDB_PREFABS, 32, 1, RGB(0, 255, 255));
PrefabImages.SetBkColor(m_Objects.GetBkColor());
m_Objects.SetImageList(&PrefabImages, LVSIL_NORMAL);
// add libraries to list
POSITION p = ENUM_START;
CPrefabLibrary *pLibrary = CPrefabLibrary::EnumLibraries(p);
while(pLibrary)
{
int iIndex = m_Libraries.AddString(pLibrary->GetName());
m_Libraries.SetItemData(iIndex, pLibrary->GetID());
pLibrary = CPrefabLibrary::EnumLibraries(p);
}
iCurLibrary = 0;
bCurLibraryModified = FALSE;
m_Libraries.SetCurSel(0);
OnSelchangeLibraries();
return TRUE;
}
void CPrefabsDlg::OnEndlabeleditObjects(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
LV_ITEM &item = pDispInfo->item;
*pResult = 0;
if(item.pszText == NULL)
return;
CPrefab *pPrefab = CPrefab::FindID(m_Objects.GetItemData(item.iItem));
pPrefab->SetName(item.pszText);
m_Objects.SetItemText(item.iItem, 0, item.pszText);
bCurLibraryModified = TRUE;
}
void CPrefabsDlg::OnClose()
{
// get library
CPrefabLibrary *pLibrary = GetCurrentLibrary();
// save it
if(bCurLibraryModified && pLibrary)
{
pLibrary->Save();
}
CDialog::OnClose();
}