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