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