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