]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/brushmanip.cpp
Merge commit '8d6828cd68e9104c2c68b962b341b4f572b4bc38' into master-merge
[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 #include <gdk/gdkkeysyms.h>
40
41 void Brush_ConstructCuboid( Brush& brush, const AABB& bounds, const char* shader, const TextureProjection& projection ){
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         return std::max( std::max( extents[0], extents[1] ), extents[2] );
75 }
76
77 inline float max_extent_2d( const Vector3& extents, int axis ){
78         switch ( axis )
79         {
80         case 0:
81                 return std::max( extents[1], extents[2] );
82         case 1:
83                 return std::max( extents[0], extents[2] );
84         default:
85                 return std::max( extents[0], extents[1] );
86         }
87 }
88
89 const std::size_t c_brushPrism_minSides = 3;
90 const std::size_t c_brushPrism_maxSides = c_brush_maxFaces - 2;
91 const char* const c_brushPrism_name = "brushPrism";
92
93 void Brush_ConstructPrism( Brush& brush, const AABB& bounds, std::size_t sides, int axis, const char* shader, const TextureProjection& projection ){
94         if ( sides < c_brushPrism_minSides ) {
95                 globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushPrism_minSides ) << "\n";
96                 return;
97         }
98         if ( sides > c_brushPrism_maxSides ) {
99                 globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushPrism_maxSides ) << "\n";
100                 return;
101         }
102
103         brush.clear();
104         brush.reserve( sides + 2 );
105
106         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
107         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
108
109         float radius = max_extent_2d( bounds.extents, axis );
110         const Vector3& mid = bounds.origin;
111         Vector3 planepts[3];
112
113         planepts[2][( axis + 1 ) % 3] = mins[( axis + 1 ) % 3];
114         planepts[2][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
115         planepts[2][axis] = maxs[axis];
116         planepts[1][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
117         planepts[1][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
118         planepts[1][axis] = maxs[axis];
119         planepts[0][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
120         planepts[0][( axis + 2 ) % 3] = maxs[( axis + 2 ) % 3];
121         planepts[0][axis] = maxs[axis];
122
123         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
124
125         planepts[0][( axis + 1 ) % 3] = mins[( axis + 1 ) % 3];
126         planepts[0][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
127         planepts[0][axis] = mins[axis];
128         planepts[1][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
129         planepts[1][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
130         planepts[1][axis] = mins[axis];
131         planepts[2][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
132         planepts[2][( axis + 2 ) % 3] = maxs[( axis + 2 ) % 3];
133         planepts[2][axis] = mins[axis];
134
135         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
136
137         for ( std::size_t i = 0 ; i < sides ; ++i )
138         {
139                 double sv = sin( i * 3.14159265 * 2 / sides );
140                 double cv = cos( i * 3.14159265 * 2 / sides );
141
142 //              planepts[0][( axis + 1 ) % 3] = static_cast<float>( floor( mid[( axis + 1 ) % 3] + radius * cv + 0.5 ) );
143 //              planepts[0][( axis + 2 ) % 3] = static_cast<float>( floor( mid[( axis + 2 ) % 3] + radius * sv + 0.5 ) );
144                 planepts[0][( axis + 1 ) % 3] = static_cast<float>( mid[( axis + 1 ) % 3] + radius * cv );
145                 planepts[0][( axis + 2 ) % 3] = static_cast<float>( mid[( axis + 2 ) % 3] + radius * sv );
146                 planepts[0][axis] = mins[axis];
147
148                 planepts[1][( axis + 1 ) % 3] = planepts[0][( axis + 1 ) % 3];
149                 planepts[1][( axis + 2 ) % 3] = planepts[0][( axis + 2 ) % 3];
150                 planepts[1][axis] = maxs[axis];
151
152 //              planepts[2][( axis + 1 ) % 3] = static_cast<float>( floor( planepts[0][( axis + 1 ) % 3] - radius * sv + 0.5 ) );
153 //              planepts[2][( axis + 2 ) % 3] = static_cast<float>( floor( planepts[0][( axis + 2 ) % 3] + radius * cv + 0.5 ) );
154                 planepts[2][( axis + 1 ) % 3] = static_cast<float>( planepts[0][( axis + 1 ) % 3] - radius * sv );
155                 planepts[2][( axis + 2 ) % 3] = static_cast<float>( planepts[0][( axis + 2 ) % 3] + radius * cv );
156                 planepts[2][axis] = maxs[axis];
157                 //globalOutputStream() << planepts[0] << "   " << planepts[2] << "  #" << i << "   sin " << sv << "  cos " << cv << "\n";
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 = c_brush_maxFaces - 1;
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         if ( sides < c_brushCone_minSides ) {
169                 globalErrorStream() << c_brushCone_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushCone_minSides ) << "\n";
170                 return;
171         }
172         if ( sides > c_brushCone_maxSides ) {
173                 globalErrorStream() << c_brushCone_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushCone_maxSides ) << "\n";
174                 return;
175         }
176
177         brush.clear();
178         brush.reserve( sides + 1 );
179
180         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
181         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
182
183         float radius = max_extent( bounds.extents );
184         const Vector3& mid = bounds.origin;
185         Vector3 planepts[3];
186
187         planepts[0][0] = mins[0]; planepts[0][1] = mins[1]; planepts[0][2] = mins[2];
188         planepts[1][0] = maxs[0]; planepts[1][1] = mins[1]; planepts[1][2] = mins[2];
189         planepts[2][0] = maxs[0]; planepts[2][1] = maxs[1]; planepts[2][2] = mins[2];
190
191         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
192
193         for ( std::size_t i = 0 ; i < sides ; ++i )
194         {
195                 double sv = sin( i * 3.14159265 * 2 / sides );
196                 double cv = cos( i * 3.14159265 * 2 / sides );
197
198                 planepts[0][0] = static_cast<float>( mid[0] + radius * cv );
199                 planepts[0][1] = static_cast<float>( mid[1] + radius * sv );
200 //              planepts[0][0] = static_cast<float>( floor( mid[0] + radius * cv + 0.5 ) );
201 //              planepts[0][1] = static_cast<float>( floor( mid[1] + radius * sv + 0.5 ) );
202                 planepts[0][2] = mins[2];
203
204                 planepts[1][0] = mid[0];
205                 planepts[1][1] = mid[1];
206                 planepts[1][2] = maxs[2];
207
208                 planepts[2][0] = static_cast<float>( planepts[0][0] - radius * sv );
209                 planepts[2][1] = static_cast<float>( planepts[0][1] + radius * cv );
210 //              planepts[2][0] = static_cast<float>( floor( planepts[0][0] - radius * sv + 0.5 ) );
211 //              planepts[2][1] = static_cast<float>( floor( planepts[0][1] + radius * cv + 0.5 ) );
212                 planepts[2][2] = maxs[2];
213
214                 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
215         }
216 }
217
218 const std::size_t c_brushSphere_minSides = 3;
219 const std::size_t c_brushSphere_maxSides = 31;
220 const char* const c_brushSphere_name = "brushSphere";
221
222 void Brush_ConstructSphere( Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
223         if ( sides < c_brushSphere_minSides ) {
224                 globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushSphere_minSides ) << "\n";
225                 return;
226         }
227         if ( sides > c_brushSphere_maxSides ) {
228                 globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushSphere_maxSides ) << "\n";
229                 return;
230         }
231
232         brush.clear();
233         brush.reserve( sides * sides );
234
235         float radius = max_extent( bounds.extents );
236         const Vector3& mid = bounds.origin;
237         Vector3 planepts[3];
238
239         double dt = 2 * c_pi / sides;
240         double dp = c_pi / sides;
241         for ( std::size_t i = 0; i < sides; i++ )
242         {
243                 for ( std::size_t j = 0; j < sides - 1; j++ )
244                 {
245                         double t = i * dt;
246                         double p = float(j * dp - c_pi / 2);
247
248                         planepts[0] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p ), radius ) );
249                         planepts[1] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p + dp ), radius ) );
250                         planepts[2] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p + dp ), radius ) );
251
252                         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
253                 }
254         }
255
256         {
257                 double p = ( sides - 1 ) * dp - c_pi / 2;
258                 for ( std::size_t i = 0; i < sides; i++ )
259                 {
260                         double t = i * dt;
261
262                         planepts[0] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p ), radius ) );
263                         planepts[1] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p + dp ), radius ) );
264                         planepts[2] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p ), radius ) );
265
266                         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
267                 }
268         }
269 }
270
271 const std::size_t c_brushRock_minSides = 10;
272 const std::size_t c_brushRock_maxSides = 1000;
273 const char* const c_brushRock_name = "brushRock";
274
275 void Brush_ConstructRock( Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
276         if ( sides < c_brushRock_minSides ) {
277                 globalErrorStream() << c_brushRock_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushRock_minSides ) << "\n";
278                 return;
279         }
280         if ( sides > c_brushRock_maxSides ) {
281                 globalErrorStream() << c_brushRock_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushRock_maxSides ) << "\n";
282                 return;
283         }
284
285         brush.clear();
286         brush.reserve( sides * sides );
287
288         float radius = max_extent( bounds.extents );
289         const Vector3& mid = bounds.origin;
290         Vector3 planepts[3];
291
292         for ( std::size_t j = 0; j < sides; j++ )
293         {
294                 planepts[0][0] = rand() - ( RAND_MAX / 2 );
295                 planepts[0][1] = rand() - ( RAND_MAX / 2 );
296                 planepts[0][2] = rand() - ( RAND_MAX / 2 );
297                 vector3_normalise( planepts[0] );
298
299                 // find two vectors that are perpendicular to planepts[0]
300                 ComputeAxisBase( planepts[0], planepts[1], planepts[2] );
301
302                 planepts[0] = vector3_added( mid, vector3_scaled( planepts[0], radius ) );
303                 planepts[1] = vector3_added( planepts[0], vector3_scaled( planepts[1], radius ) );
304                 planepts[2] = vector3_added( planepts[0], vector3_scaled( planepts[2], radius ) );
305
306 #if 0
307                 // make sure the orientation is right
308                 if ( vector3_dot( vector3_subtracted( planepts[0], mid ), vector3_cross( vector3_subtracted( planepts[1], mid ), vector3_subtracted( planepts[2], mid ) ) ) > 0 ) {
309                         Vector3 h;
310                         h = planepts[1];
311                         planepts[1] = planepts[2];
312                         planepts[2] = h;
313                         globalOutputStream() << "flip\n";
314                 }
315                 else{
316                         globalOutputStream() << "no flip\n";
317                 }
318 #endif
319
320                 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
321         }
322 }
323
324 int GetViewAxis(){
325         switch ( GlobalXYWnd_getCurrentViewType() )
326         {
327         case XY:
328                 return 2;
329         case XZ:
330                 return 1;
331         case YZ:
332                 return 0;
333         }
334         return 2;
335 }
336
337 void Brush_ConstructPrefab( Brush& brush, EBrushPrefab type, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
338         switch ( type )
339         {
340         case eBrushCuboid:
341         {
342                 UndoableCommand undo( "brushCuboid" );
343
344                 Brush_ConstructCuboid( brush, bounds, shader, projection );
345         }
346         break;
347         case eBrushPrism:
348         {
349                 int axis = GetViewAxis();
350                 StringOutputStream command;
351                 command << c_brushPrism_name << " -sides " << Unsigned( sides ) << " -axis " << axis;
352                 UndoableCommand undo( command.c_str() );
353
354                 Brush_ConstructPrism( brush, bounds, sides, axis, shader, projection );
355         }
356         break;
357         case eBrushCone:
358         {
359                 StringOutputStream command;
360                 command << c_brushCone_name << " -sides " << Unsigned( sides );
361                 UndoableCommand undo( command.c_str() );
362
363                 Brush_ConstructCone( brush, bounds, sides, shader, projection );
364         }
365         break;
366         case eBrushSphere:
367         {
368                 StringOutputStream command;
369                 command << c_brushSphere_name << " -sides " << Unsigned( sides );
370                 UndoableCommand undo( command.c_str() );
371
372                 Brush_ConstructSphere( brush, bounds, sides, shader, projection );
373         }
374         break;
375         case eBrushRock:
376         {
377                 StringOutputStream command;
378                 command << c_brushRock_name << " -sides " << Unsigned( sides );
379                 UndoableCommand undo( command.c_str() );
380
381                 Brush_ConstructRock( brush, bounds, sides, shader, projection );
382         }
383         break;
384         }
385 }
386
387
388 void ConstructRegionBrushes( scene::Node* brushes[6], const Vector3& region_mins, const Vector3& region_maxs ){
389         {
390                 // set mins
391                 Vector3 mins( region_mins[0] - 32, region_mins[1] - 32, region_mins[2] - 32 );
392
393                 // vary maxs
394                 for ( std::size_t i = 0; i < 3; i++ )
395                 {
396                         Vector3 maxs( region_maxs[0] + 32, region_maxs[1] + 32, region_maxs[2] + 32 );
397                         maxs[i] = region_mins[i];
398                         Brush_ConstructCuboid( *Node_getBrush( *brushes[i] ), aabb_for_minmax( mins, maxs ), texdef_name_default(), TextureProjection() );
399                 }
400         }
401
402         {
403                 // set maxs
404                 Vector3 maxs( region_maxs[0] + 32, region_maxs[1] + 32, region_maxs[2] + 32 );
405
406                 // vary mins
407                 for ( std::size_t i = 0; i < 3; i++ )
408                 {
409                         Vector3 mins( region_mins[0] - 32, region_mins[1] - 32, region_mins[2] - 32 );
410                         mins[i] = region_maxs[i];
411                         Brush_ConstructCuboid( *Node_getBrush( *brushes[i + 3] ), aabb_for_minmax( mins, maxs ), texdef_name_default(), TextureProjection() );
412                 }
413         }
414 }
415
416
417 void Scene_BrushSetTexdef_Selected( scene::Graph& graph, const TextureProjection& projection ){
418         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
419                 face.SetTexdef(projection);
420         });
421         SceneChangeNotify();
422 }
423
424 void Scene_BrushSetTexdef_Component_Selected( scene::Graph& graph, const TextureProjection& projection ){
425         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
426                 face.SetTexdef(projection);
427         });
428         SceneChangeNotify();
429 }
430
431
432 void Scene_BrushSetFlags_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
433         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
434                 face.SetFlags(flags);
435         });
436         SceneChangeNotify();
437 }
438
439 void Scene_BrushSetFlags_Component_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
440         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
441                 face.SetFlags(flags);
442         });
443         SceneChangeNotify();
444 }
445
446 void Scene_BrushShiftTexdef_Selected( scene::Graph& graph, float s, float t ){
447         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
448                 face.ShiftTexdef(s, t);
449         });
450         SceneChangeNotify();
451 }
452
453 void Scene_BrushShiftTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
454         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
455                 face.ShiftTexdef(s, t);
456         });
457         SceneChangeNotify();
458 }
459
460 void Scene_BrushScaleTexdef_Selected( scene::Graph& graph, float s, float t ){
461         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
462                 face.ScaleTexdef(s, t);
463         });
464         SceneChangeNotify();
465 }
466
467 void Scene_BrushScaleTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
468         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
469                 face.ScaleTexdef(s, t);
470         });
471         SceneChangeNotify();
472 }
473
474 void Scene_BrushRotateTexdef_Selected( scene::Graph& graph, float angle ){
475         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
476                 face.RotateTexdef(angle);
477         });
478         SceneChangeNotify();
479 }
480
481 void Scene_BrushRotateTexdef_Component_Selected( scene::Graph& graph, float angle ){
482         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
483                 face.RotateTexdef(angle);
484         });
485         SceneChangeNotify();
486 }
487
488
489 void Scene_BrushSetShader_Selected( scene::Graph& graph, const char* name ){
490         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
491                 face.SetShader(name);
492         });
493         SceneChangeNotify();
494 }
495
496 void Scene_BrushSetShader_Component_Selected( scene::Graph& graph, const char* name ){
497         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
498                 face.SetShader(name);
499         });
500         SceneChangeNotify();
501 }
502
503 void Scene_BrushSetDetail_Selected( scene::Graph& graph, bool detail ){
504         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
505                 face.setDetail(detail);
506         });
507         SceneChangeNotify();
508 }
509
510 bool Face_FindReplaceShader( Face& face, const char* find, const char* replace ){
511         if ( shader_equal( face.GetShader(), find ) ) {
512                 face.SetShader( replace );
513                 return true;
514         }
515         return false;
516 }
517
518 bool DoingSearch( const char *repl ){
519         return ( repl == NULL || ( strcmp( "textures/", repl ) == 0 ) );
520 }
521
522 void Scene_BrushFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
523         if ( DoingSearch( replace ) ) {
524                 Scene_ForEachBrush_ForEachFaceInstance(graph, [&](FaceInstance &faceinst) {
525                         if (shader_equal(faceinst.getFace().GetShader(), find)) {
526                                 faceinst.setSelected(SelectionSystem::eFace, true);
527                         }
528                 });
529         }
530         else
531         {
532                 Scene_ForEachBrush_ForEachFace(graph, [&](Face &face) { Face_FindReplaceShader(face, find, replace); });
533         }
534 }
535
536 void Scene_BrushFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
537         if ( DoingSearch( replace ) ) {
538                 Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, [&](FaceInstance &faceinst) {
539                         if (shader_equal(faceinst.getFace().GetShader(), find)) {
540                                 faceinst.setSelected(SelectionSystem::eFace, true);
541                         }
542                 });
543         }
544         else
545         {
546                 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
547                         Face_FindReplaceShader(face, find, replace);
548                 });
549         }
550 }
551
552 // TODO: find for components
553 // d1223m: dont even know what they are...
554 void Scene_BrushFindReplaceShader_Component_Selected( scene::Graph& graph, const char* find, const char* replace ){
555         if ( DoingSearch( replace ) ) {
556
557         }
558         else
559         {
560                 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
561                         Face_FindReplaceShader(face, find, replace);
562                 });
563         }
564 }
565
566
567 void Scene_BrushFitTexture_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
568         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
569                 face.FitTexture(s_repeat, t_repeat);
570         });
571         SceneChangeNotify();
572 }
573
574 void Scene_BrushFitTexture_Component_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
575         Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
576                 face.FitTexture(s_repeat, t_repeat);
577         });
578         SceneChangeNotify();
579 }
580
581 TextureProjection g_defaultTextureProjection;
582
583 const TextureProjection& TextureTransform_getDefault(){
584         TexDef_Construct_Default( g_defaultTextureProjection );
585         return g_defaultTextureProjection;
586 }
587
588 void Scene_BrushConstructPrefab( scene::Graph& graph, EBrushPrefab type, std::size_t sides, const char* shader ){
589         if ( GlobalSelectionSystem().countSelected() != 0 ) {
590                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
591
592                 Brush* brush = Node_getBrush( path.top() );
593                 if ( brush != 0 ) {
594                         AABB bounds = brush->localAABB(); // copy bounds because the brush will be modified
595                         Brush_ConstructPrefab( *brush, type, bounds, sides, shader, TextureTransform_getDefault() );
596                         SceneChangeNotify();
597                 }
598         }
599 }
600
601 void Scene_BrushResize_Selected( scene::Graph& graph, const AABB& bounds, const char* shader ){
602         if ( GlobalSelectionSystem().countSelected() != 0 ) {
603                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
604
605                 Brush* brush = Node_getBrush( path.top() );
606                 if ( brush != 0 ) {
607                         Brush_ConstructCuboid( *brush, bounds, shader, TextureTransform_getDefault() );
608                         SceneChangeNotify();
609                 }
610         }
611 }
612
613 bool Brush_hasShader( const Brush& brush, const char* name ){
614         for ( Brush::const_iterator i = brush.begin(); i != brush.end(); ++i )
615         {
616                 if ( shader_equal( ( *i )->GetShader(), name ) ) {
617                         return true;
618                 }
619         }
620         return false;
621 }
622
623 class BrushSelectByShaderWalker : public scene::Graph::Walker
624 {
625 const char* m_name;
626 public:
627 BrushSelectByShaderWalker( const char* name )
628         : m_name( name ){
629 }
630
631 bool pre( const scene::Path& path, scene::Instance& instance ) const {
632         if ( path.top().get().visible() ) {
633                 Brush* brush = Node_getBrush( path.top() );
634                 if ( brush != 0 && Brush_hasShader( *brush, m_name ) ) {
635                         Instance_getSelectable( instance )->setSelected( true );
636                 }
637         }
638         else{
639                 return false;
640         }
641         return true;
642 }
643 };
644
645 void Scene_BrushSelectByShader( scene::Graph& graph, const char* name ){
646         graph.traverse( BrushSelectByShaderWalker( name ) );
647 }
648
649 void Scene_BrushSelectByShader_Component( scene::Graph& graph, const char* name ){
650         Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, [&](FaceInstance &face) {
651                 printf("checking %s = %s\n", face.getFace().GetShader(), name);
652                 if (shader_equal(face.getFace().GetShader(), name)) {
653                 face.setSelected( SelectionSystem::eFace, true );
654         }
655         });
656 }
657
658 void Scene_BrushGetTexdef_Selected( scene::Graph& graph, TextureProjection& projection ){
659         bool done = false;
660         Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
661                 if (!done) {
662                         done = true;
663                         face.GetTexdef(projection);
664 }
665         });
666 }
667
668 void Scene_BrushGetTexdef_Component_Selected( scene::Graph& graph, TextureProjection& projection ){
669 #if 1
670         if ( !g_SelectedFaceInstances.empty() ) {
671                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
672                 faceInstance.getFace().GetTexdef( projection );
673         }
674 #else
675         FaceGetTexdef visitor( projection );
676         Scene_ForEachSelectedBrushFace( graph, visitor );
677 #endif
678 }
679
680 void Scene_BrushGetShaderSize_Component_Selected( scene::Graph& graph, size_t& width, size_t& height ){
681         if ( !g_SelectedFaceInstances.empty() ) {
682                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
683                 width = faceInstance.getFace().getShader().width();
684                 height = faceInstance.getFace().getShader().height();
685         }
686 }
687
688
689 void Scene_BrushGetFlags_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
690 #if 1
691         if ( GlobalSelectionSystem().countSelected() != 0 ) {
692                 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
693                 if ( brush != 0 ) {
694                         bool done = false;
695                         Brush_forEachFace(*brush, [&](Face &face) {
696                                 if (!done) {
697                                         done = true;
698                                         face.GetFlags(flags);
699                                 }
700                         });
701                 }
702         }
703 #else
704         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetFlags( flags ) );
705 #endif
706 }
707
708 void Scene_BrushGetFlags_Component_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
709 #if 1
710         if ( !g_SelectedFaceInstances.empty() ) {
711                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
712                 faceInstance.getFace().GetFlags( flags );
713         }
714 #else
715         Scene_ForEachSelectedBrushFace( graph, FaceGetFlags( flags ) );
716 #endif
717 }
718
719
720 void Scene_BrushGetShader_Selected( scene::Graph& graph, CopiedString& shader ){
721 #if 1
722         if ( GlobalSelectionSystem().countSelected() != 0 ) {
723                 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
724                 if ( brush != 0 ) {
725                         bool done = false;
726                         Brush_forEachFace(*brush, [&](Face &face) {
727                                 if (!done) {
728                                         done = true;
729                                         shader = face.GetShader();
730                                 }
731                         });
732                 }
733         }
734 #else
735         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetShader( shader ) );
736 #endif
737 }
738
739 void Scene_BrushGetShader_Component_Selected( scene::Graph& graph, CopiedString& shader ){
740 #if 1
741         if ( !g_SelectedFaceInstances.empty() ) {
742                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
743                 shader = faceInstance.getFace().GetShader();
744         }
745 #else
746         FaceGetShader visitor( shader );
747         Scene_ForEachSelectedBrushFace( graph, visitor );
748 #endif
749 }
750
751
752 class filter_face_shader : public FaceFilter
753 {
754 const char* m_shader;
755 public:
756 filter_face_shader( const char* shader ) : m_shader( shader ){
757 }
758
759 bool filter( const Face& face ) const {
760         return shader_equal( face.GetShader(), m_shader );
761 }
762 };
763
764 class filter_face_shader_prefix : public FaceFilter
765 {
766 const char* m_prefix;
767 public:
768 filter_face_shader_prefix( const char* prefix ) : m_prefix( prefix ){
769 }
770
771 bool filter( const Face& face ) const {
772         return shader_equal_n( face.GetShader(), m_prefix, strlen( m_prefix ) );
773 }
774 };
775
776 class filter_face_flags : public FaceFilter
777 {
778 int m_flags;
779 public:
780 filter_face_flags( int flags ) : m_flags( flags ){
781 }
782
783 bool filter( const Face& face ) const {
784         return ( face.getShader().shaderFlags() & m_flags ) != 0;
785 }
786 };
787
788 class filter_face_contents : public FaceFilter
789 {
790 int m_contents;
791 public:
792 filter_face_contents( int contents ) : m_contents( contents ){
793 }
794
795 bool filter( const Face& face ) const {
796         return ( face.getShader().m_flags.m_contentFlags & m_contents ) != 0;
797 }
798 };
799
800
801
802 class filter_brush_any_face : public BrushFilter
803 {
804 FaceFilter* m_filter;
805 public:
806 filter_brush_any_face( FaceFilter* filter ) : m_filter( filter ){
807 }
808
809 bool filter( const Brush& brush ) const {
810         bool filtered = false;
811         Brush_forEachFace(brush, [&](Face &face) {
812                 if (m_filter->filter(face)) {
813                         filtered = true;
814                 }
815         });
816         return filtered;
817 }
818 };
819
820 class filter_brush_all_faces : public BrushFilter
821 {
822 FaceFilter* m_filter;
823 public:
824 filter_brush_all_faces( FaceFilter* filter ) : m_filter( filter ){
825 }
826 bool filter( const Brush& brush ) const {
827         bool filtered = true;
828         Brush_forEachFace(brush, [&](Face &face) {
829                 if (!m_filter->filter(face)) {
830                         filtered = false;
831                 }
832         });
833         return filtered;
834 }
835 };
836
837
838 filter_face_flags g_filter_face_clip( QER_CLIP );
839 filter_brush_all_faces g_filter_brush_clip( &g_filter_face_clip );
840
841 filter_face_shader g_filter_face_clip_q2( "textures/clip" );
842 filter_brush_all_faces g_filter_brush_clip_q2( &g_filter_face_clip_q2 );
843
844 filter_face_shader g_filter_face_weapclip( "textures/common/weapclip" );
845 filter_brush_all_faces g_filter_brush_weapclip( &g_filter_face_weapclip );
846
847 filter_face_shader g_filter_face_commonclip( "textures/common/clip" );
848 filter_brush_all_faces g_filter_brush_commonclip( &g_filter_face_commonclip );
849
850 filter_face_shader g_filter_face_fullclip( "textures/common/fullclip" );
851 filter_brush_all_faces g_filter_brush_fullclip( &g_filter_face_fullclip );
852
853 filter_face_shader g_filter_face_botclip( "textures/common/botclip" );
854 filter_brush_all_faces g_filter_brush_botclip( &g_filter_face_botclip );
855
856 filter_face_shader_prefix g_filter_face_caulk( "textures/common/caulk" );
857 filter_brush_all_faces g_filter_brush_caulk( &g_filter_face_caulk );
858
859 filter_face_shader_prefix g_filter_face_caulk_ja( "textures/system/caulk" );
860 filter_brush_all_faces g_filter_brush_caulk_ja( &g_filter_face_caulk_ja );
861
862 filter_face_flags g_filter_face_liquids( QER_LIQUID );
863 filter_brush_any_face g_filter_brush_liquids( &g_filter_face_liquids );
864
865 filter_face_shader_prefix g_filter_face_liquidsdir( "textures/liquids/" );
866 filter_brush_any_face g_filter_brush_liquidsdir( &g_filter_face_liquidsdir );
867
868 filter_face_shader g_filter_face_hint( "textures/common/hint" );
869 filter_brush_any_face g_filter_brush_hint( &g_filter_face_hint );
870
871 filter_face_shader g_filter_face_hintlocal( "textures/common/hintlocal" );
872 filter_brush_any_face g_filter_brush_hintlocal( &g_filter_face_hintlocal );
873
874 filter_face_shader g_filter_face_hint_q2( "textures/hint" );
875 filter_brush_any_face g_filter_brush_hint_q2( &g_filter_face_hint_q2 );
876
877 filter_face_shader g_filter_face_hint_ja( "textures/system/hint" );
878 filter_brush_any_face g_filter_brush_hint_ja( &g_filter_face_hint_ja );
879
880 filter_face_shader g_filter_face_subtlehint( "textures/common/subtlehint" );
881 filter_brush_any_face g_filter_brush_subtlehint( &g_filter_face_subtlehint );
882
883 filter_face_shader g_filter_face_areaportal( "textures/common/areaportal" );
884 filter_brush_any_face g_filter_brush_areaportal( &g_filter_face_areaportal );
885
886 filter_face_shader g_filter_face_visportal( "textures/editor/visportal" );
887 filter_brush_any_face g_filter_brush_visportal( &g_filter_face_visportal );
888
889 filter_face_shader g_filter_face_clusterportal( "textures/common/clusterportal" );
890 filter_brush_all_faces g_filter_brush_clusterportal( &g_filter_face_clusterportal );
891
892 filter_face_shader g_filter_face_lightgrid( "textures/common/lightgrid" );
893 filter_brush_all_faces g_filter_brush_lightgrid( &g_filter_face_lightgrid );
894
895 filter_face_flags g_filter_face_translucent( QER_TRANS | QER_ALPHATEST );
896 filter_brush_any_face g_filter_brush_translucent( &g_filter_face_translucent );
897
898 filter_face_contents g_filter_face_detail( BRUSH_DETAIL_MASK );
899 filter_brush_all_faces g_filter_brush_detail( &g_filter_face_detail );
900
901 filter_face_shader_prefix g_filter_face_decals( "textures/decals/" );
902 filter_brush_any_face g_filter_brush_decals( &g_filter_face_decals );
903
904
905 void BrushFilters_construct(){
906         add_brush_filter( g_filter_brush_clip, EXCLUDE_CLIP );
907         add_brush_filter( g_filter_brush_clip_q2, EXCLUDE_CLIP );
908         add_brush_filter( g_filter_brush_weapclip, EXCLUDE_CLIP );
909         add_brush_filter( g_filter_brush_fullclip, EXCLUDE_CLIP );
910         add_brush_filter( g_filter_brush_commonclip, EXCLUDE_CLIP );
911         add_brush_filter( g_filter_brush_botclip, EXCLUDE_BOTCLIP );
912         add_brush_filter( g_filter_brush_caulk, EXCLUDE_CAULK );
913         add_brush_filter( g_filter_brush_caulk_ja, EXCLUDE_CAULK );
914         add_face_filter( g_filter_face_caulk, EXCLUDE_CAULK );
915         add_face_filter( g_filter_face_caulk_ja, EXCLUDE_CAULK );
916         add_brush_filter( g_filter_brush_liquids, EXCLUDE_LIQUIDS );
917         add_brush_filter( g_filter_brush_liquidsdir, EXCLUDE_LIQUIDS );
918         add_brush_filter( g_filter_brush_hint, EXCLUDE_HINTSSKIPS );
919         add_brush_filter( g_filter_brush_hintlocal, EXCLUDE_HINTSSKIPS );
920         add_brush_filter( g_filter_brush_hint_q2, EXCLUDE_HINTSSKIPS );
921         add_brush_filter( g_filter_brush_hint_ja, EXCLUDE_HINTSSKIPS );
922         add_brush_filter( g_filter_brush_subtlehint, EXCLUDE_HINTSSKIPS );
923         add_brush_filter( g_filter_brush_clusterportal, EXCLUDE_CLUSTERPORTALS );
924         add_brush_filter( g_filter_brush_visportal, EXCLUDE_VISPORTALS );
925         add_brush_filter( g_filter_brush_areaportal, EXCLUDE_AREAPORTALS );
926         add_brush_filter( g_filter_brush_translucent, EXCLUDE_TRANSLUCENT );
927         add_brush_filter( g_filter_brush_detail, EXCLUDE_DETAILS );
928         add_brush_filter( g_filter_brush_detail, EXCLUDE_STRUCTURAL, true );
929         add_brush_filter( g_filter_brush_lightgrid, EXCLUDE_LIGHTGRID );
930         add_brush_filter( g_filter_brush_decals, EXCLUDE_DECALS );
931 }
932
933 #if 0
934
935 void normalquantisation_draw(){
936         glPointSize( 1 );
937         glBegin( GL_POINTS );
938         for ( std::size_t i = 0; i <= c_quantise_normal; ++i )
939         {
940                 for ( std::size_t j = 0; j <= c_quantise_normal; ++j )
941                 {
942                         Normal3f vertex( normal3f_normalised( Normal3f(
943                                                                                                           static_cast<float>( c_quantise_normal - j - i ),
944                                                                                                           static_cast<float>( i ),
945                                                                                                           static_cast<float>( j )
946                                                                                                           ) ) );
947                         VectorScale( normal3f_to_array( vertex ), 64.f, normal3f_to_array( vertex ) );
948                         glVertex3fv( normal3f_to_array( vertex ) );
949                         vertex.x = -vertex.x;
950                         glVertex3fv( normal3f_to_array( vertex ) );
951                 }
952         }
953         glEnd();
954 }
955
956 class RenderableNormalQuantisation : public OpenGLRenderable
957 {
958 public:
959 void render( RenderStateFlags state ) const {
960         normalquantisation_draw();
961 }
962 };
963
964 const float g_test_quantise_normal = 1.f / static_cast<float>( 1 << 3 );
965
966 class TestNormalQuantisation
967 {
968 void check_normal( const Normal3f& normal, const Normal3f& other ){
969         spherical_t spherical = spherical_from_normal3f( normal );
970         double longditude = RAD2DEG( spherical.longditude );
971         double latitude = RAD2DEG( spherical.latitude );
972         double x = cos( spherical.longditude ) * sin( spherical.latitude );
973         double y = sin( spherical.longditude ) * sin( spherical.latitude );
974         double z = cos( spherical.latitude );
975
976         ASSERT_MESSAGE( normal3f_dot( normal, other ) > 0.99, "bleh" );
977 }
978
979 void test_normal( const Normal3f& normal ){
980         Normal3f test = normal3f_from_spherical( spherical_from_normal3f( normal ) );
981         check_normal( normal, test );
982
983         EOctant octant = normal3f_classify_octant( normal );
984         Normal3f folded = normal3f_fold_octant( normal, octant );
985         ESextant sextant = normal3f_classify_sextant( folded );
986         folded = normal3f_fold_sextant( folded, sextant );
987
988         double scale = static_cast<float>( c_quantise_normal ) / ( folded.x + folded.y + folded.z );
989
990         double zbits = folded.z * scale;
991         double ybits = folded.y * scale;
992
993         std::size_t zbits_q = static_cast<std::size_t>( zbits );
994         std::size_t ybits_q = static_cast<std::size_t>( ybits );
995
996         ASSERT_MESSAGE( zbits_q <= ( c_quantise_normal / 8 ) * 3, "bleh" );
997         ASSERT_MESSAGE( ybits_q <= ( c_quantise_normal / 2 ), "bleh" );
998         ASSERT_MESSAGE( zbits_q + ( ( c_quantise_normal / 2 ) - ybits_q ) <= ( c_quantise_normal / 2 ), "bleh" );
999
1000         std::size_t y_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? ybits_q : ( c_quantise_normal / 2 ) - ybits_q;
1001         std::size_t z_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? zbits_q : ( c_quantise_normal / 2 ) - zbits_q;
1002         std::size_t index = ( c_quantise_normal / 4 ) * y_t + z_t;
1003         ASSERT_MESSAGE( index <= ( c_quantise_normal / 4 ) * ( c_quantise_normal / 2 ), "bleh" );
1004
1005         Normal3f tmp( c_quantise_normal - zbits_q - ybits_q, ybits_q, zbits_q );
1006         tmp = normal3f_normalised( tmp );
1007
1008         Normal3f unfolded = normal3f_unfold_octant( normal3f_unfold_sextant( tmp, sextant ), octant );
1009
1010         check_normal( normal, unfolded );
1011
1012         double dot = normal3f_dot( normal, unfolded );
1013         float length = VectorLength( normal3f_to_array( unfolded ) );
1014         float inv_length = 1 / length;
1015
1016         Normal3f quantised = normal3f_quantised( normal );
1017         check_normal( normal, quantised );
1018 }
1019 void test2( const Normal3f& normal, const Normal3f& other ){
1020         if ( normal3f_quantised( normal ) != normal3f_quantised( other ) ) {
1021                 int bleh = 0;
1022         }
1023 }
1024
1025 static Normal3f normalise( float x, float y, float z ){
1026         return normal3f_normalised( Normal3f( x, y, z ) );
1027 }
1028
1029 float vec_rand(){
1030         return static_cast<float>( rand() - ( RAND_MAX / 2 ) );
1031 }
1032
1033 Normal3f normal3f_rand(){
1034         return normalise( vec_rand(), vec_rand(), vec_rand() );
1035 }
1036
1037 public:
1038 TestNormalQuantisation(){
1039         for ( int i = 4096; i > 0; --i )
1040                 test_normal( normal3f_rand() );
1041
1042         test_normal( normalise( 1, 0, 0 ) );
1043         test_normal( normalise( 0, 1, 0 ) );
1044         test_normal( normalise( 0, 0, 1 ) );
1045         test_normal( normalise( 1, 1, 0 ) );
1046         test_normal( normalise( 1, 0, 1 ) );
1047         test_normal( normalise( 0, 1, 1 ) );
1048
1049         test_normal( normalise( 10000, 10000, 10000 ) );
1050         test_normal( normalise( 10000, 10000, 10001 ) );
1051         test_normal( normalise( 10000, 10000, 10002 ) );
1052         test_normal( normalise( 10000, 10000, 10010 ) );
1053         test_normal( normalise( 10000, 10000, 10020 ) );
1054         test_normal( normalise( 10000, 10000, 10030 ) );
1055         test_normal( normalise( 10000, 10000, 10100 ) );
1056         test_normal( normalise( 10000, 10000, 10101 ) );
1057         test_normal( normalise( 10000, 10000, 10102 ) );
1058         test_normal( normalise( 10000, 10000, 10200 ) );
1059         test_normal( normalise( 10000, 10000, 10201 ) );
1060         test_normal( normalise( 10000, 10000, 10202 ) );
1061         test_normal( normalise( 10000, 10000, 10203 ) );
1062         test_normal( normalise( 10000, 10000, 10300 ) );
1063
1064
1065         test2( normalise( 10000, 10000, 10000 ), normalise( 10000, 10000, 10001 ) );
1066         test2( normalise( 10000, 10000, 10001 ), normalise( 10000, 10001, 10000 ) );
1067 }
1068 };
1069
1070 TestNormalQuantisation g_testNormalQuantisation;
1071
1072
1073 #endif
1074
1075 #if 0
1076 class TestSelectableObserver : public observer_template<const Selectable&>
1077 {
1078 public:
1079 void notify( const Selectable& arguments ){
1080         bool bleh = arguments.isSelected();
1081 }
1082 };
1083
1084 inline void test_bleh(){
1085         TestSelectableObserver test;
1086         ObservableSelectableInstance< SingleObservable< SelectionChangeCallback > > bleh;
1087         bleh.attach( test );
1088         bleh.setSelected( true );
1089         bleh.detach( test );
1090 }
1091
1092 class TestBleh
1093 {
1094 public:
1095 TestBleh(){
1096         test_bleh();
1097 }
1098 };
1099
1100 const TestBleh testbleh;
1101 #endif
1102
1103
1104 #if 0
1105 class TestRefcountedString
1106 {
1107 public:
1108 TestRefcountedString(){
1109         {
1110                 // copy construct
1111                 SmartString string1( "string1" );
1112                 SmartString string2( string1 );
1113                 SmartString string3( string2 );
1114         }
1115         {
1116                 // refcounted assignment
1117                 SmartString string1( "string1" );
1118                 SmartString string2( "string2" );
1119                 string1 = string2;
1120         }
1121         {
1122                 // copy assignment
1123                 SmartString string1( "string1" );
1124                 SmartString string2( "string2" );
1125                 string1 = string2.c_str();
1126         }
1127         {
1128                 // self-assignment
1129                 SmartString string1( "string1" );
1130                 string1 = string1;
1131         }
1132         {
1133                 // self-assignment via another reference
1134                 SmartString string1( "string1" );
1135                 SmartString string2( string1 );
1136                 string1 = string2;
1137         }
1138 }
1139 };
1140
1141 const TestRefcountedString g_testRefcountedString;
1142
1143 #endif
1144
1145 void Select_MakeDetail(){
1146         UndoableCommand undo( "brushSetDetail" );
1147         Scene_BrushSetDetail_Selected( GlobalSceneGraph(), true );
1148 }
1149
1150 void Select_MakeStructural(){
1151         UndoableCommand undo( "brushClearDetail" );
1152         Scene_BrushSetDetail_Selected( GlobalSceneGraph(), false );
1153 }
1154
1155 class BrushMakeSided
1156 {
1157 std::size_t m_count;
1158 public:
1159 BrushMakeSided( std::size_t count )
1160         : m_count( count ){
1161 }
1162
1163 void set(){
1164         Scene_BrushConstructPrefab( GlobalSceneGraph(), eBrushPrism, m_count, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
1165 }
1166
1167 typedef MemberCaller<BrushMakeSided, void(), &BrushMakeSided::set> SetCaller;
1168 };
1169
1170
1171 BrushMakeSided g_brushmakesided3( 3 );
1172 BrushMakeSided g_brushmakesided4( 4 );
1173 BrushMakeSided g_brushmakesided5( 5 );
1174 BrushMakeSided g_brushmakesided6( 6 );
1175 BrushMakeSided g_brushmakesided7( 7 );
1176 BrushMakeSided g_brushmakesided8( 8 );
1177 BrushMakeSided g_brushmakesided9( 9 );
1178
1179 inline int axis_for_viewtype( int viewtype ){
1180         switch ( viewtype )
1181         {
1182         case XY:
1183                 return 2;
1184         case XZ:
1185                 return 1;
1186         case YZ:
1187                 return 0;
1188         }
1189         return 2;
1190 }
1191
1192 class BrushPrefab
1193 {
1194 EBrushPrefab m_type;
1195 public:
1196 BrushPrefab( EBrushPrefab type )
1197         : m_type( type ){
1198 }
1199
1200 void set(){
1201         DoSides( m_type, axis_for_viewtype( GetViewAxis() ) );
1202 }
1203
1204 typedef MemberCaller<BrushPrefab, void(), &BrushPrefab::set> SetCaller;
1205 };
1206
1207 BrushPrefab g_brushprism( eBrushPrism );
1208 BrushPrefab g_brushcone( eBrushCone );
1209 BrushPrefab g_brushsphere( eBrushSphere );
1210 BrushPrefab g_brushrock( eBrushRock );
1211
1212
1213 void FlipClip();
1214
1215 void SplitClip();
1216
1217 void Clip();
1218
1219 void OnClipMode( bool enable );
1220
1221 bool ClipMode();
1222
1223
1224 void ClipSelected(){
1225         if ( ClipMode() ) {
1226                 UndoableCommand undo( "clipperClip" );
1227                 Clip();
1228         }
1229 }
1230
1231 void SplitSelected(){
1232         if ( ClipMode() ) {
1233                 UndoableCommand undo( "clipperSplit" );
1234                 SplitClip();
1235         }
1236 }
1237
1238 void FlipClipper(){
1239         FlipClip();
1240 }
1241
1242
1243 Callback<void()> g_texture_lock_status_changed;
1244 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_texdef_movelock_caller( g_brush_texturelock_enabled );
1245 ToggleItem g_texdef_movelock_item( g_texdef_movelock_caller );
1246
1247 void Texdef_ToggleMoveLock(){
1248         g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
1249         g_texdef_movelock_item.update();
1250         g_texture_lock_status_changed();
1251 }
1252
1253
1254 void Brush_registerCommands(){
1255         GlobalToggles_insert( "TogTexLock", makeCallbackF(Texdef_ToggleMoveLock), ToggleItem::AddCallbackCaller( g_texdef_movelock_item ), Accelerator( 'T', (GdkModifierType)GDK_SHIFT_MASK ) );
1256
1257         GlobalCommands_insert( "BrushPrism", BrushPrefab::SetCaller( g_brushprism ) );
1258         GlobalCommands_insert( "BrushCone", BrushPrefab::SetCaller( g_brushcone ) );
1259         GlobalCommands_insert( "BrushSphere", BrushPrefab::SetCaller( g_brushsphere ) );
1260         GlobalCommands_insert( "BrushRock", BrushPrefab::SetCaller( g_brushrock ) );
1261
1262         GlobalCommands_insert( "Brush3Sided", BrushMakeSided::SetCaller( g_brushmakesided3 ), Accelerator( '3', (GdkModifierType)GDK_CONTROL_MASK ) );
1263         GlobalCommands_insert( "Brush4Sided", BrushMakeSided::SetCaller( g_brushmakesided4 ), Accelerator( '4', (GdkModifierType)GDK_CONTROL_MASK ) );
1264         GlobalCommands_insert( "Brush5Sided", BrushMakeSided::SetCaller( g_brushmakesided5 ), Accelerator( '5', (GdkModifierType)GDK_CONTROL_MASK ) );
1265         GlobalCommands_insert( "Brush6Sided", BrushMakeSided::SetCaller( g_brushmakesided6 ), Accelerator( '6', (GdkModifierType)GDK_CONTROL_MASK ) );
1266         GlobalCommands_insert( "Brush7Sided", BrushMakeSided::SetCaller( g_brushmakesided7 ), Accelerator( '7', (GdkModifierType)GDK_CONTROL_MASK ) );
1267         GlobalCommands_insert( "Brush8Sided", BrushMakeSided::SetCaller( g_brushmakesided8 ), Accelerator( '8', (GdkModifierType)GDK_CONTROL_MASK ) );
1268         GlobalCommands_insert( "Brush9Sided", BrushMakeSided::SetCaller( g_brushmakesided9 ), Accelerator( '9', (GdkModifierType)GDK_CONTROL_MASK ) );
1269
1270         GlobalCommands_insert( "ClipSelected", makeCallbackF(ClipSelected), Accelerator( GDK_KEY_Return ) );
1271         GlobalCommands_insert( "SplitSelected", makeCallbackF(SplitSelected), Accelerator( GDK_KEY_Return, (GdkModifierType)GDK_SHIFT_MASK ) );
1272         GlobalCommands_insert( "FlipClip", makeCallbackF(FlipClipper), Accelerator( GDK_KEY_Return, (GdkModifierType)GDK_CONTROL_MASK ) );
1273
1274         GlobalCommands_insert( "MakeDetail", makeCallbackF(Select_MakeDetail), Accelerator( 'M', (GdkModifierType)GDK_CONTROL_MASK ) );
1275         GlobalCommands_insert( "MakeStructural", makeCallbackF(Select_MakeStructural), Accelerator( 'S', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
1276 }
1277
1278 void Brush_constructMenu( ui::Menu menu ){
1279         create_menu_item_with_mnemonic( menu, "Prism...", "BrushPrism" );
1280         create_menu_item_with_mnemonic( menu, "Cone...", "BrushCone" );
1281         create_menu_item_with_mnemonic( menu, "Sphere...", "BrushSphere" );
1282         create_menu_item_with_mnemonic( menu, "Rock...", "BrushRock" );
1283         menu_separator( menu );
1284         {
1285                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "CSG" );
1286                 if ( g_Layout_enableDetachableMenus.m_value ) {
1287                         menu_tearoff( menu_in_menu );
1288                 }
1289                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Subtract", "CSGSubtract" );
1290                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Merge", "CSGMerge" );
1291                 create_menu_item_with_mnemonic( menu_in_menu, "Make _Room", "CSGRoom" );
1292                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Tool", "CSGTool" );
1293         }
1294         menu_separator( menu );
1295         {
1296                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Clipper" );
1297                 if ( g_Layout_enableDetachableMenus.m_value ) {
1298                         menu_tearoff( menu_in_menu );
1299                 }
1300
1301                 create_menu_item_with_mnemonic( menu_in_menu, "Clip selection", "ClipSelected" );
1302                 create_menu_item_with_mnemonic( menu_in_menu, "Split selection", "SplitSelected" );
1303                 create_menu_item_with_mnemonic( menu_in_menu, "Flip Clip orientation", "FlipClip" );
1304         }
1305         menu_separator( menu );
1306         create_menu_item_with_mnemonic( menu, "Make detail", "MakeDetail" );
1307         create_menu_item_with_mnemonic( menu, "Make structural", "MakeStructural" );
1308 //      create_menu_item_with_mnemonic( menu, "Snap selection to _grid", "SnapToGrid" );
1309
1310         create_check_menu_item_with_mnemonic( menu, "Texture Lock", "TogTexLock" );
1311         menu_separator( menu );
1312         create_menu_item_with_mnemonic( menu, "Copy Face Texture", "FaceCopyTexture" );
1313         create_menu_item_with_mnemonic( menu, "Paste Face Texture", "FacePasteTexture" );
1314
1315         command_connect_accelerator( "Brush3Sided" );
1316         command_connect_accelerator( "Brush4Sided" );
1317         command_connect_accelerator( "Brush5Sided" );
1318         command_connect_accelerator( "Brush6Sided" );
1319         command_connect_accelerator( "Brush7Sided" );
1320         command_connect_accelerator( "Brush8Sided" );
1321         command_connect_accelerator( "Brush9Sided" );
1322 }