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