Merging r38951 through r38987 from trunk into 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 #if 0
1044                 case NDOF_MOTION:
1045                         // should have been caught by tranform_modal
1046                         return OPERATOR_PASS_THROUGH;
1047 #endif
1048                 default:
1049                         handled = 0;
1050                         break;
1051                 }
1052
1053                 // Numerical input events
1054                 t->redraw |= handleNumInput(&(t->num), event);
1055
1056                 // Snapping events
1057                 t->redraw |= handleSnapping(t, event);
1058
1059         }
1060         else if (event->val==KM_RELEASE) {
1061                 switch (event->type){
1062                 case LEFTSHIFTKEY:
1063                 case RIGHTSHIFTKEY:
1064                         t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1065                         t->redraw |= TREDRAW_HARD;
1066                         break;
1067
1068                 case MIDDLEMOUSE:
1069                         if ((t->flag & T_NO_CONSTRAINT)==0) {
1070                                 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1071                                 postSelectConstraint(t);
1072                                 t->redraw |= TREDRAW_HARD;
1073                         }
1074                         break;
1075                 case LEFTALTKEY:
1076                         if(t->options&CTX_MOVIECLIP) {
1077                                 restoreTransObjects(t);
1078
1079                                 t->flag&= ~T_RELATIVE_POSITION;
1080                                 t->redraw |= TREDRAW_HARD;
1081                         }
1082                         break;
1083 //              case LEFTMOUSE:
1084 //              case RIGHTMOUSE:
1085 //                      if(WM_modal_tweak_exit(event, t->event_type))
1086 ////                    if (t->options & CTX_TWEAK)
1087 //                              t->state = TRANS_CONFIRM;
1088 //                      break;
1089                 default:
1090                         handled = 0;
1091                         break;
1092                 }
1093
1094                 /* confirm transform if launch key is released after mouse move */
1095                 if (t->flag & T_RELEASE_CONFIRM)
1096                 {
1097                         /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1098                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE))
1099                         {
1100                                 t->state = TRANS_CONFIRM;
1101                         }
1102                 }
1103         }
1104
1105         // Per transform event, if present
1106         if (t->handleEvent)
1107                 t->redraw |= t->handleEvent(t, event);
1108
1109         if (handled || t->redraw)
1110                 return 0;
1111         else
1112                 return OPERATOR_PASS_THROUGH;
1113 }
1114
1115 int calculateTransformCenter(bContext *C, int centerMode, float *vec)
1116 {
1117         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1118         int success = 1;
1119
1120         t->state = TRANS_RUNNING;
1121
1122         t->options = CTX_NONE;
1123
1124         t->mode = TFM_DUMMY;
1125
1126         initTransInfo(C, t, NULL, NULL);        // internal data, mouse, vectors
1127
1128         createTransData(C, t);                          // make TransData structs from selection
1129
1130         t->around = centerMode;                         // override userdefined mode
1131
1132         if (t->total == 0) {
1133                 success = 0;
1134         }
1135         else {
1136                 success = 1;
1137
1138                 calculateCenter(t);
1139
1140                 // Copy center from constraint center. Transform center can be local
1141                 VECCOPY(vec, t->con.center);
1142         }
1143
1144
1145         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1146         special_aftertrans_update(C, t);
1147
1148         postTrans(C, t);
1149
1150         MEM_freeN(t);
1151
1152         return success;
1153 }
1154
1155 typedef enum {
1156         UP,
1157         DOWN,
1158         LEFT,
1159         RIGHT
1160 } ArrowDirection;
1161 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1162 {
1163         switch(d)
1164         {
1165                 case LEFT:
1166                         offset = -offset;
1167                         length = -length;
1168                         size = -size;
1169                 case RIGHT:
1170                         glBegin(GL_LINES);
1171                         glVertex2s( offset, 0);
1172                         glVertex2s( offset + length, 0);
1173                         glVertex2s( offset + length, 0);
1174                         glVertex2s( offset + length - size, -size);
1175                         glVertex2s( offset + length, 0);
1176                         glVertex2s( offset + length - size,  size);
1177                         glEnd();
1178                         break;
1179                 case DOWN:
1180                         offset = -offset;
1181                         length = -length;
1182                         size = -size;
1183                 case UP:
1184                         glBegin(GL_LINES);
1185                         glVertex2s( 0, offset);
1186                         glVertex2s( 0, offset + length);
1187                         glVertex2s( 0, offset + length);
1188                         glVertex2s(-size, offset + length - size);
1189                         glVertex2s( 0, offset + length);
1190                         glVertex2s( size, offset + length - size);
1191                         glEnd();
1192                         break;
1193         }
1194 }
1195
1196 static void drawArrowHead(ArrowDirection d, short size)
1197 {
1198         switch(d)
1199         {
1200                 case LEFT:
1201                         size = -size;
1202                 case RIGHT:
1203                         glBegin(GL_LINES);
1204                         glVertex2s( 0, 0);
1205                         glVertex2s( -size, -size);
1206                         glVertex2s( 0, 0);
1207                         glVertex2s( -size,  size);
1208                         glEnd();
1209                         break;
1210                 case DOWN:
1211                         size = -size;
1212                 case UP:
1213                         glBegin(GL_LINES);
1214                         glVertex2s( 0, 0);
1215                         glVertex2s(-size, -size);
1216                         glVertex2s( 0, 0);
1217                         glVertex2s( size, -size);
1218                         glEnd();
1219                         break;
1220         }
1221 }
1222
1223 static void drawArc(float size, float angle_start, float angle_end, int segments)
1224 {
1225         float delta = (angle_end - angle_start) / segments;
1226         float angle;
1227
1228         glBegin(GL_LINE_STRIP);
1229
1230         for( angle = angle_start; angle < angle_end; angle += delta)
1231         {
1232                 glVertex2f( cosf(angle) * size, sinf(angle) * size);
1233         }
1234         glVertex2f( cosf(angle_end) * size, sinf(angle_end) * size);
1235
1236         glEnd();
1237 }
1238
1239 static int helpline_poll(bContext *C)
1240 {
1241         ARegion *ar= CTX_wm_region(C);
1242         
1243         if(ar && ar->regiontype==RGN_TYPE_WINDOW)
1244                 return 1;
1245         return 0;
1246 }
1247
1248 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1249 {
1250         TransInfo *t = (TransInfo*)customdata;
1251
1252         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR))
1253         {
1254                 float vecrot[3], cent[2];
1255                 int mval[2];
1256
1257                 mval[0]= x;
1258                 mval[1]= y;
1259
1260                 VECCOPY(vecrot, t->center);
1261                 if(t->flag & T_EDIT) {
1262                         Object *ob= t->obedit;
1263                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1264                 }
1265                 else if(t->flag & T_POSE) {
1266                         Object *ob=t->poseobj;
1267                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1268                 }
1269
1270                 projectFloatView(t, vecrot, cent);      // no overflow in extreme cases
1271
1272                 glPushMatrix();
1273
1274                 switch(t->helpline)
1275                 {
1276                         case HLP_SPRING:
1277                                 UI_ThemeColor(TH_WIRE);
1278
1279                                 setlinestyle(3);
1280                                 glBegin(GL_LINE_STRIP);
1281                                 glVertex2iv(t->mval);
1282                                 glVertex2fv(cent);
1283                                 glEnd();
1284
1285                                 glTranslatef(mval[0], mval[1], 0);
1286                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1287
1288                                 setlinestyle(0);
1289                                 glLineWidth(3.0);
1290                                 drawArrow(UP, 5, 10, 5);
1291                                 drawArrow(DOWN, 5, 10, 5);
1292                                 glLineWidth(1.0);
1293                                 break;
1294                         case HLP_HARROW:
1295                                 UI_ThemeColor(TH_WIRE);
1296
1297                                 glTranslatef(mval[0], mval[1], 0);
1298
1299                                 glLineWidth(3.0);
1300                                 drawArrow(RIGHT, 5, 10, 5);
1301                                 drawArrow(LEFT, 5, 10, 5);
1302                                 glLineWidth(1.0);
1303                                 break;
1304                         case HLP_VARROW:
1305                                 UI_ThemeColor(TH_WIRE);
1306
1307                                 glTranslatef(mval[0], mval[1], 0);
1308
1309                                 glLineWidth(3.0);
1310                                 glBegin(GL_LINES);
1311                                 drawArrow(UP, 5, 10, 5);
1312                                 drawArrow(DOWN, 5, 10, 5);
1313                                 glLineWidth(1.0);
1314                                 break;
1315                         case HLP_ANGLE:
1316                                 {
1317                                         float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1318                                         float angle = atan2f(dy, dx);
1319                                         float dist = sqrtf(dx*dx + dy*dy);
1320                                         float delta_angle = MIN2(15.0f / dist, (float)M_PI/4.0f);
1321                                         float spacing_angle = MIN2(5.0f / dist, (float)M_PI/12.0f);
1322                                         UI_ThemeColor(TH_WIRE);
1323
1324                                         setlinestyle(3);
1325                                         glBegin(GL_LINE_STRIP);
1326                                         glVertex2iv(t->mval);
1327                                         glVertex2fv(cent);
1328                                         glEnd();
1329
1330                                         glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1331
1332                                         setlinestyle(0);
1333                                         glLineWidth(3.0);
1334                                         drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1335                                         drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1336
1337                                         glPushMatrix();
1338
1339                                         glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1340                                         glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1341
1342                                         drawArrowHead(DOWN, 5);
1343
1344                                         glPopMatrix();
1345
1346                                         glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1347                                         glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1348
1349                                         drawArrowHead(UP, 5);
1350
1351                                         glLineWidth(1.0);
1352                                         break;
1353                                 }
1354                                 case HLP_TRACKBALL:
1355                                 {
1356                                         unsigned char col[3], col2[3];
1357                                         UI_GetThemeColor3ubv(TH_GRID, col);
1358
1359                                         glTranslatef(mval[0], mval[1], 0);
1360
1361                                         glLineWidth(3.0);
1362
1363                                         UI_make_axis_color(col, col2, 'X');
1364                                         glColor3ubv((GLubyte *)col2);
1365
1366                                         drawArrow(RIGHT, 5, 10, 5);
1367                                         drawArrow(LEFT, 5, 10, 5);
1368
1369                                         UI_make_axis_color(col, col2, 'Y');
1370                                         glColor3ubv((GLubyte *)col2);
1371
1372                                         drawArrow(UP, 5, 10, 5);
1373                                         drawArrow(DOWN, 5, 10, 5);
1374                                         glLineWidth(1.0);
1375                                         break;
1376                                 }
1377                 }
1378
1379                 glPopMatrix();
1380         }
1381 }
1382
1383 static void drawTransformView(const struct bContext *C, struct ARegion *UNUSED(ar), void *arg)
1384 {
1385         TransInfo *t = arg;
1386
1387         drawConstraint(t);
1388         drawPropCircle(C, t);
1389         drawSnapping(C, t);
1390 }
1391
1392 #if 0
1393 static void drawTransformPixel(const struct bContext *UNUSED(C), struct ARegion *UNUSED(ar), void *UNUSED(arg))
1394 {
1395 //      TransInfo *t = arg;
1396 //
1397 //      drawHelpline(C, t->mval[0], t->mval[1], t);
1398 }
1399 #endif
1400
1401 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1402 {
1403         ToolSettings *ts = CTX_data_tool_settings(C);
1404         int constraint_axis[3] = {0, 0, 0};
1405         int proportional = 0;
1406
1407         if (RNA_struct_find_property(op->ptr, "value"))
1408         {
1409                 if (t->flag & T_AUTOVALUES)
1410                 {
1411                         RNA_float_set_array(op->ptr, "value", t->auto_values);
1412                 }
1413                 else
1414                 {
1415                         RNA_float_set_array(op->ptr, "value", t->values);
1416                 }
1417         }
1418
1419         /* convert flag to enum */
1420         switch(t->flag & (T_PROP_EDIT|T_PROP_CONNECTED))
1421         {
1422         case (T_PROP_EDIT|T_PROP_CONNECTED):
1423                 proportional = PROP_EDIT_CONNECTED;
1424                 break;
1425         case T_PROP_EDIT:
1426                 proportional = PROP_EDIT_ON;
1427                 break;
1428         default:
1429                 proportional = PROP_EDIT_OFF;
1430         }
1431
1432         // If modal, save settings back in scene if not set as operator argument
1433         if (t->flag & T_MODAL) {
1434
1435                 /* save settings if not set in operator */
1436                 if (RNA_struct_find_property(op->ptr, "proportional") && !RNA_property_is_set(op->ptr, "proportional")) {
1437                         if (t->obedit)
1438                                 ts->proportional = proportional;
1439                         else
1440                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1441                 }
1442
1443                 if (RNA_struct_find_property(op->ptr, "proportional_size") && !RNA_property_is_set(op->ptr, "proportional_size")) {
1444                         ts->proportional_size = t->prop_size;
1445                 }
1446                         
1447                 if (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") && !RNA_property_is_set(op->ptr, "proportional_edit_falloff")) {
1448                         ts->prop_mode = t->prop_mode;
1449                 }
1450                 
1451                 /* do we check for parameter? */
1452                 if (t->modifiers & MOD_SNAP) {
1453                         ts->snap_flag |= SCE_SNAP;
1454                 } else {
1455                         ts->snap_flag &= ~SCE_SNAP;
1456                 }
1457
1458                 if(t->spacetype == SPACE_VIEW3D) {
1459                         if (RNA_struct_find_property(op->ptr, "constraint_orientation") && !RNA_property_is_set(op->ptr, "constraint_orientation")) {
1460                                 View3D *v3d = t->view;
1461         
1462                                 v3d->twmode = t->current_orientation;
1463                         }
1464                 }
1465         }
1466         
1467         if (RNA_struct_find_property(op->ptr, "proportional"))
1468         {
1469                 RNA_enum_set(op->ptr, "proportional", proportional);
1470                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1471                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1472         }
1473
1474         if (RNA_struct_find_property(op->ptr, "axis"))
1475         {
1476                 RNA_float_set_array(op->ptr, "axis", t->axis);
1477         }
1478
1479         if (RNA_struct_find_property(op->ptr, "mirror"))
1480         {
1481                 RNA_boolean_set(op->ptr, "mirror", t->flag & T_MIRROR);
1482         }
1483
1484         if (RNA_struct_find_property(op->ptr, "constraint_axis"))
1485         {
1486                 /* constraint orientation can be global, event if user selects something else
1487                  * so use the orientation in the constraint if set
1488                  * */
1489                 if (t->con.mode & CON_APPLY) {
1490                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1491                 } else {
1492                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1493                 }
1494
1495                 if (t->con.mode & CON_APPLY)
1496                 {
1497                         if (t->con.mode & CON_AXIS0) {
1498                                 constraint_axis[0] = 1;
1499                         }
1500                         if (t->con.mode & CON_AXIS1) {
1501                                 constraint_axis[1] = 1;
1502                         }
1503                         if (t->con.mode & CON_AXIS2) {
1504                                 constraint_axis[2] = 1;
1505                         }
1506                 }
1507
1508                 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1509         }
1510 }
1511
1512 /* note: caller needs to free 't' on a 0 return */
1513 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1514 {
1515         int options = 0;
1516
1517         t->context = C;
1518
1519         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1520
1521         t->state = TRANS_STARTING;
1522
1523         if(RNA_struct_find_property(op->ptr, "texture_space"))
1524                 if(RNA_boolean_get(op->ptr, "texture_space"))
1525                         options |= CTX_TEXTURE;
1526         
1527         t->options = options;
1528
1529         t->mode = mode;
1530
1531         t->launch_event = event ? event->type : -1;
1532
1533         if (t->launch_event == EVT_TWEAK_R)
1534         {
1535                 t->launch_event = RIGHTMOUSE;
1536         }
1537         else if (t->launch_event == EVT_TWEAK_L)
1538         {
1539                 t->launch_event = LEFTMOUSE;
1540         }
1541
1542         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1543         // For manipulator only, so assume LEFTMOUSE
1544         if (t->launch_event == 0)
1545         {
1546                 t->launch_event = LEFTMOUSE;
1547         }
1548
1549         if (!initTransInfo(C, t, op, event))                                    // internal data, mouse, vectors
1550         {
1551                 return 0;
1552         }
1553
1554         if(t->spacetype == SPACE_VIEW3D)
1555         {
1556                 //calc_manipulator_stats(curarea);
1557                 initTransformOrientation(C, t);
1558
1559                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1560                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1561                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1562                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1563         }
1564         else if(t->spacetype == SPACE_IMAGE) {
1565                 unit_m3(t->spacemtx);
1566                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1567                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1568         }
1569         else if(t->spacetype == SPACE_CLIP) {
1570                 unit_m3(t->spacemtx);
1571                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1572                 t->options |= CTX_MOVIECLIP;
1573         }
1574         else
1575                 unit_m3(t->spacemtx);
1576
1577         createTransData(C, t);                  // make TransData structs from selection
1578
1579         if (t->total == 0) {
1580                 postTrans(C, t);
1581                 return 0;
1582         }
1583
1584         /* Stupid code to have Ctrl-Click on manipulator work ok */
1585         if(event)
1586         {
1587                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1588                 wmKeyMapItem *kmi;
1589
1590                 for (kmi = keymap->items.first; kmi; kmi = kmi->next)
1591                 {
1592                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
1593                         {
1594                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
1595                                         (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1596                                         (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
1597                                         (kmi->type == OSKEY && event->oskey)) {
1598                                         t->modifiers |= MOD_SNAP_INVERT;
1599                                 }
1600                                 break;
1601                         }
1602                 }
1603
1604         }
1605
1606         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1607
1608         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1609         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1610         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1611         mode = t->mode;
1612
1613         calculatePropRatio(t);
1614         calculateCenter(t);
1615
1616         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1617
1618         switch (mode) {
1619         case TFM_TRANSLATION:
1620                 initTranslation(t);
1621                 break;
1622         case TFM_ROTATION:
1623                 initRotation(t);
1624                 break;
1625         case TFM_RESIZE:
1626                 initResize(t);
1627                 break;
1628         case TFM_TOSPHERE:
1629                 initToSphere(t);
1630                 break;
1631         case TFM_SHEAR:
1632                 initShear(t);
1633                 break;
1634         case TFM_WARP:
1635                 initWarp(t);
1636                 break;
1637         case TFM_SHRINKFATTEN:
1638                 initShrinkFatten(t);
1639                 break;
1640         case TFM_TILT:
1641                 initTilt(t);
1642                 break;
1643         case TFM_CURVE_SHRINKFATTEN:
1644                 initCurveShrinkFatten(t);
1645                 break;
1646         case TFM_TRACKBALL:
1647                 initTrackball(t);
1648                 break;
1649         case TFM_PUSHPULL:
1650                 initPushPull(t);
1651                 break;
1652         case TFM_CREASE:
1653                 initCrease(t);
1654                 break;
1655         case TFM_BONESIZE:
1656                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1657                         bArmature *arm= t->poseobj->data;
1658                         if(arm->drawtype==ARM_ENVELOPE)
1659                                 initBoneEnvelope(t);
1660                         else
1661                                 initBoneSize(t);
1662                 }
1663                 break;
1664         case TFM_BONE_ENVELOPE:
1665                 initBoneEnvelope(t);
1666                 break;
1667         case TFM_EDGE_SLIDE:
1668                 initEdgeSlide(t);
1669                 break;
1670         case TFM_BONE_ROLL:
1671                 initBoneRoll(t);
1672                 break;
1673         case TFM_TIME_TRANSLATE:
1674                 initTimeTranslate(t);
1675                 break;
1676         case TFM_TIME_SLIDE:
1677                 initTimeSlide(t);
1678                 break;
1679         case TFM_TIME_SCALE:
1680                 initTimeScale(t);
1681                 break;
1682         case TFM_TIME_DUPLICATE:
1683                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1684                  * so that duplicate-culling will work properly
1685                  */
1686                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1687                         initTranslation(t);
1688                 else
1689                         initTimeTranslate(t);
1690                 t->mode = mode;
1691                 break;
1692         case TFM_TIME_EXTEND:
1693                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1694                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1695                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1696                  * depending on which editor this was called from
1697                  */
1698                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1699                         initTranslation(t);
1700                 else
1701                         initTimeTranslate(t);
1702                 break;
1703         case TFM_BAKE_TIME:
1704                 initBakeTime(t);
1705                 break;
1706         case TFM_MIRROR:
1707                 initMirror(t);
1708                 break;
1709         case TFM_BEVEL:
1710                 initBevel(t);
1711                 break;
1712         case TFM_BWEIGHT:
1713                 initBevelWeight(t);
1714                 break;
1715         case TFM_ALIGN:
1716                 initAlign(t);
1717                 break;
1718         case TFM_SEQ_SLIDE:
1719                 initSeqSlide(t);
1720                 break;
1721         }
1722
1723         if(t->state == TRANS_CANCEL)
1724         {
1725                 postTrans(C, t);
1726                 return 0;
1727         }
1728
1729
1730         /* overwrite initial values if operator supplied a non-null vector */
1731         if (RNA_property_is_set(op->ptr, "value"))
1732         {
1733                 float values[4]= {0}; /* incase value isn't length 4, avoid uninitialized memory  */
1734                 RNA_float_get_array(op->ptr, "value", values);
1735                 QUATCOPY(t->values, values);
1736                 QUATCOPY(t->auto_values, values);
1737                 t->flag |= T_AUTOVALUES;
1738         }
1739
1740         /* Transformation axis from operator */
1741         if (RNA_struct_find_property(op->ptr, "axis") && RNA_property_is_set(op->ptr, "axis"))
1742         {
1743                 RNA_float_get_array(op->ptr, "axis", t->axis);
1744                 normalize_v3(t->axis);
1745                 copy_v3_v3(t->axis_orig, t->axis);
1746         }
1747
1748         /* Constraint init from operator */
1749         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1750         {
1751                 int constraint_axis[3];
1752
1753                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1754
1755                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1756                 {
1757                         t->con.mode |= CON_APPLY;
1758
1759                         if (constraint_axis[0]) {
1760                                 t->con.mode |= CON_AXIS0;
1761                         }
1762                         if (constraint_axis[1]) {
1763                                 t->con.mode |= CON_AXIS1;
1764                         }
1765                         if (constraint_axis[2]) {
1766                                 t->con.mode |= CON_AXIS2;
1767                         }
1768
1769                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1770                 }
1771         }
1772
1773         t->context = NULL;
1774
1775         return 1;
1776 }
1777
1778 void transformApply(bContext *C, TransInfo *t)
1779 {
1780         t->context = C;
1781
1782         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT)))
1783         {
1784                 selectConstraint(t);
1785                 if (t->transform) {
1786                         t->transform(t, t->mval);  // calls recalcData()
1787                         viewRedrawForce(C, t);
1788                 }
1789                 t->redraw = TREDRAW_NOTHING;
1790         } else if (t->redraw & TREDRAW_SOFT) {
1791                 viewRedrawForce(C, t);
1792         }
1793
1794         /* If auto confirm is on, break after one pass */
1795         if (t->options & CTX_AUTOCONFIRM)
1796         {
1797                 t->state = TRANS_CONFIRM;
1798         }
1799
1800         if (BKE_ptcache_get_continue_physics())
1801         {
1802                 // TRANSFORM_FIX_ME
1803                 //do_screenhandlers(G.curscreen);
1804                 t->redraw |= TREDRAW_HARD;
1805         }
1806
1807         t->context = NULL;
1808 }
1809
1810 void drawTransformApply(const bContext *C, struct ARegion *UNUSED(ar), void *arg)
1811 {
1812         TransInfo *t = arg;
1813
1814         if (t->redraw & TREDRAW_SOFT) {
1815                 t->redraw |= TREDRAW_HARD;
1816                 transformApply((bContext *)C, t);
1817         }
1818 }
1819
1820 int transformEnd(bContext *C, TransInfo *t)
1821 {
1822         int exit_code = OPERATOR_RUNNING_MODAL;
1823
1824         t->context = C;
1825
1826         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING)
1827         {
1828                 /* handle restoring objects */
1829                 if(t->state == TRANS_CANCEL)
1830                 {
1831                         /* exception, edge slide transformed UVs too */
1832                         if(t->mode==TFM_EDGE_SLIDE)
1833                                 doEdgeSlide(t, 0.0f);
1834                         
1835                         exit_code = OPERATOR_CANCELLED;
1836                         restoreTransObjects(t); // calls recalcData()
1837                 }
1838                 else
1839                 {
1840                         exit_code = OPERATOR_FINISHED;
1841                 }
1842
1843                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1844                 special_aftertrans_update(C, t);
1845
1846                 /* free data */
1847                 postTrans(C, t);
1848
1849                 /* send events out for redraws */
1850                 viewRedrawPost(C, t);
1851
1852                 /*  Undo as last, certainly after special_trans_update! */
1853
1854                 if(t->state == TRANS_CANCEL) {
1855 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1856                 }
1857                 else {
1858 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1859 //                      else ED_undo_push(C, transform_to_undostr(t));
1860                 }
1861                 t->undostr= NULL;
1862
1863                 viewRedrawForce(C, t);
1864         }
1865
1866         t->context = NULL;
1867
1868         return exit_code;
1869 }
1870
1871 /* ************************** TRANSFORM LOCKS **************************** */
1872
1873 static void protectedTransBits(short protectflag, float *vec)
1874 {
1875         if(protectflag & OB_LOCK_LOCX)
1876                 vec[0]= 0.0f;
1877         if(protectflag & OB_LOCK_LOCY)
1878                 vec[1]= 0.0f;
1879         if(protectflag & OB_LOCK_LOCZ)
1880                 vec[2]= 0.0f;
1881 }
1882
1883 static void protectedSizeBits(short protectflag, float *size)
1884 {
1885         if(protectflag & OB_LOCK_SCALEX)
1886                 size[0]= 1.0f;
1887         if(protectflag & OB_LOCK_SCALEY)
1888                 size[1]= 1.0f;
1889         if(protectflag & OB_LOCK_SCALEZ)
1890                 size[2]= 1.0f;
1891 }
1892
1893 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1894 {
1895         if(protectflag & OB_LOCK_ROTX)
1896                 eul[0]= oldeul[0];
1897         if(protectflag & OB_LOCK_ROTY)
1898                 eul[1]= oldeul[1];
1899         if(protectflag & OB_LOCK_ROTZ)
1900                 eul[2]= oldeul[2];
1901 }
1902
1903
1904 /* this function only does the delta rotation */
1905 /* axis-angle is usually internally stored as quats... */
1906 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1907 {
1908         /* check that protection flags are set */
1909         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1910                 return;
1911         
1912         if (protectflag & OB_LOCK_ROT4D) {
1913                 /* axis-angle getting limited as 4D entities that they are... */
1914                 if (protectflag & OB_LOCK_ROTW)
1915                         *angle= oldAngle;
1916                 if (protectflag & OB_LOCK_ROTX)
1917                         axis[0]= oldAxis[0];
1918                 if (protectflag & OB_LOCK_ROTY)
1919                         axis[1]= oldAxis[1];
1920                 if (protectflag & OB_LOCK_ROTZ)
1921                         axis[2]= oldAxis[2];
1922         }
1923         else {
1924                 /* axis-angle get limited with euler... */
1925                 float eul[3], oldeul[3];
1926                 
1927                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1928                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
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                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1938                 
1939                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1940                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1941                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1942                         axis[1]= 1.0f;
1943                 }
1944         }
1945 }
1946
1947 /* this function only does the delta rotation */
1948 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1949 {
1950         /* check that protection flags are set */
1951         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1952                 return;
1953         
1954         if (protectflag & OB_LOCK_ROT4D) {
1955                 /* quaternions getting limited as 4D entities that they are... */
1956                 if (protectflag & OB_LOCK_ROTW)
1957                         quat[0]= oldquat[0];
1958                 if (protectflag & OB_LOCK_ROTX)
1959                         quat[1]= oldquat[1];
1960                 if (protectflag & OB_LOCK_ROTY)
1961                         quat[2]= oldquat[2];
1962                 if (protectflag & OB_LOCK_ROTZ)
1963                         quat[3]= oldquat[3];
1964         }
1965         else {
1966                 /* quaternions get limited with euler... (compatability mode) */
1967                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1968                 float qlen;
1969
1970                 qlen= normalize_qt_qt(nquat, quat);
1971                 normalize_qt_qt(noldquat, oldquat);
1972
1973                 quat_to_eul(eul, nquat);
1974                 quat_to_eul(oldeul, noldquat);
1975
1976                 if (protectflag & OB_LOCK_ROTX)
1977                         eul[0]= oldeul[0];
1978                 if (protectflag & OB_LOCK_ROTY)
1979                         eul[1]= oldeul[1];
1980                 if (protectflag & OB_LOCK_ROTZ)
1981                         eul[2]= oldeul[2];
1982
1983                 eul_to_quat( quat,eul);
1984
1985                 /* restore original quat size */
1986                 mul_qt_fl(quat, qlen);
1987                 
1988                 /* quaternions flip w sign to accumulate rotations correctly */
1989                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
1990                         mul_qt_fl(quat, -1.0f);
1991                 }
1992         }
1993 }
1994
1995 /* ******************* TRANSFORM LIMITS ********************** */
1996
1997 static void constraintTransLim(TransInfo *UNUSED(t), TransData *td)
1998 {
1999         if (td->con) {
2000                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
2001                 bConstraintOb cob= {NULL};
2002                 bConstraint *con;
2003                 
2004                 /* Make a temporary bConstraintOb for using these limit constraints
2005                  *      - they only care that cob->matrix is correctly set ;-)
2006                  *      - current space should be local
2007                  */
2008                 unit_m4(cob.matrix);
2009                 VECCOPY(cob.matrix[3], td->loc);
2010                 
2011                 /* Evaluate valid constraints */
2012                 for (con= td->con; con; con= con->next) {
2013                         float tmat[4][4];
2014                         
2015                         /* only consider constraint if enabled */
2016                         if (con->flag & CONSTRAINT_DISABLE) continue;
2017                         if (con->enforce == 0.0f) continue;
2018                         
2019                         /* only use it if it's tagged for this purpose (and the right type) */
2020                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2021                                 bLocLimitConstraint *data= con->data;
2022                                 
2023                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2024                                         continue;
2025                                 
2026                                 /* do space conversions */
2027                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2028                                         /* just multiply by td->mtx (this should be ok) */
2029                                         copy_m4_m4(tmat, cob.matrix);
2030                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2031                                 }
2032                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2033                                         /* skip... incompatable spacetype */
2034                                         continue;
2035                                 }
2036                                 
2037                                 /* do constraint */
2038                                 cti->evaluate_constraint(con, &cob, NULL);
2039                                 
2040                                 /* convert spaces again */
2041                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2042                                         /* just multiply by td->mtx (this should be ok) */
2043                                         copy_m4_m4(tmat, cob.matrix);
2044                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2045                                 }
2046                         }
2047                 }
2048                 
2049                 /* copy results from cob->matrix */
2050                 VECCOPY(td->loc, cob.matrix[3]);
2051         }
2052 }
2053
2054 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2055 {
2056         /* Make a temporary bConstraintOb for use by limit constraints
2057          *      - they only care that cob->matrix is correctly set ;-)
2058          *      - current space should be local
2059          */
2060         memset(cob, 0, sizeof(bConstraintOb));
2061         if (td->ext)
2062         {
2063                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2064                         /* quats */
2065                         /* objects and bones do normalization first too, otherwise
2066                            we don't necessarily end up with a rotation matrix, and
2067                            then conversion back to quat gives a different result */
2068                         float quat[4];
2069                         normalize_qt_qt(quat, td->ext->quat);
2070                         quat_to_mat4(cob->matrix, quat);
2071                 }
2072                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2073                         /* axis angle */
2074                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2075                 }
2076                 else {
2077                         /* eulers */
2078                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2079                 }
2080         }
2081 }
2082
2083 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2084 {
2085         if (td->con) {
2086                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2087                 bConstraintOb cob;
2088                 bConstraint *con;
2089                 int dolimit = 0;
2090                 
2091                 /* Evaluate valid constraints */
2092                 for (con= td->con; con; con= con->next) {
2093                         /* only consider constraint if enabled */
2094                         if (con->flag & CONSTRAINT_DISABLE) continue;
2095                         if (con->enforce == 0.0f) continue;
2096                         
2097                         /* we're only interested in Limit-Rotation constraints */
2098                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2099                                 bRotLimitConstraint *data= con->data;
2100                                 float tmat[4][4];
2101                                 
2102                                 /* only use it if it's tagged for this purpose */
2103                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2104                                         continue;
2105
2106                                 /* skip incompatable spacetypes */
2107                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2108                                         continue;
2109
2110                                 /* only do conversion if necessary, to preserve quats and eulers */
2111                                 if(!dolimit) {
2112                                         constraintob_from_transdata(&cob, td);
2113                                         dolimit= 1;
2114                                 }
2115                                 
2116                                 /* do space conversions */
2117                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2118                                         /* just multiply by td->mtx (this should be ok) */
2119                                         copy_m4_m4(tmat, cob.matrix);
2120                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2121                                 }
2122                                 
2123                                 /* do constraint */
2124                                 cti->evaluate_constraint(con, &cob, NULL);
2125                                 
2126                                 /* convert spaces again */
2127                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2128                                         /* just multiply by td->mtx (this should be ok) */
2129                                         copy_m4_m4(tmat, cob.matrix);
2130                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2131                                 }
2132                         }
2133                 }
2134                 
2135                 if(dolimit) {
2136                         /* copy results from cob->matrix */
2137                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2138                                 /* quats */
2139                                 mat4_to_quat( td->ext->quat,cob.matrix);
2140                         }
2141                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2142                                 /* axis angle */
2143                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2144                         }
2145                         else {
2146                                 /* eulers */
2147                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2148                         }
2149                 }
2150         }
2151 }
2152
2153 static void constraintSizeLim(TransInfo *t, TransData *td)
2154 {
2155         if (td->con && td->ext) {
2156                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2157                 bConstraintOb cob= {NULL};
2158                 bConstraint *con;
2159                 
2160                 /* Make a temporary bConstraintOb for using these limit constraints
2161                  *      - they only care that cob->matrix is correctly set ;-)
2162                  *      - current space should be local
2163                  */
2164                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2165                         /* scale val and reset size */
2166                         return; // TODO: fix this case
2167                 }
2168                 else {
2169                         /* Reset val if SINGLESIZE but using a constraint */
2170                         if (td->flag & TD_SINGLESIZE)
2171                                 return;
2172                         
2173                         size_to_mat4( cob.matrix,td->ext->size);
2174                 }
2175                 
2176                 /* Evaluate valid constraints */
2177                 for (con= td->con; con; con= con->next) {
2178                         /* only consider constraint if enabled */
2179                         if (con->flag & CONSTRAINT_DISABLE) continue;
2180                         if (con->enforce == 0.0f) continue;
2181                         
2182                         /* we're only interested in Limit-Scale constraints */
2183                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2184                                 bSizeLimitConstraint *data= con->data;
2185                                 float tmat[4][4];
2186                                 
2187                                 /* only use it if it's tagged for this purpose */
2188                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2189                                         continue;
2190                                 
2191                                 /* do space conversions */
2192                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2193                                         /* just multiply by td->mtx (this should be ok) */
2194                                         copy_m4_m4(tmat, cob.matrix);
2195                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2196                                 }
2197                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2198                                         /* skip... incompatable spacetype */
2199                                         continue;
2200                                 }
2201                                 
2202                                 /* do constraint */
2203                                 cti->evaluate_constraint(con, &cob, NULL);
2204                                 
2205                                 /* convert spaces again */
2206                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2207                                         /* just multiply by td->mtx (this should be ok) */
2208                                         copy_m4_m4(tmat, cob.matrix);
2209                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2210                                 }
2211                         }
2212                 }
2213                 
2214                 /* copy results from cob->matrix */
2215                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2216                         /* scale val and reset size */
2217                         return; // TODO: fix this case
2218                 }
2219                 else {
2220                         /* Reset val if SINGLESIZE but using a constraint */
2221                         if (td->flag & TD_SINGLESIZE)
2222                                 return;
2223                         
2224                         mat4_to_size( td->ext->size,cob.matrix);
2225                 }
2226         }
2227 }
2228
2229 /* ************************** WARP *************************** */
2230
2231 static void postInputWarp(TransInfo *t, float values[3])
2232 {
2233         mul_v3_fl(values, (float)(M_PI * 2));
2234
2235         if (t->customData) /* non-null value indicates reversed input */
2236         {
2237                 negate_v3(values);
2238         }
2239 }
2240
2241 void initWarp(TransInfo *t)
2242 {
2243         float max[3], min[3];
2244         int i;
2245         
2246         t->mode = TFM_WARP;
2247         t->transform = Warp;
2248         t->handleEvent = handleEventWarp;
2249         
2250         setInputPostFct(&t->mouse, postInputWarp);
2251         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2252         
2253         t->idx_max = 0;
2254         t->num.idx_max = 0;
2255         t->snap[0] = 0.0f;
2256         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2257         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2258         
2259         t->num.increment = 1.0f;
2260
2261         t->flag |= T_NO_CONSTRAINT;
2262         
2263         /* we need min/max in view space */
2264         for(i = 0; i < t->total; i++) {
2265                 float center[3];
2266                 copy_v3_v3(center, t->data[i].center);
2267                 mul_m3_v3(t->data[i].mtx, center);
2268                 mul_m4_v3(t->viewmat, center);
2269                 sub_v3_v3(center, t->viewmat[3]);
2270                 if (i)
2271                         minmax_v3v3_v3(min, max, center);
2272                 else {
2273                         copy_v3_v3(max, center);
2274                         copy_v3_v3(min, center);
2275                 }
2276         }
2277
2278         mid_v3_v3v3(t->center, min, max);
2279
2280         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2281         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2282 }
2283
2284 int handleEventWarp(TransInfo *t, wmEvent *event)
2285 {
2286         int status = 0;
2287         
2288         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2289         {
2290                 // Use customData pointer to signal warp direction
2291                 if      (t->customData == NULL)
2292                         t->customData = (void*)1;
2293                 else
2294                         t->customData = NULL;
2295                 
2296                 status = 1;
2297         }
2298         
2299         return status;
2300 }
2301
2302 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2303 {
2304         TransData *td = t->data;
2305         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2306         int i;
2307         char str[50];
2308         
2309         curs= give_cursor(t->scene, t->view);
2310         /*
2311          * gcursor is the one used for helpline.
2312          * It has to be in the same space as the drawing loop
2313          * (that means it needs to be in the object's space when in edit mode and
2314          *  in global space in object mode)
2315          *
2316          * cursor is used for calculations.
2317          * It needs to be in view space, but we need to take object's offset
2318          * into account if in Edit mode.
2319          */
2320         VECCOPY(cursor, curs);
2321         VECCOPY(gcursor, cursor);
2322         if (t->flag & T_EDIT) {
2323                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2324                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2325                 mul_m3_v3(t->data->smtx, gcursor);
2326         }
2327         mul_m4_v3(t->viewmat, cursor);
2328         sub_v3_v3(cursor, t->viewmat[3]);
2329         
2330         /* amount of radians for warp */
2331         circumfac = t->values[0];
2332         
2333         snapGrid(t, &circumfac);
2334         applyNumInput(&t->num, &circumfac);
2335         
2336         /* header print for NumInput */
2337         if (hasNumInput(&t->num)) {
2338                 char c[20];
2339                 
2340                 outputNumInput(&(t->num), c);
2341                 
2342                 sprintf(str, "Warp: %s", c);
2343
2344                 circumfac = DEG2RADF(circumfac);
2345         }
2346         else {
2347                 /* default header print */
2348                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2349         }
2350         
2351         t->values[0] = circumfac;
2352
2353         circumfac /= 2; /* only need 180 on each side to make 360 */
2354         
2355         for(i = 0; i < t->total; i++, td++) {
2356                 float loc[3];
2357                 if (td->flag & TD_NOACTION)
2358                         break;
2359                 
2360                 if (td->flag & TD_SKIP)
2361                         continue;
2362                 
2363                 /* translate point to center, rotate in such a way that outline==distance */
2364                 VECCOPY(vec, td->iloc);
2365                 mul_m3_v3(td->mtx, vec);
2366                 mul_m4_v3(t->viewmat, vec);
2367                 sub_v3_v3(vec, t->viewmat[3]);
2368                 
2369                 dist= vec[0]-cursor[0];
2370                 
2371                 /* t->val is X dimension projected boundbox */
2372                 phi0= (circumfac*dist/t->val);
2373                 
2374                 vec[1]= (vec[1]-cursor[1]);
2375                 
2376                 co= (float)cos(phi0);
2377                 si= (float)sin(phi0);
2378                 loc[0]= -si*vec[1]+cursor[0];
2379                 loc[1]= co*vec[1]+cursor[1];
2380                 loc[2]= vec[2];
2381                 
2382                 mul_m4_v3(t->viewinv, loc);
2383                 sub_v3_v3(loc, t->viewinv[3]);
2384                 mul_m3_v3(td->smtx, loc);
2385                 
2386                 sub_v3_v3(loc, td->iloc);
2387                 mul_v3_fl(loc, td->factor);
2388                 add_v3_v3v3(td->loc, td->iloc, loc);
2389         }
2390         
2391         recalcData(t);
2392         
2393         ED_area_headerprint(t->sa, str);
2394         
2395         return 1;
2396 }
2397
2398 /* ************************** SHEAR *************************** */
2399
2400 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2401 {
2402         mul_v3_fl(values, 0.05f);
2403 }
2404
2405 void initShear(TransInfo *t)
2406 {
2407         t->mode = TFM_SHEAR;
2408         t->transform = Shear;
2409         t->handleEvent = handleEventShear;
2410         
2411         setInputPostFct(&t->mouse, postInputShear);
2412         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2413         
2414         t->idx_max = 0;
2415         t->num.idx_max = 0;
2416         t->snap[0] = 0.0f;
2417         t->snap[1] = 0.1f;
2418         t->snap[2] = t->snap[1] * 0.1f;
2419         
2420         t->num.increment = 0.1f;
2421
2422         t->flag |= T_NO_CONSTRAINT;
2423 }
2424
2425 int handleEventShear(TransInfo *t, wmEvent *event)
2426 {
2427         int status = 0;
2428         
2429         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2430         {
2431                 // Use customData pointer to signal Shear direction
2432                 if      (t->customData == NULL)
2433                 {
2434                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2435                         t->customData = (void*)1;
2436                 }
2437                 else
2438                 {
2439                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2440                         t->customData = NULL;
2441                 }
2442                 
2443                 status = 1;
2444         }
2445         
2446         return status;
2447 }
2448
2449
2450 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2451 {
2452         TransData *td = t->data;
2453         float vec[3];
2454         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2455         float value;
2456         int i;
2457         char str[50];
2458         
2459         copy_m3_m4(persmat, t->viewmat);
2460         invert_m3_m3(persinv, persmat);
2461         
2462         value = t->values[0];
2463         
2464         snapGrid(t, &value);
2465         
2466         applyNumInput(&t->num, &value);
2467         
2468         /* header print for NumInput */
2469         if (hasNumInput(&t->num)) {
2470                 char c[20];
2471                 
2472                 outputNumInput(&(t->num), c);
2473                 
2474                 sprintf(str, "Shear: %s %s", c, t->proptext);
2475         }
2476         else {
2477                 /* default header print */
2478                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2479         }
2480         
2481         unit_m3(smat);
2482         
2483         // Custom data signals shear direction
2484         if (t->customData == NULL)
2485                 smat[1][0] = value;
2486         else
2487                 smat[0][1] = value;
2488         
2489         mul_m3_m3m3(tmat, smat, persmat);
2490         mul_m3_m3m3(totmat, persinv, tmat);
2491         
2492         for(i = 0 ; i < t->total; i++, td++) {
2493                 if (td->flag & TD_NOACTION)
2494                         break;
2495                 
2496                 if (td->flag & TD_SKIP)
2497                         continue;
2498                 
2499                 if (t->obedit) {
2500                         float mat3[3][3];
2501                         mul_m3_m3m3(mat3, totmat, td->mtx);
2502                         mul_m3_m3m3(tmat, td->smtx, mat3);
2503                 }
2504                 else {
2505                         copy_m3_m3(tmat, totmat);
2506                 }
2507                 sub_v3_v3v3(vec, td->center, t->center);
2508                 
2509                 mul_m3_v3(tmat, vec);
2510                 
2511                 add_v3_v3(vec, t->center);
2512                 sub_v3_v3(vec, td->center);
2513                 
2514                 mul_v3_fl(vec, td->factor);
2515                 
2516                 add_v3_v3v3(td->loc, td->iloc, vec);
2517         }
2518         
2519         recalcData(t);
2520         
2521         ED_area_headerprint(t->sa, str);
2522
2523         return 1;
2524 }
2525
2526 /* ************************** RESIZE *************************** */
2527
2528 void initResize(TransInfo *t)
2529 {
2530         t->mode = TFM_RESIZE;
2531         t->transform = Resize;
2532         
2533         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2534         
2535         t->flag |= T_NULL_ONE;
2536         t->num.flag |= NUM_NULL_ONE;
2537         t->num.flag |= NUM_AFFECT_ALL;
2538         if (!t->obedit) {
2539                 t->flag |= T_NO_ZERO;
2540                 t->num.flag |= NUM_NO_ZERO;
2541         }
2542         
2543         t->idx_max = 2;
2544         t->num.idx_max = 2;
2545         t->snap[0] = 0.0f;
2546         t->snap[1] = 0.1f;
2547         t->snap[2] = t->snap[1] * 0.1f;
2548
2549         t->num.increment = t->snap[1];
2550 }
2551
2552 static void headerResize(TransInfo *t, float vec[3], char *str) {
2553         char tvec[60];
2554         char *spos= str;
2555         if (hasNumInput(&t->num)) {
2556                 outputNumInput(&(t->num), tvec);
2557         }
2558         else {
2559                 sprintf(&tvec[0], "%.4f", vec[0]);
2560                 sprintf(&tvec[20], "%.4f", vec[1]);
2561                 sprintf(&tvec[40], "%.4f", vec[2]);
2562         }
2563         
2564         if (t->con.mode & CON_APPLY) {
2565                 switch(t->num.idx_max) {
2566                 case 0:
2567                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2568                         break;
2569                 case 1:
2570                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2571                         break;
2572                 case 2:
2573                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2574                 }
2575         }
2576         else {
2577                 if (t->flag & T_2D_EDIT)
2578                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2579                 else
2580                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2581         }
2582         
2583         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2584                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2585         }
2586
2587         (void)spos;
2588 }
2589
2590 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2591 #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)
2592
2593 /* smat is reference matrix, only scaled */
2594 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2595 {
2596         float vec[3];
2597         
2598         copy_v3_v3(vec, mat[0]);
2599         size[0]= normalize_v3(vec);
2600         copy_v3_v3(vec, mat[1]);
2601         size[1]= normalize_v3(vec);
2602         copy_v3_v3(vec, mat[2]);
2603         size[2]= normalize_v3(vec);
2604         
2605         /* first tried with dotproduct... but the sign flip is crucial */
2606         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2607         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2608         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2609 }
2610
2611
2612 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3]) {
2613         float tmat[3][3], smat[3][3], center[3];
2614         float vec[3];
2615         
2616         if (t->flag & T_EDIT) {
2617                 mul_m3_m3m3(smat, mat, td->mtx);
2618                 mul_m3_m3m3(tmat, td->smtx, smat);
2619         }
2620         else {
2621                 copy_m3_m3(tmat, mat);
2622         }
2623         
2624         if (t->con.applySize) {
2625                 t->con.applySize(t, td, tmat);
2626         }
2627         
2628         /* local constraint shouldn't alter center */
2629         if (t->around == V3D_LOCAL) {
2630                 if (t->flag & T_OBJECT) {
2631                         VECCOPY(center, td->center);
2632                 }
2633                 else if (t->flag & T_EDIT) {
2634                         
2635                         if(t->around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2636                                 VECCOPY(center, td->center);
2637                         }
2638                         else {
2639                                 VECCOPY(center, t->center);
2640                         }
2641                 }
2642                 else {
2643                         VECCOPY(center, t->center);
2644                 }
2645         }
2646         else {
2647                 VECCOPY(center, t->center);
2648         }
2649         
2650         if (td->ext) {
2651                 float fsize[3];
2652                 
2653                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2654                         float obsizemat[3][3];
2655                         // Reorient the size mat to fit the oriented object.
2656                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2657                         //print_m3("obsizemat", obsizemat);
2658                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2659                         //print_v3("fsize", fsize);
2660                 }
2661                 else {
2662                         mat3_to_size( fsize,tmat);
2663                 }
2664                 
2665                 protectedSizeBits(td->protectflag, fsize);
2666                 
2667                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2668                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)){
2669                                 /* scale val and reset size */
2670                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2671                                 
2672                                 td->ext->size[0] = td->ext->isize[0];
2673                                 td->ext->size[1] = td->ext->isize[1];
2674                                 td->ext->size[2] = td->ext->isize[2];
2675                          }
2676                         else {
2677                                 /* Reset val if SINGLESIZE but using a constraint */
2678                                 if (td->flag & TD_SINGLESIZE)
2679                                          *td->val = td->ival;
2680                                 
2681                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2682                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2683                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2684                         }
2685                 }
2686                 
2687                 constraintSizeLim(t, td);
2688         }
2689         
2690         /* For individual element center, Editmode need to use iloc */
2691         if (t->flag & T_POINTS)
2692                 sub_v3_v3v3(vec, td->iloc, center);
2693         else
2694                 sub_v3_v3v3(vec, td->center, center);
2695         
2696         mul_m3_v3(tmat, vec);
2697         
2698         add_v3_v3(vec, center);
2699         if (t->flag & T_POINTS)
2700                 sub_v3_v3(vec, td->iloc);
2701         else
2702                 sub_v3_v3(vec, td->center);
2703         
2704         mul_v3_fl(vec, td->factor);
2705         
2706         if (t->flag & (T_OBJECT|T_POSE)) {
2707                 mul_m3_v3(td->smtx, vec);
2708         }
2709         
2710         protectedTransBits(td->protectflag, vec);
2711         add_v3_v3v3(td->loc, td->iloc, vec);
2712         
2713         constraintTransLim(t, td);
2714 }
2715
2716 int Resize(TransInfo *t, const int mval[2])
2717 {
2718         TransData *td;
2719         float size[3], mat[3][3];
2720         float ratio;
2721         int i;
2722         char str[200];
2723         
2724         /* for manipulator, center handle, the scaling can't be done relative to center */
2725         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2726         {
2727                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2728         }
2729         else
2730         {
2731                 ratio = t->values[0];
2732         }
2733         
2734         size[0] = size[1] = size[2] = ratio;
2735         
2736         snapGrid(t, size);
2737         
2738         if (hasNumInput(&t->num)) {
2739                 applyNumInput(&t->num, size);
2740                 constraintNumInput(t, size);
2741         }
2742         
2743         applySnapping(t, size);
2744         
2745         if (t->flag & T_AUTOVALUES)
2746         {
2747                 VECCOPY(size, t->auto_values);
2748         }
2749         
2750         VECCOPY(t->values, size);
2751         
2752         size_to_mat3( mat,size);
2753         
2754         if (t->con.applySize) {
2755                 t->con.applySize(t, NULL, mat);
2756         }
2757         
2758         copy_m3_m3(t->mat, mat);        // used in manipulator
2759         
2760         headerResize(t, size, str);
2761         
2762         for(i = 0, td=t->data; i < t->total; i++, td++) {
2763                 if (td->flag & TD_NOACTION)
2764                         break;
2765                 
2766                 if (td->flag & TD_SKIP)
2767                         continue;
2768                 
2769                 ElementResize(t, td, mat);
2770         }
2771         
2772         /* evil hack - redo resize if cliping needed */
2773         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2774                 size_to_mat3( mat,size);
2775                 
2776                 if (t->con.applySize)
2777                         t->con.applySize(t, NULL, mat);
2778                 
2779                 for(i = 0, td=t->data; i < t->total; i++, td++)
2780                         ElementResize(t, td, mat);
2781         }
2782         
2783         recalcData(t);
2784         
2785         ED_area_headerprint(t->sa, str);
2786         
2787         return 1;
2788 }
2789
2790 /* ************************** TOSPHERE *************************** */
2791
2792 void initToSphere(TransInfo *t)
2793 {
2794         TransData *td = t->data;
2795         int i;
2796         
2797         t->mode = TFM_TOSPHERE;
2798         t->transform = ToSphere;
2799         
2800         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2801         
2802         t->idx_max = 0;
2803         t->num.idx_max = 0;
2804         t->snap[0] = 0.0f;
2805         t->snap[1] = 0.1f;
2806         t->snap[2] = t->snap[1] * 0.1f;
2807         
2808         t->num.increment = t->snap[1];
2809
2810         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2811         t->flag |= T_NO_CONSTRAINT;
2812         
2813         // Calculate average radius
2814         for(i = 0 ; i < t->total; i++, td++) {
2815                 t->val += len_v3v3(t->center, td->iloc);
2816         }
2817         
2818         t->val /= (float)t->total;
2819 }
2820
2821 int ToSphere(TransInfo *t, const int UNUSED(mval[2]))
2822 {
2823         float vec[3];
2824         float ratio, radius;
2825         int i;
2826         char str[64];
2827         TransData *td = t->data;
2828         
2829         ratio = t->values[0];
2830         
2831         snapGrid(t, &ratio);
2832         
2833         applyNumInput(&t->num, &ratio);
2834         
2835         if (ratio < 0)
2836                 ratio = 0.0f;
2837         else if (ratio > 1)
2838                 ratio = 1.0f;
2839         
2840         t->values[0] = ratio;
2841
2842         /* header print for NumInput */
2843         if (hasNumInput(&t->num)) {
2844                 char c[20];
2845                 
2846                 outputNumInput(&(t->num), c);
2847                 
2848                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2849         }
2850         else {
2851                 /* default header print */
2852                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2853         }
2854         
2855         
2856         for(i = 0 ; i < t->total; i++, td++) {
2857                 float tratio;
2858                 if (td->flag & TD_NOACTION)
2859                         break;
2860                 
2861                 if (td->flag & TD_SKIP)
2862                         continue;
2863                 
2864                 sub_v3_v3v3(vec, td->iloc, t->center);
2865                 
2866                 radius = normalize_v3(vec);
2867                 
2868                 tratio = ratio * td->factor;
2869                 
2870                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2871                 
2872                 add_v3_v3v3(td->loc, t->center, vec);
2873         }
2874         
2875         
2876         recalcData(t);
2877         
2878         ED_area_headerprint(t->sa, str);
2879         
2880         return 1;
2881 }
2882
2883 /* ************************** ROTATION *************************** */
2884
2885
2886 static void postInputRotation(TransInfo *t, float values[3])
2887 {
2888         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2889                 t->con.applyRot(t, NULL, t->axis, values);
2890         }
2891 }
2892
2893 void initRotation(TransInfo *t)
2894 {
2895         t->mode = TFM_ROTATION;
2896         t->transform = Rotation;
2897         
2898         setInputPostFct(&t->mouse, postInputRotation);
2899         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2900         
2901         t->idx_max = 0;
2902         t->num.idx_max = 0;
2903         t->snap[0] = 0.0f;
2904         t->snap[1] = (float)((5.0/180)*M_PI);
2905         t->snap[2] = t->snap[1] * 0.2f;
2906         
2907         t->num.increment = 1.0f;
2908
2909         if (t->flag & T_2D_EDIT)
2910                 t->flag |= T_NO_CONSTRAINT;
2911
2912         negate_v3_v3(t->axis, t->viewinv[2]);
2913         normalize_v3(t->axis);
2914
2915         copy_v3_v3(t->axis_orig, t->axis);
2916 }
2917
2918 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around) {
2919         float vec[3], totmat[3][3], smat[3][3];
2920         float eul[3], fmat[3][3], quat[4];
2921         float *center = t->center;
2922         
2923         /* local constraint shouldn't alter center */
2924         if (around == V3D_LOCAL) {
2925                 if (t->flag & (T_OBJECT|T_POSE)) {
2926                         center = td->center;
2927                 }
2928                 else {
2929                         if(around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2930                                 center = td->center;
2931                         }
2932                 }
2933         }
2934         
2935         if (t->flag & T_POINTS) {
2936                 mul_m3_m3m3(totmat, mat, td->mtx);
2937                 mul_m3_m3m3(smat, td->smtx, totmat);
2938                 
2939                 sub_v3_v3v3(vec, td->iloc, center);
2940                 mul_m3_v3(smat, vec);
2941                 
2942                 add_v3_v3v3(td->loc, vec, center);
2943                 
2944                 sub_v3_v3v3(vec,td->loc,td->iloc);
2945                 protectedTransBits(td->protectflag, vec);
2946                 add_v3_v3v3(td->loc, td->iloc, vec);
2947                 
2948                 
2949                 if(td->flag & TD_USEQUAT) {
2950                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, NULL, NULL, NULL, NULL, NULL);
2951                         mat3_to_quat( quat,fmat);       // Actual transform
2952                         
2953                         if(td->ext->quat){
2954                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2955                                 
2956                                 /* is there a reason not to have this here? -jahka */
2957                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2958                         }
2959                 }
2960         }
2961         /**
2962          * HACK WARNING
2963          *
2964          * This is some VERY ugly special case to deal with pose mode.
2965          *
2966          * The problem is that mtx and smtx include each bone orientation.
2967          *
2968          * That is needed to rotate each bone properly, HOWEVER, to calculate
2969          * the translation component, we only need the actual armature object's
2970          * matrix (and inverse). That is not all though. Once the proper translation
2971          * has been computed, it has to be converted back into the bone's space.
2972          */
2973         else if (t->flag & T_POSE) {
2974                 float pmtx[3][3], imtx[3][3];
2975                 
2976                 // Extract and invert armature object matrix
2977                 copy_m3_m4(pmtx, t->poseobj->obmat);
2978          &nbs