]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/radiant/NewTexWnd.cpp
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / tools / radiant / NewTexWnd.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31
32 #include "qe3.h"
33 #include "Radiant.h"
34 #include "NewTexWnd.h"
35 #include "io.h"
36
37 #include "../../renderer/tr_local.h"
38
39 #ifdef _DEBUG
40         #define new DEBUG_NEW
41         #undef THIS_FILE
42 static char THIS_FILE[] = __FILE__;
43 #endif
44
45 /*
46  =======================================================================================================================
47  =======================================================================================================================
48  */
49 bool Sys_KeyDown( int key ) {
50         return ( ( ::GetAsyncKeyState( key ) & 0x8000 ) != 0 );
51 }
52
53 // CNewTexWnd
54 IMPLEMENT_DYNCREATE(CNewTexWnd, CWnd);
55
56 /*
57  =======================================================================================================================
58  =======================================================================================================================
59  */
60 CNewTexWnd::CNewTexWnd() {
61         m_bNeedRange = true;
62         hglrcTexture = NULL;
63         hdcTexture = NULL;
64         cursor.x = cursor.y = 0;
65         origin.x = origin.y = 0;
66 }
67
68 /*
69  =======================================================================================================================
70  =======================================================================================================================
71  */
72 CNewTexWnd::~CNewTexWnd() {
73 }
74
75 BEGIN_MESSAGE_MAP(CNewTexWnd, CWnd)
76 //{{AFX_MSG_MAP(CNewTexWnd)
77         ON_WM_CREATE()
78         ON_WM_SIZE()
79         ON_WM_PARENTNOTIFY()
80         ON_WM_KEYDOWN()
81         ON_WM_KEYUP()
82         ON_WM_PAINT()
83         ON_WM_VSCROLL()
84         ON_WM_LBUTTONDOWN()
85         ON_WM_MBUTTONDOWN()
86         ON_WM_RBUTTONDOWN()
87         ON_WM_LBUTTONUP()
88         ON_WM_MBUTTONUP()
89         ON_WM_RBUTTONUP()
90         ON_WM_MOUSEMOVE()
91         ON_WM_MOUSEWHEEL()
92         //}}AFX_MSG_MAP
93         ON_WM_SETFOCUS()
94 END_MESSAGE_MAP()
95 //
96 // =======================================================================================================================
97 //    CNewTexWnd message handlers
98 // =======================================================================================================================
99 //
100 BOOL CNewTexWnd::PreCreateWindow(CREATESTRUCT &cs) {
101         WNDCLASS        wc;
102         HINSTANCE       hInstance = AfxGetInstanceHandle();
103         if (::GetClassInfo(hInstance, TEXTURE_WINDOW_CLASS, &wc) == FALSE) {
104                 // Register a new class
105                 memset(&wc, 0, sizeof(wc));
106                 wc.style = CS_NOCLOSE | CS_PARENTDC;    // | CS_OWNDC;
107                 wc.lpszClassName = TEXTURE_WINDOW_CLASS;
108                 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
109                 wc.lpfnWndProc = ::DefWindowProc;
110                 if (AfxRegisterClass(&wc) == FALSE) {
111                         Error("CNewTexWnd RegisterClass: failed");
112                 }
113         }
114
115         cs.lpszClass = TEXTURE_WINDOW_CLASS;
116         cs.lpszName = "TEX";
117         if (cs.style != QE3_CHILDSTYLE && cs.style != QE3_STYLE) {
118                 cs.style = QE3_SPLITTER_STYLE;
119         }
120
121         return CWnd::PreCreateWindow(cs);
122 }
123
124 /*
125  =======================================================================================================================
126  =======================================================================================================================
127  */
128 int CNewTexWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) {
129         if (CWnd::OnCreate(lpCreateStruct) == -1) {
130                 return -1;
131         }
132
133         ShowScrollBar(SB_VERT, g_PrefsDlg.m_bTextureScrollbar);
134         m_bNeedRange = true;
135
136         hdcTexture = GetDC();
137         QEW_SetupPixelFormat(hdcTexture->m_hDC, false);
138
139         EnableToolTips(TRUE);
140         EnableTrackingToolTips(TRUE);
141
142         return 0;
143 }
144
145 /*
146  =======================================================================================================================
147  =======================================================================================================================
148  */
149 void CNewTexWnd::OnSize(UINT nType, int cx, int cy) {
150         CWnd::OnSize(nType, cx, cy);
151         GetClientRect(rectClient);
152         m_bNeedRange = true;
153 }
154
155 /*
156  =======================================================================================================================
157  =======================================================================================================================
158  */
159 void CNewTexWnd::OnParentNotify(UINT message, LPARAM lParam) {
160         CWnd::OnParentNotify(message, lParam);
161 }
162
163 /*
164  =======================================================================================================================
165  =======================================================================================================================
166  */
167 void CNewTexWnd::UpdatePrefs() {
168         ShowScrollBar(SB_VERT, g_PrefsDlg.m_bTextureScrollbar);
169         m_bNeedRange = true;
170         Invalidate();
171         UpdateWindow();
172 }
173
174 /*
175  =======================================================================================================================
176  =======================================================================================================================
177  */
178 void CNewTexWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
179         g_pParentWnd->HandleKey(nChar, nRepCnt, nFlags);
180 }
181
182 /*
183  =======================================================================================================================
184  =======================================================================================================================
185  */
186 void CNewTexWnd::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) {
187         g_pParentWnd->HandleKey(nChar, nRepCnt, nFlags, false);
188 }
189
190 /*
191  =======================================================================================================================
192  =======================================================================================================================
193  */
194 const idMaterial *CNewTexWnd::NextPos() {
195         const idMaterial *mat = NULL;
196         while (1) {
197                 if (currentIndex >= declManager->GetNumDecls( DECL_MATERIAL )) {
198                         return NULL;
199                 }
200
201                 mat = declManager->MaterialByIndex(currentIndex, false);
202
203                 currentIndex++;
204
205                 //if (mat->getName()[0] == '(') { // fake color texture
206                 //      continue;
207                 //}
208
209                 if ( !mat->IsValid() ) {
210                         continue;
211                 }
212
213                 if (!mat->TestMaterialFlag(MF_EDITOR_VISIBLE)) {
214                         continue;
215                 }
216                 break;
217         }
218
219         // ensure it is uploaded
220         declManager->FindMaterial(mat->GetName());
221
222         int width = mat->GetEditorImage()->uploadWidth * ((float)g_PrefsDlg.m_nTextureScale / 100);
223         int height = mat->GetEditorImage()->uploadHeight * ((float)g_PrefsDlg.m_nTextureScale / 100);
224
225         if (current.x + width > rectClient.Width() - 8 && currentRow) {
226                 // go to the next row unless the texture is the first on the row
227                 current.x = 8;
228                 current.y -= currentRow + FONT_HEIGHT + 4;
229                 currentRow = 0;
230         }
231
232         draw = current;
233
234         // Is our texture larger than the row? If so, grow the row height to match it
235         if (currentRow < height) {
236                 currentRow = height;
237         }
238
239         // never go less than 64, or the names get all crunched up
240         current.x += width < 64 ? 64 : width;
241         current.x += 8;
242         return mat;
243 }
244
245 /*
246  =======================================================================================================================
247  =======================================================================================================================
248  */
249 void CNewTexWnd::OnPaint() {
250
251         CPaintDC        dc(this);       // device context for painting
252
253         int nOld = g_qeglobals.d_texturewin.m_nTotalHeight;
254
255         //hdcTexture = GetDC();
256         if (!qwglMakeCurrent(dc.GetSafeHdc(), win32.hGLRC)) {
257                 common->Printf("ERROR: wglMakeCurrent failed..\n ");
258         }
259         else {
260                 const char      *name;
261                 qglClearColor
262                 (
263                         g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][0],
264                         g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][1],
265                         g_qeglobals.d_savedinfo.colors[COLOR_TEXTUREBACK][2],
266                         0
267                 );
268                 qglViewport(0, 0, rectClient.Width(), rectClient.Height());
269                 qglScissor(0, 0, rectClient.Width(), rectClient.Height());
270                 qglMatrixMode(GL_PROJECTION);
271                 qglLoadIdentity();
272                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
273                 qglDisable(GL_DEPTH_TEST);
274                 qglDisable(GL_BLEND);
275                 qglOrtho(0, rectClient.Width(), origin.y - rectClient.Height(), origin.y, -100, 100);
276                 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
277
278                 // init stuff
279                 current.x = 8;
280                 current.y = -8;
281                 currentRow = 0;
282                 currentIndex = 0;
283                 while (1) {
284                         const idMaterial *mat = NextPos();
285                         if (mat == NULL) {
286                                 break;
287                         }
288
289                         int width = mat->GetEditorImage()->uploadWidth * ((float)g_PrefsDlg.m_nTextureScale / 100);
290                         int height = mat->GetEditorImage()->uploadHeight * ((float)g_PrefsDlg.m_nTextureScale / 100);
291
292                         // Is this texture visible?
293                         if ((draw.y - height - FONT_HEIGHT < origin.y) && (draw.y > origin.y - rectClient.Height())) {
294                                 // if in use, draw a background
295                                 qglLineWidth(1);
296                                 qglColor3f(1, 1, 1);
297                                 globalImages->BindNull();
298                                 qglBegin(GL_LINE_LOOP);
299                                 qglVertex2f(draw.x - 1, draw.y + 1 - FONT_HEIGHT);
300                                 qglVertex2f(draw.x - 1, draw.y - height - 1 - FONT_HEIGHT);
301                                 qglVertex2f(draw.x + 1 + width, draw.y - height - 1 - FONT_HEIGHT);
302                                 qglVertex2f(draw.x + 1 + width, draw.y + 1 - FONT_HEIGHT);
303                                 qglEnd();
304
305                                 // Draw the texture
306                                 float   fScale = (g_PrefsDlg.m_bHiColorTextures == TRUE) ? ((float)g_PrefsDlg.m_nTextureScale / 100) : 1.0;
307
308                                 mat->GetEditorImage()->Bind();
309                                 QE_CheckOpenGLForErrors();
310                                 qglColor3f(1, 1, 1);
311                                 qglBegin(GL_QUADS);
312                                 qglTexCoord2f(0, 0);
313                                 qglVertex2f(draw.x, draw.y - FONT_HEIGHT);
314                                 qglTexCoord2f(1, 0);
315                                 qglVertex2f(draw.x + width, draw.y - FONT_HEIGHT);
316                                 qglTexCoord2f(1, 1);
317                                 qglVertex2f(draw.x + width, draw.y - FONT_HEIGHT - height);
318                                 qglTexCoord2f(0, 1);
319                                 qglVertex2f(draw.x, draw.y - FONT_HEIGHT - height);
320                                 qglEnd();
321
322                                 // draw the selection border
323                                 if ( !idStr::Icmp(g_qeglobals.d_texturewin.texdef.name, mat->GetName()) ) {
324                                         qglLineWidth(3);
325                                         qglColor3f(1, 0, 0);
326                                         globalImages->BindNull();
327
328                                         qglBegin(GL_LINE_LOOP);
329                                         qglVertex2f(draw.x - 4, draw.y - FONT_HEIGHT + 4);
330                                         qglVertex2f(draw.x - 4, draw.y - FONT_HEIGHT - height - 4);
331                                         qglVertex2f(draw.x + 4 + width, draw.y - FONT_HEIGHT - height - 4);
332                                         qglVertex2f(draw.x + 4 + width, draw.y - FONT_HEIGHT + 4);
333                                         qglEnd();
334
335                                         qglLineWidth(1);
336                                 }
337
338                                 // draw the texture name
339                                 globalImages->BindNull();
340                                 qglColor3f(1, 1, 1);
341                                 qglRasterPos2f(draw.x, draw.y - FONT_HEIGHT + 2);
342
343                                 // don't draw the directory name
344                                 for (name = mat->GetName(); *name && *name != '/' && *name != '\\'; name++) {
345                                         ;
346                                 }
347
348                                 if (!*name) {
349                                         name = mat->GetName();
350                                 }
351                                 else {
352                                         name++;
353                                 }
354                                 qglCallLists(strlen(name), GL_UNSIGNED_BYTE, name);
355                                 //qglCallLists(va("%s -- %d, %d" strlen(name), GL_UNSIGNED_BYTE, name);
356                         } 
357                 }
358
359                 g_qeglobals.d_texturewin.m_nTotalHeight = abs(draw.y) + 100;
360
361                 // reset the current texture
362                 globalImages->BindNull();
363                 qglFinish();
364                 qwglSwapBuffers(dc.GetSafeHdc());
365                 TRACE("Texture Paint\n");
366         }
367
368         if (g_PrefsDlg.m_bTextureScrollbar && (m_bNeedRange || g_qeglobals.d_texturewin.m_nTotalHeight != nOld)) {
369                 m_bNeedRange = false;
370                 SetScrollRange(SB_VERT, 0, g_qeglobals.d_texturewin.m_nTotalHeight, TRUE);
371         }
372
373         //ReleaseDC(hdcTexture);
374 }
375
376 /*
377  =======================================================================================================================
378  =======================================================================================================================
379  */
380 void CNewTexWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) {
381         CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
382
383         int n = GetScrollPos(SB_VERT);
384         switch (nSBCode)
385         {
386                 case SB_LINEUP: {
387                                 n = (n - 15 > 0) ? n - 15 : 0;
388                                 break;
389                         }
390
391                 case SB_LINEDOWN: {
392                                 n = (n + 15 < g_qeglobals.d_texturewin.m_nTotalHeight) ? n + 15 : n;
393                                 break;
394                         }
395
396                 case SB_PAGEUP: {
397                                 n = (n - g_qeglobals.d_texturewin.height > 0) ? n - g_qeglobals.d_texturewin.height : 0;
398                                 break;
399                         }
400
401                 case SB_PAGEDOWN: {
402                                 n = (n + g_qeglobals.d_texturewin.height < g_qeglobals.d_texturewin.m_nTotalHeight) ? n + g_qeglobals.d_texturewin.height : n;
403                                 break;
404                         }
405
406                 case SB_THUMBPOSITION: {
407                                 n = nPos;
408                                 break;
409                         }
410
411                 case SB_THUMBTRACK: {
412                                 n = nPos;
413                                 break;
414                         }
415         }
416
417         SetScrollPos(SB_VERT, n);
418         origin.y = -n;
419         Invalidate();
420         UpdateWindow();
421
422         // Sys_UpdateWindows(W_TEXTURE);
423 }
424
425 BOOL CNewTexWnd::DestroyWindow() {
426         ReleaseDC(hdcTexture);
427         return CWnd::DestroyWindow();
428 }
429
430 /*
431  =======================================================================================================================
432  =======================================================================================================================
433  */                                                 
434 BOOL CNewTexWnd::Create
435 (
436         LPCTSTR                 lpszClassName,
437         LPCTSTR                 lpszWindowName,
438         DWORD                   dwStyle,
439         const RECT              &rect,
440         CWnd                    *pParentWnd,
441         UINT                    nID,
442         CCreateContext  *pContext
443 ) {
444         BOOL    ret = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
445         if (ret) {
446                 hdcTexture = GetDC();
447                 QEW_SetupPixelFormat(hdcTexture->m_hDC, false);
448         }
449
450         return ret;
451 }
452
453 const idMaterial *CNewTexWnd::getMaterialAtPoint(CPoint point) {
454
455         // init stuff
456         int my = rectClient.Height() - 1 - point.y;
457         my += origin.y - rectClient.Height();
458
459         current.x = 8;
460         current.y = -8;
461         currentRow = 0;
462         currentIndex = 0;
463
464         while (1) {
465                 const idMaterial *mat = NextPos();
466                 if (mat == NULL) {
467                         return NULL;
468                 }
469
470                 int width = mat->GetEditorImage()->uploadWidth * ((float)g_PrefsDlg.m_nTextureScale / 100);
471                 int height = mat->GetEditorImage()->uploadHeight * ((float)g_PrefsDlg.m_nTextureScale / 100);
472                 //if (point.x > draw.x && point.x - draw.x < width && my < draw.y && my + draw.y < height + FONT_HEIGHT) {
473                 if (point.x > draw.x && point.x - draw.x < width && my < draw.y &&  draw.y - my < height + FONT_HEIGHT) {
474                         return mat;
475                 }
476         
477         }
478
479 }
480 /*
481  =======================================================================================================================
482  =======================================================================================================================
483  */
484 void CNewTexWnd::OnLButtonDown(UINT nFlags, CPoint point) {
485         cursor = point;
486
487         SetFocus();
488         bool fitScale = Sys_KeyDown(VK_CONTROL);
489         bool edit = Sys_KeyDown(VK_SHIFT) && !fitScale;
490
491         const idMaterial *mat = getMaterialAtPoint(point);
492         if (mat) {
493                 Select_SetDefaultTexture(mat, fitScale, true);
494         } else {
495                 Sys_Status("Did not select a texture\n", 0);
496         }
497
498         //
499         UpdateSurfaceDialog();
500         UpdatePatchInspector();
501 }
502
503 /*
504  =======================================================================================================================
505  =======================================================================================================================
506  */
507 void CNewTexWnd::OnMButtonDown(UINT nFlags, CPoint point) {
508         CWnd::OnMButtonDown(nFlags, point);
509
510 }
511
512 /*
513  =======================================================================================================================
514  =======================================================================================================================
515  */
516 void CNewTexWnd::OnRButtonDown(UINT nFlags, CPoint point) {
517         cursor = point;
518         SetFocus();
519 }
520
521 /*
522  ===============================t========================================================================================
523  =======================================================================================================================
524  */
525 void CNewTexWnd::OnLButtonUp(UINT nFlags, CPoint point) {
526         CWnd::OnLButtonUp(nFlags, point);
527         g_pParentWnd->SetFocus();
528 }
529
530 /*
531  =======================================================================================================================
532  =======================================================================================================================
533  */
534 void CNewTexWnd::OnMButtonUp(UINT nFlags, CPoint point) {
535         CWnd::OnMButtonUp(nFlags, point);
536 }
537
538 /*
539  =======================================================================================================================
540  =======================================================================================================================
541  */
542 void CNewTexWnd::OnRButtonUp(UINT nFlags, CPoint point) {
543         CWnd::OnRButtonUp(nFlags, point);
544 }
545
546 extern float    fDiff(float f1, float f2);
547
548 /*
549  =======================================================================================================================
550  =======================================================================================================================
551  */
552 void CNewTexWnd::OnMouseMove(UINT nFlags, CPoint point) {
553         int scale = 1;
554
555         if (Sys_KeyDown(VK_SHIFT)) {
556                 scale = 4;
557         }
558
559         // rbutton = drag texture origin
560         if (Sys_KeyDown(VK_RBUTTON)) {
561                 if (point.y != cursor.y) {
562                         if (Sys_KeyDown(VK_MENU)) {
563                                 long    *px = &point.x;
564                                 long    *px2 = &cursor.x;
565
566                                 if (fDiff(point.y, cursor.y) > fDiff(point.x, cursor.x)) {
567                                         px = &point.y;
568                                         px2 = &cursor.y;
569                                 }
570
571                                 if (*px > *px2) {
572                                         // zoom in
573                                         g_PrefsDlg.m_nTextureScale += 4;
574                                         if (g_PrefsDlg.m_nTextureScale > 500) {
575                                                 g_PrefsDlg.m_nTextureScale = 500;
576                                         }
577                                 }
578                                 else if (*px < *px2) {
579                                         // zoom out
580                                         g_PrefsDlg.m_nTextureScale -= 4;
581                                         if (g_PrefsDlg.m_nTextureScale < 1) {
582                                                 g_PrefsDlg.m_nTextureScale = 1;
583                                         }
584                                 }
585
586                                 *px2 = *px;
587                                 CPoint screen = cursor;
588                                 ClientToScreen(&screen);
589                                 SetCursorPos(screen.x, screen.y);
590                                 //Sys_SetCursorPos(cursor.x, cursor.y);
591                                 InvalidateRect(NULL, false);
592                                 UpdateWindow();
593                         }
594                         else if (point.y != cursor.y || point.x != cursor.x) {
595                                 origin.y += (point.y - cursor.y) * scale;
596                                 if (origin.y > 0) {
597                                         origin.y = 0;
598                                 }
599
600                                 //Sys_SetCursorPos(cursor.x, cursor.y);
601                                 CPoint screen = cursor;
602                                 ClientToScreen(&screen);
603                                 SetCursorPos(screen.x, screen.y);
604                                 if (g_PrefsDlg.m_bTextureScrollbar) {
605                                         SetScrollPos(SB_VERT, abs(origin.y));
606                                 }
607
608                                 InvalidateRect(NULL, false);
609                                 UpdateWindow();
610                         }
611                 }
612
613                 return;
614         }
615 }
616
617 /*
618  =======================================================================================================================
619  =======================================================================================================================
620  */
621 void CNewTexWnd::LoadMaterials() {
622 }
623
624
625 void Texture_SetTexture(texdef_t *texdef, brushprimit_texdef_t  *brushprimit_texdef, bool bFitScale, bool bSetSelection) {
626         
627         if (texdef->name[0] == '(') {
628                 Sys_Status("Can't select an entity texture\n", 0);
629                 return;
630         }
631
632         g_qeglobals.d_texturewin.texdef = *texdef;
633
634         //
635         // store the texture coordinates for new brush primitive mode be sure that all the
636         // callers are using the default 2x2 texture
637         //
638         if (g_qeglobals.m_bBrushPrimitMode) {
639                 g_qeglobals.d_texturewin.brushprimit_texdef = *brushprimit_texdef;
640         }
641
642         g_dlgFind.updateTextures(texdef->name);
643         
644         if (!g_dlgFind.isOpen() && bSetSelection) {
645                 Select_SetTexture(texdef, brushprimit_texdef, bFitScale);
646         }
647
648         g_Inspectors->texWnd.EnsureTextureIsVisible(texdef->name);
649
650         if ( g_Inspectors->mediaDlg.IsWindowVisible() ) {
651                 g_Inspectors->mediaDlg.SelectCurrentItem(true, g_qeglobals.d_texturewin.texdef.name, CDialogTextures::MATERIALS);
652         }
653
654         g_qeglobals.d_texturewin.texdef = *texdef;
655         // store the texture coordinates for new brush primitive mode be sure that all the
656         // callers are using the default 2x2 texture
657         //
658         if (g_qeglobals.m_bBrushPrimitMode) {
659                 g_qeglobals.d_texturewin.brushprimit_texdef = *brushprimit_texdef;
660         }
661
662
663         Sys_UpdateWindows(W_TEXTURE);
664
665
666 }
667
668 const idMaterial *Texture_LoadLight(const char *name) {
669         return declManager->FindMaterial(name);
670 }
671
672
673 void Texture_ClearInuse(void) {
674 }
675
676 void Texture_ShowAll(void) {
677         int count = declManager->GetNumDecls( DECL_MATERIAL );
678         for (int i = 0; i < count; i++) {
679                 const idMaterial *mat = declManager->MaterialByIndex(i, false);
680                 if ( mat ) {
681                         mat->SetMaterialFlag(MF_EDITOR_VISIBLE);
682                 }
683         }
684         g_Inspectors->SetWindowText("Textures (all)");
685         Sys_UpdateWindows(W_TEXTURE);
686 }
687
688 void Texture_HideAll() {
689         int count = declManager->GetNumDecls( DECL_MATERIAL );
690         for (int i = 0; i < count; i++) {
691                 const idMaterial *mat = declManager->MaterialByIndex(i, false);
692                 if ( mat ) {
693                         mat->ClearMaterialFlag(MF_EDITOR_VISIBLE);
694                 }
695         }
696         g_Inspectors->SetWindowText("Textures (all)");
697         Sys_UpdateWindows(W_TEXTURE);
698 }
699
700 const idMaterial *Texture_ForName(const char *name) {
701         const idMaterial *mat = declManager->FindMaterial(name);
702         if ( !mat ) {
703                 mat = declManager->FindMaterial("_default");
704         } else {
705                 mat->SetMaterialFlag(MF_EDITOR_VISIBLE);
706         }
707         return mat;
708 }
709
710 void Texture_ShowInuse(void) {
711         Texture_HideAll();
712
713         brush_t *b;
714         for (b = active_brushes.next; b != NULL && b != &active_brushes; b = b->next) {
715                 if (b->pPatch) {
716                         Texture_ForName(b->pPatch->d_texture->GetName());
717                 } else {
718                         for (face_t *f = b->brush_faces; f; f = f->next) {
719                                 Texture_ForName(f->texdef.name);
720                         }
721                 }
722         }
723
724         for (b = selected_brushes.next; b != NULL && b != &selected_brushes; b = b->next) {
725                 if (b->pPatch) {
726                         Texture_ForName(b->pPatch->d_texture->GetName());
727                 } else {
728                         for (face_t *f = b->brush_faces; f; f = f->next) {
729                                 Texture_ForName(f->texdef.name);
730                         }
731                 }
732         }
733
734         Sys_UpdateWindows(W_TEXTURE);
735
736         g_Inspectors->SetWindowText("Textures (in use)");
737 }
738
739 void Texture_Cleanup(CStringList *pList) {
740 }
741
742 int                             texture_mode = GL_LINEAR_MIPMAP_LINEAR;
743 bool texture_showinuse = true;
744
745
746 /*
747  =======================================================================================================================
748     Texture_SetMode
749  =======================================================================================================================
750  */
751 void Texture_SetMode(int iMenu) {
752         int             iMode;
753         HMENU   hMenu;
754         bool    texturing = true;
755
756         hMenu = GetMenu(g_pParentWnd->GetSafeHwnd());
757
758         switch (iMenu)
759         {
760                 case ID_VIEW_NEAREST:
761                         iMode = GL_NEAREST;
762                         break;
763                 case ID_VIEW_NEARESTMIPMAP:
764                         iMode = GL_NEAREST_MIPMAP_NEAREST;
765                         break;
766                 case ID_VIEW_LINEAR:
767                         iMode = GL_NEAREST_MIPMAP_LINEAR;
768                         break;
769                 case ID_VIEW_BILINEAR:
770                         iMode = GL_LINEAR;
771                         break;
772                 case ID_VIEW_BILINEARMIPMAP:
773                         iMode = GL_LINEAR_MIPMAP_NEAREST;
774                         break;
775                 case ID_VIEW_TRILINEAR:
776                         iMode = GL_LINEAR_MIPMAP_LINEAR;
777                         break;
778
779                 case ID_TEXTURES_WIREFRAME:
780                         iMode = 0;
781                         texturing = false;
782                         break;
783
784                 case ID_TEXTURES_FLATSHADE:
785                 default:
786                         iMode = 0;
787                         texturing = false;
788                         break;
789         }
790
791         CheckMenuItem(hMenu, ID_VIEW_NEAREST, MF_BYCOMMAND | MF_UNCHECKED);
792         CheckMenuItem(hMenu, ID_VIEW_NEARESTMIPMAP, MF_BYCOMMAND | MF_UNCHECKED);
793         CheckMenuItem(hMenu, ID_VIEW_LINEAR, MF_BYCOMMAND | MF_UNCHECKED);
794         CheckMenuItem(hMenu, ID_VIEW_BILINEARMIPMAP, MF_BYCOMMAND | MF_UNCHECKED);
795         CheckMenuItem(hMenu, ID_VIEW_BILINEAR, MF_BYCOMMAND | MF_UNCHECKED);
796         CheckMenuItem(hMenu, ID_VIEW_TRILINEAR, MF_BYCOMMAND | MF_UNCHECKED);
797         CheckMenuItem(hMenu, ID_TEXTURES_WIREFRAME, MF_BYCOMMAND | MF_UNCHECKED);
798         CheckMenuItem(hMenu, ID_TEXTURES_FLATSHADE, MF_BYCOMMAND | MF_UNCHECKED);
799
800         CheckMenuItem(hMenu, iMenu, MF_BYCOMMAND | MF_CHECKED);
801
802         g_qeglobals.d_savedinfo.iTexMenu = iMenu;
803         texture_mode = iMode;
804
805         if (!texturing && iMenu == ID_TEXTURES_WIREFRAME) {
806                 g_pParentWnd->GetCamera()->Camera().draw_mode = cd_wire;
807                 Map_BuildBrushData();
808                 Sys_UpdateWindows(W_ALL);
809                 return;
810         }
811         else if (!texturing && iMenu == ID_TEXTURES_FLATSHADE) {
812                 g_pParentWnd->GetCamera()->Camera().draw_mode = cd_solid;
813                 Map_BuildBrushData();
814                 Sys_UpdateWindows(W_ALL);
815                 return;
816         }
817
818         if (g_pParentWnd->GetCamera()->Camera().draw_mode != cd_texture) {
819                 g_pParentWnd->GetCamera()->Camera().draw_mode = cd_texture;
820                 Map_BuildBrushData();
821         }
822
823         Sys_UpdateWindows(W_ALL);
824 }
825
826
827
828 void CNewTexWnd::EnsureTextureIsVisible(const char *name) {
829         // scroll origin so the texture is completely on screen
830         // init stuff
831         current.x = 8;
832         current.y = -8;
833         currentRow = 0;
834         currentIndex = 0;
835
836         while (1) {
837                 const idMaterial *mat = NextPos();
838                 if (mat == NULL) {
839                         break;
840                 }
841
842                 int width = mat->GetEditorImage()->uploadWidth * ((float)g_PrefsDlg.m_nTextureScale / 100);
843                 int height = mat->GetEditorImage()->uploadHeight * ((float)g_PrefsDlg.m_nTextureScale / 100);
844
845                 if ( !idStr::Icmp(name, mat->GetName()) ) {
846                         if (current.y > origin.y) {
847                                 origin.y = current.y;
848                                 Sys_UpdateWindows(W_TEXTURE);
849                                 return;
850                         }
851
852                         if (current.y - height - 2 * FONT_HEIGHT < origin.y - rectClient.Height()) {
853                                 origin.y = current.y - height - 2 * FONT_HEIGHT + rectClient.Height();
854                                 Sys_UpdateWindows(W_TEXTURE);
855                                 return;
856                         }
857
858                         return;
859                 }
860         }
861
862 }
863
864
865 BOOL CNewTexWnd::OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult ) {
866         static char tip[1024];
867         CPoint point;
868         GetCursorPos(&point);
869         const idMaterial *mat = getMaterialAtPoint(point);
870
871         if (mat) {
872             TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
873                 strcpy(tip, mat->GetDescription());
874             pTTT->lpszText = tip;
875             pTTT->hinst = NULL;
876             return(TRUE);
877     }
878     return(FALSE);
879 }
880
881 int CNewTexWnd::OnToolHitTest(CPoint point, TOOLINFO * pTI)
882 {
883         const idMaterial *mat = getMaterialAtPoint(point);
884         if (mat) {
885                 return 0;
886         }
887         return -1;
888 }
889
890 BOOL CNewTexWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
891 {
892         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
893         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
894         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
895         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
896         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
897         OnVScroll((zDelta >= 0) ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
898         return TRUE;
899 }
900
901 BOOL CNewTexWnd::PreTranslateMessage(MSG* pMsg)
902 {
903         if (pMsg->message == WM_KEYDOWN) {
904                 if (pMsg->wParam == VK_ESCAPE) {
905                         g_pParentWnd->GetCamera()->SetFocus();
906                         Select_Deselect();
907                         return TRUE;
908                 }
909                 if (pMsg->wParam == VK_RIGHT || pMsg->wParam == VK_LEFT || pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN) {
910                         g_pParentWnd->PostMessage(WM_KEYDOWN, pMsg->wParam);
911                         return TRUE;
912                 }
913         }
914         return CWnd::PreTranslateMessage(pMsg);
915 }
916
917 void CNewTexWnd::OnSetFocus(CWnd* pOldWnd)
918 {
919         CWnd::OnSetFocus(pOldWnd);
920         Invalidate();
921         RedrawWindow();
922 }