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