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