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"
44 #include "patchmanip.h"
45 #include "patchdialog.h"
46 #include "selection.h"
47 #include "texwindow.h"
49 #include "mainframe.h"
52 #include "entityinspector.h"
56 select_workzone_t g_select_workzone;
60 Loops over all selected brushes and stores their
61 world AABBs in the specified array.
63 class CollectSelectedBrushesBounds : public SelectionSystem::Visitor
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
70 CollectSelectedBrushesBounds( AABB* bounds, Unsigned max, Unsigned& count )
77 void visit( scene::Instance& instance ) const {
78 ASSERT_MESSAGE( m_count <= m_max, "Invalid m_count in CollectSelectedBrushesBounds" );
80 // stop if the array is already full
81 if ( m_count == m_max ) {
85 Selectable* selectable = Instance_getSelectable( instance );
86 if ( ( selectable != 0 )
87 && instance.isSelected() ) {
89 if ( Instance_getBrush( instance ) != 0 ) {
90 m_bounds[m_count] = instance.worldAABB();
98 Selects all objects that intersect one of the bounding AABBs.
99 The exact intersection-method is specified through TSelectionPolicy
101 template<class TSelectionPolicy>
102 class SelectByBounds : public scene::Graph::Walker
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
109 SelectByBounds( AABB* aabbs, Unsigned count )
114 bool pre( const scene::Path& path, scene::Instance& instance ) const {
115 if( path.top().get().visible() ){
116 Selectable* selectable = Instance_getSelectable( instance );
119 Entity* entity = Node_getEntity( path.top() );
121 if ( string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ) {
126 if ( ( path.size() > 1 ) &&
127 ( !path.top().get().isRoot() ) &&
128 ( selectable != 0 ) &&
129 ( !node_is_group( path.top() ) )
131 for ( Unsigned i = 0; i < m_count; ++i )
133 if ( policy.Evaluate( m_aabbs[i], instance ) ) {
134 selectable->setSelected( true );
147 Performs selection operation on the global scenegraph.
148 If delete_bounds_src is true, then the objects which were
149 used as source for the selection aabbs will be deleted.
151 static void DoSelection( bool delete_bounds_src = true ){
152 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
153 // we may not need all AABBs since not all selected objects have to be brushes
154 const Unsigned max = (Unsigned)GlobalSelectionSystem().countSelected();
155 AABB* aabbs = new AABB[max];
158 CollectSelectedBrushesBounds collector( aabbs, max, count );
159 GlobalSelectionSystem().foreachSelected( collector );
161 // nothing usable in selection
167 // delete selected objects
168 if ( delete_bounds_src ) { // see deleteSelection
169 UndoableCommand undo( "deleteSelected" );
173 // select objects with bounds
174 GlobalSceneGraph().traverse( SelectByBounds<TSelectionPolicy>( aabbs, count ) );
183 SelectionPolicy for SelectByBounds
184 Returns true if box and the AABB of instance intersect
186 class SelectionPolicy_Touching
189 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
190 const AABB& other( instance.worldAABB() );
191 for ( Unsigned i = 0; i < 3; ++i )
193 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] + other.extents[i] ) ) {
202 SelectionPolicy for SelectByBounds
203 Returns true if the AABB of instance is inside box
205 class SelectionPolicy_Inside
208 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
209 const AABB& other( instance.worldAABB() );
210 for ( Unsigned i = 0; i < 3; ++i )
212 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] - other.extents[i] ) ) {
220 class DeleteSelected : public scene::Graph::Walker
222 mutable bool m_remove;
223 mutable bool m_removedChild;
226 : m_remove( false ), m_removedChild( false ){
228 bool pre( const scene::Path& path, scene::Instance& instance ) const {
229 m_removedChild = false;
231 Selectable* selectable = Instance_getSelectable( instance );
233 && selectable->isSelected()
235 && !path.top().get().isRoot() ) {
238 return false; // dont traverse into child elements
242 void post( const scene::Path& path, scene::Instance& instance ) const {
244 if ( m_removedChild ) {
245 m_removedChild = false;
247 // delete empty entities
248 Entity* entity = Node_getEntity( path.top() );
250 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
251 && Node_getTraversable( path.top() )->empty() ) {
252 Path_deleteTop( path );
256 // node should be removed
258 if ( Node_isEntity( path.parent() ) != 0 ) {
259 m_removedChild = true;
263 Path_deleteTop( path );
268 void Scene_DeleteSelected( scene::Graph& graph ){
269 graph.traverse( DeleteSelected() );
273 void Select_Delete( void ){
274 Scene_DeleteSelected( GlobalSceneGraph() );
277 class InvertSelectionWalker : public scene::Graph::Walker
279 SelectionSystem::EMode m_mode;
280 mutable Selectable* m_selectable;
282 InvertSelectionWalker( SelectionSystem::EMode mode )
283 : m_mode( mode ), m_selectable( 0 ){
285 bool pre( const scene::Path& path, scene::Instance& instance ) const {
286 if( !path.top().get().visible() ){
290 Selectable* selectable = Instance_getSelectable( instance );
294 case SelectionSystem::eEntity:
295 if ( Node_isEntity( path.top() ) != 0 ) {
296 m_selectable = path.top().get().visible() ? selectable : 0;
299 case SelectionSystem::ePrimitive:
300 m_selectable = path.top().get().visible() ? selectable : 0;
302 case SelectionSystem::eComponent:
308 void post( const scene::Path& path, scene::Instance& instance ) const {
309 if ( m_selectable != 0 ) {
310 m_selectable->setSelected( !m_selectable->isSelected() );
316 void Scene_Invert_Selection( scene::Graph& graph ){
317 graph.traverse( InvertSelectionWalker( GlobalSelectionSystem().Mode() ) );
320 void Select_Invert(){
321 Scene_Invert_Selection( GlobalSceneGraph() );
324 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
326 mutable std::size_t m_depth;
327 NodeSmartReference worldspawn;
329 ExpandSelectionToEntitiesWalker() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
331 bool pre( const scene::Path& path, scene::Instance& instance ) const {
335 NodeSmartReference me( path.top().get() );
336 if ( me == worldspawn ) {
340 if ( m_depth == 2 ) { // entity depth
341 // traverse and select children if any one is selected
342 if ( instance.childSelected() ) {
343 Instance_setSelected( instance, true );
345 return Node_getEntity( path.top() )->isContainer() && instance.isSelected();
347 else if ( m_depth == 3 ) { // primitive depth
348 Instance_setSelected( instance, true );
353 void post( const scene::Path& path, scene::Instance& instance ) const {
358 void Scene_ExpandSelectionToEntities(){
359 GlobalSceneGraph().traverse( ExpandSelectionToEntitiesWalker() );
365 void Selection_UpdateWorkzone(){
366 if ( GlobalSelectionSystem().countSelected() != 0 ) {
367 Select_GetBounds( g_select_workzone.d_work_min, g_select_workzone.d_work_max );
370 typedef FreeCaller<void(), Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
372 IdleDraw g_idleWorkzone = IdleDraw( SelectionUpdateWorkzoneCaller() );
375 const select_workzone_t& Select_getWorkZone(){
376 g_idleWorkzone.flush();
377 return g_select_workzone;
380 void UpdateWorkzone_ForSelection(){
381 g_idleWorkzone.queueDraw();
384 // update the workzone to the current selection
385 void UpdateWorkzone_ForSelectionChanged( const Selectable& selectable ){
386 if ( selectable.isSelected() ) {
387 UpdateWorkzone_ForSelection();
391 void Select_SetShader( const char* shader ){
392 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
393 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
394 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
396 Scene_BrushSetShader_Component_Selected( GlobalSceneGraph(), shader );
399 void Select_SetTexdef( const TextureProjection& projection ){
400 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
401 Scene_BrushSetTexdef_Selected( GlobalSceneGraph(), projection );
403 Scene_BrushSetTexdef_Component_Selected( GlobalSceneGraph(), projection );
406 void Select_SetFlags( const ContentsFlagsValue& flags ){
407 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
408 Scene_BrushSetFlags_Selected( GlobalSceneGraph(), flags );
410 Scene_BrushSetFlags_Component_Selected( GlobalSceneGraph(), flags );
413 void Select_GetBounds( Vector3& mins, Vector3& maxs ){
415 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
416 maxs = vector3_added( bounds.origin, bounds.extents );
417 mins = vector3_subtracted( bounds.origin, bounds.extents );
420 void Select_GetMid( Vector3& mid ){
422 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
423 mid = vector3_snapped( bounds.origin );
427 void Select_FlipAxis( int axis ){
428 Vector3 flip( 1, 1, 1 );
430 GlobalSelectionSystem().scaleSelected( flip );
434 void Select_Scale( float x, float y, float z ){
435 GlobalSelectionSystem().scaleSelected( Vector3( x, y, z ) );
451 inline Matrix4 matrix4_rotation_for_axis90( axis_t axis, sign_t sign ){
455 if ( sign == eSignPositive ) {
456 return matrix4_rotation_for_sincos_x( 1, 0 );
460 return matrix4_rotation_for_sincos_x( -1, 0 );
463 if ( sign == eSignPositive ) {
464 return matrix4_rotation_for_sincos_y( 1, 0 );
468 return matrix4_rotation_for_sincos_y( -1, 0 );
470 default: //case eAxisZ:
471 if ( sign == eSignPositive ) {
472 return matrix4_rotation_for_sincos_z( 1, 0 );
476 return matrix4_rotation_for_sincos_z( -1, 0 );
481 inline void matrix4_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign ){
482 matrix4_multiply_by_matrix4( matrix, matrix4_rotation_for_axis90( axis, sign ) );
485 inline void matrix4_pivoted_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign, const Vector3& pivotpoint ){
486 matrix4_translate_by_vec3( matrix, pivotpoint );
487 matrix4_rotate_by_axis90( matrix, axis, sign );
488 matrix4_translate_by_vec3( matrix, vector3_negated( pivotpoint ) );
491 inline Quaternion quaternion_for_axis90( axis_t axis, sign_t sign ){
496 if ( sign == eSignPositive ) {
497 return Quaternion( c_half_sqrt2f, 0, 0, c_half_sqrt2f );
501 return Quaternion( -c_half_sqrt2f, 0, 0, -c_half_sqrt2f );
504 if ( sign == eSignPositive ) {
505 return Quaternion( 0, c_half_sqrt2f, 0, c_half_sqrt2f );
509 return Quaternion( 0, -c_half_sqrt2f, 0, -c_half_sqrt2f );
511 default: //case eAxisZ:
512 if ( sign == eSignPositive ) {
513 return Quaternion( 0, 0, c_half_sqrt2f, c_half_sqrt2f );
517 return Quaternion( 0, 0, -c_half_sqrt2f, -c_half_sqrt2f );
521 quaternion_for_matrix4_rotation( matrix4_rotation_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
525 void Select_RotateAxis( int axis, float deg ){
526 if ( fabs( deg ) == 90.f ) {
527 GlobalSelectionSystem().rotateSelected( quaternion_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
534 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_x_degrees( deg ) ) );
537 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_y_degrees( deg ) ) );
540 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_z_degrees( deg ) ) );
547 void Select_ShiftTexture( float x, float y ){
548 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
549 Scene_BrushShiftTexdef_Selected( GlobalSceneGraph(), x, y );
550 Scene_PatchTranslateTexture_Selected( GlobalSceneGraph(), x, y );
552 //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
553 Scene_BrushShiftTexdef_Component_Selected( GlobalSceneGraph(), x, y );
556 void Select_ScaleTexture( float x, float y ){
557 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
558 Scene_BrushScaleTexdef_Selected( GlobalSceneGraph(), x, y );
559 Scene_PatchScaleTexture_Selected( GlobalSceneGraph(), x, y );
561 Scene_BrushScaleTexdef_Component_Selected( GlobalSceneGraph(), x, y );
564 void Select_RotateTexture( float amt ){
565 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
566 Scene_BrushRotateTexdef_Selected( GlobalSceneGraph(), amt );
567 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), amt );
569 Scene_BrushRotateTexdef_Component_Selected( GlobalSceneGraph(), amt );
572 // TTimo modified to handle shader architecture:
573 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
574 void FindReplaceTextures( const char* pFind, const char* pReplace, bool bSelected ){
575 if ( !texdef_name_valid( pFind ) ) {
576 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pFind << "', aborted\n";
579 if ( !texdef_name_valid( pReplace ) ) {
580 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
584 StringOutputStream command;
585 command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
586 UndoableCommand undo( command.c_str() );
589 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
590 Scene_BrushFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
591 Scene_PatchFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
593 Scene_BrushFindReplaceShader_Component_Selected( GlobalSceneGraph(), pFind, pReplace );
597 Scene_BrushFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
598 Scene_PatchFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
602 typedef std::vector<const char*> PropertyValues;
604 bool propertyvalues_contain( const PropertyValues& propertyvalues, const char *str ){
605 for ( PropertyValues::const_iterator i = propertyvalues.begin(); i != propertyvalues.end(); ++i )
607 if ( string_equal( str, *i ) ) {
614 class EntityFindByPropertyValueWalker : public scene::Graph::Walker
616 const PropertyValues& m_propertyvalues;
619 EntityFindByPropertyValueWalker( const char *prop, const PropertyValues& propertyvalues )
620 : m_propertyvalues( propertyvalues ), m_prop( prop ){
622 bool pre( const scene::Path& path, scene::Instance& instance ) const {
623 if( !path.top().get().visible() ){
626 Entity* entity = Node_getEntity( path.top() );
628 && propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
629 Instance_getSelectable( instance )->setSelected( true );
635 void Scene_EntitySelectByPropertyValues( scene::Graph& graph, const char *prop, const PropertyValues& propertyvalues ){
636 graph.traverse( EntityFindByPropertyValueWalker( prop, propertyvalues ) );
639 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
641 PropertyValues& m_propertyvalues;
644 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
645 : m_propertyvalues( propertyvalues ), m_prop( prop ){
647 bool pre( const scene::Path& path, scene::Instance& instance ) const {
648 Selectable* selectable = Instance_getSelectable( instance );
650 && selectable->isSelected() ) {
651 Entity* entity = Node_getEntity( path.top() );
653 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
654 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
662 void Scene_EntityGetPropertyValues( scene::Graph& graph, const char *prop, PropertyValues& propertyvalues ){
663 graph.traverse( EntityGetSelectedPropertyValuesWalker( prop, propertyvalues ) );
666 void Select_AllOfType(){
667 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
668 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
669 GlobalSelectionSystem().setSelectedAllComponents( false );
670 Scene_BrushSelectByShader_Component( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
675 PropertyValues propertyvalues;
676 const char *prop = EntityInspector_getCurrentKey();
677 if ( !prop || !*prop ) {
680 Scene_EntityGetPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
681 GlobalSelectionSystem().setSelectedAll( false );
682 if ( !propertyvalues.empty() ) {
683 Scene_EntitySelectByPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
687 Scene_BrushSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
688 Scene_PatchSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
693 void Select_Inside( void ){
694 SelectByBounds<SelectionPolicy_Inside>::DoSelection();
697 void Select_Touching( void ){
698 SelectByBounds<SelectionPolicy_Touching>::DoSelection( false );
701 void Select_FitTexture( float horizontal, float vertical ){
702 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
703 Scene_BrushFitTexture_Selected( GlobalSceneGraph(), horizontal, vertical );
705 Scene_BrushFitTexture_Component_Selected( GlobalSceneGraph(), horizontal, vertical );
711 #include "commands.h"
714 inline void hide_node( scene::Node& node, bool hide ){
716 ? node.enable( scene::Node::eHidden )
717 : node.disable( scene::Node::eHidden );
720 bool g_nodes_be_hidden = false;
722 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_hidden_caller( g_nodes_be_hidden );
724 ToggleItem g_hidden_item( g_hidden_caller );
726 class HideSelectedWalker : public scene::Graph::Walker
730 HideSelectedWalker( bool hide )
733 bool pre( const scene::Path& path, scene::Instance& instance ) const {
734 Selectable* selectable = Instance_getSelectable( instance );
736 && selectable->isSelected() ) {
737 g_nodes_be_hidden = m_hide;
738 hide_node( path.top(), m_hide );
744 void Scene_Hide_Selected( bool hide ){
745 GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
749 Scene_Hide_Selected( true );
755 GlobalSelectionSystem().setSelectedAll( false );
756 g_hidden_item.update();
760 class HideAllWalker : public scene::Graph::Walker
764 HideAllWalker( bool hide )
767 bool pre( const scene::Path& path, scene::Instance& instance ) const {
768 hide_node( path.top(), m_hide );
773 void Scene_Hide_All( bool hide ){
774 GlobalSceneGraph().traverse( HideAllWalker( hide ) );
777 void Select_ShowAllHidden(){
778 Scene_Hide_All( false );
780 g_nodes_be_hidden = false;
781 g_hidden_item.update();
784 void Hide_registerCommands(){
785 GlobalCommands_insert( "ShowHidden", makeCallbackF( Select_ShowAllHidden ), Accelerator( 'H', (GdkModifierType)GDK_SHIFT_MASK ) );
786 GlobalToggles_insert( "HideSelected", makeCallbackF( HideSelected ), ToggleItem::AddCallbackCaller( g_hidden_item ), Accelerator( 'H' ) );
791 void Selection_Flipx(){
792 UndoableCommand undo( "mirrorSelected -axis x" );
793 Select_FlipAxis( 0 );
796 void Selection_Flipy(){
797 UndoableCommand undo( "mirrorSelected -axis y" );
798 Select_FlipAxis( 1 );
801 void Selection_Flipz(){
802 UndoableCommand undo( "mirrorSelected -axis z" );
803 Select_FlipAxis( 2 );
806 void Selection_Rotatex(){
807 UndoableCommand undo( "rotateSelected -axis x -angle -90" );
808 Select_RotateAxis( 0,-90 );
811 void Selection_Rotatey(){
812 UndoableCommand undo( "rotateSelected -axis y -angle 90" );
813 Select_RotateAxis( 1, 90 );
816 void Selection_Rotatez(){
817 UndoableCommand undo( "rotateSelected -axis z -angle -90" );
818 Select_RotateAxis( 2,-90 );
823 void Nudge( int nDim, float fNudge ){
824 Vector3 translate( 0, 0, 0 );
825 translate[nDim] = fNudge;
827 GlobalSelectionSystem().translateSelected( translate );
830 void Selection_NudgeZ( float amount ){
831 StringOutputStream command;
832 command << "nudgeSelected -axis z -amount " << amount;
833 UndoableCommand undo( command.c_str() );
838 void Selection_MoveDown(){
839 Selection_NudgeZ( -GetGridSize() );
842 void Selection_MoveUp(){
843 Selection_NudgeZ( GetGridSize() );
846 void SceneSelectionChange( const Selectable& selectable ){
850 SignalHandlerId Selection_boundsChanged;
852 void Selection_construct(){
853 typedef FreeCaller<void(const Selectable&), SceneSelectionChange> SceneSelectionChangeCaller;
854 GlobalSelectionSystem().addSelectionChangeCallback( SceneSelectionChangeCaller() );
855 typedef FreeCaller<void(const Selectable&), UpdateWorkzone_ForSelectionChanged> UpdateWorkzoneForSelectionChangedCaller;
856 GlobalSelectionSystem().addSelectionChangeCallback( UpdateWorkzoneForSelectionChangedCaller() );
857 typedef FreeCaller<void(), UpdateWorkzone_ForSelection> UpdateWorkzoneForSelectionCaller;
858 Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( UpdateWorkzoneForSelectionCaller() );
861 void Selection_destroy(){
862 GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
867 #include <gdk/gdkkeysyms.h>
870 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
872 return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
874 return quaternion_multiplied_by_quaternion(
875 quaternion_multiplied_by_quaternion(
876 quaternion_for_z( degrees_to_radians( eulerXYZ[2] ) ),
877 quaternion_for_y( degrees_to_radians( eulerXYZ[1] ) )
879 quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
882 double cx = cos( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
883 double sx = sin( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
884 double cy = cos( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
885 double sy = sin( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
886 double cz = cos( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
887 double sz = sin( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
890 cz * cy * sx - sz * sy * cx,
891 cz * sy * cx + sz * cy * sx,
892 sz * cy * cx - cz * sy * sx,
893 cz * cy * cx + sz * sy * sx
900 ui::SpinButton x{ui::null};
901 ui::SpinButton y{ui::null};
902 ui::SpinButton z{ui::null};
903 ui::Window window{ui::null};
906 static gboolean rotatedlg_apply( ui::Widget widget, RotateDialog* rotateDialog ){
909 gtk_spin_button_update ( rotateDialog->x );
910 gtk_spin_button_update ( rotateDialog->y );
911 gtk_spin_button_update ( rotateDialog->z );
912 eulerXYZ[0] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->x ) );
913 eulerXYZ[1] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->y ) );
914 eulerXYZ[2] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->z ) );
916 StringOutputStream command;
917 command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
918 UndoableCommand undo( command.c_str() );
920 GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ) );
924 static gboolean rotatedlg_cancel( ui::Widget widget, RotateDialog* rotateDialog ){
925 rotateDialog->window.hide();
927 gtk_spin_button_set_value( rotateDialog->x, 0.0f ); // reset to 0 on close
928 gtk_spin_button_set_value( rotateDialog->y, 0.0f );
929 gtk_spin_button_set_value( rotateDialog->z, 0.0f );
934 static gboolean rotatedlg_ok( ui::Widget widget, RotateDialog* rotateDialog ){
935 rotatedlg_apply( widget, rotateDialog );
936 // rotatedlg_cancel( widget, rotateDialog );
937 rotateDialog->window.hide();
941 static gboolean rotatedlg_delete( ui::Widget widget, GdkEventAny *event, RotateDialog* rotateDialog ){
942 rotatedlg_cancel( widget, rotateDialog );
946 RotateDialog g_rotate_dialog;
948 if ( !g_rotate_dialog.window ) {
949 g_rotate_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary rotation", G_CALLBACK(rotatedlg_delete ), &g_rotate_dialog );
951 auto accel = ui::AccelGroup(ui::New);
952 g_rotate_dialog.window.add_accel_group( accel );
955 auto hbox = create_dialog_hbox( 4, 4 );
956 g_rotate_dialog.window.add(hbox);
958 auto table = create_dialog_table( 3, 2, 4, 4 );
959 hbox.pack_start( table, TRUE, TRUE, 0 );
961 ui::Widget label = ui::Label( " X " );
963 table.attach(label, {0, 1, 0, 1}, {0, 0});
966 ui::Widget label = ui::Label( " Y " );
968 table.attach(label, {0, 1, 1, 2}, {0, 0});
971 ui::Widget label = ui::Label( " Z " );
973 table.attach(label, {0, 1, 2, 3}, {0, 0});
976 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
977 auto spin = ui::SpinButton( adj, 1, 0 );
979 table.attach(spin, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
980 spin.dimensions(64, -1);
981 gtk_spin_button_set_wrap( spin, TRUE );
983 gtk_widget_grab_focus( spin );
985 g_rotate_dialog.x = spin;
988 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
989 auto spin = ui::SpinButton( adj, 1, 0 );
991 table.attach(spin, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
992 spin.dimensions(64, -1);
993 gtk_spin_button_set_wrap( spin, TRUE );
995 g_rotate_dialog.y = spin;
998 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
999 auto spin = ui::SpinButton( adj, 1, 0 );
1001 table.attach(spin, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1002 spin.dimensions(64, -1);
1003 gtk_spin_button_set_wrap( spin, TRUE );
1005 g_rotate_dialog.z = spin;
1009 auto vbox = create_dialog_vbox( 4 );
1010 hbox.pack_start( vbox, TRUE, TRUE, 0 );
1012 auto button = create_dialog_button( "OK", G_CALLBACK( rotatedlg_ok ), &g_rotate_dialog );
1013 vbox.pack_start( button, FALSE, FALSE, 0 );
1014 widget_make_default( button );
1015 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1018 auto button = create_dialog_button( "Cancel", G_CALLBACK( rotatedlg_cancel ), &g_rotate_dialog );
1019 vbox.pack_start( button, FALSE, FALSE, 0 );
1020 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1023 auto button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
1024 vbox.pack_start( button, FALSE, FALSE, 0 );
1030 g_rotate_dialog.window.show();
1043 ui::Entry x{ui::null};
1044 ui::Entry y{ui::null};
1045 ui::Entry z{ui::null};
1046 ui::Window window{ui::null};
1049 static gboolean scaledlg_apply( ui::Widget widget, ScaleDialog* scaleDialog ){
1052 sx = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->x ) ) ) );
1053 sy = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->y ) ) ) );
1054 sz = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->z ) ) ) );
1056 StringOutputStream command;
1057 command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1058 UndoableCommand undo( command.c_str() );
1060 Select_Scale( sx, sy, sz );
1065 static gboolean scaledlg_cancel( ui::Widget widget, ScaleDialog* scaleDialog ){
1066 scaleDialog->window.hide();
1068 scaleDialog->x.text("1.0");
1069 scaleDialog->y.text("1.0");
1070 scaleDialog->z.text("1.0");
1075 static gboolean scaledlg_ok( ui::Widget widget, ScaleDialog* scaleDialog ){
1076 scaledlg_apply( widget, scaleDialog );
1077 //scaledlg_cancel( widget, scaleDialog );
1078 scaleDialog->window.hide();
1082 static gboolean scaledlg_delete( ui::Widget widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1083 scaledlg_cancel( widget, scaleDialog );
1087 ScaleDialog g_scale_dialog;
1090 if ( !g_scale_dialog.window ) {
1091 g_scale_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary scale", G_CALLBACK(scaledlg_delete ), &g_scale_dialog );
1093 auto accel = ui::AccelGroup(ui::New);
1094 g_scale_dialog.window.add_accel_group( accel );
1097 auto hbox = create_dialog_hbox( 4, 4 );
1098 g_scale_dialog.window.add(hbox);
1100 auto table = create_dialog_table( 3, 2, 4, 4 );
1101 hbox.pack_start( table, TRUE, TRUE, 0 );
1103 ui::Widget label = ui::Label( " X " );
1105 table.attach(label, {0, 1, 0, 1}, {0, 0});
1108 ui::Widget label = ui::Label( " Y " );
1110 table.attach(label, {0, 1, 1, 2}, {0, 0});
1113 ui::Widget label = ui::Label( " Z " );
1115 table.attach(label, {0, 1, 2, 3}, {0, 0});
1118 auto entry = ui::Entry(ui::New);
1121 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1123 g_scale_dialog.x = entry;
1126 auto entry = ui::Entry(ui::New);
1129 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1131 g_scale_dialog.y = entry;
1134 auto entry = ui::Entry(ui::New);
1137 table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1139 g_scale_dialog.z = entry;
1143 auto vbox = create_dialog_vbox( 4 );
1144 hbox.pack_start( vbox, TRUE, TRUE, 0 );
1146 auto button = create_dialog_button( "OK", G_CALLBACK( scaledlg_ok ), &g_scale_dialog );
1147 vbox.pack_start( button, FALSE, FALSE, 0 );
1148 widget_make_default( button );
1149 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1152 auto button = create_dialog_button( "Cancel", G_CALLBACK( scaledlg_cancel ), &g_scale_dialog );
1153 vbox.pack_start( button, FALSE, FALSE, 0 );
1154 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1157 auto button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1158 vbox.pack_start( button, FALSE, FALSE, 0 );
1164 g_scale_dialog.window.show();