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