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
24 #include "debugging/debugging.h"
27 #include "iselection.h"
32 #include "stream/stringstream.h"
33 #include "signal/isignal.h"
34 #include "shaderlib.h"
37 #include "gtkutil/idledraw.h"
38 #include "gtkutil/dialog.h"
39 #include "gtkutil/widget.h"
40 #include "brushmanip.h"
42 #include "patchmanip.h"
43 #include "patchdialog.h"
44 #include "selection.h"
45 #include "texwindow.h"
47 #include "mainframe.h"
50 #include "entityinspector.h"
54 select_workzone_t g_select_workzone;
58 Loops over all selected brushes and stores their
59 world AABBs in the specified array.
61 class CollectSelectedBrushesBounds : public SelectionSystem::Visitor
63 AABB* m_bounds; // array of AABBs
64 Unsigned m_max; // max AABB-elements in array
65 Unsigned& m_count; // count of valid AABBs stored in array
68 CollectSelectedBrushesBounds( AABB* bounds, Unsigned max, Unsigned& count )
75 void visit( scene::Instance& instance ) const {
76 ASSERT_MESSAGE( m_count <= m_max, "Invalid m_count in CollectSelectedBrushesBounds" );
78 // stop if the array is already full
79 if ( m_count == m_max ) {
83 Selectable* selectable = Instance_getSelectable( instance );
84 if ( ( selectable != 0 )
85 && instance.isSelected() ) {
87 if ( Instance_getBrush( instance ) != 0 ) {
88 m_bounds[m_count] = instance.worldAABB();
96 Selects all objects that intersect one of the bounding AABBs.
97 The exact intersection-method is specified through TSelectionPolicy
99 template<class TSelectionPolicy>
100 class SelectByBounds : public scene::Graph::Walker
102 AABB* m_aabbs; // selection aabbs
103 Unsigned m_count; // number of aabbs in m_aabbs
104 TSelectionPolicy policy; // type that contains a custom intersection method aabb<->aabb
107 SelectByBounds( AABB* aabbs, Unsigned count )
112 bool pre( const scene::Path& path, scene::Instance& instance ) const {
113 if( path.top().get().visible() ){
114 Selectable* selectable = Instance_getSelectable( instance );
117 Entity* entity = Node_getEntity( path.top() );
119 if ( string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ) {
124 if ( ( path.size() > 1 ) &&
125 ( !path.top().get().isRoot() ) &&
126 ( selectable != 0 ) &&
127 ( !node_is_group( path.top() ) )
129 for ( Unsigned i = 0; i < m_count; ++i )
131 if ( policy.Evaluate( m_aabbs[i], instance ) ) {
132 selectable->setSelected( true );
145 Performs selection operation on the global scenegraph.
146 If delete_bounds_src is true, then the objects which were
147 used as source for the selection aabbs will be deleted.
149 static void DoSelection( bool delete_bounds_src = true ){
150 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
151 // we may not need all AABBs since not all selected objects have to be brushes
152 const Unsigned max = (Unsigned)GlobalSelectionSystem().countSelected();
153 AABB* aabbs = new AABB[max];
156 CollectSelectedBrushesBounds collector( aabbs, max, count );
157 GlobalSelectionSystem().foreachSelected( collector );
159 // nothing usable in selection
165 // delete selected objects
166 if ( delete_bounds_src ) { // see deleteSelection
167 UndoableCommand undo( "deleteSelected" );
171 // select objects with bounds
172 GlobalSceneGraph().traverse( SelectByBounds<TSelectionPolicy>( aabbs, count ) );
181 SelectionPolicy for SelectByBounds
182 Returns true if box and the AABB of instance intersect
184 class SelectionPolicy_Touching
187 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
188 const AABB& other( instance.worldAABB() );
189 for ( Unsigned i = 0; i < 3; ++i )
191 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] + other.extents[i] ) ) {
200 SelectionPolicy for SelectByBounds
201 Returns true if the AABB of instance is inside box
203 class SelectionPolicy_Inside
206 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
207 const AABB& other( instance.worldAABB() );
208 for ( Unsigned i = 0; i < 3; ++i )
210 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] - other.extents[i] ) ) {
218 class DeleteSelected : public scene::Graph::Walker
220 mutable bool m_remove;
221 mutable bool m_removedChild;
224 : m_remove( false ), m_removedChild( false ){
226 bool pre( const scene::Path& path, scene::Instance& instance ) const {
227 m_removedChild = false;
229 Selectable* selectable = Instance_getSelectable( instance );
231 && selectable->isSelected()
233 && !path.top().get().isRoot() ) {
236 return false; // dont traverse into child elements
240 void post( const scene::Path& path, scene::Instance& instance ) const {
242 if ( m_removedChild ) {
243 m_removedChild = false;
245 // delete empty entities
246 Entity* entity = Node_getEntity( path.top() );
248 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
249 && Node_getTraversable( path.top() )->empty() ) {
250 Path_deleteTop( path );
254 // node should be removed
256 if ( Node_isEntity( path.parent() ) != 0 ) {
257 m_removedChild = true;
261 Path_deleteTop( path );
266 void Scene_DeleteSelected( scene::Graph& graph ){
267 graph.traverse( DeleteSelected() );
271 void Select_Delete( void ){
272 Scene_DeleteSelected( GlobalSceneGraph() );
275 class InvertSelectionWalker : public scene::Graph::Walker
277 SelectionSystem::EMode m_mode;
278 mutable Selectable* m_selectable;
280 InvertSelectionWalker( SelectionSystem::EMode mode )
281 : m_mode( mode ), m_selectable( 0 ){
283 bool pre( const scene::Path& path, scene::Instance& instance ) const {
284 if( !path.top().get().visible() ){
288 Selectable* selectable = Instance_getSelectable( instance );
292 case SelectionSystem::eEntity:
293 if ( Node_isEntity( path.top() ) != 0 ) {
294 m_selectable = path.top().get().visible() ? selectable : 0;
297 case SelectionSystem::ePrimitive:
298 m_selectable = path.top().get().visible() ? selectable : 0;
300 case SelectionSystem::eComponent:
306 void post( const scene::Path& path, scene::Instance& instance ) const {
307 if ( m_selectable != 0 ) {
308 m_selectable->setSelected( !m_selectable->isSelected() );
314 void Scene_Invert_Selection( scene::Graph& graph ){
315 graph.traverse( InvertSelectionWalker( GlobalSelectionSystem().Mode() ) );
318 void Select_Invert(){
319 Scene_Invert_Selection( GlobalSceneGraph() );
322 //interesting printings
323 class ExpandSelectionToEntitiesWalker_dbg : public scene::Graph::Walker
325 mutable std::size_t m_depth;
326 NodeSmartReference worldspawn;
328 ExpandSelectionToEntitiesWalker_dbg() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
330 bool pre( const scene::Path& path, scene::Instance& instance ) const {
332 globalOutputStream() << "pre depth_" << m_depth;
333 globalOutputStream() << " path.size()_" << path.size();
334 if ( path.top().get() == worldspawn )
335 globalOutputStream() << " worldspawn";
336 if( path.top().get().isRoot() )
337 globalOutputStream() << " path.top().get().isRoot()";
338 Entity* entity = Node_getEntity( path.top() );
340 globalOutputStream() << " entity!=0";
341 if( entity->isContainer() ){
342 globalOutputStream() << " entity->isContainer()";
344 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
346 globalOutputStream() << "\n";
347 // globalOutputStream() << "" << ;
348 // globalOutputStream() << "" << ;
349 // globalOutputStream() << "" << ;
350 // globalOutputStream() << "" << ;
353 void post( const scene::Path& path, scene::Instance& instance ) const {
354 globalOutputStream() << "post depth_" << m_depth;
355 globalOutputStream() << " path.size()_" << path.size();
356 if ( path.top().get() == worldspawn )
357 globalOutputStream() << " worldspawn";
358 if( path.top().get().isRoot() )
359 globalOutputStream() << " path.top().get().isRoot()";
360 Entity* entity = Node_getEntity( path.top() );
362 globalOutputStream() << " entity!=0";
363 if( entity->isContainer() ){
364 globalOutputStream() << " entity->isContainer()";
366 globalOutputStream() << " classname_" << entity->getKeyValue( "classname" );
368 globalOutputStream() << "\n";
373 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
375 mutable std::size_t m_depth;
376 NodeSmartReference worldspawn;
378 ExpandSelectionToEntitiesWalker() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
380 bool pre( const scene::Path& path, scene::Instance& instance ) const {
384 // NodeSmartReference me( path.top().get() );
385 // if ( me == worldspawn ) {
389 if ( m_depth == 2 ) { // entity depth
390 // traverse and select children if any one is selected
391 bool beselected = false;
392 if ( instance.childSelected() ) {
394 if( path.top().get() != worldspawn ){
395 Instance_setSelected( instance, true );
398 return Node_getEntity( path.top() )->isContainer() && beselected;
400 else if ( m_depth == 3 ) { // primitive depth
401 Instance_setSelected( instance, true );
406 void post( const scene::Path& path, scene::Instance& instance ) const {
411 void Scene_ExpandSelectionToEntities(){
412 GlobalSceneGraph().traverse( ExpandSelectionToEntitiesWalker() );
418 void Selection_UpdateWorkzone(){
419 if ( GlobalSelectionSystem().countSelected() != 0 ) {
420 Select_GetBounds( g_select_workzone.d_work_min, g_select_workzone.d_work_max );
423 typedef FreeCaller<Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
425 IdleDraw g_idleWorkzone = IdleDraw( SelectionUpdateWorkzoneCaller() );
428 const select_workzone_t& Select_getWorkZone(){
429 g_idleWorkzone.flush();
430 return g_select_workzone;
433 void UpdateWorkzone_ForSelection(){
434 g_idleWorkzone.queueDraw();
437 // update the workzone to the current selection
438 void UpdateWorkzone_ForSelectionChanged( const Selectable& selectable ){
439 if ( selectable.isSelected() ) {
440 UpdateWorkzone_ForSelection();
444 void Select_SetShader( const char* shader ){
445 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
446 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
447 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
449 Scene_BrushSetShader_Component_Selected( GlobalSceneGraph(), shader );
452 void Select_SetTexdef( const TextureProjection& projection ){
453 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
454 Scene_BrushSetTexdef_Selected( GlobalSceneGraph(), projection );
456 Scene_BrushSetTexdef_Component_Selected( GlobalSceneGraph(), projection );
459 void Select_SetFlags( const ContentsFlagsValue& flags ){
460 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
461 Scene_BrushSetFlags_Selected( GlobalSceneGraph(), flags );
463 Scene_BrushSetFlags_Component_Selected( GlobalSceneGraph(), flags );
466 void Select_GetBounds( Vector3& mins, Vector3& maxs ){
468 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
469 maxs = vector3_added( bounds.origin, bounds.extents );
470 mins = vector3_subtracted( bounds.origin, bounds.extents );
473 void Select_GetMid( Vector3& mid ){
475 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
476 mid = vector3_snapped( bounds.origin );
480 void Select_FlipAxis( int axis ){
481 Vector3 flip( 1, 1, 1 );
483 GlobalSelectionSystem().scaleSelected( flip );
487 void Select_Scale( float x, float y, float z ){
488 GlobalSelectionSystem().scaleSelected( Vector3( x, y, z ) );
504 inline Matrix4 matrix4_rotation_for_axis90( axis_t axis, sign_t sign ){
508 if ( sign == eSignPositive ) {
509 return matrix4_rotation_for_sincos_x( 1, 0 );
513 return matrix4_rotation_for_sincos_x( -1, 0 );
516 if ( sign == eSignPositive ) {
517 return matrix4_rotation_for_sincos_y( 1, 0 );
521 return matrix4_rotation_for_sincos_y( -1, 0 );
523 default: //case eAxisZ:
524 if ( sign == eSignPositive ) {
525 return matrix4_rotation_for_sincos_z( 1, 0 );
529 return matrix4_rotation_for_sincos_z( -1, 0 );
534 inline void matrix4_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign ){
535 matrix4_multiply_by_matrix4( matrix, matrix4_rotation_for_axis90( axis, sign ) );
538 inline void matrix4_pivoted_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign, const Vector3& pivotpoint ){
539 matrix4_translate_by_vec3( matrix, pivotpoint );
540 matrix4_rotate_by_axis90( matrix, axis, sign );
541 matrix4_translate_by_vec3( matrix, vector3_negated( pivotpoint ) );
544 inline Quaternion quaternion_for_axis90( axis_t axis, sign_t sign ){
549 if ( sign == eSignPositive ) {
550 return Quaternion( c_half_sqrt2f, 0, 0, c_half_sqrt2f );
554 return Quaternion( -c_half_sqrt2f, 0, 0, -c_half_sqrt2f );
557 if ( sign == eSignPositive ) {
558 return Quaternion( 0, c_half_sqrt2f, 0, c_half_sqrt2f );
562 return Quaternion( 0, -c_half_sqrt2f, 0, -c_half_sqrt2f );
564 default: //case eAxisZ:
565 if ( sign == eSignPositive ) {
566 return Quaternion( 0, 0, c_half_sqrt2f, c_half_sqrt2f );
570 return Quaternion( 0, 0, -c_half_sqrt2f, -c_half_sqrt2f );
574 quaternion_for_matrix4_rotation( matrix4_rotation_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
578 void Select_RotateAxis( int axis, float deg ){
579 if ( fabs( deg ) == 90.f ) {
580 GlobalSelectionSystem().rotateSelected( quaternion_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ), true );
587 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_x_degrees( deg ) ), false );
590 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_y_degrees( deg ) ), false );
593 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_z_degrees( deg ) ), false );
600 void Select_ShiftTexture( float x, float y ){
601 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
602 Scene_BrushShiftTexdef_Selected( GlobalSceneGraph(), x, y );
603 Scene_PatchTranslateTexture_Selected( GlobalSceneGraph(), x, y );
605 //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
606 Scene_BrushShiftTexdef_Component_Selected( GlobalSceneGraph(), x, y );
609 void Select_ScaleTexture( float x, float y ){
610 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
611 Scene_BrushScaleTexdef_Selected( GlobalSceneGraph(), x, y );
612 Scene_PatchScaleTexture_Selected( GlobalSceneGraph(), x, y );
614 Scene_BrushScaleTexdef_Component_Selected( GlobalSceneGraph(), x, y );
617 void Select_RotateTexture( float amt ){
618 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
619 Scene_BrushRotateTexdef_Selected( GlobalSceneGraph(), amt );
620 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), amt );
622 Scene_BrushRotateTexdef_Component_Selected( GlobalSceneGraph(), amt );
625 // TTimo modified to handle shader architecture:
626 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
627 void FindReplaceTextures( const char* pFind, const char* pReplace, bool bSelected ){
628 if ( !texdef_name_valid( pFind ) ) {
629 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pFind << "', aborted\n";
632 if ( !texdef_name_valid( pReplace ) ) {
633 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
637 StringOutputStream command;
638 command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
639 UndoableCommand undo( command.c_str() );
642 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
643 Scene_BrushFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
644 Scene_PatchFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
646 Scene_BrushFindReplaceShader_Component_Selected( GlobalSceneGraph(), pFind, pReplace );
650 Scene_BrushFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
651 Scene_PatchFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
655 typedef std::vector<const char*> PropertyValues;
657 bool propertyvalues_contain( const PropertyValues& propertyvalues, const char *str ){
658 for ( PropertyValues::const_iterator i = propertyvalues.begin(); i != propertyvalues.end(); ++i )
660 if ( string_equal( str, *i ) ) {
667 class EntityFindByPropertyValueWalker : public scene::Graph::Walker
669 const PropertyValues& m_propertyvalues;
671 const NodeSmartReference worldspawn;
673 EntityFindByPropertyValueWalker( const char *prop, const PropertyValues& propertyvalues )
674 : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
676 bool pre( const scene::Path& path, scene::Instance& instance ) const {
677 if( !path.top().get().visible() ){
681 if ( path.top().get() == worldspawn ) {
685 Entity* entity = Node_getEntity( path.top() );
687 if( propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
688 Instance_getSelectable( instance )->setSelected( true );
693 else if( path.size() > 2 && !path.top().get().isRoot() ){
694 Selectable* selectable = Instance_getSelectable( instance );
695 if( selectable != 0 )
696 selectable->setSelected( true );
702 void Scene_EntitySelectByPropertyValues( scene::Graph& graph, const char *prop, const PropertyValues& propertyvalues ){
703 graph.traverse( EntityFindByPropertyValueWalker( prop, propertyvalues ) );
706 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
708 PropertyValues& m_propertyvalues;
710 const NodeSmartReference worldspawn;
712 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
713 : m_propertyvalues( propertyvalues ), m_prop( prop ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
715 bool pre( const scene::Path& path, scene::Instance& instance ) const {
716 Entity* entity = Node_getEntity( path.top() );
718 if( path.top().get() != worldspawn ){
719 Selectable* selectable = Instance_getSelectable( instance );
720 if ( ( selectable != 0 && selectable->isSelected() ) || instance.childSelected() ) {
721 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
722 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
732 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
734 PropertyValues& m_propertyvalues;
736 mutable bool m_selected_children;
737 const NodeSmartReference worldspawn;
739 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
740 : m_propertyvalues( propertyvalues ), m_prop( prop ), m_selected_children( false ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
742 bool pre( const scene::Path& path, scene::Instance& instance ) const {
743 Selectable* selectable = Instance_getSelectable( instance );
745 && selectable->isSelected() ) {
746 Entity* entity = Node_getEntity( path.top() );
748 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
749 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
754 m_selected_children = true;
759 void post( const scene::Path& path, scene::Instance& instance ) const {
760 Entity* entity = Node_getEntity( path.top() );
761 if( entity != 0 && m_selected_children ){
762 m_selected_children = false;
763 if( path.top().get() == worldspawn )
765 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
766 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
772 void Scene_EntityGetPropertyValues( scene::Graph& graph, const char *prop, PropertyValues& propertyvalues ){
773 graph.traverse( EntityGetSelectedPropertyValuesWalker( prop, propertyvalues ) );
776 void Select_AllOfType(){
777 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
778 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
779 GlobalSelectionSystem().setSelectedAllComponents( false );
780 Scene_BrushSelectByShader_Component( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
785 PropertyValues propertyvalues;
786 const char *prop = EntityInspector_getCurrentKey();
787 if ( !prop || !*prop ) {
790 Scene_EntityGetPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
791 GlobalSelectionSystem().setSelectedAll( false );
792 if ( !propertyvalues.empty() ) {
793 Scene_EntitySelectByPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
797 Scene_BrushSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
798 Scene_PatchSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
803 void Select_Inside( void ){
804 SelectByBounds<SelectionPolicy_Inside>::DoSelection();
807 void Select_Touching( void ){
808 SelectByBounds<SelectionPolicy_Touching>::DoSelection( false );
811 void Select_FitTexture( float horizontal, float vertical ){
812 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
813 Scene_BrushFitTexture_Selected( GlobalSceneGraph(), horizontal, vertical );
815 Scene_BrushFitTexture_Component_Selected( GlobalSceneGraph(), horizontal, vertical );
821 #include "commands.h"
824 inline void hide_node( scene::Node& node, bool hide ){
826 ? node.enable( scene::Node::eHidden )
827 : node.disable( scene::Node::eHidden );
830 bool g_nodes_be_hidden = false;
832 BoolExportCaller g_hidden_caller( g_nodes_be_hidden );
833 ToggleItem g_hidden_item( g_hidden_caller );
835 class HideSelectedWalker : public scene::Graph::Walker
839 HideSelectedWalker( bool hide )
842 bool pre( const scene::Path& path, scene::Instance& instance ) const {
843 Selectable* selectable = Instance_getSelectable( instance );
845 && selectable->isSelected() ) {
846 g_nodes_be_hidden = m_hide;
847 hide_node( path.top(), m_hide );
853 void Scene_Hide_Selected( bool hide ){
854 GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
858 Scene_Hide_Selected( true );
864 GlobalSelectionSystem().setSelectedAll( false );
865 g_hidden_item.update();
869 class HideAllWalker : public scene::Graph::Walker
873 HideAllWalker( bool hide )
876 bool pre( const scene::Path& path, scene::Instance& instance ) const {
877 hide_node( path.top(), m_hide );
882 void Scene_Hide_All( bool hide ){
883 GlobalSceneGraph().traverse( HideAllWalker( hide ) );
886 void Select_ShowAllHidden(){
887 Scene_Hide_All( false );
889 g_nodes_be_hidden = false;
890 g_hidden_item.update();
894 void Selection_Flipx(){
895 UndoableCommand undo( "mirrorSelected -axis x" );
896 Select_FlipAxis( 0 );
899 void Selection_Flipy(){
900 UndoableCommand undo( "mirrorSelected -axis y" );
901 Select_FlipAxis( 1 );
904 void Selection_Flipz(){
905 UndoableCommand undo( "mirrorSelected -axis z" );
906 Select_FlipAxis( 2 );
909 void Selection_Rotatex(){
910 UndoableCommand undo( "rotateSelected -axis x -angle -90" );
911 Select_RotateAxis( 0,-90 );
914 void Selection_Rotatey(){
915 UndoableCommand undo( "rotateSelected -axis y -angle 90" );
916 Select_RotateAxis( 1, 90 );
919 void Selection_Rotatez(){
920 UndoableCommand undo( "rotateSelected -axis z -angle -90" );
921 Select_RotateAxis( 2,-90 );
923 #include "xywindow.h"
924 void Selection_FlipHorizontally(){
925 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
938 void Selection_FlipVertically(){
939 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
952 void Selection_RotateClockwise(){
953 UndoableCommand undo( "rotateSelected Clockwise 90" );
954 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
958 Select_RotateAxis( 2, -90 );
961 Select_RotateAxis( 1, 90 );
964 Select_RotateAxis( 0, -90 );
969 void Selection_RotateAnticlockwise(){
970 UndoableCommand undo( "rotateSelected Anticlockwise 90" );
971 VIEWTYPE viewtype = GlobalXYWnd_getCurrentViewType();
975 Select_RotateAxis( 2, 90 );
978 Select_RotateAxis( 1, -90 );
981 Select_RotateAxis( 0, 90 );
989 void Select_registerCommands(){
990 GlobalCommands_insert( "ShowHidden", FreeCaller<Select_ShowAllHidden>(), Accelerator( 'H', (GdkModifierType)GDK_SHIFT_MASK ) );
991 GlobalToggles_insert( "HideSelected", FreeCaller<HideSelected>(), ToggleItem::AddCallbackCaller( g_hidden_item ), Accelerator( 'H' ) );
993 GlobalCommands_insert( "MirrorSelectionX", FreeCaller<Selection_Flipx>() );
994 GlobalCommands_insert( "RotateSelectionX", FreeCaller<Selection_Rotatex>() );
995 GlobalCommands_insert( "MirrorSelectionY", FreeCaller<Selection_Flipy>() );
996 GlobalCommands_insert( "RotateSelectionY", FreeCaller<Selection_Rotatey>() );
997 GlobalCommands_insert( "MirrorSelectionZ", FreeCaller<Selection_Flipz>() );
998 GlobalCommands_insert( "RotateSelectionZ", FreeCaller<Selection_Rotatez>() );
1000 GlobalCommands_insert( "MirrorSelectionHorizontally", FreeCaller<Selection_FlipHorizontally>() );
1001 GlobalCommands_insert( "MirrorSelectionVertically", FreeCaller<Selection_FlipVertically>() );
1003 GlobalCommands_insert( "RotateSelectionClockwise", FreeCaller<Selection_RotateClockwise>() );
1004 GlobalCommands_insert( "RotateSelectionAnticlockwise", FreeCaller<Selection_RotateAnticlockwise>() );
1008 void Nudge( int nDim, float fNudge ){
1009 Vector3 translate( 0, 0, 0 );
1010 translate[nDim] = fNudge;
1012 GlobalSelectionSystem().translateSelected( translate );
1015 void Selection_NudgeZ( float amount ){
1016 StringOutputStream command;
1017 command << "nudgeSelected -axis z -amount " << amount;
1018 UndoableCommand undo( command.c_str() );
1023 void Selection_MoveDown(){
1024 Selection_NudgeZ( -GetGridSize() );
1027 void Selection_MoveUp(){
1028 Selection_NudgeZ( GetGridSize() );
1031 void SceneSelectionChange( const Selectable& selectable ){
1032 SceneChangeNotify();
1035 SignalHandlerId Selection_boundsChanged;
1037 void Selection_construct(){
1038 typedef FreeCaller1<const Selectable&, SceneSelectionChange> SceneSelectionChangeCaller;
1039 GlobalSelectionSystem().addSelectionChangeCallback( SceneSelectionChangeCaller() );
1040 typedef FreeCaller1<const Selectable&, UpdateWorkzone_ForSelectionChanged> UpdateWorkzoneForSelectionChangedCaller;
1041 GlobalSelectionSystem().addSelectionChangeCallback( UpdateWorkzoneForSelectionChangedCaller() );
1042 typedef FreeCaller<UpdateWorkzone_ForSelection> UpdateWorkzoneForSelectionCaller;
1043 Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( UpdateWorkzoneForSelectionCaller() );
1046 void Selection_destroy(){
1047 GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
1051 #include "gtkdlgs.h"
1052 #include <gtk/gtkbox.h>
1053 #include <gtk/gtkspinbutton.h>
1054 #include <gtk/gtktable.h>
1055 #include <gtk/gtklabel.h>
1056 #include <gdk/gdkkeysyms.h>
1059 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
1061 return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
1063 return quaternion_multiplied_by_quaternion(
1064 quaternion_multiplied_by_quaternion(
1065 quaternion_for_z( degrees_to_radians( eulerXYZ[2] ) ),
1066 quaternion_for_y( degrees_to_radians( eulerXYZ[1] ) )
1068 quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
1071 double cx = cos( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
1072 double sx = sin( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
1073 double cy = cos( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
1074 double sy = sin( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
1075 double cz = cos( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
1076 double sz = sin( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
1079 cz * cy * sx - sz * sy * cx,
1080 cz * sy * cx + sz * cy * sx,
1081 sz * cy * cx - cz * sy * sx,
1082 cz * cy * cx + sz * sy * sx
1095 static gboolean rotatedlg_apply( GtkWidget *widget, RotateDialog* rotateDialog ){
1098 gtk_spin_button_update ( rotateDialog->x );
1099 gtk_spin_button_update ( rotateDialog->y );
1100 gtk_spin_button_update ( rotateDialog->z );
1101 eulerXYZ[0] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->x ) );
1102 eulerXYZ[1] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->y ) );
1103 eulerXYZ[2] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->z ) );
1105 StringOutputStream command;
1106 command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
1107 UndoableCommand undo( command.c_str() );
1109 GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ), false );
1113 static gboolean rotatedlg_cancel( GtkWidget *widget, RotateDialog* rotateDialog ){
1114 gtk_widget_hide( GTK_WIDGET( rotateDialog->window ) );
1116 gtk_spin_button_set_value( rotateDialog->x, 0.0f ); // reset to 0 on close
1117 gtk_spin_button_set_value( rotateDialog->y, 0.0f );
1118 gtk_spin_button_set_value( rotateDialog->z, 0.0f );
1123 static gboolean rotatedlg_ok( GtkWidget *widget, RotateDialog* rotateDialog ){
1124 rotatedlg_apply( widget, rotateDialog );
1125 // rotatedlg_cancel( widget, rotateDialog );
1126 gtk_widget_hide( GTK_WIDGET( rotateDialog->window ) );
1130 static gboolean rotatedlg_delete( GtkWidget *widget, GdkEventAny *event, RotateDialog* rotateDialog ){
1131 rotatedlg_cancel( widget, rotateDialog );
1135 RotateDialog g_rotate_dialog;
1137 if ( g_rotate_dialog.window == NULL ) {
1138 g_rotate_dialog.window = create_dialog_window( MainFrame_getWindow(), "Arbitrary rotation", G_CALLBACK( rotatedlg_delete ), &g_rotate_dialog );
1140 GtkAccelGroup* accel = gtk_accel_group_new();
1141 gtk_window_add_accel_group( g_rotate_dialog.window, accel );
1144 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1145 gtk_container_add( GTK_CONTAINER( g_rotate_dialog.window ), GTK_WIDGET( hbox ) );
1147 GtkTable* table = create_dialog_table( 3, 2, 4, 4 );
1148 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1150 GtkWidget* label = gtk_label_new( " X " );
1151 gtk_widget_show( label );
1152 gtk_table_attach( table, label, 0, 1, 0, 1,
1153 (GtkAttachOptions) ( 0 ),
1154 (GtkAttachOptions) ( 0 ), 0, 0 );
1157 GtkWidget* label = gtk_label_new( " Y " );
1158 gtk_widget_show( label );
1159 gtk_table_attach( table, label, 0, 1, 1, 2,
1160 (GtkAttachOptions) ( 0 ),
1161 (GtkAttachOptions) ( 0 ), 0, 0 );
1164 GtkWidget* label = gtk_label_new( " Z " );
1165 gtk_widget_show( label );
1166 gtk_table_attach( table, label, 0, 1, 2, 3,
1167 (GtkAttachOptions) ( 0 ),
1168 (GtkAttachOptions) ( 0 ), 0, 0 );
1171 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 0, -359, 359, 1, 10, 0 ) );
1172 GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 0 ) );
1173 gtk_widget_show( GTK_WIDGET( spin ) );
1174 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 0, 1,
1175 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1176 (GtkAttachOptions) ( 0 ), 0, 0 );
1177 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
1178 gtk_spin_button_set_wrap( spin, TRUE );
1180 gtk_widget_grab_focus( GTK_WIDGET( spin ) );
1182 g_rotate_dialog.x = spin;
1185 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 0, -359, 359, 1, 10, 0 ) );
1186 GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 0 ) );
1187 gtk_widget_show( GTK_WIDGET( spin ) );
1188 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 1, 2,
1189 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1190 (GtkAttachOptions) ( 0 ), 0, 0 );
1191 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
1192 gtk_spin_button_set_wrap( spin, TRUE );
1194 g_rotate_dialog.y = spin;
1197 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 0, -359, 359, 1, 10, 0 ) );
1198 GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 0 ) );
1199 gtk_widget_show( GTK_WIDGET( spin ) );
1200 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 2, 3,
1201 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1202 (GtkAttachOptions) ( 0 ), 0, 0 );
1203 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
1204 gtk_spin_button_set_wrap( spin, TRUE );
1206 g_rotate_dialog.z = spin;
1210 GtkVBox* vbox = create_dialog_vbox( 4 );
1211 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1213 GtkButton* button = create_dialog_button( "OK", G_CALLBACK( rotatedlg_ok ), &g_rotate_dialog );
1214 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1215 widget_make_default( GTK_WIDGET( button ) );
1216 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1219 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( rotatedlg_cancel ), &g_rotate_dialog );
1220 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1221 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1224 GtkButton* button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
1225 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1231 gtk_widget_show( GTK_WIDGET( g_rotate_dialog.window ) );
1250 static gboolean scaledlg_apply( GtkWidget *widget, ScaleDialog* scaleDialog ){
1253 sx = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->x ) ) ) );
1254 sy = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->y ) ) ) );
1255 sz = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->z ) ) ) );
1257 StringOutputStream command;
1258 command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1259 UndoableCommand undo( command.c_str() );
1261 Select_Scale( sx, sy, sz );
1266 static gboolean scaledlg_cancel( GtkWidget *widget, ScaleDialog* scaleDialog ){
1267 gtk_widget_hide( GTK_WIDGET( scaleDialog->window ) );
1269 gtk_entry_set_text( GTK_ENTRY( scaleDialog->x ), "1.0" );
1270 gtk_entry_set_text( GTK_ENTRY( scaleDialog->y ), "1.0" );
1271 gtk_entry_set_text( GTK_ENTRY( scaleDialog->z ), "1.0" );
1276 static gboolean scaledlg_ok( GtkWidget *widget, ScaleDialog* scaleDialog ){
1277 scaledlg_apply( widget, scaleDialog );
1278 //scaledlg_cancel( widget, scaleDialog );
1279 gtk_widget_hide( GTK_WIDGET( scaleDialog->window ) );
1283 static gboolean scaledlg_delete( GtkWidget *widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1284 scaledlg_cancel( widget, scaleDialog );
1288 ScaleDialog g_scale_dialog;
1291 if ( g_scale_dialog.window == NULL ) {
1292 g_scale_dialog.window = create_dialog_window( MainFrame_getWindow(), "Arbitrary scale", G_CALLBACK( scaledlg_delete ), &g_scale_dialog );
1294 GtkAccelGroup* accel = gtk_accel_group_new();
1295 gtk_window_add_accel_group( g_scale_dialog.window, accel );
1298 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1299 gtk_container_add( GTK_CONTAINER( g_scale_dialog.window ), GTK_WIDGET( hbox ) );
1301 GtkTable* table = create_dialog_table( 3, 2, 4, 4 );
1302 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1304 GtkWidget* label = gtk_label_new( " X " );
1305 gtk_widget_show( label );
1306 gtk_table_attach( table, label, 0, 1, 0, 1,
1307 (GtkAttachOptions) ( 0 ),
1308 (GtkAttachOptions) ( 0 ), 0, 0 );
1311 GtkWidget* label = gtk_label_new( " Y " );
1312 gtk_widget_show( label );
1313 gtk_table_attach( table, label, 0, 1, 1, 2,
1314 (GtkAttachOptions) ( 0 ),
1315 (GtkAttachOptions) ( 0 ), 0, 0 );
1318 GtkWidget* label = gtk_label_new( " Z " );
1319 gtk_widget_show( label );
1320 gtk_table_attach( table, label, 0, 1, 2, 3,
1321 (GtkAttachOptions) ( 0 ),
1322 (GtkAttachOptions) ( 0 ), 0, 0 );
1325 GtkWidget* entry = gtk_entry_new();
1326 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1327 gtk_widget_show( entry );
1328 gtk_table_attach( table, entry, 1, 2, 0, 1,
1329 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1330 (GtkAttachOptions) ( 0 ), 0, 0 );
1332 g_scale_dialog.x = entry;
1335 GtkWidget* entry = gtk_entry_new();
1336 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1337 gtk_widget_show( entry );
1338 gtk_table_attach( table, entry, 1, 2, 1, 2,
1339 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1340 (GtkAttachOptions) ( 0 ), 0, 0 );
1342 g_scale_dialog.y = entry;
1345 GtkWidget* entry = gtk_entry_new();
1346 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1347 gtk_widget_show( entry );
1348 gtk_table_attach( table, entry, 1, 2, 2, 3,
1349 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1350 (GtkAttachOptions) ( 0 ), 0, 0 );
1352 g_scale_dialog.z = entry;
1356 GtkVBox* vbox = create_dialog_vbox( 4 );
1357 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1359 GtkButton* button = create_dialog_button( "OK", G_CALLBACK( scaledlg_ok ), &g_scale_dialog );
1360 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1361 widget_make_default( GTK_WIDGET( button ) );
1362 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1365 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( scaledlg_cancel ), &g_scale_dialog );
1366 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1367 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1370 GtkButton* button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1371 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1377 gtk_widget_show( GTK_WIDGET( g_scale_dialog.window ) );