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