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