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