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