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