]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/select.cpp
Merge pull request #21 from merlin1991/Q3-gamepack-fix
[xonotic/netradiant.git] / radiant / select.cpp
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 // select.c
23 #include "stdafx.h"
24 #include <assert.h>
25 #include "filters.h"
26
27 // externs
28 CPtrArray g_SelectedFaces;
29 CPtrArray g_SelectedFaceBrushes;
30 CPtrArray& g_ptrSelectedFaces = g_SelectedFaces;
31 CPtrArray& g_ptrSelectedFaceBrushes = g_SelectedFaceBrushes;
32
33 /*
34    ===========
35    Test_Ray
36    ===========
37  */
38 #define DIST_START  999999
39 trace_t Test_Ray( vec3_t origin, vec3_t dir, int flags ){
40         brush_t *brush;
41         face_t  *face;
42         float dist;
43         trace_t t;
44
45         memset( &t, 0, sizeof( t ) );
46         t.dist = DIST_START;
47
48         if ( flags & SF_CYCLE ) {
49                 CPtrArray array;
50                 brush_t *pToSelect = ( selected_brushes.next != &selected_brushes ) ? selected_brushes.next : NULL;
51                 Select_Deselect();
52
53                 // go through active brushes and accumulate all "hit" brushes
54                 for ( brush = active_brushes.next ; brush != &active_brushes ; brush = brush->next )
55                 {
56                         //if ( (flags & SF_ENTITIES_FIRST) && brush->owner == world_entity)
57                         //  continue;
58
59                         if ( brush->bFiltered ) {
60                                 continue;
61                         }
62
63                         if ( !g_PrefsDlg.m_bSelectCurves && brush->patchBrush ) {
64                                 continue;
65                         }
66
67                         if ( !g_PrefsDlg.m_bSelectModels && ( brush->owner->eclass->nShowFlags & ECLASS_MISCMODEL ) ) {
68                                 continue;
69                         }
70
71                         //if (!g_bShowPatchBounds && brush->patchBrush)
72                         //  continue;
73
74                         face = Brush_Ray( origin, dir, brush, &dist, flags );
75
76                         if ( face ) {
77                                 array.Add( brush );
78                         }
79                 }
80
81                 int nSize = array.GetSize();
82                 if ( nSize > 0 ) {
83                         bool bFound = false;
84                         for ( int i = 0; i < nSize; i++ )
85                         {
86                                 brush_t *b = reinterpret_cast<brush_t*>( array.GetAt( i ) );
87                                 // did we hit the last one selected yet ?
88                                 if ( b == pToSelect ) {
89                                         // yes we want to select the next one in the list
90                                         int n = ( i > 0 ) ? i - 1 : nSize - 1;
91                                         pToSelect = reinterpret_cast<brush_t*>( array.GetAt( n ) );
92                                         bFound = true;
93                                         break;
94                                 }
95                         }
96                         if ( !bFound ) {
97                                 pToSelect = reinterpret_cast<brush_t*>( array.GetAt( 0 ) );
98                         }
99                 }
100                 if ( pToSelect ) {
101                         face = Brush_Ray( origin, dir, pToSelect, &dist, flags );
102                         t.dist = dist;
103                         t.brush = pToSelect;
104                         t.face = face;
105                         t.selected = false;
106                         return t;
107                 }
108         }
109
110         if ( !( flags & SF_SELECTED_ONLY ) ) {
111                 for ( brush = active_brushes.next ; brush != &active_brushes ; brush = brush->next )
112                 {
113                         if ( ( flags & SF_ENTITIES_FIRST ) && ( brush->owner == world_entity || !brush->owner->eclass->fixedsize ) ) {
114                                 continue;
115                         }
116
117                         if ( brush->bFiltered ) {
118                                 continue;
119                         }
120
121                         if ( !g_PrefsDlg.m_bSelectCurves && brush->patchBrush ) {
122                                 continue;
123                         }
124
125                         if ( !g_PrefsDlg.m_bSelectModels && ( brush->owner->eclass->nShowFlags & ECLASS_MISCMODEL ) ) {
126                                 continue;
127                         }
128
129                         //if (!g_bShowPatchBounds && brush->patchBrush)
130                         //  continue;
131
132                         face = Brush_Ray( origin, dir, brush, &dist, flags );
133                         if ( face && dist > 0 && dist < t.dist ) {
134                                 t.dist = dist;
135                                 t.brush = brush;
136                                 t.face = face;
137                                 t.selected = false;
138                         }
139                 }
140         }
141
142
143         for ( brush = selected_brushes.next ; brush != &selected_brushes ; brush = brush->next )
144         {
145                 if ( ( flags & SF_ENTITIES_FIRST ) && ( brush->owner == world_entity || !brush->owner->eclass->fixedsize ) ) {
146                         continue;
147                 }
148
149                 if ( brush->bFiltered ) {
150                         continue;
151                 }
152
153                 if ( !g_PrefsDlg.m_bSelectCurves && brush->patchBrush ) {
154                         continue;
155                 }
156
157                 if ( !g_PrefsDlg.m_bSelectModels && ( brush->owner->eclass->nShowFlags & ECLASS_MISCMODEL ) ) {
158                         continue;
159                 }
160
161                 face = Brush_Ray( origin, dir, brush, &dist, flags );
162                 if ( dist > 0 && dist < t.dist ) {
163                         t.dist = dist;
164                         t.brush = brush;
165                         t.face = face;
166                         t.selected = true;
167                 }
168         }
169
170         // if entites first, but didn't find any, check regular
171
172         if ( ( flags & SF_ENTITIES_FIRST ) && t.brush == NULL ) {
173                 return Test_Ray( origin, dir, flags & ~SF_ENTITIES_FIRST );
174         }
175
176         return t;
177
178 }
179
180
181 /*
182    ============
183    Select_Brush
184
185    ============
186  */
187 void Select_Brush( brush_t *brush, bool bComplete, bool bStatus ){
188         brush_t *b;
189         entity_t    *e;
190
191         g_ptrSelectedFaces.RemoveAll();
192         g_ptrSelectedFaceBrushes.RemoveAll();
193         if ( g_qeglobals.d_select_count < 2 ) {
194                 g_qeglobals.d_select_order[g_qeglobals.d_select_count] = brush;
195         }
196         g_qeglobals.d_select_count++;
197
198         e = brush->owner;
199         if ( e ) {
200                 // select complete entity on first click
201                 if ( e != world_entity && bComplete == true ) {
202                         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
203                                 if ( b->owner == e ) {
204                                         goto singleselect;
205                                 }
206                         for ( b = e->brushes.onext ; b != &e->brushes ; b = b->onext )
207                         {
208                                 if ( b == brush ) { // make sure we add the actual selected brush last, mainly for cycle select
209                                         continue;
210                                 }
211                                 Brush_RemoveFromList( b );
212                                 Brush_AddToList( b, &selected_brushes );
213                         }
214                         Brush_RemoveFromList( brush );
215                         Brush_AddToList( brush, &selected_brushes );
216                 }
217                 else
218                 {
219 singleselect:
220                         Brush_RemoveFromList( brush );
221                         Brush_AddToList( brush, &selected_brushes );
222                         UpdatePatchInspector();
223                 }
224
225                 if ( e->eclass ) {
226                         UpdateEntitySel( brush->owner->eclass );
227                 }
228
229                 UpdateSurfaceDialog();
230         }
231
232         if ( bStatus ) {
233                 vec3_t vMin, vMax, vSize;
234                 Select_GetBounds( vMin, vMax );
235                 VectorSubtract( vMax, vMin, vSize );
236                 CString strStatus;
237                 strStatus.Format( "Selection X:: %.1f  Y:: %.1f  Z:: %.1f", vSize[0], vSize[1], vSize[2] );
238                 g_pParentWnd->SetStatusText( 2, strStatus );
239         }
240 }
241
242 /*
243    =============
244    Select_FaceInSelectedBrushes
245    =============
246  */
247 bool Select_FaceInSelectedBrushes( face_t *face ){
248         brush_t *brush;
249         face_t  *tface;
250
251         for ( brush = selected_brushes.next; brush != &selected_brushes; brush = brush->next )
252         {
253                 for ( tface = brush->brush_faces; tface; tface = tface->next )
254                 {
255                         if ( tface == face ) {
256                                 return true;
257                         }
258                 }
259         }
260
261         return false;
262 }
263
264 /*
265    ============
266    Select_Ray
267
268    If the origin is inside a brush, that brush will be ignored.
269    ============
270  */
271 void Select_Ray( vec3_t origin, vec3_t dir, int flags ){
272         trace_t t;
273         face_t  *tface;
274         bool bOk;
275         static trace_t lastTrace = {
276                 NULL, //        brush
277                 NULL, //        face
278                 0,  //  dist
279                 false //        selected
280         };
281
282         t = Test_Ray( origin, dir, flags );
283         if ( !t.brush ) {
284                 return;
285         }
286
287         if ( flags & SF_SINGLEFACE ) {
288                 if ( flags & SF_DRAG ) {
289                         if ( t.brush == lastTrace.brush && t.face == lastTrace.face ) {
290                                 return;
291                         }
292                 }
293                 lastTrace = t;
294
295                 if ( Select_FaceInSelectedBrushes( t.face ) ) {
296                         // Deselect the brush
297                         Brush_RemoveFromList( t.brush );
298                         Brush_AddToList( t.brush, &active_brushes );
299                         UpdatePatchInspector();
300
301                         // Select all of the brush's faces except the one we are pointing at
302                         for ( tface = t.brush->brush_faces; tface; tface = tface->next )
303                         {
304                                 if ( tface == t.face ) {
305                                         continue;
306                                 }
307
308                                 bOk = true;
309                                 // NOTE: keep the size check in the loop, we remove stuff inside
310                                 for ( int i = 0; i < g_SelectedFaces.GetSize(); i++ )
311                                 {
312                                         if ( tface == reinterpret_cast<face_t*>( g_SelectedFaces.GetAt( i ) ) ) {
313                                                 bOk = false;
314                                         }
315                                 }
316
317                                 if ( bOk ) {
318                                         g_SelectedFaces.Add( tface );
319                                         g_SelectedFaceBrushes.Add( t.brush );
320                                 }
321                         }
322                         g_qeglobals.d_select_mode = sel_facets_off;
323                 }
324                 else
325                 {
326                         bOk = true;
327                         // NOTE: keep the size check in the loop, we remove stuff inside
328                         for ( int i = 0; i < g_SelectedFaces.GetSize(); i++ )
329                         {
330                                 if ( t.face == reinterpret_cast<face_t*>( g_SelectedFaces.GetAt( i ) ) ) {
331                                         bOk = false;
332                                         if ( flags & SF_DRAG_ON ) {
333                                                 continue;
334                                         }
335
336                                         g_qeglobals.d_select_mode = sel_facets_off;
337                                         // need to remove i'th entry
338                                         g_SelectedFaces.RemoveAt( i, 1 );
339                                         g_SelectedFaceBrushes.RemoveAt( i, 1 );
340                                 }
341                         }
342
343                         if ( bOk && !( flags & SF_DRAG_OFF ) ) {
344                                 g_SelectedFaces.Add( t.face );
345                                 g_SelectedFaceBrushes.Add( t.brush );
346                                 g_qeglobals.d_select_mode = sel_facets_on;
347                         }
348                 }
349                 UpdateSurfaceDialog();
350                 Sys_UpdateWindows( W_ALL );
351                 //g_qeglobals.d_select_mode = sel_brush;
352                 // Texture_SetTexture requires a brushprimit_texdef fitted to the default width=2 height=2 texture
353                 brushprimit_texdef_t brushprimit_texdef;
354                 ConvertTexMatWithQTexture( &t.face->brushprimit_texdef, t.face->d_texture, &brushprimit_texdef, NULL );
355                 Texture_SetTexture( &t.face->texdef, &brushprimit_texdef, false, NULL, false );
356                 return;
357         }
358
359         // move the brush to the other list
360         if ( t.selected ) {
361                 if ( flags & SF_DRAG_ON ) {
362                         return;
363                 }
364
365                 g_qeglobals.d_select_mode = sel_brush_off;
366                 Brush_RemoveFromList( t.brush );
367                 Brush_AddToList( t.brush, &active_brushes );
368
369                 UpdatePatchInspector();
370         }
371         else
372         {
373                 if ( flags & SF_DRAG_OFF ) {
374                         return;
375                 }
376
377                 g_qeglobals.d_select_mode = sel_brush_on;
378                 Select_Brush( t.brush, g_PrefsDlg.m_nCamDragMultiSelect == 1 ? Sys_AltDown() : !Sys_AltDown() );
379         }
380         UpdateSurfaceDialog();
381         Sys_UpdateWindows( W_ALL );
382 }
383
384
385 void Select_Delete( void ){
386         brush_t *brush;
387         entity_t *e;
388
389         g_ptrSelectedFaces.RemoveAll();
390         g_ptrSelectedFaceBrushes.RemoveAll();
391
392         g_qeglobals.d_select_mode = sel_brush;
393
394         g_qeglobals.d_select_count = 0;
395         g_qeglobals.d_num_move_points = 0;
396         while ( selected_brushes.next != &selected_brushes )
397         {
398                 brush = selected_brushes.next;
399                 if ( brush->patchBrush ) {
400                         Patch_Delete( brush->pPatch );
401                 }
402                 e = brush->owner;
403                 Brush_Free( brush );
404                 // remove if no brushes
405                 if ( e != world_entity && e->brushes.onext == &e->brushes ) {
406                         Entity_Free( e );
407                 }
408         }
409
410         Sys_MarkMapModified();
411         UpdateSurfaceDialog();
412         Sys_UpdateWindows( W_ALL );
413 }
414
415 // update the workzone to a given brush
416 void UpdateWorkzone_ForBrush( brush_t* b ){
417         VectorCopy( b->mins, g_qeglobals.d_work_min );
418         VectorCopy( b->maxs, g_qeglobals.d_work_max );
419         //++timo clean
420 #if 0
421         // will update the workzone to the given brush
422         // g_pParentWnd->ActiveXY()->GetViewType()
423         // cf VIEWTYPE defintion: enum VIEWTYPE {YZ, XZ, XY};
424         // we fit our work zone to the last brush on the list (b)
425         int nViewType = g_pParentWnd->ActiveXY()->GetViewType();
426         int nDim1 = ( nViewType == YZ ) ? 1 : 0;
427         int nDim2 = ( nViewType == XY ) ? 1 : 2;
428         g_qeglobals.d_work_min[nDim1] = b->mins[nDim1];
429         g_qeglobals.d_work_max[nDim1] = b->maxs[nDim1];
430         g_qeglobals.d_work_min[nDim2] = b->mins[nDim2];
431         g_qeglobals.d_work_max[nDim2] = b->maxs[nDim2];
432 #endif
433 }
434
435 // here to filter new brushes once unselected
436 extern void PerformFiltering();
437
438 void Select_Deselect( bool bDeselectFaces ){
439         brush_t *b;
440
441         Patch_Deselect();
442
443         g_pParentWnd->ActiveXY()->UndoClear();
444
445         g_qeglobals.d_workcount++;
446         g_qeglobals.d_select_count = 0;
447         g_qeglobals.d_num_move_points = 0;
448         b = selected_brushes.next;
449
450         if ( b == &selected_brushes ) {
451                 if ( bDeselectFaces ) {
452                         g_ptrSelectedFaces.RemoveAll();
453                         g_ptrSelectedFaceBrushes.RemoveAll();
454                 }
455                 PerformFiltering();
456                 UpdateSurfaceDialog();
457                 Sys_UpdateWindows( W_ALL );
458                 return;
459         }
460
461         if ( bDeselectFaces ) {
462                 g_ptrSelectedFaces.RemoveAll();
463                 g_ptrSelectedFaceBrushes.RemoveAll();
464         }
465
466         g_qeglobals.d_select_mode = sel_brush;
467
468         UpdateWorkzone_ForBrush( b );
469
470         selected_brushes.next->prev = &active_brushes;
471         selected_brushes.prev->next = active_brushes.next;
472         active_brushes.next->prev = selected_brushes.prev;
473         active_brushes.next = selected_brushes.next;
474         selected_brushes.prev = selected_brushes.next = &selected_brushes;
475
476         // filter newly created stuff once it's unselected
477         PerformFiltering();
478         UpdateSurfaceDialog();
479         Sys_UpdateWindows( W_ALL );
480 }
481
482 /*
483    ============
484    Select_Move
485    ============
486  */
487 /*! Moves the currently selected brush/patch
488     \param delta How far to move the selection (x,y,z)
489     \param bSnap If the move should snap to grid points
490  */
491 void Select_Move( vec3_t delta, bool bSnap ){
492         brush_t *b;
493
494         // actually move the selected brushes
495         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
496                 Brush_Move( b, delta, bSnap );
497
498         vec3_t vMin, vMax;
499         Select_GetBounds( vMin, vMax );
500         CString strStatus;
501         strStatus.Format( "Origin X:: %.1f  Y:: %.1f  Z:: %.1f", vMin[0], vMax[1], vMax[2] );
502         g_pParentWnd->SetStatusText( 2, strStatus );
503
504         //Sys_UpdateWindows (W_ALL);
505 }
506
507 /*
508    =================
509    Select_NudgeVerts
510    =================
511  */
512 /*! Moves the currently selected brush/patch vertices
513     \param delta How far to move the vertices (x,y,z)
514     \param bSnap If the move should snap to grid points
515  */
516 void Select_NudgePoint( vec3_t delta, qboolean bSnap ){
517         if ( g_qeglobals.d_select_mode == sel_vertex ) {
518                 // move selected verts
519                 brush_t *b;
520                 vec3_t end;
521                 qboolean success = true;
522                 for ( b = selected_brushes.next; b != &selected_brushes; b = b->next )
523                 {
524                         success &= (qboolean)Brush_MoveVertex( b, g_qeglobals.d_move_points[0], delta, end, bSnap );
525                 }
526                 if ( success ) {
527                         VectorCopy( end, g_qeglobals.d_move_points[0] );
528                 }
529         }
530         else if ( g_qeglobals.d_select_mode == sel_curvepoint ) {
531                 // move selected patch control points
532                 Patch_UpdateSelected( delta );
533         }
534 }
535
536 /*
537    ============
538    Select_Clone
539
540    Creates an exact duplicate of the selection in place, then moves
541    the selected brushes off of their old positions
542    ============
543  */
544 void Select_Clone( void ){
545         g_bScreenUpdates = false;
546         g_pParentWnd->Copy();
547         Select_Deselect();
548         g_pParentWnd->Paste();
549         g_pParentWnd->NudgeSelection( 2, g_qeglobals.d_gridsize );
550         g_pParentWnd->NudgeSelection( 3, g_qeglobals.d_gridsize );
551         Undo_Start( "clone" );
552         Undo_EndBrushList( &selected_brushes );
553         Undo_End();
554         g_bScreenUpdates = true;
555         Sys_UpdateWindows( W_ALL );
556 }
557
558 //++timo clean
559 #if 0
560 /*
561    ============
562    Select_SetTexture
563    Timo : bFitScale to compute scale on the plane and counteract plane / axial plane snapping
564    Timo :       brush primitive texturing
565         the brushprimit_texdef given must be understood as a qtexture_t width=2 height=2 ( HiRes )
566    Timo :  texture plugin, added an IPluginTexdef* parameter
567         must be casted to an IPluginTexdef!
568         if not NULL, get ->Copy() of it into each face or brush ( and remember to hook )
569         if NULL, means we have no information, ask for a default
570    TTimo - shader code cleanup
571    added IShader* parameter
572    ============
573  */
574 void WINAPI Select_SetTexture2( IShader* pShader, texdef_t *texdef, brushprimit_texdef_t *brushprimit_texdef, bool bFitScale, void* pPlugTexdef ){
575         brush_t *b;
576         int nCount = g_ptrSelectedFaces.GetSize();
577         if ( nCount > 0 ) {
578                 Undo_Start( "set face textures" );
579                 ASSERT( g_ptrSelectedFaces.GetSize() == g_ptrSelectedFaceBrushes.GetSize() );
580                 for ( int i = 0; i < nCount; i++ )
581                 {
582                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
583                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
584                         Undo_AddBrush( selBrush );
585                         //++timo TODO: propagate the IShader* ..
586                         SetFaceTexdef( selFace, texdef, brushprimit_texdef, bFitScale, static_cast<IPluginTexdef *>( pPlugTexdef ) );
587                         Brush_Build( selBrush, bFitScale );
588                         Undo_EndBrush( selBrush );
589                 }
590                 Undo_End();
591         }
592         else if ( selected_brushes.next != &selected_brushes ) {
593                 Undo_Start( "set brush textures" );
594                 for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
595                         if ( !b->owner->eclass->fixedsize ) {
596                                 Undo_AddBrush( b );
597                                 Brush_SetTexture2( b, pShader, texdef, brushprimit_texdef, bFitScale, static_cast<IPluginTexdef *>( pPlugTexdef ) );
598                                 Undo_EndBrush( b );
599                         }
600                 Undo_End();
601         }
602         Sys_UpdateWindows( W_ALL );
603 }
604 #endif
605
606 /*
607    ============
608    Select_SetTexture
609    Timo : bFitScale to compute scale on the plane and counteract plane / axial plane snapping
610    Timo :       brush primitive texturing
611         the brushprimit_texdef given must be understood as a qtexture_t width=2 height=2 ( HiRes )
612    Timo :  texture plugin, added an IPluginTexdef* parameter
613         must be casted to an IPluginTexdef!
614         if not NULL, get ->Copy() of it into each face or brush ( and remember to hook )
615         if NULL, means we have no information, ask for a default
616    ============
617  */
618 void WINAPI Select_SetTexture( texdef_t *texdef, brushprimit_texdef_t *brushprimit_texdef, bool bFitScale, void* pPlugTexdef ){
619         /*
620            #ifdef _DEBUG
621            static int count = 0;
622            #endif
623          */
624         brush_t *b;
625         /*
626            #ifdef _DEBUG
627            count++;
628            Sys_Printf("count: %d\n", count);
629            if(count==4)
630            Sys_Printf("break!\n");
631            #endif
632          */
633         int nCount = g_ptrSelectedFaces.GetSize();
634         if ( nCount > 0 ) {
635                 Undo_Start( "set face textures" );
636                 assert( g_ptrSelectedFaces.GetSize() == g_ptrSelectedFaceBrushes.GetSize() );
637                 for ( int i = 0; i < nCount; i++ )
638                 {
639                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
640                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
641                         Undo_AddBrush( selBrush );
642                         SetFaceTexdef( selFace, texdef, brushprimit_texdef, bFitScale, static_cast<IPluginTexdef *>( pPlugTexdef ) );
643                         Brush_Build( selBrush, bFitScale );
644                         Undo_EndBrush( selBrush );
645                 }
646                 Undo_End();
647         }
648         else if ( selected_brushes.next != &selected_brushes ) {
649                 Undo_Start( "set brush textures" );
650                 for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
651                         if ( !b->owner->eclass->fixedsize ) {
652                                 Undo_AddBrush( b );
653                                 Brush_SetTexture( b, texdef, brushprimit_texdef, bFitScale, static_cast<IPluginTexdef *>( pPlugTexdef ) );
654                                 Undo_EndBrush( b );
655                         }
656                 Undo_End();
657         }
658         //++timo FIXME: not necessary in every cases, write a message defering / move one level up
659         Sys_UpdateWindows( W_ALL );
660 }
661
662
663 /*
664    ================================================================
665
666    TRANSFORMATIONS
667
668    ================================================================
669  */
670
671 void Select_GetBounds( vec3_t mins, vec3_t maxs ){
672         brush_t *b;
673         int i;
674
675         for ( i = 0 ; i < 3 ; i++ )
676         {
677                 mins[i] = 99999;
678                 maxs[i] = -99999;
679         }
680
681         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
682         {
683                 if ( b->owner->eclass->fixedsize ) {
684                         for ( i = 0 ; i < 3 ; i++ )
685                         {
686                                 if ( b->owner->origin[i] < mins[i] ) {
687                                         mins[i] = b->owner->origin[i];
688                                 }
689                                 if ( b->owner->origin[i] > maxs[i] ) {
690                                         maxs[i] = b->owner->origin[i];
691                                 }
692                         }
693                 }
694                 else
695                 {
696                         for ( i = 0 ; i < 3 ; i++ )
697                         {
698                                 if ( b->mins[i] < mins[i] ) {
699                                         mins[i] = b->mins[i];
700                                 }
701                                 if ( b->maxs[i] > maxs[i] ) {
702                                         maxs[i] = b->maxs[i];
703                                 }
704                         }
705                 }
706         }
707 }
708
709 void Select_GetTrueMid( vec3_t mid ){
710         vec3_t mins, maxs;
711         Select_GetBounds( mins, maxs );
712
713         for ( int i = 0 ; i < 3 ; i++ )
714                 mid[i] = ( mins[i] + ( ( maxs[i] - mins[i] ) / 2 ) );
715 }
716
717 void Select_GetMid( vec3_t mid ){
718         vec3_t mins, maxs;
719         int i;
720
721         if ( !g_PrefsDlg.m_bSnap ) {
722                 Select_GetTrueMid( mid );
723                 return;
724         }
725
726         Select_GetBounds( mins, maxs );
727
728         for ( i = 0 ; i < 3 ; i++ )
729                 mid[i] = g_qeglobals.d_gridsize * floor( ( ( mins[i] + maxs[i] ) * 0.5 ) / g_qeglobals.d_gridsize );
730 }
731
732 vec3_t select_origin;
733 vec3_t select_matrix[3];
734 qboolean select_fliporder;
735
736 // FIXME: bApplyBPrimit is supposed to be temporary
737 // TODO: manage Brush_Build calls, too many of them with the texture processing
738 // FIXME: the undo doesn't seem to work correctly on texturing and flip/rotate operations?? this is not supposed to be related to the texture locking code, so what is happening?
739 // FIXME: ApplyMatrix works on flipping operation, b0rks on Rotations (so does the "regular" rotation code??)
740 // FIXME: what is getting called in free rotation mode? that used to work right?
741 void Select_ApplyMatrix( bool bSnap, bool bRotation, int nAxis, float fDeg ){ //, qboolean bApplyBPrimit)
742         brush_t *b;
743         face_t  *f;
744         int i, j;
745         vec3_t temp, tmporigin;
746
747         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
748         {
749                 if ( b->owner->eclass->fixedsize ) {
750                         VectorCopy( b->owner->origin, tmporigin );
751                         // transform the origin point
752                         VectorSubtract( b->owner->origin, select_origin, temp );
753                         for ( j = 0 ; j < 3 ; j++ )
754                                 b->owner->origin[j] = DotProduct( temp, select_matrix[j] ) + select_origin[j];
755
756                         // update the origin key
757                         char text[64];
758                         sprintf( text, "%i %i %i",
759                                          (int)b->owner->origin[0], (int)b->owner->origin[1], (int)b->owner->origin[2] );
760                         SetKeyValue( b->owner, "origin", text );
761
762                         /*\todo remove brush-based bounding box for fixedsize entities */
763                         VectorSubtract( b->owner->origin, tmporigin, temp );
764                         for ( f = b->brush_faces ; f ; f = f->next )
765                         {
766                                 // move fixedsize bbox to new origin
767                                 for ( i = 0 ; i < 3 ; i++ )
768                                         VectorAdd( f->planepts[i], temp, f->planepts[i] );
769                         }
770                         Brush_Build( b, bSnap,true,false,false ); // don't filter
771
772                 }
773                 else if ( b->patchBrush ) {
774                         if ( !bRotation && !( ( g_qeglobals.d_select_mode == sel_curvepoint && g_qeglobals.d_num_move_points != 0 ) || g_bPatchBendMode ) ) {
775                                 // invert patch if this is a mirroring operation, unless points are selected or bendmode is active
776                                 patchInvert( b->pPatch );
777                         }
778                         // NOTE: does not clamp points to integers
779                         Patch_ApplyMatrix( b->pPatch, select_origin, select_matrix, false );
780                 }
781                 else
782                 {
783                         for ( f = b->brush_faces ; f ; f = f->next )
784                         {
785                                 // FIXME: only in BP mode!
786                                 // if we are using Brush Primitives texturing, we need to compute the texture matrix after the geometric transformation
787                                 // (with the default texturing you don't need to compute anything for flipping and mirroring operations)
788                                 //      if (bApplyBPrimit) {
789                                 //        ApplyMatrix_BrushPrimit (f, select_matrix, select_origin, select_fliporder);
790                                 //      }
791                                 for ( i = 0 ; i < 3 ; i++ )
792                                 {
793                                         VectorSubtract( f->planepts[i], select_origin, temp );
794                                         for ( j = 0 ; j < 3 ; j++ )
795                                                 f->planepts[i][j] = DotProduct( temp, select_matrix[j] ) + select_origin[j];
796                                 }
797                                 if ( select_fliporder ) {
798                                         VectorCopy( f->planepts[0], temp );
799                                         VectorCopy( f->planepts[2], f->planepts[0] );
800                                         VectorCopy( temp, f->planepts[2] );
801                                 }
802                         }
803                         Brush_Build( b, bSnap,true,false,false ); // don't filter
804                 }
805         }
806 }
807
808 void ProjectOnPlane( vec3_t& normal,float dist,vec3_t& ez, vec3_t& p ){
809         if ( fabs( ez[0] ) == 1 ) {
810                 p[0] = ( dist - normal[1] * p[1] - normal[2] * p[2] ) / normal[0];
811         }
812         else if ( fabs( ez[1] ) == 1 ) {
813                 p[1] = ( dist - normal[0] * p[0] - normal[2] * p[2] ) / normal[1];
814         }
815         else{
816                 p[2] = ( dist - normal[0] * p[0] - normal[1] * p[1] ) / normal[2];
817         }
818 }
819
820 void Back( vec3_t& dir, vec3_t& p ){
821         if ( fabs( dir[0] ) == 1 ) {
822                 p[0] = 0;
823         }
824         else if ( fabs( dir[1] ) == 1 ) {
825                 p[1] = 0;
826         }
827         else{p[2] = 0; }
828 }
829
830
831
832 // using scale[0] and scale[1]
833 void ComputeScale( vec3_t& rex, vec3_t& rey, vec3_t& p, face_t* f ){
834         float px = DotProduct( rex, p );
835         float py = DotProduct( rey, p );
836         px *= f->texdef.scale[0];
837         py *= f->texdef.scale[1];
838         vec3_t aux;
839         VectorCopy( rex, aux );
840         VectorScale( aux, px, aux );
841         VectorCopy( aux, p );
842         VectorCopy( rey, aux );
843         VectorScale( aux, py, aux );
844         VectorAdd( p, aux, p );
845 }
846
847 void ComputeAbsolute( face_t* f, vec3_t& p1, vec3_t& p2, vec3_t& p3 ){
848         vec3_t ex,ey,ez;            // local axis base
849
850 #ifdef _DEBUG
851         if ( g_qeglobals.m_bBrushPrimitMode ) {
852                 Sys_Printf( "Warning : illegal call of ComputeAbsolute in brush primitive mode\n" );
853         }
854 #endif
855
856         // compute first local axis base
857         TextureAxisFromPlane( &f->plane, ex, ey );
858         CrossProduct( ex, ey, ez );
859
860         vec3_t aux;
861         VectorCopy( ex, aux );
862         VectorScale( aux, -f->texdef.shift[0], aux );
863         VectorCopy( aux, p1 );
864         VectorCopy( ey, aux );
865         VectorScale( aux, -f->texdef.shift[1], aux );
866         VectorAdd( p1, aux, p1 );
867         VectorCopy( p1, p2 );
868         VectorAdd( p2, ex, p2 );
869         VectorCopy( p1, p3 );
870         VectorAdd( p3, ey, p3 );
871         VectorCopy( ez, aux );
872         VectorScale( aux, -f->texdef.rotate, aux );
873         VectorRotate( p1, aux, p1 );
874         VectorRotate( p2, aux, p2 );
875         VectorRotate( p3, aux, p3 );
876         // computing rotated local axis base
877         vec3_t rex,rey;
878         VectorCopy( ex, rex );
879         VectorRotate( rex, aux, rex );
880         VectorCopy( ey, rey );
881         VectorRotate( rey, aux, rey );
882
883         ComputeScale( rex,rey,p1,f );
884         ComputeScale( rex,rey,p2,f );
885         ComputeScale( rex,rey,p3,f );
886
887         // project on normal plane
888         // along ez
889         // assumes plane normal is normalized
890         ProjectOnPlane( f->plane.normal,f->plane.dist,ez,p1 );
891         ProjectOnPlane( f->plane.normal,f->plane.dist,ez,p2 );
892         ProjectOnPlane( f->plane.normal,f->plane.dist,ez,p3 );
893 };
894
895
896 void AbsoluteToLocal( plane_t normal2, face_t* f, vec3_t& p1, vec3_t& p2, vec3_t& p3 ){
897         vec3_t ex,ey,ez;
898
899 #ifdef _DEBUG
900         if ( g_qeglobals.m_bBrushPrimitMode ) {
901                 Sys_Printf( "Warning : illegal call of AbsoluteToLocal in brush primitive mode\n" );
902         }
903 #endif
904
905         // computing new local axis base
906         TextureAxisFromPlane( &normal2, ex, ey );
907         CrossProduct( ex, ey, ez );
908
909         // projecting back on (ex,ey)
910         Back( ez,p1 );
911         Back( ez,p2 );
912         Back( ez,p3 );
913
914         vec3_t aux;
915         // rotation
916         VectorCopy( p2, aux );
917         VectorSubtract( aux, p1,aux );
918
919         float x = DotProduct( aux,ex );
920         float y = DotProduct( aux,ey );
921         f->texdef.rotate = 180 * atan2( y,x ) / Q_PI;
922
923         vec3_t rex,rey;
924         // computing rotated local axis base
925         VectorCopy( ez, aux );
926         VectorScale( aux, f->texdef.rotate, aux );
927         VectorCopy( ex, rex );
928         VectorRotate( rex, aux, rex );
929         VectorCopy( ey, rey );
930         VectorRotate( rey, aux, rey );
931
932         // scale
933         VectorCopy( p2, aux );
934         VectorSubtract( aux, p1, aux );
935         f->texdef.scale[0] = DotProduct( aux, rex );
936         VectorCopy( p3, aux );
937         VectorSubtract( aux, p1, aux );
938         f->texdef.scale[1] = DotProduct( aux, rey );
939
940         // shift
941         // only using p1
942         x = DotProduct( rex,p1 );
943         y = DotProduct( rey,p1 );
944         x /= f->texdef.scale[0];
945         y /= f->texdef.scale[1];
946
947         VectorCopy( rex, p1 );
948         VectorScale( p1, x, p1 );
949         VectorCopy( rey, aux );
950         VectorScale( aux, y, aux );
951         VectorAdd( p1, aux, p1 );
952         VectorCopy( ez, aux );
953         VectorScale( aux, -f->texdef.rotate, aux );
954         VectorRotate( p1, aux, p1 );
955         f->texdef.shift[0] = -DotProduct( p1, ex );
956         f->texdef.shift[1] = -DotProduct( p1, ey );
957
958         // stored rot is good considering local axis base
959         // change it if necessary
960         f->texdef.rotate = -f->texdef.rotate;
961
962         Clamp( f->texdef.shift[0], f->d_texture->width );
963         Clamp( f->texdef.shift[1], f->d_texture->height );
964         Clamp( f->texdef.rotate, 360 );
965
966 }
967
968 void RotateFaceTexture( face_t* f, int nAxis, float fDeg ){
969         vec3_t p1,p2,p3, rota;
970         p1[0] = p1[1] = p1[2] = 0;
971         VectorCopy( p1, p2 );
972         VectorCopy( p1, p3 );
973         VectorCopy( p1, rota );
974         ComputeAbsolute( f, p1, p2, p3 );
975
976         rota[nAxis] = fDeg;
977         VectorRotateOrigin( p1, rota, select_origin, p1 );
978         VectorRotateOrigin( p2, rota, select_origin, p2 );
979         VectorRotateOrigin( p3, rota, select_origin, p3 );
980
981         plane_t normal2;
982         vec3_t vNormal;
983         vNormal[0] = f->plane.normal[0];
984         vNormal[1] = f->plane.normal[1];
985         vNormal[2] = f->plane.normal[2];
986         VectorRotate( vNormal, rota, vNormal );
987         normal2.normal[0] = vNormal[0];
988         normal2.normal[1] = vNormal[1];
989         normal2.normal[2] = vNormal[2];
990         AbsoluteToLocal( normal2, f, p1, p2,p3 );
991
992 }
993
994 void RotateTextures( int nAxis, float fDeg, vec3_t vOrigin ){
995         for ( brush_t* b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
996         {
997                 for ( face_t* f = b->brush_faces ; f ; f = f->next )
998                 {
999                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1000                                 RotateFaceTexture_BrushPrimit( f, nAxis, fDeg, vOrigin );
1001                         }
1002                         else{
1003                                 RotateFaceTexture( f, nAxis, fDeg );
1004                         }
1005                 }
1006                 Brush_Build( b, false,true,false,false ); // don't filter
1007         }
1008 }
1009
1010 void Select_ApplyMatrix_BrushPrimit(){
1011   #ifdef _DEBUG
1012         if ( !g_qeglobals.m_bBrushPrimitMode ) {
1013                 Sys_FPrintf( SYS_ERR,"ERROR: Select_ApplyMatrix_BrushPrimit called in non-BP mode\n" );
1014         }
1015   #endif
1016         for ( brush_t* b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1017         {
1018                 for ( face_t* f = b->brush_faces ; f ; f = f->next )
1019                 {
1020                         ApplyMatrix_BrushPrimit( f, select_matrix, select_origin );
1021                 }
1022         }
1023 }
1024
1025 void Select_FlipAxis( int axis ){
1026         int i;
1027
1028         Select_GetMid( select_origin );
1029         for ( i = 0 ; i < 3 ; i++ )
1030         {
1031                 VectorCopy( vec3_origin, select_matrix[i] );
1032                 select_matrix[i][i] = 1;
1033         }
1034         select_matrix[axis][axis] = -1;
1035         select_fliporder = true;
1036
1037         // texture locking
1038         if ( g_PrefsDlg.m_bRotateLock ) {
1039                 // axis flipping inverts space orientation, we have to use a general texture locking algorithm instead of the RotateFaceTexture
1040                 if ( g_qeglobals.m_bBrushPrimitMode ) {
1041                         Select_ApplyMatrix_BrushPrimit();
1042                 }
1043                 else
1044                 {
1045                         // there's never been flip locking for non BP mode, this would be tricky to write and there's not much interest for it with the coming of BP format
1046                         // what could be done is converting regular to BP, locking, then back to regular :)
1047                         Sys_FPrintf( SYS_WRN, "WARNING: regular texturing doesn't have texture lock on flipping operations\n" );
1048                 }
1049         }
1050         // geometric transformation
1051         Select_ApplyMatrix( true, false, 0, 0 );
1052         Sys_UpdateWindows( W_ALL );
1053 }
1054
1055
1056 void Select_Scale( float x, float y, float z ){
1057         Select_GetMid( select_origin );
1058         for ( brush_t* b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1059         {
1060                 // ignore fixedsize entities
1061                 if ( b->owner->eclass->fixedsize ) {
1062                         continue;
1063                 }
1064                 for ( face_t* f = b->brush_faces ; f ; f = f->next )
1065                 {
1066                         for ( int i = 0 ; i < 3 ; i++ )
1067                         {
1068                                 f->planepts[i][0] -= select_origin[0];
1069                                 f->planepts[i][1] -= select_origin[1];
1070                                 f->planepts[i][2] -= select_origin[2];
1071                                 f->planepts[i][0] *= x;
1072                                 f->planepts[i][1] *= y;
1073                                 f->planepts[i][2] *= z;
1074
1075                                 f->planepts[i][0] += select_origin[0];
1076                                 f->planepts[i][1] += select_origin[1];
1077                                 f->planepts[i][2] += select_origin[2];
1078                         }
1079                 }
1080                 Brush_Build( b, false,true,false,false ); // don't filter
1081                 if ( b->patchBrush ) {
1082                         vec3_t v;
1083                         v[0] = x;
1084                         v[1] = y;
1085                         v[2] = z;
1086                         Patch_Scale( b->pPatch, select_origin, v );
1087                 }
1088         }
1089 }
1090
1091 void Select_RotateAxis( int axis, float deg, bool bPaint, bool bMouse ){
1092         int i;
1093         vec_t c, s;
1094
1095         if ( deg == 0 ) {
1096                 return;
1097         }
1098
1099         if ( bMouse ) {
1100                 VectorCopy( g_pParentWnd->ActiveXY()->RotateOrigin(), select_origin );
1101         }
1102         else
1103         {
1104                 Select_GetMid( select_origin );
1105         }
1106
1107         /*
1108            if(axis == 2)
1109            {
1110            vec3_t rotation;
1111            VectorSet(rotation, 0, 0, 360 - deg);
1112            for(brush_t *b = selected_brushes.next; b != &selected_brushes; b = b->next)
1113             if(b->owner->model.pEdit)
1114               b->owner->model.pEdit->Rotate(select_origin, rotation);
1115            }
1116          */
1117
1118         select_fliporder = false;
1119
1120         // the "90" degrees algorithm is mostly used on axis rotate as a speedup and possibly avoiding rounding errors as much as possible
1121         // previous implementation was doing an indirect-oriented rotation over the plane whereas the general algo below was doing a direct-oriented rotation
1122         // this was confusing the texture locking algorithms, fixed it to be direct-oriented (side consequence is that the axis rotate toolbar button rotates the other way now)
1123         // NOTE: previous algo was using vec3_origin in the matrix computation..
1124         //   I don't see what an origin does in linear transformations (3x3 matrixes always relate to a (0,0,0) origin)
1125         //   in Radiant it's initialized as (0,0,0) and never set to another value
1126         //   so I got rid of it when it's not used for initialisation tasks (and even if it's not (0,0,0) it should not matter
1127         if ( deg == 90 ) {
1128                 c = 0;
1129                 s = 1;
1130         }
1131         else
1132         {
1133                 c = cos( deg * Q_PI / 180.0 );
1134                 s = sin( deg * Q_PI / 180.0 );
1135         }
1136
1137         for ( i = 0 ; i < 3 ; i++ )
1138         {
1139                 VectorCopy( vec3_origin, select_matrix[i] );
1140                 select_matrix[i][i] = 1;
1141         }
1142
1143         switch ( axis )
1144         {
1145         case 0:
1146                 select_matrix[1][1] = c;
1147                 select_matrix[1][2] = s;
1148                 select_matrix[2][1] = -s;
1149                 select_matrix[2][2] = c;
1150                 break;
1151         case 1:
1152                 select_matrix[0][0] = c;
1153                 select_matrix[0][2] = s;
1154                 select_matrix[2][0] = -s;
1155                 select_matrix[2][2] = c;
1156                 break;
1157         case 2:
1158                 select_matrix[0][0] = c;
1159                 select_matrix[0][1] = s;
1160                 select_matrix[1][0] = -s;
1161                 select_matrix[1][1] = c;
1162                 break;
1163         }
1164
1165
1166         // texture locking
1167         if ( g_PrefsDlg.m_bRotateLock ) {
1168                 // Terrible hack, reversing input rotation angle to correct
1169                 // texture rotation direction for X and Z axes.
1170                 // RotateTextures needs to be changed to fix this properly?
1171                 if ( axis == 1 ) {
1172                         RotateTextures( axis, deg, select_origin );
1173                 }
1174                 else{
1175                         RotateTextures( axis, deg * -1, select_origin );
1176                 }
1177         }
1178         // geometric transformation
1179         Select_ApplyMatrix( !bMouse, true, axis, deg ); //, false);
1180
1181         if ( bPaint ) {
1182                 Sys_UpdateWindows( W_ALL );
1183         }
1184 }
1185
1186 /*
1187    ================================================================
1188
1189    GROUP SELECTIONS
1190
1191    ================================================================
1192  */
1193
1194 void Select_RealCompleteTall( vec3_t mins, vec3_t maxs ){
1195         brush_t *b, *next;
1196
1197         int nDim1 = ( g_pParentWnd->ActiveXY()->GetViewType() == YZ ) ? 1 : 0;
1198         int nDim2 = ( g_pParentWnd->ActiveXY()->GetViewType() == XY ) ? 1 : 2;
1199
1200         g_qeglobals.d_select_mode = sel_brush;
1201
1202         for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1203         {
1204                 next = b->next;
1205
1206                 if ( b->bFiltered ) {
1207                         continue;
1208                 }
1209
1210                 if ( ( b->maxs[nDim1] > maxs[nDim1] || b->mins[nDim1] < mins[nDim1] )
1211                          || ( b->maxs[nDim2] > maxs[nDim2] || b->mins[nDim2] < mins[nDim2] ) ) {
1212                         continue;
1213                 }
1214
1215                 Brush_RemoveFromList( b );
1216                 Brush_AddToList( b, &selected_brushes );
1217         }
1218 }
1219
1220 void Select_CompleteTall( void ){
1221         vec3_t mins, maxs;
1222
1223         if ( !QE_SingleBrush() ) {
1224                 return;
1225         }
1226
1227         Undo_Start( "select complete tall" );
1228         Undo_AddBrushList( &selected_brushes );
1229         Undo_End();
1230
1231         VectorCopy( selected_brushes.next->mins, mins );
1232         VectorCopy( selected_brushes.next->maxs, maxs );
1233         Select_Delete();
1234
1235         Select_RealCompleteTall( mins, maxs );
1236         Sys_UpdateWindows( W_ALL );
1237 }
1238
1239 void Select_PartialTall( void ){
1240         brush_t *b, *next;
1241         vec3_t mins, maxs;
1242
1243         if ( !QE_SingleBrush() ) {
1244                 return;
1245         }
1246
1247         Undo_Start( "select complete tall" );
1248         Undo_AddBrushList( &selected_brushes );
1249         Undo_End();
1250
1251         g_qeglobals.d_select_mode = sel_brush;
1252
1253         VectorCopy( selected_brushes.next->mins, mins );
1254         VectorCopy( selected_brushes.next->maxs, maxs );
1255         Select_Delete();
1256
1257         int nDim1 = ( g_pParentWnd->ActiveXY()->GetViewType() == YZ ) ? 1 : 0;
1258         int nDim2 = ( g_pParentWnd->ActiveXY()->GetViewType() == XY ) ? 1 : 2;
1259
1260         for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1261         {
1262                 next = b->next;
1263
1264                 if ( b->bFiltered ) {
1265                         continue;
1266                 }
1267
1268                 if ( ( b->mins[nDim1] > maxs[nDim1] || b->maxs[nDim1] < mins[nDim1] )
1269                          || ( b->mins[nDim2] > maxs[nDim2] || b->maxs[nDim2] < mins[nDim2] ) ) {
1270                         continue;
1271                 }
1272
1273
1274                 Brush_RemoveFromList( b );
1275                 Brush_AddToList( b, &selected_brushes );
1276         }
1277
1278         Sys_UpdateWindows( W_ALL );
1279 }
1280
1281 void Select_Touching( void ){
1282         brush_t *b, *next;
1283         int i;
1284         vec3_t mins, maxs;
1285
1286         if ( !QE_SingleBrush() ) {
1287                 return;
1288         }
1289
1290         g_qeglobals.d_select_mode = sel_brush;
1291
1292         VectorCopy( selected_brushes.next->mins, mins );
1293         VectorCopy( selected_brushes.next->maxs, maxs );
1294
1295         for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1296         {
1297                 next = b->next;
1298
1299                 if ( b->bFiltered ) {
1300                         continue;
1301                 }
1302
1303                 for ( i = 0 ; i < 3 ; i++ )
1304                         if ( b->mins[i] > maxs[i] + 1 || b->maxs[i] < mins[i] - 1 ) {
1305                                 break;
1306                         }
1307
1308                 if ( i == 3 ) {
1309                         Brush_RemoveFromList( b );
1310                         Brush_AddToList( b, &selected_brushes );
1311                 }
1312         }
1313
1314         Sys_UpdateWindows( W_ALL );
1315 }
1316
1317 void Select_Inside( void ){
1318         brush_t *b, *next;
1319         int i;
1320         vec3_t mins, maxs;
1321
1322         if ( !QE_SingleBrush() ) {
1323                 return;
1324         }
1325
1326         Undo_Start( "select inside" );
1327         Undo_AddBrushList( &selected_brushes );
1328         Undo_End();
1329
1330         g_qeglobals.d_select_mode = sel_brush;
1331
1332         VectorCopy( selected_brushes.next->mins, mins );
1333         VectorCopy( selected_brushes.next->maxs, maxs );
1334         Select_Delete();
1335
1336         for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1337         {
1338                 next = b->next;
1339
1340                 if ( b->bFiltered ) {
1341                         continue;
1342                 }
1343
1344                 for ( i = 0 ; i < 3 ; i++ )
1345                         if ( b->maxs[i] > maxs[i] || b->mins[i] < mins[i] ) {
1346                                 break;
1347                         }
1348                 if ( i == 3 ) {
1349                         Brush_RemoveFromList( b );
1350                         Brush_AddToList( b, &selected_brushes );
1351                 }
1352         }
1353
1354         Sys_UpdateWindows( W_ALL );
1355 }
1356
1357 void Select_SelectGroup( entity_t* group ){
1358         brush_t*  b;
1359         //brush_t*  next;
1360
1361         Undo_Start( "select func group" );
1362         Undo_AddBrushList( &selected_brushes );
1363         Undo_End();
1364
1365         Select_Deselect();
1366
1367         b = &group->brushes;
1368
1369         do
1370         {
1371                 b = b->onext;
1372                 Brush_RemoveFromList( b );
1373                 Brush_AddToList( b, &selected_brushes );
1374         } while ( b->onext != &group->brushes );
1375
1376         Sys_UpdateWindows( W_ALL );
1377 }
1378
1379
1380 void Select_Ungroup( void ){
1381         int numselectedgroups;
1382         entity_t    *e;
1383         brush_t     *b,* sb;
1384
1385         numselectedgroups = 0;
1386         for ( sb = selected_brushes.next; sb != &selected_brushes; sb = sb->next )
1387         {
1388                 e = sb->owner;
1389
1390                 if ( e == world_entity || e->eclass->fixedsize ) {
1391                         continue;
1392                 }
1393
1394                 for ( b = e->brushes.onext; b != &e->brushes; b = e->brushes.onext )
1395                 {
1396                         Entity_UnlinkBrush( b );
1397                         Entity_LinkBrush( world_entity, b );
1398                 }
1399                 Entity_Free( e );
1400                 numselectedgroups++;
1401         }
1402
1403         if ( numselectedgroups <= 0 ) {
1404                 Sys_Printf( "No grouped entities selected.\n" );
1405                 return;
1406         }
1407         Sys_Printf( "Ungrouped %d entit%s.\n", numselectedgroups, ( numselectedgroups == 1 ) ? "y" : "ies" );
1408         Sys_UpdateWindows( W_ALL );
1409 }
1410
1411 /*!
1412    group selected brushes into specified entity
1413    if an entity is empty afterwards, destroy it
1414  */
1415 void Select_GroupEntity( entity_t* group ){
1416         entity_t* e;
1417         brush_t *b;
1418
1419         if ( group->eclass->fixedsize ) {
1420                 Sys_FPrintf( SYS_ERR, "Select_GroupEntity: can't group anything to a fixedsize entity\n" );
1421                 return;
1422         }
1423
1424         for ( b = selected_brushes.next; b != &selected_brushes; b = b->next )
1425         {
1426                 if ( b->owner->eclass->fixedsize ) {
1427                         continue;
1428                 }
1429                 e = b->owner;
1430                 Entity_UnlinkBrush( b );
1431                 Entity_LinkBrush( group, b );
1432                 if ( e != world_entity && e->brushes.onext == &e->brushes ) {
1433                         Undo_AddEntity( e );
1434                         Entity_Free( e );
1435                 }
1436         }
1437 }
1438
1439 /*!
1440    merge all selected entities together into the first one selected
1441    NOTE: makes use of order of selected_brushes list
1442    can be used to move world brushes in an entity, or to merge several ents together
1443    NOTE: didn't devise a strategy on the epairs, we merge into the first entity and use those
1444  */
1445 void Select_MergeEntity(){
1446         entity_t* e = NULL;
1447         brush_t* b;
1448         for ( b = selected_brushes.next; b != &selected_brushes; b = b->next )
1449         {
1450                 if ( !b->owner->eclass->fixedsize ) {
1451                         e = b->owner;
1452                         break;
1453                 }
1454         }
1455
1456         if ( e != NULL ) {
1457                 Select_GroupEntity( e );
1458
1459                 int count = 0;
1460                 for ( b = e->brushes.onext; b != &e->brushes; b = b->onext )
1461                 {
1462                         //Brush_RemoveFromList (b);
1463                         //Brush_AddToList(b, &active_brushes);
1464                         count++;
1465                 }
1466                 Sys_Printf( "Merged %d brushes into %s entity\n", count, ValueForKey( e, "classname" ) );
1467         }
1468 }
1469
1470 /*
1471    ====================
1472    Select_Seperate
1473    ====================
1474  */
1475 void Select_Seperate( void ) {
1476         Select_GroupEntity( world_entity );
1477 }
1478
1479 /*
1480    ====================
1481    Select_MakeStructural
1482    ====================
1483  */
1484 void Select_MakeStructural( void ){
1485         brush_t *b;
1486         face_t  *f;
1487
1488         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1489         {
1490                 for ( f = b->brush_faces ; f ; f = f->next )
1491                         f->texdef.contents &= ~CONTENTS_DETAIL;
1492                 b->bFiltered = FilterBrush( b );
1493         }
1494         Select_Deselect();
1495         Sys_UpdateWindows( W_ALL );
1496 }
1497
1498 void Select_MakeDetail( void ){
1499         brush_t *b;
1500         face_t  *f;
1501
1502         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1503         {
1504                 for ( f = b->brush_faces ; f ; f = f->next )
1505                         f->texdef.contents |= CONTENTS_DETAIL;
1506                 b->bFiltered = FilterBrush( b );
1507         }
1508         Select_Deselect();
1509         Sys_UpdateWindows( W_ALL );
1510 }
1511
1512 // brush primitive texture adjustments, use the camera view to map adjustments
1513 // ShiftTextureRelative_BrushPrimit ( s , t ) will shift relative to the texture
1514 void ShiftTextureRelative_Camera( face_t *f, int x, int y ){
1515         vec3_t vecS, vecT;
1516         vec_t XY[2]; // the values we are going to send for translation
1517         vec_t sgn[2]; // +1 or -1
1518         int axis[2];
1519         CamWnd* pCam;
1520
1521         // get the two relative texture axes for the current texturing
1522         BrushPrimit_GetRelativeAxes( f, vecS, vecT );
1523
1524         // center point of the face, project it on the camera space
1525         vec3_t C;
1526         VectorClear( C );
1527         int i;
1528         for ( i = 0; i < f->face_winding->numpoints; i++ )
1529         {
1530                 VectorAdd( C,f->face_winding->points[i],C );
1531         }
1532         VectorScale( C,1.0 / f->face_winding->numpoints,C );
1533
1534         pCam = g_pParentWnd->GetCamWnd();
1535         pCam->MatchViewAxes( C, vecS, axis[0], sgn[0] );
1536         pCam->MatchViewAxes( C, vecT, axis[1], sgn[1] );
1537
1538         // this happens when the two directions can't be mapped on two different directions on the screen
1539         // then the move will occur against a single axis
1540         // (i.e. the user is not positioned well enough to send understandable shift commands)
1541         // NOTE: in most cases this warning is not very relevant because the user would use one of the two axes
1542         // for which the solution is easy (the other one being unknown)
1543         // so this warning could be removed
1544         if ( axis[0] == axis[1] ) {
1545                 Sys_FPrintf( SYS_WRN, "Warning: degenerate in ShiftTextureRelative_Camera\n" );
1546         }
1547
1548         // compute the X Y geometric increments
1549         // those geometric increments will be applied along the texture axes (the ones we computed above)
1550         XY[0] = 0;
1551         XY[1] = 0;
1552         if ( x != 0 ) {
1553                 // moving right/left
1554                 XY[axis[0]] += sgn[0] * x;
1555         }
1556         if ( y != 0 ) {
1557                 XY[axis[1]] += sgn[1] * y;
1558         }
1559         // we worked out a move along vecS vecT, and we now it's geometric amplitude
1560         // apply it
1561         ShiftTextureRelative_BrushPrimit( f, XY[0], XY[1] );
1562 }
1563
1564 void Select_ShiftTexture( int x, int y ){
1565         brush_t     *b;
1566         face_t      *f;
1567
1568         int nFaceCount = g_ptrSelectedFaces.GetSize();
1569
1570         if ( selected_brushes.next == &selected_brushes && nFaceCount == 0 ) {
1571                 return;
1572         }
1573
1574         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1575         {
1576                 for ( f = b->brush_faces ; f ; f = f->next )
1577                 {
1578                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1579                                 ShiftTextureRelative_Camera( f, x, y );
1580                         }
1581                         else
1582                         {
1583                                 f->texdef.shift[0] += x;
1584                                 f->texdef.shift[1] += y;
1585                         }
1586                 }
1587                 Brush_Build( b,true,true,false,false ); // don't filter
1588                 if ( b->patchBrush ) {
1589                         Patch_ShiftTexture( b->pPatch, x, y );
1590                 }
1591         }
1592
1593         if ( nFaceCount > 0 ) {
1594                 for ( int i = 0; i < nFaceCount; i++ )
1595                 {
1596                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
1597                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
1598                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1599                                 ShiftTextureRelative_Camera( selFace, x, y );
1600                         }
1601                         else
1602                         {
1603                                 selFace->texdef.shift[0] += x;
1604                                 selFace->texdef.shift[1] += y;
1605                         }
1606                         Brush_Build( selBrush,true,true,false,false ); // don't filter
1607                 }
1608         }
1609
1610         Sys_UpdateWindows( W_CAMERA );
1611 }
1612
1613 //  setting float as input
1614 void Select_ScaleTexture( float x, float y ){
1615         brush_t     *b;
1616         face_t      *f;
1617
1618         int nFaceCount = g_ptrSelectedFaces.GetSize();
1619
1620         if ( selected_brushes.next == &selected_brushes && nFaceCount == 0 ) {
1621                 return;
1622         }
1623
1624         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1625         {
1626                 for ( f = b->brush_faces ; f ; f = f->next )
1627                 {
1628                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1629                                 // apply same scale as the spinner button of the surface inspector
1630                                 float shift[2];
1631                                 float rotate;
1632                                 float scale[2];
1633                                 brushprimit_texdef_t bp;
1634                                 // compute normalized texture matrix
1635                                 ConvertTexMatWithQTexture( &f->brushprimit_texdef, f->d_texture, &bp, NULL );
1636                                 // compute fake shift scale rot
1637                                 TexMatToFakeTexCoords( bp.coords, shift, &rotate, scale );
1638                                 // update
1639                                 scale[0] += static_cast<float>( x ) * 0.1;
1640                                 scale[1] += static_cast<float>( y ) * 0.1;
1641                                 // compute new normalized texture matrix
1642                                 FakeTexCoordsToTexMat( shift, rotate, scale, bp.coords );
1643                                 // apply to face texture matrix
1644                                 ConvertTexMatWithQTexture( &bp, NULL, &f->brushprimit_texdef, f->d_texture );
1645                         }
1646                         else
1647                         {
1648                                 f->texdef.scale[0] += x;
1649                                 f->texdef.scale[1] += y;
1650                         }
1651                 }
1652                 Brush_Build( b,true,true,false,false ); // don't filter
1653                 if ( b->patchBrush ) {
1654                         Patch_ScaleTexture( b->pPatch, x, y );
1655                 }
1656         }
1657
1658         if ( nFaceCount > 0 ) {
1659                 for ( int i = 0; i < nFaceCount; i++ )
1660                 {
1661                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
1662                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
1663                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1664                                 float shift[2];
1665                                 float rotate;
1666                                 float scale[2];
1667                                 brushprimit_texdef_t bp;
1668                                 ConvertTexMatWithQTexture( &selFace->brushprimit_texdef, selFace->d_texture, &bp, NULL );
1669                                 TexMatToFakeTexCoords( bp.coords, shift, &rotate, scale );
1670                                 scale[0] += static_cast<float>( x ) * 0.1;
1671                                 scale[1] += static_cast<float>( y ) * 0.1;
1672                                 FakeTexCoordsToTexMat( shift, rotate, scale, bp.coords );
1673                                 ConvertTexMatWithQTexture( &bp, NULL, &selFace->brushprimit_texdef, selFace->d_texture );
1674                         }
1675                         else
1676                         {
1677                                 selFace->texdef.scale[0] += x;
1678                                 selFace->texdef.scale[1] += y;
1679                         }
1680                         Brush_Build( selBrush,true,true,false,false ); // don't filter
1681                 }
1682         }
1683
1684         Sys_UpdateWindows( W_CAMERA );
1685 }
1686
1687 void Select_RotateTexture( int amt ){
1688         brush_t     *b;
1689         face_t      *f;
1690
1691         int nFaceCount = g_ptrSelectedFaces.GetSize();
1692
1693         if ( selected_brushes.next == &selected_brushes && nFaceCount == 0 ) {
1694                 return;
1695         }
1696
1697         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1698         {
1699                 for ( f = b->brush_faces ; f ; f = f->next )
1700                 {
1701                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1702                                 // apply same scale as the spinner button of the surface inspector
1703                                 float shift[2];
1704                                 float rotate;
1705                                 float scale[2];
1706                                 brushprimit_texdef_t bp;
1707                                 // compute normalized texture matrix
1708                                 ConvertTexMatWithQTexture( &f->brushprimit_texdef, f->d_texture, &bp, NULL );
1709                                 // compute fake shift scale rot
1710                                 TexMatToFakeTexCoords( bp.coords, shift, &rotate, scale );
1711                                 // update
1712                                 rotate += amt;
1713                                 // compute new normalized texture matrix
1714                                 FakeTexCoordsToTexMat( shift, rotate, scale, bp.coords );
1715                                 // apply to face texture matrix
1716                                 ConvertTexMatWithQTexture( &bp, NULL, &f->brushprimit_texdef, f->d_texture );
1717                         }
1718                         else
1719                         {
1720                                 f->texdef.rotate += amt;
1721                                 f->texdef.rotate = static_cast<int>( f->texdef.rotate ) % 360;
1722                         }
1723                 }
1724                 Brush_Build( b,true,true,false,false ); // don't filter
1725                 if ( b->patchBrush ) {
1726                         //Patch_RotateTexture(b->nPatchID, amt);
1727                         Patch_RotateTexture( b->pPatch, amt );
1728                 }
1729         }
1730
1731         if ( nFaceCount > 0 ) {
1732                 for ( int i = 0; i < nFaceCount; i++ )
1733                 {
1734                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
1735                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
1736                         if ( g_qeglobals.m_bBrushPrimitMode ) {
1737                                 float shift[2];
1738                                 float rotate;
1739                                 float scale[2];
1740                                 brushprimit_texdef_t bp;
1741                                 ConvertTexMatWithQTexture( &selFace->brushprimit_texdef, selFace->d_texture, &bp, NULL );
1742                                 TexMatToFakeTexCoords( bp.coords, shift, &rotate, scale );
1743                                 rotate += amt;
1744                                 FakeTexCoordsToTexMat( shift, rotate, scale, bp.coords );
1745                                 ConvertTexMatWithQTexture( &bp, NULL, &selFace->brushprimit_texdef, selFace->d_texture );
1746                         }
1747                         else
1748                         {
1749                                 selFace->texdef.rotate += amt;
1750                                 selFace->texdef.rotate = static_cast<int>( selFace->texdef.rotate ) % 360;
1751                         }
1752                         Brush_Build( selBrush,true,true,false,false ); // don't filter
1753                 }
1754         }
1755
1756         Sys_UpdateWindows( W_CAMERA );
1757 }
1758
1759 // TTimo modified to handle shader architecture:
1760 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
1761 void FindReplaceTextures( const char* pFind, const char* pReplace, bool bSelected, bool bForce, bool bSelectMatchingFaces ){
1762         if ( strchr( pFind, ' ' ) || strchr( pReplace, ' ' ) ) {
1763                 Sys_FPrintf( SYS_WRN, "FindReplaceTextures: '%s' or '%s' have spaces, aborted\n", pFind, pReplace );
1764                 return;
1765         }
1766
1767         brush_t* pList = ( bSelected ) ? &selected_brushes : &active_brushes;
1768         if ( !bSelected ) {
1769                 Select_Deselect();
1770         }
1771
1772         //++timo BP mode: replacing a texture in BP mode is not that easy, you need to recompute the texture matrix
1773         // if the size of the replacing texture differs, otherwise you get wrong scaling
1774         if ( g_qeglobals.m_bBrushPrimitMode ) {
1775                 Sys_Printf( "TODO: finalize find/replace code for brush primitives" );
1776         }
1777
1778         CPtrArray mFaces;
1779         for ( brush_t* pBrush = pList->next ; pBrush != pList; pBrush = pBrush->next )
1780         {
1781                 if ( !bSelectMatchingFaces && pBrush->patchBrush ) {
1782                         Patch_FindReplaceTexture( pBrush, pFind, pReplace, bForce );
1783                 }
1784
1785                 bool found = false; //spog
1786                 for ( face_t* pFace = pBrush->brush_faces; pFace; pFace = pFace->next )
1787                 {
1788                         if ( bForce || strcmpi( pFace->pShader->getName(), pFind ) == 0 ) {
1789                                 if ( !bSelectMatchingFaces ) {
1790                                         pFace->pShader->DecRef();
1791                                         pFace->pShader = QERApp_Shader_ForName( pReplace );
1792                                         pFace->pShader->IncRef();
1793                                         pFace->d_texture = pFace->pShader->getTexture();
1794                                         pFace->texdef.SetName( pReplace );
1795                                         found = true;
1796                                 }
1797                                 else if ( bSelectMatchingFaces ) {
1798                                         mFaces.Add( pFace );
1799                                 }
1800                         }
1801                 }
1802
1803                 if ( found ) { // spog - speed increase, only build brushes that changed
1804                         Brush_Build( pBrush );
1805                 }
1806
1807         }
1808
1809         if ( bSelectMatchingFaces ) {
1810                 if ( bSelected ) {
1811                         Select_Deselect();
1812                 }
1813
1814                 int nSize = mFaces.GetSize();
1815                 for ( int i = 0; i < nSize; i++ ) {
1816                         g_SelectedFaces.Add( reinterpret_cast<face_t *>( mFaces.GetAt( i ) ) );
1817                 }
1818         }
1819
1820         Sys_UpdateWindows( W_CAMERA );
1821 }
1822
1823 void Select_AllOfType(){
1824         brush_t *b, *next;
1825         entity_t    *e;
1826         // if no brush selected, we will select based on texture
1827         //   the first selected face's texture if any, or the current texture
1828         // if a brush is selected, we will select entities (first non-worldspawn owner in selected brushes)
1829         if ( selected_brushes.next == &selected_brushes ) {
1830
1831                 CString strName;
1832                 if ( g_ptrSelectedFaces.GetSize() == 0 ) {
1833                         strName = g_qeglobals.d_texturewin.texdef.GetName();
1834                 }
1835                 else
1836                 {
1837                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( 0 ) );
1838                         strName = selFace->texdef.GetName();
1839                 }
1840
1841                 Sys_Printf( "Selecting all brushes with the texture %s\n", strName.GetBuffer() );
1842
1843                 Select_Deselect();
1844                 for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1845                 {
1846                         next = b->next;
1847
1848                         if ( b->bFiltered ) {
1849                                 continue;
1850                         }
1851
1852                         if ( b->patchBrush ) {
1853                                 if ( strcmpi( strName, b->pPatch->pShader->getName() ) == 0 ) {
1854                                         Brush_RemoveFromList( b );
1855                                         Brush_AddToList( b, &selected_brushes );
1856                                 }
1857                         }
1858                         else
1859                         {
1860                                 for ( face_t* pFace = b->brush_faces; pFace; pFace = pFace->next )
1861                                 {
1862                                         if ( strcmpi( strName, pFace->texdef.GetName() ) == 0 ) {
1863                                                 Brush_RemoveFromList( b );
1864                                                 Brush_AddToList( b, &selected_brushes );
1865                                         }
1866                                 }
1867                         }
1868                 }
1869                 Sys_UpdateWindows( W_ALL );
1870                 return;
1871         }
1872
1873
1874         b = selected_brushes.next;
1875         e = b->owner;
1876
1877         if ( e != NULL ) {
1878                 if ( e != world_entity ) {
1879                         CString strName = e->eclass->name;
1880                         CString strKey, strVal;
1881                         bool bCriteria = GetSelectAllCriteria( strKey, strVal );
1882                         Sys_Printf( "Selecting all %s entities\n", strName.GetBuffer() );
1883                         Select_Deselect();
1884
1885                         for ( b = active_brushes.next ; b != &active_brushes ; b = next )
1886                         {
1887                                 next = b->next;
1888
1889                                 if ( b->bFiltered ) {
1890                                         continue;
1891                                 }
1892
1893                                 e = b->owner;
1894                                 if ( e != NULL ) {
1895                                         if ( strcmpi( e->eclass->name, strName ) == 0 ) {
1896                                                 bool doIt = true;
1897                                                 if ( bCriteria ) {
1898                                                         CString str = ValueForKey( e, strKey );
1899                                                         if ( str.CompareNoCase( strVal ) != 0 ) {
1900                                                                 doIt = false;
1901                                                         }
1902                                                 }
1903                                                 if ( doIt ) {
1904                                                         Brush_RemoveFromList( b );
1905                                                         Brush_AddToList( b, &selected_brushes );
1906                                                 }
1907                                         }
1908                                 }
1909                         }
1910                 }
1911         }
1912         Sys_UpdateWindows( W_ALL );
1913
1914 }
1915
1916 void Select_Reselect(){
1917         Select_Brush( selected_brushes.next );
1918         Sys_UpdateWindows( W_ALL );
1919 }
1920
1921
1922 void Select_FitTexture( int nHeight, int nWidth ){
1923         brush_t     *b;
1924
1925         int nFaceCount = g_ptrSelectedFaces.GetSize();
1926
1927         if ( selected_brushes.next == &selected_brushes && nFaceCount == 0 ) {
1928                 return;
1929         }
1930
1931         for ( b = selected_brushes.next ; b != &selected_brushes ; b = b->next )
1932         {
1933                 Brush_FitTexture( b, nHeight, nWidth );
1934                 Brush_Build( b,true,true,false,false ); // don't filter
1935         }
1936
1937         if ( nFaceCount > 0 ) {
1938                 for ( int i = 0; i < nFaceCount; i++ )
1939                 {
1940                         face_t *selFace = reinterpret_cast<face_t*>( g_ptrSelectedFaces.GetAt( i ) );
1941                         brush_t *selBrush = reinterpret_cast<brush_t*>( g_ptrSelectedFaceBrushes.GetAt( i ) );
1942                         Face_FitTexture( selFace, nHeight, nWidth );
1943                         Brush_Build( selBrush,true,true,false,false ); // don't filter
1944                 }
1945         }
1946
1947         Sys_UpdateWindows( W_CAMERA );
1948 }
1949
1950 void Select_Hide(){
1951         for ( brush_t* b = selected_brushes.next ; b && b != &selected_brushes ; b = b->next )
1952         {
1953                 b->hiddenBrush = true;
1954                 b->bFiltered = true;
1955         }
1956         Sys_UpdateWindows( W_ALL );
1957 }
1958
1959 void Select_ShowAllHidden(){
1960         brush_t* b;
1961         for ( b = selected_brushes.next ; b && b != &selected_brushes ; b = b->next )
1962         {
1963                 if ( b->hiddenBrush ) {
1964                         b->hiddenBrush = false;
1965                         b->bFiltered = FilterBrush( b );
1966                 }
1967         }
1968         for ( b = active_brushes.next ; b && b != &active_brushes ; b = b->next )
1969         {
1970                 if ( b->hiddenBrush ) {
1971                         b->hiddenBrush = false;
1972                         b->bFiltered = FilterBrush( b );
1973                 }
1974         }
1975         Sys_UpdateWindows( W_ALL );
1976 }
1977
1978
1979 /*
1980    ============
1981    Select_Invert
1982    ============
1983  */
1984 void Select_Invert( void ){
1985         brush_t *next, *prev, *b;
1986
1987         Sys_Printf( "inverting selection...\n" );
1988
1989         next = active_brushes.next;
1990         prev = active_brushes.prev;
1991         if ( selected_brushes.next != &selected_brushes ) {
1992                 active_brushes.next = selected_brushes.next;
1993                 active_brushes.prev = selected_brushes.prev;
1994                 active_brushes.next->prev = &active_brushes;
1995                 active_brushes.prev->next = &active_brushes;
1996         }
1997         else
1998         {
1999                 active_brushes.next = &active_brushes;
2000                 active_brushes.prev = &active_brushes;
2001         }
2002         if ( next != &active_brushes ) {
2003                 selected_brushes.next = next;
2004                 selected_brushes.prev = prev;
2005                 selected_brushes.next->prev = &selected_brushes;
2006                 selected_brushes.prev->next = &selected_brushes;
2007         }
2008         else
2009         {
2010                 selected_brushes.next = &selected_brushes;
2011                 selected_brushes.prev = &selected_brushes;
2012         }
2013
2014         // now check if any hidden brush is selected
2015         for ( b = selected_brushes.next; b != &selected_brushes; )
2016         {
2017                 if ( b->patchBrush ) {
2018                         b->pPatch->bSelected = true;
2019                 }
2020
2021                 if ( b->bFiltered ) {
2022                         brush_t *pb = b;
2023                         b = b->next;
2024                         Brush_RemoveFromList( pb );
2025                         Brush_AddToList( pb, &active_brushes );
2026                 }
2027                 else{ b = b->next; }
2028
2029         }
2030
2031         for ( b = active_brushes.next; b != &active_brushes; b = b->next )
2032         {
2033                 if ( b->patchBrush ) {
2034                         b->pPatch->bSelected = false;
2035                 }
2036         }
2037
2038         // since invert selection only works at the brush level,
2039         // set g_qeglobals.d_select_mode accordingly
2040         g_qeglobals.d_select_mode = sel_brush;
2041
2042         // since invert selection only works at the brush level,
2043         // set g_qeglobals.d_select_mode accordingly
2044         g_qeglobals.d_select_mode = sel_brush;
2045
2046         Sys_UpdateWindows( W_ALL );
2047
2048         Sys_Printf( "done.\n" );
2049 }
2050
2051 #ifdef ENABLE_GROUPS
2052 /*
2053    ===========
2054    Select_Name
2055    ===========
2056  */
2057 void Select_Name( const char *pName ){
2058         if ( g_qeglobals.m_bBrushPrimitMode ) {
2059                 for ( brush_t* b = selected_brushes.next ; b && b != &selected_brushes ; b = b->next )
2060                 {
2061                         Brush_SetEpair( b, "Name", pName );
2062                 }
2063         }
2064 }
2065
2066 /*
2067    =================
2068    Select_AddToGroup
2069    add selected brushes to a group, update the tree
2070    =================
2071  */
2072 void Select_AddToGroup( const char *pName ){
2073         if ( g_qeglobals.m_bBrushPrimitMode ) {
2074                 for ( brush_t* b = selected_brushes.next ; b && b != &selected_brushes ; b = b->next )
2075                 {
2076                         Brush_SetEpair( b, "group", pName );
2077                         Group_AddToProperGroup( b );
2078                 }
2079         }
2080 }
2081 #endif