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