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