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