c8874d13cac3f47ef200bbc7c1039f2c1be935a7
[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);
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 view_editmove(event->type);
1302                                 t->redraw = 1;
1303                                 break;
1304                         case PADMINUS:
1305                                 if (event->alt && t->flag & T_PROP_EDIT) {
1306                                         t->prop_size *= 0.90909090f;
1307                                         calculatePropRatio(t);
1308                                 }
1309                                 t->redraw = 1;
1310                                 break;
1311                         case PAGEDOWNKEY:
1312                         case WHEELUPMOUSE:
1313                                 if (t->flag & T_AUTOIK) {
1314                                         transform_autoik_update(t, -1);
1315                                 }
1316                                 else view_editmove(event->type);
1317                                 t->redraw = 1;
1318                                 break;
1319                         case LEFTALTKEY:
1320                         case RIGHTALTKEY:
1321                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1322                                         t->flag |= T_ALT_TRANSFORM;
1323                                         t->redraw |= TREDRAW_HARD;
1324                                 }
1325
1326                                 break;
1327                         default:
1328                                 handled = 0;
1329                                 break;
1330                 }
1331
1332                 // Numerical input events
1333                 t->redraw |= handleNumInput(&(t->num), event);
1334
1335                 // Snapping key events
1336                 t->redraw |= handleSnapping(t, event);
1337
1338         }
1339         else if (event->val == KM_RELEASE) {
1340                 switch (event->type) {
1341                         case LEFTSHIFTKEY:
1342                         case RIGHTSHIFTKEY:
1343                                 t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1344                                 t->redraw |= TREDRAW_HARD;
1345                                 break;
1346
1347                         case MIDDLEMOUSE:
1348                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1349                                         t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1350                                         postSelectConstraint(t);
1351                                         t->redraw |= TREDRAW_HARD;
1352                                 }
1353                                 break;
1354 //              case LEFTMOUSE:
1355 //              case RIGHTMOUSE:
1356 //                      if (WM_modal_tweak_exit(event, t->event_type))
1357 ////                    if (t->options & CTX_TWEAK)
1358 //                              t->state = TRANS_CONFIRM;
1359 //                      break;
1360                         case LEFTALTKEY:
1361                         case RIGHTALTKEY:
1362                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1363                                         t->flag &= ~T_ALT_TRANSFORM;
1364                                         t->redraw |= TREDRAW_HARD;
1365                                 }
1366
1367                                 break;
1368                         default:
1369                                 handled = 0;
1370                                 break;
1371                 }
1372
1373                 /* confirm transform if launch key is released after mouse move */
1374                 if (t->flag & T_RELEASE_CONFIRM) {
1375                         /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1376                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1377                                 t->state = TRANS_CONFIRM;
1378                         }
1379                 }
1380         }
1381         else
1382                 handled = 0;
1383
1384         // Per transform event, if present
1385         if (t->handleEvent)
1386                 t->redraw |= t->handleEvent(t, event);
1387
1388         if (handled || t->redraw) {
1389                 return 0;
1390         }
1391         else {
1392                 return OPERATOR_PASS_THROUGH;
1393         }
1394 }
1395
1396 int calculateTransformCenter(bContext *C, int centerMode, float cent3d[3], int cent2d[2])
1397 {
1398         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1399         int success;
1400
1401         t->state = TRANS_RUNNING;
1402
1403         t->options = CTX_NONE;
1404
1405         t->mode = TFM_DUMMY;
1406
1407         initTransInfo(C, t, NULL, NULL);    // internal data, mouse, vectors
1408
1409         createTransData(C, t);              // make TransData structs from selection
1410
1411         t->around = centerMode;             // override userdefined mode
1412
1413         if (t->total == 0) {
1414                 success = FALSE;
1415         }
1416         else {
1417                 success = TRUE;
1418
1419                 calculateCenter(t);
1420
1421                 if (cent2d) {
1422                         copy_v2_v2_int(cent2d, t->center2d);
1423                 }
1424
1425                 if (cent3d) {
1426                         // Copy center from constraint center. Transform center can be local
1427                         copy_v3_v3(cent3d, t->con.center);
1428                 }
1429         }
1430
1431
1432         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1433         special_aftertrans_update(C, t);
1434
1435         postTrans(C, t);
1436
1437         MEM_freeN(t);
1438
1439         return success;
1440 }
1441
1442 typedef enum {
1443         UP,
1444         DOWN,
1445         LEFT,
1446         RIGHT
1447 } ArrowDirection;
1448 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1449 {
1450         switch (d) {
1451                 case LEFT:
1452                         offset = -offset;
1453                         length = -length;
1454                         size = -size;
1455                 case RIGHT:
1456                         glBegin(GL_LINES);
1457                         glVertex2s(offset, 0);
1458                         glVertex2s(offset + length, 0);
1459                         glVertex2s(offset + length, 0);
1460                         glVertex2s(offset + length - size, -size);
1461                         glVertex2s(offset + length, 0);
1462                         glVertex2s(offset + length - size,  size);
1463                         glEnd();
1464                         break;
1465                 case DOWN:
1466                         offset = -offset;
1467                         length = -length;
1468                         size = -size;
1469                 case UP:
1470                         glBegin(GL_LINES);
1471                         glVertex2s(0, offset);
1472                         glVertex2s(0, offset + length);
1473                         glVertex2s(0, offset + length);
1474                         glVertex2s(-size, offset + length - size);
1475                         glVertex2s(0, offset + length);
1476                         glVertex2s(size, offset + length - size);
1477                         glEnd();
1478                         break;
1479         }
1480 }
1481
1482 static void drawArrowHead(ArrowDirection d, short size)
1483 {
1484         switch (d) {
1485                 case LEFT:
1486                         size = -size;
1487                 case RIGHT:
1488                         glBegin(GL_LINES);
1489                         glVertex2s(0, 0);
1490                         glVertex2s(-size, -size);
1491                         glVertex2s(0, 0);
1492                         glVertex2s(-size,  size);
1493                         glEnd();
1494                         break;
1495                 case DOWN:
1496                         size = -size;
1497                 case UP:
1498                         glBegin(GL_LINES);
1499                         glVertex2s(0, 0);
1500                         glVertex2s(-size, -size);
1501                         glVertex2s(0, 0);
1502                         glVertex2s(size, -size);
1503                         glEnd();
1504                         break;
1505         }
1506 }
1507
1508 static void drawArc(float size, float angle_start, float angle_end, int segments)
1509 {
1510         float delta = (angle_end - angle_start) / segments;
1511         float angle;
1512         int a;
1513
1514         glBegin(GL_LINE_STRIP);
1515
1516         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1517                 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1518         }
1519         glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1520
1521         glEnd();
1522 }
1523
1524 static int helpline_poll(bContext *C)
1525 {
1526         ARegion *ar = CTX_wm_region(C);
1527         
1528         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1529                 return 1;
1530         return 0;
1531 }
1532
1533 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1534 {
1535         TransInfo *t = (TransInfo *)customdata;
1536
1537         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1538                 float vecrot[3], cent[2];
1539                 int mval[2];
1540
1541                 mval[0] = x;
1542                 mval[1] = y;
1543
1544                 copy_v3_v3(vecrot, t->center);
1545                 if (t->flag & T_EDIT) {
1546                         Object *ob = t->obedit;
1547                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1548                 }
1549                 else if (t->flag & T_POSE) {
1550                         Object *ob = t->poseobj;
1551                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1552                 }
1553
1554                 projectFloatViewEx(t, vecrot, cent, V3D_PROJ_TEST_CLIP_ZERO);
1555
1556                 glPushMatrix();
1557
1558                 switch (t->helpline) {
1559                         case HLP_SPRING:
1560                                 UI_ThemeColor(TH_WIRE);
1561
1562                                 setlinestyle(3);
1563                                 glBegin(GL_LINE_STRIP);
1564                                 glVertex2iv(t->mval);
1565                                 glVertex2fv(cent);
1566                                 glEnd();
1567
1568                                 glTranslatef(mval[0], mval[1], 0);
1569                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1570
1571                                 setlinestyle(0);
1572                                 glLineWidth(3.0);
1573                                 drawArrow(UP, 5, 10, 5);
1574                                 drawArrow(DOWN, 5, 10, 5);
1575                                 glLineWidth(1.0);
1576                                 break;
1577                         case HLP_HARROW:
1578                                 UI_ThemeColor(TH_WIRE);
1579
1580                                 glTranslatef(mval[0], mval[1], 0);
1581
1582                                 glLineWidth(3.0);
1583                                 drawArrow(RIGHT, 5, 10, 5);
1584                                 drawArrow(LEFT, 5, 10, 5);
1585                                 glLineWidth(1.0);
1586                                 break;
1587                         case HLP_VARROW:
1588                                 UI_ThemeColor(TH_WIRE);
1589
1590                                 glTranslatef(mval[0], mval[1], 0);
1591
1592                                 glLineWidth(3.0);
1593                                 drawArrow(UP, 5, 10, 5);
1594                                 drawArrow(DOWN, 5, 10, 5);
1595                                 glLineWidth(1.0);
1596                                 break;
1597                         case HLP_ANGLE:
1598                         {
1599                                 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1600                                 float angle = atan2f(dy, dx);
1601                                 float dist = sqrtf(dx * dx + dy * dy);
1602                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1603                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1604                                 UI_ThemeColor(TH_WIRE);
1605
1606                                 setlinestyle(3);
1607                                 glBegin(GL_LINE_STRIP);
1608                                 glVertex2iv(t->mval);
1609                                 glVertex2fv(cent);
1610                                 glEnd();
1611
1612                                 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1613
1614                                 setlinestyle(0);
1615                                 glLineWidth(3.0);
1616                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1617                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1618
1619                                 glPushMatrix();
1620
1621                                 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1622                                 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1623
1624                                 drawArrowHead(DOWN, 5);
1625
1626                                 glPopMatrix();
1627
1628                                 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1629                                 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1630
1631                                 drawArrowHead(UP, 5);
1632
1633                                 glLineWidth(1.0);
1634                                 break;
1635                         }
1636                         case HLP_TRACKBALL:
1637                         {
1638                                 unsigned char col[3], col2[3];
1639                                 UI_GetThemeColor3ubv(TH_GRID, col);
1640
1641                                 glTranslatef(mval[0], mval[1], 0);
1642
1643                                 glLineWidth(3.0);
1644
1645                                 UI_make_axis_color(col, col2, 'X');
1646                                 glColor3ubv((GLubyte *)col2);
1647
1648                                 drawArrow(RIGHT, 5, 10, 5);
1649                                 drawArrow(LEFT, 5, 10, 5);
1650
1651                                 UI_make_axis_color(col, col2, 'Y');
1652                                 glColor3ubv((GLubyte *)col2);
1653
1654                                 drawArrow(UP, 5, 10, 5);
1655                                 drawArrow(DOWN, 5, 10, 5);
1656                                 glLineWidth(1.0);
1657                                 break;
1658                         }
1659                 }
1660
1661                 glPopMatrix();
1662         }
1663 }
1664
1665 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1666 {
1667         TransInfo *t = arg;
1668
1669         drawConstraint(t);
1670         drawPropCircle(C, t);
1671         drawSnapping(C, t);
1672
1673         /* edge slide, vert slide */
1674         drawEdgeSlide(C, t);
1675         drawVertSlide(C, t);
1676 }
1677
1678 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1679 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1680 {
1681         rcti rect;
1682         const char *printable = IFACE_("Auto Keying On");
1683         float      printable_size[2];
1684         int xco, yco;
1685
1686         ED_region_visible_rect(ar, &rect);
1687         
1688         BLF_width_and_height_default(printable, &printable_size[0], &printable_size[1]);
1689         
1690         xco = rect.xmax - (int)printable_size[0] - 10;
1691         yco = rect.ymax - (int)printable_size[1] - 10;
1692         
1693         /* warning text (to clarify meaning of overlays)
1694          * - original color was red to match the icon, but that clashes badly with a less nasty border
1695          */
1696         UI_ThemeColorShade(TH_TEXT_HI, -50);
1697 #ifdef WITH_INTERNATIONAL
1698         BLF_draw_default(xco, ar->winy - 17, 0.0f, printable, sizeof(printable));
1699 #else
1700         BLF_draw_default_ascii(xco, ar->winy - 17, 0.0f, printable, sizeof(printable));
1701 #endif
1702         
1703         /* autokey recording icon... */
1704         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1705         glEnable(GL_BLEND);
1706         
1707         xco -= (ICON_DEFAULT_WIDTH + 2);
1708         UI_icon_draw(xco, yco, ICON_REC);
1709         
1710         glDisable(GL_BLEND);
1711 }
1712
1713 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1714 {       
1715         TransInfo *t = arg;
1716         Scene *scene = t->scene;
1717         Object *ob = OBACT;
1718         
1719         /* draw autokeyframing hint in the corner 
1720          * - only draw if enabled (advanced users may be distracted/annoyed), 
1721          *   for objects that will be autokeyframed (no point ohterwise),
1722          *   AND only for the active region (as showing all is too overwhelming)
1723          */
1724         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1725                 if (ar == t->ar) {
1726                         if (t->flag & (T_OBJECT | T_POSE)) {
1727                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1728                                         drawAutoKeyWarning(t, ar);
1729                                 }
1730                         }
1731                 }
1732         }
1733 }
1734
1735 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1736 {
1737         ToolSettings *ts = CTX_data_tool_settings(C);
1738         int constraint_axis[3] = {0, 0, 0};
1739         int proportional = 0;
1740         PropertyRNA *prop;
1741
1742         // Save back mode in case we're in the generic operator
1743         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1744                 RNA_property_enum_set(op->ptr, prop, t->mode);
1745         }
1746
1747         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1748                 float *values = (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1749                 if (RNA_property_array_check(prop)) {
1750                         RNA_property_float_set_array(op->ptr, prop, values);
1751                 }
1752                 else {
1753                         RNA_property_float_set(op->ptr, prop, values[0]);
1754                 }
1755         }
1756
1757         /* convert flag to enum */
1758         switch (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) {
1759                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1760                         proportional = PROP_EDIT_CONNECTED;
1761                         break;
1762                 case T_PROP_EDIT:
1763                         proportional = PROP_EDIT_ON;
1764                         break;
1765                 default:
1766                         proportional = PROP_EDIT_OFF;
1767         }
1768
1769         // If modal, save settings back in scene if not set as operator argument
1770         if (t->flag & T_MODAL) {
1771
1772                 /* save settings if not set in operator */
1773                 if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1774                     !RNA_property_is_set(op->ptr, prop))
1775                 {
1776                         if (t->obedit)
1777                                 ts->proportional = proportional;
1778                         else if (t->options & CTX_MASK)
1779                                 ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1780                         else
1781                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1782                 }
1783
1784                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1785                     !RNA_property_is_set(op->ptr, prop))
1786                 {
1787                         ts->proportional_size = t->prop_size;
1788                 }
1789
1790                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1791                     !RNA_property_is_set(op->ptr, prop))
1792                 {
1793                         ts->prop_mode = t->prop_mode;
1794                 }
1795                 
1796                 /* do we check for parameter? */
1797                 if (t->modifiers & MOD_SNAP) {
1798                         ts->snap_flag |= SCE_SNAP;
1799                 }
1800                 else {
1801                         ts->snap_flag &= ~SCE_SNAP;
1802                 }
1803
1804                 if (t->spacetype == SPACE_VIEW3D) {
1805                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1806                             !RNA_property_is_set(op->ptr, prop))
1807                         {
1808                                 View3D *v3d = t->view;
1809
1810                                 v3d->twmode = t->current_orientation;
1811                         }
1812                 }
1813         }
1814         
1815         if (RNA_struct_find_property(op->ptr, "proportional")) {
1816                 RNA_enum_set(op->ptr, "proportional", proportional);
1817                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1818                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1819         }
1820
1821         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1822                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1823         }
1824
1825         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1826                 RNA_property_boolean_set(op->ptr, prop, t->flag & T_MIRROR);
1827         }
1828
1829         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1830                 /* constraint orientation can be global, event if user selects something else
1831                  * so use the orientation in the constraint if set
1832                  * */
1833                 if (t->con.mode & CON_APPLY) {
1834                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1835                 }
1836                 else {
1837                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1838                 }
1839
1840                 if (t->con.mode & CON_APPLY) {
1841                         if (t->con.mode & CON_AXIS0) {
1842                                 constraint_axis[0] = 1;
1843                         }
1844                         if (t->con.mode & CON_AXIS1) {
1845                                 constraint_axis[1] = 1;
1846                         }
1847                         if (t->con.mode & CON_AXIS2) {
1848                                 constraint_axis[2] = 1;
1849                         }
1850                 }
1851
1852                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
1853         }
1854 }
1855
1856 /* note: caller needs to free 't' on a 0 return */
1857 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1858 {
1859         int options = 0;
1860         PropertyRNA *prop;
1861
1862         t->context = C;
1863
1864         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1865
1866         t->state = TRANS_STARTING;
1867
1868         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
1869                 if (RNA_property_boolean_get(op->ptr, prop)) {
1870                         options |= CTX_TEXTURE;
1871                 }
1872         }
1873         
1874         t->options = options;
1875
1876         t->mode = mode;
1877
1878         t->launch_event = event ? event->type : -1;
1879
1880         if (t->launch_event == EVT_TWEAK_R) {
1881                 t->launch_event = RIGHTMOUSE;
1882         }
1883         else if (t->launch_event == EVT_TWEAK_L) {
1884                 t->launch_event = LEFTMOUSE;
1885         }
1886
1887         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1888         // For manipulator only, so assume LEFTMOUSE
1889         if (t->launch_event == 0) {
1890                 t->launch_event = LEFTMOUSE;
1891         }
1892
1893         if (!initTransInfo(C, t, op, event)) {  /* internal data, mouse, vectors */
1894                 return 0;
1895         }
1896
1897         if (t->spacetype == SPACE_VIEW3D) {
1898                 //calc_manipulator_stats(curarea);
1899                 initTransformOrientation(C, t);
1900
1901                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1902                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1903                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1904                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1905         }
1906         else if (t->spacetype == SPACE_IMAGE) {
1907                 unit_m3(t->spacemtx);
1908                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1909                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1910                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1911         }
1912         else if (t->spacetype == SPACE_CLIP) {
1913                 unit_m3(t->spacemtx);
1914                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1915         }
1916         else if (t->spacetype == SPACE_NODE) {
1917                 unit_m3(t->spacemtx);
1918                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
1919                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1920                 /*t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);*/
1921         }
1922         else
1923                 unit_m3(t->spacemtx);
1924
1925         createTransData(C, t);          // make TransData structs from selection
1926
1927         if (t->total == 0) {
1928                 postTrans(C, t);
1929                 return 0;
1930         }
1931
1932         /* Stupid code to have Ctrl-Click on manipulator work ok */
1933         if (event) {
1934                 /* do this only for translation/rotation/resize due to only this
1935                  * moded are available from manipulator and doing such check could
1936                  * lead to keymap conflicts for other modes (see #31584)
1937                  */
1938                 if (ELEM3(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
1939                         wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1940                         wmKeyMapItem *kmi;
1941
1942                         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1943                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
1944                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
1945                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1946                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
1947                                             ((kmi->type == OSKEY) &&                         event->oskey) )
1948                                         {
1949                                                 t->modifiers |= MOD_SNAP_INVERT;
1950                                         }
1951                                         break;
1952                                 }
1953                         }
1954                 }
1955
1956         }
1957
1958         t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1959
1960         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1961
1962         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1963         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1964         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1965         mode = t->mode;
1966
1967         calculatePropRatio(t);
1968         calculateCenter(t);
1969
1970         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1971
1972         switch (mode) {
1973                 case TFM_TRANSLATION:
1974                         initTranslation(t);
1975                         break;
1976                 case TFM_ROTATION:
1977                         initRotation(t);
1978                         break;
1979                 case TFM_RESIZE:
1980                         initResize(t);
1981                         break;
1982                 case TFM_SKIN_RESIZE:
1983                         initSkinResize(t);
1984                         break;
1985                 case TFM_TOSPHERE:
1986                         initToSphere(t);
1987                         break;
1988                 case TFM_SHEAR:
1989                         initShear(t);
1990                         break;
1991                 case TFM_WARP:
1992                         initWarp(t);
1993                         break;
1994                 case TFM_SHRINKFATTEN:
1995                         initShrinkFatten(t);
1996                         break;
1997                 case TFM_TILT:
1998                         initTilt(t);
1999                         break;
2000                 case TFM_CURVE_SHRINKFATTEN:
2001                         initCurveShrinkFatten(t);
2002                         break;
2003                 case TFM_MASK_SHRINKFATTEN:
2004                         initMaskShrinkFatten(t);
2005                         break;
2006                 case TFM_TRACKBALL:
2007                         initTrackball(t);
2008                         break;
2009                 case TFM_PUSHPULL:
2010                         initPushPull(t);
2011                         break;
2012                 case TFM_CREASE:
2013                         initCrease(t);
2014                         break;
2015                 case TFM_BONESIZE:
2016                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2017                         bArmature *arm = t->poseobj->data;
2018                         if (arm->drawtype == ARM_ENVELOPE)
2019                                 initBoneEnvelope(t);
2020                         else
2021                                 initBoneSize(t);
2022                 }
2023                 break;
2024                 case TFM_BONE_ENVELOPE:
2025                         initBoneEnvelope(t);
2026                         break;
2027                 case TFM_EDGE_SLIDE:
2028                         initEdgeSlide(t);
2029                         break;
2030                 case TFM_VERT_SLIDE:
2031                         initVertSlide(t);
2032                         break;
2033                 case TFM_BONE_ROLL:
2034                         initBoneRoll(t);
2035                         break;
2036                 case TFM_TIME_TRANSLATE:
2037                         initTimeTranslate(t);
2038                         break;
2039                 case TFM_TIME_SLIDE:
2040                         initTimeSlide(t);
2041                         break;
2042                 case TFM_TIME_SCALE:
2043                         initTimeScale(t);
2044                         break;
2045                 case TFM_TIME_DUPLICATE:
2046                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2047                          * so that duplicate-culling will work properly
2048                          */
2049                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2050                                 initTranslation(t);
2051                         else
2052                                 initTimeTranslate(t);
2053                         t->mode = mode;
2054                         break;
2055                 case TFM_TIME_EXTEND:
2056                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2057                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2058                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2059                          * depending on which editor this was called from
2060                          */
2061                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2062                                 initTranslation(t);
2063                         else
2064                                 initTimeTranslate(t);
2065                         break;
2066                 case TFM_BAKE_TIME:
2067                         initBakeTime(t);
2068                         break;
2069                 case TFM_MIRROR:
2070                         initMirror(t);
2071                         break;
2072                 case TFM_BEVEL:
2073                         initBevel(t);
2074                         break;
2075                 case TFM_BWEIGHT:
2076                         initBevelWeight(t);
2077                         break;
2078                 case TFM_ALIGN:
2079                         initAlign(t);
2080                         break;
2081                 case TFM_SEQ_SLIDE:
2082                         initSeqSlide(t);
2083                         break;
2084         }
2085
2086         if (t->state == TRANS_CANCEL) {
2087                 postTrans(C, t);
2088                 return 0;
2089         }
2090
2091
2092         /* overwrite initial values if operator supplied a non-null vector */
2093         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2094                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2095
2096                 if (RNA_property_array_check(prop)) {
2097                         RNA_float_get_array(op->ptr, "value", values);
2098                 }
2099                 else {
2100                         values[0] = RNA_float_get(op->ptr, "value");
2101                 }
2102
2103                 copy_v4_v4(t->values, values);
2104                 copy_v4_v4(t->auto_values, values);
2105                 t->flag |= T_AUTOVALUES;
2106         }
2107
2108         /* Transformation axis from operator */
2109         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2110                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2111                 normalize_v3(t->axis);
2112                 copy_v3_v3(t->axis_orig, t->axis);
2113         }
2114
2115         /* Constraint init from operator */
2116         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2117                 int constraint_axis[3];
2118
2119                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2120
2121                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2122                         t->con.mode |= CON_APPLY;
2123
2124                         if (constraint_axis[0]) {
2125                                 t->con.mode |= CON_AXIS0;
2126                         }
2127                         if (constraint_axis[1]) {
2128                                 t->con.mode |= CON_AXIS1;
2129                         }
2130                         if (constraint_axis[2]) {
2131                                 t->con.mode |= CON_AXIS2;
2132                         }
2133
2134                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2135                 }
2136         }
2137
2138         t->context = NULL;
2139
2140         return 1;
2141 }
2142
2143 void transformApply(bContext *C, TransInfo *t)
2144 {
2145         t->context = C;
2146
2147         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2148                 selectConstraint(t);
2149                 if (t->transform) {
2150                         t->transform(t, t->mval);  // calls recalcData()
2151                         viewRedrawForce(C, t);
2152                 }
2153                 t->redraw = TREDRAW_NOTHING;
2154         }
2155         else if (t->redraw & TREDRAW_SOFT) {
2156                 viewRedrawForce(C, t);
2157         }
2158
2159         /* If auto confirm is on, break after one pass */
2160         if (t->options & CTX_AUTOCONFIRM) {
2161                 t->state = TRANS_CONFIRM;
2162         }
2163
2164         t->context = NULL;
2165 }
2166
2167 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2168 {
2169         TransInfo *t = arg;
2170
2171         if (t->redraw & TREDRAW_SOFT) {
2172                 t->redraw |= TREDRAW_HARD;
2173                 transformApply((bContext *)C, t);
2174         }
2175 }
2176
2177 int transformEnd(bContext *C, TransInfo *t)
2178 {
2179         int exit_code = OPERATOR_RUNNING_MODAL;
2180
2181         t->context = C;
2182
2183         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2184                 /* handle restoring objects */
2185                 if (t->state == TRANS_CANCEL) {
2186                         /* exception, edge slide transformed UVs too */
2187                         if (t->mode == TFM_EDGE_SLIDE)
2188                                 doEdgeSlide(t, 0.0f);
2189                         
2190                         exit_code = OPERATOR_CANCELLED;
2191                         restoreTransObjects(t); // calls recalcData()
2192                 }
2193                 else {
2194                         exit_code = OPERATOR_FINISHED;
2195                 }
2196
2197                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
2198                 special_aftertrans_update(C, t);
2199
2200                 /* free data */
2201                 postTrans(C, t);
2202
2203                 /* send events out for redraws */
2204                 viewRedrawPost(C, t);
2205
2206                 /*  Undo as last, certainly after special_trans_update! */
2207
2208                 if (t->state == TRANS_CANCEL) {
2209 //                      if (t->undostr) ED_undo_push(C, t->undostr);
2210                 }
2211                 else {
2212 //                      if (t->undostr) ED_undo_push(C, t->undostr);
2213 //                      else ED_undo_push(C, transform_to_undostr(t));
2214                 }
2215                 t->undostr = NULL;
2216
2217                 viewRedrawForce(C, t);
2218         }
2219
2220         t->context = NULL;
2221
2222         return exit_code;
2223 }
2224
2225 /* ************************** TRANSFORM LOCKS **************************** */
2226
2227 static void protectedTransBits(short protectflag, float *vec)
2228 {
2229         if (protectflag & OB_LOCK_LOCX)
2230                 vec[0] = 0.0f;
2231         if (protectflag & OB_LOCK_LOCY)
2232                 vec[1] = 0.0f;
2233         if (protectflag & OB_LOCK_LOCZ)
2234                 vec[2] = 0.0f;
2235 }
2236
2237 static void protectedSizeBits(short protectflag, float *size)
2238 {
2239         if (protectflag & OB_LOCK_SCALEX)
2240                 size[0] = 1.0f;
2241         if (protectflag & OB_LOCK_SCALEY)
2242                 size[1] = 1.0f;
2243         if (protectflag & OB_LOCK_SCALEZ)
2244                 size[2] = 1.0f;
2245 }
2246
2247 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
2248 {
2249         if (protectflag & OB_LOCK_ROTX)
2250                 eul[0] = oldeul[0];
2251         if (protectflag & OB_LOCK_ROTY)
2252                 eul[1] = oldeul[1];
2253         if (protectflag & OB_LOCK_ROTZ)
2254                 eul[2] = oldeul[2];
2255 }
2256
2257
2258 /* this function only does the delta rotation */
2259 /* axis-angle is usually internally stored as quats... */
2260 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2261 {
2262         /* check that protection flags are set */
2263         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2264                 return;
2265         
2266         if (protectflag & OB_LOCK_ROT4D) {
2267                 /* axis-angle getting limited as 4D entities that they are... */
2268                 if (protectflag & OB_LOCK_ROTW)
2269                         *angle = oldAngle;
2270                 if (protectflag & OB_LOCK_ROTX)
2271                         axis[0] = oldAxis[0];
2272                 if (protectflag & OB_LOCK_ROTY)
2273                         axis[1] = oldAxis[1];
2274                 if (protectflag & OB_LOCK_ROTZ)
2275                         axis[2] = oldAxis[2];
2276         }
2277         else {
2278                 /* axis-angle get limited with euler... */
2279                 float eul[3], oldeul[3];
2280                 
2281                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2282                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2283                 
2284                 if (protectflag & OB_LOCK_ROTX)
2285                         eul[0] = oldeul[0];
2286                 if (protectflag & OB_LOCK_ROTY)
2287                         eul[1] = oldeul[1];
2288                 if (protectflag & OB_LOCK_ROTZ)
2289                         eul[2] = oldeul[2];
2290                 
2291                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2292                 
2293                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2294                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2295                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2296                         axis[1] = 1.0f;
2297                 }
2298         }
2299 }
2300
2301 /* this function only does the delta rotation */
2302 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
2303 {
2304         /* check that protection flags are set */
2305         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2306                 return;
2307         
2308         if (protectflag & OB_LOCK_ROT4D) {
2309                 /* quaternions getting limited as 4D entities that they are... */
2310                 if (protectflag & OB_LOCK_ROTW)
2311                         quat[0] = oldquat[0];
2312                 if (protectflag & OB_LOCK_ROTX)
2313                         quat[1] = oldquat[1];
2314                 if (protectflag & OB_LOCK_ROTY)
2315                         quat[2] = oldquat[2];
2316                 if (protectflag & OB_LOCK_ROTZ)
2317                         quat[3] = oldquat[3];
2318         }
2319         else {
2320                 /* quaternions get limited with euler... (compatibility mode) */
2321                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2322                 float qlen;
2323
2324                 qlen = normalize_qt_qt(nquat, quat);
2325                 normalize_qt_qt(noldquat, oldquat);
2326
2327                 quat_to_eul(eul, nquat);
2328                 quat_to_eul(oldeul, noldquat);
2329
2330                 if (protectflag & OB_LOCK_ROTX)
2331                         eul[0] = oldeul[0];
2332                 if (protectflag & OB_LOCK_ROTY)
2333                         eul[1] = oldeul[1];
2334                 if (protectflag & OB_LOCK_ROTZ)
2335                         eul[2] = oldeul[2];
2336
2337                 eul_to_quat(quat, eul);
2338
2339                 /* restore original quat size */
2340                 mul_qt_fl(quat, qlen);
2341                 
2342                 /* quaternions flip w sign to accumulate rotations correctly */
2343                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2344                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2345                 {
2346                         mul_qt_fl(quat, -1.0f);
2347                 }
2348         }
2349 }
2350
2351 /* ******************* TRANSFORM LIMITS ********************** */
2352
2353 static void constraintTransLim(TransInfo *t, TransData *td)
2354 {
2355         if (td->con) {
2356                 bConstraintTypeInfo *ctiLoc = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
2357                 bConstraintTypeInfo *ctiDist = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
2358                 
2359                 bConstraintOb cob = {NULL};
2360                 bConstraint *con;
2361                 float ctime = (float)(t->scene->r.cfra);
2362                 
2363                 /* Make a temporary bConstraintOb for using these limit constraints
2364                  *  - they only care that cob->matrix is correctly set ;-)
2365                  *      - current space should be local
2366                  */
2367                 unit_m4(cob.matrix);
2368                 copy_v3_v3(cob.matrix[3], td->loc);
2369                 
2370                 /* Evaluate valid constraints */
2371                 for (con = td->con; con; con = con->next) {
2372                         bConstraintTypeInfo *cti = NULL;
2373                         ListBase targets = {NULL, NULL};
2374                         
2375                         /* only consider constraint if enabled */
2376                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2377                         if (con->enforce == 0.0f) continue;
2378                         
2379                         /* only use it if it's tagged for this purpose (and the right type) */
2380                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2381                                 bLocLimitConstraint *data = con->data;
2382                                 
2383                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2384                                         continue;
2385                                 cti = ctiLoc;
2386                         }
2387                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2388                                 bDistLimitConstraint *data = con->data;
2389                                 
2390                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2391                                         continue;
2392                                 cti = ctiDist;
2393                         }
2394                         
2395                         if (cti) {
2396                                 /* do space conversions */
2397                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2398                                         /* just multiply by td->mtx (this should be ok) */
2399                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2400                                 }
2401                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2402                                         /* skip... incompatable spacetype */
2403                                         continue;
2404                                 }
2405                                 
2406                                 /* get constraint targets if needed */
2407                                 BKE_get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2408                                 
2409                                 /* do constraint */
2410                                 cti->evaluate_constraint(con, &cob, &targets);
2411                                 
2412                                 /* convert spaces again */
2413                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2414                                         /* just multiply by td->smtx (this should be ok) */
2415                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2416                                 }
2417                                 
2418                                 /* free targets list */
2419                                 BLI_freelistN(&targets);
2420                         }
2421                 }
2422                 
2423                 /* copy results from cob->matrix */
2424                 copy_v3_v3(td->loc, cob.matrix[3]);
2425         }
2426 }
2427
2428 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2429 {
2430         /* Make a temporary bConstraintOb for use by limit constraints
2431          *  - they only care that cob->matrix is correctly set ;-)
2432          *      - current space should be local
2433          */
2434         memset(cob, 0, sizeof(bConstraintOb));
2435         if (td->ext) {
2436                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2437                         /* quats */
2438                         /* objects and bones do normalization first too, otherwise
2439                          * we don't necessarily end up with a rotation matrix, and
2440                          * then conversion back to quat gives a different result */
2441                         float quat[4];
2442                         normalize_qt_qt(quat, td->ext->quat);
2443                         quat_to_mat4(cob->matrix, quat);
2444                 }
2445                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2446                         /* axis angle */
2447                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2448                 }
2449                 else {
2450                         /* eulers */
2451                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2452                 }
2453         }
2454 }
2455
2456 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2457 {
2458         if (td->con) {
2459                 bConstraintTypeInfo *cti = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2460                 bConstraintOb cob;
2461                 bConstraint *con;
2462                 int do_limit = FALSE;
2463
2464                 /* Evaluate valid constraints */
2465                 for (con = td->con; con; con = con->next) {
2466                         /* only consider constraint if enabled */
2467                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2468                         if (con->enforce == 0.0f) continue;
2469
2470                         /* we're only interested in Limit-Rotation constraints */
2471                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2472                                 bRotLimitConstraint *data = con->data;
2473
2474                                 /* only use it if it's tagged for this purpose */
2475                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2476                                         continue;
2477
2478                                 /* skip incompatable spacetypes */
2479                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2480                                         continue;
2481
2482                                 /* only do conversion if necessary, to preserve quats and eulers */
2483                                 if (do_limit == FALSE) {
2484                                         constraintob_from_transdata(&cob, td);
2485                                         do_limit = TRUE;
2486                                 }
2487
2488                                 /* do space conversions */
2489                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2490                                         /* just multiply by td->mtx (this should be ok) */
2491                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2492                                 }
2493                                 
2494                                 /* do constraint */
2495                                 cti->evaluate_constraint(con, &cob, NULL);
2496                                 
2497                                 /* convert spaces again */
2498                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2499                                         /* just multiply by td->smtx (this should be ok) */
2500                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2501                                 }
2502                         }
2503                 }
2504                 
2505                 if (do_limit) {
2506                         /* copy results from cob->matrix */
2507                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2508                                 /* quats */
2509                                 mat4_to_quat(td->ext->quat, cob.matrix);
2510                         }
2511                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2512                                 /* axis angle */
2513                                 mat4_to_axis_angle(&td->ext->quat[1], &td->ext->quat[0], cob.matrix);
2514                         }
2515                         else {
2516                                 /* eulers */
2517                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2518                         }
2519                 }
2520         }
2521 }
2522
2523 static void constraintSizeLim(TransInfo *t, TransData *td)
2524 {
2525         if (td->con && td->ext) {
2526                 bConstraintTypeInfo *cti = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2527                 bConstraintOb cob = {NULL};
2528                 bConstraint *con;
2529                 float size_sign[3], size_abs[3];
2530                 int i;
2531                 
2532                 /* Make a temporary bConstraintOb for using these limit constraints
2533                  *  - they only care that cob->matrix is correctly set ;-)
2534                  *      - current space should be local
2535                  */
2536                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2537                         /* scale val and reset size */
2538                         return; // TODO: fix this case
2539                 }
2540                 else {
2541                         /* Reset val if SINGLESIZE but using a constraint */
2542                         if (td->flag & TD_SINGLESIZE)
2543                                 return;
2544
2545                         /* separate out sign to apply back later */
2546                         for (i = 0; i < 3; i++) {
2547                                 size_sign[i] = signf(td->ext->size[i]);
2548                                 size_abs[i] = fabsf(td->ext->size[i]);
2549                         }
2550                         
2551                         size_to_mat4(cob.matrix, size_abs);
2552                 }
2553                 
2554                 /* Evaluate valid constraints */
2555                 for (con = td->con; con; con = con->next) {
2556                         /* only consider constraint if enabled */
2557                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2558                         if (con->enforce == 0.0f) continue;
2559                         
2560                         /* we're only interested in Limit-Scale constraints */
2561                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2562                                 bSizeLimitConstraint *data = con->data;
2563                                 
2564                                 /* only use it if it's tagged for this purpose */
2565                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2566                                         continue;
2567                                 
2568                                 /* do space conversions */
2569                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2570                                         /* just multiply by td->mtx (this should be ok) */
2571                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2572                                 }
2573                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2574                                         /* skip... incompatible spacetype */
2575                                         continue;
2576                                 }
2577                                 
2578                                 /* do constraint */
2579                                 cti->evaluate_constraint(con, &cob, NULL);
2580                                 
2581                                 /* convert spaces again */
2582                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2583                                         /* just multiply by td->smtx (this should be ok) */
2584                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2585                                 }
2586                         }
2587                 }
2588
2589                 /* copy results from cob->matrix */
2590                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2591                         /* scale val and reset size */
2592                         return; // TODO: fix this case
2593                 }
2594                 else {
2595                         /* Reset val if SINGLESIZE but using a constraint */
2596                         if (td->flag & TD_SINGLESIZE)
2597                                 return;
2598
2599                         /* extrace scale from matrix and apply back sign */
2600                         mat4_to_size(td->ext->size, cob.matrix);
2601                         mul_v3_v3(td->ext->size, size_sign);
2602                 }
2603         }
2604 }
2605
2606 /* ************************** WARP *************************** */
2607
2608 static void postInputWarp(TransInfo *t, float values[3])
2609 {
2610         mul_v3_fl(values, (float)(M_PI * 2));
2611
2612         if (t->customData) { /* non-null value indicates reversed input */
2613                 negate_v3(values);
2614         }
2615 }
2616
2617 void initWarp(TransInfo *t)
2618 {
2619         float max[3], min[3];
2620         int i;
2621         
2622         t->mode = TFM_WARP;
2623         t->transform = Warp;
2624         t->handleEvent = handleEventWarp;
2625         
2626         setInputPostFct(&t->mouse, postInputWarp);
2627         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2628         
2629         t->idx_max = 0;
2630         t->num.idx_max = 0;
2631         t->snap[0] = 0.0f;
2632         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2633         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2634         
2635         t->num.increment = 1.0f;
2636
2637         t->flag |= T_NO_CONSTRAINT;
2638         
2639         /* we need min/max in view space */
2640         for (i = 0; i < t->total; i++) {
2641                 float center[3];
2642                 copy_v3_v3(center, t->data[i].center);
2643                 mul_m3_v3(t->data[i].mtx, center);
2644                 mul_m4_v3(t->viewmat, center);
2645                 sub_v3_v3(center, t->viewmat[3]);
2646                 if (i) {
2647                         minmax_v3v3_v3(min, max, center);
2648                 }
2649                 else {
2650                         copy_v3_v3(max, center);
2651                         copy_v3_v3(min, center);
2652                 }
2653         }
2654
2655         mid_v3_v3v3(t->center, min, max);
2656
2657         if (max[0] == min[0]) max[0] += 0.1f;  /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2658         t->val = (max[0] - min[0]) / 2.0f; /* t->val is X dimension projected boundbox */
2659 }
2660
2661 int handleEventWarp(TransInfo *t, wmEvent *event)
2662 {
2663         int status = 0;
2664         
2665         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2666                 // Use customData pointer to signal warp direction
2667                 if (t->customData == NULL)
2668                         t->customData = (void *)1;
2669                 else
2670                         t->customData = NULL;
2671                 
2672                 status = 1;
2673         }
2674         
2675         return status;
2676 }
2677
2678 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2679 {
2680         TransData *td = t->data;
2681         float vec[3], circumfac, dist, phi0, co, si, cursor[3], gcursor[3];
2682         const float *curs;
2683         int i;
2684         char str[50];
2685         
2686         curs = give_cursor(t->scene, t->view);
2687         /*
2688          * gcursor is the one used for helpline.
2689          * It has to be in the same space as the drawing loop
2690          * (that means it needs to be in the object's space when in edit mode and
2691          *  in global space in object mode)
2692          *
2693          * cursor is used for calculations.
2694          * It needs to be in view space, but we need to take object's offset
2695          * into account if in Edit mode.
2696          */
2697         copy_v3_v3(cursor, curs);
2698         copy_v3_v3(gcursor, cursor);
2699         if (t->flag & T_EDIT) {
2700                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2701                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2702                 mul_m3_v3(t->data->smtx, gcursor);
2703         }
2704         mul_m4_v3(t->viewmat, cursor);
2705         sub_v3_v3(cursor, t->viewmat[3]);
2706         
2707         /* amount of radians for warp */
2708         circumfac = t->values[0];
2709         
2710         snapGrid(t, &circumfac);
2711         applyNumInput(&t->num, &circumfac);
2712         
2713         /* header print for NumInput */
2714         if (hasNumInput(&t->num)) {
2715                 char c[NUM_STR_REP_LEN];
2716                 
2717                 outputNumInput(&(t->num), c);
2718                 
2719                 sprintf(str, IFACE_("Warp: %s"), c);
2720
2721                 circumfac = DEG2RADF(circumfac);
2722         }
2723         else {
2724                 /* default header print */
2725                 sprintf(str, IFACE_("Warp: %.3f"), RAD2DEGF(circumfac));
2726         }
2727         
2728         t->values[0] = circumfac;
2729
2730         circumfac /= 2; /* only need 180 on each side to make 360 */
2731         
2732         for (i = 0; i < t->total; i++, td++) {
2733                 float loc[3];
2734                 if (td->flag & TD_NOACTION)
2735                         break;
2736                 
2737                 if (td->flag & TD_SKIP)
2738                         continue;
2739                 
2740                 /* translate point to center, rotate in such a way that outline==distance */
2741                 copy_v3_v3(vec, td->iloc);
2742                 mul_m3_v3(td->mtx, vec);
2743                 mul_m4_v3(t->viewmat, vec);
2744                 sub_v3_v3(vec, t->viewmat[3]);
2745                 
2746                 dist = vec[0] - cursor[0];
2747                 
2748                 /* t->val is X dimension projected boundbox */
2749                 phi0 = (circumfac * dist / t->val);
2750                 
2751                 vec[1] = (vec[1] - cursor[1]);
2752                 
2753                 co = (float)cos(phi0);
2754                 si = (float)sin(phi0);
2755                 loc[0] = -si * vec[1] + cursor[0];
2756                 loc[1] = co * vec[1] + cursor[1];
2757                 loc[2] = vec[2];
2758                 
2759                 mul_m4_v3(t->viewinv, loc);
2760                 sub_v3_v3(loc, t->viewinv[3]);
2761                 mul_m3_v3(td->smtx, loc);
2762                 
2763                 sub_v3_v3(loc, td->iloc);
2764                 mul_v3_fl(loc, td->factor);
2765                 add_v3_v3v3(td->loc, td->iloc, loc);
2766         }
2767         
2768         recalcData(t);
2769         
2770         ED_area_headerprint(t->sa, str);
2771         
2772         return 1;
2773 }
2774
2775 /* ************************** SHEAR *************************** */
2776
2777 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2778 {
2779         mul_v3_fl(values, 0.05f);
2780 }
2781
2782 void initShear(TransInfo *t)
2783 {
2784         t->mode = TFM_SHEAR;
2785         t->transform = Shear;
2786         t->handleEvent = handleEventShear;
2787         
2788         setInputPostFct(&t->mouse, postInputShear);
2789         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2790         
2791         t->idx_max = 0;
2792         t->num.idx_max = 0;
2793         t->snap[0] = 0.0f;
2794         t->snap[1] = 0.1f;
2795         t->snap[2] = t->snap[1] * 0.1f;
2796         
2797         t->num.increment = 0.1f;
2798
2799         t->flag |= T_NO_CONSTRAINT;
2800 }
2801
2802 int handleEventShear(TransInfo *t, wmEvent *event)
2803 {
2804         int status = 0;
2805         
2806         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2807                 // Use customData pointer to signal Shear direction
2808                 if (t->customData == NULL) {
2809                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2810                         t->customData = (void *)1;
2811                 }
2812                 else {
2813                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2814                         t->customData = NULL;
2815                 }
2816
2817                 status = 1;
2818         }
2819         else if (event->type == XKEY && event->val == KM_PRESS) {
2820                 initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2821                 t->customData = NULL;
2822                 
2823                 status = 1;
2824         }
2825         else if (event->type == YKEY && event->val == KM_PRESS) {
2826                 initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2827                 t->customData = (void *)1;
2828                 
2829                 status = 1;
2830         }
2831         
2832         return status;
2833 }
2834
2835
2836 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2837 {
2838         TransData *td = t->data;
2839         float vec[3];
2840         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2841         float value;
2842         int i;
2843         char str[50];
2844         
2845         copy_m3_m4(persmat, t->viewmat);
2846         invert_m3_m3(persinv, persmat);
2847         
2848         value = t->values[0];
2849         
2850         snapGrid(t, &value);
2851         
2852         applyNumInput(&t->num, &value);
2853         
2854         /* header print for NumInput */
2855         if (hasNumInput(&t->num)) {
2856                 char c[NUM_STR_REP_LEN];
2857                 
2858                 outputNumInput(&(t->num), c);
2859                 
2860                 sprintf(str, IFACE_("Shear: %s %s"), c, t->proptext);
2861         }
2862         else {
2863                 /* default header print */
2864                 sprintf(str, IFACE_("Shear: %.3f %s (Press X or Y to set shear axis)"), value, t->proptext);
2865         }
2866         
2867         t->values[0] = value;
2868
2869         unit_m3(smat);
2870         
2871         // Custom data signals shear direction
2872         if (t->customData == NULL)
2873                 smat[1][0] = value;
2874         else
2875                 smat[0][1] = value;
2876         
2877         mul_m3_m3m3(tmat, smat, persmat);
2878         mul_m3_m3m3(totmat, persinv, tmat);
2879         
2880         for (i = 0; i < t->total; i++, td++) {
2881                 if (td->flag & TD_NOACTION)
2882                         break;
2883             &