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