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