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