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