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