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