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