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