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