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