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