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 );
710 inline void hide_node( scene::Node& node, bool hide ){
712 ? node.enable( scene::Node::eHidden )
713 : node.disable( scene::Node::eHidden );
716 class HideSelectedWalker : public scene::Graph::Walker
720 HideSelectedWalker( bool hide )
723 bool pre( const scene::Path& path, scene::Instance& instance ) const {
724 Selectable* selectable = Instance_getSelectable( instance );
726 && selectable->isSelected() ) {
727 hide_node( path.top(), m_hide );
733 void Scene_Hide_Selected( bool hide ){
734 GlobalSceneGraph().traverse( HideSelectedWalker( hide ) );
738 Scene_Hide_Selected( true );
744 GlobalSelectionSystem().setSelectedAll( false );
748 class HideAllWalker : public scene::Graph::Walker
752 HideAllWalker( bool hide )
755 bool pre( const scene::Path& path, scene::Instance& instance ) const {
756 hide_node( path.top(), m_hide );
761 void Scene_Hide_All( bool hide ){
762 GlobalSceneGraph().traverse( HideAllWalker( hide ) );
765 void Select_ShowAllHidden(){
766 Scene_Hide_All( false );
772 void Selection_Flipx(){
773 UndoableCommand undo( "mirrorSelected -axis x" );
774 Select_FlipAxis( 0 );
777 void Selection_Flipy(){
778 UndoableCommand undo( "mirrorSelected -axis y" );
779 Select_FlipAxis( 1 );
782 void Selection_Flipz(){
783 UndoableCommand undo( "mirrorSelected -axis z" );
784 Select_FlipAxis( 2 );
787 void Selection_Rotatex(){
788 UndoableCommand undo( "rotateSelected -axis x -angle -90" );
789 Select_RotateAxis( 0,-90 );
792 void Selection_Rotatey(){
793 UndoableCommand undo( "rotateSelected -axis y -angle 90" );
794 Select_RotateAxis( 1, 90 );
797 void Selection_Rotatez(){
798 UndoableCommand undo( "rotateSelected -axis z -angle -90" );
799 Select_RotateAxis( 2,-90 );
804 void Nudge( int nDim, float fNudge ){
805 Vector3 translate( 0, 0, 0 );
806 translate[nDim] = fNudge;
808 GlobalSelectionSystem().translateSelected( translate );
811 void Selection_NudgeZ( float amount ){
812 StringOutputStream command;
813 command << "nudgeSelected -axis z -amount " << amount;
814 UndoableCommand undo( command.c_str() );
819 void Selection_MoveDown(){
820 Selection_NudgeZ( -GetGridSize() );
823 void Selection_MoveUp(){
824 Selection_NudgeZ( GetGridSize() );
827 void SceneSelectionChange( const Selectable& selectable ){
831 SignalHandlerId Selection_boundsChanged;
833 void Selection_construct(){
834 typedef FreeCaller<void(const Selectable&), SceneSelectionChange> SceneSelectionChangeCaller;
835 GlobalSelectionSystem().addSelectionChangeCallback( SceneSelectionChangeCaller() );
836 typedef FreeCaller<void(const Selectable&), UpdateWorkzone_ForSelectionChanged> UpdateWorkzoneForSelectionChangedCaller;
837 GlobalSelectionSystem().addSelectionChangeCallback( UpdateWorkzoneForSelectionChangedCaller() );
838 typedef FreeCaller<void(), UpdateWorkzone_ForSelection> UpdateWorkzoneForSelectionCaller;
839 Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( UpdateWorkzoneForSelectionCaller() );
842 void Selection_destroy(){
843 GlobalSceneGraph().removeBoundsChangedCallback( Selection_boundsChanged );
848 #include <gdk/gdkkeysyms.h>
851 inline Quaternion quaternion_for_euler_xyz_degrees( const Vector3& eulerXYZ ){
853 return quaternion_for_matrix4_rotation( matrix4_rotation_for_euler_xyz_degrees( eulerXYZ ) );
855 return quaternion_multiplied_by_quaternion(
856 quaternion_multiplied_by_quaternion(
857 quaternion_for_z( degrees_to_radians( eulerXYZ[2] ) ),
858 quaternion_for_y( degrees_to_radians( eulerXYZ[1] ) )
860 quaternion_for_x( degrees_to_radians( eulerXYZ[0] ) )
863 double cx = cos( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
864 double sx = sin( degrees_to_radians( eulerXYZ[0] * 0.5 ) );
865 double cy = cos( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
866 double sy = sin( degrees_to_radians( eulerXYZ[1] * 0.5 ) );
867 double cz = cos( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
868 double sz = sin( degrees_to_radians( eulerXYZ[2] * 0.5 ) );
871 cz * cy * sx - sz * sy * cx,
872 cz * sy * cx + sz * cy * sx,
873 sz * cy * cx - cz * sy * sx,
874 cz * cy * cx + sz * sy * sx
881 ui::SpinButton x{ui::null};
882 ui::SpinButton y{ui::null};
883 ui::SpinButton z{ui::null};
884 ui::Window window{ui::null};
887 static gboolean rotatedlg_apply( ui::Widget widget, RotateDialog* rotateDialog ){
890 gtk_spin_button_update ( rotateDialog->x );
891 gtk_spin_button_update ( rotateDialog->y );
892 gtk_spin_button_update ( rotateDialog->z );
893 eulerXYZ[0] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->x ) );
894 eulerXYZ[1] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->y ) );
895 eulerXYZ[2] = static_cast<float>( gtk_spin_button_get_value( rotateDialog->z ) );
897 StringOutputStream command;
898 command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
899 UndoableCommand undo( command.c_str() );
901 GlobalSelectionSystem().rotateSelected( quaternion_for_euler_xyz_degrees( eulerXYZ ) );
905 static gboolean rotatedlg_cancel( ui::Widget widget, RotateDialog* rotateDialog ){
906 rotateDialog->window.hide();
908 gtk_spin_button_set_value( rotateDialog->x, 0.0f ); // reset to 0 on close
909 gtk_spin_button_set_value( rotateDialog->y, 0.0f );
910 gtk_spin_button_set_value( rotateDialog->z, 0.0f );
915 static gboolean rotatedlg_ok( ui::Widget widget, RotateDialog* rotateDialog ){
916 rotatedlg_apply( widget, rotateDialog );
917 // rotatedlg_cancel( widget, rotateDialog );
918 rotateDialog->window.hide();
922 static gboolean rotatedlg_delete( ui::Widget widget, GdkEventAny *event, RotateDialog* rotateDialog ){
923 rotatedlg_cancel( widget, rotateDialog );
927 RotateDialog g_rotate_dialog;
929 if ( !g_rotate_dialog.window ) {
930 g_rotate_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary rotation", G_CALLBACK(rotatedlg_delete ), &g_rotate_dialog );
932 auto accel = ui::AccelGroup(ui::New);
933 g_rotate_dialog.window.add_accel_group( accel );
936 auto hbox = create_dialog_hbox( 4, 4 );
937 g_rotate_dialog.window.add(hbox);
939 auto table = create_dialog_table( 3, 2, 4, 4 );
940 hbox.pack_start( table, TRUE, TRUE, 0 );
942 ui::Widget label = ui::Label( " X " );
944 table.attach(label, {0, 1, 0, 1}, {0, 0});
947 ui::Widget label = ui::Label( " Y " );
949 table.attach(label, {0, 1, 1, 2}, {0, 0});
952 ui::Widget label = ui::Label( " Z " );
954 table.attach(label, {0, 1, 2, 3}, {0, 0});
957 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
958 auto spin = ui::SpinButton( adj, 1, 0 );
960 table.attach(spin, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
961 spin.dimensions(64, -1);
962 gtk_spin_button_set_wrap( spin, TRUE );
964 gtk_widget_grab_focus( spin );
966 g_rotate_dialog.x = spin;
969 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
970 auto spin = ui::SpinButton( adj, 1, 0 );
972 table.attach(spin, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
973 spin.dimensions(64, -1);
974 gtk_spin_button_set_wrap( spin, TRUE );
976 g_rotate_dialog.y = spin;
979 auto adj = ui::Adjustment( 0, -359, 359, 1, 10, 0 );
980 auto spin = ui::SpinButton( adj, 1, 0 );
982 table.attach(spin, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
983 spin.dimensions(64, -1);
984 gtk_spin_button_set_wrap( spin, TRUE );
986 g_rotate_dialog.z = spin;
990 auto vbox = create_dialog_vbox( 4 );
991 hbox.pack_start( vbox, TRUE, TRUE, 0 );
993 auto button = create_dialog_button( "OK", G_CALLBACK( rotatedlg_ok ), &g_rotate_dialog );
994 vbox.pack_start( button, FALSE, FALSE, 0 );
995 widget_make_default( button );
996 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
999 auto button = create_dialog_button( "Cancel", G_CALLBACK( rotatedlg_cancel ), &g_rotate_dialog );
1000 vbox.pack_start( button, FALSE, FALSE, 0 );
1001 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1004 auto button = create_dialog_button( "Apply", G_CALLBACK( rotatedlg_apply ), &g_rotate_dialog );
1005 vbox.pack_start( button, FALSE, FALSE, 0 );
1011 g_rotate_dialog.window.show();
1024 ui::Entry x{ui::null};
1025 ui::Entry y{ui::null};
1026 ui::Entry z{ui::null};
1027 ui::Window window{ui::null};
1030 static gboolean scaledlg_apply( ui::Widget widget, ScaleDialog* scaleDialog ){
1033 sx = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->x ) ) ) );
1034 sy = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->y ) ) ) );
1035 sz = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( scaleDialog->z ) ) ) );
1037 StringOutputStream command;
1038 command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1039 UndoableCommand undo( command.c_str() );
1041 Select_Scale( sx, sy, sz );
1046 static gboolean scaledlg_cancel( ui::Widget widget, ScaleDialog* scaleDialog ){
1047 scaleDialog->window.hide();
1049 scaleDialog->x.text("1.0");
1050 scaleDialog->y.text("1.0");
1051 scaleDialog->z.text("1.0");
1056 static gboolean scaledlg_ok( ui::Widget widget, ScaleDialog* scaleDialog ){
1057 scaledlg_apply( widget, scaleDialog );
1058 //scaledlg_cancel( widget, scaleDialog );
1059 scaleDialog->window.hide();
1063 static gboolean scaledlg_delete( ui::Widget widget, GdkEventAny *event, ScaleDialog* scaleDialog ){
1064 scaledlg_cancel( widget, scaleDialog );
1068 ScaleDialog g_scale_dialog;
1071 if ( !g_scale_dialog.window ) {
1072 g_scale_dialog.window = MainFrame_getWindow().create_dialog_window("Arbitrary scale", G_CALLBACK(scaledlg_delete ), &g_scale_dialog );
1074 auto accel = ui::AccelGroup(ui::New);
1075 g_scale_dialog.window.add_accel_group( accel );
1078 auto hbox = create_dialog_hbox( 4, 4 );
1079 g_scale_dialog.window.add(hbox);
1081 auto table = create_dialog_table( 3, 2, 4, 4 );
1082 hbox.pack_start( table, TRUE, TRUE, 0 );
1084 ui::Widget label = ui::Label( " X " );
1086 table.attach(label, {0, 1, 0, 1}, {0, 0});
1089 ui::Widget label = ui::Label( " Y " );
1091 table.attach(label, {0, 1, 1, 2}, {0, 0});
1094 ui::Widget label = ui::Label( " Z " );
1096 table.attach(label, {0, 1, 2, 3}, {0, 0});
1099 auto entry = ui::Entry(ui::New);
1102 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1104 g_scale_dialog.x = entry;
1107 auto entry = ui::Entry(ui::New);
1110 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1112 g_scale_dialog.y = entry;
1115 auto entry = ui::Entry(ui::New);
1118 table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
1120 g_scale_dialog.z = entry;
1124 auto vbox = create_dialog_vbox( 4 );
1125 hbox.pack_start( vbox, TRUE, TRUE, 0 );
1127 auto button = create_dialog_button( "OK", G_CALLBACK( scaledlg_ok ), &g_scale_dialog );
1128 vbox.pack_start( button, FALSE, FALSE, 0 );
1129 widget_make_default( button );
1130 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1133 auto button = create_dialog_button( "Cancel", G_CALLBACK( scaledlg_cancel ), &g_scale_dialog );
1134 vbox.pack_start( button, FALSE, FALSE, 0 );
1135 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1138 auto button = create_dialog_button( "Apply", G_CALLBACK( scaledlg_apply ), &g_scale_dialog );
1139 vbox.pack_start( button, FALSE, FALSE, 0 );
1145 g_scale_dialog.window.show();