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