]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/select.cpp
Merge commit '0261afc6df181092b7d57751fec84e21d3ac593c' into garux-merge
[xonotic/netradiant.git] / radiant / select.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 "select.h"
23
24 #include <gtk/gtk.h>
25
26 #include "debugging/debugging.h"
27
28 #include "ientity.h"
29 #include "iselection.h"
30 #include "iundo.h"
31
32 #include <vector>
33
34 #include "stream/stringstream.h"
35 #include "signal/isignal.h"
36 #include "shaderlib.h"
37 #include "scenelib.h"
38
39 #include "gtkutil/idledraw.h"
40 #include "gtkutil/dialog.h"
41 #include "gtkutil/widget.h"
42 #include "brushmanip.h"
43 #include "brush.h"
44 #include "patch.h"
45 #include "patchmanip.h"
46 #include "patchdialog.h"
47 #include "selection.h"
48 #include "texwindow.h"
49 #include "gtkmisc.h"
50 #include "mainframe.h"
51 #include "grid.h"
52 #include "map.h"
53 #include "entityinspector.h"
54
55
56
57 select_workzone_t g_select_workzone;
58
59
60 /**
61    Loops over all selected brushes and stores their
62    world AABBs in the specified array.
63  */
64 class CollectSelectedBrushesBounds : public SelectionSystem::Visitor
65 {
66 AABB* m_bounds;     // array of AABBs
67 Unsigned m_max;     // max AABB-elements in array
68 Unsigned& m_count;  // count of valid AABBs stored in array
69
70 public:
71 CollectSelectedBrushesBounds( AABB* bounds, Unsigned max, Unsigned& count )
72         : m_bounds( bounds ),
73         m_max( max ),
74         m_count( count ){
75         m_count = 0;
76 }
77
78 void visit( scene::Instance& instance ) const {
79         ASSERT_MESSAGE( m_count <= m_max, "Invalid m_count in CollectSelectedBrushesBounds" );
80
81         // stop if the array is already full
82         if ( m_count == m_max ) {
83                 return;
84         }
85
86         Selectable* selectable = Instance_getSelectable( instance );
87         if ( ( selectable != 0 )
88                  && instance.isSelected() ) {
89                 // brushes only
90                 if ( Instance_getBrush( instance ) != 0 ) {
91                         m_bounds[m_count] = instance.worldAABB();
92                         ++m_count;
93                 }
94         }
95 }
96 };
97
98 /**
99    Selects all objects that intersect one of the bounding AABBs.
100    The exact intersection-method is specified through TSelectionPolicy
101  */
102 template<class TSelectionPolicy>
103 class SelectByBounds : public scene::Graph::Walker
104 {
105 AABB* m_aabbs;             // selection aabbs
106 Unsigned m_count;          // number of aabbs in m_aabbs
107 TSelectionPolicy policy;   // type that contains a custom intersection method aabb<->aabb
108
109 public:
110 SelectByBounds( AABB* aabbs, Unsigned count )
111         : m_aabbs( aabbs ),
112         m_count( count ){
113 }
114
115 bool pre( const scene::Path& path, scene::Instance& instance ) const {
116         if( path.top().get().visible() ){
117                 Selectable* selectable = Instance_getSelectable( instance );
118
119                 // ignore worldspawn
120                 Entity* entity = Node_getEntity( path.top() );
121                 if ( entity ) {
122                         if ( string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ) {
123                                 return true;
124                         }
125                 }
126
127                 if ( ( path.size() > 1 ) &&
128                         ( !path.top().get().isRoot() ) &&
129                         ( selectable != 0 ) &&
130                         ( !node_is_group( path.top() ) )
131                         ) {
132                         for ( Unsigned i = 0; i < m_count; ++i )
133                         {
134                                 if ( policy.Evaluate( m_aabbs[i], instance ) ) {
135                                         selectable->setSelected( true );
136                                 }
137                         }
138                 }
139         }
140         else{
141                 return false;
142         }
143
144         return true;
145 }
146
147 /**
148    Performs selection operation on the global scenegraph.
149    If delete_bounds_src is true, then the objects which were
150    used as source for the selection aabbs will be deleted.
151  */
152 static void DoSelection( bool delete_bounds_src = true ){
153         if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
154                 // we may not need all AABBs since not all selected objects have to be brushes
155                 const Unsigned max = (Unsigned)GlobalSelectionSystem().countSelected();
156                 AABB* aabbs = new AABB[max];
157
158                 Unsigned count;
159                 CollectSelectedBrushesBounds collector( aabbs, max, count );
160                 GlobalSelectionSystem().foreachSelected( collector );
161
162                 // nothing usable in selection
163                 if ( !count ) {
164                         delete[] aabbs;
165                         return;
166                 }
167
168                 // delete selected objects
169                 if ( delete_bounds_src ) { // see deleteSelection
170                         UndoableCommand undo( "deleteSelected" );
171                         Select_Delete();
172                 }
173
174                 // select objects with bounds
175                 GlobalSceneGraph().traverse( SelectByBounds<TSelectionPolicy>( aabbs, count ) );
176
177                 SceneChangeNotify();
178                 delete[] aabbs;
179         }
180 }
181 };
182
183 /**
184    SelectionPolicy for SelectByBounds
185    Returns true if box and the AABB of instance intersect
186  */
187 class SelectionPolicy_Touching
188 {
189 public:
190 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
191         const AABB& other( instance.worldAABB() );
192         for ( Unsigned i = 0; i < 3; ++i )
193         {
194                 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] + other.extents[i] ) ) {
195                         return false;
196                 }
197         }
198         return true;
199 }
200 };
201
202 /**
203    SelectionPolicy for SelectByBounds
204    Returns true if the AABB of instance is inside box
205  */
206 class SelectionPolicy_Inside
207 {
208 public:
209 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
210         const AABB& other( instance.worldAABB() );
211         for ( Unsigned i = 0; i < 3; ++i )
212         {
213                 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] - other.extents[i] ) ) {
214                         return false;
215                 }
216         }
217         return true;
218 }
219 };
220
221 class DeleteSelected : public scene::Graph::Walker
222 {
223 mutable bool m_remove;
224 mutable bool m_removedChild;
225 public:
226 DeleteSelected()
227         : m_remove( false ), m_removedChild( false ){
228 }
229 bool pre( const scene::Path& path, scene::Instance& instance ) const {
230         m_removedChild = false;
231
232         Selectable* selectable = Instance_getSelectable( instance );
233         if ( selectable != 0
234                  && selectable->isSelected()
235                  && path.size() > 1
236                  && !path.top().get().isRoot() ) {
237                 m_remove = true;
238
239                 return false; // dont traverse into child elements
240         }
241         return true;
242 }
243 void post( const scene::Path& path, scene::Instance& instance ) const {
244
245         if ( m_removedChild ) {
246                 m_removedChild = false;
247
248                 // delete empty entities
249                 Entity* entity = Node_getEntity( path.top() );
250                 if ( entity != 0
251                          && path.top().get_pointer() != Map_FindWorldspawn( g_map )
252                          && Node_getTraversable( path.top() )->empty() ) {
253                         Path_deleteTop( path );
254                 }
255         }
256
257         // node should be removed
258         if ( m_remove ) {
259                 if ( Node_isEntity( path.parent() ) != 0 ) {
260                         m_removedChild = true;
261                 }
262
263                 m_remove = false;
264                 Path_deleteTop( path );
265         }
266 }
267 };
268
269 void Scene_DeleteSelected( scene::Graph& graph ){
270         graph.traverse( DeleteSelected() );
271         SceneChangeNotify();
272 }
273
274 void Select_Delete( void ){
275         Scene_DeleteSelected( GlobalSceneGraph() );
276 }
277
278 class InvertSelectionWalker : public scene::Graph::Walker
279 {
280 SelectionSystem::EMode m_mode;
281 SelectionSystem::EComponentMode m_compmode;
282 mutable Selectable* m_selectable;
283 public:
284 InvertSelectionWalker( SelectionSystem::EMode mode, SelectionSystem::EComponentMode compmode )
285         : m_mode( mode ), m_compmode( compmode ), m_selectable( 0 ){
286 }
287 bool pre( const scene::Path& path, scene::Instance& instance ) const {
288         if( !path.top().get().visible() ){
289                 m_selectable = 0;
290                 return false;
291         }
292         Selectable* selectable = Instance_getSelectable( instance );
293         if ( selectable ) {
294                 switch ( m_mode )
295                 {
296                 case SelectionSystem::eEntity:
297                         if ( Node_isEntity( path.top() ) != 0 ) {
298                                 m_selectable = path.top().get().visible() ? selectable : 0;
299                         }
300                         break;
301                 case SelectionSystem::ePrimitive:
302                         m_selectable = path.top().get().visible() ? selectable : 0;
303                         break;
304                 case SelectionSystem::eComponent:
305                         BrushInstance* brushinstance = Instance_getBrush( instance );
306                         if( brushinstance != 0 ){
307                                 if( brushinstance->isSelected() )
308                                         brushinstance->invertComponentSelection( m_compmode );
309                         }
310                         else{
311                                 PatchInstance* patchinstance = Instance_getPatch( instance );
312                                 if( patchinstance != 0 && m_compmode == SelectionSystem::eVertex ){
313                                         if( patchinstance->isSelected() )
314                                                 patchinstance->invertComponentSelection();
315                                 }
316                         }
317                         break;
318                 }
319         }
320         return true;
321 }
322 void post( const scene::Path& path, scene::Instance& instance ) const {
323         if ( m_selectable != 0 ) {
324                 m_selectable->setSelected( !m_selectable->isSelected() );
325                 m_selectable = 0;
326         }
327 }
328 };
329
330 void Scene_Invert_Selection( scene::Graph& graph ){
331         graph.traverse( InvertSelectionWalker( GlobalSelectionSystem().Mode(), GlobalSelectionSystem().ComponentMode() ) );
332 }
333
334 void Select_Invert(){
335         Scene_Invert_Selection( GlobalSceneGraph() );
336 }
337
338 //interesting printings
339 class ExpandSelectionToEntitiesWalker_dbg : public scene::Graph::Walker
340 {
341 mutable std::size_t m_depth;
342 NodeSmartReference worldspawn;
343 public:
344 ExpandSelectionToEntitiesWalker_dbg() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
345 }
346 bool pre( const scene::Path& path, scene::Instance& instance ) const {
347         ++m_depth;
348         globalOutputStream() << "pre depth_" << m_depth;
349         globalOutputStream() << " path.size()_" << path.size();
350         if ( path.top().get() == worldspawn )
351                 globalOutputStream() << " worldspawn";
352         if( path.top().get().isRoot() )
353                 globalOutputStream() << " path.top().get().isRoot()";
354         Entity* entity = Node_getEntity( path.top() );
355         if ( entity != 0 ){
356                 globalOutputStream() << " entity!=0";
357                 if( entity->isContainer() ){
358                         globalOutputStream() << " entity->isContainer()";
359                 }
360                 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
361         }
362         globalOutputStream() << "\n";
363 //      globalOutputStream() << "" <<  ;
364 //      globalOutputStream() << "" <<  ;
365 //      globalOutputStream() << "" <<  ;
366 //      globalOutputStream() << "" <<  ;
367         return true;
368 }
369 void post( const scene::Path& path, scene::Instance& instance ) const {
370         globalOutputStream() << "post depth_" << m_depth;
371         globalOutputStream() << " path.size()_" << path.size();
372         if ( path.top().get() == worldspawn )
373                 globalOutputStream() << " worldspawn";
374         if( path.top().get().isRoot() )
375                 globalOutputStream() << " path.top().get().isRoot()";
376         Entity* entity = Node_getEntity( path.top() );
377         if ( entity != 0 ){
378                 globalOutputStream() << " entity!=0";
379                 if( entity->isContainer() ){
380                         globalOutputStream() << " entity->isContainer()";
381                 }
382                 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
383         }
384         globalOutputStream() << "\n";
385         --m_depth;
386 }
387 };
388
389 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
390 {
391 mutable std::size_t m_depth;
392 NodeSmartReference worldspawn;
393 public:
394 ExpandSelectionToEntitiesWalker() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
395 }
396 bool pre( const scene::Path& path, scene::Instance& instance ) const {
397         ++m_depth;
398
399         // ignore worldspawn
400 //      NodeSmartReference me( path.top().get() );
401 //      if ( me == worldspawn ) {
402 //              return false;
403 //      }
404
405         if ( m_depth == 2 ) { // entity depth
406                 // traverse and select children if any one is selected
407                 bool beselected = false;
408                 if ( instance.childSelected() ) {
409                         beselected = true;
410                         if( path.top().get() != worldspawn ){
411                                 Instance_setSelected( instance, true );
412                         }
413                 }
414                 return Node_getEntity( path.top() )->isContainer() && beselected;
415         }
416         else if ( m_depth == 3 ) { // primitive depth
417                 Instance_setSelected( instance, true );
418                 return false;
419         }
420         return true;
421 }
422 void post( const scene::Path& path, scene::Instance& instance ) const {
423         --m_depth;
424 }
425 };
426
427 void Scene_ExpandSelectionToEntities(){
428         GlobalSceneGraph().traverse( ExpandSelectionToEntitiesWalker() );
429 }
430
431
432 namespace
433 {
434 void Selection_UpdateWorkzone(){
435         if ( GlobalSelectionSystem().countSelected() != 0 ) {
436                 Select_GetBounds( g_select_workzone.d_work_min, g_select_workzone.d_work_max );
437         }
438 }
439 typedef FreeCaller<void(), Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
440
441 IdleDraw g_idleWorkzone = IdleDraw( SelectionUpdateWorkzoneCaller() );
442 }
443
444 const select_workzone_t& Select_getWorkZone(){
445         g_idleWorkzone.flush();
446         return g_select_workzone;
447 }
448
449 void UpdateWorkzone_ForSelection(){
450         g_idleWorkzone.queueDraw();
451 }
452
453 // update the workzone to the current selection
454 void UpdateWorkzone_ForSelectionChanged( const Selectable& selectable ){
455         if ( selectable.isSelected() ) {
456                 UpdateWorkzone_ForSelection();
457         }
458 }
459
460 void Select_SetShader( const char* shader ){
461         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
462                 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
463                 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
464         }
465         Scene_BrushSetShader_Component_Selected( GlobalSceneGraph(), shader );
466 }
467
468 void Select_SetTexdef( const TextureProjection& projection ){
469         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
470                 Scene_BrushSetTexdef_Selected( GlobalSceneGraph(), projection );
471         }
472         Scene_BrushSetTexdef_Component_Selected( GlobalSceneGraph(), projection );
473 }
474
475 void Select_SetFlags( const ContentsFlagsValue& flags ){
476         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
477                 Scene_BrushSetFlags_Selected( GlobalSceneGraph(), flags );
478         }
479         Scene_BrushSetFlags_Component_Selected( GlobalSceneGraph(), flags );
480 }
481
482 void Select_GetBounds( Vector3& mins, Vector3& maxs ){
483         AABB bounds;
484         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
485         maxs = vector3_added( bounds.origin, bounds.extents );
486         mins = vector3_subtracted( bounds.origin, bounds.extents );
487 }
488
489 void Select_GetMid( Vector3& mid ){
490         AABB bounds;
491         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
492         mid = vector3_snapped( bounds.origin );
493 }
494
495
496 void Select_FlipAxis( int axis ){
497         Vector3 flip( 1, 1, 1 );
498         flip[axis] = -1;
499         GlobalSelectionSystem().scaleSelected( flip );
500 }
501
502
503 void Select_Scale( float x, float y, float z ){
504         GlobalSelectionSystem().scaleSelected( Vector3( x, y, z ) );
505 }
506
507 enum axis_t
508 {
509         eAxisX = 0,
510         eAxisY = 1,
511         eAxisZ = 2,
512 };
513
514 enum sign_t
515 {
516         eSignPositive = 1,
517         eSignNegative = -1,
518 };
519
520 inline Matrix4 matrix4_rotation_for_axis90( axis_t axis, sign_t sign ){
521         switch ( axis )
522         {
523         case eAxisX:
524                 if ( sign == eSignPositive ) {
525                         return matrix4_rotation_for_sincos_x( 1, 0 );
526                 }
527                 else
528                 {
529                         return matrix4_rotation_for_sincos_x( -1, 0 );
530                 }
531         case eAxisY:
532                 if ( sign == eSignPositive ) {
533                         return matrix4_rotation_for_sincos_y( 1, 0 );
534                 }
535                 else
536                 {
537                         return matrix4_rotation_for_sincos_y( -1, 0 );
538                 }
539         default: //case eAxisZ:
540                 if ( sign == eSignPositive ) {
541                         return matrix4_rotation_for_sincos_z( 1, 0 );
542                 }
543                 else
544                 {
545                         return matrix4_rotation_for_sincos_z( -1, 0 );
546                 }
547         }
548 }
549
550 inline void matrix4_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign ){
551         matrix4_multiply_by_matrix4( matrix, matrix4_rotation_for_axis90( axis, sign ) );
552 }
553
554 inline void matrix4_pivoted_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign, const Vector3& pivotpoint ){
555         matrix4_translate_by_vec3( matrix, pivotpoint );
556         matrix4_rotate_by_axis90( matrix, axis, sign );
557         matrix4_translate_by_vec3( matrix, vector3_negated( pivotpoint ) );
558 }
559
560 inline Quaternion quaternion_for_axis90( axis_t axis, sign_t sign ){
561 #if 1
562         switch ( axis )
563         {
564         case eAxisX:
565                 if ( sign == eSignPositive ) {
566                         return Quaternion( c_half_sqrt2f, 0, 0, c_half_sqrt2f );
567                 }
568                 else
569                 {
570                         return Quaternion( -c_half_sqrt2f, 0, 0, -c_half_sqrt2f );
571                 }
572         case eAxisY:
573                 if ( sign == eSignPositive ) {
574                         return Quaternion( 0, c_half_sqrt2f, 0, c_half_sqrt2f );
575                 }
576                 else
577                 {
578                         return Quaternion( 0, -c_half_sqrt2f, 0, -c_half_sqrt2f );
579                 }
580         default: //case eAxisZ:
581                 if ( sign == eSignPositive ) {
582                         return Quaternion( 0, 0, c_half_sqrt2f, c_half_sqrt2f );
583                 }
584                 else
585                 {
586                         return Quaternion( 0, 0, -c_half_sqrt2f, -c_half_sqrt2f );
587                 }
588         }
589 #else
590         quaternion_for_matrix4_rotation( matrix4_rotation_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
591 #endif
592 }
593
594 void Select_RotateAxis( int axis, float deg ){
595         if ( fabs( deg ) == 90.f ) {
596                 GlobalSelectionSystem().rotateSelected( quaternion_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ), true );
597         }
598         else
599         {
600                 switch ( axis )
601                 {
602                 case 0:
603                         GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_x_degrees( deg ) ), false );
604                         break;
605                 case 1:
606                         GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_y_degrees( deg ) ), false );
607                         break;
608                 case 2:
609                         GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_z_degrees( deg ) ), false );
610                         break;
611                 }
612         }
613 }
614
615
616 void Select_ShiftTexture( float x, float y ){
617         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
618                 Scene_BrushShiftTexdef_Selected( GlobalSceneGraph(), x, y );
619                 Scene_PatchTranslateTexture_Selected( GlobalSceneGraph(), x, y );
620         }
621         //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
622         Scene_BrushShiftTexdef_Component_Selected( GlobalSceneGraph(), x, y );
623 }
624
625 void Select_ScaleTexture( float x, float y ){
626         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
627                 Scene_BrushScaleTexdef_Selected( GlobalSceneGraph(), x, y );
628                 Scene_PatchScaleTexture_Selected( GlobalSceneGraph(), x, y );
629         }
630         Scene_BrushScaleTexdef_Component_Selected( GlobalSceneGraph(), x, y );
631 }
632
633 void Select_RotateTexture( float amt ){
634         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
635                 Scene_BrushRotateTexdef_Selected( GlobalSceneGraph(), amt );
636                 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), amt );
637         }
638         Scene_BrushRotateTexdef_Component_Selected( GlobalSceneGraph(), amt );
639 }
640
641 // TTimo modified to handle shader architecture:
642 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
643 void FindReplaceTextures( const char* pFind, const char* pReplace, bool bSelected ){
644         if ( !texdef_name_valid( pFind ) ) {
645                 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pFind << "', aborted\n";
646                 return;
647         }
648         if ( !texdef_name_valid( pReplace ) ) {
649                 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
650                 return;
651         }
652
653         StringOutputStream command;
654         command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
655         UndoableCommand undo( command.c_str() );
656
657         if ( bSelected ) {
658                 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
659                         Scene_BrushFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
660                         Scene_PatchFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
661                 }
662                 Scene_BrushFindReplaceShader_Component_Selected( GlobalSceneGraph(), pFind, pReplace );
663         }
664         else
665         {
666                 Scene_BrushFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
667                 Scene_PatchFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
668         }
669 }
670
671 typedef std::vector<const char*> PropertyValues;
672
673 bool propertyvalues_contain( const PropertyValues& propertyvalues, const char *str ){
674         for ( PropertyValues::const_iterator i = propertyvalues.begin(); i != propertyvalues.end(); ++i )
675         {
676                 if ( string_equal( str, *i ) ) {
677                         return true;
678                 }
679         }
680         return false;
681 }
682
683 class EntityFindByPropertyValueWalker : public scene::Graph::Walker
684 {
685 const PropertyValues& m_propertyvalues;
686 const char *m_prop;
687 const NodeSmartReference worldspawn;
688 public:
689 EntityFindByPropertyValueWalker( const char *prop, const PropertyValues& propertyvalues )
690         : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
691 }
692 bool pre( const scene::Path& path, scene::Instance& instance ) const {
693         if( !path.top().get().visible() ){
694                 return false;
695         }
696         // ignore worldspawn
697         if ( path.top().get() == worldspawn ) {
698                 return false;
699         }
700
701         Entity* entity = Node_getEntity( path.top() );
702         if ( entity != 0 ){
703                 if( propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
704                         Instance_getSelectable( instance )->setSelected( true );
705                         return true;
706                 }
707                 return false;
708         }
709         else if( path.size() > 2 && !path.top().get().isRoot() ){
710                 Selectable* selectable = Instance_getSelectable( instance );
711                 if( selectable != 0 )
712                         selectable->setSelected( true );
713         }
714         return true;
715 }
716 };
717
718 void Scene_EntitySelectByPropertyValues( scene::Graph& graph, const char *prop, const PropertyValues& propertyvalues ){
719         graph.traverse( EntityFindByPropertyValueWalker( prop, propertyvalues ) );
720 }
721
722 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
723 {
724 PropertyValues& m_propertyvalues;
725 const char *m_prop;
726 const NodeSmartReference worldspawn;
727 public:
728 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
729         : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
730 }
731 bool pre( const scene::Path& path, scene::Instance& instance ) const {
732         Entity* entity = Node_getEntity( path.top() );
733         if ( entity != 0 ){
734                 if( path.top().get() != worldspawn ){
735                         Selectable* selectable = Instance_getSelectable( instance );
736                         if ( ( selectable != 0 && selectable->isSelected() ) || instance.childSelected() ) {
737                                 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
738                                         m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
739                                 }
740                         }
741                 }
742                 return false;
743         }
744         return true;
745 }
746 };
747 /*
748 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
749 {
750 PropertyValues& m_propertyvalues;
751 const char *m_prop;
752 mutable bool m_selected_children;
753 const NodeSmartReference worldspawn;
754 public:
755 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
756         : m_propertyvalues( propertyvalues ), m_prop( prop ), m_selected_children( false ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
757 }
758 bool pre( const scene::Path& path, scene::Instance& instance ) const {
759         Selectable* selectable = Instance_getSelectable( instance );
760         if ( selectable != 0
761                  && selectable->isSelected() ) {
762                 Entity* entity = Node_getEntity( path.top() );
763                 if ( entity != 0 ) {
764                         if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
765                                 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
766                         }
767                         return false;
768                 }
769                 else{
770                         m_selected_children = true;
771                 }
772         }
773         return true;
774 }
775 void post( const scene::Path& path, scene::Instance& instance ) const {
776         Entity* entity = Node_getEntity( path.top() );
777         if( entity != 0 && m_selected_children ){
778                 m_selected_children = false;
779                 if( path.top().get() == worldspawn )
780                         return;
781                 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
782                         m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
783                 }
784         }
785 }
786 };
787 */
788 void Scene_EntityGetPropertyValues( scene::Graph& graph, const char *prop, PropertyValues& propertyvalues ){
789         graph.traverse( EntityGetSelectedPropertyValuesWalker( prop, propertyvalues ) );
790 }
791
792 void Select_AllOfType(){
793         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
794                 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
795                         GlobalSelectionSystem().setSelectedAllComponents( false );
796                         Scene_BrushSelectByShader_Component( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
797                 }
798         }
799         else
800         {
801                 PropertyValues propertyvalues;
802                 const char *prop = EntityInspector_getCurrentKey();
803                 if ( !prop || !*prop ) {
804                         prop = "classname";
805                 }
806                 Scene_EntityGetPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
807                 GlobalSelectionSystem().setSelectedAll( false );
808                 if ( !propertyvalues.empty() ) {
809                         Scene_EntitySelectByPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
810                 }
811                 else
812                 {
813                         Scene_BrushSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
814                         Scene_PatchSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
815                 }
816         }
817 }
818
819 void Select_Inside( void ){
820         SelectByBounds<SelectionPolicy_Inside>::DoSelection();
821 }
822
823 void Select_Touching( void ){
824         SelectByBounds<SelectionPolicy_Touching>::DoSelection( false );
825 }
826
827 void Select_FitTexture( float horizontal, float vertical ){
828         if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
829                 Scene_BrushFitTexture_Selected( GlobalSceneGraph(), horizontal, vertical );
830         }
831         Scene_BrushFitTexture_Component_Selected( GlobalSceneGraph(), horizontal, vertical );
832
833         SceneChangeNotify();
834 }
835
836
837 #include "commands.h"
838 #include "dialog.h"
839
840 inline void hide_node( scene::Node& node, bool hide ){
841         hide
842         ? node.enable( scene::Node::eHidden )
843         : node.disable( scene::Node::eHidden );
844 }
845
846 bool g_nodes_be_hidden = false;
847
848 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_hidden_caller( g_nodes_be_hidden );
849
850 ToggleItem g_hidden_item( g_hidden_caller );
851
852 class HideSelectedWalker : public scene::Graph::Walker
853 {
854 bool m_hide;
855 public:
856 HideSelectedWalker( bool hide )
857         : m_hide( hide ){
858 }
859 bool pre( const scene::Path& path, scene::Instance& instance ) const {
860         Selectable* selectable = Instance_getSelectable( instance );
861         if ( selectable != 0
862                  && selectable->isSelected() ) {
863                 g_nodes_be_hidden = m_hide;
864                 hide_node( path.top(), m_hide );
865         }
866         return true;
867 }
868 };
869
870 void Scene_Hide_Selected( bool hide ){
871         GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
872 }
873
874 void Select_Hide(){
875         Scene_Hide_Selected( true );
876         SceneChangeNotify();
877 }
878
879 void HideSelected(){
880         Select_Hide();
881         GlobalSelectionSystem().setSelectedAll( false );
882         g_hidden_item.update();
883 }
884
885
886 class HideAllWalker : public scene::Graph::Walker
887 {
888 bool m_hide;
889 public:
890 HideAllWalker( bool hide )
891         : m_hide( hide ){
892 }
893 bool pre( const scene::Path& path, scene::Instance& instance ) const {
894         hide_node( path.top(), m_hide );
895         return true;
896 }
897 };
898
899 void Scene_Hide_All( bool hide ){
900         GlobalSceneGraph().traverse( HideAllWalker( hide ) );
901 }
902
903 void Select_ShowAllHidden(){
904         Scene_Hide_All( false );
905         SceneChangeNotify();
906         g_nodes_be_hidden = false;
907         g_hidden_item.update();
908 }
909
910
911 void Selection_Flipx(){
912         UndoableCommand undo( "mirrorSelected -axis x" );
913         Select_FlipAxis( 0 );
914 }
915
916 void Selection_Flipy(){
917         UndoableCommand undo( "mirrorSelected -axis y" );
918         Select_FlipAxis( 1 );
919 }
920
921 void Selection_Flipz(){
922         UndoableCommand undo( "mirrorSelected -axis z" );
923         Select_FlipAxis( 2 );
924 }
925
926 void Selection_Rotatex(){
927         UndoableCommand undo( "rotateSelected -axis x -angle -90" );
928         Select_RotateAxis( 0,-90 );
929 }
930
931 void Selection_Rotatey(){
932         UndoableCommand undo( "rotateSelected -axis y -angle 90" );
933         Select_RotateAxis( 1, 90 );
934 }
935
936 void Selection_Rotatez(){
937         UndoableCommand undo( "rotateSelected -axis z -angle -90" );
938         Select_RotateAxis( 2,-90 );
939 }
940 #include "xywindow.h"
941 void Selection_FlipHorizontally(){
942         VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
943         switch ( viewtype )
944         {
945         case XY:
946         case XZ:
947                 Selection_Flipx();
948                 break;
949         default:
950                 Selection_Flipy();
951                 break;
952         }
953 }
954
955 void Selection_FlipVertically(){
956         VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
957         switch ( viewtype )
958         {
959         case XZ:
960         case YZ:
961                 Selection_Flipz();
962                 break;
963         default:
964                 Selection_Flipy();
965                 break;
966         }
967 }
968
969 void Selection_RotateClockwise(){
970         UndoableCommand undo( "rotateSelected Clockwise 90" );
971         VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
972         switch ( viewtype )
973         {
974         case XY:
975                 Select_RotateAxis( 2, -90 );
976                 break;
977         case XZ:
978                 Select_RotateAxis( 1, 90 );
979                 break;
980         default:
981                 Select_RotateAxis( 0, -90 );
982                 break;
983         }
984 }
985
986 void Selection_RotateAnticlockwise(){
987         UndoableCommand undo( "rotateSelected Anticlockwise 90" );
988         VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
989         switch ( viewtype )
990         {
991         case XY:
992                 Select_RotateAxis( 2, 90 );
993                 break;
994         case XZ:
995                 Select_RotateAxis( 1, -90 );
996                 break;
997         default:
998                 Select_RotateAxis( 0, 90 );
999                 break;
1000         }
1001
1002 }
1003
1004
1005
1006 void Select_registerCommands(){
1007         GlobalCommands_insert( "ShowHidden", makeCallbackF( Select_ShowAllHidden ), Accelerator( 'H', (GdkModifierType)GDK_SHIFT_MASK ) );
1008         GlobalToggles_insert( "HideSelected", makeCallbackF( HideSelected ), ToggleItem::AddCallbackCaller( g_hidden_item ), Accelerator( 'H' ) );
1009
1010         GlobalCommands_insert( "MirrorSelectionX", makeCallbackF( Selection_Flipx ) );
1011         GlobalCommands_insert( "RotateSelectionX", makeCallbackF( Selection_Rotatex ) );
1012         GlobalCommands_insert( "MirrorSelectionY", makeCallbackF( Selection_Flipy ) );
1013         GlobalCommands_insert( "RotateSelectionY", makeCallbackF( Selection_Rotatey ) );
1014         GlobalCommands_insert( "MirrorSelectionZ", makeCallbackF( Selection_Flipz ) );
1015         GlobalCommands_insert( "RotateSelectionZ", makeCallbackF( Selection_Rotatez ) );
1016
1017         GlobalCommands_insert( "MirrorSelectionHorizontally", makeCallbackF( Selection_FlipHorizontally ) );
1018         GlobalCommands_insert( "MirrorSelectionVertically", makeCallbackF( Selection_FlipVertically ) );
1019
1020         GlobalCommands_insert( "RotateSelectionClockwise", makeCallbackF( Selection_RotateClockwise ) );
1021         GlobalCommands_insert( "RotateSelectionAnticlockwise", makeCallbackF( Selection_RotateAnticlockwise ) );
1022 }
1023
1024
1025 void Nudge( int nDim, float fNudge ){
1026         Vector3 translate( 0, 0, 0 );
1027         translate[nDim] = fNudge;
1028
1029         GlobalSelectionSystem().translateSelected( translate );
1030 }
1031
1032 void Selection_NudgeZ( float amount ){
1033         StringOutputStream command;
1034         command << "nudgeSelected -axis z -amount " << amount;
1035         UndoableCommand undo( command.c_str() );
1036
1037         Nudge( 2, amount );
1038 }
1039
1040 void Selection_MoveDown(){
1041         Selection_NudgeZ( -GetGridSize() );
1042 }
1043
1044 void Selection_MoveUp(){
1045         Selection_NudgeZ( GetGridSize() );
1046 }
1047
1048 void SceneSelectionChange( const Selectable& selectable ){
1049         SceneChangeNotify();
1050 }
1051
1052 SignalHandlerId Selection_boundsChanged;
1053
1054 void Selection_construct(){
1055         typedef FreeCaller<void(const Selectable&), SceneSelectionChange> SceneSelectionChangeCaller;
1056         GlobalSelectionSystem().addSelectionChangeCallback( SceneSelectionChangeCaller() );
1057         typedef FreeCaller<void(const Selectable&), UpdateWorkzone_ForSelectionChanged> UpdateWorkzoneForSelectionChangedCaller;
1058         GlobalSelectionSystem().addSelectionChangeCallback( UpdateWorkzoneForSelectionChangedCaller() );
1059         typedef FreeCaller<void(), UpdateWorkzone_ForSelection> UpdateWorkzoneForSelectionCaller;
1060         Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( UpdateWorkzoneForSelectionCaller() );
1061 }
1062
1063 void Selection_destroy(){
1064         GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
1065 }
1066
1067
1068 #include "gtkdlgs.h"
1069 #include <gdk/gdkkeysyms.h>
1070
1071
1072 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
1073 #if 0
1074         return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
1075 #elif 0
1076         return quaternion_multiplied_by_quaternion(
1077                            quaternion_multiplied_by_quaternion(
1078                                    quaternion_for_z( degrees_to_radians( eulerXYZ[2] ) ),
1079                                    quaternion_for_y( degrees_to_radians( eulerXYZ[1] ) )
1080                                    ),
1081                            quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
1082                            );
1083 #elif 1
1084         double cx = cos( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
1085         double sx = sin( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
1086         double cy = cos( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
1087         double sy = sin( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
1088         double cz = cos( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
1089         double sz = sin( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
1090
1091         return Quaternion(
1092                            cz * cy * sx - sz * sy * cx,
1093                            cz * sy * cx + sz * cy * sx,
1094                            sz * cy * cx - cz * sy * sx,
1095                            cz * cy * cx + sz * sy * sx
1096                            );
1097 #endif
1098 }
1099
1100 struct RotateDialog
1101 {
1102         ui::SpinButton x{ui::null};
1103         ui::SpinButton y{ui::null};
1104         ui::SpinButton z{ui::null};
1105         ui::Window window{ui::null};
1106 };
1107
1108 static gboolean rotatedlg_apply( ui::Widget widget, RotateDialog* rotateDialog ){
1109         Vector3 eulerXYZ;
1110
1111         gtk_spin_button_update ( rotateDialog->x );
1112         gtk_spin_button_update ( rotateDialog->y );
1113         gtk_spin_button_update ( rotateDialog->z );
1114         eulerXYZ[0] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->x ) );
1115         eulerXYZ[1] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->y ) );
1116         eulerXYZ[2] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->z ) );
1117
1118         StringOutputStream command;
1119         command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
1120         UndoableCommand undo( command.c_str() );
1121
1122         GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ), false );
1123         return TRUE;
1124 }
1125
1126 static gboolean rotatedlg_cancel( ui::Widget widget, RotateDialog* rotateDialog ){
1127         rotateDialog->window.hide();
1128
1129         gtk_spin_button_set_value( rotateDialog->x, 0.0f ); // reset to 0 on close
1130         gtk_spin_button_set_value( rotateDialog->y, 0.0f );
1131         gtk_spin_button_set_value( rotateDialog->z, 0.0f );
1132
1133         return TRUE;
1134 }
1135
1136 static gboolean rotatedlg_ok( ui::Widget widget, RotateDialog* rotateDialog ){
1137         rotatedlg_apply( widget, rotateDialog );
1138 //      rotatedlg_cancel( widget, rotateDialog );
1139         rotateDialog->window.hide();
1140         return TRUE;
1141 }
1142
1143 static gboolean rotatedlg_delete( ui::Widget widget, GdkEventAny *event, RotateDialog* rotateDialog ){
1144         rotatedlg_cancel( widget, rotateDialog );
1145         return TRUE;
1146 }
1147
1148 RotateDialog g_rotate_dialog;
1149 void DoRotateDlg(){
1150         if ( !g_rotate_dialog.window ) {
1151                 g_rotate_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary rotation", G_CALLBACK(rotatedlg_delete ), &g_rotate_dialog );
1152
1153                 auto accel = ui::AccelGroup(ui::New);
1154                 g_rotate_dialog.window.add_accel_group( accel );
1155
1156                 {
1157                         auto hbox = create_dialog_hbox( 4, 4 );
1158                         g_rotate_dialog.window.add(hbox);
1159                         {
1160                                 auto table = create_dialog_table( 3, 2, 4, 4 );
1161                                 hbox.pack_start( table, TRUE, TRUE, 0 );
1162                                 {
1163                                         ui::Widget label = ui::Label( "  X  " );
1164                                         label.show();
1165                     table.attach(label, {0, 1, 0, 1}, {0, 0});
1166                                 }
1167                                 {
1168                                         ui::Widget label = ui::Label( "  Y  " );
1169                                         label.show();
1170                     table.attach(label, {0, 1, 1, 2}, {0, 0});
1171                                 }
1172                                 {
1173                                         ui::Widget label = ui::Label( "  Z  " );
1174                                         label.show();
1175                     table.attach(label, {0, 1, 2, 3}, {0, 0});
1176                                 }
1177                                 {
1178                                         auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1179                                         auto spin = ui::SpinButton( adj, 1, 1 );
1180                                         spin.show();
1181                     table.attach(spin, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1182                     spin.dimensions(64, -1);
1183                                         gtk_spin_button_set_wrap( spin, TRUE );
1184
1185                                         gtk_widget_grab_focus( spin  );
1186
1187                                         g_rotate_dialog.x = spin;
1188                                 }
1189                                 {
1190                                         auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1191                                         auto spin = ui::SpinButton( adj, 1, 1 );
1192                                         spin.show();
1193                     table.attach(spin, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1194                     spin.dimensions(64, -1);
1195                                         gtk_spin_button_set_wrap( spin, TRUE );
1196
1197                                         g_rotate_dialog.y = spin;
1198                                 }
1199                                 {
1200                                         auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1201                                         auto spin = ui::SpinButton( adj, 1, 1 );
1202                                         spin.show();
1203                     table.attach(spin, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1204                     spin.dimensions(64, -1);
1205                                         gtk_spin_button_set_wrap( spin, TRUE );
1206
1207                                         g_rotate_dialog.z = spin;
1208                                 }
1209                         }
1210                         {
1211                                 auto vbox = create_dialog_vbox( 4 );
1212                                 hbox.pack_start( vbox, TRUE, TRUE, 0 );
1213                                 {
1214                                         auto button = create_dialog_button( "OK", G_CALLBACK( rotatedlg_ok ), &g_rotate_dialog );
1215                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1216                                         widget_make_default( button );
1217                                         gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1218                                 }
1219                                 {
1220                                         auto button = create_dialog_button( "Cancel", G_CALLBACK( rotatedlg_cancel ), &g_rotate_dialog );
1221                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1222                                         gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1223                                 }
1224                                 {
1225                                         auto button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
1226                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1227                                 }
1228                         }
1229                 }
1230         }
1231
1232         g_rotate_dialog.window.show();
1233 }
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 struct ScaleDialog
1244 {
1245         ui::Entry x{ui::null};
1246         ui::Entry y{ui::null};
1247         ui::Entry z{ui::null};
1248         ui::Window window{ui::null};
1249 };
1250
1251 static gboolean scaledlg_apply( ui::Widget widget, ScaleDialog* scaleDialog ){
1252         float sx, sy, sz;
1253
1254         sx = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->x ) ) ) );
1255         sy = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->y ) ) ) );
1256         sz = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->z ) ) ) );
1257
1258         StringOutputStream command;
1259         command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1260         UndoableCommand undo( command.c_str() );
1261
1262         Select_Scale( sx, sy, sz );
1263
1264         return TRUE;
1265 }
1266
1267 static gboolean scaledlg_cancel( ui::Widget widget, ScaleDialog* scaleDialog ){
1268         scaleDialog->window.hide();
1269
1270         scaleDialog->x.text("1.0");
1271         scaleDialog->y.text("1.0");
1272         scaleDialog->z.text("1.0");
1273
1274         return TRUE;
1275 }
1276
1277 static gboolean scaledlg_ok( ui::Widget widget, ScaleDialog* scaleDialog ){
1278         scaledlg_apply( widget, scaleDialog );
1279         //scaledlg_cancel( widget, scaleDialog );
1280         scaleDialog->window.hide();
1281         return TRUE;
1282 }
1283
1284 static gboolean scaledlg_delete( ui::Widget widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1285         scaledlg_cancel( widget, scaleDialog );
1286         return TRUE;
1287 }
1288
1289 ScaleDialog g_scale_dialog;
1290
1291 void DoScaleDlg(){
1292         if ( !g_scale_dialog.window ) {
1293                 g_scale_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary scale", G_CALLBACK(scaledlg_delete ), &g_scale_dialog );
1294
1295                 auto accel = ui::AccelGroup(ui::New);
1296                 g_scale_dialog.window.add_accel_group( accel );
1297
1298                 {
1299                         auto hbox = create_dialog_hbox( 4, 4 );
1300                         g_scale_dialog.window.add(hbox);
1301                         {
1302                                 auto table = create_dialog_table( 3, 2, 4, 4 );
1303                                 hbox.pack_start( table, TRUE, TRUE, 0 );
1304                                 {
1305                                         ui::Widget label = ui::Label( "  X  " );
1306                                         label.show();
1307                     table.attach(label, {0, 1, 0, 1}, {0, 0});
1308                                 }
1309                                 {
1310                                         ui::Widget label = ui::Label( "  Y  " );
1311                                         label.show();
1312                     table.attach(label, {0, 1, 1, 2}, {0, 0});
1313                                 }
1314                                 {
1315                                         ui::Widget label = ui::Label( "  Z  " );
1316                                         label.show();
1317                     table.attach(label, {0, 1, 2, 3}, {0, 0});
1318                                 }
1319                                 {
1320                                         auto entry = ui::Entry(ui::New);
1321                                         entry.text("1.0");
1322                                         entry.show();
1323                     table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1324
1325                                         g_scale_dialog.x = entry;
1326                                 }
1327                                 {
1328                                         auto entry = ui::Entry(ui::New);
1329                                         entry.text("1.0");
1330                                         entry.show();
1331                     table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1332
1333                                         g_scale_dialog.y = entry;
1334                                 }
1335                                 {
1336                                         auto entry = ui::Entry(ui::New);
1337                                         entry.text("1.0");
1338                                         entry.show();
1339                     table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1340
1341                                         g_scale_dialog.z = entry;
1342                                 }
1343                         }
1344                         {
1345                                 auto vbox = create_dialog_vbox( 4 );
1346                                 hbox.pack_start( vbox, TRUE, TRUE, 0 );
1347                                 {
1348                                         auto button = create_dialog_button( "OK", G_CALLBACK( scaledlg_ok ), &g_scale_dialog );
1349                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1350                                         widget_make_default( button );
1351                                         gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1352                                 }
1353                                 {
1354                                         auto button = create_dialog_button( "Cancel", G_CALLBACK( scaledlg_cancel ), &g_scale_dialog );
1355                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1356                                         gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1357                                 }
1358                                 {
1359                                         auto button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1360                                         vbox.pack_start( button, FALSE, FALSE, 0 );
1361                                 }
1362                         }
1363                 }
1364         }
1365
1366         g_scale_dialog.window.show();
1367 }