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