Cleanup: trailing space
[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_CANCEL, "CANCEL", 0, "Cancel", ""},
806                 {TFM_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
807                 {TFM_MODAL_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
808                 {TFM_MODAL_ROTATE, "ROTATE", 0, "Rotate", ""},
809                 {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
810                 {TFM_MODAL_SNAP_INV_ON, "SNAP_INV_ON", 0, "Invert Snap On", ""},
811                 {TFM_MODAL_SNAP_INV_OFF, "SNAP_INV_OFF", 0, "Invert Snap Off", ""},
812                 {TFM_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap Toggle", ""},
813                 {TFM_MODAL_AXIS_X, "AXIS_X", 0, "Orientation X axis", ""},
814                 {TFM_MODAL_AXIS_Y, "AXIS_Y", 0, "Orientation Y axis", ""},
815                 {TFM_MODAL_AXIS_Z, "AXIS_Z", 0, "Orientation Z axis", ""},
816                 {TFM_MODAL_PLANE_X, "PLANE_X", 0, "Orientation X plane", ""},
817                 {TFM_MODAL_PLANE_Y, "PLANE_Y", 0, "Orientation Y plane", ""},
818                 {TFM_MODAL_PLANE_Z, "PLANE_Z", 0, "Orientation Z plane", ""},
819                 {TFM_MODAL_CONS_OFF, "CONS_OFF", 0, "Remove Constraints", ""},
820                 {TFM_MODAL_ADD_SNAP, "ADD_SNAP", 0, "Add Snap Point", ""},
821                 {TFM_MODAL_REMOVE_SNAP, "REMOVE_SNAP", 0, "Remove Last Snap Point", ""},
822                 {NUM_MODAL_INCREMENT_UP, "INCREMENT_UP", 0, "Numinput Increment Up", ""},
823                 {NUM_MODAL_INCREMENT_DOWN, "INCREMENT_DOWN", 0, "Numinput Increment Down", ""},
824                 {TFM_MODAL_PROPSIZE_UP, "PROPORTIONAL_SIZE_UP", 0, "Increase Proportional Influence", ""},
825                 {TFM_MODAL_PROPSIZE_DOWN, "PROPORTIONAL_SIZE_DOWN", 0, "Decrease Proportional Influence", ""},
826                 {TFM_MODAL_AUTOIK_LEN_INC, "AUTOIK_CHAIN_LEN_UP", 0, "Increase Max AutoIK Chain Length", ""},
827                 {TFM_MODAL_AUTOIK_LEN_DEC, "AUTOIK_CHAIN_LEN_DOWN", 0, "Decrease Max AutoIK Chain Length", ""},
828                 {TFM_MODAL_EDGESLIDE_UP, "EDGESLIDE_EDGE_NEXT", 0, "Select next Edge Slide Edge", ""},
829                 {TFM_MODAL_EDGESLIDE_DOWN, "EDGESLIDE_PREV_NEXT", 0, "Select previous Edge Slide Edge", ""},
830                 {TFM_MODAL_PROPSIZE, "PROPORTIONAL_SIZE", 0, "Adjust Proportional Influence", ""},
831                 {TFM_MODAL_INSERTOFS_TOGGLE_DIR, "INSERTOFS_TOGGLE_DIR", 0, "Toggle Direction for Node Auto-offset", ""},
832                 {0, NULL, 0, NULL, NULL}
833         };
834
835         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "Transform Modal Map");
836
837         /* this function is called for each spacetype, only needs to add map once */
838         if (keymap && keymap->modal_items) return NULL;
839
840         keymap = WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
841
842         /* items for modal map */
843         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CANCEL);
844         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
845         WM_modalkeymap_add_item(keymap, RETKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
846         WM_modalkeymap_add_item(keymap, PADENTER,  KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
847
848         WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, TFM_MODAL_TRANSLATE);
849         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, TFM_MODAL_ROTATE);
850         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, TFM_MODAL_RESIZE);
851
852         WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_SNAP_TOGGLE);
853
854         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
855         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
856
857         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
858         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
859
860         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, TFM_MODAL_ADD_SNAP);
861         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ALT, 0, TFM_MODAL_REMOVE_SNAP);
862
863         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
864         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
865         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_UP);
866         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_DOWN);
867         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
868         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
869         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_UP);
870         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_DOWN);
871         WM_modalkeymap_add_item(keymap, MOUSEPAN, 0, 0, 0, TFM_MODAL_PROPSIZE);
872
873         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_UP);
874         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_DOWN);
875
876         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
877         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
878         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
879         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
880
881         /* node editor only */
882         WM_modalkeymap_add_item(keymap, TKEY, KM_PRESS, 0, 0, TFM_MODAL_INSERTOFS_TOGGLE_DIR);
883
884         return keymap;
885 }
886
887 static void transform_event_xyz_constraint(TransInfo *t, short key_type, char cmode)
888 {
889         if (!(t->flag & T_NO_CONSTRAINT)) {
890                 int constraint_axis, constraint_plane;
891                 const bool edit_2d = (t->flag & T_2D_EDIT) != 0;
892                 const char *msg1 = "", *msg2 = "", *msg3 = "";
893                 char axis;
894
895                 /* Initialize */
896                 switch (key_type) {
897                         case XKEY:
898                                 msg1 = IFACE_("along X");
899                                 msg2 = IFACE_("along %s X");
900                                 msg3 = IFACE_("locking %s X");
901                                 axis = 'X';
902                                 constraint_axis = CON_AXIS0;
903                                 break;
904                         case YKEY:
905                                 msg1 = IFACE_("along Y");
906                                 msg2 = IFACE_("along %s Y");
907                                 msg3 = IFACE_("locking %s Y");
908                                 axis = 'Y';
909                                 constraint_axis = CON_AXIS1;
910                                 break;
911                         case ZKEY:
912                                 msg1 = IFACE_("along Z");
913                                 msg2 = IFACE_("along %s Z");
914                                 msg3 = IFACE_("locking %s Z");
915                                 axis = 'Z';
916                                 constraint_axis = CON_AXIS2;
917                                 break;
918                         default:
919                                 /* Invalid key */
920                                 return;
921                 }
922                 constraint_plane = ((CON_AXIS0 | CON_AXIS1 | CON_AXIS2) & (~constraint_axis));
923
924                 if (edit_2d && (key_type != ZKEY)) {
925                         if (cmode == axis) {
926                                 stopConstraint(t);
927                         }
928                         else {
929                                 setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg1);
930                         }
931                 }
932                 else if (!edit_2d) {
933                         if (cmode == axis) {
934                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
935                                         stopConstraint(t);
936                                 }
937                                 else {
938                                         short orientation = (t->current_orientation != V3D_MANIP_GLOBAL ?
939                                                              t->current_orientation : V3D_MANIP_LOCAL);
940                                         if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
941                                                 setUserConstraint(t, orientation, constraint_axis, msg2);
942                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
943                                                 setUserConstraint(t, orientation, constraint_plane, msg3);
944                                 }
945                         }
946                         else {
947                                 if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
948                                         setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg2);
949                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
950                                         setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_plane, msg3);
951                         }
952                 }
953                 t->redraw |= TREDRAW_HARD;
954         }
955 }
956
957 int transformEvent(TransInfo *t, const wmEvent *event)
958 {
959         char cmode = constraintModeToChar(t);
960         bool handled = false;
961         const int modifiers_prev = t->modifiers;
962
963         t->redraw |= handleMouseInput(t, &t->mouse, event);
964
965         /* Handle modal numinput events first, if already activated. */
966         if (((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
967             hasNumInput(&t->num) && handleNumInput(t->context, &(t->num), event))
968         {
969                 t->redraw |= TREDRAW_HARD;
970                 handled = true;
971         }
972         else if (event->type == MOUSEMOVE) {
973                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
974                         t->con.mode |= CON_SELECT;
975
976                 copy_v2_v2_int(t->mval, event->mval);
977
978                 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
979                 t->redraw |= TREDRAW_HARD;
980
981                 if (t->state == TRANS_STARTING) {
982                         t->state = TRANS_RUNNING;
983                 }
984
985                 applyMouseInput(t, &t->mouse, t->mval, t->values);
986
987                 // Snapping mouse move events
988                 t->redraw |= handleSnapping(t, event);
989                 handled = true;
990         }
991         /* handle modal keymap first */
992         else if (event->type == EVT_MODAL_MAP) {
993                 switch (event->val) {
994                         case TFM_MODAL_CANCEL:
995                                 t->state = TRANS_CANCEL;
996                                 handled = true;
997                                 break;
998                         case TFM_MODAL_CONFIRM:
999                                 t->state = TRANS_CONFIRM;
1000                                 handled = true;
1001                                 break;
1002                         case TFM_MODAL_TRANSLATE:
1003                                 /* only switch when... */
1004                                 if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1005                                         restoreTransObjects(t);
1006                                         resetTransModal(t);
1007                                         resetTransRestrictions(t);
1008                                         initTranslation(t);
1009                                         initSnapping(t, NULL); // need to reinit after mode change
1010                                         t->redraw |= TREDRAW_HARD;
1011                                         WM_event_add_mousemove(t->context);
1012                                         handled = true;
1013                                 }
1014                                 else if (t->mode == TFM_SEQ_SLIDE) {
1015                                         t->flag ^= T_ALT_TRANSFORM;
1016                                         t->redraw |= TREDRAW_HARD;
1017                                         handled = true;
1018                                 }
1019                                 else {
1020                                         if (t->obedit && t->obedit->type == OB_MESH) {
1021                                                 if ((t->mode == TFM_TRANSLATION) && (t->spacetype == SPACE_VIEW3D)) {
1022                                                         restoreTransObjects(t);
1023                                                         resetTransModal(t);
1024                                                         resetTransRestrictions(t);
1025
1026                                                         /* first try edge slide */
1027                                                         initEdgeSlide(t);
1028                                                         /* if that fails, do vertex slide */
1029                                                         if (t->state == TRANS_CANCEL) {
1030                                                                 t->state = TRANS_STARTING;
1031                                                                 initVertSlide(t);
1032                                                         }
1033                                                         /* vert slide can fail on unconnected vertices (rare but possible) */
1034                                                         if (t->state == TRANS_CANCEL) {
1035                                                                 t->mode = TFM_TRANSLATION;
1036                                                                 t->state = TRANS_STARTING;
1037                                                                 restoreTransObjects(t);
1038                                                                 resetTransRestrictions(t);
1039                                                                 initTranslation(t);
1040                                                         }
1041                                                         initSnapping(t, NULL); // need to reinit after mode change
1042                                                         t->redraw |= TREDRAW_HARD;
1043                                                         handled = true;
1044                                                         WM_event_add_mousemove(t->context);
1045                                                 }
1046                                         }
1047                                         else if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
1048                                                 if (t->mode == TFM_TRANSLATION) {
1049                                                         restoreTransObjects(t);
1050
1051                                                         t->flag ^= T_ALT_TRANSFORM;
1052                                                         t->redraw |= TREDRAW_HARD;
1053                                                         handled = true;
1054                                                 }
1055                                         }
1056                                 }
1057                                 break;
1058                         case TFM_MODAL_ROTATE:
1059                                 /* only switch when... */
1060                                 if (!(t->options & CTX_TEXTURE) && !(t->options & (CTX_MOVIECLIP | CTX_MASK))) {
1061                                         if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1062                                                 restoreTransObjects(t);
1063                                                 resetTransModal(t);
1064                                                 resetTransRestrictions(t);
1065
1066                                                 if (t->mode == TFM_ROTATION) {
1067                                                         initTrackball(t);
1068                                                 }
1069                                                 else {
1070                                                         initRotation(t);
1071                                                 }
1072                                                 initSnapping(t, NULL); // need to reinit after mode change
1073                                                 t->redraw |= TREDRAW_HARD;
1074                                                 handled = true;
1075                                         }
1076                                 }
1077                                 break;
1078                         case TFM_MODAL_RESIZE:
1079                                 /* only switch when... */
1080                                 if (ELEM(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1081
1082                                         /* Scale isn't normally very useful after extrude along normals, see T39756 */
1083                                         if ((t->con.mode & CON_APPLY) && (t->con.orientation == V3D_MANIP_NORMAL)) {
1084                                                 stopConstraint(t);
1085                                         }
1086
1087                                         restoreTransObjects(t);
1088                                         resetTransModal(t);
1089                                         resetTransRestrictions(t);
1090                                         initResize(t);
1091                                         initSnapping(t, NULL); // need to reinit after mode change
1092                                         t->redraw |= TREDRAW_HARD;
1093                                         handled = true;
1094                                 }
1095                                 else if (t->mode == TFM_SHRINKFATTEN) {
1096                                         t->flag ^= T_ALT_TRANSFORM;
1097                                         t->redraw |= TREDRAW_HARD;
1098                                         handled = true;
1099                                 }
1100                                 else if (t->mode == TFM_RESIZE) {
1101                                         if (t->options & CTX_MOVIECLIP) {
1102                                                 restoreTransObjects(t);
1103
1104                                                 t->flag ^= T_ALT_TRANSFORM;
1105                                                 t->redraw |= TREDRAW_HARD;
1106                                                 handled = true;
1107                                         }
1108                                 }
1109                                 break;
1110
1111                         case TFM_MODAL_SNAP_INV_ON:
1112                                 t->modifiers |= MOD_SNAP_INVERT;
1113                                 t->redraw |= TREDRAW_HARD;
1114                                 handled = true;
1115                                 break;
1116                         case TFM_MODAL_SNAP_INV_OFF:
1117                                 t->modifiers &= ~MOD_SNAP_INVERT;
1118                                 t->redraw |= TREDRAW_HARD;
1119                                 handled = true;
1120                                 break;
1121                         case TFM_MODAL_SNAP_TOGGLE:
1122                                 t->modifiers ^= MOD_SNAP;
1123                                 t->redraw |= TREDRAW_HARD;
1124                                 handled = true;
1125                                 break;
1126                         case TFM_MODAL_AXIS_X:
1127                                 if (!(t->flag & T_NO_CONSTRAINT)) {
1128                                         transform_event_xyz_constraint(t, XKEY, cmode);
1129                                         t->redraw |= TREDRAW_HARD;
1130                                         handled = true;
1131                                 }
1132                                 break;
1133                         case TFM_MODAL_AXIS_Y:
1134                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1135                                         transform_event_xyz_constraint(t, YKEY, cmode);
1136                                         t->redraw |= TREDRAW_HARD;
1137                                         handled = true;
1138                                 }
1139                                 break;
1140                         case TFM_MODAL_AXIS_Z:
1141                                 if ((t->flag & (T_NO_CONSTRAINT)) == 0) {
1142                                         transform_event_xyz_constraint(t, ZKEY, cmode);
1143                                         t->redraw |= TREDRAW_HARD;
1144                                         handled = true;
1145                                 }
1146                                 break;
1147                         case TFM_MODAL_PLANE_X:
1148                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1149                                         if (cmode == 'X') {
1150                                                 stopConstraint(t);
1151                                         }
1152                                         else {
1153                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1 | CON_AXIS2), IFACE_("locking %s X"));
1154                                         }
1155                                         t->redraw |= TREDRAW_HARD;
1156                                         handled = true;
1157                                 }
1158                                 break;
1159                         case TFM_MODAL_PLANE_Y:
1160                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1161                                         if (cmode == 'Y') {
1162                                                 stopConstraint(t);
1163                                         }
1164                                         else {
1165                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS2), IFACE_("locking %s Y"));
1166                                         }
1167                                         t->redraw |= TREDRAW_HARD;
1168                                         handled = true;
1169                                 }
1170                                 break;
1171                         case TFM_MODAL_PLANE_Z:
1172                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1173                                         if (cmode == 'Z') {
1174                                                 stopConstraint(t);
1175                                         }
1176                                         else {
1177                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS1), IFACE_("locking %s Z"));
1178                                         }
1179                                         t->redraw |= TREDRAW_HARD;
1180                                         handled = true;
1181                                 }
1182                                 break;
1183                         case TFM_MODAL_CONS_OFF:
1184                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1185                                         stopConstraint(t);
1186                                         t->redraw |= TREDRAW_HARD;
1187                                         handled = true;
1188                                 }
1189                                 break;
1190                         case TFM_MODAL_ADD_SNAP:
1191                                 addSnapPoint(t);
1192                                 t->redraw |= TREDRAW_HARD;
1193                                 handled = true;
1194                                 break;
1195                         case TFM_MODAL_REMOVE_SNAP:
1196                                 removeSnapPoint(t);
1197                                 t->redraw |= TREDRAW_HARD;
1198                                 handled = true;
1199                                 break;
1200                         case TFM_MODAL_PROPSIZE:
1201                                 /* MOUSEPAN usage... */
1202                                 if (t->flag & T_PROP_EDIT) {
1203                                         float fac = 1.0f + 0.005f *(event->y - event->prevy);
1204                                         t->prop_size *= fac;
1205                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO) {
1206                                                 t->prop_size = 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                                 else {
1397                                         if (!(t->flag & T_NO_CONSTRAINT)) {
1398                                                 stopConstraint(t);
1399                                                 t->redraw |= TREDRAW_HARD;
1400                                                 handled = true;
1401                                         }
1402                                 }
1403                                 break;
1404                         case XKEY:
1405                         case YKEY:
1406                         case ZKEY:
1407                                 if (!(t->flag & T_NO_CONSTRAINT)) {
1408                                         transform_event_xyz_constraint(t, event->type, cmode);
1409                                         handled = true;
1410                                 }
1411                                 break;
1412                         case OKEY:
1413                                 if (t->flag & T_PROP_EDIT && event->shift) {
1414                                         t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1415                                         calculatePropRatio(t);
1416                                         t->redraw |= TREDRAW_HARD;
1417                                         handled = true;
1418                                 }
1419                                 break;
1420                         case PADPLUSKEY:
1421                                 if (event->alt && t->flag & T_PROP_EDIT) {
1422                                         t->prop_size *= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1423                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1424                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1425                                         calculatePropRatio(t);
1426                                         t->redraw = TREDRAW_HARD;
1427                                         handled = true;
1428                                 }
1429                                 break;
1430                         case PAGEUPKEY:
1431                         case WHEELDOWNMOUSE:
1432                                 if (t->flag & T_AUTOIK) {
1433                                         transform_autoik_update(t, 1);
1434                                 }
1435                                 else {
1436                                         view_editmove(event->type);
1437                                 }
1438                                 t->redraw = TREDRAW_HARD;
1439                                 handled = true;
1440                                 break;
1441                         case PADMINUS:
1442                                 if (event->alt && t->flag & T_PROP_EDIT) {
1443                                         t->prop_size /= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1444                                         calculatePropRatio(t);
1445                                         t->redraw = TREDRAW_HARD;
1446                                         handled = true;
1447                                 }
1448                                 break;
1449                         case PAGEDOWNKEY:
1450                         case WHEELUPMOUSE:
1451                                 if (t->flag & T_AUTOIK) {
1452                                         transform_autoik_update(t, -1);
1453                                 }
1454                                 else {
1455                                         view_editmove(event->type);
1456                                 }
1457                                 t->redraw = TREDRAW_HARD;
1458                                 handled = true;
1459                                 break;
1460                         case LEFTALTKEY:
1461                         case RIGHTALTKEY:
1462                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1463                                         t->flag |= T_ALT_TRANSFORM;
1464                                         t->redraw |= TREDRAW_HARD;
1465                                         handled = true;
1466                                 }
1467                                 break;
1468                         default:
1469                                 break;
1470                 }
1471
1472                 /* Snapping key events */
1473                 t->redraw |= handleSnapping(t, event);
1474         }
1475         else if (event->val == KM_RELEASE) {
1476                 switch (event->type) {
1477                         case LEFTSHIFTKEY:
1478                         case RIGHTSHIFTKEY:
1479                                 t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1480                                 t->redraw |= TREDRAW_HARD;
1481                                 handled = true;
1482                                 break;
1483
1484                         case MIDDLEMOUSE:
1485                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1486                                         t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1487                                         postSelectConstraint(t);
1488                                         t->redraw |= TREDRAW_HARD;
1489                                         handled = true;
1490                                 }
1491                                 break;
1492                         case LEFTALTKEY:
1493                         case RIGHTALTKEY:
1494                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1495                                         t->flag &= ~T_ALT_TRANSFORM;
1496                                         t->redraw |= TREDRAW_HARD;
1497                                         handled = true;
1498                                 }
1499                                 break;
1500                         default:
1501                                 break;
1502                 }
1503
1504                 /* confirm transform if launch key is released after mouse move */
1505                 if (t->flag & T_RELEASE_CONFIRM) {
1506                         /* XXX Keyrepeat bug in Xorg messes this up, will test when fixed */
1507                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1508                                 t->state = TRANS_CONFIRM;
1509                         }
1510                 }
1511         }
1512
1513         /* if we change snap options, get the unsnapped values back */
1514         if ((t->modifiers   & (MOD_SNAP | MOD_SNAP_INVERT)) !=
1515             (modifiers_prev & (MOD_SNAP | MOD_SNAP_INVERT)))
1516         {
1517                 applyMouseInput(t, &t->mouse, t->mval, t->values);
1518         }
1519
1520         /* Per transform event, if present */
1521         if (t->handleEvent &&
1522             (!handled ||
1523              /* Needed for vertex slide, see [#38756] */
1524              (event->type == MOUSEMOVE)))
1525         {
1526                 t->redraw |= t->handleEvent(t, event);
1527         }
1528
1529         /* Try to init modal numinput now, if possible. */
1530         if (!(handled || t->redraw) && ((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
1531             handleNumInput(t->context, &(t->num), event))
1532         {
1533                 t->redraw |= TREDRAW_HARD;
1534                 handled = true;
1535         }
1536
1537         if (handled || t->redraw) {
1538                 return 0;
1539         }
1540         else {
1541                 return OPERATOR_PASS_THROUGH;
1542         }
1543 }
1544
1545 bool calculateTransformCenter(bContext *C, int centerMode, float cent3d[3], float cent2d[2])
1546 {
1547         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1548         bool success;
1549
1550         t->state = TRANS_RUNNING;
1551
1552         /* avoid calculating PET */
1553         t->options = CTX_NO_PET;
1554
1555         t->mode = TFM_DUMMY;
1556
1557         initTransInfo(C, t, NULL, NULL);
1558
1559         /* avoid doing connectivity lookups (when V3D_AROUND_LOCAL_ORIGINS is set) */
1560         t->around = V3D_AROUND_CENTER_BOUNDS;
1561
1562         createTransData(C, t);              // make TransData structs from selection
1563
1564         t->around = centerMode;             // override userdefined mode
1565
1566         if (t->total == 0) {
1567                 success = false;
1568         }
1569         else {
1570                 success = true;
1571
1572                 calculateCenter(t);
1573
1574                 if (cent2d) {
1575                         copy_v2_v2(cent2d, t->center2d);
1576                 }
1577
1578                 if (cent3d) {
1579                         // Copy center from constraint center. Transform center can be local
1580                         copy_v3_v3(cent3d, t->center_global);
1581                 }
1582         }
1583
1584
1585         /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
1586         special_aftertrans_update(C, t);
1587
1588         postTrans(C, t);
1589
1590         MEM_freeN(t);
1591
1592         return success;
1593 }
1594
1595 typedef enum {
1596         UP,
1597         DOWN,
1598         LEFT,
1599         RIGHT
1600 } ArrowDirection;
1601 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1602 {
1603         switch (d) {
1604                 case LEFT:
1605                         offset = -offset;
1606                         length = -length;
1607                         size = -size;
1608                         ATTR_FALLTHROUGH;
1609                 case RIGHT:
1610                         glBegin(GL_LINES);
1611                         glVertex2s(offset, 0);
1612                         glVertex2s(offset + length, 0);
1613                         glVertex2s(offset + length, 0);
1614                         glVertex2s(offset + length - size, -size);
1615                         glVertex2s(offset + length, 0);
1616                         glVertex2s(offset + length - size,  size);
1617                         glEnd();
1618                         break;
1619
1620                 case DOWN:
1621                         offset = -offset;
1622                         length = -length;
1623                         size = -size;
1624                         ATTR_FALLTHROUGH;
1625                 case UP:
1626                         glBegin(GL_LINES);
1627                         glVertex2s(0, offset);
1628                         glVertex2s(0, offset + length);
1629                         glVertex2s(0, offset + length);
1630                         glVertex2s(-size, offset + length - size);
1631                         glVertex2s(0, offset + length);
1632                         glVertex2s(size, offset + length - size);
1633                         glEnd();
1634                         break;
1635         }
1636 }
1637
1638 static void drawArrowHead(ArrowDirection d, short size)
1639 {
1640         switch (d) {
1641                 case LEFT:
1642                         size = -size;
1643                         ATTR_FALLTHROUGH;
1644                 case RIGHT:
1645                         glBegin(GL_LINES);
1646                         glVertex2s(0, 0);
1647                         glVertex2s(-size, -size);
1648                         glVertex2s(0, 0);
1649                         glVertex2s(-size,  size);
1650                         glEnd();
1651                         break;
1652
1653                 case DOWN:
1654                         size = -size;
1655                         ATTR_FALLTHROUGH;
1656                 case UP:
1657                         glBegin(GL_LINES);
1658                         glVertex2s(0, 0);
1659                         glVertex2s(-size, -size);
1660                         glVertex2s(0, 0);
1661                         glVertex2s(size, -size);
1662                         glEnd();
1663                         break;
1664         }
1665 }
1666
1667 static void drawArc(float size, float angle_start, float angle_end, int segments)
1668 {
1669         float delta = (angle_end - angle_start) / segments;
1670         float angle;
1671         int a;
1672
1673         glBegin(GL_LINE_STRIP);
1674
1675         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1676                 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1677         }
1678         glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1679
1680         glEnd();
1681 }
1682
1683 static int helpline_poll(bContext *C)
1684 {
1685         ARegion *ar = CTX_wm_region(C);
1686
1687         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1688                 return 1;
1689         return 0;
1690 }
1691
1692 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1693 {
1694         TransInfo *t = (TransInfo *)customdata;
1695
1696         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1697                 float cent[2];
1698                 int mval[2];
1699
1700                 mval[0] = x;
1701                 mval[1] = y;
1702
1703                 projectFloatViewEx(t, t->center_global, cent, V3D_PROJ_TEST_CLIP_ZERO);
1704
1705                 glPushMatrix();
1706
1707                 switch (t->helpline) {
1708                         case HLP_SPRING:
1709                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1710
1711                                 setlinestyle(3);
1712                                 glLineWidth(1);
1713                                 glBegin(GL_LINES);
1714                                 glVertex2iv(t->mval);
1715                                 glVertex2fv(cent);
1716                                 glEnd();
1717
1718                                 glTranslate2iv(mval);
1719                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1720
1721                                 setlinestyle(0);
1722                                 glLineWidth(3.0);
1723                                 drawArrow(UP, 5, 10, 5);
1724                                 drawArrow(DOWN, 5, 10, 5);
1725                                 break;
1726                         case HLP_HARROW:
1727                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1728
1729                                 glTranslate2iv(mval);
1730
1731                                 glLineWidth(3.0);
1732                                 drawArrow(RIGHT, 5, 10, 5);
1733                                 drawArrow(LEFT, 5, 10, 5);
1734                                 break;
1735                         case HLP_VARROW:
1736                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1737
1738                                 glTranslate2iv(mval);
1739
1740                                 glLineWidth(3.0);
1741                                 drawArrow(UP, 5, 10, 5);
1742                                 drawArrow(DOWN, 5, 10, 5);
1743                                 break;
1744                         case HLP_ANGLE:
1745                         {
1746                                 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1747                                 float angle = atan2f(dy, dx);
1748                                 float dist = hypotf(dx, dy);
1749                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1750                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1751                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1752
1753                                 setlinestyle(3);
1754                                 glLineWidth(1);
1755                                 glBegin(GL_LINES);
1756                                 glVertex2iv(t->mval);
1757                                 glVertex2fv(cent);
1758                                 glEnd();
1759
1760                                 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1761
1762                                 setlinestyle(0);
1763                                 glLineWidth(3.0);
1764                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1765                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1766
1767                                 glPushMatrix();
1768
1769                                 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1770                                 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1771
1772                                 drawArrowHead(DOWN, 5);
1773
1774                                 glPopMatrix();
1775
1776                                 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1777                                 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1778
1779                                 drawArrowHead(UP, 5);
1780                                 break;
1781                         }
1782                         case HLP_TRACKBALL:
1783                         {
1784                                 unsigned char col[3], col2[3];
1785                                 UI_GetThemeColor3ubv(TH_GRID, col);
1786
1787                                 glTranslate2iv(mval);
1788
1789                                 glLineWidth(3.0);
1790
1791                                 UI_make_axis_color(col, col2, 'X');
1792                                 glColor3ubv((GLubyte *)col2);
1793
1794                                 drawArrow(RIGHT, 5, 10, 5);
1795                                 drawArrow(LEFT, 5, 10, 5);
1796
1797                                 UI_make_axis_color(col, col2, 'Y');
1798                                 glColor3ubv((GLubyte *)col2);
1799
1800                                 drawArrow(UP, 5, 10, 5);
1801                                 drawArrow(DOWN, 5, 10, 5);
1802                                 break;
1803                         }
1804                 }
1805
1806                 glPopMatrix();
1807         }
1808 }
1809
1810 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1811 {
1812         TransInfo *t = arg;
1813
1814         glLineWidth(1.0);
1815
1816         drawConstraint(t);
1817         drawPropCircle(C, t);
1818         drawSnapping(C, t);
1819
1820         /* edge slide, vert slide */
1821         drawEdgeSlide(t);
1822         drawVertSlide(t);
1823 }
1824
1825 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1826 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1827 {
1828         rcti rect;
1829         const char *printable = IFACE_("Auto Keying On");
1830         float      printable_size[2];
1831         int xco, yco;
1832
1833         ED_region_visible_rect(ar, &rect);
1834
1835         BLF_width_and_height_default(printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1836
1837         xco = (rect.xmax - U.widget_unit) - (int)printable_size[0];
1838         yco = (rect.ymax - U.widget_unit);
1839
1840         /* warning text (to clarify meaning of overlays)
1841          * - original color was red to match the icon, but that clashes badly with a less nasty border
1842          */
1843         UI_ThemeColorShade(TH_TEXT_HI, -50);
1844 #ifdef WITH_INTERNATIONAL
1845         BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1846 #else
1847         BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1848 #endif
1849
1850         /* autokey recording icon... */
1851         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1852         glEnable(GL_BLEND);
1853
1854         xco -= U.widget_unit;
1855         yco -= (int)printable_size[1] / 2;
1856
1857         UI_icon_draw(xco, yco, ICON_REC);
1858
1859         glDisable(GL_BLEND);
1860 }
1861
1862 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1863 {
1864         TransInfo *t = arg;
1865         Scene *scene = t->scene;
1866         Object *ob = OBACT;
1867
1868         /* draw autokeyframing hint in the corner
1869          * - only draw if enabled (advanced users may be distracted/annoyed),
1870          *   for objects that will be autokeyframed (no point ohterwise),
1871          *   AND only for the active region (as showing all is too overwhelming)
1872          */
1873         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1874                 if (ar == t->ar) {
1875                         if (t->flag & (T_OBJECT | T_POSE)) {
1876                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1877                                         drawAutoKeyWarning(t, ar);
1878                                 }
1879                         }
1880                 }
1881         }
1882 }
1883
1884 /**
1885  * \see #initTransform which reads values from the operator.
1886  */
1887 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1888 {
1889         ToolSettings *ts = CTX_data_tool_settings(C);
1890         int constraint_axis[3] = {0, 0, 0};
1891         int proportional = 0;
1892         PropertyRNA *prop;
1893
1894         // Save back mode in case we're in the generic operator
1895         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1896                 RNA_property_enum_set(op->ptr, prop, t->mode);
1897         }
1898
1899         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1900                 float values[4];
1901
1902                 copy_v4_v4(values, (t->flag & T_AUTOVALUES) ? t->auto_values : t->values);
1903
1904                 if (RNA_property_array_check(prop)) {
1905                         RNA_property_float_set_array(op->ptr, prop, values);
1906                 }
1907                 else {
1908                         RNA_property_float_set(op->ptr, prop, values[0]);
1909                 }
1910         }
1911
1912         /* convert flag to enum */
1913         switch (t->flag & T_PROP_EDIT_ALL) {
1914                 case T_PROP_EDIT:
1915                         proportional = PROP_EDIT_ON;
1916                         break;
1917                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1918                         proportional = PROP_EDIT_CONNECTED;
1919                         break;
1920                 case (T_PROP_EDIT | T_PROP_PROJECTED):
1921                         proportional = PROP_EDIT_PROJECTED;
1922                         break;
1923                 default:
1924                         proportional = PROP_EDIT_OFF;
1925                         break;
1926         }
1927
1928         // If modal, save settings back in scene if not set as operator argument
1929         if ((t->flag & T_MODAL) || (op->flag & OP_IS_REPEAT)) {
1930                 /* save settings if not set in operator */
1931
1932                 /* skip saving proportional edit if it was not actually used */
1933                 if (!(t->options & CTX_NO_PET)) {
1934                         if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1935                             !RNA_property_is_set(op->ptr, prop))
1936                         {
1937                                 if (t->spacetype == SPACE_IPO)
1938                                         ts->proportional_fcurve = proportional;
1939                                 else if (t->spacetype == SPACE_ACTION)
1940                                         ts->proportional_action = proportional;
1941                                 else if (t->obedit)
1942                                         ts->proportional = proportional;
1943                                 else if (t->options & CTX_MASK)
1944                                         ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1945                                 else
1946                                         ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1947                         }
1948
1949                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_size"))) {
1950                                 ts->proportional_size =
1951                                         RNA_property_is_set(op->ptr, prop) ? RNA_property_float_get(op->ptr, prop) : t->prop_size;
1952                         }
1953
1954                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1955                             !RNA_property_is_set(op->ptr, prop))
1956                         {
1957                                 ts->prop_mode = t->prop_mode;
1958                         }
1959                 }
1960
1961                 /* do we check for parameter? */
1962                 if (t->modifiers & MOD_SNAP) {
1963                         ts->snap_flag |= SCE_SNAP;
1964                 }
1965                 else {
1966                         ts->snap_flag &= ~SCE_SNAP;
1967                 }
1968
1969                 if (t->spacetype == SPACE_VIEW3D) {
1970                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1971                             !RNA_property_is_set(op->ptr, prop))
1972                         {
1973                                 View3D *v3d = t->view;
1974
1975                                 v3d->twmode = t->current_orientation;
1976                         }
1977                 }
1978         }
1979
1980         if ((prop = RNA_struct_find_property(op->ptr, "proportional"))) {
1981                 RNA_property_enum_set(op->ptr, prop, proportional);
1982                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1983                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1984         }
1985
1986         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1987                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1988         }
1989
1990         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1991                 RNA_property_boolean_set(op->ptr, prop, (t->flag & T_MIRROR) != 0);
1992         }
1993
1994         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1995                 /* constraint orientation can be global, event if user selects something else
1996                  * so use the orientation in the constraint if set
1997                  * */
1998                 if (t->con.mode & CON_APPLY) {
1999                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
2000                 }
2001                 else {
2002                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
2003                 }
2004
2005                 if (t->con.mode & CON_APPLY) {
2006                         if (t->con.mode & CON_AXIS0) {
2007                                 constraint_axis[0] = 1;
2008                         }
2009                         if (t->con.mode & CON_AXIS1) {
2010                                 constraint_axis[1] = 1;
2011                         }
2012                         if (t->con.mode & CON_AXIS2) {
2013                                 constraint_axis[2] = 1;
2014                         }
2015                 }
2016
2017                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
2018         }
2019
2020         {
2021                 const char *prop_id = NULL;
2022                 if (t->mode == TFM_SHRINKFATTEN) {
2023                         prop_id = "use_even_offset";
2024                 }
2025
2026                 if (prop_id && (prop = RNA_struct_find_property(op->ptr, prop_id))) {
2027
2028                         RNA_property_boolean_set(op->ptr, prop, (t->flag & T_ALT_TRANSFORM) != 0);
2029                 }
2030         }
2031
2032         if ((prop = RNA_struct_find_property(op->ptr, "correct_uv"))) {
2033                 RNA_property_boolean_set(op->ptr, prop, (t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) != 0);
2034         }
2035 }
2036
2037 /**
2038  * \note  caller needs to free 't' on a 0 return
2039  * \warning  \a event might be NULL (when tweaking from redo panel)
2040  * \see #saveTransform which writes these values back.
2041  */
2042 bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event, int mode)
2043 {
2044         int options = 0;
2045         PropertyRNA *prop;
2046
2047         t->context = C;
2048
2049         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
2050
2051         t->state = TRANS_STARTING;
2052
2053         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
2054                 if (RNA_property_boolean_get(op->ptr, prop)) {
2055                         options |= CTX_TEXTURE;
2056                 }
2057         }
2058
2059         if ((prop = RNA_struct_find_property(op->ptr, "gpencil_strokes")) && RNA_property_is_set(op->ptr, prop)) {
2060                 if (RNA_property_boolean_get(op->ptr, prop)) {
2061                         options |= CTX_GPENCIL_STROKES;
2062                 }
2063         }
2064
2065         t->options = options;
2066
2067         t->mode = mode;
2068
2069         t->launch_event = event ? event->type : -1;
2070
2071         if (t->launch_event == EVT_TWEAK_R) {
2072                 t->launch_event = RIGHTMOUSE;
2073         }
2074         else if (t->launch_event == EVT_TWEAK_L) {
2075                 t->launch_event = LEFTMOUSE;
2076         }
2077
2078         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
2079         // For manipulator only, so assume LEFTMOUSE
2080         if (t->launch_event == 0) {
2081                 t->launch_event = LEFTMOUSE;
2082         }
2083
2084         unit_m3(t->spacemtx);
2085
2086         initTransInfo(C, t, op, event);
2087         initTransformOrientation(C, t);
2088
2089         if (t->spacetype == SPACE_VIEW3D) {
2090                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
2091                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2092                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2093                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2094         }
2095         else if (t->spacetype == SPACE_IMAGE) {
2096                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2097                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2098                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2099         }
2100         else if (t->spacetype == SPACE_CLIP) {
2101                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2102                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2103         }
2104         else if (t->spacetype == SPACE_NODE) {
2105                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
2106                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2107                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2108         }
2109         else if (t->spacetype == SPACE_IPO) {
2110                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2111                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2112                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2113         }
2114         else if (t->spacetype == SPACE_ACTION) {
2115                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2116                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2117                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2118         }
2119
2120         createTransData(C, t);          // make TransData structs from selection
2121
2122         if (t->total == 0) {
2123                 postTrans(C, t);
2124                 return 0;
2125         }
2126
2127         if (event) {
2128                 /* keymap for shortcut header prints */
2129                 t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
2130
2131                 /* Stupid code to have Ctrl-Click on manipulator work ok
2132                  *
2133                  * do this only for translation/rotation/resize due to only this
2134                  * moded are available from manipulator and doing such check could
2135                  * lead to keymap conflicts for other modes (see #31584)
2136                  */
2137                 if (ELEM(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
2138                         wmKeyMapItem *kmi;
2139
2140                         for (kmi = t->keymap->items.first; kmi; kmi = kmi->next) {
2141                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
2142                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
2143                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
2144                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
2145                                             ((kmi->type == OSKEY) &&                         event->oskey) )
2146                                         {
2147                                                 t->modifiers |= MOD_SNAP_INVERT;
2148                                         }
2149                                         break;
2150                                 }
2151                         }
2152                 }
2153         }
2154
2155         initSnapping(t, op); // Initialize snapping data AFTER mode flags
2156
2157         initSnapSpatial(t, t->snap_spatial);
2158
2159         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
2160         /* EVIL2: we gave as argument also texture space context bit... was cleared */
2161         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
2162         mode = t->mode;
2163
2164         calculatePropRatio(t);
2165         calculateCenter(t);
2166
2167         if (event) {
2168                 /* Initialize accurate transform to settings requested by keymap. */
2169                 bool use_accurate = false;
2170                 if ((prop = RNA_struct_find_property(op->ptr, "use_accurate")) && RNA_property_is_set(op->ptr, prop)) {
2171                         if (RNA_property_boolean_get(op->ptr, prop)) {
2172                                 use_accurate = true;
2173                         }
2174                 }
2175                 initMouseInput(t, &t->mouse, t->center2d, event->mval, use_accurate);
2176         }
2177
2178         switch (mode) {
2179                 case TFM_TRANSLATION:
2180                         initTranslation(t);
2181                         break;
2182                 case TFM_ROTATION:
2183                         initRotation(t);
2184                         break;
2185                 case TFM_RESIZE:
2186                         initResize(t);
2187                         break;
2188                 case TFM_SKIN_RESIZE:
2189                         initSkinResize(t);
2190                         break;
2191                 case TFM_TOSPHERE:
2192                         initToSphere(t);
2193                         break;
2194                 case TFM_SHEAR:
2195                         initShear(t);
2196                         break;
2197                 case TFM_BEND:
2198                         initBend(t);
2199                         break;
2200                 case TFM_SHRINKFATTEN:
2201                         initShrinkFatten(t);
2202                         break;
2203                 case TFM_TILT:
2204                         initTilt(t);
2205                         break;
2206                 case TFM_CURVE_SHRINKFATTEN:
2207                         initCurveShrinkFatten(t);
2208                         break;
2209                 case TFM_MASK_SHRINKFATTEN:
2210                         initMaskShrinkFatten(t);
2211                         break;
2212                 case TFM_GPENCIL_SHRINKFATTEN:
2213                         initGPShrinkFatten(t);
2214                         break;
2215                 case TFM_TRACKBALL:
2216                         initTrackball(t);
2217                         break;
2218                 case TFM_PUSHPULL:
2219                         initPushPull(t);
2220                         break;
2221                 case TFM_CREASE:
2222                         initCrease(t);
2223                         break;
2224                 case TFM_BONESIZE:
2225                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2226                         bArmature *arm = t->poseobj->data;
2227                         if (arm->drawtype == ARM_ENVELOPE) {
2228                                 initBoneEnvelope(t);
2229                                 t->mode = TFM_BONE_ENVELOPE_DIST;
2230                         }
2231                         else {
2232                                 initBoneSize(t);
2233                         }
2234                         break;
2235                 }
2236                 case TFM_BONE_ENVELOPE:
2237                         initBoneEnvelope(t);
2238                         break;
2239                 case TFM_BONE_ENVELOPE_DIST:
2240                         initBoneEnvelope(t);
2241                         t->mode = TFM_BONE_ENVELOPE_DIST;
2242                         break;
2243                 case TFM_EDGE_SLIDE:
2244                 case TFM_VERT_SLIDE:
2245                 {
2246                         const bool use_even = (op ? RNA_boolean_get(op->ptr, "use_even") : false);
2247                         const bool flipped = (op ? RNA_boolean_get(op->ptr, "flipped") : false);
2248                         const bool use_clamp = (op ? RNA_boolean_get(op->ptr, "use_clamp") : true);
2249                         if (mode == TFM_EDGE_SLIDE) {
2250                                 const bool use_double_side = (op ? !RNA_boolean_get(op->ptr, "single_side") : true);
2251                                 initEdgeSlide_ex(t, use_double_side, use_even, flipped, use_clamp);
2252                         }
2253                         else {
2254                                 initVertSlide_ex(t, use_even, flipped, use_clamp);
2255                         }
2256                         break;
2257                 }
2258                 case TFM_BONE_ROLL:
2259                         initBoneRoll(t);
2260                         break;
2261                 case TFM_TIME_TRANSLATE:
2262                         initTimeTranslate(t);
2263                         break;
2264                 case TFM_TIME_SLIDE:
2265                         initTimeSlide(t);
2266                         break;
2267                 case TFM_TIME_SCALE:
2268                         initTimeScale(t);
2269                         break;
2270                 case TFM_TIME_DUPLICATE:
2271                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2272                          * so that duplicate-culling will work properly
2273                          */
2274                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2275                                 initTranslation(t);
2276                         else
2277                                 initTimeTranslate(t);
2278                         t->mode = mode;
2279                         break;
2280                 case TFM_TIME_EXTEND:
2281                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2282                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2283                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2284                          * depending on which editor this was called from
2285                          */
2286                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2287                                 initTranslation(t);
2288                         else
2289                                 initTimeTranslate(t);
2290                         break;
2291                 case TFM_BAKE_TIME:
2292                         initBakeTime(t);
2293                         break;
2294                 case TFM_MIRROR:
2295                         initMirror(t);
2296                         break;
2297                 case TFM_BWEIGHT:
2298                         initBevelWeight(t);
2299                         break;
2300                 case TFM_ALIGN:
2301                         initAlign(t);
2302                         break;
2303                 case TFM_SEQ_SLIDE:
2304                         initSeqSlide(t);
2305                         break;
2306         }
2307
2308         if (t->state == TRANS_CANCEL) {
2309                 postTrans(C, t);
2310                 return 0;
2311         }
2312
2313         /* Transformation axis from operator */
2314         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2315                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2316                 normalize_v3(t->axis);
2317                 copy_v3_v3(t->axis_orig, t->axis);
2318         }
2319
2320         /* Constraint init from operator */
2321         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2322                 int constraint_axis[3];
2323
2324                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2325
2326                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2327                         t->con.mode |= CON_APPLY;
2328
2329                         if (constraint_axis[0]) {
2330                                 t->con.mode |= CON_AXIS0;
2331                         }
2332                         if (constraint_axis[1]) {
2333                                 t->con.mode |= CON_AXIS1;
2334                         }
2335                         if (constraint_axis[2]) {
2336                                 t->con.mode |= CON_AXIS2;
2337                         }
2338
2339                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2340                 }
2341         }
2342
2343         /* overwrite initial values if operator supplied a non-null vector
2344          *
2345          * keep last so we can apply the constraints space.
2346          */
2347         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2348                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2349
2350                 if (RNA_property_array_check(prop)) {
2351                         RNA_float_get_array(op->ptr, "value", values);
2352                 }
2353                 else {
2354                         values[0] = RNA_float_get(op->ptr, "value");
2355                 }
2356
2357                 copy_v4_v4(t->values, values);
2358                 copy_v4_v4(t->auto_values, values);
2359                 t->flag |= T_AUTOVALUES;
2360         }
2361
2362         t->context = NULL;
2363
2364         return 1;
2365 }
2366
2367 void transformApply(bContext *C, TransInfo *t)
2368 {
2369         t->context = C;
2370
2371         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2372                 selectConstraint(t);
2373                 if (t->transform) {
2374                         t->transform(t, t->mval);  // calls recalcData()
2375                         viewRedrawForce(C, t);
2376                 }
2377                 t->redraw = TREDRAW_NOTHING;
2378         }
2379         else if (t->redraw & TREDRAW_SOFT) {
2380                 viewRedrawForce(C, t);
2381         }
2382
2383         /* If auto confirm is on, break after one pass */
2384         if (t->options & CTX_AUTOCONFIRM) {
2385                 t->state = TRANS_CONFIRM;
2386         }
2387
2388         t->context = NULL;
2389 }
2390
2391 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2392 {
2393         TransInfo *t = arg;
2394
2395         if (t->redraw & TREDRAW_SOFT) {
2396                 t->redraw |= TREDRAW_HARD;
2397                 transformApply((bContext *)C, t);
2398         }
2399 }
2400
2401 int transformEnd(bContext *C, TransInfo *t)
2402 {
2403         int exit_code = OPERATOR_RUNNING_MODAL;
2404
2405         t->context = C;
2406
2407         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2408                 /* handle restoring objects */
2409                 if (t->state == TRANS_CANCEL) {
2410                         /* exception, edge slide transformed UVs too */
2411                         if (t->mode == TFM_EDGE_SLIDE) {
2412                                 doEdgeSlide(t, 0.0f);
2413                         }
2414                         else if (t->mode == TFM_VERT_SLIDE) {
2415                                 doVertSlide(t, 0.0f);
2416                         }
2417
2418                         exit_code = OPERATOR_CANCELLED;
2419                         restoreTransObjects(t); // calls recalcData()
2420                 }
2421                 else {
2422                         exit_code = OPERATOR_FINISHED;
2423                 }
2424
2425                 /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
2426                 special_aftertrans_update(C, t);
2427
2428                 /* free data */
2429                 postTrans(C, t);
2430
2431                 /* send events out for redraws */
2432                 viewRedrawPost(C, t);
2433
2434                 viewRedrawForce(C, t);
2435         }
2436
2437         t->context = NULL;
2438
2439         return exit_code;
2440 }
2441
2442 /* ************************** TRANSFORM LOCKS **************************** */
2443
2444 static void protectedTransBits(short protectflag, float vec[3])
2445 {
2446         if (protectflag & OB_LOCK_LOCX)
2447                 vec[0] = 0.0f;
2448         if (protectflag & OB_LOCK_LOCY)
2449                 vec[1] = 0.0f;
2450         if (protectflag & OB_LOCK_LOCZ)
2451                 vec[2] = 0.0f;
2452 }
2453
2454 static void protectedSizeBits(short protectflag, float size[3])
2455 {
2456         if (protectflag & OB_LOCK_SCALEX)
2457                 size[0] = 1.0f;
2458         if (protectflag & OB_LOCK_SCALEY)
2459                 size[1] = 1.0f;
2460         if (protectflag & OB_LOCK_SCALEZ)
2461                 size[2] = 1.0f;
2462 }
2463
2464 static void protectedRotateBits(short protectflag, float eul[3], const float oldeul[3])
2465 {
2466         if (protectflag & OB_LOCK_ROTX)
2467                 eul[0] = oldeul[0];
2468         if (protectflag & OB_LOCK_ROTY)
2469                 eul[1] = oldeul[1];
2470         if (protectflag & OB_LOCK_ROTZ)
2471                 eul[2] = oldeul[2];
2472 }
2473
2474
2475 /* this function only does the delta rotation */
2476 /* axis-angle is usually internally stored as quats... */
2477 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2478 {
2479         /* check that protection flags are set */
2480         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2481                 return;
2482
2483         if (protectflag & OB_LOCK_ROT4D) {
2484                 /* axis-angle getting limited as 4D entities that they are... */
2485                 if (protectflag & OB_LOCK_ROTW)
2486                         *angle = oldAngle;
2487                 if (protectflag & OB_LOCK_ROTX)
2488                         axis[0] = oldAxis[0];
2489                 if (protectflag & OB_LOCK_ROTY)
2490                         axis[1] = oldAxis[1];
2491                 if (protectflag & OB_LOCK_ROTZ)
2492                         axis[2] = oldAxis[2];
2493         }
2494         else {
2495                 /* axis-angle get limited with euler... */
2496                 float eul[3], oldeul[3];
2497
2498                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2499                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2500
2501                 if (protectflag & OB_LOCK_ROTX)
2502                         eul[0] = oldeul[0];
2503                 if (protectflag & OB_LOCK_ROTY)
2504                         eul[1] = oldeul[1];
2505                 if (protectflag & OB_LOCK_ROTZ)
2506                         eul[2] = oldeul[2];
2507
2508                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2509
2510                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2511                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2512                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2513                         axis[1] = 1.0f;
2514                 }
2515         }
2516 }
2517
2518 /* this function only does the delta rotation */
2519 static void protectedQuaternionBits(short protectflag, float quat[4], const float oldquat[4])
2520 {
2521         /* check that protection flags are set */
2522         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2523                 return;
2524
2525         if (protectflag & OB_LOCK_ROT4D) {
2526                 /* quaternions getting limited as 4D entities that they are... */
2527                 if (protectflag & OB_LOCK_ROTW)
2528                         quat[0] = oldquat[0];
2529                 if (protectflag & OB_LOCK_ROTX)
2530                         quat[1] = oldquat[1];
2531                 if (protectflag & OB_LOCK_ROTY)
2532                         quat[2] = oldquat[2];
2533                 if (protectflag & OB_LOCK_ROTZ)
2534                         quat[3] = oldquat[3];
2535         }
2536         else {
2537                 /* quaternions get limited with euler... (compatibility mode) */
2538                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2539                 float qlen;
2540
2541                 qlen = normalize_qt_qt(nquat, quat);
2542                 normalize_qt_qt(noldquat, oldquat);
2543
2544                 quat_to_eul(eul, nquat);
2545                 quat_to_eul(oldeul, noldquat);
2546
2547                 if (protectflag & OB_LOCK_ROTX)
2548                         eul[0] = oldeul[0];
2549                 if (protectflag & OB_LOCK_ROTY)
2550                         eul[1] = oldeul[1];
2551                 if (protectflag & OB_LOCK_ROTZ)
2552                         eul[2] = oldeul[2];
2553
2554                 eul_to_quat(quat, eul);
2555
2556                 /* restore original quat size */
2557                 mul_qt_fl(quat, qlen);
2558
2559                 /* quaternions flip w sign to accumulate rotations correctly */
2560                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2561                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2562                 {
2563                         mul_qt_fl(quat, -1.0f);
2564                 }
2565         }
2566 }
2567
2568 /* ******************* TRANSFORM LIMITS ********************** */
2569
2570 static void constraintTransLim(TransInfo *t, TransData *td)
2571 {
2572         if (td->con) {
2573                 const bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_LOCLIMIT);
2574                 const bConstraintTypeInfo *ctiDist = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_DISTLIMIT);
2575
2576                 bConstraintOb cob = {NULL};
2577                 bConstraint *con;
2578                 float ctime = (float)(t->scene->r.cfra);
2579
2580                 /* Make a temporary bConstraintOb for using these limit constraints
2581                  *  - they only care that cob->matrix is correctly set ;-)
2582                  *      - current space should be local
2583                  */
2584                 unit_m4(cob.matrix);
2585                 copy_v3_v3(cob.matrix[3], td->loc);
2586
2587                 /* Evaluate valid constraints */
2588                 for (con = td->con; con; con = con->next) {
2589                         const bConstraintTypeInfo *cti = NULL;
2590                         ListBase targets = {NULL, NULL};
2591
2592                         /* only consider constraint if enabled */
2593                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2594                         if (con->enforce == 0.0f) continue;
2595
2596                         /* only use it if it's tagged for this purpose (and the right type) */
2597                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2598                                 bLocLimitConstraint *data = con->data;
2599
2600                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2601                                         continue;
2602                                 cti = ctiLoc;
2603                         }
2604                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2605                                 bDistLimitConstraint *data = con->data;
2606
2607                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2608                                         continue;
2609                                 cti = ctiDist;
2610                         }
2611
2612                         if (cti) {
2613                                 /* do space conversions */
2614                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2615                                         /* just multiply by td->mtx (this should be ok) */
2616                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2617                                 }
2618                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2619                                         /* skip... incompatable spacetype */
2620                                         continue;
2621                                 }
2622
2623                                 /* get constraint targets if needed */
2624                                 BKE_constraint_targets_for_solving_get(con, &cob, &targets, ctime);
2625
2626                                 /* do constraint */
2627                                 cti->evaluate_constraint(con, &cob, &targets);
2628
2629                                 /* convert spaces again */
2630                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2631                                         /* just multiply by td->smtx (this should be ok) */
2632                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2633                                 }
2634
2635                                 /* free targets list */
2636                                 BLI_freelistN(&targets);
2637                         }
2638                 }
2639
2640                 /* copy results from cob->matrix */
2641                 copy_v3_v3(td->loc, cob.matrix[3]);
2642         }
2643 }
2644
2645 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2646 {
2647         /* Make a temporary bConstraintOb for use by limit constraints
2648          *  - they only care that cob->matrix is correctly set ;-)
2649          *      - current space should be local
2650          */
2651         memset(cob, 0, sizeof(bConstraintOb));
2652         if (td->ext) {
2653                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2654                         /* quats */
2655                         /* objects and bones do normalization first too, otherwise
2656                          * we don't necessarily end up with a rotation matrix, and
2657                          * then conversion back to quat gives a different result */
2658                         float quat[4];
2659                         normalize_qt_qt(quat, td->ext->quat);
2660                         quat_to_mat4(cob->matrix, quat);
2661                 }
2662                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2663                         /* axis angle */
2664                         axis_angle_to_mat4(cob->matrix, td->ext->rotAxis, *td->ext->rotAngle);
2665                 }
2666                 else {
2667                         /* eulers */
2668                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2669                 }
2670         }
2671 }
2672
2673 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2674 {
2675         if (td->con) {
2676                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT);
2677                 bConstraintOb cob;
2678                 bConstraint *con;
2679                 bool do_limit = false;
2680
2681                 /* Evaluate valid constraints */
2682                 for (con = td->con; con; con = con->next) {
2683                         /* only consider constraint if enabled */
2684                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2685                         if (con->enforce == 0.0f) continue;
2686
2687                         /* we're only interested in Limit-Rotation constraints */
2688                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2689                                 bRotLimitConstraint *data = con->data;
2690
2691                                 /* only use it if it's tagged for this purpose */
2692                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2693                                         continue;
2694
2695                                 /* skip incompatable spacetypes */
2696                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2697                                         continue;
2698
2699                                 /* only do conversion if necessary, to preserve quats and eulers */
2700                                 if (do_limit == false) {
2701                                         constraintob_from_transdata(&cob, td);
2702                                         do_limit = true;
2703                                 }
2704
2705                                 /* do space conversions */
2706                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2707                                         /* just multiply by td->mtx (this should be ok) */
2708                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2709                                 }
2710
2711                                 /* do constraint */
2712                                 cti->evaluate_constraint(con, &cob, NULL);
2713
2714                                 /* convert spaces again */
2715                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2716                                         /* just multiply by td->smtx (this should be ok) */
2717                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2718                                 }
2719                         }
2720                 }
2721
2722                 if (do_limit) {
2723                         /* copy results from cob->matrix */
2724                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2725                                 /* quats */
2726                                 mat4_to_quat(td->ext->quat, cob.matrix);
2727                         }
2728                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2729                                 /* axis angle */
2730                                 mat4_to_axis_angle(td->ext->rotAxis, td->ext->rotAngle, cob.matrix);
2731                         }
2732                         else {
2733                                 /* eulers */
2734                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2735                         }
2736                 }
2737         }
2738 }
2739
2740 static void constraintSizeLim(TransInfo *t, TransData *td)
2741 {
2742         if (td->con && td->ext) {
2743                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT);
2744                 bConstraintOb cob = {NULL};
2745                 bConstraint *con;
2746                 float size_sign[3], size_abs[3];
2747                 int i;
2748
2749                 /* Make a temporary bConstraintOb for using these limit constraints
2750                  *  - they only care that cob->matrix is correctly set ;-)
2751                  *      - current space should be local
2752                  */
2753                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2754                         /* scale val and reset size */
2755                         return; // TODO: fix this case
2756                 }
2757                 else {
2758                         /* Reset val if SINGLESIZE but using a constraint */
2759                         if (td->flag & TD_SINGLESIZE)
2760                                 return;
2761
2762                         /* separate out sign to apply back later */
2763                         for (i = 0; i < 3; i++) {
2764                                 size_sign[i] = signf(td->ext->size[i]);
2765                                 size_abs[i] = fabsf(td->ext->size[i]);
2766                         }
2767
2768                         size_to_mat4(cob.matrix, size_abs);
2769                 }
2770
2771                 /* Evaluate valid constraints */
2772                 for (con = td->con; con; con = con->next) {
2773                         /* only consider constraint if enabled */
2774                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2775                         if (con->enforce == 0.0f) continue;
2776
2777                         /* we're only interested in Limit-Scale constraints */
2778                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2779                                 bSizeLimitConstraint *data = con->data;
2780
2781                                 /* only use it if it's tagged for this purpose */
2782                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2783                                         continue;
2784
2785                                 /* do space conversions */
2786                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2787                                         /* just multiply by td->mtx (this should be ok) */
2788                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2789                                 }
2790                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2791                                         /* skip... incompatible spacetype */
2792                                         continue;
2793                                 }
2794
2795                                 /* do constraint */
2796                                 cti->evaluate_constraint(con, &cob, NULL);
2797
2798                                 /* convert spaces again */
2799                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2800                                         /* just multiply by td->smtx (this should be ok) */
2801                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2802                                 }
2803                         }
2804                 }
2805
2806                 /* copy results from cob->matrix */
2807                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2808                         /* scale val and reset size */
2809                         return; // TODO: fix this case
2810                 }
2811                 else {
2812                         /* Reset val if SINGLESIZE but using a constraint */
2813                         if (td->flag & TD_SINGLESIZE)
2814                                 return;
2815
2816                         /* extrace scale from matrix and apply back sign */
2817                         mat4_to_size(td->ext->size, cob.matrix);
2818                         mul_v3_v3(td->ext->size, size_sign);
2819                 }
2820         }
2821 }
2822
2823
2824 /* -------------------------------------------------------------------- */
2825 /* Transform (Bend) */
2826
2827 /** \name Transform Bend
2828  * \{ */
2829
2830 struct BendCustomData {
2831         float warp_sta[3];
2832         float warp_end[3];
2833
2834         float warp_nor[3];
2835         float warp_tan[3];
2836
2837         /* for applying the mouse distance */
2838         float warp_init_dist;
2839 };
2840
2841 static void initBend(TransInfo *t)
2842 {
2843         const float mval_fl[2] = {UNPACK2(t->mval)};
2844         const float *curs;
2845         float tvec[3];
2846         struct BendCustomData *data;
2847
2848         t->mode = TFM_BEND;
2849         t->transform = Bend;
2850         t->handleEvent = handleEventBend;
2851
2852         setInputPostFct(&t->mouse, postInputRotation);
2853         initMouseInputMode(t, &t->mouse, INPUT_ANGLE_SPRING);
2854
2855         t->idx_max = 1;
2856         t->num.idx_max = 1;
2857         t->snap[0] = 0.0f;
2858         t->snap[1] = DEG2RAD(5.0);
2859         t->snap[2] = DEG2RAD(1.0);
2860
2861         copy_v3_fl(t->num.val_inc, t->snap[1]);
2862         t->num.unit_sys = t->scene->unit.system;
2863         t->num.unit_use_radians = (t->scene->unit.system_rotation == USER_UNIT_ROT_RADIANS);
2864         t->num.unit_type[0] = B_UNIT_ROTATION;
2865         t->num.unit_type[1] = B_UNIT_LENGTH;
2866
2867         t->flag |= T_NO_CONSTRAINT;
2868
2869         //copy_v3_v3(t->center, ED_view3d_cursor3d_get(t->scene, t->view));
2870         if ((t->flag & T_OVERRIDE_CENTER) == 0) {
2871            &n