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