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