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