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