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