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