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