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