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 Selectable* selectable = Instance_getSelectable( instance );
116 Entity* entity = Node_getEntity( path.top() );
118 if ( string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ) {
123 if ( ( path.size() > 1 ) &&
124 ( !path.top().get().isRoot() ) &&
127 for ( Unsigned i = 0; i < m_count; ++i )
129 if ( policy.Evaluate( m_aabbs[i], instance ) ) {
130 selectable->setSelected( true );
139 Performs selection operation on the global scenegraph.
140 If delete_bounds_src is true, then the objects which were
141 used as source for the selection aabbs will be deleted.
143 static void DoSelection( bool delete_bounds_src = true ){
144 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
145 // we may not need all AABBs since not all selected objects have to be brushes
146 const Unsigned max = (Unsigned)GlobalSelectionSystem().countSelected();
147 AABB* aabbs = new AABB[max];
150 CollectSelectedBrushesBounds collector( aabbs, max, count );
151 GlobalSelectionSystem().foreachSelected( collector );
153 // nothing usable in selection
159 // delete selected objects
160 if ( delete_bounds_src ) { // see deleteSelection
161 UndoableCommand undo( "deleteSelected" );
165 // select objects with bounds
166 GlobalSceneGraph().traverse( SelectByBounds<TSelectionPolicy>( aabbs, count ) );
175 SelectionPolicy for SelectByBounds
176 Returns true if box and the AABB of instance intersect
178 class SelectionPolicy_Touching
181 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
182 const AABB& other( instance.worldAABB() );
183 for ( Unsigned i = 0; i < 3; ++i )
185 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] + other.extents[i] ) ) {
194 SelectionPolicy for SelectByBounds
195 Returns true if the AABB of instance is inside box
197 class SelectionPolicy_Inside
200 bool Evaluate( const AABB& box, scene::Instance& instance ) const {
201 const AABB& other( instance.worldAABB() );
202 for ( Unsigned i = 0; i < 3; ++i )
204 if ( fabsf( box.origin[i] - other.origin[i] ) > ( box.extents[i] - other.extents[i] ) ) {
212 class DeleteSelected : public scene::Graph::Walker
214 mutable bool m_remove;
215 mutable bool m_removedChild;
218 : m_remove( false ), m_removedChild( false ){
220 bool pre( const scene::Path& path, scene::Instance& instance ) const {
221 m_removedChild = false;
223 Selectable* selectable = Instance_getSelectable( instance );
225 && selectable->isSelected()
227 && !path.top().get().isRoot() ) {
230 return false; // dont traverse into child elements
234 void post( const scene::Path& path, scene::Instance& instance ) const {
236 if ( m_removedChild ) {
237 m_removedChild = false;
239 // delete empty entities
240 Entity* entity = Node_getEntity( path.top() );
242 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
243 && Node_getTraversable( path.top() )->empty() ) {
244 Path_deleteTop( path );
248 // node should be removed
250 if ( Node_isEntity( path.parent() ) != 0 ) {
251 m_removedChild = true;
255 Path_deleteTop( path );
260 void Scene_DeleteSelected( scene::Graph& graph ){
261 graph.traverse( DeleteSelected() );
265 void Select_Delete( void ){
266 Scene_DeleteSelected( GlobalSceneGraph() );
269 class InvertSelectionWalker : public scene::Graph::Walker
271 SelectionSystem::EMode m_mode;
272 mutable Selectable* m_selectable;
274 InvertSelectionWalker( SelectionSystem::EMode mode )
275 : m_mode( mode ), m_selectable( 0 ){
277 bool pre( const scene::Path& path, scene::Instance& instance ) const {
278 Selectable* selectable = Instance_getSelectable( instance );
282 case SelectionSystem::eEntity:
283 if ( Node_isEntity( path.top() ) != 0 ) {
284 m_selectable = path.top().get().visible() ? selectable : 0;
287 case SelectionSystem::ePrimitive:
288 m_selectable = path.top().get().visible() ? selectable : 0;
290 case SelectionSystem::eComponent:
296 void post( const scene::Path& path, scene::Instance& instance ) const {
297 if ( m_selectable != 0 ) {
298 m_selectable->setSelected( !m_selectable->isSelected() );
304 void Scene_Invert_Selection( scene::Graph& graph ){
305 graph.traverse( InvertSelectionWalker( GlobalSelectionSystem().Mode() ) );
308 void Select_Invert(){
309 Scene_Invert_Selection( GlobalSceneGraph() );
312 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
314 mutable std::size_t m_depth;
315 NodeSmartReference worldspawn;
317 ExpandSelectionToEntitiesWalker() : m_depth( 0 ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
319 bool pre( const scene::Path& path, scene::Instance& instance ) const {
323 NodeSmartReference me( path.top().get() );
324 if ( me == worldspawn ) {
328 if ( m_depth == 2 ) { // entity depth
329 // traverse and select children if any one is selected
330 if ( instance.childSelected() ) {
331 Instance_setSelected( instance, true );
333 return Node_getEntity( path.top() )->isContainer() && instance.isSelected();
335 else if ( m_depth == 3 ) { // primitive depth
336 Instance_setSelected( instance, true );
341 void post( const scene::Path& path, scene::Instance& instance ) const {
346 void Scene_ExpandSelectionToEntities(){
347 GlobalSceneGraph().traverse( ExpandSelectionToEntitiesWalker() );
353 void Selection_UpdateWorkzone(){
354 if ( GlobalSelectionSystem().countSelected() != 0 ) {
355 Select_GetBounds( g_select_workzone.d_work_min, g_select_workzone.d_work_max );
358 typedef FreeCaller<Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
360 IdleDraw g_idleWorkzone = IdleDraw( SelectionUpdateWorkzoneCaller() );
363 const select_workzone_t& Select_getWorkZone(){
364 g_idleWorkzone.flush();
365 return g_select_workzone;
368 void UpdateWorkzone_ForSelection(){
369 g_idleWorkzone.queueDraw();
372 // update the workzone to the current selection
373 void UpdateWorkzone_ForSelectionChanged( const Selectable& selectable ){
374 if ( selectable.isSelected() ) {
375 UpdateWorkzone_ForSelection();
379 void Select_SetShader( const char* shader ){
380 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
381 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
382 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
384 Scene_BrushSetShader_Component_Selected( GlobalSceneGraph(), shader );
387 void Select_SetTexdef( const TextureProjection& projection ){
388 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
389 Scene_BrushSetTexdef_Selected( GlobalSceneGraph(), projection );
391 Scene_BrushSetTexdef_Component_Selected( GlobalSceneGraph(), projection );
394 void Select_SetFlags( const ContentsFlagsValue& flags ){
395 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
396 Scene_BrushSetFlags_Selected( GlobalSceneGraph(), flags );
398 Scene_BrushSetFlags_Component_Selected( GlobalSceneGraph(), flags );
401 void Select_GetBounds( Vector3& mins, Vector3& maxs ){
403 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
404 maxs = vector3_added( bounds.origin, bounds.extents );
405 mins = vector3_subtracted( bounds.origin, bounds.extents );
408 void Select_GetMid( Vector3& mid ){
410 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
411 mid = vector3_snapped( bounds.origin );
415 void Select_FlipAxis( int axis ){
416 Vector3 flip( 1, 1, 1 );
418 GlobalSelectionSystem().scaleSelected( flip );
422 void Select_Scale( float x, float y, float z ){
423 GlobalSelectionSystem().scaleSelected( Vector3( x, y, z ) );
439 inline Matrix4 matrix4_rotation_for_axis90( axis_t axis, sign_t sign ){
443 if ( sign == eSignPositive ) {
444 return matrix4_rotation_for_sincos_x( 1, 0 );
448 return matrix4_rotation_for_sincos_x( -1, 0 );
451 if ( sign == eSignPositive ) {
452 return matrix4_rotation_for_sincos_y( 1, 0 );
456 return matrix4_rotation_for_sincos_y( -1, 0 );
458 default: //case eAxisZ:
459 if ( sign == eSignPositive ) {
460 return matrix4_rotation_for_sincos_z( 1, 0 );
464 return matrix4_rotation_for_sincos_z( -1, 0 );
469 inline void matrix4_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign ){
470 matrix4_multiply_by_matrix4( matrix, matrix4_rotation_for_axis90( axis, sign ) );
473 inline void matrix4_pivoted_rotate_by_axis90( Matrix4& matrix, axis_t axis, sign_t sign, const Vector3& pivotpoint ){
474 matrix4_translate_by_vec3( matrix, pivotpoint );
475 matrix4_rotate_by_axis90( matrix, axis, sign );
476 matrix4_translate_by_vec3( matrix, vector3_negated( pivotpoint ) );
479 inline Quaternion quaternion_for_axis90( axis_t axis, sign_t sign ){
484 if ( sign == eSignPositive ) {
485 return Quaternion( c_half_sqrt2f, 0, 0, c_half_sqrt2f );
489 return Quaternion( -c_half_sqrt2f, 0, 0, -c_half_sqrt2f );
492 if ( sign == eSignPositive ) {
493 return Quaternion( 0, c_half_sqrt2f, 0, c_half_sqrt2f );
497 return Quaternion( 0, -c_half_sqrt2f, 0, -c_half_sqrt2f );
499 default: //case eAxisZ:
500 if ( sign == eSignPositive ) {
501 return Quaternion( 0, 0, c_half_sqrt2f, c_half_sqrt2f );
505 return Quaternion( 0, 0, -c_half_sqrt2f, -c_half_sqrt2f );
509 quaternion_for_matrix4_rotation( matrix4_rotation_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
513 void Select_RotateAxis( int axis, float deg ){
514 if ( fabs( deg ) == 90.f ) {
515 GlobalSelectionSystem().rotateSelected( quaternion_for_axis90( (axis_t)axis, ( deg > 0 ) ? eSignPositive : eSignNegative ) );
522 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_x_degrees( deg ) ) );
525 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_y_degrees( deg ) ) );
528 GlobalSelectionSystem().rotateSelected( quaternion_for_matrix4_rotation( matrix4_rotation_for_z_degrees( deg ) ) );
535 void Select_ShiftTexture( float x, float y ){
536 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
537 Scene_BrushShiftTexdef_Selected( GlobalSceneGraph(), x, y );
538 Scene_PatchTranslateTexture_Selected( GlobalSceneGraph(), x, y );
540 //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
541 Scene_BrushShiftTexdef_Component_Selected( GlobalSceneGraph(), x, y );
544 void Select_ScaleTexture( float x, float y ){
545 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
546 Scene_BrushScaleTexdef_Selected( GlobalSceneGraph(), x, y );
547 Scene_PatchScaleTexture_Selected( GlobalSceneGraph(), x, y );
549 Scene_BrushScaleTexdef_Component_Selected( GlobalSceneGraph(), x, y );
552 void Select_RotateTexture( float amt ){
553 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
554 Scene_BrushRotateTexdef_Selected( GlobalSceneGraph(), amt );
555 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), amt );
557 Scene_BrushRotateTexdef_Component_Selected( GlobalSceneGraph(), amt );
560 // TTimo modified to handle shader architecture:
561 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
562 void FindReplaceTextures( const char* pFind, const char* pReplace, bool bSelected ){
563 if ( !texdef_name_valid( pFind ) ) {
564 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pFind << "', aborted\n";
567 if ( !texdef_name_valid( pReplace ) ) {
568 globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
572 StringOutputStream command;
573 command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
574 UndoableCommand undo( command.c_str() );
577 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
578 Scene_BrushFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
579 Scene_PatchFindReplaceShader_Selected( GlobalSceneGraph(), pFind, pReplace );
581 Scene_BrushFindReplaceShader_Component_Selected( GlobalSceneGraph(), pFind, pReplace );
585 Scene_BrushFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
586 Scene_PatchFindReplaceShader( GlobalSceneGraph(), pFind, pReplace );
590 typedef std::vector<const char*> PropertyValues;
592 bool propertyvalues_contain( const PropertyValues& propertyvalues, const char *str ){
593 for ( PropertyValues::const_iterator i = propertyvalues.begin(); i != propertyvalues.end(); ++i )
595 if ( string_equal( str, *i ) ) {
602 class EntityFindByPropertyValueWalker : public scene::Graph::Walker
604 const PropertyValues& m_propertyvalues;
607 EntityFindByPropertyValueWalker( const char *prop, const PropertyValues& propertyvalues )
608 : m_propertyvalues( propertyvalues ), m_prop( prop ){
610 bool pre( const scene::Path& path, scene::Instance& instance ) const {
611 Entity* entity = Node_getEntity( path.top() );
613 && propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
614 Instance_getSelectable( instance )->setSelected( true );
620 void Scene_EntitySelectByPropertyValues( scene::Graph& graph, const char *prop, const PropertyValues& propertyvalues ){
621 graph.traverse( EntityFindByPropertyValueWalker( prop, propertyvalues ) );
624 class EntityGetSelectedPropertyValuesWalker : public scene::Graph::Walker
626 PropertyValues& m_propertyvalues;
629 EntityGetSelectedPropertyValuesWalker( const char *prop, PropertyValues& propertyvalues )
630 : m_propertyvalues( propertyvalues ), m_prop( prop ){
632 bool pre( const scene::Path& path, scene::Instance& instance ) const {
633 Selectable* selectable = Instance_getSelectable( instance );
635 && selectable->isSelected() ) {
636 Entity* entity = Node_getEntity( path.top() );
638 if ( !propertyvalues_contain( m_propertyvalues, entity->getKeyValue( m_prop ) ) ) {
639 m_propertyvalues.push_back( entity->getKeyValue( m_prop ) );
647 void Scene_EntityGetPropertyValues( scene::Graph& graph, const char *prop, PropertyValues& propertyvalues ){
648 graph.traverse( EntityGetSelectedPropertyValuesWalker( prop, propertyvalues ) );
651 void Select_AllOfType(){
652 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
653 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
654 GlobalSelectionSystem().setSelectedAllComponents( false );
655 Scene_BrushSelectByShader_Component( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
660 PropertyValues propertyvalues;
661 const char *prop = EntityInspector_getCurrentKey();
662 if ( !prop || !*prop ) {
665 Scene_EntityGetPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
666 GlobalSelectionSystem().setSelectedAll( false );
667 if ( !propertyvalues.empty() ) {
668 Scene_EntitySelectByPropertyValues( GlobalSceneGraph(), prop, propertyvalues );
672 Scene_BrushSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
673 Scene_PatchSelectByShader( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
678 void Select_Inside( void ){
679 SelectByBounds<SelectionPolicy_Inside>::DoSelection();
682 void Select_Touching( void ){
683 SelectByBounds<SelectionPolicy_Touching>::DoSelection( false );
686 void Select_FitTexture( float horizontal, float vertical ){
687 if ( GlobalSelectionSystem().Mode() != SelectionSystem::eComponent ) {
688 Scene_BrushFitTexture_Selected( GlobalSceneGraph(), horizontal, vertical );
690 Scene_BrushFitTexture_Component_Selected( GlobalSceneGraph(), horizontal, vertical );
695 inline void hide_node( scene::Node& node, bool hide ){
697 ? node.enable( scene::Node::eHidden )
698 : node.disable( scene::Node::eHidden );
701 class HideSelectedWalker : public scene::Graph::Walker
705 HideSelectedWalker( bool hide )
708 bool pre( const scene::Path& path, scene::Instance& instance ) const {
709 Selectable* selectable = Instance_getSelectable( instance );
711 && selectable->isSelected() ) {
712 hide_node( path.top(), m_hide );
718 void Scene_Hide_Selected( bool hide ){
719 GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
723 Scene_Hide_Selected( true );
729 GlobalSelectionSystem().setSelectedAll( false );
733 class HideAllWalker : public scene::Graph::Walker
737 HideAllWalker( bool hide )
740 bool pre( const scene::Path& path, scene::Instance& instance ) const {
741 hide_node( path.top(), m_hide );
746 void Scene_Hide_All( bool hide ){
747 GlobalSceneGraph().traverse( HideAllWalker( hide ) );
750 void Select_ShowAllHidden(){
751 Scene_Hide_All( false );
757 void Selection_Flipx(){
758 UndoableCommand undo( "mirrorSelected -axis x" );
759 Select_FlipAxis( 0 );
762 void Selection_Flipy(){
763 UndoableCommand undo( "mirrorSelected -axis y" );
764 Select_FlipAxis( 1 );
767 void Selection_Flipz(){
768 UndoableCommand undo( "mirrorSelected -axis z" );
769 Select_FlipAxis( 2 );
772 void Selection_Rotatex(){
773 UndoableCommand undo( "rotateSelected -axis x -angle -90" );
774 Select_RotateAxis( 0,-90 );
777 void Selection_Rotatey(){
778 UndoableCommand undo( "rotateSelected -axis y -angle 90" );
779 Select_RotateAxis( 1, 90 );
782 void Selection_Rotatez(){
783 UndoableCommand undo( "rotateSelected -axis z -angle -90" );
784 Select_RotateAxis( 2,-90 );
789 void Nudge( int nDim, float fNudge ){
790 Vector3 translate( 0, 0, 0 );
791 translate[nDim] = fNudge;
793 GlobalSelectionSystem().translateSelected( translate );
796 void Selection_NudgeZ( float amount ){
797 StringOutputStream command;
798 command << "nudgeSelected -axis z -amount " << amount;
799 UndoableCommand undo( command.c_str() );
804 void Selection_MoveDown(){
805 Selection_NudgeZ( -GetGridSize() );
808 void Selection_MoveUp(){
809 Selection_NudgeZ( GetGridSize() );
812 void SceneSelectionChange( const Selectable& selectable ){
816 SignalHandlerId Selection_boundsChanged;
818 void Selection_construct(){
819 typedef FreeCaller1<const Selectable&, SceneSelectionChange> SceneSelectionChangeCaller;
820 GlobalSelectionSystem().addSelectionChangeCallback( SceneSelectionChangeCaller() );
821 typedef FreeCaller1<const Selectable&, UpdateWorkzone_ForSelectionChanged> UpdateWorkzoneForSelectionChangedCaller;
822 GlobalSelectionSystem().addSelectionChangeCallback( UpdateWorkzoneForSelectionChangedCaller() );
823 typedef FreeCaller<UpdateWorkzone_ForSelection> UpdateWorkzoneForSelectionCaller;
824 Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( UpdateWorkzoneForSelectionCaller() );
827 void Selection_destroy(){
828 GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
833 #include <gdk/gdkkeysyms.h>
836 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
838 return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
840 return quaternion_multiplied_by_quaternion(
841 quaternion_multiplied_by_quaternion(
842 quaternion_for_z( degrees_to_radians( eulerXYZ[2] ) ),
843 quaternion_for_y( degrees_to_radians( eulerXYZ[1] ) )
845 quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
848 double cx = cos( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
849 double sx = sin( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
850 double cy = cos( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
851 double sy = sin( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
852 double cz = cos( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
853 double sz = sin( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
856 cz * cy * sx - sz * sy * cx,
857 cz * sy * cx + sz * cy * sx,
858 sz * cy * cx - cz * sy * sx,
859 cz * cy * cx + sz * sy * sx
872 static gboolean rotatedlg_apply( ui::Widget widget, RotateDialog* rotateDialog ){
875 eulerXYZ[0] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->x ) );
876 eulerXYZ[1] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->y ) );
877 eulerXYZ[2] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->z ) );
879 StringOutputStream command;
880 command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
881 UndoableCommand undo( command.c_str() );
883 GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ) );
887 static gboolean rotatedlg_cancel( ui::Widget widget, RotateDialog* rotateDialog ){
888 gtk_widget_hide( GTK_WIDGET( rotateDialog->window ) );
890 gtk_spin_button_set_value( rotateDialog->x, 0.0f ); // reset to 0 on close
891 gtk_spin_button_set_value( rotateDialog->y, 0.0f );
892 gtk_spin_button_set_value( rotateDialog->z, 0.0f );
897 static gboolean rotatedlg_ok( ui::Widget widget, RotateDialog* rotateDialog ){
898 rotatedlg_apply( widget, rotateDialog );
899 gtk_widget_hide( GTK_WIDGET( rotateDialog->window ) );
903 static gboolean rotatedlg_delete( ui::Widget widget, GdkEventAny *event, RotateDialog* rotateDialog ){
904 rotatedlg_cancel( widget, rotateDialog );
908 RotateDialog g_rotate_dialog;
910 if ( !g_rotate_dialog.window ) {
911 g_rotate_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary rotation", G_CALLBACK(rotatedlg_delete ), &g_rotate_dialog );
913 auto accel = ui::AccelGroup();
914 g_rotate_dialog.window.add_accel_group( accel );
917 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
918 gtk_container_add( GTK_CONTAINER( g_rotate_dialog.window ), GTK_WIDGET( hbox ) );
920 GtkTable* table = create_dialog_table( 3, 2, 4, 4 );
921 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
923 ui::Widget label = ui::Label( " X " );
924 gtk_widget_show( label );
925 gtk_table_attach( table, label, 0, 1, 0, 1,
926 (GtkAttachOptions) ( 0 ),
927 (GtkAttachOptions) ( 0 ), 0, 0 );
930 ui::Widget label = ui::Label( " Y " );
931 gtk_widget_show( label );
932 gtk_table_attach( table, label, 0, 1, 1, 2,
933 (GtkAttachOptions) ( 0 ),
934 (GtkAttachOptions) ( 0 ), 0, 0 );
937 ui::Widget label = ui::Label( " Z " );
938 gtk_widget_show( label );
939 gtk_table_attach( table, label, 0, 1, 2, 3,
940 (GtkAttachOptions) ( 0 ),
941 (GtkAttachOptions) ( 0 ), 0, 0 );
944 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
945 GtkSpinButton* spin = ui::SpinButton( adj, 1, 0 );
946 gtk_widget_show( GTK_WIDGET( spin ) );
947 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 0, 1,
948 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
949 (GtkAttachOptions) ( 0 ), 0, 0 );
950 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
951 gtk_spin_button_set_wrap( spin, TRUE );
953 gtk_widget_grab_focus( GTK_WIDGET( spin ) );
955 g_rotate_dialog.x = spin;
958 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
959 GtkSpinButton* spin = ui::SpinButton( adj, 1, 0 );
960 gtk_widget_show( GTK_WIDGET( spin ) );
961 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 1, 2,
962 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
963 (GtkAttachOptions) ( 0 ), 0, 0 );
964 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
965 gtk_spin_button_set_wrap( spin, TRUE );
967 g_rotate_dialog.y = spin;
970 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
971 GtkSpinButton* spin = ui::SpinButton( adj, 1, 0 );
972 gtk_widget_show( GTK_WIDGET( spin ) );
973 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 2, 3,
974 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
975 (GtkAttachOptions) ( 0 ), 0, 0 );
976 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
977 gtk_spin_button_set_wrap( spin, TRUE );
979 g_rotate_dialog.z = spin;
983 GtkVBox* vbox = create_dialog_vbox( 4 );
984 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
986 auto button = create_dialog_button( "OK", G_CALLBACK( rotatedlg_ok ), &g_rotate_dialog );
987 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
988 widget_make_default( button );
989 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
992 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( rotatedlg_cancel ), &g_rotate_dialog );
993 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
994 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
997 GtkButton* button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
998 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1004 gtk_widget_show( GTK_WIDGET( g_rotate_dialog.window ) );
1023 static gboolean scaledlg_apply( ui::Widget widget, ScaleDialog* scaleDialog ){
1026 sx = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->x ) ) ) );
1027 sy = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->y ) ) ) );
1028 sz = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->z ) ) ) );
1030 StringOutputStream command;
1031 command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1032 UndoableCommand undo( command.c_str() );
1034 Select_Scale( sx, sy, sz );
1039 static gboolean scaledlg_cancel( ui::Widget widget, ScaleDialog* scaleDialog ){
1040 gtk_widget_hide( GTK_WIDGET( scaleDialog->window ) );
1042 gtk_entry_set_text( GTK_ENTRY( scaleDialog->x ), "1.0" );
1043 gtk_entry_set_text( GTK_ENTRY( scaleDialog->y ), "1.0" );
1044 gtk_entry_set_text( GTK_ENTRY( scaleDialog->z ), "1.0" );
1049 static gboolean scaledlg_ok( ui::Widget widget, ScaleDialog* scaleDialog ){
1050 scaledlg_apply( widget, scaleDialog );
1051 gtk_widget_hide( GTK_WIDGET( scaleDialog->window ) );
1055 static gboolean scaledlg_delete( ui::Widget widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1056 scaledlg_cancel( widget, scaleDialog );
1060 ScaleDialog g_scale_dialog;
1063 if ( !g_scale_dialog.window ) {
1064 g_scale_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary scale", G_CALLBACK(scaledlg_delete ), &g_scale_dialog );
1066 auto accel = ui::AccelGroup();
1067 g_scale_dialog.window.add_accel_group( accel );
1070 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1071 gtk_container_add( GTK_CONTAINER( g_scale_dialog.window ), GTK_WIDGET( hbox ) );
1073 GtkTable* table = create_dialog_table( 3, 2, 4, 4 );
1074 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1076 ui::Widget label = ui::Label( " X " );
1077 gtk_widget_show( label );
1078 gtk_table_attach( table, label, 0, 1, 0, 1,
1079 (GtkAttachOptions) ( 0 ),
1080 (GtkAttachOptions) ( 0 ), 0, 0 );
1083 ui::Widget label = ui::Label( " Y " );
1084 gtk_widget_show( label );
1085 gtk_table_attach( table, label, 0, 1, 1, 2,
1086 (GtkAttachOptions) ( 0 ),
1087 (GtkAttachOptions) ( 0 ), 0, 0 );
1090 ui::Widget label = ui::Label( " Z " );
1091 gtk_widget_show( label );
1092 gtk_table_attach( table, label, 0, 1, 2, 3,
1093 (GtkAttachOptions) ( 0 ),
1094 (GtkAttachOptions) ( 0 ), 0, 0 );
1097 ui::Widget entry = ui::Entry();
1098 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1099 gtk_widget_show( entry );
1100 gtk_table_attach( table, entry, 1, 2, 0, 1,
1101 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1102 (GtkAttachOptions) ( 0 ), 0, 0 );
1104 g_scale_dialog.x = entry;
1107 ui::Widget entry = ui::Entry();
1108 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1109 gtk_widget_show( entry );
1110 gtk_table_attach( table, entry, 1, 2, 1, 2,
1111 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1112 (GtkAttachOptions) ( 0 ), 0, 0 );
1114 g_scale_dialog.y = entry;
1117 ui::Widget entry = ui::Entry();
1118 gtk_entry_set_text( GTK_ENTRY( entry ), "1.0" );
1119 gtk_widget_show( entry );
1120 gtk_table_attach( table, entry, 1, 2, 2, 3,
1121 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1122 (GtkAttachOptions) ( 0 ), 0, 0 );
1124 g_scale_dialog.z = entry;
1128 GtkVBox* vbox = create_dialog_vbox( 4 );
1129 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1131 auto button = create_dialog_button( "OK", G_CALLBACK( scaledlg_ok ), &g_scale_dialog );
1132 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1133 widget_make_default( button );
1134 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1137 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( scaledlg_cancel ), &g_scale_dialog );
1138 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1139 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1142 GtkButton* button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1143 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1149 gtk_widget_show( GTK_WIDGET( g_scale_dialog.window ) );