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