refactored plugin api; refactored callback library; added signals library
[xonotic/netradiant.git] / radiant / brushmanip.cpp
1 /*
2 Copyright (C) 1999-2006 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 #include "brushmanip.h"
23
24
25 #include "gtkutil/widget.h"
26 #include "gtkutil/menu.h"
27 #include "gtkmisc.h"
28 #include "brushnode.h"
29 #include "map.h"
30 #include "texwindow.h"
31 #include "gtkdlgs.h"
32 #include "commands.h"
33 #include "mainframe.h"
34 #include "dialog.h"
35 #include "xywindow.h"
36 #include "preferences.h"
37
38 #include <list>
39
40 void Brush_ConstructCuboid(Brush& brush, const AABB& bounds, const char* shader, const TextureProjection& projection)
41 {
42   const unsigned char box[3][2] = { { 0, 1 }, { 2, 0 }, { 1, 2 } };
43   Vector3 mins(vector3_subtracted(bounds.origin, bounds.extents));
44   Vector3 maxs(vector3_added(bounds.origin, bounds.extents));
45
46   brush.clear();
47   brush.reserve(6);
48
49   {
50     for(int i=0; i < 3; ++i)
51     {
52       Vector3 planepts1(maxs);
53       Vector3 planepts2(maxs);
54       planepts2[box[i][0]] = mins[box[i][0]];
55       planepts1[box[i][1]] = mins[box[i][1]];
56
57       brush.addPlane(maxs, planepts1, planepts2, shader, projection);
58     }
59   }
60   {
61     for(int i=0; i < 3; ++i)
62     {
63       Vector3 planepts1(mins);
64       Vector3 planepts2(mins);
65       planepts1[box[i][0]] = maxs[box[i][0]];
66       planepts2[box[i][1]] = maxs[box[i][1]];
67
68       brush.addPlane(mins, planepts1, planepts2, shader, projection);
69     }
70   }
71 }
72
73 inline float max_extent(const Vector3& extents)
74 {
75   return std::max(std::max(extents[0], extents[1]), extents[2]);
76 }
77
78 inline float max_extent_2d(const Vector3& extents, int axis)
79 {
80   switch(axis)
81   {
82   case 0:
83     return std::max(extents[1], extents[2]);
84   case 1:
85     return std::max(extents[0], extents[2]);
86   default:
87     return std::max(extents[0], extents[1]);
88   }
89 }
90
91 const std::size_t c_brushPrism_minSides = 3;
92 const std::size_t c_brushPrism_maxSides = c_brush_maxFaces - 2;
93 const char* const c_brushPrism_name = "brushPrism";
94
95 void Brush_ConstructPrism(Brush& brush, const AABB& bounds, std::size_t sides, int axis, const char* shader, const TextureProjection& projection)
96 {
97   if(sides < c_brushPrism_minSides)
98   {
99     globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned(sides) << ": too few sides, minimum is " << Unsigned(c_brushPrism_minSides) << "\n";
100     return;
101   }
102   if(sides > c_brushPrism_maxSides)
103   {
104     globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned(sides) << ": too many sides, maximum is " << Unsigned(c_brushPrism_maxSides) << "\n";
105     return;
106   }
107
108   brush.clear();
109   brush.reserve(sides+2);
110
111   Vector3 mins(vector3_subtracted(bounds.origin, bounds.extents));
112   Vector3 maxs(vector3_added(bounds.origin, bounds.extents));
113
114   float radius = max_extent_2d(bounds.extents, axis);
115   const Vector3& mid = bounds.origin;
116   Vector3 planepts[3];
117
118   planepts[2][(axis+1)%3] = mins[(axis+1)%3];
119   planepts[2][(axis+2)%3] = mins[(axis+2)%3];
120   planepts[2][axis] = maxs[axis];
121   planepts[1][(axis+1)%3] = maxs[(axis+1)%3];
122   planepts[1][(axis+2)%3] = mins[(axis+2)%3];
123   planepts[1][axis] = maxs[axis];
124   planepts[0][(axis+1)%3] = maxs[(axis+1)%3];
125   planepts[0][(axis+2)%3] = maxs[(axis+2)%3];
126   planepts[0][axis] = maxs[axis];
127
128   brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
129
130   planepts[0][(axis+1)%3] = mins[(axis+1)%3];
131   planepts[0][(axis+2)%3] = mins[(axis+2)%3];
132   planepts[0][axis] = mins[axis];
133   planepts[1][(axis+1)%3] = maxs[(axis+1)%3];
134   planepts[1][(axis+2)%3] = mins[(axis+2)%3];
135   planepts[1][axis] = mins[axis];
136   planepts[2][(axis+1)%3] = maxs[(axis+1)%3];
137   planepts[2][(axis+2)%3] = maxs[(axis+2)%3];
138   planepts[2][axis] = mins[axis];
139
140   brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
141  
142   for (std::size_t i=0 ; i<sides ; ++i)
143   {
144     double sv = sin (i*3.14159265*2/sides);
145     double cv = cos (i*3.14159265*2/sides);
146
147     planepts[0][(axis+1)%3] = static_cast<float>(floor(mid[(axis+1)%3]+radius*cv+0.5));
148     planepts[0][(axis+2)%3] = static_cast<float>(floor(mid[(axis+2)%3]+radius*sv+0.5));
149     planepts[0][axis] = mins[axis];
150
151     planepts[1][(axis+1)%3] = planepts[0][(axis+1)%3];
152     planepts[1][(axis+2)%3] = planepts[0][(axis+2)%3];
153     planepts[1][axis] = maxs[axis];
154
155     planepts[2][(axis+1)%3] = static_cast<float>(floor(planepts[0][(axis+1)%3] - radius*sv + 0.5));
156     planepts[2][(axis+2)%3] = static_cast<float>(floor(planepts[0][(axis+2)%3] + radius*cv + 0.5));
157     planepts[2][axis] = maxs[axis];
158
159     brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
160   }
161 }
162
163 const std::size_t c_brushCone_minSides = 3;
164 const std::size_t c_brushCone_maxSides = 32;
165 const char* const c_brushCone_name = "brushCone";
166
167 void Brush_ConstructCone(Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection)
168 {
169   if(sides < c_brushCone_minSides)
170   {
171     globalErrorStream() << c_brushCone_name << ": sides " << Unsigned(sides) << ": too few sides, minimum is " << Unsigned(c_brushCone_minSides) << "\n";
172     return;
173   }
174   if(sides > c_brushCone_maxSides)
175   {
176     globalErrorStream() << c_brushCone_name << ": sides " << Unsigned(sides) << ": too many sides, maximum is " << Unsigned(c_brushCone_maxSides) << "\n";
177     return;
178   }
179
180   brush.clear();
181   brush.reserve(sides+1);
182
183   Vector3 mins(vector3_subtracted(bounds.origin, bounds.extents));
184   Vector3 maxs(vector3_added(bounds.origin, bounds.extents));
185
186   float radius = max_extent(bounds.extents);
187   const Vector3& mid = bounds.origin;
188   Vector3 planepts[3];
189
190   planepts[0][0] = mins[0];planepts[0][1] = mins[1];planepts[0][2] = mins[2];
191   planepts[1][0] = maxs[0];planepts[1][1] = mins[1];planepts[1][2] = mins[2];
192   planepts[2][0] = maxs[0];planepts[2][1] = maxs[1];planepts[2][2] = mins[2];
193
194   brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
195
196   for (std::size_t i=0 ; i<sides ; ++i)
197   {
198     double sv = sin (i*3.14159265*2/sides);
199     double cv = cos (i*3.14159265*2/sides);
200
201     planepts[0][0] = static_cast<float>(floor(mid[0]+radius*cv+0.5));
202     planepts[0][1] = static_cast<float>(floor(mid[1]+radius*sv+0.5));
203     planepts[0][2] = mins[2];
204
205     planepts[1][0] = mid[0];
206     planepts[1][1] = mid[1];
207     planepts[1][2] = maxs[2];
208
209     planepts[2][0] = static_cast<float>(floor(planepts[0][0] - radius * sv + 0.5));
210     planepts[2][1] = static_cast<float>(floor(planepts[0][1] + radius * cv + 0.5));
211     planepts[2][2] = maxs[2];
212
213     brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
214   }
215 }
216
217 const std::size_t c_brushSphere_minSides = 3;
218 const std::size_t c_brushSphere_maxSides = 7;
219 const char* const c_brushSphere_name = "brushSphere";
220
221 void Brush_ConstructSphere(Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection)
222 {
223   if(sides < c_brushSphere_minSides)
224   {
225     globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned(sides) << ": too few sides, minimum is " << Unsigned(c_brushSphere_minSides) << "\n";
226     return;
227   }
228   if(sides > c_brushSphere_maxSides)
229   {
230     globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned(sides) << ": too many sides, maximum is " << Unsigned(c_brushSphere_maxSides) << "\n";
231     return;
232   }
233
234   brush.clear();
235   brush.reserve(sides*sides);
236
237   float radius = max_extent(bounds.extents);
238   const Vector3& mid = bounds.origin;
239   Vector3 planepts[3];
240
241   double dt = 2 * c_pi / sides;
242   double dp = c_pi / sides;
243   for(std::size_t i=0; i < sides; i++)
244   {
245     for(std::size_t j=0;j < sides-1; j++)
246     {
247       double t = i * dt;
248       double p = float(j * dp - c_pi / 2);
249
250       planepts[0] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p), radius));
251       planepts[1] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p + dp), radius));
252       planepts[2] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius));
253
254       brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
255     }
256   }
257
258   {
259     double p = (sides - 1) * dp - c_pi / 2;
260     for(std::size_t i = 0; i < sides; i++)
261     {
262       double t = i * dt;
263
264       planepts[0] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t, p), radius));
265       planepts[1] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius));
266       planepts[2] = vector3_added(mid, vector3_scaled(vector3_for_spherical(t + dt, p), radius));
267
268       brush.addPlane(planepts[0], planepts[1], planepts[2], shader, projection);
269     }
270   }
271 }
272
273 int GetViewAxis()
274 {
275   switch(GlobalXYWnd_getCurrentViewType())
276   {
277     case XY:
278       return 2;
279     case XZ:
280       return 1;
281     case YZ:
282       return 0;
283   }
284   return 2;
285 }
286
287 void Brush_ConstructPrefab(Brush& brush, EBrushPrefab type, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection)
288 {
289   switch(type)
290   {
291   case eBrushCuboid:
292     {
293       UndoableCommand undo("brushCuboid");
294
295       Brush_ConstructCuboid(brush, bounds, shader, projection);
296     }
297     break;
298   case eBrushPrism:
299     {
300       int axis = GetViewAxis();
301       StringOutputStream command;
302       command << c_brushPrism_name << " -sides " << Unsigned(sides) << " -axis " << axis;
303       UndoableCommand undo(command.c_str());
304
305       Brush_ConstructPrism(brush, bounds, sides, axis, shader, projection);
306     }
307     break;
308   case eBrushCone:
309     {
310       StringOutputStream command;
311       command << c_brushCone_name << " -sides " << Unsigned(sides);
312       UndoableCommand undo(command.c_str());
313
314       Brush_ConstructCone(brush, bounds, sides, shader, projection);
315     }
316     break;
317   case eBrushSphere:
318     {
319       StringOutputStream command;
320       command << c_brushSphere_name << " -sides " << Unsigned(sides);
321       UndoableCommand undo(command.c_str());
322
323       Brush_ConstructSphere(brush, bounds, sides, shader, projection);
324     }
325     break;
326   }
327 }
328
329
330 void ConstructRegionBrushes(scene::Node* brushes[6], const Vector3& region_mins, const Vector3& region_maxs)
331 {
332   {
333     // set mins
334     Vector3 mins(region_mins[0]-32, region_mins[1]-32, region_mins[2]-32);
335
336     // vary maxs
337     for(std::size_t i=0; i<3; i++)
338     {
339       Vector3 maxs(region_maxs[0]+32, region_maxs[1]+32, region_maxs[2]+32);
340       maxs[i] = region_mins[i];
341       Brush_ConstructCuboid(*Node_getBrush(*brushes[i]), aabb_for_minmax(mins, maxs), texdef_name_default(), TextureProjection());
342     }
343   }
344
345   {
346     // set maxs
347     Vector3 maxs(region_maxs[0]+32, region_maxs[1]+32, region_maxs[2]+32);
348
349     // vary mins
350     for(std::size_t i=0; i<3; i++)
351     {
352       Vector3 mins(region_mins[0]-32, region_mins[1]-32, region_mins[2]-32);
353       mins[i] = region_maxs[i];
354       Brush_ConstructCuboid(*Node_getBrush(*brushes[i+3]), aabb_for_minmax(mins, maxs), texdef_name_default(), TextureProjection());
355     }
356   }
357 }
358
359
360 class FaceSetTexdef
361 {
362   const TextureProjection& m_projection;
363 public:
364   FaceSetTexdef(const TextureProjection& projection) : m_projection(projection)
365   {
366   }
367   void operator()(Face& face) const
368   {
369     face.SetTexdef(m_projection);
370   }
371 };
372
373 void Scene_BrushSetTexdef_Selected(scene::Graph& graph, const TextureProjection& projection)
374 {
375   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceSetTexdef(projection));
376   SceneChangeNotify();
377 }
378
379 void Scene_BrushSetTexdef_Component_Selected(scene::Graph& graph, const TextureProjection& projection)
380 {
381   Scene_ForEachSelectedBrushFace(graph, FaceSetTexdef(projection));
382   SceneChangeNotify();
383 }
384
385
386 class FaceSetFlags
387 {
388   const ContentsFlagsValue& m_projection;
389 public:
390   FaceSetFlags(const ContentsFlagsValue& flags) : m_projection(flags)
391   {
392   }
393   void operator()(Face& face) const
394   {
395     face.SetFlags(m_projection);
396   }
397 };
398
399 void Scene_BrushSetFlags_Selected(scene::Graph& graph, const ContentsFlagsValue& flags)
400 {
401   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceSetFlags(flags));
402   SceneChangeNotify();
403 }
404
405 void Scene_BrushSetFlags_Component_Selected(scene::Graph& graph, const ContentsFlagsValue& flags)
406 {
407   Scene_ForEachSelectedBrushFace(graph, FaceSetFlags(flags));
408   SceneChangeNotify();
409 }
410
411 class FaceShiftTexdef
412 {
413   float m_s, m_t;
414 public:
415   FaceShiftTexdef(float s, float t) : m_s(s), m_t(t)
416   {
417   }
418   void operator()(Face& face) const
419   {
420     face.ShiftTexdef(m_s, m_t);
421   }
422 };
423
424 void Scene_BrushShiftTexdef_Selected(scene::Graph& graph, float s, float t)
425 {
426   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceShiftTexdef(s, t));
427   SceneChangeNotify();
428 }
429
430 void Scene_BrushShiftTexdef_Component_Selected(scene::Graph& graph, float s, float t)
431 {
432   Scene_ForEachSelectedBrushFace(graph, FaceShiftTexdef(s, t));
433   SceneChangeNotify();
434 }
435
436 class FaceScaleTexdef
437 {
438   float m_s, m_t;
439 public:
440   FaceScaleTexdef(float s, float t) : m_s(s), m_t(t)
441   {
442   }
443   void operator()(Face& face) const
444   {
445     face.ScaleTexdef(m_s, m_t);
446   }
447 };
448
449 void Scene_BrushScaleTexdef_Selected(scene::Graph& graph, float s, float t)
450 {
451   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceScaleTexdef(s, t));
452   SceneChangeNotify();
453 }
454
455 void Scene_BrushScaleTexdef_Component_Selected(scene::Graph& graph, float s, float t)
456 {
457   Scene_ForEachSelectedBrushFace(graph, FaceScaleTexdef(s, t));
458   SceneChangeNotify();
459 }
460
461 class FaceRotateTexdef
462 {
463   float m_angle;
464 public:
465   FaceRotateTexdef(float angle) : m_angle(angle)
466   {
467   }
468   void operator()(Face& face) const
469   {
470     face.RotateTexdef(m_angle);
471   }
472 };
473
474 void Scene_BrushRotateTexdef_Selected(scene::Graph& graph, float angle)
475 {
476   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceRotateTexdef(angle));
477   SceneChangeNotify();
478 }
479
480 void Scene_BrushRotateTexdef_Component_Selected(scene::Graph& graph, float angle)
481 {
482   Scene_ForEachSelectedBrushFace(graph, FaceRotateTexdef(angle));
483   SceneChangeNotify();
484 }
485
486
487 class FaceSetShader
488 {
489   const char* m_name;
490 public:
491   FaceSetShader(const char* name) : m_name(name) {}
492   void operator()(Face& face) const
493   {
494     face.SetShader(m_name);
495   }
496 };
497
498 void Scene_BrushSetShader_Selected(scene::Graph& graph, const char* name)
499 {
500   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceSetShader(name));
501   SceneChangeNotify();
502 }
503
504 void Scene_BrushSetShader_Component_Selected(scene::Graph& graph, const char* name)
505 {
506   Scene_ForEachSelectedBrushFace(graph, FaceSetShader(name));
507   SceneChangeNotify();
508 }
509
510 class FaceSetDetail
511 {
512   bool m_detail;
513 public:
514   FaceSetDetail(bool detail) : m_detail(detail)
515   {
516   }
517   void operator()(Face& face) const
518   {
519     face.setDetail(m_detail);
520   }
521 };
522
523 void Scene_BrushSetDetail_Selected(scene::Graph& graph, bool detail)
524 {
525   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceSetDetail(detail));
526   SceneChangeNotify();
527 }
528
529 bool Face_FindReplaceShader(Face& face, const char* find, const char* replace)
530 {
531   if(shader_equal(face.GetShader(), find))
532   {
533     face.SetShader(replace);
534     return true;
535   }
536   return false;
537 }
538
539 class FaceFindReplaceShader
540 {
541   const char* m_find;
542   const char* m_replace;
543 public:
544   FaceFindReplaceShader(const char* find, const char* replace) : m_find(find), m_replace(replace)
545   {
546   }
547   void operator()(Face& face) const
548   {
549     Face_FindReplaceShader(face, m_find, m_replace);
550   }
551 };
552
553 void Scene_BrushFindReplaceShader(scene::Graph& graph, const char* find, const char* replace)
554 {
555   Scene_ForEachBrush_ForEachFace(graph, FaceFindReplaceShader(find, replace));
556 }
557
558 void Scene_BrushFindReplaceShader_Selected(scene::Graph& graph, const char* find, const char* replace)
559 {
560   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceFindReplaceShader(find, replace));
561 }
562
563 void Scene_BrushFindReplaceShader_Component_Selected(scene::Graph& graph, const char* find, const char* replace)
564 {
565   Scene_ForEachSelectedBrushFace(graph, FaceFindReplaceShader(find, replace));
566 }
567
568
569 class FaceFitTexture
570 {
571   float m_s_repeat, m_t_repeat;
572 public:
573   FaceFitTexture(float s_repeat, float t_repeat) : m_s_repeat(s_repeat), m_t_repeat(t_repeat)
574   {
575   }
576   void operator()(Face& face) const
577   {
578     face.FitTexture(m_s_repeat, m_t_repeat);
579   }
580 };
581
582 void Scene_BrushFitTexture_Selected(scene::Graph& graph, float s_repeat, float t_repeat)
583 {
584   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceFitTexture(s_repeat, t_repeat));
585   SceneChangeNotify();
586 }
587
588 void Scene_BrushFitTexture_Component_Selected(scene::Graph& graph, float s_repeat, float t_repeat)
589 {
590   Scene_ForEachSelectedBrushFace(graph, FaceFitTexture(s_repeat, t_repeat));
591   SceneChangeNotify();
592 }
593
594 TextureProjection g_defaultTextureProjection;
595 const TextureProjection& TextureTransform_getDefault()
596 {
597   TexDef_Construct_Default(g_defaultTextureProjection);
598   return g_defaultTextureProjection;
599 }
600
601 void Scene_BrushConstructPrefab(scene::Graph& graph, EBrushPrefab type, std::size_t sides, const char* shader)
602 {
603   if(GlobalSelectionSystem().countSelected() != 0)
604   {
605     const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
606
607     Brush* brush = Node_getBrush(path.top());
608     if(brush != 0)
609     {
610       AABB bounds = brush->localAABB(); // copy bounds because the brush will be modified
611       Brush_ConstructPrefab(*brush, type, bounds, sides, shader, TextureTransform_getDefault());
612       SceneChangeNotify();
613     }
614   }
615 }
616
617 void Scene_BrushResize_Selected(scene::Graph& graph, const AABB& bounds, const char* shader)
618 {
619   if(GlobalSelectionSystem().countSelected() != 0)
620   {
621     const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
622
623     Brush* brush = Node_getBrush(path.top());
624     if(brush != 0)
625     {
626       Brush_ConstructCuboid(*brush, bounds, shader, TextureTransform_getDefault());
627       SceneChangeNotify();
628     }
629   }
630 }
631
632 bool Brush_hasShader(const Brush& brush, const char* name)
633 {
634   for(Brush::const_iterator i = brush.begin(); i != brush.end(); ++i)
635   {
636     if(shader_equal((*i)->GetShader(), name))
637     {
638       return true;
639     }
640   }
641   return false;
642 }
643
644 class BrushSelectByShaderWalker : public scene::Graph::Walker
645 {
646   const char* m_name;
647 public:
648   BrushSelectByShaderWalker(const char* name)
649     : m_name(name)
650   {
651   }
652   bool pre(const scene::Path& path, scene::Instance& instance) const
653   {
654     if(path.top().get().visible())
655     {
656       Brush* brush = Node_getBrush(path.top());
657       if(brush != 0 && Brush_hasShader(*brush, m_name))
658       {
659         Instance_getSelectable(instance)->setSelected(true);
660       }
661     }
662     return true;
663   }
664 };
665
666 void Scene_BrushSelectByShader(scene::Graph& graph, const char* name)
667 {
668   graph.traverse(BrushSelectByShaderWalker(name));
669 }
670
671 class FaceSelectByShader
672 {
673   const char* m_name;
674 public:
675   FaceSelectByShader(const char* name)
676     : m_name(name)
677   {
678   }
679   void operator()(FaceInstance& face) const
680   {
681     if(shader_equal(face.getFace().GetShader(), m_name))
682     {
683       face.setSelected(SelectionSystem::eFace, true);
684     }
685   }
686 };
687
688 void Scene_BrushSelectByShader_Component(scene::Graph& graph, const char* name)
689 {
690   Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, FaceSelectByShader(name));
691 }
692
693 class FaceGetTexdef
694 {
695   TextureProjection& m_projection;
696   mutable bool m_done;
697 public:
698   FaceGetTexdef(TextureProjection& projection)
699     : m_projection(projection), m_done(false)
700   {
701   }
702   void operator()(Face& face) const
703   {
704     if(!m_done)
705     {
706       m_done = true;
707       face.GetTexdef(m_projection);
708     }
709   }
710 };
711
712
713 void Scene_BrushGetTexdef_Selected(scene::Graph& graph, TextureProjection& projection)
714 {
715   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetTexdef(projection));
716 }
717
718 void Scene_BrushGetTexdef_Component_Selected(scene::Graph& graph, TextureProjection& projection)
719 {
720 #if 1
721   if(!g_SelectedFaceInstances.empty())
722   {
723     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
724     faceInstance.getFace().GetTexdef(projection);
725   }
726 #else
727   FaceGetTexdef visitor(projection);
728   Scene_ForEachSelectedBrushFace(graph, visitor);
729 #endif
730 }
731
732
733 class FaceGetFlags
734 {
735   ContentsFlagsValue& m_flags;
736   mutable bool m_done;
737 public:
738   FaceGetFlags(ContentsFlagsValue& flags)
739     : m_flags(flags), m_done(false)
740   {
741   }
742   void operator()(Face& face) const
743   {
744     if(!m_done)
745     {
746       m_done = true;
747       face.GetFlags(m_flags);
748     }
749   }
750 };
751
752
753 void Scene_BrushGetFlags_Selected(scene::Graph& graph, ContentsFlagsValue& flags)
754 {
755 #if 1
756   if(GlobalSelectionSystem().countSelected() != 0)
757   {
758     BrushInstance* brush = Instance_getBrush(GlobalSelectionSystem().ultimateSelected());
759     if(brush != 0)
760     {
761       Brush_forEachFace(*brush, FaceGetFlags(flags));
762     }
763   }
764 #else
765   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetFlags(flags));
766 #endif
767 }
768
769 void Scene_BrushGetFlags_Component_Selected(scene::Graph& graph, ContentsFlagsValue& flags)
770 {
771 #if 1
772   if(!g_SelectedFaceInstances.empty())
773   {
774     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
775     faceInstance.getFace().GetFlags(flags);
776   }
777 #else
778   Scene_ForEachSelectedBrushFace(graph, FaceGetFlags(flags));
779 #endif
780 }
781
782
783 class FaceGetShader
784 {
785   CopiedString& m_shader;
786   mutable bool m_done;
787 public:
788   FaceGetShader(CopiedString& shader)
789     : m_shader(shader), m_done(false)
790   {
791   }
792   void operator()(Face& face) const
793   {
794     if(!m_done)
795     {
796       m_done = true;
797       m_shader = face.GetShader();
798     }
799   }
800 };
801
802 void Scene_BrushGetShader_Selected(scene::Graph& graph, CopiedString& shader)
803 {
804 #if 1
805   if(GlobalSelectionSystem().countSelected() != 0)
806   {
807     BrushInstance* brush = Instance_getBrush(GlobalSelectionSystem().ultimateSelected());
808     if(brush != 0)
809     {
810       Brush_forEachFace(*brush, FaceGetShader(shader));
811     }
812   }
813 #else
814   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetShader(shader));
815 #endif
816 }
817
818 void Scene_BrushGetShader_Component_Selected(scene::Graph& graph, CopiedString& shader)
819 {
820 #if 1
821   if(!g_SelectedFaceInstances.empty())
822   {
823     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
824     shader = faceInstance.getFace().GetShader();
825   }
826 #else
827   FaceGetShader visitor(shader);
828   Scene_ForEachSelectedBrushFace(graph, visitor);
829 #endif
830 }
831
832
833 class filter_face_shader : public FaceFilter
834 {
835   const char* m_shader;
836 public:
837   filter_face_shader(const char* shader) : m_shader(shader)
838   {
839   }
840   bool filter(const Face& face) const
841   {
842     return shader_equal(face.GetShader(), m_shader);
843   }
844 };
845
846 class filter_face_shader_substring : public FaceFilter
847 {
848   const char* m_shader;
849 public:
850   filter_face_shader_substring(const char* shader) : m_shader(shader)
851   {
852   }
853   bool filter(const Face& face) const
854   {
855     return shader_equal_n(face.GetShader(), m_shader, strlen(m_shader));
856   }
857 };
858
859 class filter_face_flags : public FaceFilter
860 {
861   int m_flags;
862 public:
863   filter_face_flags(int flags) : m_flags(flags)
864   {
865   }
866   bool filter(const Face& face) const
867   {
868     return (face.getShader().shaderFlags() & m_flags) != 0;
869   }
870 };
871
872 class filter_face_contents : public FaceFilter
873 {
874   int m_contents;
875 public:
876   filter_face_contents(int contents) : m_contents(contents)
877   {
878   }
879   bool filter(const Face& face) const
880   {
881     return (face.getShader().m_flags.m_contentFlags & m_contents) != 0;
882   }
883 };
884
885
886
887 class FaceFilterAny
888 {
889   FaceFilter* m_filter;
890   bool& m_filtered;
891 public:
892   FaceFilterAny(FaceFilter* filter, bool& filtered) : m_filter(filter), m_filtered(filtered)
893   {
894     m_filtered = false;
895   }
896   void operator()(Face& face) const
897   {
898     if(m_filter->filter(face))
899     {
900       m_filtered = true;
901     }
902   }
903 };
904
905 class filter_brush_any_face : public BrushFilter
906 {
907   FaceFilter* m_filter;
908 public:
909   filter_brush_any_face(FaceFilter* filter) : m_filter(filter)
910   {
911   }
912   bool filter(const Brush& brush) const
913   {
914     bool filtered;
915     Brush_forEachFace(brush, FaceFilterAny(m_filter, filtered));
916     return filtered;
917   }   
918 };
919
920 class FaceFilterAll
921 {
922   FaceFilter* m_filter;
923   bool& m_filtered;
924 public:
925   FaceFilterAll(FaceFilter* filter, bool& filtered) : m_filter(filter), m_filtered(filtered)
926   {
927     m_filtered = true;
928   }
929   void operator()(Face& face) const
930   {
931     if(!m_filter->filter(face))
932     {
933       m_filtered = false;
934     }
935   }
936 };
937
938 class filter_brush_all_faces : public BrushFilter
939 {
940   FaceFilter* m_filter;
941 public:
942   filter_brush_all_faces(FaceFilter* filter) : m_filter(filter)
943   {
944   }
945   bool filter(const Brush& brush) const
946   {
947     bool filtered;
948     Brush_forEachFace(brush, FaceFilterAll(m_filter, filtered));
949     return filtered;
950   }   
951 };
952
953
954 filter_face_flags g_filter_face_clip(QER_CLIP);
955 filter_brush_all_faces g_filter_brush_clip(&g_filter_face_clip);
956
957 filter_face_shader g_filter_face_clip_q2("textures/clip");
958 filter_brush_all_faces g_filter_brush_clip_q2(&g_filter_face_clip_q2);
959
960 filter_face_shader g_filter_face_weapclip("textures/common/weapclip");
961 filter_brush_all_faces g_filter_brush_weapclip(&g_filter_face_weapclip);
962
963 filter_face_shader g_filter_face_botclip("textures/common/botclip");
964 filter_brush_all_faces g_filter_brush_botclip(&g_filter_face_botclip);
965
966 filter_face_shader g_filter_face_caulk("textures/common/caulk");
967 filter_brush_all_faces g_filter_brush_caulk(&g_filter_face_caulk);
968
969 filter_face_shader g_filter_face_caulk_ja("textures/system/caulk");
970 filter_brush_all_faces g_filter_brush_caulk_ja(&g_filter_face_caulk_ja);
971
972 filter_face_shader_substring g_filter_face_liquids("textures/liquids/");
973 filter_brush_any_face g_filter_brush_liquids(&g_filter_face_liquids);
974
975 filter_face_shader g_filter_face_hint("textures/common/hint");
976 filter_brush_any_face g_filter_brush_hint(&g_filter_face_hint);
977
978 filter_face_shader g_filter_face_hint_q2("textures/hint");
979 filter_brush_any_face g_filter_brush_hint_q2(&g_filter_face_hint_q2);
980
981 filter_face_shader g_filter_face_hint_ja("textures/system/hint");
982 filter_brush_any_face g_filter_brush_hint_ja(&g_filter_face_hint_ja);
983
984 filter_face_shader g_filter_face_areaportal("textures/common/areaportal");
985 filter_brush_all_faces g_filter_brush_areaportal(&g_filter_face_areaportal);
986
987 filter_face_shader g_filter_face_visportal("textures/editor/visportal");
988 filter_brush_any_face g_filter_brush_visportal(&g_filter_face_visportal);
989
990 filter_face_shader g_filter_face_clusterportal("textures/common/clusterportal");
991 filter_brush_all_faces g_filter_brush_clusterportal(&g_filter_face_clusterportal);
992
993 filter_face_shader g_filter_face_lightgrid("textures/common/lightgrid");
994 filter_brush_all_faces g_filter_brush_lightgrid(&g_filter_face_lightgrid);
995
996 filter_face_flags g_filter_face_translucent(QER_TRANS);
997 filter_brush_all_faces g_filter_brush_translucent(&g_filter_face_translucent);
998
999 filter_face_contents g_filter_face_detail(CONTENTS_DETAIL);
1000 filter_brush_all_faces g_filter_brush_detail(&g_filter_face_detail);
1001
1002
1003 void BrushFilters_construct()
1004 {
1005   add_brush_filter(g_filter_brush_clip, EXCLUDE_CLIP);
1006   add_brush_filter(g_filter_brush_clip_q2, EXCLUDE_CLIP);
1007   add_brush_filter(g_filter_brush_weapclip, EXCLUDE_CLIP);
1008   add_brush_filter(g_filter_brush_botclip, EXCLUDE_BOTCLIP);
1009   add_brush_filter(g_filter_brush_caulk, EXCLUDE_CAULK);
1010   add_brush_filter(g_filter_brush_caulk_ja, EXCLUDE_CAULK);
1011   add_brush_filter(g_filter_brush_liquids, EXCLUDE_LIQUIDS);
1012   add_brush_filter(g_filter_brush_hint, EXCLUDE_HINTSSKIPS);
1013   add_brush_filter(g_filter_brush_hint_q2, EXCLUDE_HINTSSKIPS);
1014   add_brush_filter(g_filter_brush_hint_ja, EXCLUDE_HINTSSKIPS);
1015   add_brush_filter(g_filter_brush_clusterportal, EXCLUDE_CLUSTERPORTALS);
1016   add_brush_filter(g_filter_brush_visportal, EXCLUDE_VISPORTALS);
1017   add_brush_filter(g_filter_brush_areaportal, EXCLUDE_AREAPORTALS);
1018   add_brush_filter(g_filter_brush_translucent, EXCLUDE_TRANSLUCENT);
1019   add_brush_filter(g_filter_brush_detail, EXCLUDE_DETAILS);
1020   add_brush_filter(g_filter_brush_detail, EXCLUDE_STRUCTURAL, true);
1021   add_brush_filter(g_filter_brush_lightgrid, EXCLUDE_LIGHTGRID);
1022 }
1023
1024 #if 0
1025
1026 void normalquantisation_draw()
1027 {
1028   glPointSize(1);
1029   glBegin(GL_POINTS);
1030   for(std::size_t i = 0; i <= c_quantise_normal; ++i)
1031   {
1032     for(std::size_t j = 0; j <= c_quantise_normal; ++j)
1033     {
1034       Normal3f vertex(normal3f_normalised(Normal3f(
1035         static_cast<float>(c_quantise_normal - j - i),
1036         static_cast<float>(i),
1037         static_cast<float>(j)
1038         )));
1039       VectorScale(normal3f_to_array(vertex), 64.f, normal3f_to_array(vertex));
1040       glVertex3fv(normal3f_to_array(vertex));
1041       vertex.x = -vertex.x;
1042       glVertex3fv(normal3f_to_array(vertex));
1043     }
1044   }
1045   glEnd();
1046 }
1047
1048 class RenderableNormalQuantisation : public OpenGLRenderable
1049 {
1050 public:
1051   void render(RenderStateFlags state) const
1052   {
1053     normalquantisation_draw();
1054   }
1055 };
1056
1057 const float g_test_quantise_normal = 1.f / static_cast<float>(1 << 3);
1058
1059 class TestNormalQuantisation
1060 {
1061   void check_normal(const Normal3f& normal, const Normal3f& other)
1062   {
1063     spherical_t spherical = spherical_from_normal3f(normal);
1064     double longditude = RAD2DEG(spherical.longditude);
1065     double latitude = RAD2DEG(spherical.latitude);
1066     double x = cos(spherical.longditude) * sin(spherical.latitude);
1067     double y = sin(spherical.longditude) * sin(spherical.latitude);
1068     double z = cos(spherical.latitude);
1069
1070     ASSERT_MESSAGE(normal3f_dot(normal, other) > 0.99, "bleh");
1071   }
1072
1073   void test_normal(const Normal3f& normal)
1074   {
1075     Normal3f test = normal3f_from_spherical(spherical_from_normal3f(normal));
1076     check_normal(normal, test);
1077
1078     EOctant octant = normal3f_classify_octant(normal);
1079     Normal3f folded = normal3f_fold_octant(normal, octant);
1080     ESextant sextant = normal3f_classify_sextant(folded);
1081     folded = normal3f_fold_sextant(folded, sextant);
1082
1083     double scale = static_cast<float>(c_quantise_normal) / (folded.x + folded.y + folded.z);
1084
1085     double zbits = folded.z * scale;
1086     double ybits = folded.y * scale;
1087
1088     std::size_t zbits_q = static_cast<std::size_t>(zbits);
1089     std::size_t ybits_q = static_cast<std::size_t>(ybits);
1090
1091     ASSERT_MESSAGE(zbits_q <= (c_quantise_normal / 8) * 3, "bleh");
1092     ASSERT_MESSAGE(ybits_q <= (c_quantise_normal / 2), "bleh");
1093     ASSERT_MESSAGE(zbits_q + ((c_quantise_normal / 2) - ybits_q) <= (c_quantise_normal / 2), "bleh");
1094
1095     std::size_t y_t = (zbits_q < (c_quantise_normal / 4)) ? ybits_q : (c_quantise_normal / 2) - ybits_q;
1096     std::size_t z_t = (zbits_q < (c_quantise_normal / 4)) ? zbits_q : (c_quantise_normal / 2) - zbits_q;
1097     std::size_t index = (c_quantise_normal / 4) * y_t + z_t;
1098     ASSERT_MESSAGE(index <= (c_quantise_normal / 4)*(c_quantise_normal / 2), "bleh");
1099
1100     Normal3f tmp(c_quantise_normal - zbits_q - ybits_q, ybits_q, zbits_q);
1101     tmp = normal3f_normalised(tmp);
1102
1103     Normal3f unfolded = normal3f_unfold_octant(normal3f_unfold_sextant(tmp, sextant), octant);
1104
1105     check_normal(normal, unfolded);
1106
1107     double dot = normal3f_dot(normal, unfolded);
1108     float length = VectorLength(normal3f_to_array(unfolded));
1109     float inv_length = 1 / length;
1110
1111     Normal3f quantised = normal3f_quantised(normal);
1112     check_normal(normal, quantised);
1113   }
1114   void test2(const Normal3f& normal, const Normal3f& other)
1115   {
1116     if(normal3f_quantised(normal) != normal3f_quantised(other))
1117     {
1118       int bleh = 0;
1119     }
1120   }
1121
1122   static Normal3f normalise(float x, float y, float z)
1123   {
1124     return normal3f_normalised(Normal3f(x, y, z));
1125   }
1126
1127   float vec_rand()
1128   {
1129     return static_cast<float>(rand() - (RAND_MAX/2));
1130   }
1131
1132   Normal3f normal3f_rand()
1133   {
1134     return normalise(vec_rand(), vec_rand(), vec_rand());
1135   }
1136
1137 public:
1138   TestNormalQuantisation()
1139   {
1140     for(int i = 4096; i > 0; --i)
1141       test_normal(normal3f_rand());
1142
1143     test_normal(normalise(1, 0, 0));
1144     test_normal(normalise(0, 1, 0));
1145     test_normal(normalise(0, 0, 1));
1146     test_normal(normalise(1, 1, 0));
1147     test_normal(normalise(1, 0, 1));
1148     test_normal(normalise(0, 1, 1));
1149     
1150     test_normal(normalise(10000, 10000, 10000));
1151     test_normal(normalise(10000, 10000, 10001));
1152     test_normal(normalise(10000, 10000, 10002));
1153     test_normal(normalise(10000, 10000, 10010));
1154     test_normal(normalise(10000, 10000, 10020));
1155     test_normal(normalise(10000, 10000, 10030));
1156     test_normal(normalise(10000, 10000, 10100));
1157     test_normal(normalise(10000, 10000, 10101));
1158     test_normal(normalise(10000, 10000, 10102));
1159     test_normal(normalise(10000, 10000, 10200));
1160     test_normal(normalise(10000, 10000, 10201));
1161     test_normal(normalise(10000, 10000, 10202));
1162     test_normal(normalise(10000, 10000, 10203));
1163     test_normal(normalise(10000, 10000, 10300));
1164
1165
1166     test2(normalise(10000, 10000, 10000), normalise(10000, 10000, 10001));
1167     test2(normalise(10000, 10000, 10001), normalise(10000, 10001, 10000));
1168   }
1169 };
1170
1171 TestNormalQuantisation g_testNormalQuantisation;
1172
1173
1174 #endif
1175
1176 #if 0
1177 class TestSelectableObserver : public observer_template<const Selectable&>
1178 {
1179 public:
1180   void notify(const Selectable& arguments)
1181   {
1182     bool bleh = arguments.isSelected();
1183   }
1184 };
1185
1186 inline void test_bleh()
1187 {
1188   TestSelectableObserver test;
1189   ObservableSelectableInstance< SingleObservable< SelectionChangeCallback > > bleh;
1190   bleh.attach(test);
1191   bleh.setSelected(true);
1192   bleh.detach(test);
1193 }
1194
1195 class TestBleh
1196 {
1197 public:
1198   TestBleh()
1199   {
1200     test_bleh();
1201   }
1202 };
1203
1204 const TestBleh testbleh;
1205 #endif
1206
1207
1208 #if 0
1209 class TestRefcountedString
1210 {
1211 public:
1212   TestRefcountedString()
1213   {
1214     {
1215       // copy construct
1216       SmartString string1("string1");
1217       SmartString string2(string1);
1218       SmartString string3(string2);
1219     }
1220     {
1221       // refcounted assignment
1222       SmartString string1("string1");
1223       SmartString string2("string2");
1224       string1 = string2;
1225     }
1226     {
1227       // copy assignment
1228       SmartString string1("string1");
1229       SmartString string2("string2");
1230       string1 = string2.c_str();
1231     }
1232     {
1233       // self-assignment
1234       SmartString string1("string1");
1235       string1 = string1;
1236     }
1237     {
1238       // self-assignment via another reference
1239       SmartString string1("string1");
1240       SmartString string2(string1);
1241       string1 = string2;
1242     }
1243   }
1244 };
1245
1246 const TestRefcountedString g_testRefcountedString;
1247
1248 #endif
1249
1250 void Select_MakeDetail()
1251 {
1252   UndoableCommand undo("brushSetDetail");
1253   Scene_BrushSetDetail_Selected(GlobalSceneGraph(), true);
1254 }
1255
1256 void Select_MakeStructural()
1257 {
1258   UndoableCommand undo("brushClearDetail");
1259   Scene_BrushSetDetail_Selected(GlobalSceneGraph(), false);
1260 }
1261
1262 class BrushMakeSided
1263 {
1264   std::size_t m_count;
1265 public:
1266   BrushMakeSided(std::size_t count)
1267     : m_count(count)
1268   {
1269   }
1270   void set()
1271   {
1272     Scene_BrushConstructPrefab(GlobalSceneGraph(), eBrushPrism, m_count, TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
1273   }
1274   typedef MemberCaller<BrushMakeSided, &BrushMakeSided::set> SetCaller;
1275 };
1276
1277
1278 BrushMakeSided g_brushmakesided3(3);
1279 BrushMakeSided g_brushmakesided4(4);
1280 BrushMakeSided g_brushmakesided5(5);
1281 BrushMakeSided g_brushmakesided6(6);
1282 BrushMakeSided g_brushmakesided7(7);
1283 BrushMakeSided g_brushmakesided8(8);
1284 BrushMakeSided g_brushmakesided9(9);
1285
1286 inline int axis_for_viewtype(int viewtype)
1287 {
1288   switch(viewtype)
1289   {
1290     case XY:
1291       return 2;
1292     case XZ:
1293       return 1;
1294     case YZ:
1295       return 0;
1296   }
1297   return 2;
1298 }
1299
1300 class BrushPrefab
1301 {
1302   EBrushPrefab m_type;
1303 public:
1304   BrushPrefab(EBrushPrefab type)
1305     : m_type(type)
1306   {
1307   }
1308   void set()
1309   {
1310     DoSides(m_type, axis_for_viewtype(GetViewAxis()));
1311   }
1312   typedef MemberCaller<BrushPrefab, &BrushPrefab::set> SetCaller;
1313 };
1314
1315 BrushPrefab g_brushprism(eBrushPrism);
1316 BrushPrefab g_brushcone(eBrushCone);
1317 BrushPrefab g_brushsphere(eBrushSphere);
1318
1319
1320 void FlipClip();
1321 void SplitClip();
1322 void Clip();
1323 void OnClipMode(bool enable);
1324 bool ClipMode();
1325
1326
1327 void ClipSelected()
1328 {
1329   if(ClipMode())
1330   {
1331     UndoableCommand undo("clipperClip");
1332     Clip();
1333   }
1334 }
1335
1336 void SplitSelected()
1337 {
1338   if(ClipMode())
1339   {
1340     UndoableCommand undo("clipperSplit");
1341     SplitClip();
1342   }
1343 }
1344
1345 void FlipClipper()
1346 {
1347   FlipClip();
1348 }
1349
1350
1351 Callback g_texture_lock_status_changed;
1352 BoolExportCaller g_texdef_movelock_caller(g_brush_texturelock_enabled);
1353 ToggleItem g_texdef_movelock_item(g_texdef_movelock_caller);
1354
1355 void Texdef_ToggleMoveLock()
1356 {
1357   g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
1358   g_texdef_movelock_item.update();
1359   g_texture_lock_status_changed();
1360 }
1361
1362
1363
1364
1365 void Face_getClosest(Face& face, SelectionTest& test, SelectionIntersection& bestIntersection, Face*& closestFace)
1366 {
1367   SelectionIntersection intersection;
1368   face.testSelect(test, intersection);
1369   if(intersection.valid()
1370     && SelectionIntersection_closer(intersection, bestIntersection))
1371   {
1372     bestIntersection = intersection;
1373     closestFace = &face;
1374   }
1375 }
1376
1377
1378 class OccludeSelector : public Selector
1379 {
1380   SelectionIntersection& m_bestIntersection;
1381   bool& m_occluded;
1382 public:
1383   OccludeSelector(SelectionIntersection& bestIntersection, bool& occluded) : m_bestIntersection(bestIntersection), m_occluded(occluded)
1384   {
1385     m_occluded = false;
1386   }
1387   void pushSelectable(Selectable& selectable)
1388   {
1389   }
1390   void popSelectable()
1391   {
1392   }
1393   void addIntersection(const SelectionIntersection& intersection)
1394   {
1395     if(SelectionIntersection_closer(intersection, m_bestIntersection))
1396     {
1397       m_bestIntersection = intersection;
1398       m_occluded = true;
1399     }
1400   }
1401 };
1402
1403 class BrushGetClosestFaceVisibleWalker : public scene::Graph::Walker
1404 {
1405   SelectionTest& m_test;
1406   Face*& m_closestFace;
1407   mutable SelectionIntersection m_bestIntersection;
1408 public:
1409   BrushGetClosestFaceVisibleWalker(SelectionTest& test, Face*& closestFace) : m_test(test), m_closestFace(closestFace)
1410   {
1411   }
1412   bool pre(const scene::Path& path, scene::Instance& instance) const
1413   {
1414     if(path.top().get().visible())
1415     {
1416       BrushInstance* brush = Instance_getBrush(instance);
1417       if(brush != 0)
1418       {
1419         m_test.BeginMesh(brush->localToWorld());
1420
1421         for(Brush::const_iterator i = brush->getBrush().begin(); i != brush->getBrush().end(); ++i)
1422         {
1423           Face_getClosest(*(*i), m_test, m_bestIntersection, m_closestFace);
1424         }
1425       }
1426       else
1427       {
1428         SelectionTestable* selectionTestable = Instance_getSelectionTestable(instance);
1429         if(selectionTestable)
1430         {
1431           bool occluded;
1432           OccludeSelector selector(m_bestIntersection, occluded);
1433           selectionTestable->testSelect(selector, m_test);
1434           if(occluded)
1435           {
1436             m_closestFace = 0;
1437           }
1438         }
1439       }
1440     }
1441     return true;
1442   }
1443 };
1444
1445 Face* Scene_BrushGetClosestFace(scene::Graph& graph, SelectionTest& test)
1446 {
1447   Face* closestFace = 0;
1448   graph.traverse(BrushGetClosestFaceVisibleWalker(test, closestFace));
1449   return closestFace;
1450 }
1451
1452 bool Scene_BrushGetClosestFaceTexture(scene::Graph& graph, SelectionTest& test, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags)
1453 {
1454   Face* face = Scene_BrushGetClosestFace(graph, test);
1455   if(face != 0)
1456   {
1457     shader = face->GetShader();
1458     face->GetTexdef(projection);
1459     flags = face->getShader().m_flags;
1460     return true;
1461   }
1462   return false;
1463 }
1464
1465 void Scene_BrushSetClosestFaceTexture(scene::Graph& graph, SelectionTest& test, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags)
1466 {
1467   Face* face = Scene_BrushGetClosestFace(graph, test);
1468   if(face != 0)
1469   {
1470     face->SetShader(shader);
1471     face->SetTexdef(projection);
1472     face->SetFlags(flags);
1473   }
1474 }
1475
1476
1477 class FaceTexture
1478 {
1479 public:
1480   TextureProjection m_projection;
1481   ContentsFlagsValue m_flags;
1482 };
1483
1484 FaceTexture g_faceTextureClipboard;
1485
1486 void FaceTextureClipboard_setDefault()
1487 {
1488   g_faceTextureClipboard.m_flags = ContentsFlagsValue(0, 0, 0, false);
1489   TexDef_Construct_Default(g_faceTextureClipboard.m_projection);
1490 }
1491
1492 void TextureClipboard_textureSelected(const char* shader)
1493 {
1494   FaceTextureClipboard_setDefault();
1495 }
1496
1497 class TextureBrowser;
1498 extern TextureBrowser g_TextureBrowser;
1499 void TextureBrowser_SetSelectedShader(TextureBrowser& textureBrowser, const char* shader);
1500 const char* TextureBrowser_GetSelectedShader(TextureBrowser& textureBrowser);
1501
1502 void Scene_copyClosestFaceTexture(SelectionTest& test)
1503 {
1504   CopiedString shader;
1505   if(Scene_BrushGetClosestFaceTexture(GlobalSceneGraph(), test, shader, g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags))
1506   {
1507     TextureBrowser_SetSelectedShader(g_TextureBrowser, shader.c_str());
1508   }
1509 }
1510
1511 void Scene_applyClosestFaceTexture(SelectionTest& test)
1512 {
1513   UndoableCommand command("facePaintTexture");
1514
1515   Scene_BrushSetClosestFaceTexture(GlobalSceneGraph(), test, TextureBrowser_GetSelectedShader(g_TextureBrowser), g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags);
1516
1517   SceneChangeNotify();
1518 }
1519
1520
1521
1522 void SelectedFaces_copyTexture()
1523 {
1524   if(!g_SelectedFaceInstances.empty())
1525   {
1526     Face& face = g_SelectedFaceInstances.last().getFace();
1527     face.GetTexdef(g_faceTextureClipboard.m_projection);
1528     g_faceTextureClipboard.m_flags = face.getShader().m_flags;
1529
1530     TextureBrowser_SetSelectedShader(g_TextureBrowser, face.getShader().getShader());
1531   }
1532 }
1533
1534 void FaceInstance_pasteTexture(FaceInstance& faceInstance)
1535 {
1536   faceInstance.getFace().SetTexdef(g_faceTextureClipboard.m_projection);
1537   faceInstance.getFace().SetShader(TextureBrowser_GetSelectedShader(g_TextureBrowser));
1538   faceInstance.getFace().SetFlags(g_faceTextureClipboard.m_flags);
1539   SceneChangeNotify();
1540 }
1541
1542 bool SelectedFaces_empty()
1543 {
1544   return g_SelectedFaceInstances.empty();
1545 }
1546
1547 void SelectedFaces_pasteTexture()
1548 {
1549   UndoableCommand command("facePasteTexture");
1550   g_SelectedFaceInstances.foreach(FaceInstance_pasteTexture);
1551 }
1552
1553 void Brush_registerCommands()
1554 {
1555   GlobalToggles_insert("TogTexLock", FreeCaller<Texdef_ToggleMoveLock>(), ToggleItem::AddCallbackCaller(g_texdef_movelock_item), Accelerator('T', (GdkModifierType)GDK_SHIFT_MASK));
1556
1557   GlobalCommands_insert("BrushPrism", BrushPrefab::SetCaller(g_brushprism));
1558   GlobalCommands_insert("BrushCone", BrushPrefab::SetCaller(g_brushcone));
1559   GlobalCommands_insert("BrushSphere", BrushPrefab::SetCaller(g_brushsphere));
1560
1561   GlobalCommands_insert("Brush3Sided", BrushMakeSided::SetCaller(g_brushmakesided3), Accelerator('3', (GdkModifierType)GDK_CONTROL_MASK));
1562   GlobalCommands_insert("Brush4Sided", BrushMakeSided::SetCaller(g_brushmakesided4), Accelerator('4', (GdkModifierType)GDK_CONTROL_MASK));
1563   GlobalCommands_insert("Brush5Sided", BrushMakeSided::SetCaller(g_brushmakesided5), Accelerator('5', (GdkModifierType)GDK_CONTROL_MASK));
1564   GlobalCommands_insert("Brush6Sided", BrushMakeSided::SetCaller(g_brushmakesided6), Accelerator('6', (GdkModifierType)GDK_CONTROL_MASK));
1565   GlobalCommands_insert("Brush7Sided", BrushMakeSided::SetCaller(g_brushmakesided7), Accelerator('7', (GdkModifierType)GDK_CONTROL_MASK));
1566   GlobalCommands_insert("Brush8Sided", BrushMakeSided::SetCaller(g_brushmakesided8), Accelerator('8', (GdkModifierType)GDK_CONTROL_MASK));
1567   GlobalCommands_insert("Brush9Sided", BrushMakeSided::SetCaller(g_brushmakesided9), Accelerator('9', (GdkModifierType)GDK_CONTROL_MASK));
1568
1569   GlobalCommands_insert("ClipSelected", FreeCaller<ClipSelected>(), Accelerator(GDK_Return));
1570   GlobalCommands_insert("SplitSelected", FreeCaller<SplitSelected>(), Accelerator(GDK_Return, (GdkModifierType)GDK_SHIFT_MASK));
1571   GlobalCommands_insert("FlipClip", FreeCaller<FlipClipper>(), Accelerator(GDK_Return, (GdkModifierType)GDK_CONTROL_MASK));
1572
1573   GlobalCommands_insert("FaceCopyTexture", FreeCaller<SelectedFaces_copyTexture>());
1574   GlobalCommands_insert("FacePasteTexture", FreeCaller<SelectedFaces_pasteTexture>());
1575
1576   GlobalCommands_insert("MakeDetail", FreeCaller<Select_MakeDetail>(), Accelerator('M', (GdkModifierType)GDK_CONTROL_MASK));
1577   GlobalCommands_insert("MakeStructural", FreeCaller<Select_MakeStructural>(), Accelerator('S', (GdkModifierType)(GDK_SHIFT_MASK|GDK_CONTROL_MASK)));
1578 }
1579
1580 void Brush_constructMenu(GtkMenu* menu)
1581 {
1582   create_menu_item_with_mnemonic(menu, "Prism...", "BrushPrism");
1583   create_menu_item_with_mnemonic(menu, "Cone...", "BrushCone");
1584   create_menu_item_with_mnemonic(menu, "Sphere...", "BrushSphere");
1585   menu_separator (menu);
1586   {
1587     GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic (menu, "CSG");
1588     create_menu_item_with_mnemonic(menu_in_menu, "Make _Hollow", "CSGHollow");
1589     create_menu_item_with_mnemonic(menu_in_menu, "CSG _Subtract", "CSGSubtract");
1590     create_menu_item_with_mnemonic(menu_in_menu, "CSG _Merge", "CSGMerge");
1591   }
1592   menu_separator(menu);
1593   {
1594     GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic (menu, "Clipper");
1595
1596     create_menu_item_with_mnemonic(menu_in_menu, "Clip selection", "ClipSelected");
1597     create_menu_item_with_mnemonic(menu_in_menu, "Split selection", "SplitSelected");
1598     create_menu_item_with_mnemonic(menu_in_menu, "Flip Clip orientation", "FlipClip");
1599   }
1600   menu_separator(menu);
1601   create_menu_item_with_mnemonic(menu, "Make detail", "MakeDetail");
1602   create_menu_item_with_mnemonic(menu, "Make structural", "MakeStructural");
1603
1604   create_check_menu_item_with_mnemonic(menu, "Texture Lock", "TogTexLock");
1605   menu_separator(menu);
1606   create_menu_item_with_mnemonic(menu, "Copy Face Texture", "FaceCopyTexture");
1607   create_menu_item_with_mnemonic(menu, "Paste Face Texture", "FacePasteTexture");
1608
1609   command_connect_accelerator("Brush3Sided");
1610   command_connect_accelerator("Brush4Sided");
1611   command_connect_accelerator("Brush5Sided");
1612   command_connect_accelerator("Brush6Sided");
1613   command_connect_accelerator("Brush7Sided");
1614   command_connect_accelerator("Brush8Sided");
1615   command_connect_accelerator("Brush9Sided");
1616 }