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