Merging r40265 through r40492 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         if ((prop= RNA_struct_find_property(op->ptr, "value"))) {
1395                 float *values= (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1396                 if (RNA_property_array_check(prop)) {
1397                         RNA_property_float_set_array(op->ptr, prop, values);
1398                 }
1399                 else {
1400                         RNA_property_float_set(op->ptr, prop, values[0]);
1401                 }
1402         }
1403
1404         /* convert flag to enum */
1405         switch(t->flag & (T_PROP_EDIT|T_PROP_CONNECTED))
1406         {
1407         case (T_PROP_EDIT|T_PROP_CONNECTED):
1408                 proportional = PROP_EDIT_CONNECTED;
1409                 break;
1410         case T_PROP_EDIT:
1411                 proportional = PROP_EDIT_ON;
1412                 break;
1413         default:
1414                 proportional = PROP_EDIT_OFF;
1415         }
1416
1417         // If modal, save settings back in scene if not set as operator argument
1418         if (t->flag & T_MODAL) {
1419
1420                 /* save settings if not set in operator */
1421                 if (RNA_struct_find_property(op->ptr, "proportional") && !RNA_property_is_set(op->ptr, "proportional")) {
1422                         if (t->obedit)
1423                                 ts->proportional = proportional;
1424                         else
1425                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1426                 }
1427
1428                 if (RNA_struct_find_property(op->ptr, "proportional_size") && !RNA_property_is_set(op->ptr, "proportional_size")) {
1429                         ts->proportional_size = t->prop_size;
1430                 }
1431                         
1432                 if (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") && !RNA_property_is_set(op->ptr, "proportional_edit_falloff")) {
1433                         ts->prop_mode = t->prop_mode;
1434                 }
1435                 
1436                 /* do we check for parameter? */
1437                 if (t->modifiers & MOD_SNAP) {
1438                         ts->snap_flag |= SCE_SNAP;
1439                 } else {
1440                         ts->snap_flag &= ~SCE_SNAP;
1441                 }
1442
1443                 if(t->spacetype == SPACE_VIEW3D) {
1444                         if (RNA_struct_find_property(op->ptr, "constraint_orientation") && !RNA_property_is_set(op->ptr, "constraint_orientation")) {
1445                                 View3D *v3d = t->view;
1446         
1447                                 v3d->twmode = t->current_orientation;
1448                         }
1449                 }
1450         }
1451         
1452         if (RNA_struct_find_property(op->ptr, "proportional"))
1453         {
1454                 RNA_enum_set(op->ptr, "proportional", proportional);
1455                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1456                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1457         }
1458
1459         if (RNA_struct_find_property(op->ptr, "axis"))
1460         {
1461                 RNA_float_set_array(op->ptr, "axis", t->axis);
1462         }
1463
1464         if (RNA_struct_find_property(op->ptr, "mirror"))
1465         {
1466                 RNA_boolean_set(op->ptr, "mirror", t->flag & T_MIRROR);
1467         }
1468
1469         if (RNA_struct_find_property(op->ptr, "constraint_axis"))
1470         {
1471                 /* constraint orientation can be global, event if user selects something else
1472                  * so use the orientation in the constraint if set
1473                  * */
1474                 if (t->con.mode & CON_APPLY) {
1475                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1476                 } else {
1477                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1478                 }
1479
1480                 if (t->con.mode & CON_APPLY)
1481                 {
1482                         if (t->con.mode & CON_AXIS0) {
1483                                 constraint_axis[0] = 1;
1484                         }
1485                         if (t->con.mode & CON_AXIS1) {
1486                                 constraint_axis[1] = 1;
1487                         }
1488                         if (t->con.mode & CON_AXIS2) {
1489                                 constraint_axis[2] = 1;
1490                         }
1491                 }
1492
1493                 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1494         }
1495 }
1496
1497 /* note: caller needs to free 't' on a 0 return */
1498 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1499 {
1500         int options = 0;
1501
1502         t->context = C;
1503
1504         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1505
1506         t->state = TRANS_STARTING;
1507
1508         if(RNA_struct_find_property(op->ptr, "texture_space"))
1509                 if(RNA_boolean_get(op->ptr, "texture_space"))
1510                         options |= CTX_TEXTURE;
1511         
1512         t->options = options;
1513
1514         t->mode = mode;
1515
1516         t->launch_event = event ? event->type : -1;
1517
1518         if (t->launch_event == EVT_TWEAK_R)
1519         {
1520                 t->launch_event = RIGHTMOUSE;
1521         }
1522         else if (t->launch_event == EVT_TWEAK_L)
1523         {
1524                 t->launch_event = LEFTMOUSE;
1525         }
1526
1527         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1528         // For manipulator only, so assume LEFTMOUSE
1529         if (t->launch_event == 0)
1530         {
1531                 t->launch_event = LEFTMOUSE;
1532         }
1533
1534         if (!initTransInfo(C, t, op, event))                                    // internal data, mouse, vectors
1535         {
1536                 return 0;
1537         }
1538
1539         if(t->spacetype == SPACE_VIEW3D)
1540         {
1541                 //calc_manipulator_stats(curarea);
1542                 initTransformOrientation(C, t);
1543
1544                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1545                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1546                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1547                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1548         }
1549         else if(t->spacetype == SPACE_IMAGE) {
1550                 unit_m3(t->spacemtx);
1551                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1552                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1553         }
1554         else if(t->spacetype == SPACE_CLIP) {
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->options |= CTX_MOVIECLIP;
1558         }
1559         else
1560                 unit_m3(t->spacemtx);
1561
1562         createTransData(C, t);                  // make TransData structs from selection
1563
1564         if (t->total == 0) {
1565                 postTrans(C, t);
1566                 return 0;
1567         }
1568
1569         /* Stupid code to have Ctrl-Click on manipulator work ok */
1570         if(event)
1571         {
1572                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1573                 wmKeyMapItem *kmi;
1574
1575                 for (kmi = keymap->items.first; kmi; kmi = kmi->next)
1576                 {
1577                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
1578                         {
1579                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
1580                                         (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1581                                         (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
1582                                         (kmi->type == OSKEY && event->oskey)) {
1583                                         t->modifiers |= MOD_SNAP_INVERT;
1584                                 }
1585                                 break;
1586                         }
1587                 }
1588
1589         }
1590
1591         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1592
1593         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1594         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1595         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1596         mode = t->mode;
1597
1598         calculatePropRatio(t);
1599         calculateCenter(t);
1600
1601         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1602
1603         switch (mode) {
1604         case TFM_TRANSLATION:
1605                 initTranslation(t);
1606                 break;
1607         case TFM_ROTATION:
1608                 initRotation(t);
1609                 break;
1610         case TFM_RESIZE:
1611                 initResize(t);
1612                 break;
1613         case TFM_TOSPHERE:
1614                 initToSphere(t);
1615                 break;
1616         case TFM_SHEAR:
1617                 initShear(t);
1618                 break;
1619         case TFM_WARP:
1620                 initWarp(t);
1621                 break;
1622         case TFM_SHRINKFATTEN:
1623                 initShrinkFatten(t);
1624                 break;
1625         case TFM_TILT:
1626                 initTilt(t);
1627                 break;
1628         case TFM_CURVE_SHRINKFATTEN:
1629                 initCurveShrinkFatten(t);
1630                 break;
1631         case TFM_TRACKBALL:
1632                 initTrackball(t);
1633                 break;
1634         case TFM_PUSHPULL:
1635                 initPushPull(t);
1636                 break;
1637         case TFM_CREASE:
1638                 initCrease(t);
1639                 break;
1640         case TFM_BONESIZE:
1641                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1642                         bArmature *arm= t->poseobj->data;
1643                         if(arm->drawtype==ARM_ENVELOPE)
1644                                 initBoneEnvelope(t);
1645                         else
1646                                 initBoneSize(t);
1647                 }
1648                 break;
1649         case TFM_BONE_ENVELOPE:
1650                 initBoneEnvelope(t);
1651                 break;
1652         case TFM_EDGE_SLIDE:
1653                 initEdgeSlide(t);
1654                 break;
1655         case TFM_BONE_ROLL:
1656                 initBoneRoll(t);
1657                 break;
1658         case TFM_TIME_TRANSLATE:
1659                 initTimeTranslate(t);
1660                 break;
1661         case TFM_TIME_SLIDE:
1662                 initTimeSlide(t);
1663                 break;
1664         case TFM_TIME_SCALE:
1665                 initTimeScale(t);
1666                 break;
1667         case TFM_TIME_DUPLICATE:
1668                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1669                  * so that duplicate-culling will work properly
1670                  */
1671                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1672                         initTranslation(t);
1673                 else
1674                         initTimeTranslate(t);
1675                 t->mode = mode;
1676                 break;
1677         case TFM_TIME_EXTEND:
1678                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1679                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1680                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1681                  * depending on which editor this was called from
1682                  */
1683                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1684                         initTranslation(t);
1685                 else
1686                         initTimeTranslate(t);
1687                 break;
1688         case TFM_BAKE_TIME:
1689                 initBakeTime(t);
1690                 break;
1691         case TFM_MIRROR:
1692                 initMirror(t);
1693                 break;
1694         case TFM_BEVEL:
1695                 initBevel(t);
1696                 break;
1697         case TFM_BWEIGHT:
1698                 initBevelWeight(t);
1699                 break;
1700         case TFM_ALIGN:
1701                 initAlign(t);
1702                 break;
1703         case TFM_SEQ_SLIDE:
1704                 initSeqSlide(t);
1705                 break;
1706         }
1707
1708         if(t->state == TRANS_CANCEL)
1709         {
1710                 postTrans(C, t);
1711                 return 0;
1712         }
1713
1714
1715         /* overwrite initial values if operator supplied a non-null vector */
1716         if (RNA_property_is_set(op->ptr, "value"))
1717         {
1718                 float values[4]= {0}; /* incase value isn't length 4, avoid uninitialized memory  */
1719                 PropertyRNA *prop= RNA_struct_find_property(op->ptr, "value");
1720
1721                 if(RNA_property_array_check(prop)) {
1722                         RNA_float_get_array(op->ptr, "value", values);
1723                 } else {
1724                         values[0]= RNA_float_get(op->ptr, "value");
1725                 }
1726
1727                 QUATCOPY(t->values, values);
1728                 QUATCOPY(t->auto_values, values);
1729                 t->flag |= T_AUTOVALUES;
1730         }
1731
1732         /* Transformation axis from operator */
1733         if (RNA_struct_find_property(op->ptr, "axis") && RNA_property_is_set(op->ptr, "axis"))
1734         {
1735                 RNA_float_get_array(op->ptr, "axis", t->axis);
1736                 normalize_v3(t->axis);
1737                 copy_v3_v3(t->axis_orig, t->axis);
1738         }
1739
1740         /* Constraint init from operator */
1741         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1742         {
1743                 int constraint_axis[3];
1744
1745                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1746
1747                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1748                 {
1749                         t->con.mode |= CON_APPLY;
1750
1751                         if (constraint_axis[0]) {
1752                                 t->con.mode |= CON_AXIS0;
1753                         }
1754                         if (constraint_axis[1]) {
1755                                 t->con.mode |= CON_AXIS1;
1756                         }
1757                         if (constraint_axis[2]) {
1758                                 t->con.mode |= CON_AXIS2;
1759                         }
1760
1761                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1762                 }
1763         }
1764
1765         t->context = NULL;
1766
1767         return 1;
1768 }
1769
1770 void transformApply(bContext *C, TransInfo *t)
1771 {
1772         t->context = C;
1773
1774         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT)))
1775         {
1776                 selectConstraint(t);
1777                 if (t->transform) {
1778                         t->transform(t, t->mval);  // calls recalcData()
1779                         viewRedrawForce(C, t);
1780                 }
1781                 t->redraw = TREDRAW_NOTHING;
1782         } else if (t->redraw & TREDRAW_SOFT) {
1783                 viewRedrawForce(C, t);
1784         }
1785
1786         /* If auto confirm is on, break after one pass */
1787         if (t->options & CTX_AUTOCONFIRM)
1788         {
1789                 t->state = TRANS_CONFIRM;
1790         }
1791
1792         if (BKE_ptcache_get_continue_physics())
1793         {
1794                 // TRANSFORM_FIX_ME
1795                 //do_screenhandlers(G.curscreen);
1796                 t->redraw |= TREDRAW_HARD;
1797         }
1798
1799         t->context = NULL;
1800 }
1801
1802 void drawTransformApply(const bContext *C, struct ARegion *UNUSED(ar), void *arg)
1803 {
1804         TransInfo *t = arg;
1805
1806         if (t->redraw & TREDRAW_SOFT) {
1807                 t->redraw |= TREDRAW_HARD;
1808                 transformApply((bContext *)C, t);
1809         }
1810 }
1811
1812 int transformEnd(bContext *C, TransInfo *t)
1813 {
1814         int exit_code = OPERATOR_RUNNING_MODAL;
1815
1816         t->context = C;
1817
1818         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING)
1819         {
1820                 /* handle restoring objects */
1821                 if(t->state == TRANS_CANCEL)
1822                 {
1823                         /* exception, edge slide transformed UVs too */
1824                         if(t->mode==TFM_EDGE_SLIDE)
1825                                 doEdgeSlide(t, 0.0f);
1826                         
1827                         exit_code = OPERATOR_CANCELLED;
1828                         restoreTransObjects(t); // calls recalcData()
1829                 }
1830                 else
1831                 {
1832                         exit_code = OPERATOR_FINISHED;
1833                 }
1834
1835                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1836                 special_aftertrans_update(C, t);
1837
1838                 /* free data */
1839                 postTrans(C, t);
1840
1841                 /* send events out for redraws */
1842                 viewRedrawPost(C, t);
1843
1844                 /*  Undo as last, certainly after special_trans_update! */
1845
1846                 if(t->state == TRANS_CANCEL) {
1847 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1848                 }
1849                 else {
1850 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1851 //                      else ED_undo_push(C, transform_to_undostr(t));
1852                 }
1853                 t->undostr= NULL;
1854
1855                 viewRedrawForce(C, t);
1856         }
1857
1858         t->context = NULL;
1859
1860         return exit_code;
1861 }
1862
1863 /* ************************** TRANSFORM LOCKS **************************** */
1864
1865 static void protectedTransBits(short protectflag, float *vec)
1866 {
1867         if(protectflag & OB_LOCK_LOCX)
1868                 vec[0]= 0.0f;
1869         if(protectflag & OB_LOCK_LOCY)
1870                 vec[1]= 0.0f;
1871         if(protectflag & OB_LOCK_LOCZ)
1872                 vec[2]= 0.0f;
1873 }
1874
1875 static void protectedSizeBits(short protectflag, float *size)
1876 {
1877         if(protectflag & OB_LOCK_SCALEX)
1878                 size[0]= 1.0f;
1879         if(protectflag & OB_LOCK_SCALEY)
1880                 size[1]= 1.0f;
1881         if(protectflag & OB_LOCK_SCALEZ)
1882                 size[2]= 1.0f;
1883 }
1884
1885 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1886 {
1887         if(protectflag & OB_LOCK_ROTX)
1888                 eul[0]= oldeul[0];
1889         if(protectflag & OB_LOCK_ROTY)
1890                 eul[1]= oldeul[1];
1891         if(protectflag & OB_LOCK_ROTZ)
1892                 eul[2]= oldeul[2];
1893 }
1894
1895
1896 /* this function only does the delta rotation */
1897 /* axis-angle is usually internally stored as quats... */
1898 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1899 {
1900         /* check that protection flags are set */
1901         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1902                 return;
1903         
1904         if (protectflag & OB_LOCK_ROT4D) {
1905                 /* axis-angle getting limited as 4D entities that they are... */
1906                 if (protectflag & OB_LOCK_ROTW)
1907                         *angle= oldAngle;
1908                 if (protectflag & OB_LOCK_ROTX)
1909                         axis[0]= oldAxis[0];
1910                 if (protectflag & OB_LOCK_ROTY)
1911                         axis[1]= oldAxis[1];
1912                 if (protectflag & OB_LOCK_ROTZ)
1913                         axis[2]= oldAxis[2];
1914         }
1915         else {
1916                 /* axis-angle get limited with euler... */
1917                 float eul[3], oldeul[3];
1918                 
1919                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1920                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1921                 
1922                 if (protectflag & OB_LOCK_ROTX)
1923                         eul[0]= oldeul[0];
1924                 if (protectflag & OB_LOCK_ROTY)
1925                         eul[1]= oldeul[1];
1926                 if (protectflag & OB_LOCK_ROTZ)
1927                         eul[2]= oldeul[2];
1928                 
1929                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1930                 
1931                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1932                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1933                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1934                         axis[1]= 1.0f;
1935                 }
1936         }
1937 }
1938
1939 /* this function only does the delta rotation */
1940 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1941 {
1942         /* check that protection flags are set */
1943         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1944                 return;
1945         
1946         if (protectflag & OB_LOCK_ROT4D) {
1947                 /* quaternions getting limited as 4D entities that they are... */
1948                 if (protectflag & OB_LOCK_ROTW)
1949                         quat[0]= oldquat[0];
1950                 if (protectflag & OB_LOCK_ROTX)
1951                         quat[1]= oldquat[1];
1952                 if (protectflag & OB_LOCK_ROTY)
1953                         quat[2]= oldquat[2];
1954                 if (protectflag & OB_LOCK_ROTZ)
1955                         quat[3]= oldquat[3];
1956         }
1957         else {
1958                 /* quaternions get limited with euler... (compatability mode) */
1959                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1960                 float qlen;
1961
1962                 qlen= normalize_qt_qt(nquat, quat);
1963                 normalize_qt_qt(noldquat, oldquat);
1964
1965                 quat_to_eul(eul, nquat);
1966                 quat_to_eul(oldeul, noldquat);
1967
1968                 if (protectflag & OB_LOCK_ROTX)
1969                         eul[0]= oldeul[0];
1970                 if (protectflag & OB_LOCK_ROTY)
1971                         eul[1]= oldeul[1];
1972                 if (protectflag & OB_LOCK_ROTZ)
1973                         eul[2]= oldeul[2];
1974
1975                 eul_to_quat( quat,eul);
1976
1977                 /* restore original quat size */
1978                 mul_qt_fl(quat, qlen);
1979                 
1980                 /* quaternions flip w sign to accumulate rotations correctly */
1981                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
1982                         mul_qt_fl(quat, -1.0f);
1983                 }
1984         }
1985 }
1986
1987 /* ******************* TRANSFORM LIMITS ********************** */
1988
1989 static void constraintTransLim(TransInfo *t, TransData *td)
1990 {
1991         if (td->con) {
1992                 bConstraintTypeInfo *ctiLoc= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
1993                 bConstraintTypeInfo *ctiDist= get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
1994                 
1995                 bConstraintOb cob= {NULL};
1996                 bConstraint *con;
1997                 float ctime = (float)(t->scene->r.cfra);
1998                 
1999                 /* Make a temporary bConstraintOb for using these limit constraints
2000                  *      - they only care that cob->matrix is correctly set ;-)
2001                  *      - current space should be local
2002                  */
2003                 unit_m4(cob.matrix);
2004                 copy_v3_v3(cob.matrix[3], td->loc);
2005                 
2006                 /* Evaluate valid constraints */
2007                 for (con= td->con; con; con= con->next) {
2008                         bConstraintTypeInfo *cti = NULL;
2009                         ListBase targets = {NULL, NULL};
2010                         float tmat[4][4];
2011                         
2012                         /* only consider constraint if enabled */
2013                         if (con->flag & CONSTRAINT_DISABLE) continue;
2014                         if (con->enforce == 0.0f) continue;
2015                         
2016                         /* only use it if it's tagged for this purpose (and the right type) */
2017                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2018                                 bLocLimitConstraint *data= con->data;
2019                                 
2020                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2021                                         continue;
2022                                 cti = ctiLoc;
2023                         }
2024                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2025                                 bDistLimitConstraint *data= con->data;
2026                                 
2027                                 if ((data->flag & LIMITDIST_TRANSFORM)==0)
2028                                         continue;
2029                                 cti = ctiDist;
2030                         }
2031                         
2032                         if (cti) {
2033                                 /* do space conversions */
2034                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2035                                         /* just multiply by td->mtx (this should be ok) */
2036                                         copy_m4_m4(tmat, cob.matrix);
2037                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2038                                 }
2039                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2040                                         /* skip... incompatable spacetype */
2041                                         continue;
2042                                 }
2043                                 
2044                                 /* get constraint targets if needed */
2045                                 get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2046                                 
2047                                 /* do constraint */
2048                                 cti->evaluate_constraint(con, &cob, &targets);
2049                                 
2050                                 /* convert spaces again */
2051                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2052                                         /* just multiply by td->mtx (this should be ok) */
2053                                         copy_m4_m4(tmat, cob.matrix);
2054                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2055                                 }
2056                                 
2057                                 /* free targets list */
2058                                 BLI_freelistN(&targets);
2059                         }
2060                 }
2061                 
2062                 /* copy results from cob->matrix */
2063                 copy_v3_v3(td->loc, cob.matrix[3]);
2064         }
2065 }
2066
2067 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2068 {
2069         /* Make a temporary bConstraintOb for use by limit constraints
2070          *      - they only care that cob->matrix is correctly set ;-)
2071          *      - current space should be local
2072          */
2073         memset(cob, 0, sizeof(bConstraintOb));
2074         if (td->ext)
2075         {
2076                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2077                         /* quats */
2078                         /* objects and bones do normalization first too, otherwise
2079                            we don't necessarily end up with a rotation matrix, and
2080                            then conversion back to quat gives a different result */
2081                         float quat[4];
2082                         normalize_qt_qt(quat, td->ext->quat);
2083                         quat_to_mat4(cob->matrix, quat);
2084                 }
2085                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2086                         /* axis angle */
2087                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2088                 }
2089                 else {
2090                         /* eulers */
2091                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2092                 }
2093         }
2094 }
2095
2096 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2097 {
2098         if (td->con) {
2099                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2100                 bConstraintOb cob;
2101                 bConstraint *con;
2102                 int dolimit = 0;
2103                 
2104                 /* Evaluate valid constraints */
2105                 for (con= td->con; con; con= con->next) {
2106                         /* only consider constraint if enabled */
2107                         if (con->flag & CONSTRAINT_DISABLE) continue;
2108                         if (con->enforce == 0.0f) continue;
2109                         
2110                         /* we're only interested in Limit-Rotation constraints */
2111                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2112                                 bRotLimitConstraint *data= con->data;
2113                                 float tmat[4][4];
2114                                 
2115                                 /* only use it if it's tagged for this purpose */
2116                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2117                                         continue;
2118
2119                                 /* skip incompatable spacetypes */
2120                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2121                                         continue;
2122
2123                                 /* only do conversion if necessary, to preserve quats and eulers */
2124                                 if(!dolimit) {
2125                                         constraintob_from_transdata(&cob, td);
2126                                         dolimit= 1;
2127                                 }
2128                                 
2129                                 /* do space conversions */
2130                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2131                                         /* just multiply by td->mtx (this should be ok) */
2132                                         copy_m4_m4(tmat, cob.matrix);
2133                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2134                                 }
2135                                 
2136                                 /* do constraint */
2137                                 cti->evaluate_constraint(con, &cob, NULL);
2138                                 
2139                                 /* convert spaces again */
2140                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2141                                         /* just multiply by td->mtx (this should be ok) */
2142                                         copy_m4_m4(tmat, cob.matrix);
2143                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2144                                 }
2145                         }
2146                 }
2147                 
2148                 if(dolimit) {
2149                         /* copy results from cob->matrix */
2150                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2151                                 /* quats */
2152                                 mat4_to_quat( td->ext->quat,cob.matrix);
2153                         }
2154                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2155                                 /* axis angle */
2156                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2157                         }
2158                         else {
2159                                 /* eulers */
2160                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2161                         }
2162                 }
2163         }
2164 }
2165
2166 static void constraintSizeLim(TransInfo *t, TransData *td)
2167 {
2168         if (td->con && td->ext) {
2169                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2170                 bConstraintOb cob= {NULL};
2171                 bConstraint *con;
2172                 
2173                 /* Make a temporary bConstraintOb for using these limit constraints
2174                  *      - they only care that cob->matrix is correctly set ;-)
2175                  *      - current space should be local
2176                  */
2177                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2178                         /* scale val and reset size */
2179                         return; // TODO: fix this case
2180                 }
2181                 else {
2182                         /* Reset val if SINGLESIZE but using a constraint */
2183                         if (td->flag & TD_SINGLESIZE)
2184                                 return;
2185                         
2186                         size_to_mat4( cob.matrix,td->ext->size);
2187                 }
2188                 
2189                 /* Evaluate valid constraints */
2190                 for (con= td->con; con; con= con->next) {
2191                         /* only consider constraint if enabled */
2192                         if (con->flag & CONSTRAINT_DISABLE) continue;
2193                         if (con->enforce == 0.0f) continue;
2194                         
2195                         /* we're only interested in Limit-Scale constraints */
2196                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2197                                 bSizeLimitConstraint *data= con->data;
2198                                 float tmat[4][4];
2199                                 
2200                                 /* only use it if it's tagged for this purpose */
2201                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2202                                         continue;
2203                                 
2204                                 /* do space conversions */
2205                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2206                                         /* just multiply by td->mtx (this should be ok) */
2207                                         copy_m4_m4(tmat, cob.matrix);
2208                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2209                                 }
2210                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2211                                         /* skip... incompatable spacetype */
2212                                         continue;
2213                                 }
2214                                 
2215                                 /* do constraint */
2216                                 cti->evaluate_constraint(con, &cob, NULL);
2217                                 
2218                                 /* convert spaces again */
2219                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2220                                         /* just multiply by td->mtx (this should be ok) */
2221                                         copy_m4_m4(tmat, cob.matrix);
2222                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2223                                 }
2224                         }
2225                 }
2226                 
2227                 /* copy results from cob->matrix */
2228                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2229                         /* scale val and reset size */
2230                         return; // TODO: fix this case
2231                 }
2232                 else {
2233                         /* Reset val if SINGLESIZE but using a constraint */
2234                         if (td->flag & TD_SINGLESIZE)
2235                                 return;
2236                         
2237                         mat4_to_size( td->ext->size,cob.matrix);
2238                 }
2239         }
2240 }
2241
2242 /* ************************** WARP *************************** */
2243
2244 static void postInputWarp(TransInfo *t, float values[3])
2245 {
2246         mul_v3_fl(values, (float)(M_PI * 2));
2247
2248         if (t->customData) /* non-null value indicates reversed input */
2249         {
2250                 negate_v3(values);
2251         }
2252 }
2253
2254 void initWarp(TransInfo *t)
2255 {
2256         float max[3], min[3];
2257         int i;
2258         
2259         t->mode = TFM_WARP;
2260         t->transform = Warp;
2261         t->handleEvent = handleEventWarp;
2262         
2263         setInputPostFct(&t->mouse, postInputWarp);
2264         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2265         
2266         t->idx_max = 0;
2267         t->num.idx_max = 0;
2268         t->snap[0] = 0.0f;
2269         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2270         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2271         
2272         t->num.increment = 1.0f;
2273
2274         t->flag |= T_NO_CONSTRAINT;
2275         
2276         /* we need min/max in view space */
2277         for(i = 0; i < t->total; i++) {
2278                 float center[3];
2279                 copy_v3_v3(center, t->data[i].center);
2280                 mul_m3_v3(t->data[i].mtx, center);
2281                 mul_m4_v3(t->viewmat, center);
2282                 sub_v3_v3(center, t->viewmat[3]);
2283                 if (i)
2284                         minmax_v3v3_v3(min, max, center);
2285                 else {
2286                         copy_v3_v3(max, center);
2287                         copy_v3_v3(min, center);
2288                 }
2289         }
2290
2291         mid_v3_v3v3(t->center, min, max);
2292
2293         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2294         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2295 }
2296
2297 int handleEventWarp(TransInfo *t, wmEvent *event)
2298 {
2299         int status = 0;
2300         
2301         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2302         {
2303                 // Use customData pointer to signal warp direction
2304                 if      (t->customData == NULL)
2305                         t->customData = (void*)1;
2306                 else
2307                         t->customData = NULL;
2308                 
2309                 status = 1;
2310         }
2311         
2312         return status;
2313 }
2314
2315 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2316 {
2317         TransData *td = t->data;
2318         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2319         int i;
2320         char str[50];
2321         
2322         curs= give_cursor(t->scene, t->view);
2323         /*
2324          * gcursor is the one used for helpline.
2325          * It has to be in the same space as the drawing loop
2326          * (that means it needs to be in the object's space when in edit mode and
2327          *  in global space in object mode)
2328          *
2329          * cursor is used for calculations.
2330          * It needs to be in view space, but we need to take object's offset
2331          * into account if in Edit mode.
2332          */
2333         copy_v3_v3(cursor, curs);
2334         copy_v3_v3(gcursor, cursor);
2335         if (t->flag & T_EDIT) {
2336                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2337                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2338                 mul_m3_v3(t->data->smtx, gcursor);
2339         }
2340         mul_m4_v3(t->viewmat, cursor);
2341         sub_v3_v3(cursor, t->viewmat[3]);
2342         
2343         /* amount of radians for warp */
2344         circumfac = t->values[0];
2345         
2346         snapGrid(t, &circumfac);
2347         applyNumInput(&t->num, &circumfac);
2348         
2349         /* header print for NumInput */
2350         if (hasNumInput(&t->num)) {
2351                 char c[20];
2352                 
2353                 outputNumInput(&(t->num), c);
2354                 
2355                 sprintf(str, "Warp: %s", c);
2356
2357                 circumfac = DEG2RADF(circumfac);
2358         }
2359         else {
2360                 /* default header print */
2361                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2362         }
2363         
2364         t->values[0] = circumfac;
2365
2366         circumfac /= 2; /* only need 180 on each side to make 360 */
2367         
2368         for(i = 0; i < t->total; i++, td++) {
2369                 float loc[3];
2370                 if (td->flag & TD_NOACTION)
2371                         break;
2372                 
2373                 if (td->flag & TD_SKIP)
2374                         continue;
2375                 
2376                 /* translate point to center, rotate in such a way that outline==distance */
2377                 copy_v3_v3(vec, td->iloc);
2378                 mul_m3_v3(td->mtx, vec);
2379                 mul_m4_v3(t->viewmat, vec);
2380                 sub_v3_v3(vec, t->viewmat[3]);
2381                 
2382                 dist= vec[0]-cursor[0];
2383                 
2384                 /* t->val is X dimension projected boundbox */
2385                 phi0= (circumfac*dist/t->val);
2386                 
2387                 vec[1]= (vec[1]-cursor[1]);
2388                 
2389                 co= (float)cos(phi0);
2390                 si= (float)sin(phi0);
2391                 loc[0]= -si*vec[1]+cursor[0];
2392                 loc[1]= co*vec[1]+cursor[1];
2393                 loc[2]= vec[2];
2394                 
2395                 mul_m4_v3(t->viewinv, loc);
2396                 sub_v3_v3(loc, t->viewinv[3]);
2397                 mul_m3_v3(td->smtx, loc);
2398                 
2399                 sub_v3_v3(loc, td->iloc);
2400                 mul_v3_fl(loc, td->factor);
2401                 add_v3_v3v3(td->loc, td->iloc, loc);
2402         }
2403         
2404         recalcData(t);
2405         
2406         ED_area_headerprint(t->sa, str);
2407         
2408         return 1;
2409 }
2410
2411 /* ************************** SHEAR *************************** */
2412
2413 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2414 {
2415         mul_v3_fl(values, 0.05f);
2416 }
2417
2418 void initShear(TransInfo *t)
2419 {
2420         t->mode = TFM_SHEAR;
2421         t->transform = Shear;
2422         t->handleEvent = handleEventShear;
2423         
2424         setInputPostFct(&t->mouse, postInputShear);
2425         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2426         
2427         t->idx_max = 0;
2428         t->num.idx_max = 0;
2429         t->snap[0] = 0.0f;
2430         t->snap[1] = 0.1f;
2431         t->snap[2] = t->snap[1] * 0.1f;
2432         
2433         t->num.increment = 0.1f;
2434
2435         t->flag |= T_NO_CONSTRAINT;
2436 }
2437
2438 int handleEventShear(TransInfo *t, wmEvent *event)
2439 {
2440         int status = 0;
2441         
2442         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2443         {
2444                 // Use customData pointer to signal Shear direction
2445                 if      (t->customData == NULL)
2446                 {
2447                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2448                         t->customData = (void*)1;
2449                 }
2450                 else
2451                 {
2452                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2453                         t->customData = NULL;
2454                 }
2455                 
2456                 status = 1;
2457         }
2458         
2459         return status;
2460 }
2461
2462
2463 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2464 {
2465         TransData *td = t->data;
2466         float vec[3];
2467         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2468         float value;
2469         int i;
2470         char str[50];
2471         
2472         copy_m3_m4(persmat, t->viewmat);
2473         invert_m3_m3(persinv, persmat);
2474         
2475         value = t->values[0];
2476         
2477         snapGrid(t, &value);
2478         
2479         applyNumInput(&t->num, &value);
2480         
2481         /* header print for NumInput */
2482         if (hasNumInput(&t->num)) {
2483                 char c[20];
2484                 
2485                 outputNumInput(&(t->num), c);
2486                 
2487                 sprintf(str, "Shear: %s %s", c, t->proptext);
2488         }
2489         else {
2490                 /* default header print */
2491                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2492         }
2493         
2494         unit_m3(smat);
2495         
2496         // Custom data signals shear direction
2497         if (t->customData == NULL)
2498                 smat[1][0] = value;
2499         else
2500                 smat[0][1] = value;
2501         
2502         mul_m3_m3m3(tmat, smat, persmat);
2503         mul_m3_m3m3(totmat, persinv, tmat);
2504         
2505         for(i = 0 ; i < t->total; i++, td++) {
2506                 if (td->flag & TD_NOACTION)
2507                         break;
2508                 
2509                 if (td->flag & TD_SKIP)
2510                         continue;
2511                 
2512                 if (t->obedit) {
2513                         float mat3[3][3];
2514                         mul_m3_m3m3(mat3, totmat, td->mtx);
2515                         mul_m3_m3m3(tmat, td->smtx, mat3);
2516                 }
2517                 else {
2518                         copy_m3_m3(tmat, totmat);
2519                 }
2520                 sub_v3_v3v3(vec, td->center, t->center);
2521                 
2522                 mul_m3_v3(tmat, vec);
2523                 
2524                 add_v3_v3(vec, t->center);
2525                 sub_v3_v3(vec, td->center);
2526                 
2527                 mul_v3_fl(vec, td->factor);
2528                 
2529                 add_v3_v3v3(td->loc, td->iloc, vec);
2530         }
2531         
2532         recalcData(t);
2533         
2534         ED_area_headerprint(t->sa, str);
2535
2536         return 1;
2537 }
2538
2539 /* ************************** RESIZE *************************** */
2540
2541 void initResize(TransInfo *t)
2542 {
2543         t->mode = TFM_RESIZE;
2544         t->transform = Resize;
2545         
2546         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2547         
2548         t->flag |= T_NULL_ONE;
2549         t->num.flag |= NUM_NULL_ONE;
2550         t->num.flag |= NUM_AFFECT_ALL;
2551         if (!t->obedit) {
2552                 t->flag |= T_NO_ZERO;
2553                 t->num.flag |= NUM_NO_ZERO;
2554         }
2555         
2556         t->idx_max = 2;
2557         t->num.idx_max = 2;
2558         t->snap[0] = 0.0f;
2559         t->snap[1] = 0.1f;
2560         t->snap[2] = t->snap[1] * 0.1f;
2561
2562         t->num.increment = t->snap[1];
2563 }
2564
2565 static void headerResize(TransInfo *t, float vec[3], char *str) {
2566         char tvec[60];
2567         char *spos= str;
2568         if (hasNumInput(&t->num)) {
2569                 outputNumInput(&(t->num), tvec);
2570         }
2571         else {
2572                 sprintf(&tvec[0], "%.4f", vec[0]);
2573                 sprintf(&tvec[20], "%.4f", vec[1]);
2574                 sprintf(&tvec[40], "%.4f", vec[2]);
2575         }
2576         
2577         if (t->con.mode & CON_APPLY) {
2578                 switch(t->num.idx_max) {
2579                 case 0:
2580                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2581                         break;
2582                 case 1:
2583                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2584                         break;
2585                 case 2:
2586                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2587                 }
2588         }
2589         else {
2590                 if (t->flag & T_2D_EDIT)
2591                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2592                 else
2593                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2594         }
2595         
2596         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2597                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2598         }
2599
2600         (void)spos;
2601 }
2602
2603 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2604 #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)
2605
2606 /* smat is reference matrix, only scaled */
2607 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2608 {
2609         float vec[3];
2610         
2611         copy_v3_v3(vec, mat[0]);
2612         size[0]= normalize_v3(vec);
2613         copy_v3_v3(vec, mat[1]);
2614         size[1]= normalize_v3(vec);
2615         copy_v3_v3(vec, mat[2]);
2616         size[2]= normalize_v3(vec);
2617         
2618         /* first tried with dotproduct... but the sign flip is crucial */
2619         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2620         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2621         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2622 }
2623
2624
2625 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3]) {
2626         float tmat[3][3], smat[3][3], center[3];
2627         float vec[3];
2628         
2629         if (t->flag & T_EDIT) {
2630                 mul_m3_m3m3(smat, mat, td->mtx);
2631                 mul_m3_m3m3(tmat, td->smtx, smat);
2632         }
2633         else {
2634                 copy_m3_m3(tmat, mat);
2635         }
2636         
2637         if (t->con.applySize) {
2638                 t->con.applySize(t, td, tmat);
2639         }
2640         
2641         /* local constraint shouldn't alter center */
2642         if (t->around == V3D_LOCAL) {
2643                 if (t->flag & T_OBJECT) {
2644                         copy_v3_v3(center, td->center);
2645                 }
2646                 else if (t->flag & T_EDIT) {
2647                         
2648                         if(t->around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2649                                 copy_v3_v3(center, td->center);
2650                         }
2651                         else {
2652                                 copy_v3_v3(center, t->center);
2653                         }
2654                 }
2655                 else {
2656                         copy_v3_v3(center, t->center);
2657                 }
2658         }
2659         else {
2660                 copy_v3_v3(center, t->center);
2661         }
2662         
2663         if (td->ext) {
2664                 float fsize[3];
2665                 
2666                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2667                         float obsizemat[3][3];
2668                         // Reorient the size mat to fit the oriented object.
2669                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2670                         //print_m3("obsizemat", obsizemat);
2671                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2672                         //print_v3("fsize", fsize);
2673                 }
2674                 else {
2675                         mat3_to_size( fsize,tmat);
2676                 }
2677                 
2678                 protectedSizeBits(td->protectflag, fsize);
2679                 
2680                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2681                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)){
2682                                 /* scale val and reset size */
2683                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2684                                 
2685                                 td->ext->size[0] = td->ext->isize[0];
2686                                 td->ext->size[1] = td->ext->isize[1];
2687                                 td->ext->size[2] = td->ext->isize[2];
2688                          }
2689                         else {
2690                                 /* Reset val if SINGLESIZE but using a constraint */
2691                                 if (td->flag & TD_SINGLESIZE)
2692                                          *td->val = td->ival;
2693                                 
2694                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2695                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2696                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2697                         }
2698                 }
2699                 
2700                 constraintSizeLim(t, td);
2701         }
2702         
2703         /* For individual element center, Editmode need to use iloc */
2704         if (t->flag & T_POINTS)
2705                 sub_v3_v3v3(vec, td->iloc, center);
2706         else
2707                 sub_v3_v3v3(vec, td->center, center);
2708         
2709         mul_m3_v3(tmat, vec);
2710         
2711         add_v3_v3(vec, center);
2712         if (t->flag & T_POINTS)
2713                 sub_v3_v3(vec, td->iloc);
2714         else
2715                 sub_v3_v3(vec, td->center);
2716         
2717         mul_v3_fl(vec, td->factor);
2718         
2719         if (t->flag & (T_OBJECT|T_POSE)) {
2720                 mul_m3_v3(td->smtx, vec);
2721         }
2722         
2723         protectedTransBits(td->protectflag, vec);
2724         add_v3_v3v3(td->loc, td->iloc, vec);
2725         
2726         constraintTransLim(t, td);
2727 }
2728
2729 int Resize(TransInfo *t, const int mval[2])
2730 {
2731         TransData *td;
2732         float size[3], mat[3][3];
2733         float ratio;
2734         int i;
2735         char str[200];
2736         
2737         /* for manipulator, center handle, the scaling can't be done relative to center */
2738         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2739         {
2740                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2741         }
2742         else
2743         {
2744                 ratio = t->values[0];
2745         }
2746         
2747         size[0] = size[1] = size[2] = ratio;
2748         
2749         snapGrid(t, size);
2750         
2751         if (hasNumInput(&t->num)) {
2752                 applyNumInput(&t->num, size);
2753                 constraintNumInput(t, size);
2754         }
2755         
2756         applySnapping(t, size);
2757         
2758         if (t->flag & T_AUTOVALUES)
2759         {
2760                 copy_v3_v3(size, t->auto_values);
2761         }
2762         
2763         copy_v3_v3(t->values, size);
2764         
2765         size_to_mat3( mat,size);
2766         
2767         if (t->con.applySize) {
2768                 t->con.applySize(t, NULL, mat);
2769         }
2770         
2771         copy_m3_m3(t->mat, mat);        // used in manipulator
2772         
2773         headerResize(t, size, str);
2774         
2775         for(i = 0, td=t->data; i < t->total; i++, td++) {
2776                 if (td->flag & TD_NOACTION)
2777                         break;
2778                 
2779                 if (td->flag & TD_SKIP)
2780                         continue;
2781                 
2782                 ElementResize(t, td, mat);
2783         }
2784         
2785         /* evil hack - redo resize if cliping needed */
2786         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2787                 size_to_mat3( mat,size);
2788                 
2789                 if (t->con.applySize)
2790                         t->con.applySize(t, NULL, mat);
2791                 
2792                 for(i = 0, td=t->data; i < t->total; i++, td++)
2793                         ElementResize(t, td, mat);
2794         }
2795         
2796         recalcData(t);
2797         
2798         ED_area_headerprint(t->sa, str);
2799         
2800         return 1;
2801 }
2802
2803 /* ************************** TOSPHERE *************************** */
2804
2805 void initToSphere(TransInfo *t)
2806 {
2807         TransData *td = t->data;
2808         int i;
2809         
2810         t->mode = TFM_TOSPHERE;
2811         t->transform = ToSphere;
2812         
2813         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2814         
2815         t->idx_max = 0;
2816         t->num.idx_max = 0;
2817         t->snap[0] = 0.0f;
2818         t->snap[1] = 0.1f;
2819         t->snap[2] = t->snap[1] * 0.1f;
2820         
2821         t->num.increment = t->snap[1];
2822
2823         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2824         t->flag |= T_NO_CONSTRAINT;
2825         
2826         // Calculate average radius
2827         for(i = 0 ; i < t->total; i++, td++) {
2828                 t->val += len_v3v3(t->center, td->iloc);
2829         }
2830         
2831         t->val /= (float)t->total;
2832 }
2833
2834 int ToSphere(TransInfo *t, const int UNUSED(mval[2]))
2835 {
2836         float vec[3];
2837         float ratio, radius;
2838         int i;
2839         char str[64];
2840         TransData *td = t->data;
2841         
2842         ratio = t->values[0];
2843         
2844         snapGrid(t, &ratio);
2845         
2846         applyNumInput(&t->num, &ratio);
2847         
2848         if (ratio < 0)
2849                 ratio = 0.0f;
2850         else if (ratio > 1)
2851                 ratio = 1.0f;
2852         
2853         t->values[0] = ratio;
2854
2855         /* header print for NumInput */
2856         if (hasNumInput(&t->num)) {
2857                 char c[20];
2858                 
2859                 outputNumInput(&(t->num), c);
2860                 
2861                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2862         }
2863         else {
2864                 /* default header print */
2865                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2866         }
2867         
2868         
2869         for(i = 0 ; i < t->total; i++, td++) {
2870                 float tratio;
2871                 if (td->flag & TD_NOACTION)
2872                         break;
2873                 
2874                 if (td->flag & TD_SKIP)
2875                         continue;
2876                 
2877                 sub_v3_v3v3(vec, td->iloc, t->center);
2878                 
2879                 radius = normalize_v3(vec);
2880                 
2881                 tratio = ratio * td->factor;
2882                 
2883                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2884                 
2885                 add_v3_v3v3(td->loc, t->center, vec);
2886         }
2887         
2888         
2889         recalcData(t);
2890         
2891         ED_area_headerprint(t->sa, str);
2892         
2893         return 1;
2894 }
2895
2896 /* ************************** ROTATION *************************** */
2897
2898
2899 static void postInputRotation(TransInfo *t, float values[3])
2900 {
2901         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2902                 t->con.applyRot(t, NULL, t->axis, values);
2903         }
2904 }
2905
2906 void initRotation(TransInfo *t)
2907 {
2908         t->mode = TFM_ROTATION;
2909         t->transform = Rotation;
2910         
2911         setInputPostFct(&t->mouse, postInputRotation);
2912         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2913         
2914         t->idx_max = 0;
2915         t->num.idx_max = 0;
2916         t->snap[0] = 0.0f;
2917         t->snap[1] = (float)((5.0/180)*M_PI);
2918         t->snap[2] = t->snap[1] * 0.2f;
2919         
2920         t->num.increment = 1.0f;
2921
2922         if (t->flag & T_2D_EDIT)
2923                 t->flag |= T_NO_CONSTRAINT;
2924
2925         negate_v3_v3(t->axis, t->viewinv[2]);
2926         normalize_v3(t->axis);
2927
2928         copy_v3_v3(t->axis_orig, t->axis);
2929 }
2930
2931 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around) {
2932         float vec[3], totmat[3][3], smat[3][3];
2933         float eul[3], fmat[3][3], quat[4];
2934         float *center = t->center;
2935         
2936         /* local constraint shouldn't alter center */
2937         if (around == V3D_LOCAL) {
2938                 if (t->flag & (T_OBJECT|T_POSE)) {
2939                         center = td->center;
2940                 }
2941                 else {
2942                         if(around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2943                                 center = td->center;
2944                         }
2945                 }
2946         }
2947         
2948         if (t->flag & T_POINTS) {
2949                 mul_m3_m3m3(totmat, mat, td->mtx);
2950                 mul_m3_m3m3(smat, td->smtx, totmat);
2951                 
2952                 sub_v3_v3v3(vec, td->iloc, center);
2953                 mul_m3_v3(smat, vec);
2954                 
2955                 add_v3_v3v3(td->loc, vec, center);
2956                 
2957                 sub_v3_v3v3(vec,td->loc,td->iloc);
2958                 protectedTransBits(td->protectflag, vec);
2959                 add_v3_v3v3(td->loc, td->iloc, vec);
2960                 
2961                 
2962                 if(td->flag & TD_USEQUAT) {
2963                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, NULL, NULL, NULL, NULL, NULL);
2964                         mat3_to_quat( quat,fmat);       // Actual transform
2965                         
2966                         if(td->ext->quat){
2967                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2968                                 
2969                                 /* is there a reason not to have this here? -jahka */
2970                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2971          &nb