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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "stdafx.h"
#include <oleauto.h>
#include <oaidl.h>
#if _MSC_VER < 1300
#include <afxpriv.h>
#endif
#include "CustomMessages.h"
#include "GlobalFunctions.h"
#include "History.h"
#include "FaceEditSheet.h"
#include "IEditorTexture.h"
#include "MainFrm.h"
#include "MapDoc.h"
#include "MapWorld.h"
#include "ReplaceTexDlg.h"
#include "TextureBrowser.h"
#include "TextureSystem.h"
#include "hammer.h"
#include "Selection.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
static LPCTSTR pszIniSection = "Texture Browser";
CStringArray CTextureBrowser::m_FilterHistory;
int CTextureBrowser::m_nFilterHistory;
char CTextureBrowser::m_szLastKeywords[MAX_PATH];
BEGIN_MESSAGE_MAP(CTextureBrowser, CDialog)
//{{AFX_MSG_MAP(CTextureBrowser)
ON_WM_SIZE()
ON_CBN_SELENDOK(IDC_TEXTURESIZE, OnSelendokTexturesize)
ON_WM_CLOSE()
ON_WM_TIMER()
ON_CBN_EDITCHANGE(IDC_FILTER, OnChangeFilterOrKeywords)
ON_CBN_SELENDOK(IDC_FILTER, OnUpdateFiltersNOW)
ON_CBN_EDITCHANGE(IDC_KEYWORDS, OnChangeFilterOrKeywords)
ON_CBN_SELENDOK(IDC_KEYWORDS, OnUpdateKeywordsNOW)
ON_BN_CLICKED(IDC_FILTER_OPAQUE, OnFilterOpaque)
ON_BN_CLICKED(IDC_FILTER_TRANSLUCENT, OnFilterTranslucent)
ON_BN_CLICKED(IDC_FILTER_SELFILLUM, OnFilterSelfIllum)
ON_BN_CLICKED(IDC_FILTER_ENVMASK, OnFilterEnvmask)
ON_BN_CLICKED(IDC_SHOW_ERROR, OnShowErrors)
ON_BN_CLICKED(IDC_USED, OnUsed)
ON_BN_CLICKED(IDC_MARK, OnMark)
ON_BN_CLICKED(IDC_REPLACE, OnReplace)
ON_BN_CLICKED(IDC_TEXTURES_OPEN_SOURCE, OnOpenSource)
ON_BN_CLICKED(IDC_TEXTURES_RELOAD, OnReload)
ON_MESSAGE(TWN_SELCHANGED, OnTexturewindowSelchange)
ON_MESSAGE(TWN_LBUTTONDBLCLK, OnTextureWindowDblClk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
// Purpose:
// Input : pParent -
//-----------------------------------------------------------------------------
CTextureBrowser::CTextureBrowser(CWnd* pParent)
: CDialog(IDD, pParent)
{
m_szNameFilter[0] = '\0';
szInitialTexture[0] = '\0';
m_bFilterChanged = FALSE;
m_uLastFilterChange = 0xffffffff;
m_bUsed = FALSE;
m_szLastKeywords[0] = '\0';
}
//-----------------------------------------------------------------------------
// Purpose: Handles resize messages. Moves the child windows to the proper positions.
// Input : nType -
// cx -
// cy -
//-----------------------------------------------------------------------------
void CTextureBrowser::OnSize(UINT nType, int cx, int cy)
{
if (nType == SIZE_MINIMIZED || !IsWindow(m_cTextureWindow.m_hWnd))
{
CDialog::OnSize(nType, cx, cy);
return;
}
// reposition controls
CRect clientrect;
GetClientRect(&clientrect);
CRect CtrlRect;
GetDlgItem(IDC_CONTROLHEIGHT)->GetWindowRect(&CtrlRect);
int iControlHeight = (CtrlRect.bottom - CtrlRect.top);
int iThisCtrlHeight;
//
// Resize the texture window.
//
CtrlRect = clientrect;
CtrlRect.bottom -= iControlHeight;
m_cTextureWindow.MoveWindow(CtrlRect);
clientrect.top = (clientrect.bottom - iControlHeight) + 4;
//
// Move the top row of controls to the correct vertical position,
// leaving their horizontal position as it was set up in the dialog.
//
int iIDList[] =
{
IDC_TEXTURESIZE,
IDC_SIZEPROMPT,
IDC_FILTERPROMPT,
IDC_FILTER,
IDC_CURNAME,
IDC_FILTER_OPAQUE,
IDC_FILTER_SELFILLUM,
IDC_SHOW_ERROR,
IDC_TEXTURES_OPEN_SOURCE,
-1
};
for (int i = 0; iIDList[i] != -1; i++)
{
CWnd *pWnd = GetDlgItem(iIDList[i]);
Assert(pWnd != NULL);
if (pWnd != NULL)
{
pWnd->GetWindowRect(&CtrlRect);
ScreenToClient(CtrlRect);
iThisCtrlHeight = CtrlRect.bottom - CtrlRect.top;
CtrlRect.top = clientrect.top;
CtrlRect.bottom = CtrlRect.top + iThisCtrlHeight;
pWnd->MoveWindow(CtrlRect);
}
}
//
// Move the middle row of controls to the correct vertical position,
// leaving their horizontal position as it was set up in the dialog.
//
int iIDList2[] =
{
IDC_KEYWORDS_TEXT,
IDC_KEYWORDS,
IDC_USED,
IDC_MARK,
IDC_REPLACE,
IDC_CURDESCRIPTION,
IDC_FILTER_TRANSLUCENT,
IDC_FILTER_ENVMASK,
IDC_TEXTURES_RELOAD,
-1
};
for (int i = 0; iIDList2[i] != -1; i++)
{
CWnd *pWnd = GetDlgItem(iIDList2[i]);
Assert(pWnd != NULL);
if (pWnd != NULL)
{
pWnd->GetWindowRect(&CtrlRect);
ScreenToClient(CtrlRect);
iThisCtrlHeight = CtrlRect.bottom - CtrlRect.top;
CtrlRect.top = clientrect.top + iControlHeight / 2 + 2;
CtrlRect.bottom = CtrlRect.top + iThisCtrlHeight;
pWnd->MoveWindow(CtrlRect);
}
}
CDialog::OnSize(nType, cx, cy);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bUsed -
//-----------------------------------------------------------------------------
void CTextureBrowser::SetUsed(BOOL bUsed)
{
m_bUsed = bUsed;
if (m_bUsed)
{
CUsedTextureList Used;
GetActiveWorld()->GetUsedTextures(Used);
m_TextureSubList.RemoveAll();
for (int i = 0; i < Used.Count(); i++)
{
TextureWindowTex_t Tex;
Tex.pTex = Used.Element(i).pTex;
Tex.nUsageCount = Used.Element(i).nUsageCount;
m_TextureSubList.AddToTail(Tex);
}
m_cTextureWindow.SetSpecificList(&m_TextureSubList);
}
else
{
m_cTextureWindow.SetSpecificList(NULL);
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnClose(void)
{
WriteSettings();
SaveAndExit();
CDialog::OnCancel();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnCancel()
{
WriteSettings();
SaveAndExit();
CDialog::OnCancel();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnUsed()
{
if(!GetActiveWorld())
return;
SetUsed(m_cUsed.GetCheck());
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pszTexture -
//-----------------------------------------------------------------------------
void CTextureBrowser::SetInitialTexture(LPCTSTR pszTexture)
{
strcpy(szInitialTexture, pszTexture);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnSelendokTexturesize()
{
// change size of textures the texutre window displays
int iCurSel = m_cSizeList.GetCurSel();
switch(iCurSel)
{
case 0:
m_cTextureWindow.SetDisplaySize(128);
break;
case 1:
m_cTextureWindow.SetDisplaySize(256);
break;
case 2:
m_cTextureWindow.SetDisplaySize(512);
break;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
BOOL CTextureBrowser::OnInitDialog()
{
CDialog::OnInitDialog();
// Iterate all the active textures for debugging.
//int nCount = g_Textures.GetActiveTextureCount();
//for (int nTexture = 0; nTexture < nCount; nTexture++)
//{
// IEditorTexture *pTexture = g_Textures.GetActiveTexture(nTexture);
// const char *pszName = pTexture->GetName();
// DBG("%d: %s\n", nTexture, pszName);
//}
m_cSizeList.SubclassDlgItem(IDC_TEXTURESIZE, this);
m_cFilter.SubclassDlgItem(IDC_FILTER, this);
m_cKeywords.SubclassDlgItem(IDC_KEYWORDS, this);
m_cCurName.SubclassDlgItem(IDC_CURNAME, this);
m_cCurDescription.SubclassDlgItem(IDC_CURDESCRIPTION, this);
m_cUsed.SubclassDlgItem(IDC_USED, this);
m_FilterOpaque.SubclassDlgItem(IDC_FILTER_OPAQUE, this);
m_FilterTranslucent.SubclassDlgItem(IDC_FILTER_TRANSLUCENT, this);
m_FilterSelfIllum.SubclassDlgItem(IDC_FILTER_SELFILLUM, this);
m_FilterEnvMask.SubclassDlgItem(IDC_FILTER_ENVMASK, this);
m_ShowErrors.SubclassDlgItem(IDC_SHOW_ERROR, this);
m_FilterOpaque.SetCheck( true );
m_FilterTranslucent.SetCheck( true );
m_FilterSelfIllum.SetCheck( true );
m_FilterEnvMask.SetCheck( true );
m_ShowErrors.SetCheck( true );
//
// Create CTextureWindow that takes up area of dummy control.
//
{
RECT r;
GetDlgItem( IDC_BROWSERDUMMY )->GetClientRect( &r );
m_cTextureWindow.Create( this, r );
}
// Show everything initially
m_cTextureWindow.SetTypeFilter( ~0, true );
//
// Add latest history to the filter combo.
//
for (int i = 0; i < m_nFilterHistory; i++)
{
m_cFilter.AddString(m_FilterHistory[i]);
}
//
// Set the name filter unless one was explicitly specified.
//
if (m_szNameFilter[0] == '\0')
{
//
// No name filter specified. Use whatever is on top of the history.
//
if (m_cFilter.GetCount() > 0)
{
m_cFilter.GetLBText(0, m_szNameFilter);
m_cFilter.SetCurSel(0);
}
}
m_cFilter.SetWindowText(m_szNameFilter);
m_cTextureWindow.SetNameFilter(m_szNameFilter);
//
// Done with the name filter; clear it for next time.
//
m_szNameFilter[0] = '\0';
// Add the global list of keywords to the keywords combo.
for( int i=0; i< g_Textures.GetNumKeywords(); i++ )
{
m_cKeywords.AddString( g_Textures.GetKeyword(i) );
}
//
// Set the keyword filter.
//
m_cKeywords.SetWindowText(m_szLastKeywords);
m_cTextureWindow.SetKeywords(m_szLastKeywords);
m_cUsed.SetCheck(m_bUsed);
// Refresh the list of used textures if enabled.
if (m_bUsed)
{
SetUsed(TRUE);
}
CWinApp *pApp = AfxGetApp();
CString str = pApp->GetProfileString(pszIniSection, "Position");
if (!str.IsEmpty())
{
RECT r;
sscanf(str, "%d %d %d %d", &r.left, &r.top, &r.right, &r.bottom);
if (r.left < 0)
{
ShowWindow(SW_SHOWMAXIMIZED);
}
else
{
MoveWindow(r.left, r.top, r.right-r.left, r.bottom-r.top, FALSE);
}
}
int iSize = pApp->GetProfileInt(pszIniSection, "ShowSize", 0);
m_cSizeList.SetCurSel(iSize);
OnSelendokTexturesize();
if (szInitialTexture[0])
{
m_cTextureWindow.SelectTexture(szInitialTexture);
}
m_cTextureWindow.ShowWindow(SW_SHOW);
SetTimer(1, 500, NULL);
m_cFilter.SetFocus();
return(FALSE);
}
//-----------------------------------------------------------------------------
// Purpose: Called when either the filter combo or the keywords combo text changes.
//-----------------------------------------------------------------------------
void CTextureBrowser::OnChangeFilterOrKeywords()
{
//
// Start a timer to repaint the texture window using the new filters.
//
m_uLastFilterChange = time(NULL);
m_bFilterChanged = TRUE;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnUpdateFiltersNOW()
{
m_uLastFilterChange = time(NULL);
m_bFilterChanged = FALSE;
CString str;
int iSel = m_cFilter.GetCurSel();
m_cFilter.GetLBText(iSel, str);
m_cTextureWindow.SetNameFilter(str);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnUpdateKeywordsNOW()
{
m_uLastFilterChange = time(NULL);
m_bFilterChanged = FALSE;
CString str;
int iSel = m_cKeywords.GetCurSel();
m_cKeywords.GetLBText(iSel, str);
m_cTextureWindow.SetKeywords(str);
}
//-----------------------------------------------------------------------------
// Purpose: Timer used to control updates when the filter terms change.
// Input : nIDEvent -
//-----------------------------------------------------------------------------
void CTextureBrowser::OnTimer(UINT nIDEvent)
{
if (!m_bFilterChanged)
{
return;
}
if ((time(NULL) - m_uLastFilterChange) > 0)
{
KillTimer(nIDEvent);
m_bFilterChanged = FALSE;
m_cTextureWindow.EnableUpdate(false);
CString str;
m_cFilter.GetWindowText(str);
m_cTextureWindow.SetNameFilter(str);
m_cTextureWindow.EnableUpdate(true);
m_cKeywords.GetWindowText(str);
m_cTextureWindow.SetKeywords(str);
SetTimer(nIDEvent, 500, NULL);
}
CDialog::OnTimer(nIDEvent);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : wParam -
// lParam -
// Output : LRESULT
//-----------------------------------------------------------------------------
LRESULT CTextureBrowser::OnTextureWindowDblClk(WPARAM wParam, LPARAM lParam)
{
WriteSettings();
SaveAndExit();
return(0);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : wParam -
// lParam -
// Output : LRESULT
//-----------------------------------------------------------------------------
LRESULT CTextureBrowser::OnTexturewindowSelchange(WPARAM wParam, LPARAM lParam)
{
IEditorTexture *pTex = g_Textures.FindActiveTexture(m_cTextureWindow.szCurTexture);
CString str;
char szName[MAX_PATH];
if (pTex != NULL)
{
// create description of texture
str.Format("%dx%d", pTex->GetWidth(), pTex->GetHeight());
pTex->GetShortName(szName);
}
else
{
szName[0] = '\0';
}
m_cCurName.SetWindowText(szName);
m_cCurDescription.SetWindowText(str);
return(0);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::WriteSettings()
{
// write position information
CWinApp *pApp = AfxGetApp();
CString str;
CRect r;
GetWindowRect(r);
str.Format("%d %d %d %d", r.left, r.top, r.right, r.bottom);
pApp->WriteProfileString(pszIniSection, "Position", str);
pApp->WriteProfileInt(pszIniSection, "ShowSize", m_cSizeList.GetCurSel());
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::SaveAndExit()
{
// save current filter string
CString str;
m_cFilter.GetWindowText(str);
int i;
for(i = 0; i < m_nFilterHistory; i++)
{
if(!m_FilterHistory[i].CompareNoCase(str))
break;
}
if(i != m_nFilterHistory) // delete first
{
m_FilterHistory.RemoveAt(i);
--m_nFilterHistory;
}
m_FilterHistory.InsertAt(0, str);
++m_nFilterHistory;
m_cKeywords.GetWindowText(m_szLastKeywords, sizeof(m_szLastKeywords));
EndDialog(IDOK);
}
//-----------------------------------------------------------------------------
// Purpose: Sets a name filter that will override whatever is in the history
// for this browser session.
//-----------------------------------------------------------------------------
void CTextureBrowser::SetFilter(const char *pszFilter)
{
if (pszFilter)
{
strcpy(m_szNameFilter, pszFilter);
}
else
{
m_szNameFilter[0] = '\0';
}
}
//-----------------------------------------------------------------------------
// Filter buttons
//-----------------------------------------------------------------------------
void CTextureBrowser::OnFilterOpaque(void)
{
bool checked = m_FilterOpaque.GetCheck( ) != 0;
m_cTextureWindow.SetTypeFilter( CTextureWindow::TYPEFILTER_OPAQUE, checked );
}
void CTextureBrowser::OnFilterTranslucent(void)
{
bool checked = m_FilterTranslucent.GetCheck( ) != 0;
m_cTextureWindow.SetTypeFilter( CTextureWindow::TYPEFILTER_TRANSLUCENT, checked );
}
void CTextureBrowser::OnFilterSelfIllum(void)
{
bool checked = m_FilterSelfIllum.GetCheck( ) != 0;
m_cTextureWindow.SetTypeFilter( CTextureWindow::TYPEFILTER_SELFILLUM, checked );
}
void CTextureBrowser::OnFilterEnvmask(void)
{
bool checked = m_FilterEnvMask.GetCheck( ) != 0;
m_cTextureWindow.SetTypeFilter( CTextureWindow::TYPEFILTER_ENVMASK, checked );
}
void CTextureBrowser::OnShowErrors(void)
{
bool checked = m_ShowErrors.GetCheck( ) != 0;
m_cTextureWindow.ShowErrors( checked );
}
//-----------------------------------------------------------------------------
// Opens the source file:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnOpenSource()
{
if ( m_cTextureWindow.szCurTexture[0] )
{
g_Textures.OpenSource( m_cTextureWindow.szCurTexture );
}
}
void CTextureBrowser::OnReload()
{
if ( m_cTextureWindow.szCurTexture[0] )
{
g_Textures.ReloadTextures( m_cTextureWindow.szCurTexture );
m_cTextureWindow.Invalidate();
if (GetMainWnd())
{
GetMainWnd()->m_TextureBar.NotifyGraphicsChanged();
GetMainWnd()->m_pFaceEditSheet->NotifyGraphicsChanged();
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTextureBrowser::OnMark(void)
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if (pDoc != NULL)
{
pDoc->ReplaceTextures(m_cTextureWindow.szCurTexture, "", TRUE, 0x100, FALSE, FALSE);
EndDialog(IDOK);
}
}
//-----------------------------------------------------------------------------
// Purpose: Invokes the replace texture dialog.
//-----------------------------------------------------------------------------
void CTextureBrowser::OnReplace(void)
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if(!pDoc)
return;
CReplaceTexDlg dlg(pDoc->GetSelection()->GetCount());
dlg.m_strFind = m_cTextureWindow.szCurTexture;
if(dlg.DoModal() != IDOK)
return;
// mark undo position
GetHistory()->MarkUndoPosition(pDoc->GetSelection()->GetList(), "Replace Textures");
if(dlg.m_bMarkOnly)
{
pDoc->SelectObject(NULL, scClear); // clear selection first
}
dlg.DoReplaceTextures();
//EndDialog(IDOK);
if (m_bUsed)
{
SetUsed(TRUE);
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the texture format for browsing. Only textures of the given
// format will be visible in the browse window. By default, this
// format will be the same as the current active texture format.
// Input : eTextureFormat - Texture format to use for browsing.
//-----------------------------------------------------------------------------
void CTextureBrowser::SetTextureFormat(TEXTUREFORMAT eTextureFormat)
{
m_cTextureWindow.SetTextureFormat(eTextureFormat);
}
|