Bugfix: [#20406] reapeat duplication along axis+view transform orientation
[blender.git] / source / blender / editors / transform / transform.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34 #include <float.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifndef WIN32
41 #include <unistd.h>
42 #else
43 #include <io.h>
44 #endif
45
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_anim_types.h"
49 #include "DNA_armature_types.h"
50 #include "DNA_action_types.h"  /* for some special action-editor settings */
51 #include "DNA_constraint_types.h"
52 #include "DNA_ipo_types.h"              /* some silly ipo flag  */
53 #include "DNA_listBase.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_scene_types.h"            /* PET modes                    */
58 #include "DNA_screen_types.h"   /* area dimensions              */
59 #include "DNA_texture_types.h"
60 #include "DNA_userdef_types.h"
61 #include "DNA_view3d_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_windowmanager_types.h"
64
65 #include "RNA_access.h"
66
67 //#include "BIF_editview.h"             /* arrows_move_cursor   */
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70 //#include "BIF_mywindow.h"
71 //#include "BIF_resources.h"
72 //#include "BIF_screen.h"
73 //#include "BIF_space.h"                        /* undo                                 */
74 //#include "BIF_toets.h"                        /* persptoetsen                 */
75 //#include "BIF_mywindow.h"             /* warp_pointer                 */
76 //#include "BIF_toolbox.h"                      /* notice                               */
77 //#include "BIF_editmesh.h"
78 //#include "BIF_editsima.h"
79 //#include "BIF_editparticle.h"
80
81 #include "BKE_action.h"
82 #include "BKE_nla.h"
83 //#include "BKE_bad_level_calls.h"/* popmenu and error  */
84 #include "BKE_bmesh.h"
85 #include "BKE_context.h"
86 #include "BKE_constraint.h"
87 #include "BKE_global.h"
88 #include "BKE_particle.h"
89 #include "BKE_pointcache.h"
90 #include "BKE_utildefines.h"
91 #include "BKE_context.h"
92 #include "BKE_unit.h"
93
94 //#include "BSE_view.h"
95
96 #include "ED_image.h"
97 #include "ED_keyframing.h"
98 #include "ED_screen.h"
99 #include "ED_space_api.h"
100 #include "ED_markers.h"
101 #include "ED_util.h"
102 #include "ED_view3d.h"
103 #include "ED_mesh.h"
104
105 #include "UI_view2d.h"
106 #include "WM_types.h"
107 #include "WM_api.h"
108
109 #include "BLI_math.h"
110 #include "BLI_blenlib.h"
111 #include "BLI_editVert.h"
112 #include "BLI_ghash.h"
113 #include "BLI_linklist.h"
114
115 #include "PIL_time.h"                   /* sleep                                */
116
117 #include "UI_resources.h"
118
119 //#include "blendef.h"
120 //
121 //#include "mydevice.h"
122
123 #include "transform.h"
124
125 /* ************************** SPACE DEPENDANT CODE **************************** */
126
127 void setTransformViewMatrices(TransInfo *t)
128 {
129         if(t->spacetype==SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
130                 RegionView3D *rv3d = t->ar->regiondata;
131
132                 copy_m4_m4(t->viewmat, rv3d->viewmat);
133                 copy_m4_m4(t->viewinv, rv3d->viewinv);
134                 copy_m4_m4(t->persmat, rv3d->persmat);
135                 copy_m4_m4(t->persinv, rv3d->persinv);
136                 t->persp = rv3d->persp;
137         }
138         else {
139                 unit_m4(t->viewmat);
140                 unit_m4(t->viewinv);
141                 unit_m4(t->persmat);
142                 unit_m4(t->persinv);
143                 t->persp = RV3D_ORTHO;
144         }
145
146         calculateCenter2D(t);
147 }
148
149 void convertViewVec(TransInfo *t, float *vec, short dx, short dy)
150 {
151         if (t->spacetype==SPACE_VIEW3D) {
152                 if (t->ar->regiontype == RGN_TYPE_WINDOW)
153                 {
154                         window_to_3d_delta(t->ar, vec, dx, dy);
155                 }
156         }
157         else if(t->spacetype==SPACE_IMAGE) {
158                 View2D *v2d = t->view;
159                 float divx, divy, aspx, aspy;
160
161                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
162
163                 divx= v2d->mask.xmax-v2d->mask.xmin;
164                 divy= v2d->mask.ymax-v2d->mask.ymin;
165
166                 vec[0]= aspx*(v2d->cur.xmax-v2d->cur.xmin)*(dx)/divx;
167                 vec[1]= aspy*(v2d->cur.ymax-v2d->cur.ymin)*(dy)/divy;
168                 vec[2]= 0.0f;
169         }
170         else if(ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
171                 View2D *v2d = t->view;
172                 float divx, divy;
173
174                 divx= v2d->mask.xmax-v2d->mask.xmin;
175                 divy= v2d->mask.ymax-v2d->mask.ymin;
176
177                 vec[0]= (v2d->cur.xmax-v2d->cur.xmin)*(dx) / (divx);
178                 vec[1]= (v2d->cur.ymax-v2d->cur.ymin)*(dy) / (divy);
179                 vec[2]= 0.0f;
180         }
181         else if(t->spacetype==SPACE_NODE) {
182                 View2D *v2d = &t->ar->v2d;
183                 float divx, divy;
184
185                 divx= v2d->mask.xmax-v2d->mask.xmin;
186                 divy= v2d->mask.ymax-v2d->mask.ymin;
187
188                 vec[0]= (v2d->cur.xmax-v2d->cur.xmin)*(dx)/divx;
189                 vec[1]= (v2d->cur.ymax-v2d->cur.ymin)*(dy)/divy;
190                 vec[2]= 0.0f;
191         }
192         else if(t->spacetype==SPACE_SEQ) {
193                 View2D *v2d = &t->ar->v2d;
194                 float divx, divy;
195
196                 divx= v2d->mask.xmax-v2d->mask.xmin;
197                 divy= v2d->mask.ymax-v2d->mask.ymin;
198
199                 vec[0]= (v2d->cur.xmax-v2d->cur.xmin)*(dx)/divx;
200                 vec[1]= (v2d->cur.ymax-v2d->cur.ymin)*(dy)/divy;
201                 vec[2]= 0.0f;
202         }
203 }
204
205 void projectIntView(TransInfo *t, float *vec, int *adr)
206 {
207         if (t->spacetype==SPACE_VIEW3D) {
208                 if(t->ar->regiontype == RGN_TYPE_WINDOW)
209                         project_int_noclip(t->ar, vec, adr);
210         }
211         else if(t->spacetype==SPACE_IMAGE) {
212                 float aspx, aspy, v[2];
213
214                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
215                 v[0]= vec[0]/aspx;
216                 v[1]= vec[1]/aspy;
217
218                 UI_view2d_to_region_no_clip(t->view, v[0], v[1], adr, adr+1);
219         }
220         else if(ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
221                 int out[2] = {0, 0};
222
223                 UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], out, out+1);
224                 adr[0]= out[0];
225                 adr[1]= out[1];
226         }
227         else if(t->spacetype==SPACE_SEQ) { /* XXX not tested yet, but should work */
228                 int out[2] = {0, 0};
229
230                 UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], out, out+1);
231                 adr[0]= out[0];
232                 adr[1]= out[1];
233         }
234 }
235
236 void projectFloatView(TransInfo *t, float *vec, float *adr)
237 {
238         if (t->spacetype==SPACE_VIEW3D) {
239                 if(t->ar->regiontype == RGN_TYPE_WINDOW)
240                         project_float_noclip(t->ar, vec, adr);
241         }
242         else if(t->spacetype==SPACE_IMAGE) {
243                 int a[2];
244
245                 projectIntView(t, vec, a);
246                 adr[0]= a[0];
247                 adr[1]= a[1];
248         }
249         else if(ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
250                 int a[2];
251
252                 projectIntView(t, vec, a);
253                 adr[0]= a[0];
254                 adr[1]= a[1];
255         }
256 }
257
258 void applyAspectRatio(TransInfo *t, float *vec)
259 {
260         SpaceImage *sima= t->sa->spacedata.first;
261
262         if ((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
263                 float aspx, aspy;
264
265                 if((sima->flag & SI_COORDFLOATS)==0) {
266                         int width, height;
267                         ED_space_image_size(sima, &width, &height);
268
269                         vec[0] *= width;
270                         vec[1] *= height;
271                 }
272
273                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
274                 vec[0] /= aspx;
275                 vec[1] /= aspy;
276         }
277 }
278
279 void removeAspectRatio(TransInfo *t, float *vec)
280 {
281         SpaceImage *sima= t->sa->spacedata.first;
282
283         if ((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
284                 float aspx, aspy;
285
286                 if((sima->flag & SI_COORDFLOATS)==0) {
287                         int width, height;
288                         ED_space_image_size(sima, &width, &height);
289
290                         vec[0] /= width;
291                         vec[1] /= height;
292                 }
293
294                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
295                 vec[0] *= aspx;
296                 vec[1] *= aspy;
297         }
298 }
299
300 static void viewRedrawForce(bContext *C, TransInfo *t)
301 {
302         if (t->spacetype == SPACE_VIEW3D)
303         {
304                 /* Do we need more refined tags? */
305                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
306                 
307                 /* for realtime animation record - send notifiers recognised by animation editors */
308                 if ((t->animtimer) && IS_AUTOKEY_ON(t->scene))
309                         WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
310         }
311         else if (t->spacetype == SPACE_ACTION) {
312                 //SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
313                 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL);
314         }
315         else if (t->spacetype == SPACE_IPO) {
316                 //SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
317                 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL);
318         }
319         else if (t->spacetype == SPACE_NLA) {
320                 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA_EDIT, NULL);
321         }
322         else if(t->spacetype == SPACE_NODE)
323         {
324                 //ED_area_tag_redraw(t->sa);
325                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE_VIEW, NULL);
326         }
327         else if(t->spacetype == SPACE_SEQ)
328         {
329                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, NULL);
330         }
331         else if (t->spacetype==SPACE_IMAGE) {
332                 // XXX how to deal with lock?
333                 SpaceImage *sima= (SpaceImage*)t->sa->spacedata.first;
334                 if(sima->lock) WM_event_add_notifier(C, NC_GEOM|ND_DATA, t->obedit->data);
335                 else ED_area_tag_redraw(t->sa);
336         }
337 }
338
339 static void viewRedrawPost(TransInfo *t)
340 {
341         ED_area_headerprint(t->sa, NULL);
342
343 #if 0 // TRANSFORM_FIX_ME
344         if(t->spacetype==SPACE_VIEW3D) {
345                 allqueue(REDRAWBUTSOBJECT, 0);
346                 allqueue(REDRAWVIEW3D, 0);
347         }
348         else if(t->spacetype==SPACE_IMAGE) {
349                 allqueue(REDRAWIMAGE, 0);
350                 allqueue(REDRAWVIEW3D, 0);
351         }
352         else if(ELEM3(t->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_IPO)) {
353                 allqueue(REDRAWVIEW3D, 0);
354                 allqueue(REDRAWACTION, 0);
355                 allqueue(REDRAWNLA, 0);
356                 allqueue(REDRAWIPO, 0);
357                 allqueue(REDRAWTIME, 0);
358                 allqueue(REDRAWBUTSOBJECT, 0);
359         }
360
361         scrarea_queue_headredraw(curarea);
362 #endif
363 }
364
365 /* ************************** TRANSFORMATIONS **************************** */
366
367 void BIF_selectOrientation() {
368 #if 0 // TRANSFORM_FIX_ME
369         short val;
370         char *str_menu = BIF_menustringTransformOrientation("Orientation");
371         val= pupmenu(str_menu);
372         MEM_freeN(str_menu);
373
374         if(val >= 0) {
375                 G.vd->twmode = val;
376         }
377 #endif
378 }
379
380 static void view_editmove(unsigned short event)
381 {
382 #if 0 // TRANSFORM_FIX_ME
383         int refresh = 0;
384         /* Regular:   Zoom in */
385         /* Shift:     Scroll up */
386         /* Ctrl:      Scroll right */
387         /* Alt-Shift: Rotate up */
388         /* Alt-Ctrl:  Rotate right */
389
390         /* only work in 3D window for now
391          * In the end, will have to send to event to a 2D window handler instead
392          */
393         if (Trans.flag & T_2D_EDIT)
394                 return;
395
396         switch(event) {
397                 case WHEELUPMOUSE:
398
399                         if( G.qual & LR_SHIFTKEY ) {
400                                 if( G.qual & LR_ALTKEY ) {
401                                         G.qual &= ~LR_SHIFTKEY;
402                                         persptoetsen(PAD2);
403                                         G.qual |= LR_SHIFTKEY;
404                                 } else {
405                                         persptoetsen(PAD2);
406                                 }
407                         } else if( G.qual & LR_CTRLKEY ) {
408                                 if( G.qual & LR_ALTKEY ) {
409                                         G.qual &= ~LR_CTRLKEY;
410                                         persptoetsen(PAD4);
411                                         G.qual |= LR_CTRLKEY;
412                                 } else {
413                                         persptoetsen(PAD4);
414                                 }
415                         } else if(U.uiflag & USER_WHEELZOOMDIR)
416                                 persptoetsen(PADMINUS);
417                         else
418                                 persptoetsen(PADPLUSKEY);
419
420                         refresh = 1;
421                         break;
422                 case WHEELDOWNMOUSE:
423                         if( G.qual & LR_SHIFTKEY ) {
424                                 if( G.qual & LR_ALTKEY ) {
425                                         G.qual &= ~LR_SHIFTKEY;
426                                         persptoetsen(PAD8);
427                                         G.qual |= LR_SHIFTKEY;
428                                 } else {
429                                         persptoetsen(PAD8);
430                                 }
431                         } else if( G.qual & LR_CTRLKEY ) {
432                                 if( G.qual & LR_ALTKEY ) {
433                                         G.qual &= ~LR_CTRLKEY;
434                                         persptoetsen(PAD6);
435                                         G.qual |= LR_CTRLKEY;
436                                 } else {
437                                         persptoetsen(PAD6);
438                                 }
439                         } else if(U.uiflag & USER_WHEELZOOMDIR)
440                                 persptoetsen(PADPLUSKEY);
441                         else
442                                 persptoetsen(PADMINUS);
443
444                         refresh = 1;
445                         break;
446         }
447
448         if (refresh)
449                 setTransformViewMatrices(&Trans);
450 #endif
451 }
452
453 #if 0
454 static char *transform_to_undostr(TransInfo *t)
455 {
456         switch (t->mode) {
457                 case TFM_TRANSLATION:
458                         return "Translate";
459                 case TFM_ROTATION:
460                         return "Rotate";
461                 case TFM_RESIZE:
462                         return "Scale";
463                 case TFM_TOSPHERE:
464                         return "To Sphere";
465                 case TFM_SHEAR:
466                         return "Shear";
467                 case TFM_WARP:
468                         return "Warp";
469                 case TFM_SHRINKFATTEN:
470                         return "Shrink/Fatten";
471                 case TFM_TILT:
472                         return "Tilt";
473                 case TFM_TRACKBALL:
474                         return "Trackball";
475                 case TFM_PUSHPULL:
476                         return "Push/Pull";
477                 case TFM_BEVEL:
478                         return "Bevel";
479                 case TFM_BWEIGHT:
480                         return "Bevel Weight";
481                 case TFM_CREASE:
482                         return "Crease";
483                 case TFM_BONESIZE:
484                         return "Bone Width";
485                 case TFM_BONE_ENVELOPE:
486                         return "Bone Envelope";
487                 case TFM_TIME_TRANSLATE:
488                         return "Translate Anim. Data";
489                 case TFM_TIME_SCALE:
490                         return "Scale Anim. Data";
491                 case TFM_TIME_SLIDE:
492                         return "Time Slide";
493                 case TFM_BAKE_TIME:
494                         return "Key Time";
495                 case TFM_MIRROR:
496                         return "Mirror";
497         }
498         return "Transform";
499 }
500 #endif
501
502 /* ************************************************* */
503
504 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
505 #define TFM_MODAL_CANCEL                1
506 #define TFM_MODAL_CONFIRM               2
507 #define TFM_MODAL_TRANSLATE             3
508 #define TFM_MODAL_ROTATE                4
509 #define TFM_MODAL_RESIZE                5
510 #define TFM_MODAL_SNAP_INV_ON   6
511 #define TFM_MODAL_SNAP_INV_OFF  7
512 #define TFM_MODAL_SNAP_TOGGLE   8
513 #define TFM_MODAL_AXIS_X                9
514 #define TFM_MODAL_AXIS_Y                10
515 #define TFM_MODAL_AXIS_Z                11
516 #define TFM_MODAL_PLANE_X               12
517 #define TFM_MODAL_PLANE_Y               13
518 #define TFM_MODAL_PLANE_Z               14
519 #define TFM_MODAL_CONS_OFF              15
520 #define TFM_MODAL_ADD_SNAP              16
521 #define TFM_MODAL_REMOVE_SNAP   17
522
523 /* called in transform_ops.c, on each regeneration of keymaps */
524 void transform_modal_keymap(wmKeyConfig *keyconf)
525 {
526         static EnumPropertyItem modal_items[] = {
527         {TFM_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
528         {TFM_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
529         {TFM_MODAL_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
530         {TFM_MODAL_ROTATE, "ROTATE", 0, "Rotate", ""},
531         {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
532         {TFM_MODAL_SNAP_INV_ON, "SNAP_INV_ON", 0, "Invert Snap On", ""},
533         {TFM_MODAL_SNAP_INV_OFF, "SNAP_INV_OFF", 0, "Invert Snap Off", ""},
534         {TFM_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap Toggle", ""},
535         {TFM_MODAL_AXIS_X, "AXIS_X", 0, "Orientation X axis", ""},
536         {TFM_MODAL_AXIS_Y, "AXIS_Y", 0, "Orientation Y axis", ""},
537         {TFM_MODAL_AXIS_Z, "AXIS_Z", 0, "Orientation Z axis", ""},
538         {TFM_MODAL_PLANE_X, "PLANE_X", 0, "Orientation X plane", ""},
539         {TFM_MODAL_PLANE_Y, "PLANE_Y", 0, "Orientation Y plane", ""},
540         {TFM_MODAL_PLANE_Z, "PLANE_Z", 0, "Orientation Z plane", ""},
541         {TFM_MODAL_CONS_OFF, "CONS_OFF", 0, "Remove Constraints", ""},
542         {TFM_MODAL_ADD_SNAP, "ADD_SNAP", 0, "Add Snap Point", ""},
543         {TFM_MODAL_REMOVE_SNAP, "REMOVE_SNAP", 0, "Remove Last Snap Point", ""},
544         {0, NULL, 0, NULL, NULL}};
545         
546         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Transform Modal Map");
547         
548         /* this function is called for each spacetype, only needs to add map once */
549         if(keymap) return;
550         
551         keymap= WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
552         
553         /* items for modal map */
554         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CANCEL);
555         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
556         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
557         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
558
559         WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, TFM_MODAL_TRANSLATE);
560         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, TFM_MODAL_ROTATE);
561         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, TFM_MODAL_RESIZE);
562         
563         WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_SNAP_TOGGLE);
564
565         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
566         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
567         
568         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, TFM_MODAL_ADD_SNAP);
569         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ALT, 0, TFM_MODAL_REMOVE_SNAP);
570
571         /* assign map to operators */
572         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_transform");
573         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_translate");
574         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_rotate");
575         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_tosphere");
576         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_resize");
577         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_shear");
578         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_warp");
579         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_shrink_fatten");
580         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_tilt");
581         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_trackball");
582         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_mirror");
583         WM_modalkeymap_assign(keymap, "TRANSFORM_OT_edge_slide");
584 }
585
586
587 int transformEvent(TransInfo *t, wmEvent *event)
588 {
589         float mati[3][3] = {{1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
590         char cmode = constraintModeToChar(t);
591         int handled = 1;
592
593         t->redraw |= handleMouseInput(t, &t->mouse, event);
594
595         if (event->type == MOUSEMOVE)
596         {
597                 t->mval[0] = event->x - t->ar->winrct.xmin;
598                 t->mval[1] = event->y - t->ar->winrct.ymin;
599
600                 t->redraw = 1;
601
602                 if (t->state == TRANS_STARTING) {
603                     t->state = TRANS_RUNNING;
604                 }
605
606                 applyMouseInput(t, &t->mouse, t->mval, t->values);
607         }
608
609         /* handle modal keymap first */
610         if (event->type == EVT_MODAL_MAP) {
611                 switch (event->val) {
612                         case TFM_MODAL_CANCEL:
613                                 t->state = TRANS_CANCEL;
614                                 break;
615                         case TFM_MODAL_CONFIRM:
616                                 t->state = TRANS_CONFIRM;
617                                 break;
618                         case TFM_MODAL_TRANSLATE:
619                                 /* only switch when... */
620                                 if( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
621                                         resetTransRestrictions(t);
622                                         restoreTransObjects(t);
623                                         initTranslation(t);
624                                         initSnapping(t, NULL); // need to reinit after mode change
625                                         t->redraw = 1;
626                                 }
627                                 break;
628                         case TFM_MODAL_ROTATE:
629                                 /* only switch when... */
630                                 if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
631                                         
632                                         resetTransRestrictions(t);
633                                         
634                                         if (t->mode == TFM_ROTATION) {
635                                                 restoreTransObjects(t);
636                                                 initTrackball(t);
637                                         }
638                                         else {
639                                                 restoreTransObjects(t);
640                                                 initRotation(t);
641                                         }
642                                         initSnapping(t, NULL); // need to reinit after mode change
643                                         t->redraw = 1;
644                                 }
645                                 break;
646                         case TFM_MODAL_RESIZE:
647                                 /* only switch when... */
648                                 if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
649                                         resetTransRestrictions(t);
650                                         restoreTransObjects(t);
651                                         initResize(t);
652                                         initSnapping(t, NULL); // need to reinit after mode change
653                                         t->redraw = 1;
654                                 }
655                                 break;
656                                 
657                         case TFM_MODAL_SNAP_INV_ON:
658                                 t->modifiers |= MOD_SNAP_INVERT;
659                                 t->redraw = 1;
660                                 break;
661                         case TFM_MODAL_SNAP_INV_OFF:
662                                 t->modifiers &= ~MOD_SNAP_INVERT;
663                                 t->redraw = 1;
664                                 break;
665                         case TFM_MODAL_SNAP_TOGGLE:
666                                 t->modifiers ^= MOD_SNAP;
667                                 t->redraw = 1;
668                                 break;
669                         case TFM_MODAL_AXIS_X:
670                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
671                                         if (cmode == 'X') {
672                                                 stopConstraint(t);
673                                         }
674                                         else {
675                                                 if (t->flag & T_2D_EDIT) {
676                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
677                                                 }
678                                                 else {
679                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS0), "along %s X");
680                                                 }
681                                         }
682                                         t->redraw = 1;
683                                 }
684                                 break;
685                         case TFM_MODAL_AXIS_Y:
686                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
687                                         if (cmode == 'Y') {
688                                                 stopConstraint(t);
689                                         }
690                                         else {
691                                                 if (t->flag & T_2D_EDIT) {
692                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
693                                                 }
694                                                 else {
695                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS1), "along %s Y");
696                                                 }
697                                         }
698                                         t->redraw = 1;
699                                 }
700                                 break;
701                         case TFM_MODAL_AXIS_Z:
702                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
703                                         if (cmode == 'Z') {
704                                                 stopConstraint(t);
705                                         }
706                                         else {
707                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS2), "along %s Z");
708                                         }
709                                         t->redraw = 1;
710                                 }
711                                 break;
712                         case TFM_MODAL_PLANE_X:
713                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
714                                         if (cmode == 'X') {
715                                                 stopConstraint(t);
716                                         }
717                                         else {
718                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
719                                         }
720                                         t->redraw = 1;
721                                 }
722                                 break;
723                         case TFM_MODAL_PLANE_Y:
724                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
725                                         if (cmode == 'Y') {
726                                                 stopConstraint(t);
727                                         }
728                                         else {
729                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
730                                         }
731                                         t->redraw = 1;
732                                 }
733                                 break;
734                         case TFM_MODAL_PLANE_Z:
735                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
736                                         if (cmode == 'Z') {
737                                                 stopConstraint(t);
738                                         }
739                                         else {
740                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
741                                         }
742                                         t->redraw = 1;
743                                 }
744                                 break;
745                         case TFM_MODAL_CONS_OFF:
746                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
747                                         stopConstraint(t);
748                                         t->redraw = 1;
749                                 }
750                                 break;
751                         case TFM_MODAL_ADD_SNAP:
752                                 addSnapPoint(t);
753                                 t->redraw = 1;
754                                 break;
755                         case TFM_MODAL_REMOVE_SNAP:
756                                 removeSnapPoint(t);
757                                 t->redraw = 1;
758                                 break;
759                         default:
760                                 handled = 0;
761                                 break;
762                 }
763         }
764         /* else do non-mapped events */
765         else if (event->val==KM_PRESS) {
766                 switch (event->type){
767                 case RIGHTMOUSE:
768                         t->state = TRANS_CANCEL;
769                         break;
770                 /* enforce redraw of transform when modifiers are used */
771                 case LEFTSHIFTKEY:
772                 case RIGHTSHIFTKEY:
773                         t->modifiers |= MOD_CONSTRAINT_PLANE;
774                         t->redraw = 1;
775                         break;
776
777                 case SPACEKEY:
778                         if ((t->spacetype==SPACE_VIEW3D) && event->alt) {
779 #if 0 // TRANSFORM_FIX_ME
780                                 short mval[2];
781
782                                 getmouseco_sc(mval);
783                                 BIF_selectOrientation();
784                                 calc_manipulator_stats(curarea);
785                                 copy_m3_m4(t->spacemtx, G.vd->twmat);
786                                 warp_pointer(mval[0], mval[1]);
787 #endif
788                         }
789                         else {
790                                 t->state = TRANS_CONFIRM;
791                         }
792                         break;
793
794                 case MIDDLEMOUSE:
795                         if ((t->flag & T_NO_CONSTRAINT)==0) {
796                                 /* exception for switching to dolly, or trackball, in camera view */
797                                 if (t->flag & T_CAMERA) {
798                                         if (t->mode==TFM_TRANSLATION)
799                                                 setLocalConstraint(t, (CON_AXIS2), "along local Z");
800                                         else if (t->mode==TFM_ROTATION) {
801                                                 restoreTransObjects(t);
802                                                 initTrackball(t);
803                                         }
804                                 }
805                                 else {
806                                         t->modifiers |= MOD_CONSTRAINT_SELECT;
807                                         if (t->con.mode & CON_APPLY) {
808                                                 stopConstraint(t);
809                                         }
810                                         else {
811                                                 if (event->shift) {
812                                                         initSelectConstraint(t, t->spacemtx);
813                                                 }
814                                                 else {
815                                                         /* bit hackish... but it prevents mmb select to print the orientation from menu */
816                                                         strcpy(t->spacename, "global");
817                                                         initSelectConstraint(t, mati);
818                                                 }
819                                                 postSelectConstraint(t);
820                                         }
821                                 }
822                                 t->redraw = 1;
823                         }
824                         break;
825                 case ESCKEY:
826                         t->state = TRANS_CANCEL;
827                         break;
828                 case PADENTER:
829                 case RETKEY:
830                         t->state = TRANS_CONFIRM;
831                         break;
832                 case GKEY:
833                         /* only switch when... */
834                         if( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
835                                 resetTransRestrictions(t);
836                                 restoreTransObjects(t);
837                                 initTranslation(t);
838                                 initSnapping(t, NULL); // need to reinit after mode change
839                                 t->redraw = 1;
840                         }
841                         break;
842                 case SKEY:
843                         /* only switch when... */
844                         if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
845                                 resetTransRestrictions(t);
846                                 restoreTransObjects(t);
847                                 initResize(t);
848                                 initSnapping(t, NULL); // need to reinit after mode change
849                                 t->redraw = 1;
850                         }
851                         break;
852                 case RKEY:
853                         /* only switch when... */
854                         if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
855
856                                 resetTransRestrictions(t);
857
858                                 if (t->mode == TFM_ROTATION) {
859                                         restoreTransObjects(t);
860                                         initTrackball(t);
861                                 }
862                                 else {
863                                         restoreTransObjects(t);
864                                         initRotation(t);
865                                 }
866                                 initSnapping(t, NULL); // need to reinit after mode change
867                                 t->redraw = 1;
868                         }
869                         break;
870                 case CKEY:
871                         if (event->alt) {
872                                 t->flag ^= T_PROP_CONNECTED;
873                                 sort_trans_data_dist(t);
874                                 calculatePropRatio(t);
875                                 t->redraw= 1;
876                         }
877                         else {
878                                 stopConstraint(t);
879                                 t->redraw = 1;
880                         }
881                         break;
882                 case XKEY:
883                         if ((t->flag & T_NO_CONSTRAINT)==0) {
884                                 if (t->flag & T_2D_EDIT) {
885                                         if (cmode == 'X') {
886                                                 stopConstraint(t);
887                                         } else {
888                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
889                                         }
890                                 } else {
891                                         if (cmode == 'X') {
892                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
893                                                         stopConstraint(t);
894                                                 } else {
895                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
896                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
897                                                                 setUserConstraint(t, orientation, (CON_AXIS0), "along %s X");
898                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
899                                                                 setUserConstraint(t, orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
900                                                 }
901                                         } else {
902                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
903                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along %s X");
904                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
905                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1|CON_AXIS2), "locking %s X");
906                                         }
907                                 }
908                                 t->redraw = 1;
909                         }
910                         break;
911                 case YKEY:
912                         if ((t->flag & T_NO_CONSTRAINT)==0) {
913                                 if (t->flag & T_2D_EDIT) {
914                                         if (cmode == 'Y') {
915                                                 stopConstraint(t);
916                                         } else {
917                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
918                                         }
919                                 } else {
920                                         if (cmode == 'Y') {
921                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
922                                                         stopConstraint(t);
923                                                 } else {
924                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
925                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
926                                                                 setUserConstraint(t, orientation, (CON_AXIS1), "along %s Y");
927                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
928                                                                 setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
929                                                 }
930                                         } else {
931                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
932                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along %s Y");
933                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
934                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS2), "locking %s Y");
935                                         }
936                                 }
937                                 t->redraw = 1;
938                         }
939                         break;
940                 case ZKEY:
941                         if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))==0) {
942                                 if (cmode == 'Z') {
943                                         if (t->con.orientation != V3D_MANIP_GLOBAL) {
944                                                 stopConstraint(t);
945                                         } else {
946                                                 short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
947                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
948                                                         setUserConstraint(t, orientation, (CON_AXIS2), "along %s Z");
949                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
950                                                         setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
951                                         }
952                                 } else {
953                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
954                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS2), "along %s Z");
955                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
956                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS1), "locking %s Z");
957                                 }
958                                 t->redraw = 1;
959                         }
960                         break;
961                 case OKEY:
962                         if (t->flag & T_PROP_EDIT && event->shift) {
963                                 t->prop_mode = (t->prop_mode + 1) % 6;
964                                 calculatePropRatio(t);
965                                 t->redraw = 1;
966                         }
967                         break;
968                 case PADPLUSKEY:
969                         if(event->alt && t->flag & T_PROP_EDIT) {
970                                 t->prop_size *= 1.1f;
971                                 calculatePropRatio(t);
972                         }
973                         t->redraw= 1;
974                         break;
975                 case PAGEUPKEY:
976                 case WHEELDOWNMOUSE:
977                         if (t->flag & T_AUTOIK) {
978                                 transform_autoik_update(t, 1);
979                         }
980                         else if(t->flag & T_PROP_EDIT) {
981                                 t->prop_size*= 1.1f;
982                                 calculatePropRatio(t);
983                         }
984                         else view_editmove(event->type);
985                         t->redraw= 1;
986                         break;
987                 case PADMINUS:
988                         if(event->alt && t->flag & T_PROP_EDIT) {
989                                 t->prop_size*= 0.90909090f;
990                                 calculatePropRatio(t);
991                         }
992                         t->redraw= 1;
993                         break;
994                 case PAGEDOWNKEY:
995                 case WHEELUPMOUSE:
996                         if (t->flag & T_AUTOIK) {
997                                 transform_autoik_update(t, -1);
998                         }
999                         else if (t->flag & T_PROP_EDIT) {
1000                                 t->prop_size*= 0.90909090f;
1001                                 calculatePropRatio(t);
1002                         }
1003                         else view_editmove(event->type);
1004                         t->redraw= 1;
1005                         break;
1006 //              case NDOFMOTION:
1007 //            viewmoveNDOF(1);
1008   //         break;
1009                 default:
1010                         handled = 0;
1011                         break;
1012                 }
1013
1014                 // Numerical input events
1015                 t->redraw |= handleNumInput(&(t->num), event);
1016
1017                 // NDof input events
1018                 switch(handleNDofInput(&(t->ndof), event))
1019                 {
1020                         case NDOF_CONFIRM:
1021                                 if ((t->options & CTX_NDOF) == 0)
1022                                 {
1023                                         /* Confirm on normal transform only */
1024                                         t->state = TRANS_CONFIRM;
1025                                 }
1026                                 break;
1027                         case NDOF_CANCEL:
1028                                 if (t->options & CTX_NDOF)
1029                                 {
1030                                         /* Cancel on pure NDOF transform */
1031                                         t->state = TRANS_CANCEL;
1032                                 }
1033                                 else
1034                                 {
1035                                         /* Otherwise, just redraw, NDof input was cancelled */
1036                                         t->redraw = 1;
1037                                 }
1038                                 break;
1039                         case NDOF_NOMOVE:
1040                                 if (t->options & CTX_NDOF)
1041                                 {
1042                                         /* Confirm on pure NDOF transform */
1043                                         t->state = TRANS_CONFIRM;
1044                                 }
1045                                 break;
1046                         case NDOF_REFRESH:
1047                                 t->redraw = 1;
1048                                 break;
1049                         default:
1050                                 handled = 0;
1051                                 break;
1052                 }
1053
1054                 // Snapping events
1055                 t->redraw |= handleSnapping(t, event);
1056
1057                 //arrows_move_cursor(event->type);
1058         }
1059         else if (event->val==KM_RELEASE) {
1060                 switch (event->type){
1061                 case LEFTSHIFTKEY:
1062                 case RIGHTSHIFTKEY:
1063                         t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1064                         t->redraw = 1;
1065                         break;
1066
1067                 case MIDDLEMOUSE:
1068                         if ((t->flag & T_NO_CONSTRAINT)==0) {
1069                                 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1070                                 postSelectConstraint(t);
1071                                 t->redraw = 1;
1072                         }
1073                         break;
1074 //              case LEFTMOUSE:
1075 //              case RIGHTMOUSE:
1076 //                      if(WM_modal_tweak_exit(event, t->event_type))
1077 ////                    if (t->options & CTX_TWEAK)
1078 //                              t->state = TRANS_CONFIRM;
1079 //                      break;
1080                 default:
1081                         handled = 0;
1082                         break;
1083                 }
1084
1085                 /* confirm transform if launch key is released after mouse move */
1086                 /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1087                 if (event->type == LEFTMOUSE /*t->launch_event*/ && t->state != TRANS_STARTING)
1088                 {
1089                         t->state = TRANS_CONFIRM;
1090                 }
1091         }
1092
1093         // Per transform event, if present
1094         if (t->handleEvent)
1095                 t->redraw |= t->handleEvent(t, event);
1096
1097         if (handled || t->redraw)
1098                 return 0;
1099         else
1100                 return OPERATOR_PASS_THROUGH;
1101 }
1102
1103 int calculateTransformCenter(bContext *C, wmEvent *event, int centerMode, float *vec)
1104 {
1105         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1106         int success = 1;
1107
1108         t->state = TRANS_RUNNING;
1109
1110         t->options = CTX_NONE;
1111
1112         t->mode = TFM_DUMMY;
1113
1114         initTransInfo(C, t, NULL, event);                                       // internal data, mouse, vectors
1115
1116         createTransData(C, t);                  // make TransData structs from selection
1117
1118         t->around = centerMode;                         // override userdefined mode
1119
1120         if (t->total == 0) {
1121                 success = 0;
1122         }
1123         else {
1124                 success = 1;
1125
1126                 calculateCenter(t);
1127
1128                 // Copy center from constraint center. Transform center can be local
1129                 VECCOPY(vec, t->con.center);
1130         }
1131
1132
1133         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1134         special_aftertrans_update(C, t);
1135
1136         postTrans(C, t);
1137
1138         MEM_freeN(t);
1139
1140         return success;
1141 }
1142
1143 typedef enum {
1144         UP,
1145         DOWN,
1146         LEFT,
1147         RIGHT
1148 } ArrowDirection;
1149 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1150 {
1151         switch(d)
1152         {
1153                 case LEFT:
1154                         offset = -offset;
1155                         length = -length;
1156                         size = -size;
1157                 case RIGHT:
1158                         glBegin(GL_LINES);
1159                         glVertex2s( offset, 0);
1160                         glVertex2s( offset + length, 0);
1161                         glVertex2s( offset + length, 0);
1162                         glVertex2s( offset + length - size, -size);
1163                         glVertex2s( offset + length, 0);
1164                         glVertex2s( offset + length - size,  size);
1165                         glEnd();
1166                         break;
1167                 case DOWN:
1168                         offset = -offset;
1169                         length = -length;
1170                         size = -size;
1171                 case UP:
1172                         glBegin(GL_LINES);
1173                         glVertex2s( 0, offset);
1174                         glVertex2s( 0, offset + length);
1175                         glVertex2s( 0, offset + length);
1176                         glVertex2s(-size, offset + length - size);
1177                         glVertex2s( 0, offset + length);
1178                         glVertex2s( size, offset + length - size);
1179                         glEnd();
1180                         break;
1181         }
1182 }
1183
1184 static void drawArrowHead(ArrowDirection d, short size)
1185 {
1186         switch(d)
1187         {
1188                 case LEFT:
1189                         size = -size;
1190                 case RIGHT:
1191                         glBegin(GL_LINES);
1192                         glVertex2s( 0, 0);
1193                         glVertex2s( -size, -size);
1194                         glVertex2s( 0, 0);
1195                         glVertex2s( -size,  size);
1196                         glEnd();
1197                         break;
1198                 case DOWN:
1199                         size = -size;
1200                 case UP:
1201                         glBegin(GL_LINES);
1202                         glVertex2s( 0, 0);
1203                         glVertex2s(-size, -size);
1204                         glVertex2s( 0, 0);
1205                         glVertex2s( size, -size);
1206                         glEnd();
1207                         break;
1208         }
1209 }
1210
1211 static void drawArc(float size, float angle_start, float angle_end, int segments)
1212 {
1213         float delta = (angle_end - angle_start) / segments;
1214         float angle;
1215
1216         glBegin(GL_LINE_STRIP);
1217
1218         for( angle = angle_start; angle < angle_end; angle += delta)
1219         {
1220                 glVertex2f( cosf(angle) * size, sinf(angle) * size);
1221         }
1222         glVertex2f( cosf(angle_end) * size, sinf(angle_end) * size);
1223
1224         glEnd();
1225 }
1226
1227 static void drawHelpline(bContext *C, int x, int y, void *customdata)
1228 {
1229         TransInfo *t = (TransInfo*)customdata;
1230
1231         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR))
1232         {
1233                 float vecrot[3], cent[2];
1234                 int mval[2] = {x, y};
1235
1236                 VECCOPY(vecrot, t->center);
1237                 if(t->flag & T_EDIT) {
1238                         Object *ob= t->obedit;
1239                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1240                 }
1241                 else if(t->flag & T_POSE) {
1242                         Object *ob=t->poseobj;
1243                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1244                 }
1245
1246                 projectFloatView(t, vecrot, cent);      // no overflow in extreme cases
1247
1248                 glPushMatrix();
1249
1250                 switch(t->helpline)
1251                 {
1252                         case HLP_SPRING:
1253                                 UI_ThemeColor(TH_WIRE);
1254
1255                                 setlinestyle(3);
1256                                 glBegin(GL_LINE_STRIP);
1257                                 glVertex2sv(t->mval);
1258                                 glVertex2fv(cent);
1259                                 glEnd();
1260
1261                                 glTranslatef(mval[0], mval[1], 0);
1262                                 glRotatef(-180 / M_PI * atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1]), 0, 0, 1);
1263
1264                                 setlinestyle(0);
1265                                 glLineWidth(3.0);
1266                                 drawArrow(UP, 5, 10, 5);
1267                                 drawArrow(DOWN, 5, 10, 5);
1268                                 glLineWidth(1.0);
1269                                 break;
1270                         case HLP_HARROW:
1271                                 UI_ThemeColor(TH_WIRE);
1272
1273                                 glTranslatef(mval[0], mval[1], 0);
1274
1275                                 glLineWidth(3.0);
1276                                 drawArrow(RIGHT, 5, 10, 5);
1277                                 drawArrow(LEFT, 5, 10, 5);
1278                                 glLineWidth(1.0);
1279                                 break;
1280                         case HLP_VARROW:
1281                                 UI_ThemeColor(TH_WIRE);
1282
1283                                 glTranslatef(mval[0], mval[1], 0);
1284
1285                                 glLineWidth(3.0);
1286                                 glBegin(GL_LINES);
1287                                 drawArrow(UP, 5, 10, 5);
1288                                 drawArrow(DOWN, 5, 10, 5);
1289                                 glLineWidth(1.0);
1290                                 break;
1291                         case HLP_ANGLE:
1292                                 {
1293                                         float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1294                                         float angle = atan2f(dy, dx);
1295                                         float dist = sqrtf(dx*dx + dy*dy);
1296                                         float delta_angle = MIN2(15 / dist, M_PI/4);
1297                                         float spacing_angle = MIN2(5 / dist, M_PI/12);
1298                                         UI_ThemeColor(TH_WIRE);
1299
1300                                         setlinestyle(3);
1301                                         glBegin(GL_LINE_STRIP);
1302                                         glVertex2sv(t->mval);
1303                                         glVertex2fv(cent);
1304                                         glEnd();
1305
1306                                         glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1307
1308                                         setlinestyle(0);
1309                                         glLineWidth(3.0);
1310                                         drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1311                                         drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1312
1313                                         glPushMatrix();
1314
1315                                         glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1316                                         glRotatef(180 / M_PI * (angle - delta_angle), 0, 0, 1);
1317
1318                                         drawArrowHead(DOWN, 5);
1319
1320                                         glPopMatrix();
1321
1322                                         glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1323                                         glRotatef(180 / M_PI * (angle + delta_angle), 0, 0, 1);
1324
1325                                         drawArrowHead(UP, 5);
1326
1327                                         glLineWidth(1.0);
1328                                         break;
1329                                 }
1330                                 case HLP_TRACKBALL:
1331                                 {
1332                                         char col[3], col2[3];
1333                                         UI_GetThemeColor3ubv(TH_GRID, col);
1334
1335                                         glTranslatef(mval[0], mval[1], 0);
1336
1337                                         glLineWidth(3.0);
1338
1339                                         UI_make_axis_color(col, col2, 'x');
1340                                         glColor3ubv((GLubyte *)col2);
1341
1342                                         drawArrow(RIGHT, 5, 10, 5);
1343                                         drawArrow(LEFT, 5, 10, 5);
1344
1345                                         UI_make_axis_color(col, col2, 'y');
1346                                         glColor3ubv((GLubyte *)col2);
1347
1348                                         drawArrow(UP, 5, 10, 5);
1349                                         drawArrow(DOWN, 5, 10, 5);
1350                                         glLineWidth(1.0);
1351                                         break;
1352                                 }
1353                 }
1354
1355                 glPopMatrix();
1356         }
1357 }
1358
1359 void drawTransformView(const struct bContext *C, struct ARegion *ar, void *arg)
1360 {
1361         TransInfo *t = arg;
1362
1363         drawConstraint(C, t);
1364         drawPropCircle(C, t);
1365         drawSnapping(C, t);
1366 }
1367
1368 void drawTransformPixel(const struct bContext *C, struct ARegion *ar, void *arg)
1369 {
1370 //      TransInfo *t = arg;
1371 //
1372 //      drawHelpline(C, t->mval[0], t->mval[1], t);
1373 }
1374
1375 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1376 {
1377         ToolSettings *ts = CTX_data_tool_settings(C);
1378         int constraint_axis[3] = {0, 0, 0};
1379         int proportional = 0;
1380
1381         if (RNA_struct_find_property(op->ptr, "value"))
1382         {
1383                 if (t->flag & T_AUTOVALUES)
1384                 {
1385                         RNA_float_set_array(op->ptr, "value", t->auto_values);
1386                 }
1387                 else
1388                 {
1389                         RNA_float_set_array(op->ptr, "value", t->values);
1390                 }
1391         }
1392
1393         /* XXX convert stupid flag to enum */
1394         switch(t->flag & (T_PROP_EDIT|T_PROP_CONNECTED))
1395         {
1396         case (T_PROP_EDIT|T_PROP_CONNECTED):
1397                 proportional = 2;
1398                 break;
1399         case T_PROP_EDIT:
1400                 proportional = 1;
1401                 break;
1402         default:
1403                 proportional = 0;
1404         }
1405
1406         // If modal, save settings back in scene if not set as operator argument
1407         if (t->flag & T_MODAL) {
1408
1409                 /* save settings if not set in operator */
1410                 if (RNA_struct_find_property(op->ptr, "proportional") && !RNA_property_is_set(op->ptr, "proportional")) {
1411                         ts->proportional = proportional;
1412                 }
1413
1414                 if (RNA_struct_find_property(op->ptr, "proportional_size") && !RNA_property_is_set(op->ptr, "proportional_size")) {
1415                         ts->proportional_size = t->prop_size;
1416                 }
1417                         
1418                 if (RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && !RNA_property_is_set(op->ptr, "proportional_editing_falloff")) {
1419                         ts->prop_mode = t->prop_mode;
1420                 }
1421                 
1422                 /* do we check for parameter? */
1423                 if (t->modifiers & MOD_SNAP) {
1424                         ts->snap_flag |= SCE_SNAP;
1425                 } else {
1426                         ts->snap_flag &= ~SCE_SNAP;
1427                 }
1428
1429                 if(t->spacetype == SPACE_VIEW3D) {
1430                         if (RNA_struct_find_property(op->ptr, "constraint_orientation") && !RNA_property_is_set(op->ptr, "constraint_orientation")) {
1431                                 View3D *v3d = t->view;
1432         
1433                                 v3d->twmode = t->current_orientation;
1434                         }
1435                 }
1436         }
1437         
1438         if (RNA_struct_find_property(op->ptr, "proportional"))
1439         {
1440                 RNA_enum_set(op->ptr, "proportional", proportional);
1441                 RNA_enum_set(op->ptr, "proportional_editing_falloff", t->prop_mode);
1442                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1443         }
1444
1445         if (RNA_struct_find_property(op->ptr, "mirror"))
1446         {
1447                 RNA_boolean_set(op->ptr, "mirror", t->flag & T_MIRROR);
1448         }
1449
1450         if (RNA_struct_find_property(op->ptr, "constraint_axis"))
1451         {
1452                 /* constraint orientation can be global, event if user selects something else
1453                  * so use the orientation in the constraint if set
1454                  * */
1455                 if (t->con.mode & CON_APPLY) {
1456                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1457                 } else {
1458                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1459                 }
1460
1461                 if (t->con.mode & CON_APPLY)
1462                 {
1463                         if (t->con.mode & CON_AXIS0) {
1464                                 constraint_axis[0] = 1;
1465                         }
1466                         if (t->con.mode & CON_AXIS1) {
1467                                 constraint_axis[1] = 1;
1468                         }
1469                         if (t->con.mode & CON_AXIS2) {
1470                                 constraint_axis[2] = 1;
1471                         }
1472                 }
1473
1474                 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1475         }
1476 }
1477
1478 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1479 {
1480         int options = 0;
1481
1482         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1483
1484         t->state = TRANS_STARTING;
1485
1486         t->options = options;
1487
1488         t->mode = mode;
1489
1490         t->launch_event = event ? event->type : -1;
1491
1492         if (!initTransInfo(C, t, op, event))                                    // internal data, mouse, vectors
1493         {
1494                 return 0;
1495         }
1496
1497         if(t->spacetype == SPACE_VIEW3D)
1498         {
1499                 //calc_manipulator_stats(curarea);
1500                 initTransformOrientation(C, t);
1501
1502                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1503                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1504                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), NULL, drawHelpline, t);
1505         }
1506         else if(t->spacetype == SPACE_IMAGE) {
1507                 unit_m3(t->spacemtx);
1508                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1509                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1510         }
1511         else
1512                 unit_m3(t->spacemtx);
1513
1514         createTransData(C, t);                  // make TransData structs from selection
1515
1516         if (t->total == 0) {
1517                 postTrans(C, t);
1518                 return 0;
1519         }
1520
1521         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1522
1523         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1524         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1525         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1526         mode = t->mode;
1527
1528         calculatePropRatio(t);
1529         calculateCenter(t);
1530
1531         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1532
1533         switch (mode) {
1534         case TFM_TRANSLATION:
1535                 initTranslation(t);
1536                 break;
1537         case TFM_ROTATION:
1538                 initRotation(t);
1539                 break;
1540         case TFM_RESIZE:
1541                 initResize(t);
1542                 break;
1543         case TFM_TOSPHERE:
1544                 initToSphere(t);
1545                 break;
1546         case TFM_SHEAR:
1547                 initShear(t);
1548                 break;
1549         case TFM_WARP:
1550                 initWarp(t);
1551                 break;
1552         case TFM_SHRINKFATTEN:
1553                 initShrinkFatten(t);
1554                 break;
1555         case TFM_TILT:
1556                 initTilt(t);
1557                 break;
1558         case TFM_CURVE_SHRINKFATTEN:
1559                 initCurveShrinkFatten(t);
1560                 break;
1561         case TFM_TRACKBALL:
1562                 initTrackball(t);
1563                 break;
1564         case TFM_PUSHPULL:
1565                 initPushPull(t);
1566                 break;
1567         case TFM_CREASE:
1568                 initCrease(t);
1569                 break;
1570         case TFM_BONESIZE:
1571                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1572                         bArmature *arm= t->poseobj->data;
1573                         if(arm->drawtype==ARM_ENVELOPE)
1574                                 initBoneEnvelope(t);
1575                         else
1576                                 initBoneSize(t);
1577                 }
1578                 break;
1579         case TFM_BONE_ENVELOPE:
1580                 initBoneEnvelope(t);
1581                 break;
1582         case TFM_EDGE_SLIDE:
1583                 initEdgeSlide(t);
1584                 break;
1585         case TFM_BONE_ROLL:
1586                 initBoneRoll(t);
1587                 break;
1588         case TFM_TIME_TRANSLATE:
1589                 initTimeTranslate(t);
1590                 break;
1591         case TFM_TIME_SLIDE:
1592                 initTimeSlide(t);
1593                 break;
1594         case TFM_TIME_SCALE:
1595                 initTimeScale(t);
1596                 break;
1597         case TFM_TIME_EXTEND:
1598                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1599                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1600                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1601                  * depending on which editor this was called from
1602                  */
1603                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1604                         initTranslation(t);
1605                 else
1606                         initTimeTranslate(t);
1607                 break;
1608         case TFM_BAKE_TIME:
1609                 initBakeTime(t);
1610                 break;
1611         case TFM_MIRROR:
1612                 initMirror(t);
1613                 break;
1614         case TFM_BEVEL:
1615                 initBevel(t);
1616                 break;
1617         case TFM_BWEIGHT:
1618                 initBevelWeight(t);
1619                 break;
1620         case TFM_ALIGN:
1621                 initAlign(t);
1622                 break;
1623         }
1624
1625         /* overwrite initial values if operator supplied a non-null vector */
1626         if (RNA_property_is_set(op->ptr, "value"))
1627         {
1628                 float values[4];
1629                 RNA_float_get_array(op->ptr, "value", values);
1630                 QUATCOPY(t->values, values);
1631                 QUATCOPY(t->auto_values, values);
1632                 t->flag |= T_AUTOVALUES;
1633         }
1634
1635         /* Constraint init from operator */
1636         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1637         {
1638                 int constraint_axis[3];
1639
1640                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1641
1642                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1643                 {
1644                         t->con.mode |= CON_APPLY;
1645
1646                         if (constraint_axis[0]) {
1647                                 t->con.mode |= CON_AXIS0;
1648                         }
1649                         if (constraint_axis[1]) {
1650                                 t->con.mode |= CON_AXIS1;
1651                         }
1652                         if (constraint_axis[2]) {
1653                                 t->con.mode |= CON_AXIS2;
1654                         }
1655
1656                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1657                 }
1658         }
1659
1660         return 1;
1661 }
1662
1663 void transformApply(bContext *C, TransInfo *t)
1664 {
1665         if (t->redraw)
1666         {
1667                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
1668                         t->con.mode |= CON_SELECT;
1669
1670                 selectConstraint(t);
1671                 if (t->transform) {
1672                         t->transform(t, t->mval);  // calls recalcData()
1673                         viewRedrawForce(C, t);
1674                 }
1675                 t->redraw = 0;
1676         }
1677
1678         /* If auto confirm is on, break after one pass */
1679         if (t->options & CTX_AUTOCONFIRM)
1680         {
1681                 t->state = TRANS_CONFIRM;
1682         }
1683
1684         if (BKE_ptcache_get_continue_physics())
1685         {
1686                 // TRANSFORM_FIX_ME
1687                 //do_screenhandlers(G.curscreen);
1688                 t->redraw = 1;
1689         }
1690 }
1691
1692 int transformEnd(bContext *C, TransInfo *t)
1693 {
1694         int exit_code = OPERATOR_RUNNING_MODAL;
1695
1696         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING)
1697         {
1698                 /* handle restoring objects */
1699                 if(t->state == TRANS_CANCEL)
1700                 {
1701                         exit_code = OPERATOR_CANCELLED;
1702                         restoreTransObjects(t); // calls recalcData()
1703                 }
1704                 else
1705                 {
1706                         exit_code = OPERATOR_FINISHED;
1707                 }
1708
1709                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1710                 special_aftertrans_update(C, t);
1711
1712                 /* free data */
1713                 postTrans(C, t);
1714
1715                 /* send events out for redraws */
1716                 viewRedrawPost(t);
1717
1718                 /*  Undo as last, certainly after special_trans_update! */
1719
1720                 if(t->state == TRANS_CANCEL) {
1721 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1722                 }
1723                 else {
1724 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1725 //                      else ED_undo_push(C, transform_to_undostr(t));
1726                 }
1727                 t->undostr= NULL;
1728
1729                 viewRedrawForce(C, t);
1730         }
1731
1732         return exit_code;
1733 }
1734
1735 /* ************************** TRANSFORM LOCKS **************************** */
1736
1737 static void protectedTransBits(short protectflag, float *vec)
1738 {
1739         if(protectflag & OB_LOCK_LOCX)
1740                 vec[0]= 0.0f;
1741         if(protectflag & OB_LOCK_LOCY)
1742                 vec[1]= 0.0f;
1743         if(protectflag & OB_LOCK_LOCZ)
1744                 vec[2]= 0.0f;
1745 }
1746
1747 static void protectedSizeBits(short protectflag, float *size)
1748 {
1749         if(protectflag & OB_LOCK_SCALEX)
1750                 size[0]= 1.0f;
1751         if(protectflag & OB_LOCK_SCALEY)
1752                 size[1]= 1.0f;
1753         if(protectflag & OB_LOCK_SCALEZ)
1754                 size[2]= 1.0f;
1755 }
1756
1757 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1758 {
1759         if(protectflag & OB_LOCK_ROTX)
1760                 eul[0]= oldeul[0];
1761         if(protectflag & OB_LOCK_ROTY)
1762                 eul[1]= oldeul[1];
1763         if(protectflag & OB_LOCK_ROTZ)
1764                 eul[2]= oldeul[2];
1765 }
1766
1767
1768 /* this function only does the delta rotation */
1769 /* axis-angle is usually internally stored as quats... */
1770 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1771 {
1772         /* check that protection flags are set */
1773         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1774                 return;
1775         
1776         if (protectflag & OB_LOCK_ROT4D) {
1777                 /* axis-angle getting limited as 4D entities that they are... */
1778                 if (protectflag & OB_LOCK_ROTW)
1779                         *angle= oldAngle;
1780                 if (protectflag & OB_LOCK_ROTX)
1781                         axis[0]= oldAxis[0];
1782                 if (protectflag & OB_LOCK_ROTY)
1783                         axis[1]= oldAxis[1];
1784                 if (protectflag & OB_LOCK_ROTZ)
1785                         axis[2]= oldAxis[2];
1786         }
1787         else {
1788                 /* axis-angle get limited with euler... */
1789                 float eul[3], oldeul[3];
1790                 
1791                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1792                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1793                 
1794                 if (protectflag & OB_LOCK_ROTX)
1795                         eul[0]= oldeul[0];
1796                 if (protectflag & OB_LOCK_ROTY)
1797                         eul[1]= oldeul[1];
1798                 if (protectflag & OB_LOCK_ROTZ)
1799                         eul[2]= oldeul[2];
1800                 
1801                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1802                 
1803                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1804                 if (IS_EQ(axis[0], axis[1]) && IS_EQ(axis[1], axis[2])) {
1805                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1806                         axis[1]= 1.0f;
1807                 }
1808         }
1809 }
1810
1811 /* this function only does the delta rotation */
1812 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1813 {
1814         /* check that protection flags are set */
1815         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1816                 return;
1817         
1818         if (protectflag & OB_LOCK_ROT4D) {
1819                 /* quaternions getting limited as 4D entities that they are... */
1820                 if (protectflag & OB_LOCK_ROTW)
1821                         quat[0]= oldquat[0];
1822                 if (protectflag & OB_LOCK_ROTX)
1823                         quat[1]= oldquat[1];
1824                 if (protectflag & OB_LOCK_ROTY)
1825                         quat[2]= oldquat[2];
1826                 if (protectflag & OB_LOCK_ROTZ)
1827                         quat[3]= oldquat[3];
1828         }
1829         else {
1830                 /* quaternions get limited with euler... (compatability mode) */
1831                 float eul[3], oldeul[3], quat1[4];
1832                 
1833                 QUATCOPY(quat1, quat);
1834                 quat_to_eul( eul,quat);
1835                 quat_to_eul( oldeul,oldquat);
1836                 
1837                 if (protectflag & OB_LOCK_ROTX)
1838                         eul[0]= oldeul[0];
1839                 if (protectflag & OB_LOCK_ROTY)
1840                         eul[1]= oldeul[1];
1841                 if (protectflag & OB_LOCK_ROTZ)
1842                         eul[2]= oldeul[2];
1843                 
1844                 eul_to_quat( quat,eul);
1845                 
1846                 /* quaternions flip w sign to accumulate rotations correctly */
1847                 if ( (quat1[0]<0.0f && quat[0]>0.0f) || (quat1[0]>0.0f && quat[0]<0.0f) ) {
1848                         mul_qt_fl(quat, -1.0f);
1849                 }
1850         }
1851 }
1852
1853 /* ******************* TRANSFORM LIMITS ********************** */
1854
1855 static void constraintTransLim(TransInfo *t, TransData *td)
1856 {
1857         if (td->con) {
1858                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
1859                 bConstraintOb cob;
1860                 bConstraint *con;
1861                 
1862                 /* Make a temporary bConstraintOb for using these limit constraints
1863                  *      - they only care that cob->matrix is correctly set ;-)
1864                  *      - current space should be local
1865                  */
1866                 memset(&cob, 0, sizeof(bConstraintOb));
1867                 unit_m4(cob.matrix);
1868                 VECCOPY(cob.matrix[3], td->loc);
1869                 
1870                 /* Evaluate valid constraints */
1871                 for (con= td->con; con; con= con->next) {
1872                         float tmat[4][4];
1873                         
1874                         /* only consider constraint if enabled */
1875                         if (con->flag & CONSTRAINT_DISABLE) continue;
1876                         if (con->enforce == 0.0f) continue;
1877                         
1878                         /* only use it if it's tagged for this purpose (and the right type) */
1879                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
1880                                 bLocLimitConstraint *data= con->data;
1881                                 
1882                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
1883                                         continue;
1884                                 
1885                                 /* do space conversions */
1886                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
1887                                         /* just multiply by td->mtx (this should be ok) */
1888                                         copy_m4_m4(tmat, cob.matrix);
1889                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
1890                                 }
1891                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
1892                                         /* skip... incompatable spacetype */
1893                                         continue;
1894                                 }
1895                                 
1896                                 /* do constraint */
1897                                 cti->evaluate_constraint(con, &cob, NULL);
1898                                 
1899                                 /* convert spaces again */
1900                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
1901                                         /* just multiply by td->mtx (this should be ok) */
1902                                         copy_m4_m4(tmat, cob.matrix);
1903                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
1904                                 }
1905                         }
1906                 }
1907                 
1908                 /* copy results from cob->matrix */
1909                 VECCOPY(td->loc, cob.matrix[3]);
1910         }
1911 }
1912
1913 static void constraintRotLim(TransInfo *t, TransData *td)
1914 {
1915         if (td->con) {
1916                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
1917                 bConstraintOb cob;
1918                 bConstraint *con;
1919
1920                 /* Make a temporary bConstraintOb for using these limit constraints
1921                  *      - they only care that cob->matrix is correctly set ;-)
1922                  *      - current space should be local
1923                  */
1924                 memset(&cob, 0, sizeof(bConstraintOb));
1925                 if (td->rotOrder == ROT_MODE_QUAT) {
1926                         /* quats */
1927                         if (td->ext)
1928                                 quat_to_mat4( cob.matrix,td->ext->quat);
1929                         else
1930                                 return;
1931                 }
1932                 else if (td->rotOrder == ROT_MODE_AXISANGLE) {
1933                         /* axis angle */
1934                         if (td->ext)
1935                                 axis_angle_to_mat4( cob.matrix,&td->ext->quat[1], td->ext->quat[0]);
1936                         else
1937                                 return;
1938                 }
1939                 else {
1940                         /* eulers */
1941                         if (td->ext)
1942                                 eulO_to_mat4( cob.matrix,td->ext->rot, td->rotOrder);
1943                         else
1944                                 return;
1945                 }
1946                 
1947                 /* Evaluate valid constraints */
1948                 for (con= td->con; con; con= con->next) {
1949                         /* only consider constraint if enabled */
1950                         if (con->flag & CONSTRAINT_DISABLE) continue;
1951                         if (con->enforce == 0.0f) continue;
1952                         
1953                         /* we're only interested in Limit-Rotation constraints */
1954                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
1955                                 bRotLimitConstraint *data= con->data;
1956                                 float tmat[4][4];
1957                                 
1958                                 /* only use it if it's tagged for this purpose */
1959                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
1960                                         continue;
1961                                 
1962                                 /* do space conversions */
1963                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
1964                                         /* just multiply by td->mtx (this should be ok) */
1965                                         copy_m4_m4(tmat, cob.matrix);
1966                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
1967                                 }
1968                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
1969                                         /* skip... incompatable spacetype */
1970                                         continue;
1971                                 }
1972                                 
1973                                 /* do constraint */
1974                                 cti->evaluate_constraint(con, &cob, NULL);
1975                                 
1976                                 /* convert spaces again */
1977                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
1978                                         /* just multiply by td->mtx (this should be ok) */
1979                                         copy_m4_m4(tmat, cob.matrix);
1980                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
1981                                 }
1982                         }
1983                 }
1984                 
1985                 /* copy results from cob->matrix */
1986                 if (td->rotOrder == ROT_MODE_QUAT) {
1987                         /* quats */
1988                         mat4_to_quat( td->ext->quat,cob.matrix);
1989                 }
1990                 else if (td->rotOrder == ROT_MODE_AXISANGLE) {
1991                         /* axis angle */
1992                         mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
1993                 }
1994                 else {
1995                         /* eulers */
1996                         mat4_to_eulO( td->ext->rot, td->rotOrder,cob.matrix);
1997                 }
1998         }
1999 }
2000
2001 static void constraintSizeLim(TransInfo *t, TransData *td)
2002 {
2003         if (td->con && td->ext) {
2004                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2005                 bConstraintOb cob;
2006                 bConstraint *con;
2007                 
2008                 /* Make a temporary bConstraintOb for using these limit constraints
2009                  *      - they only care that cob->matrix is correctly set ;-)
2010                  *      - current space should be local
2011                  */
2012                 memset(&cob, 0, sizeof(bConstraintOb));
2013                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2014                         /* scale val and reset size */
2015                         return; // TODO: fix this case
2016                 }
2017                 else {
2018                         /* Reset val if SINGLESIZE but using a constraint */
2019                         if (td->flag & TD_SINGLESIZE)
2020                                 return;
2021                         
2022                         size_to_mat4( cob.matrix,td->ext->size);
2023                 }
2024                 
2025                 /* Evaluate valid constraints */
2026                 for (con= td->con; con; con= con->next) {
2027                         /* only consider constraint if enabled */
2028                         if (con->flag & CONSTRAINT_DISABLE) continue;
2029                         if (con->enforce == 0.0f) continue;
2030                         
2031                         /* we're only interested in Limit-Scale constraints */
2032                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2033                                 bSizeLimitConstraint *data= con->data;
2034                                 float tmat[4][4];
2035                                 
2036                                 /* only use it if it's tagged for this purpose */
2037                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2038                                         continue;
2039                                 
2040                                 /* do space conversions */
2041                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2042                                         /* just multiply by td->mtx (this should be ok) */
2043                                         copy_m4_m4(tmat, cob.matrix);
2044                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2045                                 }
2046                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2047                                         /* skip... incompatable spacetype */
2048                                         continue;
2049                                 }
2050                                 
2051                                 /* do constraint */
2052                                 cti->evaluate_constraint(con, &cob, NULL);
2053                                 
2054                                 /* convert spaces again */
2055                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2056                                         /* just multiply by td->mtx (this should be ok) */
2057                                         copy_m4_m4(tmat, cob.matrix);
2058                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2059                                 }
2060                         }
2061                 }
2062                 
2063                 /* copy results from cob->matrix */
2064                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2065                         /* scale val and reset size */
2066                         return; // TODO: fix this case
2067                 }
2068                 else {
2069                         /* Reset val if SINGLESIZE but using a constraint */
2070                         if (td->flag & TD_SINGLESIZE)
2071                                 return;
2072                         
2073                         mat4_to_size( td->ext->size,cob.matrix);
2074                 }
2075         }
2076 }
2077
2078 /* ************************** WARP *************************** */
2079
2080 void initWarp(TransInfo *t)
2081 {
2082         float max[3], min[3];
2083         int i;
2084         
2085         t->mode = TFM_WARP;
2086         t->transform = Warp;
2087         t->handleEvent = handleEventWarp;
2088         
2089         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2090         
2091         t->idx_max = 0;
2092         t->num.idx_max = 0;
2093         t->snap[0] = 0.0f;
2094         t->snap[1] = 5.0f;
2095         t->snap[2] = 1.0f;
2096         
2097         t->flag |= T_NO_CONSTRAINT;
2098         
2099         /* we need min/max in view space */
2100         for(i = 0; i < t->total; i++) {
2101                 float center[3];
2102                 VECCOPY(center, t->data[i].center);
2103                 mul_m3_v3(t->data[i].mtx, center);
2104                 mul_m4_v3(t->viewmat, center);
2105                 sub_v3_v3v3(center, center, t->viewmat[3]);
2106                 if (i)
2107                         minmax_v3_v3v3(min, max, center);
2108                 else {
2109                         VECCOPY(max, center);
2110                         VECCOPY(min, center);
2111                 }
2112         }
2113         
2114         t->center[0]= (min[0]+max[0])/2.0f;
2115         t->center[1]= (min[1]+max[1])/2.0f;
2116         t->center[2]= (min[2]+max[2])/2.0f;
2117
2118         if (max[0] == min[0]) max[0] += 0.1; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2119         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2120 }
2121
2122 int handleEventWarp(TransInfo *t, wmEvent *event)
2123 {
2124         int status = 0;
2125         
2126         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2127         {
2128                 // Use customData pointer to signal warp direction
2129                 if      (t->customData == 0)
2130                         t->customData = (void*)1;
2131                 else
2132                         t->customData = 0;
2133                 
2134                 status = 1;
2135         }
2136         
2137         return status;
2138 }
2139
2140 int Warp(TransInfo *t, short mval[2])
2141 {
2142         TransData *td = t->data;
2143         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2144         int i;
2145         char str[50];
2146         
2147         curs= give_cursor(t->scene, t->view);
2148         /*
2149          * gcursor is the one used for helpline.
2150          * It has to be in the same space as the drawing loop
2151          * (that means it needs to be in the object's space when in edit mode and
2152          *  in global space in object mode)
2153          *
2154          * cursor is used for calculations.
2155          * It needs to be in view space, but we need to take object's offset
2156          * into account if in Edit mode.
2157          */
2158         VECCOPY(cursor, curs);
2159         VECCOPY(gcursor, cursor);
2160         if (t->flag & T_EDIT) {
2161                 sub_v3_v3v3(cursor, cursor, t->obedit->obmat[3]);
2162                 sub_v3_v3v3(gcursor, gcursor, t->obedit->obmat[3]);
2163                 mul_m3_v3(t->data->smtx, gcursor);
2164         }
2165         mul_m4_v3(t->viewmat, cursor);
2166         sub_v3_v3v3(cursor, cursor, t->viewmat[3]);
2167         
2168         /* amount of degrees for warp */
2169         circumfac = 360.0f * t->values[0];
2170         
2171         if (t->customData) /* non-null value indicates reversed input */
2172         {
2173                 circumfac *= -1;
2174         }
2175         
2176         snapGrid(t, &circumfac);
2177         applyNumInput(&t->num, &circumfac);
2178         
2179         /* header print for NumInput */
2180         if (hasNumInput(&t->num)) {
2181                 char c[20];
2182                 
2183                 outputNumInput(&(t->num), c);
2184                 
2185                 sprintf(str, "Warp: %s", c);
2186         }
2187         else {
2188                 /* default header print */
2189                 sprintf(str, "Warp: %.3f", circumfac);
2190         }
2191         
2192         circumfac*= (float)(-M_PI/360.0);
2193         
2194         for(i = 0; i < t->total; i++, td++) {
2195                 float loc[3];
2196                 if (td->flag & TD_NOACTION)
2197                         break;
2198                 
2199                 if (td->flag & TD_SKIP)
2200                         continue;
2201                 
2202                 /* translate point to center, rotate in such a way that outline==distance */
2203                 VECCOPY(vec, td->iloc);
2204                 mul_m3_v3(td->mtx, vec);
2205                 mul_m4_v3(t->viewmat, vec);
2206                 sub_v3_v3v3(vec, vec, t->viewmat[3]);
2207                 
2208                 dist= vec[0]-cursor[0];
2209                 
2210                 /* t->val is X dimension projected boundbox */
2211                 phi0= (circumfac*dist/t->val);
2212                 
2213                 vec[1]= (vec[1]-cursor[1]);
2214                 
2215                 co= (float)cos(phi0);
2216                 si= (float)sin(phi0);
2217                 loc[0]= -si*vec[1]+cursor[0];
2218                 loc[1]= co*vec[1]+cursor[1];
2219                 loc[2]= vec[2];
2220                 
2221                 mul_m4_v3(t->viewinv, loc);
2222                 sub_v3_v3v3(loc, loc, t->viewinv[3]);
2223                 mul_m3_v3(td->smtx, loc);
2224                 
2225                 sub_v3_v3v3(loc, loc, td->iloc);
2226                 mul_v3_fl(loc, td->factor);
2227                 add_v3_v3v3(td->loc, td->iloc, loc);
2228         }
2229         
2230         recalcData(t);
2231         
2232         ED_area_headerprint(t->sa, str);
2233         
2234         return 1;
2235 }
2236
2237 /* ************************** SHEAR *************************** */
2238
2239 void initShear(TransInfo *t)
2240 {
2241         t->mode = TFM_SHEAR;
2242         t->transform = Shear;
2243         t->handleEvent = handleEventShear;
2244         
2245         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2246         
2247         t->idx_max = 0;
2248         t->num.idx_max = 0;
2249         t->snap[0] = 0.0f;
2250         t->snap[1] = 0.1f;
2251         t->snap[2] = t->snap[1] * 0.1f;
2252         
2253         t->flag |= T_NO_CONSTRAINT;
2254 }
2255
2256 int handleEventShear(TransInfo *t, wmEvent *event)
2257 {
2258         int status = 0;
2259         
2260         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2261         {
2262                 // Use customData pointer to signal Shear direction
2263                 if      (t->customData == 0)
2264                 {
2265                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2266                         t->customData = (void*)1;
2267                 }
2268                 else
2269                 {
2270                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2271                         t->customData = 0;
2272                 }
2273                 
2274                 status = 1;
2275         }
2276         
2277         return status;
2278 }
2279
2280
2281 int Shear(TransInfo *t, short mval[2])
2282 {
2283         TransData *td = t->data;
2284         float vec[3];
2285         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2286         float value;
2287         int i;
2288         char str[50];
2289         
2290         copy_m3_m4(persmat, t->viewmat);
2291         invert_m3_m3(persinv, persmat);
2292         
2293         value = 0.05f * t->values[0];
2294         
2295         snapGrid(t, &value);
2296         
2297         applyNumInput(&t->num, &value);
2298         
2299         /* header print for NumInput */
2300         if (hasNumInput(&t->num)) {
2301                 char c[20];
2302                 
2303                 outputNumInput(&(t->num), c);
2304                 
2305                 sprintf(str, "Shear: %s %s", c, t->proptext);
2306         }
2307         else {
2308                 /* default header print */
2309                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2310         }
2311         
2312         unit_m3(smat);
2313         
2314         // Custom data signals shear direction
2315         if (t->customData == 0)
2316                 smat[1][0] = value;
2317         else
2318                 smat[0][1] = value;
2319         
2320         mul_m3_m3m3(tmat, smat, persmat);
2321         mul_m3_m3m3(totmat, persinv, tmat);
2322         
2323         for(i = 0 ; i < t->total; i++, td++) {
2324                 if (td->flag & TD_NOACTION)
2325                         break;
2326                 
2327                 if (td->flag & TD_SKIP)
2328                         continue;
2329                 
2330                 if (t->obedit) {
2331                         float mat3[3][3];
2332                         mul_m3_m3m3(mat3, totmat, td->mtx);
2333                         mul_m3_m3m3(tmat, td->smtx, mat3);
2334                 }
2335                 else {
2336                         copy_m3_m3(tmat, totmat);
2337                 }
2338                 sub_v3_v3v3(vec, td->center, t->center);
2339                 
2340                 mul_m3_v3(tmat, vec);
2341                 
2342                 add_v3_v3v3(vec, vec, t->center);
2343                 sub_v3_v3v3(vec, vec, td->center);
2344                 
2345                 mul_v3_fl(vec, td->factor);
2346                 
2347                 add_v3_v3v3(td->loc, td->iloc, vec);
2348         }
2349         
2350         recalcData(t);
2351         
2352         ED_area_headerprint(t->sa, str);
2353
2354         return 1;
2355 }
2356
2357 /* ************************** RESIZE *************************** */
2358
2359 void initResize(TransInfo *t)
2360 {
2361         t->mode = TFM_RESIZE;
2362         t->transform = Resize;
2363         
2364         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2365         
2366         t->flag |= T_NULL_ONE;
2367         t->num.flag |= NUM_NULL_ONE;
2368         t->num.flag |= NUM_AFFECT_ALL;
2369         if (!t->obedit) {
2370                 t->flag |= T_NO_ZERO;
2371                 t->num.flag |= NUM_NO_ZERO;
2372         }
2373         
2374         t->idx_max = 2;
2375         t->num.idx_max = 2;
2376         t->snap[0] = 0.0f;
2377         t->snap[1] = 0.1f;
2378         t->snap[2] = t->snap[1] * 0.1f;
2379 }
2380
2381 static void headerResize(TransInfo *t, float vec[3], char *str) {
2382         char tvec[60];
2383         if (hasNumInput(&t->num)) {
2384                 outputNumInput(&(t->num), tvec);
2385         }
2386         else {
2387                 sprintf(&tvec[0], "%.4f", vec[0]);
2388                 sprintf(&tvec[20], "%.4f", vec[1]);
2389                 sprintf(&tvec[40], "%.4f", vec[2]);
2390         }
2391         
2392         if (t->con.mode & CON_APPLY) {
2393                 switch(t->num.idx_max) {
2394                 case 0:
2395                         sprintf(str, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2396                         break;
2397                 case 1:
2398                         sprintf(str, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2399                         break;
2400                 case 2:
2401                         sprintf(str, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2402                 }
2403         }
2404         else {
2405                 if (t->flag & T_2D_EDIT)
2406                         sprintf(str, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2407                 else
2408                         sprintf(str, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2409         }
2410 }
2411
2412 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2413 #define VECSIGNFLIP(a, b) ((SIGN(a[0]) & SIGN(b[0]))==0 || (SIGN(a[1]) & SIGN(b[1]))==0 || (SIGN(a[2]) & SIGN(b[2]))==0)
2414
2415 /* smat is reference matrix, only scaled */
2416 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2417 {
2418         float vec[3];
2419         
2420         copy_v3_v3(vec, mat[0]);
2421         size[0]= normalize_v3(vec);
2422         copy_v3_v3(vec, mat[1]);
2423         size[1]= normalize_v3(vec);
2424         copy_v3_v3(vec, mat[2]);
2425         size[2]= normalize_v3(vec);
2426         
2427         /* first tried with dotproduct... but the sign flip is crucial */
2428         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2429         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2430         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2431 }
2432
2433
2434 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3]) {
2435         float tmat[3][3], smat[3][3], center[3];
2436         float vec[3];
2437         
2438         if (t->flag & T_EDIT) {
2439                 mul_m3_m3m3(smat, mat, td->mtx);
2440                 mul_m3_m3m3(tmat, td->smtx, smat);
2441         }
2442         else {
2443                 copy_m3_m3(tmat, mat);
2444         }
2445         
2446         if (t->con.applySize) {
2447                 t->con.applySize(t, td, tmat);
2448         }
2449         
2450         /* local constraint shouldn't alter center */
2451         if (t->around == V3D_LOCAL) {
2452                 if (t->flag & T_OBJECT) {
2453                         VECCOPY(center, td->center);
2454                 }
2455                 else if (t->flag & T_EDIT) {
2456                         
2457                         if(t->around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2458                                 VECCOPY(center, td->center);
2459                         }
2460                         else {
2461                                 VECCOPY(center, t->center);
2462                         }
2463                 }
2464                 else {
2465                         VECCOPY(center, t->center);
2466                 }
2467         }
2468         else {
2469                 VECCOPY(center, t->center);
2470         }
2471         
2472         if (td->ext) {
2473                 float fsize[3];
2474                 
2475                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2476                         float obsizemat[3][3];
2477                         // Reorient the size mat to fit the oriented object.
2478                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2479                         //print_m3("obsizemat", obsizemat);
2480                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2481                         //print_v3("fsize", fsize);
2482                 }
2483                 else {
2484                         mat3_to_size( fsize,tmat);
2485                 }
2486                 
2487                 protectedSizeBits(td->protectflag, fsize);
2488                 
2489                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2490                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)){
2491                                 /* scale val and reset size */
2492                                 *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2493                                 
2494                                 td->ext->size[0] = td->ext->isize[0];
2495                                 td->ext->size[1] = td->ext->isize[1];
2496                                 td->ext->size[2] = td->ext->isize[2];
2497                         }
2498                         else {
2499                                 /* Reset val if SINGLESIZE but using a constraint */
2500                                 if (td->flag & TD_SINGLESIZE)
2501                                         *td->val = td->ival;
2502                                 
2503                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2504                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2505                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2506                         }
2507                 }
2508                 
2509                 constraintSizeLim(t, td);
2510         }
2511         
2512         /* For individual element center, Editmode need to use iloc */
2513         if (t->flag & T_POINTS)
2514                 sub_v3_v3v3(vec, td->iloc, center);
2515         else
2516                 sub_v3_v3v3(vec, td->center, center);
2517         
2518         mul_m3_v3(tmat, vec);
2519         
2520         add_v3_v3v3(vec, vec, center);
2521         if (t->flag & T_POINTS)
2522                 sub_v3_v3v3(vec, vec, td->iloc);
2523         else
2524                 sub_v3_v3v3(vec, vec, td->center);
2525         
2526         mul_v3_fl(vec, td->factor);
2527         
2528         if (t->flag & (T_OBJECT|T_POSE)) {
2529                 mul_m3_v3(td->smtx, vec);
2530         }
2531         
2532         protectedTransBits(td->protectflag, vec);
2533         add_v3_v3v3(td->loc, td->iloc, vec);
2534         
2535         constraintTransLim(t, td);
2536 }
2537
2538 int Resize(TransInfo *t, short mval[2])
2539 {
2540         TransData *td;
2541         float size[3], mat[3][3];
2542         float ratio;
2543         int i;
2544         char str[200];
2545         
2546         /* for manipulator, center handle, the scaling can't be done relative to center */
2547         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2548         {
2549                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2550         }
2551         else
2552         {
2553                 ratio = t->values[0];
2554         }
2555         
2556         size[0] = size[1] = size[2] = ratio;
2557         
2558         snapGrid(t, size);
2559         
2560         if (hasNumInput(&t->num)) {
2561                 applyNumInput(&t->num, size);
2562                 constraintNumInput(t, size);
2563         }
2564         
2565         applySnapping(t, size);
2566         
2567         if (t->flag & T_AUTOVALUES)
2568         {
2569                 VECCOPY(size, t->auto_values);
2570         }
2571         
2572         VECCOPY(t->values, size);
2573         
2574         size_to_mat3( mat,size);
2575         
2576         if (t->con.applySize) {
2577                 t->con.applySize(t, NULL, mat);
2578         }
2579         
2580         copy_m3_m3(t->mat, mat);        // used in manipulator
2581         
2582         headerResize(t, size, str);
2583         
2584         for(i = 0, td=t->data; i < t->total; i++, td++) {
2585                 if (td->flag & TD_NOACTION)
2586                         break;
2587                 
2588                 if (td->flag & TD_SKIP)
2589                         continue;
2590                 
2591                 ElementResize(t, td, mat);
2592         }
2593         
2594         /* evil hack - redo resize if cliping needed */
2595         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2596                 size_to_mat3( mat,size);
2597                 
2598                 if (t->con.applySize)
2599                         t->con.applySize(t, NULL, mat);
2600                 
2601                 for(i = 0, td=t->data; i < t->total; i++, td++)
2602                         ElementResize(t, td, mat);
2603         }
2604         
2605         recalcData(t);
2606         
2607         ED_area_headerprint(t->sa, str);
2608         
2609         return 1;
2610 }
2611
2612 /* ************************** TOSPHERE *************************** */
2613
2614 void initToSphere(TransInfo *t)
2615 {
2616         TransData *td = t->data;
2617         int i;
2618         
2619         t->mode = TFM_TOSPHERE;
2620         t->transform = ToSphere;
2621         
2622         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2623         
2624         t->idx_max = 0;
2625         t->num.idx_max = 0;
2626         t->snap[0] = 0.0f;
2627         t->snap[1] = 0.1f;
2628         t->snap[2] = t->snap[1] * 0.1f;
2629         
2630         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2631         t->flag |= T_NO_CONSTRAINT;
2632         
2633         // Calculate average radius
2634         for(i = 0 ; i < t->total; i++, td++) {
2635                 t->val += len_v3v3(t->center, td->iloc);
2636         }
2637         
2638         t->val /= (float)t->total;
2639 }
2640
2641 int ToSphere(TransInfo *t, short mval[2])
2642 {
2643         float vec[3];
2644         float ratio, radius;
2645         int i;
2646         char str[64];
2647         TransData *td = t->data;
2648         
2649         ratio = t->values[0];
2650         
2651         snapGrid(t, &ratio);
2652         
2653         applyNumInput(&t->num, &ratio);
2654         
2655         if (ratio < 0)
2656                 ratio = 0.0f;
2657         else if (ratio > 1)
2658                 ratio = 1.0f;
2659         
2660         /* header print for NumInput */
2661         if (hasNumInput(&t->num)) {
2662                 char c[20];
2663                 
2664                 outputNumInput(&(t->num), c);
2665                 
2666                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2667         }
2668         else {
2669                 /* default header print */
2670                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2671         }
2672         
2673         
2674         for(i = 0 ; i < t->total; i++, td++) {
2675                 float tratio;
2676                 if (td->flag & TD_NOACTION)
2677                         break;
2678                 
2679                 if (td->flag & TD_SKIP)
2680                         continue;
2681                 
2682                 sub_v3_v3v3(vec, td->iloc, t->center);
2683                 
2684                 radius = normalize_v3(vec);
2685                 
2686                 tratio = ratio * td->factor;
2687                 
2688                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2689                 
2690                 add_v3_v3v3(td->loc, t->center, vec);
2691         }
2692         
2693         
2694         recalcData(t);
2695         
2696         ED_area_headerprint(t->sa, str);
2697         
2698         return 1;
2699 }
2700
2701 /* ************************** ROTATION *************************** */
2702
2703
2704 void initRotation(TransInfo *t)
2705 {
2706         t->mode = TFM_ROTATION;
2707         t->transform = Rotation;
2708         
2709         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2710         
2711         t->ndof.axis = 16;
2712         /* Scale down and flip input for rotation */
2713         t->ndof.factor[0] = -0.2f;
2714         
2715         t->idx_max = 0;
2716         t->num.idx_max = 0;
2717         t->snap[0] = 0.0f;
2718         t->snap[1] = (float)((5.0/180)*M_PI);
2719         t->snap[2] = t->snap[1] * 0.2f;
2720         
2721         if (t->flag & T_2D_EDIT)
2722                 t->flag |= T_NO_CONSTRAINT;
2723 }
2724
2725 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around) {
2726         float vec[3], totmat[3][3], smat[3][3];
2727         float eul[3], fmat[3][3], quat[4];
2728         float *center = t->center;
2729         
2730         /* local constraint shouldn't alter center */
2731         if (around == V3D_LOCAL) {
2732                 if (t->flag & (T_OBJECT|T_POSE)) {
2733                         center = td->center;
2734                 }
2735                 else {
2736                         if(around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2737                                 center = td->center;
2738                         }
2739                 }
2740         }
2741         
2742         if (t->flag & T_POINTS) {
2743                 mul_m3_m3m3(totmat, mat, td->mtx);
2744                 mul_m3_m3m3(smat, td->smtx, totmat);
2745                 
2746                 sub_v3_v3v3(vec, td->iloc, center);
2747                 mul_m3_v3(smat, vec);
2748                 
2749                 add_v3_v3v3(td->loc, vec, center);
2750                 
2751                 sub_v3_v3v3(vec,td->loc,td->iloc);
2752                 protectedTransBits(td->protectflag, vec);
2753                 add_v3_v3v3(td->loc, td->iloc, vec);
2754                 
2755                 
2756                 if(td->flag & TD_USEQUAT) {
2757                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, 0, 0, 0, 0, 0);
2758                         mat3_to_quat( quat,fmat);       // Actual transform
2759                         
2760                         if(td->ext->quat){
2761                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2762                                 
2763                                 /* is there a reason not to have this here? -jahka */
2764                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2765                         }
2766                 }
2767         }
2768         /**
2769          * HACK WARNING
2770          *
2771          * This is some VERY ugly special case to deal with pose mode.
2772          *
2773          * The problem is that mtx and smtx include each bone orientation.
2774          *
2775          * That is needed to rotate each bone properly, HOWEVER, to calculate
2776          * the translation component, we only need the actual armature object's
2777          * matrix (and inverse). That is not all though. Once the proper translation
2778          * has been computed, it has to be converted back into the bone's space.
2779          */
2780         else if (t->flag & T_POSE) {
2781                 float pmtx[3][3], imtx[3][3];
2782                 
2783                 // Extract and invert armature object matrix
2784                 copy_m3_m4(pmtx, t->poseobj->obmat);
2785                 invert_m3_m3(imtx, pmtx);
2786                 
2787                 if ((td->flag & TD_NO_LOC) == 0)
2788                 {
2789                         sub_v3_v3v3(vec, td->center, center);
2790                         
2791                         mul_m3_v3(pmtx, vec);   // To Global space
2792                         mul_m3_v3(mat, vec);            // Applying rotation
2793                         mul_m3_v3(imtx, vec);   // To Local space
2794                         
2795                         add_v3_v3v3(vec, vec, center);
2796                         /* vec now is the location where the object has to be */
2797                         
2798                         sub_v3_v3v3(vec, vec, td->center); // Translation needed from the initial location
2799                         
2800                         mul_m3_v3(pmtx, vec);   // To Global space
2801                         mul_m3_v3(td->smtx, vec);// To Pose space
2802                         
2803                         protectedTransBits(td->protectflag, vec);
2804                         
2805                         add_v3_v3v3(td->loc, td->iloc, vec);
2806                         
2807                         constraintTransLim(t, td);
2808                 }
2809                 
2810                 /* rotation */
2811                 if ((t->flag & T_V3D_ALIGN)==0) { // align mode doesn't rotate objects itself
2812                         /* euler or quaternion/axis-angle? */
2813                         if (td->rotOrder == ROT_MODE_QUAT) {
2814                                 mul_serie_m3(fmat, td->mtx, mat, td->smtx, 0, 0, 0, 0, 0);
2815                                 
2816                                 mat3_to_quat( quat,fmat);       // Actual transform
2817                                 
2818                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2819                                 /* this function works on end result */
2820                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2821                                 
2822                         }
2823                         else if (td->rotOrder == ROT_MODE_AXISANGLE) {
2824                                 /* calculate effect based on quats */
2825                                 float iquat[4], tquat[4];
2826                                 
2827                                 axis_angle_to_quat(iquat, td->ext->irotAxis, td->ext->irotAngle);
2828                                 
2829                                 mul_serie_m3(fmat, td->mtx, mat, td->smtx, 0, 0, 0, 0, 0);
2830                                 mat3_to_quat( quat,fmat);       // Actual transform
2831                                 mul_qt_qtqt(tquat, quat, iquat);
2832                                 
2833                                 quat_to_axis_angle( td->ext->rotAxis, td->ext->rotAngle,tquat); 
2834                                 
2835                                 /* this function works on end result */
2836                                 protectedAxisAngleBits(td->protectflag, td->ext->rotAxis, td->ext->rotAngle, td->ext->irotAxis, td->ext->irotAngle);
2837                         }
2838                         else { 
2839                                 float eulmat[3][3];
2840                                 
2841                                 mul_m3_m3m3(totmat, mat, td->mtx);
2842                                 mul_m3_m3m3(smat, td->smtx, totmat);
2843                                 
2844                                 /* calculate the total rotatation in eulers */
2845                                 VECCOPY(eul, td->ext->irot);
2846                                 eulO_to_mat3( eulmat,eul, td->rotOrder);
2847                                 
2848                                 /* mat = transform, obmat = bone rotation */
2849                                 mul_m3_m3m3(fmat, smat, eulmat);
2850                                 
2851                                 mat3_to_compatible_eulO( eul, td->ext->rot, td->rotOrder,fmat);
2852                                 
2853                                 /* and apply (to end result only) */
2854                                 protectedRotateBits(td->protectflag, eul, td->ext->irot);
2855                                 VECCOPY(td->ext->rot, eul);
2856                         }
2857                         
2858                         constraintRotLim(t, td);
2859                 }
2860         }
2861         else {
2862                 if ((td->flag & TD_NO_LOC) == 0)
2863                 {
2864                         /* translation */
2865                         sub_v3_v3v3(vec, td->center, center);
2866                         mul_m3_v3(mat, vec);
2867                         add_v3_v3v3(vec, vec, center);
2868                         /* vec now is the location where the object has to be */
2869                         sub_v3_v3v3(vec, vec, td->center);
2870                         mul_m3_v3(td->smtx, vec);
2871                         
2872                         protectedTransBits(td->protectflag, vec);
2873                         
2874                         add_v3_v3v3(td->loc, td->iloc, vec);
2875                 }
2876                 
2877                 
2878                 constraintTransLim(t, td);
2879                 
2880                 /* rotation */
2881                 if ((t->flag & T_V3D_ALIGN)==0) { // align mode doesn't rotate objects itself
2882                         /* euler or quaternion? */
2883                     if ((td->rotOrder == ROT_MODE_QUAT) || (td->flag & TD_USEQUAT)) {
2884                                 mul_serie_m3(fmat, td->mtx, mat, td->smtx, 0, 0, 0, 0, 0);
2885                                 mat3_to_quat( quat,fmat);       // Actual transform
2886                                 
2887                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2888                                 /* this function works on end result */
2889                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2890                         }
2891                         else if (td->rotOrder == ROT_MODE_AXISANGLE) {
2892                                 /* calculate effect based on quats */
2893                                 float iquat[4], tquat[4];
2894                                 
2895                                 axis_angle_to_quat(iquat, td->ext->irotAxis, td->ext->irotAngle);
2896                                 
2897                                 mul_serie_m3(fmat, td->mtx, mat, td->smtx, 0, 0, 0, 0, 0);
2898                                 mat3_to_quat( quat,fmat);       // Actual transform
2899                                 mul_qt_qtqt(tquat, quat, iquat);
2900                                 
2901                                 quat_to_axis_angle( td->ext->rotAxis, td->ext->rotAngle,quat); 
2902                                 
2903                                 /* this function works on end result */
2904                                 protectedAxisAngleBits(td->protectflag, td->ext->rotAxis, td->ext->rotAngle, td->ext->irotAxis, td->ext->irotAngle);
2905                         }
2906                         else {
2907                                 float obmat[3][3];
2908                                 
2909                                 mul_m3_m3m3(totmat, mat, td->mtx);
2910                                 mul_m3_m3m3(smat, td->smtx, totmat);
2911                                 
2912                                 /* calculate the total rotatation in eulers */
2913                                 add_v3_v3v3(eul, td->ext->irot, td->ext->drot); /* we have to correct for delta rot */
2914                                 eulO_to_mat3( obmat,eul, td->rotOrder);
2915                                 /* mat = transform, obmat = object rotation */
2916                                 mul_m3_m3m3(fmat, smat, obmat);
2917                                 
2918                                 mat3_to_compatible_eulO( eul, td->ext->rot, td->rotOrder,fmat);
2919                                 
2920                                 /* correct back for delta rot */
2921                                 sub_v3_v3v3(eul, eul, td->ext->drot);
2922                                 
2923                                 /* and apply */
2924                                 protectedRotateBits(td->protectflag, eul, td->ext->irot);
2925                                 VECCOPY(td->ext->rot, eul);
2926                         }
2927                         
2928                         constraintRotLim(t, td);
2929                 }
2930         }
2931 }
2932
2933 static void applyRotation(TransInfo *t, float angle, float axis[3])
2934 {
2935         TransData *td = t->data;
2936         float mat[3][3];