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