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