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