Merging r39135 through r39172 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                                 break;
629                         case TFM_MODAL_ROTATE:
630                                 /* only switch when... */
631                                 if(!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
632                                         if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
633                                                 
634                                                 resetTransRestrictions(t);
635                                                 
636                                                 if (t->mode == TFM_ROTATION) {
637                                                         restoreTransObjects(t);
638                                                         initTrackball(t);
639                                                 }
640                                                 else {
641                                                         restoreTransObjects(t);
642                                                         initRotation(t);
643                                                 }
644                                                 initSnapping(t, NULL); // need to reinit after mode change
645                                                 t->redraw |= TREDRAW_HARD;
646                                         }
647                                 }
648                                 break;
649                         case TFM_MODAL_RESIZE:
650                                 /* only switch when... */
651                                 if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
652                                         resetTransRestrictions(t);
653                                         restoreTransObjects(t);
654                                         initResize(t);
655                                         initSnapping(t, NULL); // need to reinit after mode change
656                                         t->redraw |= TREDRAW_HARD;
657                                 }
658                                 break;
659                                 
660                         case TFM_MODAL_SNAP_INV_ON:
661                                 t->modifiers |= MOD_SNAP_INVERT;
662                                 t->redraw |= TREDRAW_HARD;
663                                 break;
664                         case TFM_MODAL_SNAP_INV_OFF:
665                                 t->modifiers &= ~MOD_SNAP_INVERT;
666                                 t->redraw |= TREDRAW_HARD;
667                                 break;
668                         case TFM_MODAL_SNAP_TOGGLE:
669                                 t->modifiers ^= MOD_SNAP;
670                                 t->redraw |= TREDRAW_HARD;
671                                 break;
672                         case TFM_MODAL_AXIS_X:
673                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
674                                         if (cmode == 'X') {
675                                                 stopConstraint(t);
676                                         }
677                                         else {
678                                                 if (t->flag & T_2D_EDIT) {
679                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
680                                                 }
681                                                 else {
682                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS0), "along %s X");
683                                                 }
684                                         }
685                                         t->redraw |= TREDRAW_HARD;
686                                 }
687                                 break;
688                         case TFM_MODAL_AXIS_Y:
689                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
690                                         if (cmode == 'Y') {
691                                                 stopConstraint(t);
692                                         }
693                                         else {
694                                                 if (t->flag & T_2D_EDIT) {
695                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
696                                                 }
697                                                 else {
698                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS1), "along %s Y");
699                                                 }
700                                         }
701                                         t->redraw |= TREDRAW_HARD;
702                                 }
703                                 break;
704                         case TFM_MODAL_AXIS_Z:
705                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
706                                         if (cmode == 'Z') {
707                                                 stopConstraint(t);
708                                         }
709                                         else {
710                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS2), "along %s Z");
711                                         }
712                                         t->redraw |= TREDRAW_HARD;
713                                 }
714                                 break;
715                         case TFM_MODAL_PLANE_X:
716                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
717                                         if (cmode == 'X') {
718                                                 stopConstraint(t);
719                                         }
720                                         else {
721                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
722                                         }
723                                         t->redraw |= TREDRAW_HARD;
724                                 }
725                                 break;
726                         case TFM_MODAL_PLANE_Y:
727                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
728                                         if (cmode == 'Y') {
729                                                 stopConstraint(t);
730                                         }
731                                         else {
732                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
733                                         }
734                                         t->redraw |= TREDRAW_HARD;
735                                 }
736                                 break;
737                         case TFM_MODAL_PLANE_Z:
738                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
739                                         if (cmode == 'Z') {
740                                                 stopConstraint(t);
741                                         }
742                                         else {
743                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
744                                         }
745                                         t->redraw |= TREDRAW_HARD;
746                                 }
747                                 break;
748                         case TFM_MODAL_CONS_OFF:
749                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
750                                         stopConstraint(t);
751                                         t->redraw |= TREDRAW_HARD;
752                                 }
753                                 break;
754                         case TFM_MODAL_ADD_SNAP:
755                                 addSnapPoint(t);
756                                 t->redraw |= TREDRAW_HARD;
757                                 break;
758                         case TFM_MODAL_REMOVE_SNAP:
759                                 removeSnapPoint(t);
760                                 t->redraw |= TREDRAW_HARD;
761                                 break;
762                         case TFM_MODAL_PROPSIZE_UP:
763                                 if(t->flag & T_PROP_EDIT) {
764                                         t->prop_size*= 1.1f;
765                                         if(t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
766                                                 t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
767                                         calculatePropRatio(t);
768                                 }
769                                 t->redraw |= TREDRAW_HARD;
770                                 break;
771                         case TFM_MODAL_PROPSIZE_DOWN:
772                                 if (t->flag & T_PROP_EDIT) {
773                                         t->prop_size*= 0.90909090f;
774                                         calculatePropRatio(t);
775                                 }
776                                 t->redraw |= TREDRAW_HARD;
777                                 break;
778                         case TFM_MODAL_AUTOIK_LEN_INC:
779                                 if (t->flag & T_AUTOIK)
780                                         transform_autoik_update(t, 1);
781                                 t->redraw |= TREDRAW_HARD;
782                                 break;
783                         case TFM_MODAL_AUTOIK_LEN_DEC:
784                                 if (t->flag & T_AUTOIK) 
785                                         transform_autoik_update(t, -1);
786                                 t->redraw |= TREDRAW_HARD;
787                                 break;
788                         default:
789                                 handled = 0;
790                                 break;
791                 }
792
793                 // Modal numinput events
794                 t->redraw |= handleNumInput(&(t->num), event);
795         }
796         /* else do non-mapped events */
797         else if (event->val==KM_PRESS) {
798                 switch (event->type){
799                 case RIGHTMOUSE:
800                         t->state = TRANS_CANCEL;
801                         break;
802                 /* enforce redraw of transform when modifiers are used */
803                 case LEFTSHIFTKEY:
804                 case RIGHTSHIFTKEY:
805                         t->modifiers |= MOD_CONSTRAINT_PLANE;
806                         t->redraw |= TREDRAW_HARD;
807                         break;
808
809                 case LEFTALTKEY:
810                         if(t->options&CTX_MOVIECLIP) {
811                                 restoreTransObjects(t);
812
813                                 t->flag|= T_RELATIVE_POSITION;
814                                 t->redraw |= TREDRAW_HARD;
815                         }
816                         break;
817
818                 case SPACEKEY:
819                         if ((t->spacetype==SPACE_VIEW3D) && event->alt) {
820 #if 0 // TRANSFORM_FIX_ME
821                                 int mval[2];
822
823                                 getmouseco_sc(mval);
824                                 BIF_selectOrientation();
825                                 calc_manipulator_stats(curarea);
826                                 copy_m3_m4(t->spacemtx, G.vd->twmat);
827                                 warp_pointer(mval[0], mval[1]);
828 #endif
829                         }
830                         else {
831                                 t->state = TRANS_CONFIRM;
832                         }
833                         break;
834
835                 case MIDDLEMOUSE:
836                         if ((t->flag & T_NO_CONSTRAINT)==0) {
837                                 /* exception for switching to dolly, or trackball, in camera view */
838                                 if (t->flag & T_CAMERA) {
839                                         if (t->mode==TFM_TRANSLATION)
840                                                 setLocalConstraint(t, (CON_AXIS2), "along local Z");
841                                         else if (t->mode==TFM_ROTATION) {
842                                                 restoreTransObjects(t);
843                                                 initTrackball(t);
844                                         }
845                                 }
846                                 else {
847                                         t->modifiers |= MOD_CONSTRAINT_SELECT;
848                                         if (t->con.mode & CON_APPLY) {
849                                                 stopConstraint(t);
850                                         }
851                                         else {
852                                                 if (event->shift) {
853                                                         initSelectConstraint(t, t->spacemtx);
854                                                 }
855                                                 else {
856                                                         /* bit hackish... but it prevents mmb select to print the orientation from menu */
857                                                         strcpy(t->spacename, "global");
858                                                         initSelectConstraint(t, mati);
859                                                 }
860                                                 postSelectConstraint(t);
861                                         }
862                                 }
863                                 t->redraw |= TREDRAW_HARD;
864                         }
865                         break;
866                 case ESCKEY:
867                         t->state = TRANS_CANCEL;
868                         break;
869                 case PADENTER:
870                 case RETKEY:
871                         t->state = TRANS_CONFIRM;
872                         break;
873                 case GKEY:
874                         /* only switch when... */
875                         if( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
876                                 resetTransRestrictions(t);
877                                 restoreTransObjects(t);
878                                 initTranslation(t);
879                                 initSnapping(t, NULL); // need to reinit after mode change
880                                 t->redraw |= TREDRAW_HARD;
881                         }
882                         break;
883                 case SKEY:
884                         /* only switch when... */
885                         if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
886                                 resetTransRestrictions(t);
887                                 restoreTransObjects(t);
888                                 initResize(t);
889                                 initSnapping(t, NULL); // need to reinit after mode change
890                                 t->redraw |= TREDRAW_HARD;
891                         }
892                         break;
893                 case RKEY:
894                         /* only switch when... */
895                         if(!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
896                                 if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
897
898                                         resetTransRestrictions(t);
899
900                                         if (t->mode == TFM_ROTATION) {
901                                                 restoreTransObjects(t);
902                                                 initTrackball(t);
903                                         }
904                                         else {
905                                                 restoreTransObjects(t);
906                                                 initRotation(t);
907                                         }
908                                         initSnapping(t, NULL); // need to reinit after mode change
909                                         t->redraw |= TREDRAW_HARD;
910                                 }
911                         }
912                         break;
913                 case CKEY:
914                         if (event->alt) {
915                                 t->flag ^= T_PROP_CONNECTED;
916                                 sort_trans_data_dist(t);
917                                 calculatePropRatio(t);
918                                 t->redraw= 1;
919                         }
920                         else {
921                                 stopConstraint(t);
922                                 t->redraw |= TREDRAW_HARD;
923                         }
924                         break;
925                 case XKEY:
926                         if ((t->flag & T_NO_CONSTRAINT)==0) {
927                                 if (t->flag & T_2D_EDIT) {
928                                         if (cmode == 'X') {
929                                                 stopConstraint(t);
930                                         } else {
931                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
932                                         }
933                                 } else {
934                                         if (cmode == 'X') {
935                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
936                                                         stopConstraint(t);
937                                                 } else {
938                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
939                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
940                                                                 setUserConstraint(t, orientation, (CON_AXIS0), "along %s X");
941                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
942                                                                 setUserConstraint(t, orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
943                                                 }
944                                         } else {
945                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
946                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along %s X");
947                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
948                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1|CON_AXIS2), "locking %s X");
949                                         }
950                                 }
951                                 t->redraw |= TREDRAW_HARD;
952                         }
953                         break;
954                 case YKEY:
955                         if ((t->flag & T_NO_CONSTRAINT)==0) {
956                                 if (t->flag & T_2D_EDIT) {
957                                         if (cmode == 'Y') {
958                                                 stopConstraint(t);
959                                         } else {
960                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
961                                         }
962                                 } else {
963                                         if (cmode == 'Y') {
964                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
965                                                         stopConstraint(t);
966                                                 } else {
967                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
968                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
969                                                                 setUserConstraint(t, orientation, (CON_AXIS1), "along %s Y");
970                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
971                                                                 setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
972                                                 }
973                                         } else {
974                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
975                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along %s Y");
976                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
977                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS2), "locking %s Y");
978                                         }
979                                 }
980                                 t->redraw |= TREDRAW_HARD;
981                         }
982                         break;
983                 case ZKEY:
984                         if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))==0) {
985                                 if (cmode == 'Z') {
986                                         if (t->con.orientation != V3D_MANIP_GLOBAL) {
987                                                 stopConstraint(t);
988                                         } else {
989                                                 short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
990                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
991                                                         setUserConstraint(t, orientation, (CON_AXIS2), "along %s Z");
992                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
993                                                         setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
994                                         }
995                                 } else {
996                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
997                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS2), "along %s Z");
998                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
999                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS1), "locking %s Z");
1000                                 }
1001                                 t->redraw |= TREDRAW_HARD;
1002                         }
1003                         break;
1004                 case OKEY:
1005                         if (t->flag & T_PROP_EDIT && event->shift) {
1006                                 t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1007                                 calculatePropRatio(t);
1008                                 t->redraw |= TREDRAW_HARD;
1009                         }
1010                         break;
1011                 case PADPLUSKEY:
1012                         if(event->alt && t->flag & T_PROP_EDIT) {
1013                                 t->prop_size *= 1.1f;
1014                                 if(t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1015                                         t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
1016                                 calculatePropRatio(t);
1017                         }
1018                         t->redraw= 1;
1019                         break;
1020                 case PAGEUPKEY:
1021                 case WHEELDOWNMOUSE:
1022                         if (t->flag & T_AUTOIK) {
1023                                 transform_autoik_update(t, 1);
1024                         }
1025                         else view_editmove(event->type);
1026                         t->redraw= 1;
1027                         break;
1028                 case PADMINUS:
1029                         if(event->alt && t->flag & T_PROP_EDIT) {
1030                                 t->prop_size*= 0.90909090f;
1031                                 calculatePropRatio(t);
1032                         }
1033                         t->redraw= 1;
1034                         break;
1035                 case PAGEDOWNKEY:
1036                 case WHEELUPMOUSE:
1037                         if (t->flag & T_AUTOIK) {
1038                                 transform_autoik_update(t, -1);
1039                         }
1040                         else view_editmove(event->type);
1041                         t->redraw= 1;
1042                         break;
1043                 default:
1044                         handled = 0;
1045                         break;
1046                 }
1047
1048                 // Numerical input events
1049                 t->redraw |= handleNumInput(&(t->num), event);
1050
1051                 // Snapping events
1052                 t->redraw |= handleSnapping(t, event);
1053
1054         }
1055         else if (event->val==KM_RELEASE) {
1056                 switch (event->type){
1057                 case LEFTSHIFTKEY:
1058                 case RIGHTSHIFTKEY:
1059                         t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1060                         t->redraw |= TREDRAW_HARD;
1061                         break;
1062
1063                 case MIDDLEMOUSE:
1064                         if ((t->flag & T_NO_CONSTRAINT)==0) {
1065                                 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1066                                 postSelectConstraint(t);
1067                                 t->redraw |= TREDRAW_HARD;
1068                         }
1069                         break;
1070                 case LEFTALTKEY:
1071                         if(t->options&CTX_MOVIECLIP) {
1072                                 restoreTransObjects(t);
1073
1074                                 t->flag&= ~T_RELATIVE_POSITION;
1075                                 t->redraw |= TREDRAW_HARD;
1076                         }
1077                         break;
1078 //              case LEFTMOUSE:
1079 //              case RIGHTMOUSE:
1080 //                      if(WM_modal_tweak_exit(event, t->event_type))
1081 ////                    if (t->options & CTX_TWEAK)
1082 //                              t->state = TRANS_CONFIRM;
1083 //                      break;
1084                 default:
1085                         handled = 0;
1086                         break;
1087                 }
1088
1089                 /* confirm transform if launch key is released after mouse move */
1090                 if (t->flag & T_RELEASE_CONFIRM)
1091                 {
1092                         /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1093                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE))
1094                         {
1095                                 t->state = TRANS_CONFIRM;
1096                         }
1097                 }
1098         }
1099
1100         // Per transform event, if present
1101         if (t->handleEvent)
1102                 t->redraw |= t->handleEvent(t, event);
1103
1104         if (handled || t->redraw)
1105                 return 0;
1106         else
1107                 return OPERATOR_PASS_THROUGH;
1108 }
1109
1110 int calculateTransformCenter(bContext *C, int centerMode, float *vec)
1111 {
1112         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1113         int success = 1;
1114
1115         t->state = TRANS_RUNNING;
1116
1117         t->options = CTX_NONE;
1118
1119         t->mode = TFM_DUMMY;
1120
1121         initTransInfo(C, t, NULL, NULL);        // internal data, mouse, vectors
1122
1123         createTransData(C, t);                          // make TransData structs from selection
1124
1125         t->around = centerMode;                         // override userdefined mode
1126
1127         if (t->total == 0) {
1128                 success = 0;
1129         }
1130         else {
1131                 success = 1;
1132
1133                 calculateCenter(t);
1134
1135                 // Copy center from constraint center. Transform center can be local
1136                 VECCOPY(vec, t->con.center);
1137         }
1138
1139
1140         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1141         special_aftertrans_update(C, t);
1142
1143         postTrans(C, t);
1144
1145         MEM_freeN(t);
1146
1147         return success;
1148 }
1149
1150 typedef enum {
1151         UP,
1152         DOWN,
1153         LEFT,
1154         RIGHT
1155 } ArrowDirection;
1156 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1157 {
1158         switch(d)
1159         {
1160                 case LEFT:
1161                         offset = -offset;
1162                         length = -length;
1163                         size = -size;
1164                 case RIGHT:
1165                         glBegin(GL_LINES);
1166                         glVertex2s( offset, 0);
1167                         glVertex2s( offset + length, 0);
1168                         glVertex2s( offset + length, 0);
1169                         glVertex2s( offset + length - size, -size);
1170                         glVertex2s( offset + length, 0);
1171                         glVertex2s( offset + length - size,  size);
1172                         glEnd();
1173                         break;
1174                 case DOWN:
1175                         offset = -offset;
1176                         length = -length;
1177                         size = -size;
1178                 case UP:
1179                         glBegin(GL_LINES);
1180                         glVertex2s( 0, offset);
1181                         glVertex2s( 0, offset + length);
1182                         glVertex2s( 0, offset + length);
1183                         glVertex2s(-size, offset + length - size);
1184                         glVertex2s( 0, offset + length);
1185                         glVertex2s( size, offset + length - size);
1186                         glEnd();
1187                         break;
1188         }
1189 }
1190
1191 static void drawArrowHead(ArrowDirection d, short size)
1192 {
1193         switch(d)
1194         {
1195                 case LEFT:
1196                         size = -size;
1197                 case RIGHT:
1198                         glBegin(GL_LINES);
1199                         glVertex2s( 0, 0);
1200                         glVertex2s( -size, -size);
1201                         glVertex2s( 0, 0);
1202                         glVertex2s( -size,  size);
1203                         glEnd();
1204                         break;
1205                 case DOWN:
1206                         size = -size;
1207                 case UP:
1208                         glBegin(GL_LINES);
1209                         glVertex2s( 0, 0);
1210                         glVertex2s(-size, -size);
1211                         glVertex2s( 0, 0);
1212                         glVertex2s( size, -size);
1213                         glEnd();
1214                         break;
1215         }
1216 }
1217
1218 static void drawArc(float size, float angle_start, float angle_end, int segments)
1219 {
1220         float delta = (angle_end - angle_start) / segments;
1221         float angle;
1222
1223         glBegin(GL_LINE_STRIP);
1224
1225         for( angle = angle_start; angle < angle_end; angle += delta)
1226         {
1227                 glVertex2f( cosf(angle) * size, sinf(angle) * size);
1228         }
1229         glVertex2f( cosf(angle_end) * size, sinf(angle_end) * size);
1230
1231         glEnd();
1232 }
1233
1234 static int helpline_poll(bContext *C)
1235 {
1236         ARegion *ar= CTX_wm_region(C);
1237         
1238         if(ar && ar->regiontype==RGN_TYPE_WINDOW)
1239                 return 1;
1240         return 0;
1241 }
1242
1243 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1244 {
1245         TransInfo *t = (TransInfo*)customdata;
1246
1247         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR))
1248         {
1249                 float vecrot[3], cent[2];
1250                 int mval[2];
1251
1252                 mval[0]= x;
1253                 mval[1]= y;
1254
1255                 VECCOPY(vecrot, t->center);
1256                 if(t->flag & T_EDIT) {
1257                         Object *ob= t->obedit;
1258                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1259                 }
1260                 else if(t->flag & T_POSE) {
1261                         Object *ob=t->poseobj;
1262                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1263                 }
1264
1265                 projectFloatView(t, vecrot, cent);      // no overflow in extreme cases
1266
1267                 glPushMatrix();
1268
1269                 switch(t->helpline)
1270                 {
1271                         case HLP_SPRING:
1272                                 UI_ThemeColor(TH_WIRE);
1273
1274                                 setlinestyle(3);
1275                                 glBegin(GL_LINE_STRIP);
1276                                 glVertex2iv(t->mval);
1277                                 glVertex2fv(cent);
1278                                 glEnd();
1279
1280                                 glTranslatef(mval[0], mval[1], 0);
1281                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1282
1283                                 setlinestyle(0);
1284                                 glLineWidth(3.0);
1285                                 drawArrow(UP, 5, 10, 5);
1286                                 drawArrow(DOWN, 5, 10, 5);
1287                                 glLineWidth(1.0);
1288                                 break;
1289                         case HLP_HARROW:
1290                                 UI_ThemeColor(TH_WIRE);
1291
1292                                 glTranslatef(mval[0], mval[1], 0);
1293
1294                                 glLineWidth(3.0);
1295                                 drawArrow(RIGHT, 5, 10, 5);
1296                                 drawArrow(LEFT, 5, 10, 5);
1297                                 glLineWidth(1.0);
1298                                 break;
1299                         case HLP_VARROW:
1300                                 UI_ThemeColor(TH_WIRE);
1301
1302                                 glTranslatef(mval[0], mval[1], 0);
1303
1304                                 glLineWidth(3.0);
1305                                 glBegin(GL_LINES);
1306                                 drawArrow(UP, 5, 10, 5);
1307                                 drawArrow(DOWN, 5, 10, 5);
1308                                 glLineWidth(1.0);
1309                                 break;
1310                         case HLP_ANGLE:
1311                                 {
1312                                         float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1313                                         float angle = atan2f(dy, dx);
1314                                         float dist = sqrtf(dx*dx + dy*dy);
1315                                         float delta_angle = MIN2(15.0f / dist, (float)M_PI/4.0f);
1316                                         float spacing_angle = MIN2(5.0f / dist, (float)M_PI/12.0f);
1317                                         UI_ThemeColor(TH_WIRE);
1318
1319                                         setlinestyle(3);
1320                                         glBegin(GL_LINE_STRIP);
1321                                         glVertex2iv(t->mval);
1322                                         glVertex2fv(cent);
1323                                         glEnd();
1324
1325                                         glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1326
1327                                         setlinestyle(0);
1328                                         glLineWidth(3.0);
1329                                         drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1330                                         drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1331
1332                                         glPushMatrix();
1333
1334                                         glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1335                                         glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1336
1337                                         drawArrowHead(DOWN, 5);
1338
1339                                         glPopMatrix();
1340
1341                                         glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1342                                         glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1343
1344                                         drawArrowHead(UP, 5);
1345
1346                                         glLineWidth(1.0);
1347                                         break;
1348                                 }
1349                                 case HLP_TRACKBALL:
1350                                 {
1351                                         unsigned char col[3], col2[3];
1352                                         UI_GetThemeColor3ubv(TH_GRID, col);
1353
1354                                         glTranslatef(mval[0], mval[1], 0);
1355
1356                                         glLineWidth(3.0);
1357
1358                                         UI_make_axis_color(col, col2, 'X');
1359                                         glColor3ubv((GLubyte *)col2);
1360
1361                                         drawArrow(RIGHT, 5, 10, 5);
1362                                         drawArrow(LEFT, 5, 10, 5);
1363
1364                                         UI_make_axis_color(col, col2, 'Y');
1365                                         glColor3ubv((GLubyte *)col2);
1366
1367                                         drawArrow(UP, 5, 10, 5);
1368                                         drawArrow(DOWN, 5, 10, 5);
1369                                         glLineWidth(1.0);
1370                                         break;
1371                                 }
1372                 }
1373
1374                 glPopMatrix();
1375         }
1376 }
1377
1378 static void drawTransformView(const struct bContext *C, struct ARegion *UNUSED(ar), void *arg)
1379 {
1380         TransInfo *t = arg;
1381
1382         drawConstraint(t);
1383         drawPropCircle(C, t);
1384         drawSnapping(C, t);
1385 }
1386
1387 #if 0
1388 static void drawTransformPixel(const struct bContext *UNUSED(C), struct ARegion *UNUSED(ar), void *UNUSED(arg))
1389 {
1390 //      TransInfo *t = arg;
1391 //
1392 //      drawHelpline(C, t->mval[0], t->mval[1], t);
1393 }
1394 #endif
1395
1396 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1397 {
1398         ToolSettings *ts = CTX_data_tool_settings(C);
1399         int constraint_axis[3] = {0, 0, 0};
1400         int proportional = 0;
1401
1402         if (RNA_struct_find_property(op->ptr, "value"))
1403         {
1404                 if (t->flag & T_AUTOVALUES)
1405                 {
1406                         RNA_float_set_array(op->ptr, "value", t->auto_values);
1407                 }
1408                 else
1409                 {
1410                         RNA_float_set_array(op->ptr, "value", t->values);
1411                 }
1412         }
1413
1414         /* convert flag to enum */
1415         switch(t->flag & (T_PROP_EDIT|T_PROP_CONNECTED))
1416         {
1417         case (T_PROP_EDIT|T_PROP_CONNECTED):
1418                 proportional = PROP_EDIT_CONNECTED;
1419                 break;
1420         case T_PROP_EDIT:
1421                 proportional = PROP_EDIT_ON;
1422                 break;
1423         default:
1424                 proportional = PROP_EDIT_OFF;
1425         }
1426
1427         // If modal, save settings back in scene if not set as operator argument
1428         if (t->flag & T_MODAL) {
1429
1430                 /* save settings if not set in operator */
1431                 if (RNA_struct_find_property(op->ptr, "proportional") && !RNA_property_is_set(op->ptr, "proportional")) {
1432                         if (t->obedit)
1433                                 ts->proportional = proportional;
1434                         else
1435                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1436                 }
1437
1438                 if (RNA_struct_find_property(op->ptr, "proportional_size") && !RNA_property_is_set(op->ptr, "proportional_size")) {
1439                         ts->proportional_size = t->prop_size;
1440                 }
1441                         
1442                 if (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") && !RNA_property_is_set(op->ptr, "proportional_edit_falloff")) {
1443                         ts->prop_mode = t->prop_mode;
1444                 }
1445                 
1446                 /* do we check for parameter? */
1447                 if (t->modifiers & MOD_SNAP) {
1448                         ts->snap_flag |= SCE_SNAP;
1449                 } else {
1450                         ts->snap_flag &= ~SCE_SNAP;
1451                 }
1452
1453                 if(t->spacetype == SPACE_VIEW3D) {
1454                         if (RNA_struct_find_property(op->ptr, "constraint_orientation") && !RNA_property_is_set(op->ptr, "constraint_orientation")) {
1455                                 View3D *v3d = t->view;
1456         
1457                                 v3d->twmode = t->current_orientation;
1458                         }
1459                 }
1460         }
1461         
1462         if (RNA_struct_find_property(op->ptr, "proportional"))
1463         {
1464                 RNA_enum_set(op->ptr, "proportional", proportional);
1465                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1466                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1467         }
1468
1469         if (RNA_struct_find_property(op->ptr, "axis"))
1470         {
1471                 RNA_float_set_array(op->ptr, "axis", t->axis);
1472         }
1473
1474         if (RNA_struct_find_property(op->ptr, "mirror"))
1475         {
1476                 RNA_boolean_set(op->ptr, "mirror", t->flag & T_MIRROR);
1477         }
1478
1479         if (RNA_struct_find_property(op->ptr, "constraint_axis"))
1480         {
1481                 /* constraint orientation can be global, event if user selects something else
1482                  * so use the orientation in the constraint if set
1483                  * */
1484                 if (t->con.mode & CON_APPLY) {
1485                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1486                 } else {
1487                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1488                 }
1489
1490                 if (t->con.mode & CON_APPLY)
1491                 {
1492                         if (t->con.mode & CON_AXIS0) {
1493                                 constraint_axis[0] = 1;
1494                         }
1495                         if (t->con.mode & CON_AXIS1) {
1496                                 constraint_axis[1] = 1;
1497                         }
1498                         if (t->con.mode & CON_AXIS2) {
1499                                 constraint_axis[2] = 1;
1500                         }
1501                 }
1502
1503                 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1504         }
1505 }
1506
1507 /* note: caller needs to free 't' on a 0 return */
1508 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1509 {
1510         int options = 0;
1511
1512         t->context = C;
1513
1514         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1515
1516         t->state = TRANS_STARTING;
1517
1518         if(RNA_struct_find_property(op->ptr, "texture_space"))
1519                 if(RNA_boolean_get(op->ptr, "texture_space"))
1520                         options |= CTX_TEXTURE;
1521         
1522         t->options = options;
1523
1524         t->mode = mode;
1525
1526         t->launch_event = event ? event->type : -1;
1527
1528         if (t->launch_event == EVT_TWEAK_R)
1529         {
1530                 t->launch_event = RIGHTMOUSE;
1531         }
1532         else if (t->launch_event == EVT_TWEAK_L)
1533         {
1534                 t->launch_event = LEFTMOUSE;
1535         }
1536
1537         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1538         // For manipulator only, so assume LEFTMOUSE
1539         if (t->launch_event == 0)
1540         {
1541                 t->launch_event = LEFTMOUSE;
1542         }
1543
1544         if (!initTransInfo(C, t, op, event))                                    // internal data, mouse, vectors
1545         {
1546                 return 0;
1547         }
1548
1549         if(t->spacetype == SPACE_VIEW3D)
1550         {
1551                 //calc_manipulator_stats(curarea);
1552                 initTransformOrientation(C, t);
1553
1554                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1555                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1556                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1557                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1558         }
1559         else if(t->spacetype == SPACE_IMAGE) {
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->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1563         }
1564         else if(t->spacetype == SPACE_CLIP) {
1565                 unit_m3(t->spacemtx);
1566                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1567                 t->options |= CTX_MOVIECLIP;
1568         }
1569         else
1570                 unit_m3(t->spacemtx);
1571
1572         createTransData(C, t);                  // make TransData structs from selection
1573
1574         if (t->total == 0) {
1575                 postTrans(C, t);
1576                 return 0;
1577         }
1578
1579         /* Stupid code to have Ctrl-Click on manipulator work ok */
1580         if(event)
1581         {
1582                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1583                 wmKeyMapItem *kmi;
1584
1585                 for (kmi = keymap->items.first; kmi; kmi = kmi->next)
1586                 {
1587                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
1588                         {
1589                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
1590                                         (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1591                                         (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
1592                                         (kmi->type == OSKEY && event->oskey)) {
1593                                         t->modifiers |= MOD_SNAP_INVERT;
1594                                 }
1595                                 break;
1596                         }
1597                 }
1598
1599         }
1600
1601         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1602
1603         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1604         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1605         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1606         mode = t->mode;
1607
1608         calculatePropRatio(t);
1609         calculateCenter(t);
1610
1611         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1612
1613         switch (mode) {
1614         case TFM_TRANSLATION:
1615                 initTranslation(t);
1616                 break;
1617         case TFM_ROTATION:
1618                 initRotation(t);
1619                 break;
1620         case TFM_RESIZE:
1621                 initResize(t);
1622                 break;
1623         case TFM_TOSPHERE:
1624                 initToSphere(t);
1625                 break;
1626         case TFM_SHEAR:
1627                 initShear(t);
1628                 break;
1629         case TFM_WARP:
1630                 initWarp(t);
1631                 break;
1632         case TFM_SHRINKFATTEN:
1633                 initShrinkFatten(t);
1634                 break;
1635         case TFM_TILT:
1636                 initTilt(t);
1637                 break;
1638         case TFM_CURVE_SHRINKFATTEN:
1639                 initCurveShrinkFatten(t);
1640                 break;
1641         case TFM_TRACKBALL:
1642                 initTrackball(t);
1643                 break;
1644         case TFM_PUSHPULL:
1645                 initPushPull(t);
1646                 break;
1647         case TFM_CREASE:
1648                 initCrease(t);
1649                 break;
1650         case TFM_BONESIZE:
1651                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1652                         bArmature *arm= t->poseobj->data;
1653                         if(arm->drawtype==ARM_ENVELOPE)
1654                                 initBoneEnvelope(t);
1655                         else
1656                                 initBoneSize(t);
1657                 }
1658                 break;
1659         case TFM_BONE_ENVELOPE:
1660                 initBoneEnvelope(t);
1661                 break;
1662         case TFM_EDGE_SLIDE:
1663                 initEdgeSlide(t);
1664                 break;
1665         case TFM_BONE_ROLL:
1666                 initBoneRoll(t);
1667                 break;
1668         case TFM_TIME_TRANSLATE:
1669                 initTimeTranslate(t);
1670                 break;
1671         case TFM_TIME_SLIDE:
1672                 initTimeSlide(t);
1673                 break;
1674         case TFM_TIME_SCALE:
1675                 initTimeScale(t);
1676                 break;
1677         case TFM_TIME_DUPLICATE:
1678                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1679                  * so that duplicate-culling will work properly
1680                  */
1681                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1682                         initTranslation(t);
1683                 else
1684                         initTimeTranslate(t);
1685                 t->mode = mode;
1686                 break;
1687         case TFM_TIME_EXTEND:
1688                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1689                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1690                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1691                  * depending on which editor this was called from
1692                  */
1693                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1694                         initTranslation(t);
1695                 else
1696                         initTimeTranslate(t);
1697                 break;
1698         case TFM_BAKE_TIME:
1699                 initBakeTime(t);
1700                 break;
1701         case TFM_MIRROR:
1702                 initMirror(t);
1703                 break;
1704         case TFM_BEVEL:
1705                 initBevel(t);
1706                 break;
1707         case TFM_BWEIGHT:
1708                 initBevelWeight(t);
1709                 break;
1710         case TFM_ALIGN:
1711                 initAlign(t);
1712                 break;
1713         case TFM_SEQ_SLIDE:
1714                 initSeqSlide(t);
1715                 break;
1716         }
1717
1718         if(t->state == TRANS_CANCEL)
1719         {
1720                 postTrans(C, t);
1721                 return 0;
1722         }
1723
1724
1725         /* overwrite initial values if operator supplied a non-null vector */
1726         if (RNA_property_is_set(op->ptr, "value"))
1727         {
1728                 float values[4]= {0}; /* incase value isn't length 4, avoid uninitialized memory  */
1729                 RNA_float_get_array(op->ptr, "value", values);
1730                 QUATCOPY(t->values, values);
1731                 QUATCOPY(t->auto_values, values);
1732                 t->flag |= T_AUTOVALUES;
1733         }
1734
1735         /* Transformation axis from operator */
1736         if (RNA_struct_find_property(op->ptr, "axis") && RNA_property_is_set(op->ptr, "axis"))
1737         {
1738                 RNA_float_get_array(op->ptr, "axis", t->axis);
1739                 normalize_v3(t->axis);
1740                 copy_v3_v3(t->axis_orig, t->axis);
1741         }
1742
1743         /* Constraint init from operator */
1744         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1745         {
1746                 int constraint_axis[3];
1747
1748                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1749
1750                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1751                 {
1752                         t->con.mode |= CON_APPLY;
1753
1754                         if (constraint_axis[0]) {
1755                                 t->con.mode |= CON_AXIS0;
1756                         }
1757                         if (constraint_axis[1]) {
1758                                 t->con.mode |= CON_AXIS1;
1759                         }
1760                         if (constraint_axis[2]) {
1761                                 t->con.mode |= CON_AXIS2;
1762                         }
1763
1764                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1765                 }
1766         }
1767
1768         t->context = NULL;
1769
1770         return 1;
1771 }
1772
1773 void transformApply(bContext *C, TransInfo *t)
1774 {
1775         t->context = C;
1776
1777         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT)))
1778         {
1779                 selectConstraint(t);
1780                 if (t->transform) {
1781                         t->transform(t, t->mval);  // calls recalcData()
1782                         viewRedrawForce(C, t);
1783                 }
1784                 t->redraw = TREDRAW_NOTHING;
1785         } else if (t->redraw & TREDRAW_SOFT) {
1786                 viewRedrawForce(C, t);
1787         }
1788
1789         /* If auto confirm is on, break after one pass */
1790         if (t->options & CTX_AUTOCONFIRM)
1791         {
1792                 t->state = TRANS_CONFIRM;
1793         }
1794
1795         if (BKE_ptcache_get_continue_physics())
1796         {
1797                 // TRANSFORM_FIX_ME
1798                 //do_screenhandlers(G.curscreen);
1799                 t->redraw |= TREDRAW_HARD;
1800         }
1801
1802         t->context = NULL;
1803 }
1804
1805 void drawTransformApply(const bContext *C, struct ARegion *UNUSED(ar), void *arg)
1806 {
1807         TransInfo *t = arg;
1808
1809         if (t->redraw & TREDRAW_SOFT) {
1810                 t->redraw |= TREDRAW_HARD;
1811                 transformApply((bContext *)C, t);
1812         }
1813 }
1814
1815 int transformEnd(bContext *C, TransInfo *t)
1816 {
1817         int exit_code = OPERATOR_RUNNING_MODAL;
1818
1819         t->context = C;
1820
1821         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING)
1822         {
1823                 /* handle restoring objects */
1824                 if(t->state == TRANS_CANCEL)
1825                 {
1826                         /* exception, edge slide transformed UVs too */
1827                         if(t->mode==TFM_EDGE_SLIDE)
1828                                 doEdgeSlide(t, 0.0f);
1829                         
1830                         exit_code = OPERATOR_CANCELLED;
1831                         restoreTransObjects(t); // calls recalcData()
1832                 }
1833                 else
1834                 {
1835                         exit_code = OPERATOR_FINISHED;
1836                 }
1837
1838                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1839                 special_aftertrans_update(C, t);
1840
1841                 /* free data */
1842                 postTrans(C, t);
1843
1844                 /* send events out for redraws */
1845                 viewRedrawPost(C, t);
1846
1847                 /*  Undo as last, certainly after special_trans_update! */
1848
1849                 if(t->state == TRANS_CANCEL) {
1850 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1851                 }
1852                 else {
1853 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1854 //                      else ED_undo_push(C, transform_to_undostr(t));
1855                 }
1856                 t->undostr= NULL;
1857
1858                 viewRedrawForce(C, t);
1859         }
1860
1861         t->context = NULL;
1862
1863         return exit_code;
1864 }
1865
1866 /* ************************** TRANSFORM LOCKS **************************** */
1867
1868 static void protectedTransBits(short protectflag, float *vec)
1869 {
1870         if(protectflag & OB_LOCK_LOCX)
1871                 vec[0]= 0.0f;
1872         if(protectflag & OB_LOCK_LOCY)
1873                 vec[1]= 0.0f;
1874         if(protectflag & OB_LOCK_LOCZ)
1875                 vec[2]= 0.0f;
1876 }
1877
1878 static void protectedSizeBits(short protectflag, float *size)
1879 {
1880         if(protectflag & OB_LOCK_SCALEX)
1881                 size[0]= 1.0f;
1882         if(protectflag & OB_LOCK_SCALEY)
1883                 size[1]= 1.0f;
1884         if(protectflag & OB_LOCK_SCALEZ)
1885                 size[2]= 1.0f;
1886 }
1887
1888 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1889 {
1890         if(protectflag & OB_LOCK_ROTX)
1891                 eul[0]= oldeul[0];
1892         if(protectflag & OB_LOCK_ROTY)
1893                 eul[1]= oldeul[1];
1894         if(protectflag & OB_LOCK_ROTZ)
1895                 eul[2]= oldeul[2];
1896 }
1897
1898
1899 /* this function only does the delta rotation */
1900 /* axis-angle is usually internally stored as quats... */
1901 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1902 {
1903         /* check that protection flags are set */
1904         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1905                 return;
1906         
1907         if (protectflag & OB_LOCK_ROT4D) {
1908                 /* axis-angle getting limited as 4D entities that they are... */
1909                 if (protectflag & OB_LOCK_ROTW)
1910                         *angle= oldAngle;
1911                 if (protectflag & OB_LOCK_ROTX)
1912                         axis[0]= oldAxis[0];
1913                 if (protectflag & OB_LOCK_ROTY)
1914                         axis[1]= oldAxis[1];
1915                 if (protectflag & OB_LOCK_ROTZ)
1916                         axis[2]= oldAxis[2];
1917         }
1918         else {
1919                 /* axis-angle get limited with euler... */
1920                 float eul[3], oldeul[3];
1921                 
1922                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1923                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1924                 
1925                 if (protectflag & OB_LOCK_ROTX)
1926                         eul[0]= oldeul[0];
1927                 if (protectflag & OB_LOCK_ROTY)
1928                         eul[1]= oldeul[1];
1929                 if (protectflag & OB_LOCK_ROTZ)
1930                         eul[2]= oldeul[2];
1931                 
1932                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1933                 
1934                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1935                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1936                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1937                         axis[1]= 1.0f;
1938                 }
1939         }
1940 }
1941
1942 /* this function only does the delta rotation */
1943 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1944 {
1945         /* check that protection flags are set */
1946         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1947                 return;
1948         
1949         if (protectflag & OB_LOCK_ROT4D) {
1950                 /* quaternions getting limited as 4D entities that they are... */
1951                 if (protectflag & OB_LOCK_ROTW)
1952                         quat[0]= oldquat[0];
1953                 if (protectflag & OB_LOCK_ROTX)
1954                         quat[1]= oldquat[1];
1955                 if (protectflag & OB_LOCK_ROTY)
1956                         quat[2]= oldquat[2];
1957                 if (protectflag & OB_LOCK_ROTZ)
1958                         quat[3]= oldquat[3];
1959         }
1960         else {
1961                 /* quaternions get limited with euler... (compatability mode) */
1962                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1963                 float qlen;
1964
1965                 qlen= normalize_qt_qt(nquat, quat);
1966                 normalize_qt_qt(noldquat, oldquat);
1967
1968                 quat_to_eul(eul, nquat);
1969                 quat_to_eul(oldeul, noldquat);
1970
1971                 if (protectflag & OB_LOCK_ROTX)
1972                         eul[0]= oldeul[0];
1973                 if (protectflag & OB_LOCK_ROTY)
1974                         eul[1]= oldeul[1];
1975                 if (protectflag & OB_LOCK_ROTZ)
1976                         eul[2]= oldeul[2];
1977
1978                 eul_to_quat( quat,eul);
1979
1980                 /* restore original quat size */
1981                 mul_qt_fl(quat, qlen);
1982                 
1983                 /* quaternions flip w sign to accumulate rotations correctly */
1984                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
1985                         mul_qt_fl(quat, -1.0f);
1986                 }
1987         }
1988 }
1989
1990 /* ******************* TRANSFORM LIMITS ********************** */
1991
1992 static void constraintTransLim(TransInfo *UNUSED(t), TransData *td)
1993 {
1994         if (td->con) {
1995                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
1996                 bConstraintOb cob= {NULL};
1997                 bConstraint *con;
1998                 
1999                 /* Make a temporary bConstraintOb for using these limit constraints
2000                  *      - they only care that cob->matrix is correctly set ;-)
2001                  *      - current space should be local
2002                  */
2003                 unit_m4(cob.matrix);
2004                 VECCOPY(cob.matrix[3], td->loc);
2005                 
2006                 /* Evaluate valid constraints */
2007                 for (con= td->con; con; con= con->next) {
2008                         float tmat[4][4];
2009                         
2010                         /* only consider constraint if enabled */
2011                         if (con->flag & CONSTRAINT_DISABLE) continue;
2012                         if (con->enforce == 0.0f) continue;
2013                         
2014                         /* only use it if it's tagged for this purpose (and the right type) */
2015                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2016                                 bLocLimitConstraint *data= con->data;
2017                                 
2018                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2019                                         continue;
2020                                 
2021                                 /* do space conversions */
2022                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2023                                         /* just multiply by td->mtx (this should be ok) */
2024                                         copy_m4_m4(tmat, cob.matrix);
2025                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2026                                 }
2027                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2028                                         /* skip... incompatable spacetype */
2029                                         continue;
2030                                 }
2031                                 
2032                                 /* do constraint */
2033                                 cti->evaluate_constraint(con, &cob, NULL);
2034                                 
2035                                 /* convert spaces again */
2036                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2037                                         /* just multiply by td->mtx (this should be ok) */
2038                                         copy_m4_m4(tmat, cob.matrix);
2039                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2040                                 }
2041                         }
2042                 }
2043                 
2044                 /* copy results from cob->matrix */
2045                 VECCOPY(td->loc, cob.matrix[3]);
2046         }
2047 }
2048
2049 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2050 {
2051         /* Make a temporary bConstraintOb for use by limit constraints
2052          *      - they only care that cob->matrix is correctly set ;-)
2053          *      - current space should be local
2054          */
2055         memset(cob, 0, sizeof(bConstraintOb));
2056         if (td->ext)
2057         {
2058                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2059                         /* quats */
2060                         /* objects and bones do normalization first too, otherwise
2061                            we don't necessarily end up with a rotation matrix, and
2062                            then conversion back to quat gives a different result */
2063                         float quat[4];
2064                         normalize_qt_qt(quat, td->ext->quat);
2065                         quat_to_mat4(cob->matrix, quat);
2066                 }
2067                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2068                         /* axis angle */
2069                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2070                 }
2071                 else {
2072                         /* eulers */
2073                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2074                 }
2075         }
2076 }
2077
2078 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2079 {
2080         if (td->con) {
2081                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2082                 bConstraintOb cob;
2083                 bConstraint *con;
2084                 int dolimit = 0;
2085                 
2086                 /* Evaluate valid constraints */
2087                 for (con= td->con; con; con= con->next) {
2088                         /* only consider constraint if enabled */
2089                         if (con->flag & CONSTRAINT_DISABLE) continue;
2090                         if (con->enforce == 0.0f) continue;
2091                         
2092                         /* we're only interested in Limit-Rotation constraints */
2093                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2094                                 bRotLimitConstraint *data= con->data;
2095                                 float tmat[4][4];
2096                                 
2097                                 /* only use it if it's tagged for this purpose */
2098                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2099                                         continue;
2100
2101                                 /* skip incompatable spacetypes */
2102                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2103                                         continue;
2104
2105                                 /* only do conversion if necessary, to preserve quats and eulers */
2106                                 if(!dolimit) {
2107                                         constraintob_from_transdata(&cob, td);
2108                                         dolimit= 1;
2109                                 }
2110                                 
2111                                 /* do space conversions */
2112                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2113                                         /* just multiply by td->mtx (this should be ok) */
2114                                         copy_m4_m4(tmat, cob.matrix);
2115                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2116                                 }
2117                                 
2118                                 /* do constraint */
2119                                 cti->evaluate_constraint(con, &cob, NULL);
2120                                 
2121                                 /* convert spaces again */
2122                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2123                                         /* just multiply by td->mtx (this should be ok) */
2124                                         copy_m4_m4(tmat, cob.matrix);
2125                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2126                                 }
2127                         }
2128                 }
2129                 
2130                 if(dolimit) {
2131                         /* copy results from cob->matrix */
2132                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2133                                 /* quats */
2134                                 mat4_to_quat( td->ext->quat,cob.matrix);
2135                         }
2136                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2137                                 /* axis angle */
2138                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2139                         }
2140                         else {
2141                                 /* eulers */
2142                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2143                         }
2144                 }
2145         }
2146 }
2147
2148 static void constraintSizeLim(TransInfo *t, TransData *td)
2149 {
2150         if (td->con && td->ext) {
2151                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2152                 bConstraintOb cob= {NULL};
2153                 bConstraint *con;
2154                 
2155                 /* Make a temporary bConstraintOb for using these limit constraints
2156                  *      - they only care that cob->matrix is correctly set ;-)
2157                  *      - current space should be local
2158                  */
2159                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2160                         /* scale val and reset size */
2161                         return; // TODO: fix this case
2162                 }
2163                 else {
2164                         /* Reset val if SINGLESIZE but using a constraint */
2165                         if (td->flag & TD_SINGLESIZE)
2166                                 return;
2167                         
2168                         size_to_mat4( cob.matrix,td->ext->size);
2169                 }
2170                 
2171                 /* Evaluate valid constraints */
2172                 for (con= td->con; con; con= con->next) {
2173                         /* only consider constraint if enabled */
2174                         if (con->flag & CONSTRAINT_DISABLE) continue;
2175                         if (con->enforce == 0.0f) continue;
2176                         
2177                         /* we're only interested in Limit-Scale constraints */
2178                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2179                                 bSizeLimitConstraint *data= con->data;
2180                                 float tmat[4][4];
2181                                 
2182                                 /* only use it if it's tagged for this purpose */
2183                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2184                                         continue;
2185                                 
2186                                 /* do space conversions */
2187                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2188                                         /* just multiply by td->mtx (this should be ok) */
2189                                         copy_m4_m4(tmat, cob.matrix);
2190                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2191                                 }
2192                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2193                                         /* skip... incompatable spacetype */
2194                                         continue;
2195                                 }
2196                                 
2197                                 /* do constraint */
2198                                 cti->evaluate_constraint(con, &cob, NULL);
2199                                 
2200                                 /* convert spaces again */
2201                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2202                                         /* just multiply by td->mtx (this should be ok) */
2203                                         copy_m4_m4(tmat, cob.matrix);
2204                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2205                                 }
2206                         }
2207                 }
2208                 
2209                 /* copy results from cob->matrix */
2210                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2211                         /* scale val and reset size */
2212                         return; // TODO: fix this case
2213                 }
2214                 else {
2215                         /* Reset val if SINGLESIZE but using a constraint */
2216                         if (td->flag & TD_SINGLESIZE)
2217                                 return;
2218                         
2219                         mat4_to_size( td->ext->size,cob.matrix);
2220                 }
2221         }
2222 }
2223
2224 /* ************************** WARP *************************** */
2225
2226 static void postInputWarp(TransInfo *t, float values[3])
2227 {
2228         mul_v3_fl(values, (float)(M_PI * 2));
2229
2230         if (t->customData) /* non-null value indicates reversed input */
2231         {
2232                 negate_v3(values);
2233         }
2234 }
2235
2236 void initWarp(TransInfo *t)
2237 {
2238         float max[3], min[3];
2239         int i;
2240         
2241         t->mode = TFM_WARP;
2242         t->transform = Warp;
2243         t->handleEvent = handleEventWarp;
2244         
2245         setInputPostFct(&t->mouse, postInputWarp);
2246         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2247         
2248         t->idx_max = 0;
2249         t->num.idx_max = 0;
2250         t->snap[0] = 0.0f;
2251         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2252         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2253         
2254         t->num.increment = 1.0f;
2255
2256         t->flag |= T_NO_CONSTRAINT;
2257         
2258         /* we need min/max in view space */
2259         for(i = 0; i < t->total; i++) {
2260                 float center[3];
2261                 copy_v3_v3(center, t->data[i].center);
2262                 mul_m3_v3(t->data[i].mtx, center);
2263                 mul_m4_v3(t->viewmat, center);
2264                 sub_v3_v3(center, t->viewmat[3]);
2265                 if (i)
2266                         minmax_v3v3_v3(min, max, center);
2267                 else {
2268                         copy_v3_v3(max, center);
2269                         copy_v3_v3(min, center);
2270                 }
2271         }
2272
2273         mid_v3_v3v3(t->center, min, max);
2274
2275         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2276         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2277 }
2278
2279 int handleEventWarp(TransInfo *t, wmEvent *event)
2280 {
2281         int status = 0;
2282         
2283         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2284         {
2285                 // Use customData pointer to signal warp direction
2286                 if      (t->customData == NULL)
2287                         t->customData = (void*)1;
2288                 else
2289                         t->customData = NULL;
2290                 
2291                 status = 1;
2292         }
2293         
2294         return status;
2295 }
2296
2297 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2298 {
2299         TransData *td = t->data;
2300         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2301         int i;
2302         char str[50];
2303         
2304         curs= give_cursor(t->scene, t->view);
2305         /*
2306          * gcursor is the one used for helpline.
2307          * It has to be in the same space as the drawing loop
2308          * (that means it needs to be in the object's space when in edit mode and
2309          *  in global space in object mode)
2310          *
2311          * cursor is used for calculations.
2312          * It needs to be in view space, but we need to take object's offset
2313          * into account if in Edit mode.
2314          */
2315         VECCOPY(cursor, curs);
2316         VECCOPY(gcursor, cursor);
2317         if (t->flag & T_EDIT) {
2318                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2319                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2320                 mul_m3_v3(t->data->smtx, gcursor);
2321         }
2322         mul_m4_v3(t->viewmat, cursor);
2323         sub_v3_v3(cursor, t->viewmat[3]);
2324         
2325         /* amount of radians for warp */
2326         circumfac = t->values[0];
2327         
2328         snapGrid(t, &circumfac);
2329         applyNumInput(&t->num, &circumfac);
2330         
2331         /* header print for NumInput */
2332         if (hasNumInput(&t->num)) {
2333                 char c[20];
2334                 
2335                 outputNumInput(&(t->num), c);
2336                 
2337                 sprintf(str, "Warp: %s", c);
2338
2339                 circumfac = DEG2RADF(circumfac);
2340         }
2341         else {
2342                 /* default header print */
2343                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2344         }
2345         
2346         t->values[0] = circumfac;
2347
2348         circumfac /= 2; /* only need 180 on each side to make 360 */
2349         
2350         for(i = 0; i < t->total; i++, td++) {
2351                 float loc[3];
2352                 if (td->flag & TD_NOACTION)
2353                         break;
2354                 
2355                 if (td->flag & TD_SKIP)
2356                         continue;
2357                 
2358                 /* translate point to center, rotate in such a way that outline==distance */
2359                 VECCOPY(vec, td->iloc);
2360                 mul_m3_v3(td->mtx, vec);
2361                 mul_m4_v3(t->viewmat, vec);
2362                 sub_v3_v3(vec, t->viewmat[3]);
2363                 
2364                 dist= vec[0]-cursor[0];
2365                 
2366                 /* t->val is X dimension projected boundbox */
2367                 phi0= (circumfac*dist/t->val);
2368                 
2369                 vec[1]= (vec[1]-cursor[1]);
2370                 
2371                 co= (float)cos(phi0);
2372                 si= (float)sin(phi0);
2373                 loc[0]= -si*vec[1]+cursor[0];
2374                 loc[1]= co*vec[1]+cursor[1];
2375                 loc[2]= vec[2];
2376                 
2377                 mul_m4_v3(t->viewinv, loc);
2378                 sub_v3_v3(loc, t->viewinv[3]);
2379                 mul_m3_v3(td->smtx, loc);
2380                 
2381                 sub_v3_v3(loc, td->iloc);
2382                 mul_v3_fl(loc, td->factor);
2383                 add_v3_v3v3(td->loc, td->iloc, loc);
2384         }
2385         
2386         recalcData(t);
2387         
2388         ED_area_headerprint(t->sa, str);
2389         
2390         return 1;
2391 }
2392
2393 /* ************************** SHEAR *************************** */
2394
2395 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2396 {
2397         mul_v3_fl(values, 0.05f);
2398 }
2399
2400 void initShear(TransInfo *t)
2401 {
2402         t->mode = TFM_SHEAR;
2403         t->transform = Shear;
2404         t->handleEvent = handleEventShear;
2405         
2406         setInputPostFct(&t->mouse, postInputShear);
2407         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2408         
2409         t->idx_max = 0;
2410         t->num.idx_max = 0;
2411         t->snap[0] = 0.0f;
2412         t->snap[1] = 0.1f;
2413         t->snap[2] = t->snap[1] * 0.1f;
2414         
2415         t->num.increment = 0.1f;
2416
2417         t->flag |= T_NO_CONSTRAINT;
2418 }
2419
2420 int handleEventShear(TransInfo *t, wmEvent *event)
2421 {
2422         int status = 0;
2423         
2424         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2425         {
2426                 // Use customData pointer to signal Shear direction
2427                 if      (t->customData == NULL)
2428                 {
2429                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2430                         t->customData = (void*)1;
2431                 }
2432                 else
2433                 {
2434                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2435                         t->customData = NULL;
2436                 }
2437                 
2438                 status = 1;
2439         }
2440         
2441         return status;
2442 }
2443
2444
2445 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2446 {
2447         TransData *td = t->data;
2448         float vec[3];
2449         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2450         float value;
2451         int i;
2452         char str[50];
2453         
2454         copy_m3_m4(persmat, t->viewmat);
2455         invert_m3_m3(persinv, persmat);
2456         
2457         value = t->values[0];
2458         
2459         snapGrid(t, &value);
2460         
2461         applyNumInput(&t->num, &value);
2462         
2463         /* header print for NumInput */
2464         if (hasNumInput(&t->num)) {
2465                 char c[20];
2466                 
2467                 outputNumInput(&(t->num), c);
2468                 
2469                 sprintf(str, "Shear: %s %s", c, t->proptext);
2470         }
2471         else {
2472                 /* default header print */
2473                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2474         }
2475         
2476         unit_m3(smat);
2477         
2478         // Custom data signals shear direction
2479         if (t->customData == NULL)
2480                 smat[1][0] = value;
2481         else
2482                 smat[0][1] = value;
2483         
2484         mul_m3_m3m3(tmat, smat, persmat);
2485         mul_m3_m3m3(totmat, persinv, tmat);
2486         
2487         for(i = 0 ; i < t->total; i++, td++) {
2488                 if (td->flag & TD_NOACTION)
2489                         break;
2490                 
2491                 if (td->flag & TD_SKIP)
2492                         continue;
2493                 
2494                 if (t->obedit) {
2495                         float mat3[3][3];
2496                         mul_m3_m3m3(mat3, totmat, td->mtx);
2497                         mul_m3_m3m3(tmat, td->smtx, mat3);
2498                 }
2499                 else {
2500                         copy_m3_m3(tmat, totmat);
2501                 }
2502                 sub_v3_v3v3(vec, td->center, t->center);
2503                 
2504                 mul_m3_v3(tmat, vec);
2505                 
2506                 add_v3_v3(vec, t->center);
2507                 sub_v3_v3(vec, td->center);
2508                 
2509                 mul_v3_fl(vec, td->factor);
2510                 
2511                 add_v3_v3v3(td->loc, td->iloc, vec);
2512         }
2513         
2514         recalcData(t);
2515         
2516         ED_area_headerprint(t->sa, str);
2517
2518         return 1;
2519 }
2520
2521 /* ************************** RESIZE *************************** */
2522
2523 void initResize(TransInfo *t)
2524 {
2525         t->mode = TFM_RESIZE;
2526         t->transform = Resize;
2527         
2528         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2529         
2530         t->flag |= T_NULL_ONE;
2531         t->num.flag |= NUM_NULL_ONE;
2532         t->num.flag |= NUM_AFFECT_ALL;
2533         if (!t->obedit) {
2534                 t->flag |= T_NO_ZERO;
2535                 t->num.flag |= NUM_NO_ZERO;
2536         }
2537         
2538         t->idx_max = 2;
2539         t->num.idx_max = 2;
2540         t->snap[0] = 0.0f;
2541         t->snap[1] = 0.1f;
2542         t->snap[2] = t->snap[1] * 0.1f;
2543
2544         t->num.increment = t->snap[1];
2545 }
2546
2547 static void headerResize(TransInfo *t, float vec[3], char *str) {
2548         char tvec[60];
2549         char *spos= str;
2550         if (hasNumInput(&t->num)) {
2551                 outputNumInput(&(t->num), tvec);
2552         }
2553         else {
2554                 sprintf(&tvec[0], "%.4f", vec[0]);
2555                 sprintf(&tvec[20], "%.4f", vec[1]);
2556                 sprintf(&tvec[40], "%.4f", vec[2]);
2557         }
2558         
2559         if (t->con.mode & CON_APPLY) {
2560                 switch(t->num.idx_max) {
2561                 case 0:
2562                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2563                         break;
2564                 case 1:
2565                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2566                         break;
2567                 case 2:
2568                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2569                 }
2570         }
2571         else {
2572                 if (t->flag & T_2D_EDIT)
2573                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2574                 else
2575                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2576         }
2577         
2578         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2579                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2580         }
2581
2582         (void)spos;
2583 }
2584
2585 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2586 #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)
2587
2588 /* smat is reference matrix, only scaled */
2589 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2590 {
2591         float vec[3];
2592         
2593         copy_v3_v3(vec, mat[0]);
2594         size[0]= normalize_v3(vec);
2595         copy_v3_v3(vec, mat[1]);
2596         size[1]= normalize_v3(vec);
2597         copy_v3_v3(vec, mat[2]);
2598         size[2]= normalize_v3(vec);
2599         
2600         /* first tried with dotproduct... but the sign flip is crucial */
2601         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2602         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2603         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2604 }
2605
2606
2607 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3]) {
2608         float tmat[3][3], smat[3][3], center[3];
2609         float vec[3];
2610         
2611         if (t->flag & T_EDIT) {
2612                 mul_m3_m3m3(smat, mat, td->mtx);
2613                 mul_m3_m3m3(tmat, td->smtx, smat);
2614         }
2615         else {
2616                 copy_m3_m3(tmat, mat);
2617         }
2618         
2619         if (t->con.applySize) {
2620                 t->con.applySize(t, td, tmat);
2621         }
2622         
2623         /* local constraint shouldn't alter center */
2624         if (t->around == V3D_LOCAL) {
2625                 if (t->flag & T_OBJECT) {
2626                         VECCOPY(center, td->center);
2627                 }
2628                 else if (t->flag & T_EDIT) {
2629                         
2630                         if(t->around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2631                                 VECCOPY(center, td->center);
2632                         }
2633                         else {
2634                                 VECCOPY(center, t->center);
2635                         }
2636                 }
2637                 else {
2638                         VECCOPY(center, t->center);
2639                 }
2640         }
2641         else {
2642                 VECCOPY(center, t->center);
2643         }
2644         
2645         if (td->ext) {
2646                 float fsize[3];
2647                 
2648                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2649                         float obsizemat[3][3];
2650                         // Reorient the size mat to fit the oriented object.
2651                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2652                         //print_m3("obsizemat", obsizemat);
2653                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2654                         //print_v3("fsize", fsize);
2655                 }
2656                 else {
2657                         mat3_to_size( fsize,tmat);
2658                 }
2659                 
2660                 protectedSizeBits(td->protectflag, fsize);
2661                 
2662                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2663                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)){
2664                                 /* scale val and reset size */
2665                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2666                                 
2667                                 td->ext->size[0] = td->ext->isize[0];
2668                                 td->ext->size[1] = td->ext->isize[1];
2669                                 td->ext->size[2] = td->ext->isize[2];
2670                          }
2671                         else {
2672                                 /* Reset val if SINGLESIZE but using a constraint */
2673                                 if (td->flag & TD_SINGLESIZE)
2674                                          *td->val = td->ival;
2675                                 
2676                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2677                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2678                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2679                         }
2680                 }
2681                 
2682                 constraintSizeLim(t, td);
2683         }
2684         
2685         /* For individual element center, Editmode need to use iloc */
2686         if (t->flag & T_POINTS)
2687                 sub_v3_v3v3(vec, td->iloc, center);
2688         else
2689                 sub_v3_v3v3(vec, td->center, center);
2690         
2691         mul_m3_v3(tmat, vec);
2692         
2693         add_v3_v3(vec, center);
2694         if (t->flag & T_POINTS)
2695                 sub_v3_v3(vec, td->iloc);
2696         else
2697                 sub_v3_v3(vec, td->center);
2698         
2699         mul_v3_fl(vec, td->factor);
2700         
2701         if (t->flag & (T_OBJECT|T_POSE)) {
2702                 mul_m3_v3(td->smtx, vec);
2703         }
2704         
2705         protectedTransBits(td->protectflag, vec);
2706         add_v3_v3v3(td->loc, td->iloc, vec);
2707         
2708         constraintTransLim(t, td);
2709 }
2710
2711 int Resize(TransInfo *t, const int mval[2])
2712 {
2713         TransData *td;
2714         float size[3], mat[3][3];
2715         float ratio;
2716         int i;
2717         char str[200];
2718         
2719         /* for manipulator, center handle, the scaling can't be done relative to center */
2720         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2721         {
2722                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2723         }
2724         else
2725         {
2726                 ratio = t->values[0];
2727         }
2728         
2729         size[0] = size[1] = size[2] = ratio;
2730         
2731         snapGrid(t, size);
2732         
2733         if (hasNumInput(&t->num)) {
2734                 applyNumInput(&t->num, size);
2735                 constraintNumInput(t, size);
2736         }
2737         
2738         applySnapping(t, size);
2739         
2740         if (t->flag & T_AUTOVALUES)
2741         {
2742                 VECCOPY(size, t->auto_values);
2743         }
2744         
2745         VECCOPY(t->values, size);
2746         
2747         size_to_mat3( mat,size);
2748         
2749         if (t->con.applySize) {
2750                 t->con.applySize(t, NULL, mat);
2751         }
2752         
2753         copy_m3_m3(t->mat, mat);        // used in manipulator
2754         
2755         headerResize(t, size, str);
2756         
2757         for(i = 0, td=t->data; i < t->total; i++, td++) {
2758                 if (td->flag & TD_NOACTION)
2759                         break;
2760                 
2761                 if (td->flag & TD_SKIP)
2762                         continue;
2763                 
2764                 ElementResize(t, td, mat);
2765         }
2766         
2767         /* evil hack - redo resize if cliping needed */
2768         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2769                 size_to_mat3( mat,size);
2770                 
2771                 if (t->con.applySize)
2772                         t->con.applySize(t, NULL, mat);
2773                 
2774                 for(i = 0, td=t->data; i < t->total; i++, td++)
2775                         ElementResize(t, td, mat);
2776         }
2777         
2778         recalcData(t);
2779         
2780         ED_area_headerprint(t->sa, str);
2781         
2782         return 1;
2783 }
2784
2785 /* ************************** TOSPHERE *************************** */
2786
2787 void initToSphere(TransInfo *t)
2788 {
2789         TransData *td = t->data;
2790         int i;
2791         
2792         t->mode = TFM_TOSPHERE;
2793         t->transform = ToSphere;
2794         
2795         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2796         
2797         t->idx_max = 0;
2798         t->num.idx_max = 0;
2799         t->snap[0] = 0.0f;
2800         t->snap[1] = 0.1f;
2801         t->snap[2] = t->snap[1] * 0.1f;
2802         
2803         t->num.increment = t->snap[1];
2804
2805         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2806         t->flag |= T_NO_CONSTRAINT;
2807         
2808         // Calculate average radius
2809         for(i = 0 ; i < t->total; i++, td++) {
2810                 t->val += len_v3v3(t->center, td->iloc);
2811         }
2812         
2813         t->val /= (float)t->total;
2814 }
2815
2816 int ToSphere(TransInfo *t, const int UNUSED(mval[2]))
2817 {
2818         float vec[3];
2819         float ratio, radius;
2820         int i;
2821         char str[64];
2822         TransData *td = t->data;
2823         
2824         ratio = t->values[0];
2825         
2826         snapGrid(t, &ratio);
2827         
2828         applyNumInput(&t->num, &ratio);
2829         
2830         if (ratio < 0)
2831                 ratio = 0.0f;
2832         else if (ratio > 1)
2833                 ratio = 1.0f;
2834         
2835         t->values[0] = ratio;
2836
2837         /* header print for NumInput */
2838         if (hasNumInput(&t->num)) {
2839                 char c[20];
2840                 
2841                 outputNumInput(&(t->num), c);
2842                 
2843                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2844         }
2845         else {
2846                 /* default header print */
2847                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2848         }
2849         
2850         
2851         for(i = 0 ; i < t->total; i++, td++) {
2852                 float tratio;
2853                 if (td->flag & TD_NOACTION)
2854                         break;
2855                 
2856                 if (td->flag & TD_SKIP)
2857                         continue;
2858                 
2859                 sub_v3_v3v3(vec, td->iloc, t->center);
2860                 
2861                 radius = normalize_v3(vec);
2862                 
2863                 tratio = ratio * td->factor;
2864                 
2865                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2866                 
2867                 add_v3_v3v3(td->loc, t->center, vec);
2868         }
2869         
2870         
2871         recalcData(t);
2872         
2873         ED_area_headerprint(t->sa, str);
2874         
2875         return 1;
2876 }
2877
2878 /* ************************** ROTATION *************************** */
2879
2880
2881 static void postInputRotation(TransInfo *t, float values[3])
2882 {
2883         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2884                 t->con.applyRot(t, NULL, t->axis, values);
2885         }
2886 }
2887
2888 void initRotation(TransInfo *t)
2889 {
2890         t->mode = TFM_ROTATION;
2891         t->transform = Rotation;
2892         
2893         setInputPostFct(&t->mouse, postInputRotation);
2894         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2895         
2896         t->idx_max = 0;
2897         t->num.idx_max = 0;
2898         t->snap[0] = 0.0f;
2899         t->snap[1] = (float)((5.0/180)*M_PI);
2900         t->snap[2] = t->snap[1] * 0.2f;
2901         
2902         t->num.increment = 1.0f;
2903
2904         if (t->flag & T_2D_EDIT)
2905                 t->flag |= T_NO_CONSTRAINT;
2906
2907         negate_v3_v3(t->axis, t->viewinv[2]);
2908         normalize_v3(t->axis);
2909
2910         copy_v3_v3(t->axis_orig, t->axis);
2911 }
2912
2913 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around) {
2914         float vec[3], totmat[3][3], smat[3][3];
2915         float eul[3], fmat[3][3], quat[4];
2916         float *center = t->center;
2917         
2918         /* local constraint shouldn't alter center */
2919         if (around == V3D_LOCAL) {
2920                 if (t->flag & (T_OBJECT|T_POSE)) {
2921                         center = td->center;
2922                 }
2923                 else {
2924                         if(around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2925                                 center = td->center;
2926                         }
2927                 }
2928         }
2929         
2930         if (t->flag & T_POINTS) {
2931                 mul_m3_m3m3(totmat, mat, td->mtx);
2932                 mul_m3_m3m3(smat, td->smtx, totmat);
2933                 
2934                 sub_v3_v3v3(vec, td->iloc, center);
2935                 mul_m3_v3(smat, vec);
2936                 
2937                 add_v3_v3v3(td->loc, vec, center);
2938                 
2939                 sub_v3_v3v3(vec,td->loc,td->iloc);
2940                 protectedTransBits(td->protectflag, vec);
2941                 add_v3_v3v3(td->loc, td->iloc, vec);
2942                 
2943                 
2944                 if(td->flag & TD_USEQUAT) {
2945                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, NULL, NULL, NULL, NULL, NULL);
2946                         mat3_to_quat( quat,fmat);       // Actual transform
2947                         
2948                         if(td->ext->quat){
2949                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2950                                 
2951                                 /* is there a reason not to have this here? -jahka */
2952                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2953                         }
2954                 }
2955         }
2956         /**
2957          * HACK WARNING
2958          *
2959          * This is some VERY ugly special case to deal with pose mode.
2960          *
2961          * The problem is that mtx and smtx include each bone orientation.
2962          *
2963          * That is needed to rotate each bone properly, HOWEVER, to calculate
2964          * the translation component, we only need the actual armature object's
2965          * matrix (and inverse). That is not all though. Once the proper translation
2966          * has been computed, it has to be converted back into the bone's space.
2967          */
2968         else if (t->flag & T_POSE) {
2969                 float pmtx[3][3], imtx[3][3];
2970                 
2971                 // Extract and invert armature object matrix
2972                 copy_m3_m4(pmtx, t->poseobj->obmat);
2973                 invert_m3_m3(imtx, pmtx);
2974                 
2975                 if ((td->flag & TD_NO_LOC) == 0)
2976                 {
2977                         sub_v3_v3v3(vec, td->center, center);