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