2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
26 #include "debugging/debugging.h"
29 #include "iselection.h"
34 #include "stream/stringstream.h"
35 #include "signal/isignal.h"
36 #include "shaderlib.h"
39 #include "gtkutil/idledraw.h"
40 #include "gtkutil/dialog.h"
41 #include "gtkutil/widget.h"
42 #include "brushmanip.h"
45 #include "patchmanip.h"
46 #include "patchdialog.h"
47 #include "selection.h"
48 #include "texwindow.h"
50 #include "mainframe.h"
53 #include "entityinspector.h"
57 select_workzone_t g_select_workzone;
61 Loops over all selected brushes and stores their
62 world AABBs in the specified array.
64 class CollectSelectedBrushesBounds : public SelectionSystem::Visitor
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
71 CollectSelectedBrushesBounds( AABB* bounds, Unsigned max, Unsigned& count )
78 void visit( scene::Instance& instance ) const {
79 ASSERT_MESSAGE( m_count <= m_max, "Invalid m_count in CollectSelectedBrushesBounds" );
81 // stop if the array is already full
82 if ( m_count == m_max ) {
86 Selectable* selectable = Instance_getSelectable( instance );
87 if ( ( selectable != 0 )
88 && instance.isSelected() ) {
90 if ( Instance_getBrush( instance ) != 0 ) {
91 m_bounds[m_count] = instance.worldAABB();
99 Selects all objects that intersect one of the bounding AABBs.
100 The exact intersection-method is specified through TSelectionPolicy
102 template<class TSelectionPolicy>
103 class SelectByBounds : public scene::Graph::Walker
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
110 SelectByBounds( AABB* aabbs, Unsigned count )
115 bool pre( const scene::Path& path, scene::Instance& instance ) const {
116 if( path.top().get().visible() ){
117 Selectable* selectable = Instance_getSelectable( instance );
120 Entity* entity = Node_getEntity( path.top() );
122 if ( string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ) {
127 if ( ( path.size() > 1 ) &&
128 ( !path.top().get().isRoot() ) &&
129 ( selectable != 0 ) &&
130 ( !node_is_group( path.top() ) )
132 for ( Unsigned i = 0; i < m_count; ++i )
134 if ( policy.Evaluate( m_aabbs[i], instance ) ) {
135 selectable->setSelected( true );
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.
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];
159 CollectSelectedBrushesBounds collector( aabbs, max, count );
160 GlobalSelectionSystem().foreachSelected( collector );
162 // nothing usable in selection
168 // delete selected objects
169 if ( delete_bounds_src ) { // see deleteSelection
170 UndoableCommand undo( "deleteSelected" );
174 // select objects with bounds
175 GlobalSceneGraph().traverse( SelectByBounds<TSelectionPolicy>( aabbs, count ) );
184 SelectionPolicy for SelectByBounds
185 Returns true if box and the AABB of instance intersect
187 class SelectionPolicy_Touching
190 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
191 const AABB& other( instance.worldAABB() );
192 for ( Unsigned i = 0; i < 3; ++i )
194 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] + other.extents[i] ) ) {
203 SelectionPolicy for SelectByBounds
204 Returns true if the AABB of instance is inside box
206 class SelectionPolicy_Inside
209 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
210 const AABB& other( instance.worldAABB() );
211 for ( Unsigned i = 0; i < 3; ++i )
213 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] - other.extents[i] ) ) {
221 class DeleteSelected : public scene::Graph::Walker
223 mutable bool m_remove;
224 mutable bool m_removedChild;
227 : m_remove( false ), m_removedChild( false ){
229 bool pre( const scene::Path& path, scene::Instance& instance ) const {
230 m_removedChild = false;
232 Selectable* selectable = Instance_getSelectable( instance );
234 && selectable->isSelected()
236 && !path.top().get().isRoot() ) {
239 return false; // dont traverse into child elements
243 void post( const scene::Path& path, scene::Instance& instance ) const {
245 if ( m_removedChild ) {
246 m_removedChild = false;
248 // delete empty entities
249 Entity* entity = Node_getEntity( path.top() );
251 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
252 && Node_getTraversable( path.top() )->empty() ) {
253 Path_deleteTop( path );
257 // node should be removed
259 if ( Node_isEntity( path.parent() ) != 0 ) {
260 m_removedChild = true;
264 Path_deleteTop( path );
269 void Scene_DeleteSelected( scene::Graph& graph ){
270 graph.traverse( DeleteSelected() );
274 void Select_Delete( void ){
275 Scene_DeleteSelected( GlobalSceneGraph() );
278 class InvertSelectionWalker : public scene::Graph::Walker
280 SelectionSystem::EMode m_mode;
281 SelectionSystem::EComponentMode m_compmode;
282 mutable Selectable* m_selectable;
284 InvertSelectionWalker( SelectionSystem::EMode mode, SelectionSystem::EComponentMode compmode )
285 : m_mode( mode ), m_compmode( compmode ), m_selectable( 0 ){
287 bool pre( const scene::Path& path, scene::Instance& instance ) const {
288 if( !path.top().get().visible() ){
292 Selectable* selectable = Instance_getSelectable( instance );
296 case SelectionSystem::eEntity:
297 if ( Node_isEntity( path.top() ) != 0 ) {
298 m_selectable = path.top().get().visible() ? selectable : 0;
301 case SelectionSystem::ePrimitive:
302 m_selectable = path.top().get().visible() ? selectable : 0;
304 case SelectionSystem::eComponent:
305 BrushInstance* brushinstance = Instance_getBrush( instance );
306 if( brushinstance != 0 ){
307 if( brushinstance->isSelected() )
308 brushinstance->invertComponentSelection( m_compmode );
311 PatchInstance* patchinstance = Instance_getPatch( instance );
312 if( patchinstance != 0 && m_compmode == SelectionSystem::eVertex ){
313 if( patchinstance->isSelected() )
314 patchinstance->invertComponentSelection();
322 void post( const scene::Path& path, scene::Instance& instance ) const {
323 if ( m_selectable != 0 ) {
324 m_selectable->setSelected( !m_selectable->isSelected() );
330 void Scene_Invert_Selection( scene::Graph& graph ){
331 graph.traverse( InvertSelectionWalker( GlobalSelectionSystem().Mode(), GlobalSelectionSystem().ComponentMode() ) );
334 void Select_Invert(){
335 Scene_Invert_Selection( GlobalSceneGraph() );
338 //interesting printings
339 class ExpandSelectionToEntitiesWalker_dbg : public scene::Graph::Walker
341 mutable std::size_t m_depth;
342 NodeSmartReference worldspawn;
344 ExpandSelectionToEntitiesWalker_dbg() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
346 bool pre( const scene::Path& path, scene::Instance& instance ) const {
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() );
356 globalOutputStream() << " entity!=0";
357 if( entity->isContainer() ){
358 globalOutputStream() << " entity->isContainer()";
360 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
362 globalOutputStream() << "\n";
363 // globalOutputStream() << "" << ;
364 // globalOutputStream() << "" << ;
365 // globalOutputStream() << "" << ;
366 // globalOutputStream() << "" << ;
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() );
378 globalOutputStream() << " entity!=0";
379 if( entity->isContainer() ){
380 globalOutputStream() << " entity->isContainer()";
382 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
384 globalOutputStream() << "\n";
389 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
391 mutable std::size_t m_depth;
392 NodeSmartReference worldspawn;
394 ExpandSelectionToEntitiesWalker() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
396 bool pre( const scene::Path& path, scene::Instance& instance ) const {
400 // NodeSmartReference me( path.top().get() );
401 // if ( me == worldspawn ) {
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() || instance.isSelected() ) {
410 if( path.top().get() != worldspawn ){
411 Instance_setSelected( instance, true );
414 return Node_getEntity( path.top() )->isContainer() && beselected;
416 else if ( m_depth == 3 ) { // primitive depth
417 Instance_setSelected( instance, true );
422 void post( const scene::Path& path, scene::Instance& instance ) const {
427 void Scene_ExpandSelectionToEntities(){
428 GlobalSceneGraph().traverse( ExpandSelectionToEntitiesWalker() );
434 void Selection_UpdateWorkzone(){
435 if ( GlobalSelectionSystem().countSelected() != 0 ) {
436 Select_GetBounds( g_select_workzone.d_work_min, g_select_workzone.d_work_max );
439 typedef FreeCaller<void(), Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
441 IdleDraw g_idleWorkzone = IdleDraw( SelectionUpdateWorkzoneCaller() );
444 const select_workzone_t& Select_getWorkZone(){
445 g_idleWorkzone.flush();
446 return g_select_workzone;
449 void UpdateWorkzone_ForSelection(){
450 g_idleWorkzone.queueDraw();
453 // update the workzone to the current selection
454 void UpdateWorkzone_ForSelectionChanged( const Selectable& selectable ){
455 if ( selectable.isSelected() ) {
456 UpdateWorkzone_ForSelection();
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 );
465 Scene_BrushSetShader_Component_Selected( GlobalSceneGraph(), shader );
468 void Select_SetTexdef( const TextureProjection& projection ){
469 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
470 Scene_BrushSetTexdef_Selected( GlobalSceneGraph(), projection );
472 Scene_BrushSetTexdef_Component_Selected( GlobalSceneGraph(), projection );
475 void Select_SetFlags( const ContentsFlagsValue& flags ){
476 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
477 Scene_BrushSetFlags_Selected( GlobalSceneGraph(), flags );
479 Scene_BrushSetFlags_Component_Selected( GlobalSceneGraph(), flags );
482 void Select_GetBounds( Vector3& mins, Vector3& maxs ){
484 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
485 maxs = vector3_added( bounds.origin, bounds.extents );
486 mins = vector3_subtracted( bounds.origin, bounds.extents );
489 void Select_GetMid( Vector3& mid ){
491 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
492 mid = vector3_snapped( bounds.origin );
496 void Select_FlipAxis( int axis ){
497 Vector3 flip( 1, 1, 1 );
499 GlobalSelectionSystem().scaleSelected( flip );
503 void Select_Scale( float x, float y, float z ){
504 GlobalSelectionSystem().scaleSelected( Vector3( x, y, z ) );
520 inline Matrix4 matrix4_rotation_for_axis90( axis_t axis, sign_t sign ){
524 if ( sign == eSignPositive ) {
525 return matrix4_rotation_for_sincos_x( 1, 0 );
529 return matrix4_rotation_for_sincos_x( -1, 0 );
532 if ( sign == eSignPositive ) {
533 return matrix4_rotation_for_sincos_y( 1, 0 );
537 return matrix4_rotation_for_sincos_y( -1, 0 );
539 default: //case eAxisZ:
540 if ( sign == eSignPositive ) {
541 return matrix4_rotation_for_sincos_z( 1, 0 );
545 return matrix4_rotation_for_sincos_z( -1, 0 );
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 ) );
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 ) );
560 inline Quaternion quaternion_for_axis90( axis_t axis, sign_t sign ){
565 if ( sign == eSignPositive ) {
566 return Quaternion( c_half_sqrt2f, 0, 0, c_half_sqrt2f );
570 return Quaternion( -c_half_sqrt2f, 0, 0, -c_half_sqrt2f );
573 if ( sign == eSignPositive ) {
574 return Quaternion( 0, c_half_sqrt2f, 0, c_half_sqrt2f );
578 return Quaternion( 0, -c_half_sqrt2f, 0, -c_half_sqrt2f );
580 default: //case eAxisZ:
581 if ( sign == eSignPositive ) {
582 return Quaternion( 0, 0, c_half_sqrt2f, c_half_sqrt2f );
586 return Quaternion( 0, 0, -c_half_sqrt2f, -c_half_sqrt2f );
590 quaternion_for_matrix4_rotation( matrix4_rotation_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
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 );
603 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_x_degrees( deg ) ), false );
606 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_y_degrees( deg ) ), false );
609 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_z_degrees( deg ) ), false );
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 );
621 //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
622 Scene_BrushShiftTexdef_Component_Selected( GlobalSceneGraph(), x, y );
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 );
630 Scene_BrushScaleTexdef_Component_Selected( GlobalSceneGraph(), x, y );
633 void Select_RotateTexture( float amt ){
634 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
635 Scene_BrushRotateTexdef_Selected( GlobalSceneGraph(), amt );
636 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), amt );
638 Scene_BrushRotateTexdef_Component_Selected( GlobalSceneGraph(), amt );
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";
648 if ( !texdef_name_valid( pReplace ) ) {
649 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
653 StringOutputStream command;
654 command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
655 UndoableCommand undo( command.c_str() );
658 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
659 Scene_BrushFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
660 Scene_PatchFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
662 Scene_BrushFindReplaceShader_Component_Selected( GlobalSceneGraph(), pFind, pReplace );
666 Scene_BrushFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
667 Scene_PatchFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
671 typedef std::vector<const char*> PropertyValues;
673 bool propertyvalues_contain( const PropertyValues& propertyvalues, const char *str ){
674 for ( PropertyValues::const_iterator i = propertyvalues.begin(); i != propertyvalues.end(); ++i )
676 if ( string_equal( str, *i ) ) {
683 class EntityFindByPropertyValueWalker : public scene::Graph::Walker
685 const PropertyValues& m_propertyvalues;
687 const NodeSmartReference worldspawn;
689 EntityFindByPropertyValueWalker( const char *prop, const PropertyValues& propertyvalues )
690 : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
692 bool pre( const scene::Path& path, scene::Instance& instance ) const {
693 if( !path.top().get().visible() ){
697 if ( path.top().get() == worldspawn ) {
701 Entity* entity = Node_getEntity( path.top() );
703 if( propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
704 Instance_getSelectable( instance )->setSelected( true );
709 else if( path.size() > 2 && !path.top().get().isRoot() ){
710 Selectable* selectable = Instance_getSelectable( instance );
711 if( selectable != 0 )
712 selectable->setSelected( true );
718 void Scene_EntitySelectByPropertyValues( scene::Graph& graph, const char *prop, const PropertyValues& propertyvalues ){
719 graph.traverse( EntityFindByPropertyValueWalker( prop, propertyvalues ) );
722 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
724 PropertyValues& m_propertyvalues;
726 const NodeSmartReference worldspawn;
728 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
729 : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
731 bool pre( const scene::Path& path, scene::Instance& instance ) const {
732 Entity* entity = Node_getEntity( path.top() );
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 ) );
748 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
750 PropertyValues& m_propertyvalues;
752 mutable bool m_selected_children;
753 const NodeSmartReference worldspawn;
755 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
756 : m_propertyvalues( propertyvalues ), m_prop( prop ), m_selected_children( false ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
758 bool pre( const scene::Path& path, scene::Instance& instance ) const {
759 Selectable* selectable = Instance_getSelectable( instance );
761 && selectable->isSelected() ) {
762 Entity* entity = Node_getEntity( path.top() );
764 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
765 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
770 m_selected_children = true;
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 )
781 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
782 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
788 void Scene_EntityGetPropertyValues( scene::Graph& graph, const char *prop, PropertyValues& propertyvalues ){
789 graph.traverse( EntityGetSelectedPropertyValuesWalker( prop, propertyvalues ) );
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() ) );
801 PropertyValues propertyvalues;
802 const char *prop = EntityInspector_getCurrentKey();
803 if ( !prop || !*prop ) {
806 Scene_EntityGetPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
807 GlobalSelectionSystem().setSelectedAll( false );
808 if ( !propertyvalues.empty() ) {
809 Scene_EntitySelectByPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
813 Scene_BrushSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
814 Scene_PatchSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
819 void Select_Inside( void ){
820 SelectByBounds<SelectionPolicy_Inside>::DoSelection();
823 void Select_Touching( void ){
824 SelectByBounds<SelectionPolicy_Touching>::DoSelection( false );
827 void Select_FitTexture( float horizontal, float vertical ){
828 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
829 Scene_BrushFitTexture_Selected( GlobalSceneGraph(), horizontal, vertical );
831 Scene_BrushFitTexture_Component_Selected( GlobalSceneGraph(), horizontal, vertical );
837 #include "commands.h"
840 inline void hide_node( scene::Node& node, bool hide ){
842 ? node.enable( scene::Node::eHidden )
843 : node.disable( scene::Node::eHidden );
846 bool g_nodes_be_hidden = false;
848 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_hidden_caller( g_nodes_be_hidden );
850 ToggleItem g_hidden_item( g_hidden_caller );
852 class HideSelectedWalker : public scene::Graph::Walker
856 HideSelectedWalker( bool hide )
859 bool pre( const scene::Path& path, scene::Instance& instance ) const {
860 Selectable* selectable = Instance_getSelectable( instance );
862 && selectable->isSelected() ) {
863 g_nodes_be_hidden = m_hide;
864 hide_node( path.top(), m_hide );
870 void Scene_Hide_Selected( bool hide ){
871 GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
875 Scene_Hide_Selected( true );
881 GlobalSelectionSystem().setSelectedAll( false );
882 g_hidden_item.update();
886 class HideAllWalker : public scene::Graph::Walker
890 HideAllWalker( bool hide )
893 bool pre( const scene::Path& path, scene::Instance& instance ) const {
894 hide_node( path.top(), m_hide );
899 void Scene_Hide_All( bool hide ){
900 GlobalSceneGraph().traverse( HideAllWalker( hide ) );
903 void Select_ShowAllHidden(){
904 Scene_Hide_All( false );
906 g_nodes_be_hidden = false;
907 g_hidden_item.update();
911 void Selection_Flipx(){
912 UndoableCommand undo( "mirrorSelected -axis x" );
913 Select_FlipAxis( 0 );
916 void Selection_Flipy(){
917 UndoableCommand undo( "mirrorSelected -axis y" );
918 Select_FlipAxis( 1 );
921 void Selection_Flipz(){
922 UndoableCommand undo( "mirrorSelected -axis z" );
923 Select_FlipAxis( 2 );
926 void Selection_Rotatex(){
927 UndoableCommand undo( "rotateSelected -axis x -angle -90" );
928 Select_RotateAxis( 0,-90 );
931 void Selection_Rotatey(){
932 UndoableCommand undo( "rotateSelected -axis y -angle 90" );
933 Select_RotateAxis( 1, 90 );
936 void Selection_Rotatez(){
937 UndoableCommand undo( "rotateSelected -axis z -angle -90" );
938 Select_RotateAxis( 2,-90 );
940 #include "xywindow.h"
941 void Selection_FlipHorizontally(){
942 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
955 void Selection_FlipVertically(){
956 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
969 void Selection_RotateClockwise(){
970 UndoableCommand undo( "rotateSelected Clockwise 90" );
971 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
975 Select_RotateAxis( 2, -90 );
978 Select_RotateAxis( 1, 90 );
981 Select_RotateAxis( 0, -90 );
986 void Selection_RotateAnticlockwise(){
987 UndoableCommand undo( "rotateSelected Anticlockwise 90" );
988 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
992 Select_RotateAxis( 2, 90 );
995 Select_RotateAxis( 1, -90 );
998 Select_RotateAxis( 0, 90 );
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' ) );
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 ) );
1017 GlobalCommands_insert( "MirrorSelectionHorizontally", makeCallbackF( Selection_FlipHorizontally ) );
1018 GlobalCommands_insert( "MirrorSelectionVertically", makeCallbackF( Selection_FlipVertically ) );
1020 GlobalCommands_insert( "RotateSelectionClockwise", makeCallbackF( Selection_RotateClockwise ) );
1021 GlobalCommands_insert( "RotateSelectionAnticlockwise", makeCallbackF( Selection_RotateAnticlockwise ) );
1025 void Nudge( int nDim, float fNudge ){
1026 Vector3 translate( 0, 0, 0 );
1027 translate[nDim] = fNudge;
1029 GlobalSelectionSystem().translateSelected( translate );
1032 void Selection_NudgeZ( float amount ){
1033 StringOutputStream command;
1034 command << "nudgeSelected -axis z -amount " << amount;
1035 UndoableCommand undo( command.c_str() );
1040 void Selection_MoveDown(){
1041 Selection_NudgeZ( -GetGridSize() );
1044 void Selection_MoveUp(){
1045 Selection_NudgeZ( GetGridSize() );
1048 void SceneSelectionChange( const Selectable& selectable ){
1049 SceneChangeNotify();
1052 SignalHandlerId Selection_boundsChanged;
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() );
1063 void Selection_destroy(){
1064 GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
1068 #include "gtkdlgs.h"
1069 #include <gdk/gdkkeysyms.h>
1072 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
1074 return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
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] ) )
1081 quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
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 ) );
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
1102 ui::SpinButton x{ui::null};
1103 ui::SpinButton y{ui::null};
1104 ui::SpinButton z{ui::null};
1105 ui::Window window{ui::null};
1108 static gboolean rotatedlg_apply( ui::Widget widget, RotateDialog* rotateDialog ){
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 ) );
1118 StringOutputStream command;
1119 command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
1120 UndoableCommand undo( command.c_str() );
1122 GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ), false );
1126 static gboolean rotatedlg_cancel( ui::Widget widget, RotateDialog* rotateDialog ){
1127 rotateDialog->window.hide();
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 );
1136 static gboolean rotatedlg_ok( ui::Widget widget, RotateDialog* rotateDialog ){
1137 rotatedlg_apply( widget, rotateDialog );
1138 // rotatedlg_cancel( widget, rotateDialog );
1139 rotateDialog->window.hide();
1143 static gboolean rotatedlg_delete( ui::Widget widget, GdkEventAny *event, RotateDialog* rotateDialog ){
1144 rotatedlg_cancel( widget, rotateDialog );
1148 RotateDialog g_rotate_dialog;
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 );
1153 auto accel = ui::AccelGroup(ui::New);
1154 g_rotate_dialog.window.add_accel_group( accel );
1157 auto hbox = create_dialog_hbox( 4, 4 );
1158 g_rotate_dialog.window.add(hbox);
1160 auto table = create_dialog_table( 3, 2, 4, 4 );
1161 hbox.pack_start( table, TRUE, TRUE, 0 );
1163 ui::Widget label = ui::Label( " X " );
1165 table.attach(label, {0, 1, 0, 1}, {0, 0});
1168 ui::Widget label = ui::Label( " Y " );
1170 table.attach(label, {0, 1, 1, 2}, {0, 0});
1173 ui::Widget label = ui::Label( " Z " );
1175 table.attach(label, {0, 1, 2, 3}, {0, 0});
1178 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1179 auto spin = ui::SpinButton( adj, 1, 1 );
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 );
1185 gtk_widget_grab_focus( spin );
1187 g_rotate_dialog.x = spin;
1190 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1191 auto spin = ui::SpinButton( adj, 1, 1 );
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 );
1197 g_rotate_dialog.y = spin;
1200 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
1201 auto spin = ui::SpinButton( adj, 1, 1 );
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 );
1207 g_rotate_dialog.z = spin;
1211 auto vbox = create_dialog_vbox( 4 );
1212 hbox.pack_start( vbox, TRUE, TRUE, 0 );
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 );
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 );
1225 auto button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
1226 vbox.pack_start( button, FALSE, FALSE, 0 );
1232 g_rotate_dialog.window.show();
1245 ui::Entry x{ui::null};
1246 ui::Entry y{ui::null};
1247 ui::Entry z{ui::null};
1248 ui::Window window{ui::null};
1251 static gboolean scaledlg_apply( ui::Widget widget, ScaleDialog* scaleDialog ){
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 ) ) ) );
1258 StringOutputStream command;
1259 command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1260 UndoableCommand undo( command.c_str() );
1262 Select_Scale( sx, sy, sz );
1267 static gboolean scaledlg_cancel( ui::Widget widget, ScaleDialog* scaleDialog ){
1268 scaleDialog->window.hide();
1270 scaleDialog->x.text("1.0");
1271 scaleDialog->y.text("1.0");
1272 scaleDialog->z.text("1.0");
1277 static gboolean scaledlg_ok( ui::Widget widget, ScaleDialog* scaleDialog ){
1278 scaledlg_apply( widget, scaleDialog );
1279 //scaledlg_cancel( widget, scaleDialog );
1280 scaleDialog->window.hide();
1284 static gboolean scaledlg_delete( ui::Widget widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1285 scaledlg_cancel( widget, scaleDialog );
1289 ScaleDialog g_scale_dialog;
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 );
1295 auto accel = ui::AccelGroup(ui::New);
1296 g_scale_dialog.window.add_accel_group( accel );
1299 auto hbox = create_dialog_hbox( 4, 4 );
1300 g_scale_dialog.window.add(hbox);
1302 auto table = create_dialog_table( 3, 2, 4, 4 );
1303 hbox.pack_start( table, TRUE, TRUE, 0 );
1305 ui::Widget label = ui::Label( " X " );
1307 table.attach(label, {0, 1, 0, 1}, {0, 0});
1310 ui::Widget label = ui::Label( " Y " );
1312 table.attach(label, {0, 1, 1, 2}, {0, 0});
1315 ui::Widget label = ui::Label( " Z " );
1317 table.attach(label, {0, 1, 2, 3}, {0, 0});
1320 auto entry = ui::Entry(ui::New);
1323 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1325 g_scale_dialog.x = entry;
1328 auto entry = ui::Entry(ui::New);
1331 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1333 g_scale_dialog.y = entry;
1336 auto entry = ui::Entry(ui::New);
1339 table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1341 g_scale_dialog.z = entry;
1345 auto vbox = create_dialog_vbox( 4 );
1346 hbox.pack_start( vbox, TRUE, TRUE, 0 );
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 );
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 );
1359 auto button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1360 vbox.pack_start( button, FALSE, FALSE, 0 );
1366 g_scale_dialog.window.show();