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