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) && ISMOUSE(t->launch_event)) {
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->context = C;
1567
1568         t->state = TRANS_RUNNING;
1569
1570         /* avoid calculating PET */
1571         t->options = CTX_NO_PET;
1572
1573         t->mode = TFM_DUMMY;
1574
1575         initTransInfo(C, t, NULL, NULL);
1576
1577         /* avoid doing connectivity lookups (when V3D_AROUND_LOCAL_ORIGINS is set) */
1578         t->around = V3D_AROUND_CENTER_BOUNDS;
1579
1580         createTransData(C, t);              // make TransData structs from selection
1581
1582         t->around = centerMode;             // override userdefined mode
1583
1584         if (t->data_len_all == 0) {
1585                 success = false;
1586         }
1587         else {
1588                 success = true;
1589
1590                 calculateCenter(t);
1591
1592                 if (cent2d) {
1593                         copy_v2_v2(cent2d, t->center2d);
1594                 }
1595
1596                 if (cent3d) {
1597                         // Copy center from constraint center. Transform center can be local
1598                         copy_v3_v3(cent3d, t->center_global);
1599                 }
1600         }
1601
1602
1603         /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
1604         special_aftertrans_update(C, t);
1605
1606         postTrans(C, t);
1607
1608         MEM_freeN(t);
1609
1610         return success;
1611 }
1612
1613 typedef enum {
1614         UP,
1615         DOWN,
1616         LEFT,
1617         RIGHT
1618 } ArrowDirection;
1619
1620 #define POS_INDEX 0
1621 /* NOTE: this --^ is a bit hackish, but simplifies Gwn_VertFormat usage among functions
1622  * private to this file  - merwin
1623  */
1624
1625 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1626 {
1627         immBegin(GWN_PRIM_LINES, 6);
1628
1629         switch (d) {
1630                 case LEFT:
1631                         offset = -offset;
1632                         length = -length;
1633                         size = -size;
1634                         ATTR_FALLTHROUGH;
1635                 case RIGHT:
1636                         immVertex2f(POS_INDEX, offset, 0);
1637                         immVertex2f(POS_INDEX, offset + length, 0);
1638                         immVertex2f(POS_INDEX, offset + length, 0);
1639                         immVertex2f(POS_INDEX, offset + length - size, -size);
1640                         immVertex2f(POS_INDEX, offset + length, 0);
1641                         immVertex2f(POS_INDEX, offset + length - size,  size);
1642                         break;
1643
1644                 case DOWN:
1645                         offset = -offset;
1646                         length = -length;
1647                         size = -size;
1648                         ATTR_FALLTHROUGH;
1649                 case UP:
1650                         immVertex2f(POS_INDEX, 0, offset);
1651                         immVertex2f(POS_INDEX, 0, offset + length);
1652                         immVertex2f(POS_INDEX, 0, offset + length);
1653                         immVertex2f(POS_INDEX, -size, offset + length - size);
1654                         immVertex2f(POS_INDEX, 0, offset + length);
1655                         immVertex2f(POS_INDEX, size, offset + length - size);
1656                         break;
1657         }
1658
1659         immEnd();
1660 }
1661
1662 static void drawArrowHead(ArrowDirection d, short size)
1663 {
1664         immBegin(GWN_PRIM_LINES, 4);
1665
1666         switch (d) {
1667                 case LEFT:
1668                         size = -size;
1669                         ATTR_FALLTHROUGH;
1670                 case RIGHT:
1671                         immVertex2f(POS_INDEX, 0, 0);
1672                         immVertex2f(POS_INDEX, -size, -size);
1673                         immVertex2f(POS_INDEX, 0, 0);
1674                         immVertex2f(POS_INDEX, -size,  size);
1675                         break;
1676
1677                 case DOWN:
1678                         size = -size;
1679                         ATTR_FALLTHROUGH;
1680                 case UP:
1681                         immVertex2f(POS_INDEX, 0, 0);
1682                         immVertex2f(POS_INDEX, -size, -size);
1683                         immVertex2f(POS_INDEX, 0, 0);
1684                         immVertex2f(POS_INDEX, size, -size);
1685                         break;
1686         }
1687
1688         immEnd();
1689 }
1690
1691 static void drawArc(float size, float angle_start, float angle_end, int segments)
1692 {
1693         float delta = (angle_end - angle_start) / segments;
1694         float angle;
1695         int a;
1696
1697         immBegin(GWN_PRIM_LINE_STRIP, segments + 1);
1698
1699         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1700                 immVertex2f(POS_INDEX, cosf(angle) * size, sinf(angle) * size);
1701         }
1702         immVertex2f(POS_INDEX, cosf(angle_end) * size, sinf(angle_end) * size);
1703
1704         immEnd();
1705 }
1706
1707 static int helpline_poll(bContext *C)
1708 {
1709         ARegion *ar = CTX_wm_region(C);
1710
1711         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1712                 return 1;
1713         return 0;
1714 }
1715
1716 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1717 {
1718         TransInfo *t = (TransInfo *)customdata;
1719
1720         if (t->helpline != HLP_NONE) {
1721                 float cent[2];
1722                 float mval[3] = {
1723                     x,
1724                     y,
1725                     0.0f,
1726                 };
1727                 float tmval[2] = {
1728                     (float)t->mval[0],
1729                     (float)t->mval[1],
1730                 };
1731
1732                 projectFloatViewEx(t, t->center_global, cent, V3D_PROJ_TEST_CLIP_ZERO);
1733
1734                 /* Offset the values for the area region. */
1735                 const float offset[2] = {
1736                     t->ar->winrct.xmin,
1737                     t->ar->winrct.ymin,
1738                 };
1739
1740                 for (int i = 0; i < 2; i++) {
1741                         cent[i] += offset[i];
1742                         tmval[i] += offset[i];
1743                 }
1744
1745                 gpuPushMatrix();
1746
1747                 /* Dashed lines first. */
1748                 if (ELEM(t->helpline, HLP_SPRING, HLP_ANGLE)) {
1749                         const uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1750
1751                         UNUSED_VARS_NDEBUG(shdr_pos); /* silence warning */
1752                         BLI_assert(shdr_pos == POS_INDEX);
1753
1754                         glLineWidth(1.0f);
1755
1756                         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
1757
1758                         float viewport_size[4];
1759                         glGetFloatv(GL_VIEWPORT, viewport_size);
1760                         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
1761
1762                         immUniform1i("num_colors", 0);  /* "simple" mode */
1763                         immUniformThemeColor(TH_VIEW_OVERLAY);
1764                         immUniform1f("dash_width", 6.0f);
1765                         immUniform1f("dash_factor", 0.5f);
1766
1767                         immBegin(GWN_PRIM_LINES, 2);
1768                         immVertex2fv(POS_INDEX, cent);
1769                         immVertex2f(POS_INDEX, tmval[0], tmval[1]);
1770                         immEnd();
1771
1772                         immUnbindProgram();
1773                 }
1774
1775                 /* And now, solid lines. */
1776                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1777                 UNUSED_VARS_NDEBUG(pos); /* silence warning */
1778                 BLI_assert(pos == POS_INDEX);
1779                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1780
1781                 switch (t->helpline) {
1782                         case HLP_SPRING:
1783                                 immUniformThemeColor(TH_VIEW_OVERLAY);
1784
1785                                 gpuTranslate3fv(mval);
1786                                 gpuRotateAxis(-RAD2DEGF(atan2f(cent[0] - tmval[0], cent[1] - tmval[1])), 'Z');
1787
1788                                 glLineWidth(3.0f);
1789                                 drawArrow(UP, 5, 10, 5);
1790                                 drawArrow(DOWN, 5, 10, 5);
1791                                 break;
1792                         case HLP_HARROW:
1793                                 immUniformThemeColor(TH_VIEW_OVERLAY);
1794                                 gpuTranslate3fv(mval);
1795
1796                                 glLineWidth(3.0f);
1797                                 drawArrow(RIGHT, 5, 10, 5);
1798                                 drawArrow(LEFT, 5, 10, 5);
1799                                 break;
1800                         case HLP_VARROW:
1801                                 immUniformThemeColor(TH_VIEW_OVERLAY);
1802
1803                                 gpuTranslate3fv(mval);
1804
1805                                 glLineWidth(3.0f);
1806                                 drawArrow(UP, 5, 10, 5);
1807                                 drawArrow(DOWN, 5, 10, 5);
1808                                 break;
1809                         case HLP_ANGLE:
1810                         {
1811                                 float dx = tmval[0] - cent[0], dy = tmval[1] - cent[1];
1812                                 float angle = atan2f(dy, dx);
1813                                 float dist = hypotf(dx, dy);
1814                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1815                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1816
1817                                 immUniformThemeColor(TH_VIEW_OVERLAY);
1818
1819                                 gpuTranslate3f(cent[0] - tmval[0] + mval[0], cent[1] - tmval[1] + mval[1], 0);
1820
1821                                 glLineWidth(3.0f);
1822                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1823                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1824
1825                                 gpuPushMatrix();
1826
1827                                 gpuTranslate3f(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1828                                 gpuRotateAxis(RAD2DEGF(angle - delta_angle), 'Z');
1829
1830                                 drawArrowHead(DOWN, 5);
1831
1832                                 gpuPopMatrix();
1833
1834                                 gpuTranslate3f(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1835                                 gpuRotateAxis(RAD2DEGF(angle + delta_angle), 'Z');
1836
1837                                 drawArrowHead(UP, 5);
1838                                 break;
1839                         }
1840                         case HLP_TRACKBALL:
1841                         {
1842                                 unsigned char col[3], col2[3];
1843                                 UI_GetThemeColor3ubv(TH_GRID, col);
1844
1845                                 gpuTranslate3fv(mval);
1846
1847                                 glLineWidth(3.0f);
1848
1849                                 UI_make_axis_color(col, col2, 'X');
1850                                 immUniformColor3ubv((GLubyte *)col2);
1851
1852                                 drawArrow(RIGHT, 5, 10, 5);
1853                                 drawArrow(LEFT, 5, 10, 5);
1854
1855                                 UI_make_axis_color(col, col2, 'Y');
1856                                 immUniformColor3ubv((GLubyte *)col2);
1857
1858                                 drawArrow(UP, 5, 10, 5);
1859                                 drawArrow(DOWN, 5, 10, 5);
1860                                 break;
1861                         }
1862                 }
1863
1864                 immUnbindProgram();
1865                 gpuPopMatrix();
1866         }
1867 }
1868
1869 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1870 {
1871         TransInfo *t = arg;
1872
1873         glLineWidth(1.0f);
1874
1875         drawConstraint(t);
1876         drawPropCircle(C, t);
1877         drawSnapping(C, t);
1878
1879         /* edge slide, vert slide */
1880         drawEdgeSlide(t);
1881         drawVertSlide(t);
1882 }
1883
1884 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1885 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1886 {
1887         rcti rect;
1888         const char *printable = IFACE_("Auto Keying On");
1889         float      printable_size[2];
1890         int xco, yco;
1891
1892         ED_region_visible_rect(ar, &rect);
1893
1894         const int font_id = BLF_default();
1895         BLF_width_and_height(font_id, printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1896
1897         xco = (rect.xmax - U.widget_unit) - (int)printable_size[0];
1898         yco = (rect.ymax - U.widget_unit);
1899
1900         /* warning text (to clarify meaning of overlays)
1901          * - original color was red to match the icon, but that clashes badly with a less nasty border
1902          */
1903         unsigned char color[3];
1904         UI_GetThemeColorShade3ubv(TH_TEXT_HI, -50, color);
1905         BLF_color3ubv(font_id, color);
1906 #ifdef WITH_INTERNATIONAL
1907         BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1908 #else
1909         BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1910 #endif
1911
1912         /* autokey recording icon... */
1913         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1914         glEnable(GL_BLEND);
1915
1916         xco -= U.widget_unit;
1917         yco -= (int)printable_size[1] / 2;
1918
1919         UI_icon_draw(xco, yco, ICON_REC);
1920
1921         glDisable(GL_BLEND);
1922 }
1923
1924 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1925 {
1926         TransInfo *t = arg;
1927         Scene *scene = t->scene;
1928         ViewLayer *view_layer = t->view_layer;
1929         Object *ob = OBACT(view_layer);
1930
1931         /* draw autokeyframing hint in the corner
1932          * - only draw if enabled (advanced users may be distracted/annoyed),
1933          *   for objects that will be autokeyframed (no point ohterwise),
1934          *   AND only for the active region (as showing all is too overwhelming)
1935          */
1936         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1937                 if (ar == t->ar) {
1938                         if (t->flag & (T_OBJECT | T_POSE)) {
1939                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1940                                         drawAutoKeyWarning(t, ar);
1941                                 }
1942                         }
1943                 }
1944         }
1945 }
1946
1947 /**
1948  * \see #initTransform which reads values from the operator.
1949  */
1950 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1951 {
1952         ToolSettings *ts = CTX_data_tool_settings(C);
1953         int constraint_axis[3] = {0, 0, 0};
1954         int proportional = 0;
1955         PropertyRNA *prop;
1956
1957         // Save back mode in case we're in the generic operator
1958         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1959                 RNA_property_enum_set(op->ptr, prop, t->mode);
1960         }
1961
1962         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1963                 float values[4];
1964
1965                 copy_v4_v4(values, (t->flag & T_AUTOVALUES) ? t->auto_values : t->values);
1966
1967                 if (RNA_property_array_check(prop)) {
1968                         RNA_property_float_set_array(op->ptr, prop, values);
1969                 }
1970                 else {
1971                         RNA_property_float_set(op->ptr, prop, values[0]);
1972                 }
1973         }
1974
1975         /* convert flag to enum */
1976         switch (t->flag & T_PROP_EDIT_ALL) {
1977                 case T_PROP_EDIT:
1978                         proportional = PROP_EDIT_ON;
1979                         break;
1980                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1981                         proportional = PROP_EDIT_CONNECTED;
1982                         break;
1983                 case (T_PROP_EDIT | T_PROP_PROJECTED):
1984                         proportional = PROP_EDIT_PROJECTED;
1985                         break;
1986                 default:
1987                         proportional = PROP_EDIT_OFF;
1988                         break;
1989         }
1990
1991         // If modal, save settings back in scene if not set as operator argument
1992         if ((t->flag & T_MODAL) || (op->flag & OP_IS_REPEAT)) {
1993                 /* save settings if not set in operator */
1994
1995                 /* skip saving proportional edit if it was not actually used */
1996                 if (!(t->options & CTX_NO_PET)) {
1997                         if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1998                             !RNA_property_is_set(op->ptr, prop))
1999                         {
2000                                 if (t->spacetype == SPACE_IPO)
2001                                         ts->proportional_fcurve = proportional;
2002                                 else if (t->spacetype == SPACE_ACTION)
2003                                         ts->proportional_action = proportional;
2004                                 else if (t->obedit_type != -1)
2005                                         ts->proportional = proportional;
2006                                 else if (t->options & CTX_MASK)
2007                                         ts->proportional_mask = (proportional != PROP_EDIT_OFF);
2008                                 else
2009                                         ts->proportional_objects = (proportional != PROP_EDIT_OFF);
2010                         }
2011
2012                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_size"))) {
2013                                 ts->proportional_size =
2014                                         RNA_property_is_set(op->ptr, prop) ? RNA_property_float_get(op->ptr, prop) : t->prop_size;
2015                         }
2016
2017                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
2018                             !RNA_property_is_set(op->ptr, prop))
2019                         {
2020                                 ts->prop_mode = t->prop_mode;
2021                         }
2022                 }
2023
2024                 /* do we check for parameter? */
2025                 if (t->modifiers & MOD_SNAP) {
2026                         ts->snap_flag |= SCE_SNAP;
2027                 }
2028                 else {
2029                         ts->snap_flag &= ~SCE_SNAP;
2030                 }
2031
2032                 if (t->spacetype == SPACE_VIEW3D) {
2033                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
2034                             !RNA_property_is_set(op->ptr, prop))
2035                         {
2036                                 t->scene->orientation_type = t->current_orientation;
2037                                 BLI_assert(((t->scene->orientation_index_custom == -1) && (t->custom_orientation == NULL)) ||
2038                                            (BKE_scene_transform_orientation_get_index(
2039                                                     t->scene, t->custom_orientation) == t->scene->orientation_index_custom));
2040                         }
2041                 }
2042         }
2043
2044         if ((prop = RNA_struct_find_property(op->ptr, "proportional"))) {
2045                 RNA_property_enum_set(op->ptr, prop, proportional);
2046                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
2047                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
2048         }
2049
2050         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
2051                 RNA_property_float_set_array(op->ptr, prop, t->axis);
2052         }
2053
2054         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
2055                 RNA_property_boolean_set(op->ptr, prop, (t->flag & T_MIRROR) != 0);
2056         }
2057
2058         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
2059                 /* constraint orientation can be global, even if user selects something else
2060                  * so use the orientation in the constraint if set */
2061                 short orientation = (t->con.mode & CON_APPLY) ? t->con.orientation : t->current_orientation;
2062
2063                 if (orientation == V3D_MANIP_CUSTOM) {
2064                         const int orientation_index_custom = BKE_scene_transform_orientation_get_index(
2065                                 t->scene, t->custom_orientation);
2066
2067                         /* Maybe we need a t->con.custom_orientation? Seems like it would always match t->custom_orientation. */
2068                         orientation = V3D_MANIP_CUSTOM + orientation_index_custom;
2069                         BLI_assert(orientation >= V3D_MANIP_CUSTOM);
2070                 }
2071                 RNA_enum_set(op->ptr, "constraint_orientation", orientation);
2072
2073                 if (t->con.mode & CON_APPLY) {
2074                         if (t->con.mode & CON_AXIS0) {
2075                                 constraint_axis[0] = 1;
2076                         }
2077                         if (t->con.mode & CON_AXIS1) {
2078                                 constraint_axis[1] = 1;
2079                         }
2080                         if (t->con.mode & CON_AXIS2) {
2081                                 constraint_axis[2] = 1;
2082                         }
2083                 }
2084
2085                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
2086         }
2087
2088         {
2089                 const char *prop_id = NULL;
2090                 if (t->mode == TFM_SHRINKFATTEN) {
2091                         prop_id = "use_even_offset";
2092                 }
2093
2094                 if (prop_id && (prop = RNA_struct_find_property(op->ptr, prop_id))) {
2095
2096                         RNA_property_boolean_set(op->ptr, prop, (t->flag & T_ALT_TRANSFORM) != 0);
2097                 }
2098         }
2099
2100         if ((prop = RNA_struct_find_property(op->ptr, "correct_uv"))) {
2101                 RNA_property_boolean_set(op->ptr, prop, (t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) != 0);
2102         }
2103 }
2104
2105 /**
2106  * \note  caller needs to free 't' on a 0 return
2107  * \warning  \a event might be NULL (when tweaking from redo panel)
2108  * \see #saveTransform which writes these values back.
2109  */
2110 bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event, int mode)
2111 {
2112         int options = 0;
2113         PropertyRNA *prop;
2114
2115         t->context = C;
2116
2117         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
2118
2119         t->state = TRANS_STARTING;
2120
2121         if ((prop = RNA_struct_find_property(op->ptr, "cursor_transform")) && RNA_property_is_set(op->ptr, prop)) {
2122                 if (RNA_property_boolean_get(op->ptr, prop)) {
2123                         options |= CTX_CURSOR;
2124                 }
2125         }
2126
2127         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
2128                 if (RNA_property_boolean_get(op->ptr, prop)) {
2129                         options |= CTX_TEXTURE;
2130                 }
2131         }
2132
2133         if ((prop = RNA_struct_find_property(op->ptr, "gpencil_strokes")) && RNA_property_is_set(op->ptr, prop)) {
2134                 if (RNA_property_boolean_get(op->ptr, prop)) {
2135                         options |= CTX_GPENCIL_STROKES;
2136                 }
2137         }
2138
2139         t->options = options;
2140
2141         t->mode = mode;
2142
2143         /* Needed to translate tweak events to mouse buttons. */
2144         t->launch_event = event ? WM_userdef_event_type_from_keymap_type(event->type) : -1;
2145
2146         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
2147         // For manipulator only, so assume LEFTMOUSE
2148         if (t->launch_event == 0) {
2149                 t->launch_event = LEFTMOUSE;
2150         }
2151
2152         unit_m3(t->spacemtx);
2153
2154         initTransInfo(C, t, op, event);
2155         initTransformOrientation(C, t);
2156
2157         if (t->spacetype == SPACE_VIEW3D) {
2158                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
2159                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2160                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2161                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2162         }
2163         else if (t->spacetype == SPACE_IMAGE) {
2164                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2165                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2166                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2167         }
2168         else if (t->spacetype == SPACE_CLIP) {
2169                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2170                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2171         }
2172         else if (t->spacetype == SPACE_NODE) {
2173                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
2174                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2175                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2176         }
2177         else if (t->spacetype == SPACE_IPO) {
2178                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2179                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2180                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2181         }
2182         else if (t->spacetype == SPACE_ACTION) {
2183                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2184                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2185                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2186         }
2187
2188         createTransData(C, t);          // make TransData structs from selection
2189
2190         if (t->data_len_all == 0) {
2191                 postTrans(C, t);
2192                 return 0;
2193         }
2194
2195         if (event) {
2196                 /* keymap for shortcut header prints */
2197                 t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
2198
2199                 /* Stupid code to have Ctrl-Click on manipulator work ok
2200                  *
2201                  * do this only for translation/rotation/resize due to only this
2202                  * moded are available from manipulator and doing such check could
2203                  * lead to keymap conflicts for other modes (see #31584)
2204                  */
2205                 if (ELEM(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
2206                         wmKeyMapItem *kmi;
2207
2208                         for (kmi = t->keymap->items.first; kmi; kmi = kmi->next) {
2209                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
2210                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
2211                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
2212                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
2213                                             ((kmi->type == OSKEY) &&                         event->oskey) )
2214                                         {
2215                                                 t->modifiers |= MOD_SNAP_INVERT;
2216                                         }
2217                                         break;
2218                                 }
2219                         }
2220                 }
2221         }
2222
2223         initSnapping(t, op); // Initialize snapping data AFTER mode flags
2224
2225         initSnapSpatial(t, t->snap_spatial);
2226
2227         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
2228         /* EVIL2: we gave as argument also texture space context bit... was cleared */
2229         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
2230         mode = t->mode;
2231
2232         calculatePropRatio(t);
2233         calculateCenter(t);
2234
2235         /* Overwrite initial values if operator supplied a non-null vector.
2236          *
2237          * Run before init functions so 'values_modal_offset' can be applied on mouse input.
2238          */
2239         BLI_assert(is_zero_v4(t->values_modal_offset));
2240         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2241                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2242
2243                 if (RNA_property_array_check(prop)) {
2244                         RNA_float_get_array(op->ptr, "value", values);
2245                 }
2246                 else {
2247                         values[0] = RNA_float_get(op->ptr, "value");
2248                 }
2249
2250                 copy_v4_v4(t->values, values);
2251
2252                 if (t->flag & T_MODAL) {
2253                         copy_v4_v4(t->values_modal_offset, values);
2254                         t->redraw = TREDRAW_HARD;
2255                 }
2256                 else {
2257                         copy_v4_v4(t->auto_values, values);
2258                         t->flag |= T_AUTOVALUES;
2259                 }
2260         }
2261
2262         /* Transformation axis from operator */
2263         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2264                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2265                 normalize_v3(t->axis);
2266                 copy_v3_v3(t->axis_orig, t->axis);
2267         }
2268
2269         /* Constraint init from operator */
2270         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2271                 int constraint_axis[3];
2272
2273                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2274
2275                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2276                         t->con.mode |= CON_APPLY;
2277
2278                         if (constraint_axis[0]) {
2279                                 t->con.mode |= CON_AXIS0;
2280                         }
2281                         if (constraint_axis[1]) {
2282                                 t->con.mode |= CON_AXIS1;
2283                         }
2284                         if (constraint_axis[2]) {
2285                                 t->con.mode |= CON_AXIS2;
2286                         }
2287
2288                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2289                 }
2290         }
2291
2292         if (event) {
2293                 /* Initialize accurate transform to settings requested by keymap. */
2294                 bool use_accurate = false;
2295                 if ((prop = RNA_struct_find_property(op->ptr, "use_accurate")) && RNA_property_is_set(op->ptr, prop)) {
2296                         if (RNA_property_boolean_get(op->ptr, prop)) {
2297                                 use_accurate = true;
2298                         }
2299                 }
2300                 initMouseInput(t, &t->mouse, t->center2d, event->mval, use_accurate);
2301         }
2302
2303         switch (mode) {
2304                 case TFM_TRANSLATION:
2305                         initTranslation(t);
2306                         break;
2307                 case TFM_ROTATION:
2308                         initRotation(t);
2309                         break;
2310                 case TFM_RESIZE:
2311                         initResize(t);
2312                         break;
2313                 case TFM_SKIN_RESIZE:
2314                         initSkinResize(t);
2315                         break;
2316                 case TFM_TOSPHERE:
2317                         initToSphere(t);
2318                         break;
2319                 case TFM_SHEAR:
2320                         initShear(t);
2321                         break;
2322                 case TFM_BEND:
2323                         initBend(t);
2324                         break;
2325                 case TFM_SHRINKFATTEN:
2326                         initShrinkFatten(t);
2327                         break;
2328                 case TFM_TILT:
2329                         initTilt(t);
2330                         break;
2331                 case TFM_CURVE_SHRINKFATTEN:
2332                         initCurveShrinkFatten(t);
2333                         break;
2334                 case TFM_MASK_SHRINKFATTEN:
2335                         initMaskShrinkFatten(t);
2336                         break;
2337                 case TFM_GPENCIL_SHRINKFATTEN:
2338                         initGPShrinkFatten(t);
2339                         break;
2340                 case TFM_TRACKBALL:
2341                         initTrackball(t);
2342                         break;
2343                 case TFM_PUSHPULL:
2344                         initPushPull(t);
2345                         break;
2346                 case TFM_CREASE:
2347                         initCrease(t);
2348                         break;
2349                 case TFM_BONESIZE:
2350                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2351                         /* Note: we have to pick one, use the active object. */
2352                         TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_OK(t);
2353                         bArmature *arm = tc->poseobj->data;
2354                         if (arm->drawtype == ARM_ENVELOPE) {
2355                                 initBoneEnvelope(t);
2356                                 t->mode = TFM_BONE_ENVELOPE_DIST;
2357                         }
2358                         else {
2359                                 initBoneSize(t);
2360                         }
2361                         break;
2362                 }
2363                 case TFM_BONE_ENVELOPE:
2364                         initBoneEnvelope(t);
2365                         break;
2366                 case TFM_BONE_ENVELOPE_DIST:
2367                         initBoneEnvelope(t);
2368                         t->mode = TFM_BONE_ENVELOPE_DIST;
2369                         break;
2370                 case TFM_EDGE_SLIDE:
2371                 case TFM_VERT_SLIDE:
2372                 {
2373                         const bool use_even = (op ? RNA_boolean_get(op->ptr, "use_even") : false);
2374                         const bool flipped = (op ? RNA_boolean_get(op->ptr, "flipped") : false);
2375                         const bool use_clamp = (op ? RNA_boolean_get(op->ptr, "use_clamp") : true);
2376                         if (mode == TFM_EDGE_SLIDE) {
2377                                 const bool use_double_side = (op ? !RNA_boolean_get(op->ptr, "single_side") : true);
2378                                 initEdgeSlide_ex(t, use_double_side, use_even, flipped, use_clamp);
2379                         }
2380                         else {
2381                                 initVertSlide_ex(t, use_even, flipped, use_clamp);
2382                         }
2383                         break;
2384                 }
2385                 case TFM_BONE_ROLL:
2386                         initBoneRoll(t);
2387                         break;
2388                 case TFM_TIME_TRANSLATE:
2389                         initTimeTranslate(t);
2390                         break;
2391                 case TFM_TIME_SLIDE:
2392                         initTimeSlide(t);
2393                         break;
2394                 case TFM_TIME_SCALE:
2395                         initTimeScale(t);
2396                         break;
2397                 case TFM_TIME_DUPLICATE:
2398                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2399                          * so that duplicate-culling will work properly
2400                          */
2401                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2402                                 initTranslation(t);
2403                         else
2404                                 initTimeTranslate(t);
2405                         t->mode = mode;
2406                         break;
2407                 case TFM_TIME_EXTEND:
2408                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2409                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2410                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2411                          * depending on which editor this was called from
2412                          */
2413                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2414                                 initTranslation(t);
2415                         else
2416                                 initTimeTranslate(t);
2417                         break;
2418                 case TFM_BAKE_TIME:
2419                         initBakeTime(t);
2420                         break;
2421                 case TFM_MIRROR:
2422                         initMirror(t);
2423                         break;
2424                 case TFM_BWEIGHT:
2425                         initBevelWeight(t);
2426                         break;
2427                 case TFM_ALIGN:
2428                         initAlign(t);
2429                         break;
2430                 case TFM_SEQ_SLIDE:
2431                         initSeqSlide(t);
2432                         break;
2433         }
2434
2435         if (t->state == TRANS_CANCEL) {
2436                 postTrans(C, t);
2437                 return 0;
2438         }
2439
2440         t->context = NULL;
2441
2442         return 1;
2443 }
2444
2445 void transformApply(bContext *C, TransInfo *t)
2446 {
2447         t->context = C;
2448
2449         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2450                 selectConstraint(t);
2451                 if (t->transform) {
2452                         t->transform(t, t->mval);  // calls recalcData()
2453                         viewRedrawForce(C, t);
2454                 }
2455                 t->redraw = TREDRAW_NOTHING;
2456         }
2457         else if (t->redraw & TREDRAW_SOFT) {
2458                 viewRedrawForce(C, t);
2459         }
2460
2461         /* If auto confirm is on, break after one pass */
2462         if (t->options & CTX_AUTOCONFIRM) {
2463                 t->state = TRANS_CONFIRM;
2464         }
2465
2466         t->context = NULL;
2467 }
2468
2469 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2470 {
2471         TransInfo *t = arg;
2472
2473         if (t->redraw & TREDRAW_SOFT) {
2474                 t->redraw |= TREDRAW_HARD;
2475                 transformApply((bContext *)C, t);
2476         }
2477 }
2478
2479 int transformEnd(bContext *C, TransInfo *t)
2480 {
2481         int exit_code = OPERATOR_RUNNING_MODAL;
2482
2483         t->context = C;
2484
2485         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2486                 /* handle restoring objects */
2487                 if (t->state == TRANS_CANCEL) {
2488                         /* exception, edge slide transformed UVs too */
2489                         if (t->mode == TFM_EDGE_SLIDE) {
2490                                 doEdgeSlide(t, 0.0f);
2491                         }
2492                         else if (t->mode == TFM_VERT_SLIDE) {
2493                                 doVertSlide(t, 0.0f);
2494                         }
2495
2496                         exit_code = OPERATOR_CANCELLED;
2497                         restoreTransObjects(t); // calls recalcData()
2498                 }
2499                 else {
2500                         exit_code = OPERATOR_FINISHED;
2501                 }
2502
2503                 /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
2504                 special_aftertrans_update(C, t);
2505
2506                 /* free data */
2507                 postTrans(C, t);
2508
2509                 /* send events out for redraws */
2510                 viewRedrawPost(C, t);
2511
2512                 viewRedrawForce(C, t);
2513         }
2514
2515         t->context = NULL;
2516
2517         return exit_code;
2518 }
2519
2520 /* ************************** TRANSFORM LOCKS **************************** */
2521
2522 static void protectedTransBits(short protectflag, float vec[3])
2523 {
2524         if (protectflag & OB_LOCK_LOCX)
2525                 vec[0] = 0.0f;
2526         if (protectflag & OB_LOCK_LOCY)
2527                 vec[1] = 0.0f;
2528         if (protectflag & OB_LOCK_LOCZ)
2529                 vec[2] = 0.0f;
2530 }
2531
2532 static void protectedSizeBits(short protectflag, float size[3])
2533 {
2534         if (protectflag & OB_LOCK_SCALEX)
2535                 size[0] = 1.0f;
2536         if (protectflag & OB_LOCK_SCALEY)
2537                 size[1] = 1.0f;
2538         if (protectflag & OB_LOCK_SCALEZ)
2539                 size[2] = 1.0f;
2540 }
2541
2542 static void protectedRotateBits(short protectflag, float eul[3], const float oldeul[3])
2543 {
2544         if (protectflag & OB_LOCK_ROTX)
2545                 eul[0] = oldeul[0];
2546         if (protectflag & OB_LOCK_ROTY)
2547                 eul[1] = oldeul[1];
2548         if (protectflag & OB_LOCK_ROTZ)
2549                 eul[2] = oldeul[2];
2550 }
2551
2552
2553 /* this function only does the delta rotation */
2554 /* axis-angle is usually internally stored as quats... */
2555 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2556 {
2557         /* check that protection flags are set */
2558         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2559                 return;
2560
2561         if (protectflag & OB_LOCK_ROT4D) {
2562                 /* axis-angle getting limited as 4D entities that they are... */
2563                 if (protectflag & OB_LOCK_ROTW)
2564                         *angle = oldAngle;
2565                 if (protectflag & OB_LOCK_ROTX)
2566                         axis[0] = oldAxis[0];
2567                 if (protectflag & OB_LOCK_ROTY)
2568                         axis[1] = oldAxis[1];
2569                 if (protectflag & OB_LOCK_ROTZ)
2570                         axis[2] = oldAxis[2];
2571         }
2572         else {
2573                 /* axis-angle get limited with euler... */
2574                 float eul[3], oldeul[3];
2575
2576                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2577                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2578
2579                 if (protectflag & OB_LOCK_ROTX)
2580                         eul[0] = oldeul[0];
2581                 if (protectflag & OB_LOCK_ROTY)
2582                         eul[1] = oldeul[1];
2583                 if (protectflag & OB_LOCK_ROTZ)
2584                         eul[2] = oldeul[2];
2585
2586                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2587
2588                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2589                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2590                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2591                         axis[1] = 1.0f;
2592                 }
2593         }
2594 }
2595
2596 /* this function only does the delta rotation */
2597 static void protectedQuaternionBits(short protectflag, float quat[4], const float oldquat[4])
2598 {
2599         /* check that protection flags are set */
2600         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2601                 return;
2602
2603         if (protectflag & OB_LOCK_ROT4D) {
2604                 /* quaternions getting limited as 4D entities that they are... */
2605                 if (protectflag & OB_LOCK_ROTW)
2606                         quat[0] = oldquat[0];
2607                 if (protectflag & OB_LOCK_ROTX)
2608                         quat[1] = oldquat[1];
2609                 if (protectflag & OB_LOCK_ROTY)
2610                         quat[2] = oldquat[2];
2611                 if (protectflag & OB_LOCK_ROTZ)
2612                         quat[3] = oldquat[3];
2613         }
2614         else {
2615                 /* quaternions get limited with euler... (compatibility mode) */
2616                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2617                 float qlen;
2618
2619                 qlen = normalize_qt_qt(nquat, quat);
2620                 normalize_qt_qt(noldquat, oldquat);
2621
2622                 quat_to_eul(eul, nquat);
2623                 quat_to_eul(oldeul, noldquat);
2624
2625                 if (protectflag & OB_LOCK_ROTX)
2626                         eul[0] = oldeul[0];
2627                 if (protectflag & OB_LOCK_ROTY)
2628                         eul[1] = oldeul[1];
2629                 if (protectflag & OB_LOCK_ROTZ)
2630                         eul[2] = oldeul[2];
2631
2632                 eul_to_quat(quat, eul);
2633
2634                 /* restore original quat size */
2635                 mul_qt_fl(quat, qlen);
2636
2637                 /* quaternions flip w sign to accumulate rotations correctly */
2638                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2639                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2640                 {
2641                         mul_qt_fl(quat, -1.0f);
2642                 }
2643         }
2644 }
2645
2646 /* ******************* TRANSFORM LIMITS ********************** */
2647
2648 static void constraintTransLim(TransInfo *t, TransData *td)
2649 {
2650         if (td->con) {
2651                 const bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_LOCLIMIT);
2652                 const bConstraintTypeInfo *ctiDist = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_DISTLIMIT);
2653
2654                 bConstraintOb cob = {NULL};
2655                 bConstraint *con;
2656                 float ctime = (float)(t->scene->r.cfra);
2657
2658                 /* Make a temporary bConstraintOb for using these limit constraints
2659                  *  - they only care that cob->matrix is correctly set ;-)
2660                  *      - current space should be local
2661                  */
2662                 unit_m4(cob.matrix);
2663                 copy_v3_v3(cob.matrix[3], td->loc);
2664
2665                 /* Evaluate valid constraints */
2666                 for (con = td->con; con; con = con->next) {
2667                         const bConstraintTypeInfo *cti = NULL;
2668                         ListBase targets = {NULL, NULL};
2669
2670                         /* only consider constraint if enabled */
2671                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2672                         if (con->enforce == 0.0f) continue;
2673
2674                         /* only use it if it's tagged for this purpose (and the right type) */
2675                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2676                                 bLocLimitConstraint *data = con->data;
2677
2678                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2679                                         continue;
2680                                 cti = ctiLoc;
2681                         }
2682                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2683                                 bDistLimitConstraint *data = con->data;
2684
2685                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2686                                         continue;
2687                                 cti = ctiDist;
2688                         }
2689
2690                         if (cti) {
2691                                 /* do space conversions */
2692                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2693                                         /* just multiply by td->mtx (this should be ok) */
2694                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2695                                 }
2696                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2697                                         /* skip... incompatable spacetype */
2698                                         continue;
2699                                 }
2700
2701                                 /* get constraint targets if needed */
2702                                 BKE_constraint_targets_for_solving_get(t->depsgraph, con, &cob, &targets, ctime);
2703
2704                                 /* do constraint */
2705                                 cti->evaluate_constraint(con, &cob, &targets);
2706
2707                                 /* convert spaces again */
2708                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2709                                         /* just multiply by td->smtx (this should be ok) */
2710                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2711                                 }
2712
2713                                 /* free targets list */
2714                                 BLI_freelistN(&targets);
2715                         }
2716                 }
2717
2718                 /* copy results from cob->matrix */
2719                 copy_v3_v3(td->loc, cob.matrix[3]);
2720         }
2721 }
2722
2723 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2724 {
2725         /* Make a temporary bConstraintOb for use by limit constraints
2726          *  - they only care that cob->matrix is correctly set ;-)
2727          *      - current space should be local
2728          */
2729         memset(cob, 0, sizeof(bConstraintOb));
2730         if (td->ext) {
2731                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2732                         /* quats */
2733                         /* objects and bones do normalization first too, otherwise
2734                          * we don't necessarily end up with a rotation matrix, and
2735                          * then conversion back to quat gives a different result */
2736                         float quat[4];
2737                         normalize_qt_qt(quat, td->ext->quat);
2738                         quat_to_mat4(cob->matrix, quat);
2739                 }
2740                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2741                         /* axis angle */
2742                         axis_angle_to_mat4(cob->matrix, td->ext->rotAxis, *td->ext->rotAngle);
2743                 }
2744                 else {
2745                         /* eulers */
2746                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2747                 }
2748         }
2749 }
2750
2751 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2752 {
2753         if (td->con) {
2754                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT);
2755                 bConstraintOb cob;
2756                 bConstraint *con;
2757                 bool do_limit = false;
2758
2759                 /* Evaluate valid constraints */
2760                 for (con = td->con; con; con = con->next) {
2761                         /* only consider constraint if enabled */
2762                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2763                         if (con->enforce == 0.0f) continue;
2764
2765                         /* we're only interested in Limit-Rotation constraints */
2766                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2767                                 bRotLimitConstraint *data = con->data;
2768
2769                                 /* only use it if it's tagged for this purpose */
2770                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2771                                         continue;
2772
2773                                 /* skip incompatable spacetypes */
2774                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2775                                         continue;
2776
2777                                 /* only do conversion if necessary, to preserve quats and eulers */
2778                                 if (do_limit == false) {
2779                                         constraintob_from_transdata(&cob, td);
2780                                         do_limit = true;
2781                                 }
2782
2783                                 /* do space conversions */
2784                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2785                                         /* just multiply by td->mtx (this should be ok) */
2786                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2787                                 }
2788
2789                                 /* do constraint */
2790                                 cti->evaluate_constraint(con, &cob, NULL);
2791
2792                                 /* convert spaces again */
2793                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2794                                         /* just multiply by td->smtx (this should be ok) */
2795                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2796                                 }
2797                         }
2798                 }
2799
2800                 if (do_limit) {
2801                         /* copy results from cob->matrix */
2802                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2803                                 /* quats */
2804                                 mat4_to_quat(td->ext->quat, cob.matrix);
2805                         }
2806                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2807                                 /* axis angle */
2808                                 mat4_to_axis_angle(td->ext->rotAxis, td->ext->rotAngle, cob.matrix);
2809                         }
2810                         else {
2811                                 /* eulers */
2812                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2813                         }
2814                 }
2815         }
2816 }
2817
2818 static void constraintSizeLim(TransInfo *t, TransData *td)
2819 {
2820         if (td->con && td->ext) {
2821                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT);
2822                 bConstraintOb cob = {NULL};
2823                 bConstraint *con;
2824                 float size_sign[3], size_abs[3];
2825                 int i;
2826
2827                 /* Make a temporary bConstraintOb for using these limit constraints
2828                  *  - they only care that cob->matrix is correctly set ;-)
2829                  *      - current space should be local
2830                  */
2831                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2832                         /* scale val and reset size */
2833                         return; // TODO: fix this case
2834                 }
2835                 else {
2836                         /* Reset val if SINGLESIZE but using a constraint */
2837                         if (td->flag & TD_SINGLESIZE)
2838                                 return;
2839
2840                         /* separate out sign to apply back later */
2841                         for (i = 0; i < 3; i++) {
2842                                 size_sign[i] = signf(td->ext->size[i]);
2843                                 size_abs[i] = fabsf(td->ext->size[i]);
2844                         }
2845
2846                         size_to_mat4(cob.matrix, size_abs);
2847                 }
2848
2849                 /* Evaluate valid constraints */
2850                 for (con = td->con; con; con = con->next) {
2851                         /* only consider constraint if enabled */
2852                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2853                         if (con->enforce == 0.0f) continue;
2854
2855                         /* we're only interested in Limit-Scale constraints */
2856                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2857                                 bSizeLimitConstraint *data = con->data;
2858
2859                                 /* only use it if it's tagged for this purpose */
2860                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2861                                         continue;
2862
2863                                 /* do space conversions */
2864                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2865                                         /* just multiply by td->mtx (this should be ok) */
2866                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);