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