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