f972375a413175736333b613a68d1e0e1cebae32
[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                 /* SpaceClip *sc = CTX_wm_space_clip(C); */ /* UNUSED */
1595                 unit_m3(t->spacemtx);
1596                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1597                 t->options |= CTX_MOVIECLIP;
1598         }
1599         else
1600                 unit_m3(t->spacemtx);
1601
1602         createTransData(C, t);                  // make TransData structs from selection
1603
1604         if (t->total == 0) {
1605                 postTrans(C, t);
1606                 return 0;
1607         }
1608
1609         /* Stupid code to have Ctrl-Click on manipulator work ok */
1610         if (event) {
1611                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1612                 wmKeyMapItem *kmi;
1613
1614                 for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1615                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
1616                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
1617                                     (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1618                                     (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
1619                                     ((kmi->type == OSKEY) &&                         event->oskey) )
1620                                 {
1621                                         t->modifiers |= MOD_SNAP_INVERT;
1622                                 }
1623                                 break;
1624                         }
1625                 }
1626
1627         }
1628
1629         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1630
1631         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1632         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1633         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1634         mode = t->mode;
1635
1636         calculatePropRatio(t);
1637         calculateCenter(t);
1638
1639         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1640
1641         switch (mode) {
1642         case TFM_TRANSLATION:
1643                 initTranslation(t);
1644                 break;
1645         case TFM_ROTATION:
1646                 initRotation(t);
1647                 break;
1648         case TFM_RESIZE:
1649                 initResize(t);
1650                 break;
1651         case TFM_TOSPHERE:
1652                 initToSphere(t);
1653                 break;
1654         case TFM_SHEAR:
1655                 initShear(t);
1656                 break;
1657         case TFM_WARP:
1658                 initWarp(t);
1659                 break;
1660         case TFM_SHRINKFATTEN:
1661                 initShrinkFatten(t);
1662                 break;
1663         case TFM_TILT:
1664                 initTilt(t);
1665                 break;
1666         case TFM_CURVE_SHRINKFATTEN:
1667                 initCurveShrinkFatten(t);
1668                 break;
1669         case TFM_TRACKBALL:
1670                 initTrackball(t);
1671                 break;
1672         case TFM_PUSHPULL:
1673                 initPushPull(t);
1674                 break;
1675         case TFM_CREASE:
1676                 initCrease(t);
1677                 break;
1678         case TFM_BONESIZE:
1679                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1680                         bArmature *arm= t->poseobj->data;
1681                         if (arm->drawtype==ARM_ENVELOPE)
1682                                 initBoneEnvelope(t);
1683                         else
1684                                 initBoneSize(t);
1685                 }
1686                 break;
1687         case TFM_BONE_ENVELOPE:
1688                 initBoneEnvelope(t);
1689                 break;
1690         case TFM_EDGE_SLIDE:
1691                 initEdgeSlide(t);
1692                 break;
1693         case TFM_BONE_ROLL:
1694                 initBoneRoll(t);
1695                 break;
1696         case TFM_TIME_TRANSLATE:
1697                 initTimeTranslate(t);
1698                 break;
1699         case TFM_TIME_SLIDE:
1700                 initTimeSlide(t);
1701                 break;
1702         case TFM_TIME_SCALE:
1703                 initTimeScale(t);
1704                 break;
1705         case TFM_TIME_DUPLICATE:
1706                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1707                  * so that duplicate-culling will work properly
1708                  */
1709                 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1710                         initTranslation(t);
1711                 else
1712                         initTimeTranslate(t);
1713                 t->mode = mode;
1714                 break;
1715         case TFM_TIME_EXTEND:
1716                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1717                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1718                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1719                  * depending on which editor this was called from
1720                  */
1721                 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1722                         initTranslation(t);
1723                 else
1724                         initTimeTranslate(t);
1725                 break;
1726         case TFM_BAKE_TIME:
1727                 initBakeTime(t);
1728                 break;
1729         case TFM_MIRROR:
1730                 initMirror(t);
1731                 break;
1732         case TFM_BEVEL:
1733                 initBevel(t);
1734                 break;
1735         case TFM_BWEIGHT:
1736                 initBevelWeight(t);
1737                 break;
1738         case TFM_ALIGN:
1739                 initAlign(t);
1740                 break;
1741         case TFM_SEQ_SLIDE:
1742                 initSeqSlide(t);
1743                 break;
1744         }
1745
1746         if (t->state == TRANS_CANCEL) {
1747                 postTrans(C, t);
1748                 return 0;
1749         }
1750
1751
1752         /* overwrite initial values if operator supplied a non-null vector */
1753         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
1754                 float values[4]= {0}; /* in case value isn't length 4, avoid uninitialized memory  */
1755
1756                 if (RNA_property_array_check(prop)) {
1757                         RNA_float_get_array(op->ptr, "value", values);
1758                 }
1759                 else {
1760                         values[0]= RNA_float_get(op->ptr, "value");
1761                 }
1762
1763                 copy_v4_v4(t->values, values);
1764                 copy_v4_v4(t->auto_values, values);
1765                 t->flag |= T_AUTOVALUES;
1766         }
1767
1768         /* Transformation axis from operator */
1769         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
1770                 RNA_property_float_get_array(op->ptr, prop, t->axis);
1771                 normalize_v3(t->axis);
1772                 copy_v3_v3(t->axis_orig, t->axis);
1773         }
1774
1775         /* Constraint init from operator */
1776         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
1777                 int constraint_axis[3];
1778
1779                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
1780
1781                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
1782                         t->con.mode |= CON_APPLY;
1783
1784                         if (constraint_axis[0]) {
1785                                 t->con.mode |= CON_AXIS0;
1786                         }
1787                         if (constraint_axis[1]) {
1788                                 t->con.mode |= CON_AXIS1;
1789                         }
1790                         if (constraint_axis[2]) {
1791                                 t->con.mode |= CON_AXIS2;
1792                         }
1793
1794                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1795                 }
1796         }
1797
1798         t->context = NULL;
1799
1800         return 1;
1801 }
1802
1803 void transformApply(bContext *C, TransInfo *t)
1804 {
1805         t->context = C;
1806
1807         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
1808                 selectConstraint(t);
1809                 if (t->transform) {
1810                         t->transform(t, t->mval);  // calls recalcData()
1811                         viewRedrawForce(C, t);
1812                 }
1813                 t->redraw = TREDRAW_NOTHING;
1814         }
1815         else if (t->redraw & TREDRAW_SOFT) {
1816                 viewRedrawForce(C, t);
1817         }
1818
1819         /* If auto confirm is on, break after one pass */
1820         if (t->options & CTX_AUTOCONFIRM) {
1821                 t->state = TRANS_CONFIRM;
1822         }
1823
1824         if (BKE_ptcache_get_continue_physics()) {
1825                 // TRANSFORM_FIX_ME
1826                 //do_screenhandlers(G.curscreen);
1827                 t->redraw |= TREDRAW_HARD;
1828         }
1829
1830         t->context = NULL;
1831 }
1832
1833 static void drawTransformApply(const bContext *C, struct ARegion *UNUSED(ar), void *arg)
1834 {
1835         TransInfo *t = arg;
1836
1837         if (t->redraw & TREDRAW_SOFT) {
1838                 t->redraw |= TREDRAW_HARD;
1839                 transformApply((bContext *)C, t);
1840         }
1841 }
1842
1843 int transformEnd(bContext *C, TransInfo *t)
1844 {
1845         int exit_code = OPERATOR_RUNNING_MODAL;
1846
1847         t->context = C;
1848
1849         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
1850                 /* handle restoring objects */
1851                 if (t->state == TRANS_CANCEL) {
1852                         /* exception, edge slide transformed UVs too */
1853                         if (t->mode==TFM_EDGE_SLIDE)
1854                                 doEdgeSlide(t, 0.0f);
1855                         
1856                         exit_code = OPERATOR_CANCELLED;
1857                         restoreTransObjects(t); // calls recalcData()
1858                 }
1859                 else {
1860                         exit_code = OPERATOR_FINISHED;
1861                 }
1862
1863                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1864                 special_aftertrans_update(C, t);
1865
1866                 /* free data */
1867                 postTrans(C, t);
1868
1869                 /* send events out for redraws */
1870                 viewRedrawPost(C, t);
1871
1872                 /*  Undo as last, certainly after special_trans_update! */
1873
1874                 if (t->state == TRANS_CANCEL) {
1875 //                      if (t->undostr) ED_undo_push(C, t->undostr);
1876                 }
1877                 else {
1878 //                      if (t->undostr) ED_undo_push(C, t->undostr);
1879 //                      else ED_undo_push(C, transform_to_undostr(t));
1880                 }
1881                 t->undostr= NULL;
1882
1883                 viewRedrawForce(C, t);
1884         }
1885
1886         t->context = NULL;
1887
1888         return exit_code;
1889 }
1890
1891 /* ************************** TRANSFORM LOCKS **************************** */
1892
1893 static void protectedTransBits(short protectflag, float *vec)
1894 {
1895         if (protectflag & OB_LOCK_LOCX)
1896                 vec[0]= 0.0f;
1897         if (protectflag & OB_LOCK_LOCY)
1898                 vec[1]= 0.0f;
1899         if (protectflag & OB_LOCK_LOCZ)
1900                 vec[2]= 0.0f;
1901 }
1902
1903 static void protectedSizeBits(short protectflag, float *size)
1904 {
1905         if (protectflag & OB_LOCK_SCALEX)
1906                 size[0]= 1.0f;
1907         if (protectflag & OB_LOCK_SCALEY)
1908                 size[1]= 1.0f;
1909         if (protectflag & OB_LOCK_SCALEZ)
1910                 size[2]= 1.0f;
1911 }
1912
1913 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1914 {
1915         if (protectflag & OB_LOCK_ROTX)
1916                 eul[0]= oldeul[0];
1917         if (protectflag & OB_LOCK_ROTY)
1918                 eul[1]= oldeul[1];
1919         if (protectflag & OB_LOCK_ROTZ)
1920                 eul[2]= oldeul[2];
1921 }
1922
1923
1924 /* this function only does the delta rotation */
1925 /* axis-angle is usually internally stored as quats... */
1926 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1927 {
1928         /* check that protection flags are set */
1929         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1930                 return;
1931         
1932         if (protectflag & OB_LOCK_ROT4D) {
1933                 /* axis-angle getting limited as 4D entities that they are... */
1934                 if (protectflag & OB_LOCK_ROTW)
1935                         *angle= oldAngle;
1936                 if (protectflag & OB_LOCK_ROTX)
1937                         axis[0]= oldAxis[0];
1938                 if (protectflag & OB_LOCK_ROTY)
1939                         axis[1]= oldAxis[1];
1940                 if (protectflag & OB_LOCK_ROTZ)
1941                         axis[2]= oldAxis[2];
1942         }
1943         else {
1944                 /* axis-angle get limited with euler... */
1945                 float eul[3], oldeul[3];
1946                 
1947                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1948                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1949                 
1950                 if (protectflag & OB_LOCK_ROTX)
1951                         eul[0]= oldeul[0];
1952                 if (protectflag & OB_LOCK_ROTY)
1953                         eul[1]= oldeul[1];
1954                 if (protectflag & OB_LOCK_ROTZ)
1955                         eul[2]= oldeul[2];
1956                 
1957                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1958                 
1959                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1960                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1961                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1962                         axis[1]= 1.0f;
1963                 }
1964         }
1965 }
1966
1967 /* this function only does the delta rotation */
1968 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1969 {
1970         /* check that protection flags are set */
1971         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1972                 return;
1973         
1974         if (protectflag & OB_LOCK_ROT4D) {
1975                 /* quaternions getting limited as 4D entities that they are... */
1976                 if (protectflag & OB_LOCK_ROTW)
1977                         quat[0]= oldquat[0];
1978                 if (protectflag & OB_LOCK_ROTX)
1979                         quat[1]= oldquat[1];
1980                 if (protectflag & OB_LOCK_ROTY)
1981                         quat[2]= oldquat[2];
1982                 if (protectflag & OB_LOCK_ROTZ)
1983                         quat[3]= oldquat[3];
1984         }
1985         else {
1986                 /* quaternions get limited with euler... (compatibility mode) */
1987                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1988                 float qlen;
1989
1990                 qlen= normalize_qt_qt(nquat, quat);
1991                 normalize_qt_qt(noldquat, oldquat);
1992
1993                 quat_to_eul(eul, nquat);
1994                 quat_to_eul(oldeul, noldquat);
1995
1996                 if (protectflag & OB_LOCK_ROTX)
1997                         eul[0]= oldeul[0];
1998                 if (protectflag & OB_LOCK_ROTY)
1999                         eul[1]= oldeul[1];
2000                 if (protectflag & OB_LOCK_ROTZ)
2001                         eul[2]= oldeul[2];
2002
2003                 eul_to_quat( quat,eul);
2004
2005                 /* restore original quat size */
2006                 mul_qt_fl(quat, qlen);
2007                 
2008                 /* quaternions flip w sign to accumulate rotations correctly */
2009                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
2010                         mul_qt_fl(quat, -1.0f);
2011                 }
2012         }
2013 }
2014
2015 /* ******************* TRANSFORM LIMITS ********************** */
2016
2017 static void constraintTransLim(TransInfo *t, TransData *td)
2018 {
2019         if (td->con) {
2020                 bConstraintTypeInfo *ctiLoc= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
2021                 bConstraintTypeInfo *ctiDist= get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
2022                 
2023                 bConstraintOb cob= {NULL};
2024                 bConstraint *con;
2025                 float ctime = (float)(t->scene->r.cfra);
2026                 
2027                 /* Make a temporary bConstraintOb for using these limit constraints
2028                  *      - they only care that cob->matrix is correctly set ;-)
2029                  *      - current space should be local
2030                  */
2031                 unit_m4(cob.matrix);
2032                 copy_v3_v3(cob.matrix[3], td->loc);
2033                 
2034                 /* Evaluate valid constraints */
2035                 for (con= td->con; con; con= con->next) {
2036                         bConstraintTypeInfo *cti = NULL;
2037                         ListBase targets = {NULL, NULL};
2038                         float tmat[4][4];
2039                         
2040                         /* only consider constraint if enabled */
2041                         if (con->flag & CONSTRAINT_DISABLE) continue;
2042                         if (con->enforce == 0.0f) continue;
2043                         
2044                         /* only use it if it's tagged for this purpose (and the right type) */
2045                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2046                                 bLocLimitConstraint *data= con->data;
2047                                 
2048                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2049                                         continue;
2050                                 cti = ctiLoc;
2051                         }
2052                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2053                                 bDistLimitConstraint *data= con->data;
2054                                 
2055                                 if ((data->flag & LIMITDIST_TRANSFORM)==0)
2056                                         continue;
2057                                 cti = ctiDist;
2058                         }
2059                         
2060                         if (cti) {
2061                                 /* do space conversions */
2062                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2063                                         /* just multiply by td->mtx (this should be ok) */
2064                                         copy_m4_m4(tmat, cob.matrix);
2065                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2066                                 }
2067                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2068                                         /* skip... incompatable spacetype */
2069                                         continue;
2070                                 }
2071                                 
2072                                 /* get constraint targets if needed */
2073                                 get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2074                                 
2075                                 /* do constraint */
2076                                 cti->evaluate_constraint(con, &cob, &targets);
2077                                 
2078                                 /* convert spaces again */
2079                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2080                                         /* just multiply by td->mtx (this should be ok) */
2081                                         copy_m4_m4(tmat, cob.matrix);
2082                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2083                                 }
2084                                 
2085                                 /* free targets list */
2086                                 BLI_freelistN(&targets);
2087                         }
2088                 }
2089                 
2090                 /* copy results from cob->matrix */
2091                 copy_v3_v3(td->loc, cob.matrix[3]);
2092         }
2093 }
2094
2095 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2096 {
2097         /* Make a temporary bConstraintOb for use by limit constraints
2098          *      - they only care that cob->matrix is correctly set ;-)
2099          *      - current space should be local
2100          */
2101         memset(cob, 0, sizeof(bConstraintOb));
2102         if (td->ext) {
2103                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2104                         /* quats */
2105                         /* objects and bones do normalization first too, otherwise
2106                          * we don't necessarily end up with a rotation matrix, and
2107                          * then conversion back to quat gives a different result */
2108                         float quat[4];
2109                         normalize_qt_qt(quat, td->ext->quat);
2110                         quat_to_mat4(cob->matrix, quat);
2111                 }
2112                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2113                         /* axis angle */
2114                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2115                 }
2116                 else {
2117                         /* eulers */
2118                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2119                 }
2120         }
2121 }
2122
2123 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2124 {
2125         if (td->con) {
2126                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2127                 bConstraintOb cob;
2128                 bConstraint *con;
2129                 int dolimit = 0;
2130                 
2131                 /* Evaluate valid constraints */
2132                 for (con= td->con; con; con= con->next) {
2133                         /* only consider constraint if enabled */
2134                         if (con->flag & CONSTRAINT_DISABLE) continue;
2135                         if (con->enforce == 0.0f) continue;
2136                         
2137                         /* we're only interested in Limit-Rotation constraints */
2138                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2139                                 bRotLimitConstraint *data= con->data;
2140                                 float tmat[4][4];
2141                                 
2142                                 /* only use it if it's tagged for this purpose */
2143                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2144                                         continue;
2145
2146                                 /* skip incompatable spacetypes */
2147                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2148                                         continue;
2149
2150                                 /* only do conversion if necessary, to preserve quats and eulers */
2151                                 if (!dolimit) {
2152                                         constraintob_from_transdata(&cob, td);
2153                                         dolimit= 1;
2154                                 }
2155                                 
2156                                 /* do space conversions */
2157                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2158                                         /* just multiply by td->mtx (this should be ok) */
2159                                         copy_m4_m4(tmat, cob.matrix);
2160                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2161                                 }
2162                                 
2163                                 /* do constraint */
2164                                 cti->evaluate_constraint(con, &cob, NULL);
2165                                 
2166                                 /* convert spaces again */
2167                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2168                                         /* just multiply by td->mtx (this should be ok) */
2169                                         copy_m4_m4(tmat, cob.matrix);
2170                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2171                                 }
2172                         }
2173                 }
2174                 
2175                 if (dolimit) {
2176                         /* copy results from cob->matrix */
2177                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2178                                 /* quats */
2179                                 mat4_to_quat( td->ext->quat,cob.matrix);
2180                         }
2181                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2182                                 /* axis angle */
2183                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2184                         }
2185                         else {
2186                                 /* eulers */
2187                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2188                         }
2189                 }
2190         }
2191 }
2192
2193 static void constraintSizeLim(TransInfo *t, TransData *td)
2194 {
2195         if (td->con && td->ext) {
2196                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2197                 bConstraintOb cob= {NULL};
2198                 bConstraint *con;
2199                 
2200                 /* Make a temporary bConstraintOb for using these limit constraints
2201                  *      - they only care that cob->matrix is correctly set ;-)
2202                  *      - current space should be local
2203                  */
2204                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2205                         /* scale val and reset size */
2206                         return; // TODO: fix this case
2207                 }
2208                 else {
2209                         /* Reset val if SINGLESIZE but using a constraint */
2210                         if (td->flag & TD_SINGLESIZE)
2211                                 return;
2212                         
2213                         size_to_mat4( cob.matrix,td->ext->size);
2214                 }
2215                 
2216                 /* Evaluate valid constraints */
2217                 for (con= td->con; con; con= con->next) {
2218                         /* only consider constraint if enabled */
2219                         if (con->flag & CONSTRAINT_DISABLE) continue;
2220                         if (con->enforce == 0.0f) continue;
2221                         
2222                         /* we're only interested in Limit-Scale constraints */
2223                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2224                                 bSizeLimitConstraint *data= con->data;
2225                                 float tmat[4][4];
2226                                 
2227                                 /* only use it if it's tagged for this purpose */
2228                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2229                                         continue;
2230                                 
2231                                 /* do space conversions */
2232                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2233                                         /* just multiply by td->mtx (this should be ok) */
2234                                         copy_m4_m4(tmat, cob.matrix);
2235                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2236                                 }
2237                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2238                                         /* skip... incompatable spacetype */
2239                                         continue;
2240                                 }
2241                                 
2242                                 /* do constraint */
2243                                 cti->evaluate_constraint(con, &cob, NULL);
2244                                 
2245                                 /* convert spaces again */
2246                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2247                                         /* just multiply by td->mtx (this should be ok) */
2248                                         copy_m4_m4(tmat, cob.matrix);
2249                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2250                                 }
2251                         }
2252                 }
2253                 
2254                 /* copy results from cob->matrix */
2255                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2256                         /* scale val and reset size */
2257                         return; // TODO: fix this case
2258                 }
2259                 else {
2260                         /* Reset val if SINGLESIZE but using a constraint */
2261                         if (td->flag & TD_SINGLESIZE)
2262                                 return;
2263                         
2264                         mat4_to_size( td->ext->size,cob.matrix);
2265                 }
2266         }
2267 }
2268
2269 /* ************************** WARP *************************** */
2270
2271 static void postInputWarp(TransInfo *t, float values[3])
2272 {
2273         mul_v3_fl(values, (float)(M_PI * 2));
2274
2275         if (t->customData) { /* non-null value indicates reversed input */
2276                 negate_v3(values);
2277         }
2278 }
2279
2280 void initWarp(TransInfo *t)
2281 {
2282         float max[3], min[3];
2283         int i;
2284         
2285         t->mode = TFM_WARP;
2286         t->transform = Warp;
2287         t->handleEvent = handleEventWarp;
2288         
2289         setInputPostFct(&t->mouse, postInputWarp);
2290         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2291         
2292         t->idx_max = 0;
2293         t->num.idx_max = 0;
2294         t->snap[0] = 0.0f;
2295         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2296         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2297         
2298         t->num.increment = 1.0f;
2299
2300         t->flag |= T_NO_CONSTRAINT;
2301         
2302         /* we need min/max in view space */
2303         for (i = 0; i < t->total; i++) {
2304                 float center[3];
2305                 copy_v3_v3(center, t->data[i].center);
2306                 mul_m3_v3(t->data[i].mtx, center);
2307                 mul_m4_v3(t->viewmat, center);
2308                 sub_v3_v3(center, t->viewmat[3]);
2309                 if (i)
2310                         minmax_v3v3_v3(min, max, center);
2311                 else {
2312                         copy_v3_v3(max, center);
2313                         copy_v3_v3(min, center);
2314                 }
2315         }
2316
2317         mid_v3_v3v3(t->center, min, max);
2318
2319         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2320         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2321 }
2322
2323 int handleEventWarp(TransInfo *t, wmEvent *event)
2324 {
2325         int status = 0;
2326         
2327         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2328                 // Use customData pointer to signal warp direction
2329                 if      (t->customData == NULL)
2330                         t->customData = (void*)1;
2331                 else
2332                         t->customData = NULL;
2333                 
2334                 status = 1;
2335         }
2336         
2337         return status;
2338 }
2339
2340 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2341 {
2342         TransData *td = t->data;
2343         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2344         int i;
2345         char str[50];
2346         
2347         curs= give_cursor(t->scene, t->view);
2348         /*
2349          * gcursor is the one used for helpline.
2350          * It has to be in the same space as the drawing loop
2351          * (that means it needs to be in the object's space when in edit mode and
2352          *  in global space in object mode)
2353          *
2354          * cursor is used for calculations.
2355          * It needs to be in view space, but we need to take object's offset
2356          * into account if in Edit mode.
2357          */
2358         copy_v3_v3(cursor, curs);
2359         copy_v3_v3(gcursor, cursor);
2360         if (t->flag & T_EDIT) {
2361                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2362                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2363                 mul_m3_v3(t->data->smtx, gcursor);
2364         }
2365         mul_m4_v3(t->viewmat, cursor);
2366         sub_v3_v3(cursor, t->viewmat[3]);
2367         
2368         /* amount of radians for warp */
2369         circumfac = t->values[0];
2370         
2371         snapGrid(t, &circumfac);
2372         applyNumInput(&t->num, &circumfac);
2373         
2374         /* header print for NumInput */
2375         if (hasNumInput(&t->num)) {
2376                 char c[20];
2377                 
2378                 outputNumInput(&(t->num), c);
2379                 
2380                 sprintf(str, "Warp: %s", c);
2381
2382                 circumfac = DEG2RADF(circumfac);
2383         }
2384         else {
2385                 /* default header print */
2386                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2387         }
2388         
2389         t->values[0] = circumfac;
2390
2391         circumfac /= 2; /* only need 180 on each side to make 360 */
2392         
2393         for (i = 0; i < t->total; i++, td++) {
2394                 float loc[3];
2395                 if (td->flag & TD_NOACTION)
2396                         break;
2397                 
2398                 if (td->flag & TD_SKIP)
2399                         continue;
2400                 
2401                 /* translate point to center, rotate in such a way that outline==distance */
2402                 copy_v3_v3(vec, td->iloc);
2403                 mul_m3_v3(td->mtx, vec);
2404                 mul_m4_v3(t->viewmat, vec);
2405                 sub_v3_v3(vec, t->viewmat[3]);
2406                 
2407                 dist= vec[0]-cursor[0];
2408                 
2409                 /* t->val is X dimension projected boundbox */
2410                 phi0= (circumfac*dist/t->val);
2411                 
2412                 vec[1]= (vec[1]-cursor[1]);
2413                 
2414                 co= (float)cos(phi0);
2415                 si= (float)sin(phi0);
2416                 loc[0]= -si*vec[1]+cursor[0];
2417                 loc[1]= co*vec[1]+cursor[1];
2418                 loc[2]= vec[2];
2419                 
2420                 mul_m4_v3(t->viewinv, loc);
2421                 sub_v3_v3(loc, t->viewinv[3]);
2422                 mul_m3_v3(td->smtx, loc);
2423                 
2424                 sub_v3_v3(loc, td->iloc);
2425                 mul_v3_fl(loc, td->factor);
2426                 add_v3_v3v3(td->loc, td->iloc, loc);
2427         }
2428         
2429         recalcData(t);
2430         
2431         ED_area_headerprint(t->sa, str);
2432         
2433         return 1;
2434 }
2435
2436 /* ************************** SHEAR *************************** */
2437
2438 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2439 {
2440         mul_v3_fl(values, 0.05f);
2441 }
2442
2443 void initShear(TransInfo *t)
2444 {
2445         t->mode = TFM_SHEAR;
2446         t->transform = Shear;
2447         t->handleEvent = handleEventShear;
2448         
2449         setInputPostFct(&t->mouse, postInputShear);
2450         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2451         
2452         t->idx_max = 0;
2453         t->num.idx_max = 0;
2454         t->snap[0] = 0.0f;
2455         t->snap[1] = 0.1f;
2456         t->snap[2] = t->snap[1] * 0.1f;
2457         
2458         t->num.increment = 0.1f;
2459
2460         t->flag |= T_NO_CONSTRAINT;
2461 }
2462
2463 int handleEventShear(TransInfo *t, wmEvent *event)
2464 {
2465         int status = 0;
2466         
2467         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2468                 // Use customData pointer to signal Shear direction
2469                 if      (t->customData == NULL) {
2470                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2471                         t->customData = (void*)1;
2472                 }
2473                 else {
2474                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2475                         t->customData = NULL;
2476                 }
2477
2478                 status = 1;
2479         }
2480         
2481         return status;
2482 }
2483
2484
2485 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2486 {
2487         TransData *td = t->data;
2488         float vec[3];
2489         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2490         float value;
2491         int i;
2492         char str[50];
2493         
2494         copy_m3_m4(persmat, t->viewmat);
2495         invert_m3_m3(persinv, persmat);
2496         
2497         value = t->values[0];
2498         
2499         snapGrid(t, &value);
2500         
2501         applyNumInput(&t->num, &value);
2502         
2503         /* header print for NumInput */
2504         if (hasNumInput(&t->num)) {
2505                 char c[20];
2506                 
2507                 outputNumInput(&(t->num), c);
2508                 
2509                 sprintf(str, "Shear: %s %s", c, t->proptext);
2510         }
2511         else {
2512                 /* default header print */
2513                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2514         }
2515         
2516         t->values[0] = value;
2517
2518         unit_m3(smat);
2519         
2520         // Custom data signals shear direction
2521         if (t->customData == NULL)
2522                 smat[1][0] = value;
2523         else
2524                 smat[0][1] = value;
2525         
2526         mul_m3_m3m3(tmat, smat, persmat);
2527         mul_m3_m3m3(totmat, persinv, tmat);
2528         
2529         for (i = 0 ; i < t->total; i++, td++) {
2530                 if (td->flag & TD_NOACTION)
2531                         break;
2532                 
2533                 if (td->flag & TD_SKIP)
2534                         continue;
2535                 
2536                 if (t->obedit) {
2537                         float mat3[3][3];
2538                         mul_m3_m3m3(mat3, totmat, td->mtx);
2539                         mul_m3_m3m3(tmat, td->smtx, mat3);
2540                 }
2541                 else {
2542                         copy_m3_m3(tmat, totmat);
2543                 }
2544                 sub_v3_v3v3(vec, td->center, t->center);
2545                 
2546                 mul_m3_v3(tmat, vec);
2547                 
2548                 add_v3_v3(vec, t->center);
2549                 sub_v3_v3(vec, td->center);
2550                 
2551                 mul_v3_fl(vec, td->factor);
2552                 
2553                 add_v3_v3v3(td->loc, td->iloc, vec);
2554         }
2555         
2556         recalcData(t);
2557         
2558         ED_area_headerprint(t->sa, str);
2559
2560         return 1;
2561 }
2562
2563 /* ************************** RESIZE *************************** */
2564
2565 void initResize(TransInfo *t)
2566 {
2567         t->mode = TFM_RESIZE;
2568         t->transform = Resize;
2569         
2570         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2571         
2572         t->flag |= T_NULL_ONE;
2573         t->num.flag |= NUM_NULL_ONE;
2574         t->num.flag |= NUM_AFFECT_ALL;
2575         if (!t->obedit) {
2576                 t->flag |= T_NO_ZERO;
2577                 t->num.flag |= NUM_NO_ZERO;
2578         }
2579         
2580         t->idx_max = 2;
2581         t->num.idx_max = 2;
2582         t->snap[0] = 0.0f;
2583         t->snap[1] = 0.1f;
2584         t->snap[2] = t->snap[1] * 0.1f;
2585
2586         t->num.increment = t->snap[1];
2587 }
2588
2589 static void headerResize(TransInfo *t, float vec[3], char *str)
2590 {
2591         char tvec[60];
2592         char *spos= str;
2593         if (hasNumInput(&t->num)) {
2594                 outputNumInput(&(t->num), tvec);
2595         }
2596         else {
2597                 BLI_snprintf(&tvec[0],  20, "%.4f", vec[0]);
2598                 BLI_snprintf(&tvec[20], 20, "%.4f", vec[1]);
2599                 BLI_snprintf(&tvec[40], 20, "%.4f", vec[2]);
2600         }
2601         
2602         if (t->con.mode & CON_APPLY) {
2603                 switch (t->num.idx_max) {
2604                 case 0:
2605                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2606                         break;
2607                 case 1:
2608                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2609                         break;
2610                 case 2:
2611                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2612                 }
2613         }
2614         else {
2615                 if (t->flag & T_2D_EDIT)
2616                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2617                 else
2618                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2619         }
2620         
2621         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2622                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2623         }
2624
2625         (void)spos;
2626 }
2627
2628 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2629 #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)
2630
2631 /* smat is reference matrix, only scaled */
2632 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2633 {
2634         float vec[3];
2635         
2636         copy_v3_v3(vec, mat[0]);
2637         size[0]= normalize_v3(vec);
2638         copy_v3_v3(vec, mat[1]);
2639         size[1]= normalize_v3(vec);
2640         copy_v3_v3(vec, mat[2]);
2641         size[2]= normalize_v3(vec);
2642         
2643         /* first tried with dotproduct... but the sign flip is crucial */
2644         if ( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2645         if ( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2646         if ( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2647 }
2648
2649
2650 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3])
2651 {
2652         float tmat[3][3], smat[3][3], center[3];
2653         float vec[3];
2654         
2655         if (t->flag & T_EDIT) {
2656                 mul_m3_m3m3(smat, mat, td->mtx);
2657                 mul_m3_m3m3(tmat, td->smtx, smat);
2658         }
2659         else {
2660                 copy_m3_m3(tmat, mat);
2661         }
2662         
2663         if (t->con.applySize) {
2664                 t->con.applySize(t, td, tmat);
2665         }
2666         
2667         /* local constraint shouldn't alter center */
2668         if ((t->around == V3D_LOCAL) &&
2669                 (   (t->flag & (T_OBJECT|T_POSE)) ||
2670                     ((t->flag & T_EDIT) && (t->settings->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE))) ||
2671                     (t->obedit && t->obedit->type == OB_ARMATURE))
2672                 )
2673         {
2674                 copy_v3_v3(center, td->center);
2675         }
2676         else {
2677                 copy_v3_v3(center, t->center);
2678         }
2679
2680         if (td->ext) {
2681                 float fsize[3];
2682                 
2683                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2684                         float obsizemat[3][3];
2685                         // Reorient the size mat to fit the oriented object.
2686                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2687                         //print_m3("obsizemat", obsizemat);
2688                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2689                         //print_v3("fsize", fsize);
2690                 }
2691                 else {
2692                         mat3_to_size( fsize,tmat);
2693                 }
2694                 
2695                 protectedSizeBits(td->protectflag, fsize);
2696                 
2697                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2698                         if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2699                                 /* scale val and reset size */
2700                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2701                                 
2702                                 td->ext->size[0] = td->ext->isize[0];
2703                                 td->ext->size[1] = td->ext->isize[1];
2704                                 td->ext->size[2] = td->ext->isize[2];
2705                          }
2706                         else {
2707                                 /* Reset val if SINGLESIZE but using a constraint */
2708                                 if (td->flag & TD_SINGLESIZE)
2709                                          *td->val = td->ival;
2710                                 
2711                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2712                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2713                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2714                         }
2715                 }
2716                 
2717                 constraintSizeLim(t, td);
2718         }
2719         
2720         /* For individual element center, Editmode need to use iloc */
2721         if (t->flag & T_POINTS)
2722                 sub_v3_v3v3(vec, td->iloc, center);
2723         else
2724                 sub_v3_v3v3(vec, td->center, center);
2725         
2726         mul_m3_v3(tmat, vec);
2727         
2728         add_v3_v3(vec, center);
2729         if (t->flag & T_POINTS)
2730                 sub_v3_v3(vec, td->iloc);
2731         else
2732                 sub_v3_v3(vec, td->center);
2733         
2734         mul_v3_fl(vec, td->factor);
2735         
2736         if (t->flag & (T_OBJECT|T_POSE)) {
2737                 mul_m3_v3(td->smtx, vec);
2738         }
2739         
2740         protectedTransBits(td->protectflag, vec);
2741         add_v3_v3v3(td->loc, td->iloc, vec);
2742         
2743         constraintTransLim(t, td);
2744 }
2745
2746 int Resize(TransInfo *t, const int mval[2])
2747 {
2748         TransData *td;
2749         float size[3], mat[3][3];
2750         float ratio;
2751         int i;
2752         char str[200];
2753         
2754         /* for manipulator, center handle, the scaling can't be done relative to center */
2755         if ( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0) {
2756                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2757         }
2758         else {
2759                 ratio = t->values[0];
2760         }
2761         
2762         size[0] = size[1] = size[2] = ratio;
2763         
2764         snapGrid(t, size);
2765         
2766         if (hasNumInput(&t->num)) {
2767                 applyNumInput(&t->num, size);
2768                 constraintNumInput(t, size);
2769         }
2770         
2771         applySnapping(t, size);
2772         
2773         if (t->flag & T_AUTOVALUES) {
2774                 copy_v3_v3(size, t->auto_values);
2775         }
2776         
2777         copy_v3_v3(t->values, size);
2778         
2779         size_to_mat3( mat,size);
2780         
2781         if (t->con.applySize) {
2782                 t->con.applySize(t, NULL, mat);
2783         }
2784         
2785         copy_m3_m3(t->mat, mat);        // used in manipulator
2786         
2787         headerResize(t, size, str);
2788         
2789         for (i = 0, td=t->data; i < t->total; i++, td++) {
2790                 if (td->flag & TD_NOACTION)
2791                         break;
2792                 
2793                 if (td->flag & TD_SKIP)
2794                         continue;
2795                 
2796                 ElementResize(t, td, mat);
2797         }
2798         
2799         /* evil hack - redo resize if cliping needed */
2800         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2801                 size_to_mat3( mat,size);
2802                 
2803                 if (t->con.applySize)
2804                         t->con.applySize(t, NULL, mat);
2805                 
2806                 for (i = 0, td=t->data; i < t->total; i++, td++)
2807                         ElementResize(t, td, mat);
2808         }
2809         
2810         recalcData(t);
2811         
2812         ED_area_headerprint(t->sa, str);
2813         
2814         return 1;
2815 }
2816
2817 /* ************************** TOSPHERE *************************** */
2818
2819 void initToSphere(TransInfo *t)
2820 {
2821         TransData *td = t->data;
2822         int i;
2823         
2824         t->mode = TFM_TOSPHERE;
2825         t->transform = ToSphere;
2826         
2827         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2828         
2829         t->idx_max = 0;
2830         t->num.idx_max = 0;
2831         t->snap[0] = 0.0f;
2832         t->snap[1] = 0.1f;
2833         t->snap[2] = t->snap[1] * 0.1f;
2834         
2835         t->num.increment = t->snap[1];
2836
2837         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2838         t->flag |= T_NO_CONSTRAINT;
2839         
2840         // Calculate average radius
2841         for (i = 0 ; i < t->total; i++, td++) {
2842                 t->val += len_v3v3(t->center, td->iloc);
2843         }
2844         
2845         t->val /= (float)t->total;
2846 }
2847
2848 int ToSphere(TransInfo *t, const int UNUSED(mval[2]))
2849 {
2850         float vec[3];
2851         float ratio, radius;
2852         int i;
2853         char str[64];
2854         TransData *td = t->data;
2855         
2856         ratio = t->values[0];
2857         
2858         snapGrid(t, &ratio);
2859         
2860         applyNumInput(&t->num, &ratio);
2861         
2862         if (ratio < 0)
2863                 ratio = 0.0f;
2864         else if (ratio > 1)
2865                 ratio = 1.0f;
2866         
2867         t->values[0] = ratio;
2868
2869         /* header print for NumInput */
2870         if (hasNumInput(&t->num)) {
2871                 char c[20];
2872                 
2873                 outputNumInput(&(t->num), c);
2874                 
2875                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2876         }
2877         else {
2878                 /* default header print */
2879                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2880         }
2881         
2882         
2883         for (i = 0 ; i < t->total; i++, td++) {
2884                 float tratio;
2885                 if (td->flag & TD_NOACTION)
2886                         break;
2887                 
2888                 if (td->flag & TD_SKIP)
2889                         continue;
2890                 
2891                 sub_v3_v3v3(vec, td->iloc, t->center);
2892                 
2893                 radius = normalize_v3(vec);
2894                 
2895                 tratio = ratio * td->factor;
2896                 
2897                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2898                 
2899                 add_v3_v3v3(td->loc, t->center, vec);
2900         }
2901         
2902         
2903         recalcData(t);
2904         
2905         ED_area_headerprint(t->sa, str);
2906         
2907         return 1;
2908 }
2909
2910 /* ************************** ROTATION *************************** */
2911
2912
2913 static void postInputRotation(TransInfo *t, float values[3])
2914 {
2915         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2916                 t->con.applyRot(t, NULL, t->axis, values);
2917         }
2918 }
2919
2920 void initRotation(TransInfo *t)
2921 {
2922         t->mode = TFM_ROTATION;
2923         t->transform = Rotation;
2924         
2925         setInputPostFct(&t->mouse, postInputRotation);
2926         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2927         
2928         t->idx_max = 0;
2929         t->num.idx_max = 0;
2930         t->snap[0] = 0.0f;
2931         t->snap[1] = (float)((5.0/180)*M_PI);
2932         t->snap[2] = t->snap[1] * 0.2f;
2933         
2934         t->num.increment = 1.0f;
2935
2936         if (t->flag & T_2D_EDIT)
2937                 t->flag |= T_NO_CONSTRAINT;
2938
2939         negate_v3_v3(t->axis, t->viewinv[2]);
2940         normalize_v3(t->axis);
2941
2942         copy_v3_v3(t->axis_orig, t->axis);
2943 }
2944
2945 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around)
2946 {
2947         float vec[3], totmat[3][3], smat[3][3];
2948         float eul[3], fmat[3][3], quat[4];
2949         float *center = t->center;
2950
2951         /* local constraint shouldn't alter center */
2952         if (around == V3D_LOCAL) {
2953                 if (    (t->flag & (T_OBJECT|T_POSE)) ||
2954                     (t->settings->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) ||
2955                         (t->obedit && t->obedit->type == OB_ARMATURE))
2956                 {
2957      &