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