]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/brushmanip.cpp
if a font was not found, fall back to fixed or courier new - should fix "missing...
[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 class FaceFindShader
554 {
555   const char* m_find;
556   const char* m_replace;
557 public:
558   FaceFindShader(const char* find) : m_find(find)
559   {
560   }
561   void operator()(FaceInstance& faceinst) const
562   {
563     if(shader_equal(faceinst.getFace().GetShader(), m_find))
564     {
565       faceinst.setSelected(SelectionSystem::eFace, true);
566     }
567   }
568 };
569
570 bool DoingSearch(const char *repl)
571 {
572     return (repl == NULL || (strcmp("textures/", repl)==0));
573 }
574
575 void Scene_BrushFindReplaceShader(scene::Graph& graph, const char* find, const char* replace)
576 {
577   if (DoingSearch(replace))
578   {
579       Scene_ForEachBrush_ForEachFaceInstance(graph, FaceFindShader(find));
580   } 
581   else 
582   {
583       Scene_ForEachBrush_ForEachFace(graph, FaceFindReplaceShader(find, replace));
584   }
585 }
586
587 void Scene_BrushFindReplaceShader_Selected(scene::Graph& graph, const char* find, const char* replace)
588 {
589   if (DoingSearch(replace)) 
590   {
591     Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, 
592                     FaceFindShader(find));
593   }
594   else
595   {
596     Scene_ForEachSelectedBrush_ForEachFace(graph, 
597                   FaceFindReplaceShader(find, replace));
598   }
599 }
600
601 // TODO: find for components 
602 // d1223m: dont even know what they are...
603 void Scene_BrushFindReplaceShader_Component_Selected(scene::Graph& graph, const char* find, const char* replace)
604 {
605   if (DoingSearch(replace))
606   {
607       
608   }
609   else
610   {
611     Scene_ForEachSelectedBrushFace(graph, FaceFindReplaceShader(find, replace));
612   }
613 }
614
615
616 class FaceFitTexture
617 {
618   float m_s_repeat, m_t_repeat;
619 public:
620   FaceFitTexture(float s_repeat, float t_repeat) : m_s_repeat(s_repeat), m_t_repeat(t_repeat)
621   {
622   }
623   void operator()(Face& face) const
624   {
625     face.FitTexture(m_s_repeat, m_t_repeat);
626   }
627 };
628
629 void Scene_BrushFitTexture_Selected(scene::Graph& graph, float s_repeat, float t_repeat)
630 {
631   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceFitTexture(s_repeat, t_repeat));
632   SceneChangeNotify();
633 }
634
635 void Scene_BrushFitTexture_Component_Selected(scene::Graph& graph, float s_repeat, float t_repeat)
636 {
637   Scene_ForEachSelectedBrushFace(graph, FaceFitTexture(s_repeat, t_repeat));
638   SceneChangeNotify();
639 }
640
641 TextureProjection g_defaultTextureProjection;
642 const TextureProjection& TextureTransform_getDefault()
643 {
644   TexDef_Construct_Default(g_defaultTextureProjection);
645   return g_defaultTextureProjection;
646 }
647
648 void Scene_BrushConstructPrefab(scene::Graph& graph, EBrushPrefab type, std::size_t sides, const char* shader)
649 {
650   if(GlobalSelectionSystem().countSelected() != 0)
651   {
652     const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
653
654     Brush* brush = Node_getBrush(path.top());
655     if(brush != 0)
656     {
657       AABB bounds = brush->localAABB(); // copy bounds because the brush will be modified
658       Brush_ConstructPrefab(*brush, type, bounds, sides, shader, TextureTransform_getDefault());
659       SceneChangeNotify();
660     }
661   }
662 }
663
664 void Scene_BrushResize_Selected(scene::Graph& graph, const AABB& bounds, const char* shader)
665 {
666   if(GlobalSelectionSystem().countSelected() != 0)
667   {
668     const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
669
670     Brush* brush = Node_getBrush(path.top());
671     if(brush != 0)
672     {
673       Brush_ConstructCuboid(*brush, bounds, shader, TextureTransform_getDefault());
674       SceneChangeNotify();
675     }
676   }
677 }
678
679 bool Brush_hasShader(const Brush& brush, const char* name)
680 {
681   for(Brush::const_iterator i = brush.begin(); i != brush.end(); ++i)
682   {
683     if(shader_equal((*i)->GetShader(), name))
684     {
685       return true;
686     }
687   }
688   return false;
689 }
690
691 class BrushSelectByShaderWalker : public scene::Graph::Walker
692 {
693   const char* m_name;
694 public:
695   BrushSelectByShaderWalker(const char* name)
696     : m_name(name)
697   {
698   }
699   bool pre(const scene::Path& path, scene::Instance& instance) const
700   {
701     if(path.top().get().visible())
702     {
703       Brush* brush = Node_getBrush(path.top());
704       if(brush != 0 && Brush_hasShader(*brush, m_name))
705       {
706         Instance_getSelectable(instance)->setSelected(true);
707       }
708     }
709     return true;
710   }
711 };
712
713 void Scene_BrushSelectByShader(scene::Graph& graph, const char* name)
714 {
715   graph.traverse(BrushSelectByShaderWalker(name));
716 }
717
718 class FaceSelectByShader
719 {
720   const char* m_name;
721 public:
722   FaceSelectByShader(const char* name)
723     : m_name(name)
724   {
725   }
726   void operator()(FaceInstance& face) const
727   {
728     printf("checking %s = %s\n", face.getFace().GetShader(), m_name);
729     if(shader_equal(face.getFace().GetShader(), m_name))
730     {
731       face.setSelected(SelectionSystem::eFace, true);
732     }
733   }
734 };
735
736 void Scene_BrushSelectByShader_Component(scene::Graph& graph, const char* name)
737 {
738   Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, FaceSelectByShader(name));
739 }
740
741 class FaceGetTexdef
742 {
743   TextureProjection& m_projection;
744   mutable bool m_done;
745 public:
746   FaceGetTexdef(TextureProjection& projection)
747     : m_projection(projection), m_done(false)
748   {
749   }
750   void operator()(Face& face) const
751   {
752     if(!m_done)
753     {
754       m_done = true;
755       face.GetTexdef(m_projection);
756     }
757   }
758 };
759
760
761 void Scene_BrushGetTexdef_Selected(scene::Graph& graph, TextureProjection& projection)
762 {
763   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetTexdef(projection));
764 }
765
766 void Scene_BrushGetTexdef_Component_Selected(scene::Graph& graph, TextureProjection& projection)
767 {
768 #if 1
769   if(!g_SelectedFaceInstances.empty())
770   {
771     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
772     faceInstance.getFace().GetTexdef(projection);
773   }
774 #else
775   FaceGetTexdef visitor(projection);
776   Scene_ForEachSelectedBrushFace(graph, visitor);
777 #endif
778 }
779
780 void Scene_BrushGetShaderSize_Component_Selected(scene::Graph& graph, size_t& width, size_t& height)
781 {
782   if(!g_SelectedFaceInstances.empty())
783   {
784     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
785         width = faceInstance.getFace().getShader().width();
786         height = faceInstance.getFace().getShader().height();
787   }
788 }
789
790
791 class FaceGetFlags
792 {
793   ContentsFlagsValue& m_flags;
794   mutable bool m_done;
795 public:
796   FaceGetFlags(ContentsFlagsValue& flags)
797     : m_flags(flags), m_done(false)
798   {
799   }
800   void operator()(Face& face) const
801   {
802     if(!m_done)
803     {
804       m_done = true;
805       face.GetFlags(m_flags);
806     }
807   }
808 };
809
810
811 void Scene_BrushGetFlags_Selected(scene::Graph& graph, ContentsFlagsValue& flags)
812 {
813 #if 1
814   if(GlobalSelectionSystem().countSelected() != 0)
815   {
816     BrushInstance* brush = Instance_getBrush(GlobalSelectionSystem().ultimateSelected());
817     if(brush != 0)
818     {
819       Brush_forEachFace(*brush, FaceGetFlags(flags));
820     }
821   }
822 #else
823   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetFlags(flags));
824 #endif
825 }
826
827 void Scene_BrushGetFlags_Component_Selected(scene::Graph& graph, ContentsFlagsValue& flags)
828 {
829 #if 1
830   if(!g_SelectedFaceInstances.empty())
831   {
832     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
833     faceInstance.getFace().GetFlags(flags);
834   }
835 #else
836   Scene_ForEachSelectedBrushFace(graph, FaceGetFlags(flags));
837 #endif
838 }
839
840
841 class FaceGetShader
842 {
843   CopiedString& m_shader;
844   mutable bool m_done;
845 public:
846   FaceGetShader(CopiedString& shader)
847     : m_shader(shader), m_done(false)
848   {
849   }
850   void operator()(Face& face) const
851   {
852     if(!m_done)
853     {
854       m_done = true;
855       m_shader = face.GetShader();
856     }
857   }
858 };
859
860 void Scene_BrushGetShader_Selected(scene::Graph& graph, CopiedString& shader)
861 {
862 #if 1
863   if(GlobalSelectionSystem().countSelected() != 0)
864   {
865     BrushInstance* brush = Instance_getBrush(GlobalSelectionSystem().ultimateSelected());
866     if(brush != 0)
867     {
868       Brush_forEachFace(*brush, FaceGetShader(shader));
869     }
870   }
871 #else
872   Scene_ForEachSelectedBrush_ForEachFace(graph, FaceGetShader(shader));
873 #endif
874 }
875
876 void Scene_BrushGetShader_Component_Selected(scene::Graph& graph, CopiedString& shader)
877 {
878 #if 1
879   if(!g_SelectedFaceInstances.empty())
880   {
881     FaceInstance& faceInstance = g_SelectedFaceInstances.last();
882     shader = faceInstance.getFace().GetShader();
883   }
884 #else
885   FaceGetShader visitor(shader);
886   Scene_ForEachSelectedBrushFace(graph, visitor);
887 #endif
888 }
889
890
891 class filter_face_shader : public FaceFilter
892 {
893   const char* m_shader;
894 public:
895   filter_face_shader(const char* shader) : m_shader(shader)
896   {
897   }
898   bool filter(const Face& face) const
899   {
900     return shader_equal(face.GetShader(), m_shader);
901   }
902 };
903
904 class filter_face_shader_prefix : public FaceFilter
905 {
906   const char* m_prefix;
907 public:
908   filter_face_shader_prefix(const char* prefix) : m_prefix(prefix)
909   {
910   }
911   bool filter(const Face& face) const
912   {
913     return shader_equal_n(face.GetShader(), m_prefix, strlen(m_prefix));
914   }
915 };
916
917 class filter_face_flags : public FaceFilter
918 {
919   int m_flags;
920 public:
921   filter_face_flags(int flags) : m_flags(flags)
922   {
923   }
924   bool filter(const Face& face) const
925   {
926     return (face.getShader().shaderFlags() & m_flags) != 0;
927   }
928 };
929
930 class filter_face_contents : public FaceFilter
931 {
932   int m_contents;
933 public:
934   filter_face_contents(int contents) : m_contents(contents)
935   {
936   }
937   bool filter(const Face& face) const
938   {
939     return (face.getShader().m_flags.m_contentFlags & m_contents) != 0;
940   }
941 };
942
943
944
945 class FaceFilterAny
946 {
947   FaceFilter* m_filter;
948   bool& m_filtered;
949 public:
950   FaceFilterAny(FaceFilter* filter, bool& filtered) : m_filter(filter), m_filtered(filtered)
951   {
952     m_filtered = false;
953   }
954   void operator()(Face& face) const
955   {
956     if(m_filter->filter(face))
957     {
958       m_filtered = true;
959     }
960   }
961 };
962
963 class filter_brush_any_face : public BrushFilter
964 {
965   FaceFilter* m_filter;
966 public:
967   filter_brush_any_face(FaceFilter* filter) : m_filter(filter)
968   {
969   }
970   bool filter(const Brush& brush) const
971   {
972     bool filtered;
973     Brush_forEachFace(brush, FaceFilterAny(m_filter, filtered));
974     return filtered;
975   }   
976 };
977
978 class FaceFilterAll
979 {
980   FaceFilter* m_filter;
981   bool& m_filtered;
982 public:
983   FaceFilterAll(FaceFilter* filter, bool& filtered) : m_filter(filter), m_filtered(filtered)
984   {
985     m_filtered = true;
986   }
987   void operator()(Face& face) const
988   {
989     if(!m_filter->filter(face))
990     {
991       m_filtered = false;
992     }
993   }
994 };
995
996 class filter_brush_all_faces : public BrushFilter
997 {
998   FaceFilter* m_filter;
999 public:
1000   filter_brush_all_faces(FaceFilter* filter) : m_filter(filter)
1001   {
1002   }
1003   bool filter(const Brush& brush) const
1004   {
1005     bool filtered;
1006     Brush_forEachFace(brush, FaceFilterAll(m_filter, filtered));
1007     return filtered;
1008   }   
1009 };
1010
1011
1012 filter_face_flags g_filter_face_clip(QER_CLIP);
1013 filter_brush_all_faces g_filter_brush_clip(&g_filter_face_clip);
1014
1015 filter_face_shader g_filter_face_clip_q2("textures/clip");
1016 filter_brush_all_faces g_filter_brush_clip_q2(&g_filter_face_clip_q2);
1017
1018 filter_face_shader g_filter_face_weapclip("textures/common/weapclip");
1019 filter_brush_all_faces g_filter_brush_weapclip(&g_filter_face_weapclip);
1020
1021 filter_face_shader g_filter_face_botclip("textures/common/botclip");
1022 filter_brush_all_faces g_filter_brush_botclip(&g_filter_face_botclip);
1023
1024 filter_face_shader_prefix g_filter_face_caulk("textures/common/caulk");
1025 filter_brush_all_faces g_filter_brush_caulk(&g_filter_face_caulk);
1026
1027 filter_face_shader_prefix g_filter_face_caulk_ja("textures/system/caulk");
1028 filter_brush_all_faces g_filter_brush_caulk_ja(&g_filter_face_caulk_ja);
1029
1030 filter_face_shader_prefix g_filter_face_liquids("textures/liquids/");
1031 filter_brush_any_face g_filter_brush_liquids(&g_filter_face_liquids);
1032
1033 filter_face_shader g_filter_face_hint("textures/common/hint");
1034 filter_brush_any_face g_filter_brush_hint(&g_filter_face_hint);
1035
1036 filter_face_shader g_filter_face_hint_q2("textures/hint");
1037 filter_brush_any_face g_filter_brush_hint_q2(&g_filter_face_hint_q2);
1038
1039 filter_face_shader g_filter_face_hint_ja("textures/system/hint");
1040 filter_brush_any_face g_filter_brush_hint_ja(&g_filter_face_hint_ja);
1041
1042 filter_face_shader g_filter_face_areaportal("textures/common/areaportal");
1043 filter_brush_all_faces g_filter_brush_areaportal(&g_filter_face_areaportal);
1044
1045 filter_face_shader g_filter_face_visportal("textures/editor/visportal");
1046 filter_brush_any_face g_filter_brush_visportal(&g_filter_face_visportal);
1047
1048 filter_face_shader g_filter_face_clusterportal("textures/common/clusterportal");
1049 filter_brush_all_faces g_filter_brush_clusterportal(&g_filter_face_clusterportal);
1050
1051 filter_face_shader g_filter_face_lightgrid("textures/common/lightgrid");
1052 filter_brush_all_faces g_filter_brush_lightgrid(&g_filter_face_lightgrid);
1053
1054 filter_face_flags g_filter_face_translucent(QER_TRANS);
1055 filter_brush_all_faces g_filter_brush_translucent(&g_filter_face_translucent);
1056
1057 filter_face_contents g_filter_face_detail(BRUSH_DETAIL_MASK);
1058 filter_brush_all_faces g_filter_brush_detail(&g_filter_face_detail);
1059
1060
1061 void BrushFilters_construct()
1062 {
1063   add_brush_filter(g_filter_brush_clip, EXCLUDE_CLIP);
1064   add_brush_filter(g_filter_brush_clip_q2, EXCLUDE_CLIP);
1065   add_brush_filter(g_filter_brush_weapclip, EXCLUDE_CLIP);
1066   add_brush_filter(g_filter_brush_botclip, EXCLUDE_BOTCLIP);
1067   add_brush_filter(g_filter_brush_caulk, EXCLUDE_CAULK);
1068   add_brush_filter(g_filter_brush_caulk_ja, EXCLUDE_CAULK);
1069   add_face_filter(g_filter_face_caulk, EXCLUDE_CAULK);
1070   add_face_filter(g_filter_face_caulk_ja, EXCLUDE_CAULK);
1071   add_brush_filter(g_filter_brush_liquids, EXCLUDE_LIQUIDS);
1072   add_brush_filter(g_filter_brush_hint, EXCLUDE_HINTSSKIPS);
1073   add_brush_filter(g_filter_brush_hint_q2, EXCLUDE_HINTSSKIPS);
1074   add_brush_filter(g_filter_brush_hint_ja, EXCLUDE_HINTSSKIPS);
1075   add_brush_filter(g_filter_brush_clusterportal, EXCLUDE_CLUSTERPORTALS);
1076   add_brush_filter(g_filter_brush_visportal, EXCLUDE_VISPORTALS);
1077   add_brush_filter(g_filter_brush_areaportal, EXCLUDE_AREAPORTALS);
1078   add_brush_filter(g_filter_brush_translucent, EXCLUDE_TRANSLUCENT);
1079   add_brush_filter(g_filter_brush_detail, EXCLUDE_DETAILS);
1080   add_brush_filter(g_filter_brush_detail, EXCLUDE_STRUCTURAL, true);
1081   add_brush_filter(g_filter_brush_lightgrid, EXCLUDE_LIGHTGRID);
1082 }
1083
1084 #if 0
1085
1086 void normalquantisation_draw()
1087 {
1088   glPointSize(1);
1089   glBegin(GL_POINTS);
1090   for(std::size_t i = 0; i <= c_quantise_normal; ++i)
1091   {
1092     for(std::size_t j = 0; j <= c_quantise_normal; ++j)
1093     {
1094       Normal3f vertex(normal3f_normalised(Normal3f(
1095         static_cast<float>(c_quantise_normal - j - i),
1096         static_cast<float>(i),
1097         static_cast<float>(j)
1098         )));
1099       VectorScale(normal3f_to_array(vertex), 64.f, normal3f_to_array(vertex));
1100       glVertex3fv(normal3f_to_array(vertex));
1101       vertex.x = -vertex.x;
1102       glVertex3fv(normal3f_to_array(vertex));
1103     }
1104   }
1105   glEnd();
1106 }
1107
1108 class RenderableNormalQuantisation : public OpenGLRenderable
1109 {
1110 public:
1111   void render(RenderStateFlags state) const
1112   {
1113     normalquantisation_draw();
1114   }
1115 };
1116
1117 const float g_test_quantise_normal = 1.f / static_cast<float>(1 << 3);
1118
1119 class TestNormalQuantisation
1120 {
1121   void check_normal(const Normal3f& normal, const Normal3f& other)
1122   {
1123     spherical_t spherical = spherical_from_normal3f(normal);
1124     double longditude = RAD2DEG(spherical.longditude);
1125     double latitude = RAD2DEG(spherical.latitude);
1126     double x = cos(spherical.longditude) * sin(spherical.latitude);
1127     double y = sin(spherical.longditude) * sin(spherical.latitude);
1128     double z = cos(spherical.latitude);
1129
1130     ASSERT_MESSAGE(normal3f_dot(normal, other) > 0.99, "bleh");
1131   }
1132
1133   void test_normal(const Normal3f& normal)
1134   {
1135     Normal3f test = normal3f_from_spherical(spherical_from_normal3f(normal));
1136     check_normal(normal, test);
1137
1138     EOctant octant = normal3f_classify_octant(normal);
1139     Normal3f folded = normal3f_fold_octant(normal, octant);
1140     ESextant sextant = normal3f_classify_sextant(folded);
1141     folded = normal3f_fold_sextant(folded, sextant);
1142
1143     double scale = static_cast<float>(c_quantise_normal) / (folded.x + folded.y + folded.z);
1144
1145     double zbits = folded.z * scale;
1146     double ybits = folded.y * scale;
1147
1148     std::size_t zbits_q = static_cast<std::size_t>(zbits);
1149     std::size_t ybits_q = static_cast<std::size_t>(ybits);
1150
1151     ASSERT_MESSAGE(zbits_q <= (c_quantise_normal / 8) * 3, "bleh");
1152     ASSERT_MESSAGE(ybits_q <= (c_quantise_normal / 2), "bleh");
1153     ASSERT_MESSAGE(zbits_q + ((c_quantise_normal / 2) - ybits_q) <= (c_quantise_normal / 2), "bleh");
1154
1155     std::size_t y_t = (zbits_q < (c_quantise_normal / 4)) ? ybits_q : (c_quantise_normal / 2) - ybits_q;
1156     std::size_t z_t = (zbits_q < (c_quantise_normal / 4)) ? zbits_q : (c_quantise_normal / 2) - zbits_q;
1157     std::size_t index = (c_quantise_normal / 4) * y_t + z_t;
1158     ASSERT_MESSAGE(index <= (c_quantise_normal / 4)*(c_quantise_normal / 2), "bleh");
1159
1160     Normal3f tmp(c_quantise_normal - zbits_q - ybits_q, ybits_q, zbits_q);
1161     tmp = normal3f_normalised(tmp);
1162
1163     Normal3f unfolded = normal3f_unfold_octant(normal3f_unfold_sextant(tmp, sextant), octant);
1164
1165     check_normal(normal, unfolded);
1166
1167     double dot = normal3f_dot(normal, unfolded);
1168     float length = VectorLength(normal3f_to_array(unfolded));
1169     float inv_length = 1 / length;
1170
1171     Normal3f quantised = normal3f_quantised(normal);
1172     check_normal(normal, quantised);
1173   }
1174   void test2(const Normal3f& normal, const Normal3f& other)
1175   {
1176     if(normal3f_quantised(normal) != normal3f_quantised(other))
1177     {
1178       int bleh = 0;
1179     }
1180   }
1181
1182   static Normal3f normalise(float x, float y, float z)
1183   {
1184     return normal3f_normalised(Normal3f(x, y, z));
1185   }
1186
1187   float vec_rand()
1188   {
1189     return static_cast<float>(rand() - (RAND_MAX/2));
1190   }
1191
1192   Normal3f normal3f_rand()
1193   {
1194     return normalise(vec_rand(), vec_rand(), vec_rand());
1195   }
1196
1197 public:
1198   TestNormalQuantisation()
1199   {
1200     for(int i = 4096; i > 0; --i)
1201       test_normal(normal3f_rand());
1202
1203     test_normal(normalise(1, 0, 0));
1204     test_normal(normalise(0, 1, 0));
1205     test_normal(normalise(0, 0, 1));
1206     test_normal(normalise(1, 1, 0));
1207     test_normal(normalise(1, 0, 1));
1208     test_normal(normalise(0, 1, 1));
1209     
1210     test_normal(normalise(10000, 10000, 10000));
1211     test_normal(normalise(10000, 10000, 10001));
1212     test_normal(normalise(10000, 10000, 10002));
1213     test_normal(normalise(10000, 10000, 10010));
1214     test_normal(normalise(10000, 10000, 10020));
1215     test_normal(normalise(10000, 10000, 10030));
1216     test_normal(normalise(10000, 10000, 10100));
1217     test_normal(normalise(10000, 10000, 10101));
1218     test_normal(normalise(10000, 10000, 10102));
1219     test_normal(normalise(10000, 10000, 10200));
1220     test_normal(normalise(10000, 10000, 10201));
1221     test_normal(normalise(10000, 10000, 10202));
1222     test_normal(normalise(10000, 10000, 10203));
1223     test_normal(normalise(10000, 10000, 10300));
1224
1225
1226     test2(normalise(10000, 10000, 10000), normalise(10000, 10000, 10001));
1227     test2(normalise(10000, 10000, 10001), normalise(10000, 10001, 10000));
1228   }
1229 };
1230
1231 TestNormalQuantisation g_testNormalQuantisation;
1232
1233
1234 #endif
1235
1236 #if 0
1237 class TestSelectableObserver : public observer_template<const Selectable&>
1238 {
1239 public:
1240   void notify(const Selectable& arguments)
1241   {
1242     bool bleh = arguments.isSelected();
1243   }
1244 };
1245
1246 inline void test_bleh()
1247 {
1248   TestSelectableObserver test;
1249   ObservableSelectableInstance< SingleObservable< SelectionChangeCallback > > bleh;
1250   bleh.attach(test);
1251   bleh.setSelected(true);
1252   bleh.detach(test);
1253 }
1254
1255 class TestBleh
1256 {
1257 public:
1258   TestBleh()
1259   {
1260     test_bleh();
1261   }
1262 };
1263
1264 const TestBleh testbleh;
1265 #endif
1266
1267
1268 #if 0
1269 class TestRefcountedString
1270 {
1271 public:
1272   TestRefcountedString()
1273   {
1274     {
1275       // copy construct
1276       SmartString string1("string1");
1277       SmartString string2(string1);
1278       SmartString string3(string2);
1279     }
1280     {
1281       // refcounted assignment
1282       SmartString string1("string1");
1283       SmartString string2("string2");
1284       string1 = string2;
1285     }
1286     {
1287       // copy assignment
1288       SmartString string1("string1");
1289       SmartString string2("string2");
1290       string1 = string2.c_str();
1291     }
1292     {
1293       // self-assignment
1294       SmartString string1("string1");
1295       string1 = string1;
1296     }
1297     {
1298       // self-assignment via another reference
1299       SmartString string1("string1");
1300       SmartString string2(string1);
1301       string1 = string2;
1302     }
1303   }
1304 };
1305
1306 const TestRefcountedString g_testRefcountedString;
1307
1308 #endif
1309
1310 void Select_MakeDetail()
1311 {
1312   UndoableCommand undo("brushSetDetail");
1313   Scene_BrushSetDetail_Selected(GlobalSceneGraph(), true);
1314 }
1315
1316 void Select_MakeStructural()
1317 {
1318   UndoableCommand undo("brushClearDetail");
1319   Scene_BrushSetDetail_Selected(GlobalSceneGraph(), false);
1320 }
1321
1322 class BrushMakeSided
1323 {
1324   std::size_t m_count;
1325 public:
1326   BrushMakeSided(std::size_t count)
1327     : m_count(count)
1328   {
1329   }
1330   void set()
1331   {
1332     Scene_BrushConstructPrefab(GlobalSceneGraph(), eBrushPrism, m_count, TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
1333   }
1334   typedef MemberCaller<BrushMakeSided, &BrushMakeSided::set> SetCaller;
1335 };
1336
1337
1338 BrushMakeSided g_brushmakesided3(3);
1339 BrushMakeSided g_brushmakesided4(4);
1340 BrushMakeSided g_brushmakesided5(5);
1341 BrushMakeSided g_brushmakesided6(6);
1342 BrushMakeSided g_brushmakesided7(7);
1343 BrushMakeSided g_brushmakesided8(8);
1344 BrushMakeSided g_brushmakesided9(9);
1345
1346 inline int axis_for_viewtype(int viewtype)
1347 {
1348   switch(viewtype)
1349   {
1350     case XY:
1351       return 2;
1352     case XZ:
1353       return 1;
1354     case YZ:
1355       return 0;
1356   }
1357   return 2;
1358 }
1359
1360 class BrushPrefab
1361 {
1362   EBrushPrefab m_type;
1363 public:
1364   BrushPrefab(EBrushPrefab type)
1365     : m_type(type)
1366   {
1367   }
1368   void set()
1369   {
1370     DoSides(m_type, axis_for_viewtype(GetViewAxis()));
1371   }
1372   typedef MemberCaller<BrushPrefab, &BrushPrefab::set> SetCaller;
1373 };
1374
1375 BrushPrefab g_brushprism(eBrushPrism);
1376 BrushPrefab g_brushcone(eBrushCone);
1377 BrushPrefab g_brushsphere(eBrushSphere);
1378
1379
1380 void FlipClip();
1381 void SplitClip();
1382 void Clip();
1383 void OnClipMode(bool enable);
1384 bool ClipMode();
1385
1386
1387 void ClipSelected()
1388 {
1389   if(ClipMode())
1390   {
1391     UndoableCommand undo("clipperClip");
1392     Clip();
1393   }
1394 }
1395
1396 void SplitSelected()
1397 {
1398   if(ClipMode())
1399   {
1400     UndoableCommand undo("clipperSplit");
1401     SplitClip();
1402   }
1403 }
1404
1405 void FlipClipper()
1406 {
1407   FlipClip();
1408 }
1409
1410
1411 Callback g_texture_lock_status_changed;
1412 BoolExportCaller g_texdef_movelock_caller(g_brush_texturelock_enabled);
1413 ToggleItem g_texdef_movelock_item(g_texdef_movelock_caller);
1414
1415 void Texdef_ToggleMoveLock()
1416 {
1417   g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
1418   g_texdef_movelock_item.update();
1419   g_texture_lock_status_changed();
1420 }
1421
1422
1423
1424
1425
1426 void Brush_registerCommands()
1427 {
1428   GlobalToggles_insert("TogTexLock", FreeCaller<Texdef_ToggleMoveLock>(), ToggleItem::AddCallbackCaller(g_texdef_movelock_item), Accelerator('T', (GdkModifierType)GDK_SHIFT_MASK));
1429
1430   GlobalCommands_insert("BrushPrism", BrushPrefab::SetCaller(g_brushprism));
1431   GlobalCommands_insert("BrushCone", BrushPrefab::SetCaller(g_brushcone));
1432   GlobalCommands_insert("BrushSphere", BrushPrefab::SetCaller(g_brushsphere));
1433
1434   GlobalCommands_insert("Brush3Sided", BrushMakeSided::SetCaller(g_brushmakesided3), Accelerator('3', (GdkModifierType)GDK_CONTROL_MASK));
1435   GlobalCommands_insert("Brush4Sided", BrushMakeSided::SetCaller(g_brushmakesided4), Accelerator('4', (GdkModifierType)GDK_CONTROL_MASK));
1436   GlobalCommands_insert("Brush5Sided", BrushMakeSided::SetCaller(g_brushmakesided5), Accelerator('5', (GdkModifierType)GDK_CONTROL_MASK));
1437   GlobalCommands_insert("Brush6Sided", BrushMakeSided::SetCaller(g_brushmakesided6), Accelerator('6', (GdkModifierType)GDK_CONTROL_MASK));
1438   GlobalCommands_insert("Brush7Sided", BrushMakeSided::SetCaller(g_brushmakesided7), Accelerator('7', (GdkModifierType)GDK_CONTROL_MASK));
1439   GlobalCommands_insert("Brush8Sided", BrushMakeSided::SetCaller(g_brushmakesided8), Accelerator('8', (GdkModifierType)GDK_CONTROL_MASK));
1440   GlobalCommands_insert("Brush9Sided", BrushMakeSided::SetCaller(g_brushmakesided9), Accelerator('9', (GdkModifierType)GDK_CONTROL_MASK));
1441
1442   GlobalCommands_insert("ClipSelected", FreeCaller<ClipSelected>(), Accelerator(GDK_Return));
1443   GlobalCommands_insert("SplitSelected", FreeCaller<SplitSelected>(), Accelerator(GDK_Return, (GdkModifierType)GDK_SHIFT_MASK));
1444   GlobalCommands_insert("FlipClip", FreeCaller<FlipClipper>(), Accelerator(GDK_Return, (GdkModifierType)GDK_CONTROL_MASK));
1445
1446   GlobalCommands_insert("MakeDetail", FreeCaller<Select_MakeDetail>(), Accelerator('M', (GdkModifierType)GDK_CONTROL_MASK));
1447   GlobalCommands_insert("MakeStructural", FreeCaller<Select_MakeStructural>(), Accelerator('S', (GdkModifierType)(GDK_SHIFT_MASK|GDK_CONTROL_MASK)));
1448 }
1449
1450 void Brush_constructMenu(GtkMenu* menu)
1451 {
1452   create_menu_item_with_mnemonic(menu, "Prism...", "BrushPrism");
1453   create_menu_item_with_mnemonic(menu, "Cone...", "BrushCone");
1454   create_menu_item_with_mnemonic(menu, "Sphere...", "BrushSphere");
1455   menu_separator (menu);
1456   {
1457     GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic (menu, "CSG");
1458     if (g_Layout_enableDetachableMenus.m_value)
1459       menu_tearoff (menu_in_menu);
1460     create_menu_item_with_mnemonic(menu_in_menu, "Make _Hollow", "CSGHollow");
1461     create_menu_item_with_mnemonic(menu_in_menu, "CSG _Subtract", "CSGSubtract");
1462     create_menu_item_with_mnemonic(menu_in_menu, "CSG _Merge", "CSGMerge");
1463   }
1464   menu_separator(menu);
1465   {
1466     GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic (menu, "Clipper");
1467     if (g_Layout_enableDetachableMenus.m_value)
1468       menu_tearoff (menu_in_menu);
1469
1470     create_menu_item_with_mnemonic(menu_in_menu, "Clip selection", "ClipSelected");
1471     create_menu_item_with_mnemonic(menu_in_menu, "Split selection", "SplitSelected");
1472     create_menu_item_with_mnemonic(menu_in_menu, "Flip Clip orientation", "FlipClip");
1473   }
1474   menu_separator(menu);
1475   create_menu_item_with_mnemonic(menu, "Make detail", "MakeDetail");
1476   create_menu_item_with_mnemonic(menu, "Make structural", "MakeStructural");
1477
1478   create_check_menu_item_with_mnemonic(menu, "Texture Lock", "TogTexLock");
1479   menu_separator(menu);
1480   create_menu_item_with_mnemonic(menu, "Copy Face Texture", "FaceCopyTexture");
1481   create_menu_item_with_mnemonic(menu, "Paste Face Texture", "FacePasteTexture");
1482
1483   command_connect_accelerator("Brush3Sided");
1484   command_connect_accelerator("Brush4Sided");
1485   command_connect_accelerator("Brush5Sided");
1486   command_connect_accelerator("Brush6Sided");
1487   command_connect_accelerator("Brush7Sided");
1488   command_connect_accelerator("Brush8Sided");
1489   command_connect_accelerator("Brush9Sided");
1490 }