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