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