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