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