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