]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/select.cpp
portability improvements
[xonotic/netradiant.git] / radiant / select.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #include "select.h"
23
24 #include "debugging/debugging.h"
25
26 #include "ientity.h"
27 #include "iselection.h"
28 #include "iundo.h"
29
30 #include <vector>
31
32 #include "stream/stringstream.h"
33 #include "signal/isignal.h"
34 #include "shaderlib.h"
35 #include "scenelib.h"
36
37 #include "gtkutil/idledraw.h"
38 #include "gtkutil/dialog.h"
39 #include "gtkutil/widget.h"
40 #include "brushmanip.h"
41 #include "patchmanip.h"
42 #include "patchdialog.h"
43 #include "selection.h"
44 #include "texwindow.h"
45 #include "gtkmisc.h"
46 #include "mainframe.h"
47 #include "grid.h"
48 #include "map.h"
49
50
51
52 select_workzone_t g_select_workzone;
53
54
55 class DeleteSelected : public scene::Graph::Walker
56 {
57   mutable bool m_remove;
58   mutable bool m_removedChild;
59 public:
60   DeleteSelected()
61     : m_remove(false), m_removedChild(false)
62   {
63   }
64   bool pre(const scene::Path& path, scene::Instance& instance) const
65   {
66     m_removedChild = false;
67
68     Selectable* selectable = Instance_getSelectable(instance);
69     if(selectable != 0
70       && selectable->isSelected()
71       && path.size() > 1
72       && !path.top().get().isRoot())
73     {
74       m_remove = true;
75
76       return false;
77     }
78     return true;
79   }
80   void post(const scene::Path& path, scene::Instance& instance) const
81   {
82     if(m_removedChild)
83     {
84       m_removedChild = false;
85
86       // delete empty entities
87       Entity* entity = Node_getEntity(path.top());
88       if(entity != 0
89         && path.top().get_pointer() != Map_FindWorldspawn(g_map)
90         && Node_getTraversable(path.top())->empty())
91       {
92         Path_deleteTop(path);
93       }
94     }
95
96     if(m_remove)
97     {
98       if(Node_isEntity(path.parent()) != 0)
99       {
100         m_removedChild = true;
101       }
102
103       m_remove = false;
104       Path_deleteTop(path);
105     }
106   }
107 };
108
109 void Scene_DeleteSelected(scene::Graph& graph)
110 {
111   graph.traverse(DeleteSelected());
112   SceneChangeNotify();
113 }
114
115 void Select_Delete (void)
116 {
117   Scene_DeleteSelected(GlobalSceneGraph());
118 }
119
120 class InvertSelectionWalker : public scene::Graph::Walker
121 {
122   SelectionSystem::EMode m_mode;
123   mutable Selectable* m_selectable;
124 public:
125   InvertSelectionWalker(SelectionSystem::EMode mode)
126     : m_mode(mode), m_selectable(0)
127   {
128   }
129   bool pre(const scene::Path& path, scene::Instance& instance) const
130   {
131     Selectable* selectable = Instance_getSelectable(instance);
132     if(selectable)
133     {
134       switch(m_mode)
135       {
136       case SelectionSystem::eEntity:
137         if(Node_isEntity(path.top()) != 0)
138         {
139           m_selectable = path.top().get().visible() ? selectable : 0;
140         }
141         break;
142       case SelectionSystem::ePrimitive:
143         m_selectable = path.top().get().visible() ? selectable : 0;
144         break;
145       case SelectionSystem::eComponent:
146         break;
147       }
148     }
149     return true;
150   }
151   void post(const scene::Path& path, scene::Instance& instance) const
152   {
153     if(m_selectable != 0)
154     {
155       m_selectable->setSelected(!m_selectable->isSelected());
156       m_selectable = 0;
157     }
158   }
159 };
160
161 void Scene_Invert_Selection(scene::Graph& graph)
162 {
163   graph.traverse(InvertSelectionWalker(GlobalSelectionSystem().Mode()));
164 }
165
166 void Select_Invert()
167 {
168   Scene_Invert_Selection(GlobalSceneGraph());
169 }
170
171 class ExpandSelectionToEntitiesWalker : public scene::Graph::Walker
172 {
173   mutable std::size_t m_depth;
174 public:
175   ExpandSelectionToEntitiesWalker() : m_depth(0)
176   {
177   }
178   bool pre(const scene::Path& path, scene::Instance& instance) const
179   {
180     ++m_depth;
181     if(m_depth == 2) // entity depth
182     {
183       // traverse and select children if any one is selected
184       return Node_getEntity(path.top())->isContainer() && instance.childSelected();
185     }
186     else if(m_depth == 3) // primitive depth
187     {
188       Instance_setSelected(instance, true);
189       return false;
190     }
191     return true;
192   }
193   void post(const scene::Path& path, scene::Instance& instance) const
194   {
195     --m_depth;
196   }
197 };
198
199 void Scene_ExpandSelectionToEntities()
200 {
201   GlobalSceneGraph().traverse(ExpandSelectionToEntitiesWalker());
202 }
203
204
205 namespace
206 {
207   void Selection_UpdateWorkzone()
208   {
209     if(GlobalSelectionSystem().countSelected() != 0)
210     {
211       Select_GetBounds(g_select_workzone.d_work_min, g_select_workzone.d_work_max);
212     }
213   }
214   typedef FreeCaller<Selection_UpdateWorkzone> SelectionUpdateWorkzoneCaller;
215
216   IdleDraw g_idleWorkzone = IdleDraw(SelectionUpdateWorkzoneCaller());
217 }
218
219 const select_workzone_t& Select_getWorkZone()
220 {
221   g_idleWorkzone.flush();
222   return g_select_workzone;
223 }
224
225 void UpdateWorkzone_ForSelection()
226 {
227   g_idleWorkzone.queueDraw();
228 }
229
230 // update the workzone to the current selection
231 void UpdateWorkzone_ForSelection(const Selectable& selectable)
232 {
233   if(selectable.isSelected())
234   {
235     UpdateWorkzone_ForSelection();
236   }
237 }
238
239 void Select_SetShader(const char* shader)
240 {
241   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
242   {
243     Scene_BrushSetShader_Selected(GlobalSceneGraph(), shader);
244     Scene_PatchSetShader_Selected(GlobalSceneGraph(), shader);
245   }
246   Scene_BrushSetShader_Component_Selected(GlobalSceneGraph(), shader);
247 }
248
249 void Select_SetTexdef(const TextureProjection& projection)
250 {
251   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
252   {
253     Scene_BrushSetTexdef_Selected(GlobalSceneGraph(), projection);
254   }
255   Scene_BrushSetTexdef_Component_Selected(GlobalSceneGraph(), projection);
256 }
257
258 void Select_SetFlags(const ContentsFlagsValue& flags)
259 {
260   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
261   {
262     Scene_BrushSetFlags_Selected(GlobalSceneGraph(), flags);
263   }
264   Scene_BrushSetFlags_Component_Selected(GlobalSceneGraph(), flags);
265 }
266
267 void Select_GetBounds (Vector3& mins, Vector3& maxs)
268 {
269   AABB bounds;
270   Scene_BoundsSelected(GlobalSceneGraph(), bounds);
271   maxs = vector3_added(bounds.origin, bounds.extents);
272   mins = vector3_subtracted(bounds.origin, bounds.extents);
273 }
274
275 void Select_GetMid (Vector3& mid)
276 {
277   AABB bounds;
278   Scene_BoundsSelected(GlobalSceneGraph(), bounds);
279   mid = vector3_snapped(bounds.origin);
280 }
281
282
283 void Select_FlipAxis (int axis)
284 {
285   Vector3 flip(1, 1, 1);
286   flip[axis] = -1;
287   GlobalSelectionSystem().scaleSelected(flip);
288 }
289
290
291 void Select_Scale(float x, float y, float z)
292 {
293   GlobalSelectionSystem().scaleSelected(Vector3(x, y, z));
294 }
295
296 enum axis_t
297 {
298   eAxisX = 0,
299   eAxisY = 1,
300   eAxisZ = 2,
301 };
302
303 enum sign_t
304 {
305   eSignPositive = 1,
306   eSignNegative = -1,
307 };
308
309 inline Matrix4 matrix4_rotation_for_axis90(axis_t axis, sign_t sign)
310 {
311   switch(axis)
312   {
313   case eAxisX:
314     if(sign == eSignPositive)
315     {
316       return matrix4_rotation_for_sincos_x(1, 0);
317     }
318     else
319     {
320       return matrix4_rotation_for_sincos_x(-1, 0);
321     }
322   case eAxisY:
323     if(sign == eSignPositive)
324     {
325       return matrix4_rotation_for_sincos_y(1, 0);
326     }
327     else
328     {
329       return matrix4_rotation_for_sincos_y(-1, 0);
330     }
331   default://case eAxisZ:
332     if(sign == eSignPositive)
333     {
334       return matrix4_rotation_for_sincos_z(1, 0);
335     }
336     else
337     {
338       return matrix4_rotation_for_sincos_z(-1, 0);
339     }
340   }
341 }
342
343 inline void matrix4_rotate_by_axis90(Matrix4& matrix, axis_t axis, sign_t sign)
344 {
345   matrix4_multiply_by_matrix4(matrix, matrix4_rotation_for_axis90(axis, sign));
346 }
347
348 inline void matrix4_pivoted_rotate_by_axis90(Matrix4& matrix, axis_t axis, sign_t sign, const Vector3& pivotpoint)
349 {
350   matrix4_translate_by_vec3(matrix, pivotpoint);
351   matrix4_rotate_by_axis90(matrix, axis, sign);
352   matrix4_translate_by_vec3(matrix, vector3_negated(pivotpoint));
353 }
354
355 inline Quaternion quaternion_for_axis90(axis_t axis, sign_t sign)
356 {
357 #if 1
358   switch(axis)
359   {
360   case eAxisX:
361     if(sign == eSignPositive)
362     {
363       return Quaternion(c_half_sqrt2f, 0, 0, c_half_sqrt2f);
364     }
365     else
366     {
367       return Quaternion(-c_half_sqrt2f, 0, 0, -c_half_sqrt2f);
368     }
369   case eAxisY:
370     if(sign == eSignPositive)
371     {
372       return Quaternion(0, c_half_sqrt2f, 0, c_half_sqrt2f);
373     }
374     else
375     {
376       return Quaternion(0, -c_half_sqrt2f, 0, -c_half_sqrt2f);
377     }
378   default://case eAxisZ:
379     if(sign == eSignPositive)
380     {
381       return Quaternion(0, 0, c_half_sqrt2f, c_half_sqrt2f);
382     }
383     else
384     {
385       return Quaternion(0, 0, -c_half_sqrt2f, -c_half_sqrt2f);
386     }
387   }
388 #else
389   quaternion_for_matrix4_rotation(matrix4_rotation_for_axis90((axis_t)axis, (deg > 0) ? eSignPositive : eSignNegative));
390 #endif
391 }
392
393 void Select_RotateAxis (int axis, float deg)
394 {
395   if(fabs(deg) == 90.f)
396   {
397     GlobalSelectionSystem().rotateSelected(quaternion_for_axis90((axis_t)axis, (deg > 0) ? eSignPositive : eSignNegative));
398   }
399   else
400   {
401     switch(axis)
402     {
403     case 0:
404       GlobalSelectionSystem().rotateSelected(quaternion_for_matrix4_rotation(matrix4_rotation_for_x_degrees(deg)));
405       break;
406     case 1:
407       GlobalSelectionSystem().rotateSelected(quaternion_for_matrix4_rotation(matrix4_rotation_for_y_degrees(deg)));
408       break;
409     case 2:
410       GlobalSelectionSystem().rotateSelected(quaternion_for_matrix4_rotation(matrix4_rotation_for_z_degrees(deg)));
411       break;
412     }
413   }
414 }
415
416
417 void Select_ShiftTexture(float x, float y)
418 {
419   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
420   {
421     Scene_BrushShiftTexdef_Selected(GlobalSceneGraph(), x, y);
422     Scene_PatchTranslateTexture_Selected(GlobalSceneGraph(), x, y);
423   }
424   //globalOutputStream() << "shift selected face textures: s=" << x << " t=" << y << '\n';
425   Scene_BrushShiftTexdef_Component_Selected(GlobalSceneGraph(), x, y);
426 }
427
428 void Select_ScaleTexture(float x, float y)
429 {
430   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
431   {
432     Scene_BrushScaleTexdef_Selected(GlobalSceneGraph(), x, y);
433     Scene_PatchScaleTexture_Selected(GlobalSceneGraph(), x, y);
434   }
435   Scene_BrushScaleTexdef_Component_Selected(GlobalSceneGraph(), x, y);
436 }
437
438 void Select_RotateTexture(float amt)
439 {
440   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
441   {
442     Scene_BrushRotateTexdef_Selected(GlobalSceneGraph(), amt);
443     Scene_PatchRotateTexture_Selected(GlobalSceneGraph(), amt);
444   }
445   Scene_BrushRotateTexdef_Component_Selected(GlobalSceneGraph(), amt);
446 }
447
448 // TTimo modified to handle shader architecture:
449 // expects shader names at input, comparison relies on shader names .. texture names no longer relevant
450 void FindReplaceTextures(const char* pFind, const char* pReplace, bool bSelected)
451 {
452   if(!texdef_name_valid(pFind))
453   {
454     globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pFind << "', aborted\n";
455     return;
456   }
457   if(!texdef_name_valid(pReplace))
458   {
459     globalErrorStream() << "FindReplaceTextures: invalid texture name: '" << pReplace << "', aborted\n";
460     return;
461   }
462
463   StringOutputStream command;
464   command << "textureFindReplace -find " << pFind << " -replace " << pReplace;
465   UndoableCommand undo(command.c_str());
466
467   if(bSelected)
468   {
469     if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
470     {
471       Scene_BrushFindReplaceShader_Selected(GlobalSceneGraph(), pFind, pReplace);
472       Scene_PatchFindReplaceShader_Selected(GlobalSceneGraph(), pFind, pReplace);
473     }
474     Scene_BrushFindReplaceShader_Component_Selected(GlobalSceneGraph(), pFind, pReplace);
475   }
476   else
477   {
478     Scene_BrushFindReplaceShader(GlobalSceneGraph(), pFind, pReplace);
479     Scene_PatchFindReplaceShader(GlobalSceneGraph(), pFind, pReplace);
480   }
481 }
482
483 typedef std::vector<const char*> Classnames;
484
485 bool classnames_match_entity(const Classnames& classnames, Entity* entity)
486 {
487   for(Classnames::const_iterator i = classnames.begin(); i != classnames.end(); ++i)
488   {
489     if(string_equal(entity->getKeyValue("classname"), *i))
490     {
491       return true;
492     }
493   }
494   return false;
495 }
496
497 class EntityFindByClassnameWalker : public scene::Graph::Walker
498 {
499   const Classnames& m_classnames;
500 public:
501   EntityFindByClassnameWalker(const Classnames& classnames)
502     : m_classnames(classnames)
503   {
504   }
505   bool pre(const scene::Path& path, scene::Instance& instance) const
506   {
507     Entity* entity = Node_getEntity(path.top());
508     if(entity != 0
509       && classnames_match_entity(m_classnames, entity))
510     {
511       Instance_getSelectable(instance)->setSelected(true);
512     }
513     return true;
514   }
515 };
516
517 void Scene_EntitySelectByClassnames(scene::Graph& graph, const Classnames& classnames)
518 {
519   graph.traverse(EntityFindByClassnameWalker(classnames));
520 }
521
522 class EntityGetSelectedClassnamesWalker : public scene::Graph::Walker
523 {
524   Classnames& m_classnames;
525 public:
526   EntityGetSelectedClassnamesWalker(Classnames& classnames)
527     : m_classnames(classnames)
528   {
529   }
530   bool pre(const scene::Path& path, scene::Instance& instance) const
531   {
532     Selectable* selectable = Instance_getSelectable(instance);
533     if(selectable != 0
534       && selectable->isSelected())
535     {
536       Entity* entity = Node_getEntity(path.top());
537       if(entity != 0)
538       {
539         m_classnames.push_back(entity->getKeyValue("classname"));
540       }
541     }
542     return true;
543   }
544 };
545
546 void Scene_EntityGetClassnames(scene::Graph& graph, Classnames& classnames)
547 {
548   graph.traverse(EntityGetSelectedClassnamesWalker(classnames));
549 }
550
551 void Select_AllOfType()
552 {
553   if(GlobalSelectionSystem().Mode() == SelectionSystem::eComponent)
554   {
555     if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace)
556     {
557       GlobalSelectionSystem().setSelectedAllComponents(false);
558       Scene_BrushSelectByShader_Component(GlobalSceneGraph(), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
559     }
560   }
561   else
562   {
563     Classnames classnames;
564     Scene_EntityGetClassnames(GlobalSceneGraph(), classnames);
565     GlobalSelectionSystem().setSelectedAll(false);
566     if(!classnames.empty())
567     {
568       Scene_EntitySelectByClassnames(GlobalSceneGraph(), classnames);
569     }
570     else
571     {
572       Scene_BrushSelectByShader(GlobalSceneGraph(), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
573       Scene_PatchSelectByShader(GlobalSceneGraph(), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
574     }
575   }
576 }
577
578
579
580 void Select_FitTexture(float horizontal, float vertical)
581 {
582   if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
583   {
584     Scene_BrushFitTexture_Selected(GlobalSceneGraph(), horizontal, vertical);
585   }
586   Scene_BrushFitTexture_Component_Selected(GlobalSceneGraph(), horizontal, vertical);
587
588   SceneChangeNotify();
589 }
590
591 inline void hide_node(scene::Node& node, bool hide)
592 {
593   hide
594     ? node.enable(scene::Node::eHidden)
595     : node.disable(scene::Node::eHidden);
596 }
597
598 class HideSelectedWalker : public scene::Graph::Walker
599 {
600   bool m_hide;
601 public:
602   HideSelectedWalker(bool hide)
603     : m_hide(hide)
604   {
605   }
606   bool pre(const scene::Path& path, scene::Instance& instance) const
607   {
608     Selectable* selectable = Instance_getSelectable(instance);
609     if(selectable != 0
610       && selectable->isSelected())
611     {
612       hide_node(path.top(), m_hide);
613     }
614     return true;
615   }
616 };
617
618 void Scene_Hide_Selected(bool hide)
619 {
620   GlobalSceneGraph().traverse(HideSelectedWalker(hide));
621 }
622
623 void Select_Hide()
624 {
625   Scene_Hide_Selected(true);
626   SceneChangeNotify();
627 }
628
629 void HideSelected()
630 {
631   Select_Hide();
632   GlobalSelectionSystem().setSelectedAll(false);
633 }
634
635
636 class HideAllWalker : public scene::Graph::Walker
637 {
638   bool m_hide;
639 public:
640   HideAllWalker(bool hide)
641     : m_hide(hide)
642   {
643   }
644   bool pre(const scene::Path& path, scene::Instance& instance) const
645   {
646     hide_node(path.top(), m_hide);
647     return true;
648   }
649 };
650
651 void Scene_Hide_All(bool hide)
652 {
653   GlobalSceneGraph().traverse(HideAllWalker(hide));
654 }
655
656 void Select_ShowAllHidden()
657 {
658   Scene_Hide_All(false);
659   SceneChangeNotify();
660 }
661
662
663
664 void Selection_Flipx()
665 {
666   UndoableCommand undo("mirrorSelected -axis x");
667   Select_FlipAxis(0);
668 }
669
670 void Selection_Flipy()
671 {
672   UndoableCommand undo("mirrorSelected -axis y");
673   Select_FlipAxis(1);
674 }
675
676 void Selection_Flipz()
677 {
678   UndoableCommand undo("mirrorSelected -axis z");
679   Select_FlipAxis(2);
680 }
681
682 void Selection_Rotatex()
683 {
684   UndoableCommand undo("rotateSelected -axis x -angle -90");
685   Select_RotateAxis(0,-90);
686 }
687
688 void Selection_Rotatey()
689 {
690   UndoableCommand undo("rotateSelected -axis y -angle 90");
691   Select_RotateAxis(1, 90);
692 }
693
694 void Selection_Rotatez()
695 {
696   UndoableCommand undo("rotateSelected -axis z -angle -90");
697   Select_RotateAxis(2,-90);
698 }
699
700
701
702 void Nudge(int nDim, float fNudge)
703 {
704   Vector3 translate(0, 0, 0);
705   translate[nDim] = fNudge;
706   
707   GlobalSelectionSystem().translateSelected(translate);
708 }
709
710 void Selection_NudgeZ(float amount)
711 {
712   StringOutputStream command;
713   command << "nudgeSelected -axis z -amount " << amount;
714   UndoableCommand undo(command.c_str());
715
716   Nudge(2, amount);
717 }
718
719 void Selection_MoveDown()
720 {
721   Selection_NudgeZ(-GetGridSize());
722 }
723
724 void Selection_MoveUp()
725 {
726   Selection_NudgeZ(GetGridSize());
727 }
728
729 void SceneSelectionChange(const Selectable& selectable)
730 {
731   SceneChangeNotify();
732 }
733
734 SignalHandlerId Selection_boundsChanged;
735
736 void Selection_construct()
737 {
738   GlobalSelectionSystem().addSelectionChangeCallback(FreeCaller1<const Selectable&, SceneSelectionChange>());
739   GlobalSelectionSystem().addSelectionChangeCallback(FreeCaller1<const Selectable&, UpdateWorkzone_ForSelection>());
740   Selection_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback(FreeCaller<UpdateWorkzone_ForSelection>());
741 }
742
743 void Selection_destroy()
744 {
745   GlobalSceneGraph().removeBoundsChangedCallback(Selection_boundsChanged);
746 }
747
748
749 #include "gtkdlgs.h"
750 #include <gtk/gtkbox.h>
751 #include <gtk/gtkspinbutton.h>
752 #include <gtk/gtktable.h>
753 #include <gtk/gtklabel.h>
754 #include <gdk/gdkkeysyms.h>
755
756 inline Quaternion quaternion_for_euler_xyz_degrees(const Vector3& eulerXYZ)
757 {
758   double cx = cos(degrees_to_radians(eulerXYZ[0] * 0.5));
759   double sx = sin(degrees_to_radians(eulerXYZ[0] * 0.5));
760   double cy = cos(degrees_to_radians(eulerXYZ[1] * 0.5));
761   double sy = sin(degrees_to_radians(eulerXYZ[1] * 0.5));
762   double cz = cos(degrees_to_radians(eulerXYZ[2] * 0.5));
763   double sz = sin(degrees_to_radians(eulerXYZ[2] * 0.5));
764
765   return Quaternion(
766     static_cast<float>(cx * sy * cz - sx * cy * sz),
767     static_cast<float>(cx * sy * sz + sx * cy * cz),
768     static_cast<float>(cx * cy * sz - sx * sy * cz),
769     static_cast<float>(cx * cy * cz + sx * sy * sz)
770   );
771 }
772
773 struct RotateDialog
774 {
775   GtkSpinButton* x;
776   GtkSpinButton* y;
777   GtkSpinButton* z;
778 };
779
780 static void rotatedlg_apply (GtkWidget *widget, RotateDialog* rotateDialog)
781 {
782   Vector3 eulerXYZ;
783
784   eulerXYZ[0] = static_cast<float>(gtk_spin_button_get_value(rotateDialog->x));
785   gtk_spin_button_set_value(rotateDialog->x, 0.0f); // reset to 0 on Apply
786   
787   eulerXYZ[1] = static_cast<float>(gtk_spin_button_get_value(rotateDialog->y));
788   gtk_spin_button_set_value(rotateDialog->y, 0.0f);
789   
790   eulerXYZ[2] = static_cast<float>(gtk_spin_button_get_value(rotateDialog->z));
791   gtk_spin_button_set_value(rotateDialog->z, 0.0f);
792
793   StringOutputStream command;
794   command << "rotateSelectedEulerXYZ -x " << eulerXYZ[0] << " -y " << eulerXYZ[1] << " -z " << eulerXYZ[2];
795   UndoableCommand undo(command.c_str());
796
797   GlobalSelectionSystem().rotateSelected(quaternion_for_euler_xyz_degrees(eulerXYZ));
798 }
799
800 void DoRotateDlg()
801 {
802   ModalDialog dialog;
803   RotateDialog rotateDialog;
804
805   GtkWindow* window = create_dialog_window(MainFrame_getWindow(), "Arbitrary rotation", G_CALLBACK(dialog_delete_callback), &dialog);
806
807   GtkAccelGroup* accel = gtk_accel_group_new();
808   gtk_window_add_accel_group(window, accel);
809
810   {
811     GtkHBox* hbox = create_dialog_hbox(4, 4);
812     gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
813     {
814       GtkTable* table = create_dialog_table(3, 2, 4, 4);
815       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(table), TRUE, TRUE, 0);
816       {
817         GtkWidget* label = gtk_label_new ("  X  ");
818         gtk_widget_show (label);
819         gtk_table_attach(table, label, 0, 1, 0, 1,
820                           (GtkAttachOptions) (0),
821                           (GtkAttachOptions) (0), 0, 0);
822       }
823       {
824         GtkWidget* label = gtk_label_new ("  Y  ");
825         gtk_widget_show (label);
826         gtk_table_attach(table, label, 0, 1, 1, 2,
827                           (GtkAttachOptions) (0),
828                           (GtkAttachOptions) (0), 0, 0);
829       }
830       {
831         GtkWidget* label = gtk_label_new ("  Z  ");
832         gtk_widget_show (label);
833         gtk_table_attach(table, label, 0, 1, 2, 3,
834                           (GtkAttachOptions) (0),
835                           (GtkAttachOptions) (0), 0, 0);
836       }
837       {
838         GtkAdjustment* adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -359, 359, 1, 10, 10));
839         GtkSpinButton* spin = GTK_SPIN_BUTTON(gtk_spin_button_new(adj, 1, 0));
840         gtk_widget_show(GTK_WIDGET(spin));
841         gtk_table_attach(table, GTK_WIDGET(spin), 1, 2, 0, 1,
842                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
843                           (GtkAttachOptions) (0), 0, 0);
844         gtk_widget_set_size_request(GTK_WIDGET(spin), 64, -1);
845         gtk_spin_button_set_wrap(spin, TRUE);
846
847         gtk_widget_grab_focus(GTK_WIDGET(spin));
848
849         rotateDialog.x = spin;
850       }
851       {
852         GtkAdjustment* adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -359, 359, 1, 10, 10));
853         GtkSpinButton* spin = GTK_SPIN_BUTTON(gtk_spin_button_new(adj, 1, 0));
854         gtk_widget_show(GTK_WIDGET(spin));
855         gtk_table_attach(table, GTK_WIDGET(spin), 1, 2, 1, 2,
856                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
857                           (GtkAttachOptions) (0), 0, 0);
858         gtk_widget_set_size_request(GTK_WIDGET(spin), 64, -1);
859         gtk_spin_button_set_wrap(spin, TRUE);
860
861         rotateDialog.y = spin;
862       }
863       {
864         GtkAdjustment* adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -359, 359, 1, 10, 10));
865         GtkSpinButton* spin = GTK_SPIN_BUTTON(gtk_spin_button_new(adj, 1, 0));
866         gtk_widget_show(GTK_WIDGET(spin));
867         gtk_table_attach(table, GTK_WIDGET(spin), 1, 2, 2, 3,
868                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
869                           (GtkAttachOptions) (0), 0, 0);
870         gtk_widget_set_size_request(GTK_WIDGET(spin), 64, -1);
871         gtk_spin_button_set_wrap(spin, TRUE);
872
873         rotateDialog.z = spin;
874       }
875     }
876     {
877       GtkVBox* vbox = create_dialog_vbox(4);
878       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
879       {
880         GtkButton* button = create_dialog_button("OK", G_CALLBACK(dialog_button_ok), &dialog);
881         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
882         widget_make_default(GTK_WIDGET(button));
883         gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
884       }
885       {
886         GtkButton* button = create_dialog_button("Cancel", G_CALLBACK(dialog_button_cancel), &dialog);
887         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
888         gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
889       }
890       {
891         GtkButton* button = create_dialog_button("Apply", G_CALLBACK(rotatedlg_apply), &rotateDialog);
892         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
893       }
894     }
895   }
896
897   if(modal_dialog_show(window, dialog) == eIDOK)
898   {
899     rotatedlg_apply(0, &rotateDialog);
900   }
901
902   gtk_widget_destroy(GTK_WIDGET(window));
903 }
904
905 void DoScaleDlg()
906 {
907   ModalDialog dialog;
908   GtkWidget* x;
909   GtkWidget* y;
910   GtkWidget* z;
911
912   GtkWindow* window = create_dialog_window(MainFrame_getWindow(), "Scale", G_CALLBACK(dialog_delete_callback), &dialog);
913
914   GtkAccelGroup* accel = gtk_accel_group_new();
915   gtk_window_add_accel_group(window, accel);
916
917   {
918     GtkHBox* hbox = create_dialog_hbox(4, 4);
919     gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
920     {
921       GtkTable* table = create_dialog_table(3, 2, 4, 4);
922       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(table), TRUE, TRUE, 0);
923       {
924         GtkWidget* label = gtk_label_new ("X:");
925         gtk_widget_show (label);
926         gtk_table_attach(table, label, 0, 1, 0, 1,
927                           (GtkAttachOptions) (GTK_FILL),
928                           (GtkAttachOptions) (0), 0, 0);
929         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
930       }
931       {
932         GtkWidget* label = gtk_label_new ("Y:");
933         gtk_widget_show (label);
934         gtk_table_attach(table, label, 0, 1, 1, 2,
935                           (GtkAttachOptions) (GTK_FILL),
936                           (GtkAttachOptions) (0), 0, 0);
937         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
938       }
939       {
940         GtkWidget* label = gtk_label_new ("Z:");
941         gtk_widget_show (label);
942         gtk_table_attach(table, label, 0, 1, 2, 3,
943                           (GtkAttachOptions) (GTK_FILL),
944                           (GtkAttachOptions) (0), 0, 0);
945         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
946       }
947       {
948         GtkWidget* entry = gtk_entry_new();
949         gtk_widget_show (entry);
950         gtk_table_attach(table, entry, 1, 2, 0, 1,
951                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
952                           (GtkAttachOptions) (0), 0, 0);
953
954         gtk_widget_grab_focus(entry);
955
956         x = entry;
957       }
958       {
959         GtkWidget* entry = gtk_entry_new();
960         gtk_widget_show (entry);
961         gtk_table_attach(table, entry, 1, 2, 1, 2,
962                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
963                           (GtkAttachOptions) (0), 0, 0);
964
965         y = entry;
966       }
967       {
968         GtkWidget* entry = gtk_entry_new();
969         gtk_widget_show (entry);
970         gtk_table_attach(table, entry, 1, 2, 2, 3,
971                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
972                           (GtkAttachOptions) (0), 0, 0);
973
974         z = entry;
975       }
976     }
977     {
978       GtkVBox* vbox = create_dialog_vbox(4);
979       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
980       {
981         GtkButton* button = create_dialog_button("OK", G_CALLBACK(dialog_button_ok), &dialog);
982         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
983         widget_make_default(GTK_WIDGET(button));
984         gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
985       }
986       {
987         GtkButton* button = create_dialog_button("Cancel", G_CALLBACK(dialog_button_cancel), &dialog);
988         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
989         gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
990       }
991     }
992   }
993
994   // Initialize dialog
995   gtk_entry_set_text (GTK_ENTRY (x), "1.0");
996   gtk_entry_set_text (GTK_ENTRY (y), "1.0");
997   gtk_entry_set_text (GTK_ENTRY (z), "1.0");
998
999   if(modal_dialog_show(window, dialog) == eIDOK)
1000   {
1001     float sx, sy, sz;
1002     sx = static_cast<float>(atof(gtk_entry_get_text (GTK_ENTRY (x))));
1003     sy = static_cast<float>(atof(gtk_entry_get_text (GTK_ENTRY (y))));
1004     sz = static_cast<float>(atof(gtk_entry_get_text (GTK_ENTRY (z))));
1005
1006     if (sx > 0 && sy > 0 && sz > 0)
1007     {
1008       StringOutputStream command;
1009       command << "scaleSelected -x " << sx << " -y " << sy << " -z " << sz;
1010       UndoableCommand undo(command.c_str());
1011
1012       Select_Scale(sx, sy, sz);
1013     }
1014     else
1015     {
1016       globalOutputStream() << "Warning.. Tried to scale by a zero value.";
1017     }
1018   }
1019
1020   gtk_widget_destroy(GTK_WIDGET(window));
1021 }