bmesh api functions, not used yet:
[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                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
590                         t->con.mode |= CON_SELECT;
591
592                 copy_v2_v2_int(t->mval, event->mval);
593
594                 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
595                 t->redraw |= TREDRAW_HARD;
596
597
598                 if (t->state == TRANS_STARTING) {
599                         t->state = TRANS_RUNNING;
600                 }
601
602                 applyMouseInput(t, &t->mouse, t->mval, t->values);
603
604                 // Snapping mouse move events
605                 t->redraw |= handleSnapping(t, event);
606         }
607
608         /* handle modal keymap first */
609         if (event->type == EVT_MODAL_MAP) {
610                 switch (event->val) {
611                         case TFM_MODAL_CANCEL:
612                                 t->state = TRANS_CANCEL;
613                                 break;
614                         case TFM_MODAL_CONFIRM:
615                                 t->state = TRANS_CONFIRM;
616                                 break;
617                         case TFM_MODAL_TRANSLATE:
618                                 /* only switch when... */
619                                 if ( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
620                                         resetTransRestrictions(t);
621                                         restoreTransObjects(t);
622                                         initTranslation(t);
623                                         initSnapping(t, NULL); // need to reinit after mode change
624                                         t->redraw |= TREDRAW_HARD;
625                                 }
626                                 else if (t->mode == TFM_TRANSLATION) {
627                                         if (t->options&CTX_MOVIECLIP) {
628                                                 restoreTransObjects(t);
629
630                                                 t->flag^= T_ALT_TRANSFORM;
631                                                 t->redraw |= TREDRAW_HARD;
632                                         }
633                                 }
634                                 break;
635                         case TFM_MODAL_ROTATE:
636                                 /* only switch when... */
637                                 if (!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
638                                         if ( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
639                                                 
640                                                 resetTransRestrictions(t);
641                                                 
642                                                 if (t->mode == TFM_ROTATION) {
643                                                         restoreTransObjects(t);
644                                                         initTrackball(t);
645                                                 }
646                                                 else {
647                                                         restoreTransObjects(t);
648                                                         initRotation(t);
649                                                 }
650                                                 initSnapping(t, NULL); // need to reinit after mode change
651                                                 t->redraw |= TREDRAW_HARD;
652                                         }
653                                 }
654                                 break;
655                         case TFM_MODAL_RESIZE:
656                                 /* only switch when... */
657                                 if ( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
658                                         resetTransRestrictions(t);
659                                         restoreTransObjects(t);
660                                         initResize(t);
661                                         initSnapping(t, NULL); // need to reinit after mode change
662                                         t->redraw |= TREDRAW_HARD;
663                                 }
664                                 break;
665                                 
666                         case TFM_MODAL_SNAP_INV_ON:
667                                 t->modifiers |= MOD_SNAP_INVERT;
668                                 t->redraw |= TREDRAW_HARD;
669                                 break;
670                         case TFM_MODAL_SNAP_INV_OFF:
671                                 t->modifiers &= ~MOD_SNAP_INVERT;
672                                 t->redraw |= TREDRAW_HARD;
673                                 break;
674                         case TFM_MODAL_SNAP_TOGGLE:
675                                 t->modifiers ^= MOD_SNAP;
676                                 t->redraw |= TREDRAW_HARD;
677                                 break;
678                         case TFM_MODAL_AXIS_X:
679                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
680                                         if (cmode == 'X') {
681                                                 stopConstraint(t);
682                                         }
683                                         else {
684                                                 if (t->flag & T_2D_EDIT) {
685                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
686                                                 }
687                                                 else {
688                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS0), "along %s X");
689                                                 }
690                                         }
691                                         t->redraw |= TREDRAW_HARD;
692                                 }
693                                 break;
694                         case TFM_MODAL_AXIS_Y:
695                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
696                                         if (cmode == 'Y') {
697                                                 stopConstraint(t);
698                                         }
699                                         else {
700                                                 if (t->flag & T_2D_EDIT) {
701                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
702                                                 }
703                                                 else {
704                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS1), "along %s Y");
705                                                 }
706                                         }
707                                         t->redraw |= TREDRAW_HARD;
708                                 }
709                                 break;
710                         case TFM_MODAL_AXIS_Z:
711                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
712                                         if (cmode == 'Z') {
713                                                 stopConstraint(t);
714                                         }
715                                         else {
716                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS2), "along %s Z");
717                                         }
718                                         t->redraw |= TREDRAW_HARD;
719                                 }
720                                 break;
721                         case TFM_MODAL_PLANE_X:
722                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
723                                         if (cmode == 'X') {
724                                                 stopConstraint(t);
725                                         }
726                                         else {
727                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
728                                         }
729                                         t->redraw |= TREDRAW_HARD;
730                                 }
731                                 break;
732                         case TFM_MODAL_PLANE_Y:
733                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
734                                         if (cmode == 'Y') {
735                                                 stopConstraint(t);
736                                         }
737                                         else {
738                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
739                                         }
740                                         t->redraw |= TREDRAW_HARD;
741                                 }
742                                 break;
743                         case TFM_MODAL_PLANE_Z:
744                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
745                                         if (cmode == 'Z') {
746                                                 stopConstraint(t);
747                                         }
748                                         else {
749                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
750                                         }
751                                         t->redraw |= TREDRAW_HARD;
752                                 }
753                                 break;
754                         case TFM_MODAL_CONS_OFF:
755                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
756                                         stopConstraint(t);
757                                         t->redraw |= TREDRAW_HARD;
758                                 }
759                                 break;
760                         case TFM_MODAL_ADD_SNAP:
761                                 addSnapPoint(t);
762                                 t->redraw |= TREDRAW_HARD;
763                                 break;
764                         case TFM_MODAL_REMOVE_SNAP:
765                                 removeSnapPoint(t);
766                                 t->redraw |= TREDRAW_HARD;
767                                 break;
768                         case TFM_MODAL_PROPSIZE_UP:
769                                 if (t->flag & T_PROP_EDIT) {
770                                         t->prop_size*= 1.1f;
771                                         if (t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
772                                                 t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
773                                         calculatePropRatio(t);
774                                 }
775                                 t->redraw |= TREDRAW_HARD;
776                                 break;
777                         case TFM_MODAL_PROPSIZE_DOWN:
778                                 if (t->flag & T_PROP_EDIT) {
779                                         t->prop_size*= 0.90909090f;
780                                         calculatePropRatio(t);
781                                 }
782                                 t->redraw |= TREDRAW_HARD;
783                                 break;
784                         case TFM_MODAL_AUTOIK_LEN_INC:
785                                 if (t->flag & T_AUTOIK)
786                                         transform_autoik_update(t, 1);
787                                 t->redraw |= TREDRAW_HARD;
788                                 break;
789                         case TFM_MODAL_AUTOIK_LEN_DEC:
790                                 if (t->flag & T_AUTOIK) 
791                                         transform_autoik_update(t, -1);
792                                 t->redraw |= TREDRAW_HARD;
793                                 break;
794                         default:
795                                 handled = 0;
796                                 break;
797                 }
798
799                 // Modal numinput events
800                 t->redraw |= handleNumInput(&(t->num), event);
801         }
802         /* else do non-mapped events */
803         else if (event->val == KM_PRESS) {
804                 switch (event->type) {
805                 case RIGHTMOUSE:
806                         t->state = TRANS_CANCEL;
807                         break;
808                 /* enforce redraw of transform when modifiers are used */
809                 case LEFTSHIFTKEY:
810                 case RIGHTSHIFTKEY:
811                         t->modifiers |= MOD_CONSTRAINT_PLANE;
812                         t->redraw |= TREDRAW_HARD;
813                         break;
814
815                 case SPACEKEY:
816                         if ((t->spacetype==SPACE_VIEW3D) && event->alt) {
817 #if 0 // TRANSFORM_FIX_ME
818                                 int mval[2];
819
820                                 getmouseco_sc(mval);
821                                 BIF_selectOrientation();
822                                 calc_manipulator_stats(curarea);
823                                 copy_m3_m4(t->spacemtx, G.vd->twmat);
824                                 warp_pointer(mval[0], mval[1]);
825 #endif
826                         }
827                         else {
828                                 t->state = TRANS_CONFIRM;
829                         }
830                         break;
831
832                 case MIDDLEMOUSE:
833                         if ((t->flag & T_NO_CONSTRAINT)==0) {
834                                 /* exception for switching to dolly, or trackball, in camera view */
835                                 if (t->flag & T_CAMERA) {
836                                         if (t->mode==TFM_TRANSLATION)
837                                                 setLocalConstraint(t, (CON_AXIS2), "along local Z");
838                                         else if (t->mode==TFM_ROTATION) {
839                                                 restoreTransObjects(t);
840                                                 initTrackball(t);
841                                         }
842                                 }
843                                 else {
844                                         t->modifiers |= MOD_CONSTRAINT_SELECT;
845                                         if (t->con.mode & CON_APPLY) {
846                                                 stopConstraint(t);
847                                         }
848                                         else {
849                                                 if (event->shift) {
850                                                         initSelectConstraint(t, t->spacemtx);
851                                                 }
852                                                 else {
853                                                         /* bit hackish... but it prevents mmb select to print the orientation from menu */
854                                                         strcpy(t->spacename, "global");
855                                                         initSelectConstraint(t, mati);
856                                                 }
857                                                 postSelectConstraint(t);
858                                         }
859                                 }
860                                 t->redraw |= TREDRAW_HARD;
861                         }
862                         break;
863                 case ESCKEY:
864                         t->state = TRANS_CANCEL;
865                         break;
866                 case PADENTER:
867                 case RETKEY:
868                         t->state = TRANS_CONFIRM;
869                         break;
870                 case GKEY:
871                         /* only switch when... */
872                         if ( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
873                                 resetTransRestrictions(t);
874                                 restoreTransObjects(t);
875                                 initTranslation(t);
876                                 initSnapping(t, NULL); // need to reinit after mode change
877                                 t->redraw |= TREDRAW_HARD;
878                         }
879                         break;
880                 case SKEY:
881                         /* only switch when... */
882                         if ( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
883                                 resetTransRestrictions(t);
884                                 restoreTransObjects(t);
885                                 initResize(t);
886                                 initSnapping(t, NULL); // need to reinit after mode change
887                                 t->redraw |= TREDRAW_HARD;
888                         }
889                         break;
890                 case RKEY:
891                         /* only switch when... */
892                         if (!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
893                                 if ( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
894
895                                         resetTransRestrictions(t);
896
897                                         if (t->mode == TFM_ROTATION) {
898                                                 restoreTransObjects(t);
899                                                 initTrackball(t);
900                                         }
901                                         else {
902                                                 restoreTransObjects(t);
903                                                 initRotation(t);
904                                         }
905                                         initSnapping(t, NULL); // need to reinit after mode change
906                                         t->redraw |= TREDRAW_HARD;
907                                 }
908                         }
909                         break;
910                 case CKEY:
911                         if (event->alt) {
912                                 t->flag ^= T_PROP_CONNECTED;
913                                 sort_trans_data_dist(t);
914                                 calculatePropRatio(t);
915                                 t->redraw= 1;
916                         }
917                         else {
918                                 stopConstraint(t);
919                                 t->redraw |= TREDRAW_HARD;
920                         }
921                         break;
922                 case XKEY:
923                         if ((t->flag & T_NO_CONSTRAINT)==0) {
924                                 if (t->flag & T_2D_EDIT) {
925                                         if (cmode == 'X') {
926                                                 stopConstraint(t);
927                                         }
928                                         else {
929                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
930                                         }
931                                 }
932                                 else {
933                                         if (cmode == 'X') {
934                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
935                                                         stopConstraint(t);
936                                                 }
937                                                 else {
938                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
939                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
940                                                                 setUserConstraint(t, orientation, (CON_AXIS0), "along %s X");
941                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
942                                                                 setUserConstraint(t, orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
943                                                 }
944                                         }
945                                         else {
946                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
947                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along %s X");
948                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
949                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1|CON_AXIS2), "locking %s X");
950                                         }
951                                 }
952                                 t->redraw |= TREDRAW_HARD;
953                         }
954                         break;
955                 case YKEY:
956                         if ((t->flag & T_NO_CONSTRAINT)==0) {
957                                 if (t->flag & T_2D_EDIT) {
958                                         if (cmode == 'Y') {
959                                                 stopConstraint(t);
960                                         }
961                                         else {
962                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
963                                         }
964                                 }
965                                 else {
966                                         if (cmode == 'Y') {
967                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
968                                                         stopConstraint(t);
969                                                 }
970                                                 else {
971                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
972                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
973                                                                 setUserConstraint(t, orientation, (CON_AXIS1), "along %s Y");
974                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
975                                                                 setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
976                                                 }
977                                         }
978                                         else {
979                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
980                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along %s Y");
981                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
982                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS2), "locking %s Y");
983                                         }
984                                 }
985                                 t->redraw |= TREDRAW_HARD;
986                         }
987                         break;
988                 case ZKEY:
989                         if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))==0) {
990                                 if (cmode == 'Z') {
991                                         if (t->con.orientation != V3D_MANIP_GLOBAL) {
992                                                 stopConstraint(t);
993                                         }
994                                         else {
995                                                 short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
996                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
997                                                         setUserConstraint(t, orientation, (CON_AXIS2), "along %s Z");
998                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
999                                                         setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
1000                                         }
1001                                 }
1002                                 else {
1003                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
1004                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS2), "along %s Z");
1005                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
1006                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS1), "locking %s Z");
1007                                 }
1008                                 t->redraw |= TREDRAW_HARD;
1009                         }
1010                         break;
1011                 case OKEY:
1012                         if (t->flag & T_PROP_EDIT && event->shift) {
1013                                 t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1014                                 calculatePropRatio(t);
1015                                 t->redraw |= TREDRAW_HARD;
1016                         }
1017                         break;
1018                 case PADPLUSKEY:
1019                         if (event->alt && t->flag & T_PROP_EDIT) {
1020                                 t->prop_size *= 1.1f;
1021                                 if (t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1022                                         t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
1023                                 calculatePropRatio(t);
1024                         }
1025                         t->redraw= 1;
1026                         break;
1027                 case PAGEUPKEY:
1028                 case WHEELDOWNMOUSE:
1029                         if (t->flag & T_AUTOIK) {
1030                                 transform_autoik_update(t, 1);
1031                         }
1032                         else view_editmove(event->type);
1033                         t->redraw= 1;
1034                         break;
1035                 case PADMINUS:
1036                         if (event->alt && t->flag & T_PROP_EDIT) {
1037                                 t->prop_size*= 0.90909090f;
1038                                 calculatePropRatio(t);
1039                         }
1040                         t->redraw= 1;
1041                         break;
1042                 case PAGEDOWNKEY:
1043                 case WHEELUPMOUSE:
1044                         if (t->flag & T_AUTOIK) {
1045                                 transform_autoik_update(t, -1);
1046                         }
1047                         else view_editmove(event->type);
1048                         t->redraw= 1;
1049                         break;
1050                 default:
1051                         handled = 0;
1052                         break;
1053                 }
1054
1055                 // Numerical input events
1056                 t->redraw |= handleNumInput(&(t->num), event);
1057
1058                 // Snapping key events
1059                 t->redraw |= handleSnapping(t, event);
1060
1061         }
1062         else if (event->val==KM_RELEASE) {
1063                 switch (event->type) {
1064                 case LEFTSHIFTKEY:
1065                 case RIGHTSHIFTKEY:
1066                         t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1067                         t->redraw |= TREDRAW_HARD;
1068                         break;
1069
1070                 case MIDDLEMOUSE:
1071                         if ((t->flag & T_NO_CONSTRAINT)==0) {
1072                                 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1073                                 postSelectConstraint(t);
1074                                 t->redraw |= TREDRAW_HARD;
1075                         }
1076                         break;
1077 //              case LEFTMOUSE:
1078 //              case RIGHTMOUSE:
1079 //                      if (WM_modal_tweak_exit(event, t->event_type))
1080 ////                    if (t->options & CTX_TWEAK)
1081 //                              t->state = TRANS_CONFIRM;
1082 //                      break;
1083                 default:
1084                         handled = 0;
1085                         break;
1086                 }
1087
1088                 /* confirm transform if launch key is released after mouse move */
1089                 if (t->flag & T_RELEASE_CONFIRM) {
1090                         /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1091                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1092                                 t->state = TRANS_CONFIRM;
1093                         }
1094                 }
1095         }
1096
1097         // Per transform event, if present
1098         if (t->handleEvent)
1099                 t->redraw |= t->handleEvent(t, event);
1100
1101         if (handled || t->redraw)
1102                 return 0;
1103         else
1104                 return OPERATOR_PASS_THROUGH;
1105 }
1106
1107 int calculateTransformCenter(bContext *C, int centerMode, float *vec)
1108 {
1109         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1110         int success = 1;
1111
1112         t->state = TRANS_RUNNING;
1113
1114         t->options = CTX_NONE;
1115
1116         t->mode = TFM_DUMMY;
1117
1118         initTransInfo(C, t, NULL, NULL);        // internal data, mouse, vectors
1119
1120         createTransData(C, t);                          // make TransData structs from selection
1121
1122         t->around = centerMode;                         // override userdefined mode
1123
1124         if (t->total == 0) {
1125                 success = 0;
1126         }
1127         else {
1128                 success = 1;
1129
1130                 calculateCenter(t);
1131
1132                 // Copy center from constraint center. Transform center can be local
1133                 copy_v3_v3(vec, t->con.center);
1134         }
1135
1136
1137         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1138         special_aftertrans_update(C, t);
1139
1140         postTrans(C, t);
1141
1142         MEM_freeN(t);
1143
1144         return success;
1145 }
1146
1147 typedef enum {
1148         UP,
1149         DOWN,
1150         LEFT,
1151         RIGHT
1152 } ArrowDirection;
1153 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1154 {
1155         switch (d) {
1156                 case LEFT:
1157                         offset = -offset;
1158                         length = -length;
1159                         size = -size;
1160                 case RIGHT:
1161                         glBegin(GL_LINES);
1162                         glVertex2s( offset, 0);
1163                         glVertex2s( offset + length, 0);
1164                         glVertex2s( offset + length, 0);
1165                         glVertex2s( offset + length - size, -size);
1166                         glVertex2s( offset + length, 0);
1167                         glVertex2s( offset + length - size,  size);
1168                         glEnd();
1169                         break;
1170                 case DOWN:
1171                         offset = -offset;
1172                         length = -length;
1173                         size = -size;
1174                 case UP:
1175                         glBegin(GL_LINES);
1176                         glVertex2s( 0, offset);
1177                         glVertex2s( 0, offset + length);
1178                         glVertex2s( 0, offset + length);
1179                         glVertex2s(-size, offset + length - size);
1180                         glVertex2s( 0, offset + length);
1181                         glVertex2s( size, offset + length - size);
1182                         glEnd();
1183                         break;
1184         }
1185 }
1186
1187 static void drawArrowHead(ArrowDirection d, short size)
1188 {
1189         switch (d) {
1190                 case LEFT:
1191                         size = -size;
1192                 case RIGHT:
1193                         glBegin(GL_LINES);
1194                         glVertex2s( 0, 0);
1195                         glVertex2s( -size, -size);
1196                         glVertex2s( 0, 0);
1197                         glVertex2s( -size,  size);
1198                         glEnd();
1199                         break;
1200                 case DOWN:
1201                         size = -size;
1202                 case UP:
1203                         glBegin(GL_LINES);
1204                         glVertex2s( 0, 0);
1205                         glVertex2s(-size, -size);
1206                         glVertex2s( 0, 0);
1207                         glVertex2s( size, -size);
1208                         glEnd();
1209                         break;
1210         }
1211 }
1212
1213 static void drawArc(float size, float angle_start, float angle_end, int segments)
1214 {
1215         float delta = (angle_end - angle_start) / segments;
1216         float angle;
1217
1218         glBegin(GL_LINE_STRIP);
1219
1220         for ( angle = angle_start; angle < angle_end; angle += delta) {
1221                 glVertex2f( cosf(angle) * size, sinf(angle) * size);
1222         }
1223         glVertex2f( cosf(angle_end) * size, sinf(angle_end) * size);
1224
1225         glEnd();
1226 }
1227
1228 static int helpline_poll(bContext *C)
1229 {
1230         ARegion *ar= CTX_wm_region(C);
1231         
1232         if (ar && ar->regiontype==RGN_TYPE_WINDOW)
1233                 return 1;
1234         return 0;
1235 }
1236
1237 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1238 {
1239         TransInfo *t = (TransInfo*)customdata;
1240
1241         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1242                 float vecrot[3], cent[2];
1243                 int mval[2];
1244
1245                 mval[0]= x;
1246                 mval[1]= y;
1247
1248                 copy_v3_v3(vecrot, t->center);
1249                 if (t->flag & T_EDIT) {
1250                         Object *ob= t->obedit;
1251                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1252                 }
1253                 else if (t->flag & T_POSE) {
1254                         Object *ob=t->poseobj;
1255                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1256                 }
1257
1258                 projectFloatView(t, vecrot, cent);      // no overflow in extreme cases
1259
1260                 glPushMatrix();
1261
1262                 switch (t->helpline) {
1263                         case HLP_SPRING:
1264                                 UI_ThemeColor(TH_WIRE);
1265
1266                                 setlinestyle(3);
1267                                 glBegin(GL_LINE_STRIP);
1268                                 glVertex2iv(t->mval);
1269                                 glVertex2fv(cent);
1270                                 glEnd();
1271
1272                                 glTranslatef(mval[0], mval[1], 0);
1273                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1274
1275                                 setlinestyle(0);
1276                                 glLineWidth(3.0);
1277                                 drawArrow(UP, 5, 10, 5);
1278                                 drawArrow(DOWN, 5, 10, 5);
1279                                 glLineWidth(1.0);
1280                                 break;
1281                         case HLP_HARROW:
1282                                 UI_ThemeColor(TH_WIRE);
1283
1284                                 glTranslatef(mval[0], mval[1], 0);
1285
1286                                 glLineWidth(3.0);
1287                                 drawArrow(RIGHT, 5, 10, 5);
1288                                 drawArrow(LEFT, 5, 10, 5);
1289                                 glLineWidth(1.0);
1290                                 break;
1291                         case HLP_VARROW:
1292                                 UI_ThemeColor(TH_WIRE);
1293
1294                                 glTranslatef(mval[0], mval[1], 0);
1295
1296                                 glLineWidth(3.0);
1297                                 glBegin(GL_LINES);
1298                                 drawArrow(UP, 5, 10, 5);
1299                                 drawArrow(DOWN, 5, 10, 5);
1300                                 glLineWidth(1.0);
1301                                 break;
1302                         case HLP_ANGLE:
1303                                 {
1304                                         float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1305                                         float angle = atan2f(dy, dx);
1306                                         float dist = sqrtf(dx*dx + dy*dy);
1307                                         float delta_angle = MIN2(15.0f / dist, (float)M_PI/4.0f);
1308                                         float spacing_angle = MIN2(5.0f / dist, (float)M_PI/12.0f);
1309                                         UI_ThemeColor(TH_WIRE);
1310
1311                                         setlinestyle(3);
1312                                         glBegin(GL_LINE_STRIP);
1313                                         glVertex2iv(t->mval);
1314                                         glVertex2fv(cent);
1315                                         glEnd();
1316
1317                                         glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1318
1319                                         setlinestyle(0);
1320                                         glLineWidth(3.0);
1321                                         drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1322                                         drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1323
1324                                         glPushMatrix();
1325
1326                                         glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1327                                         glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1328
1329                                         drawArrowHead(DOWN, 5);
1330
1331                                         glPopMatrix();
1332
1333                                         glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1334                                         glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1335
1336                                         drawArrowHead(UP, 5);
1337
1338                                         glLineWidth(1.0);
1339                                         break;
1340                                 }
1341                                 case HLP_TRACKBALL:
1342                                 {
1343                                         unsigned char col[3], col2[3];
1344                                         UI_GetThemeColor3ubv(TH_GRID, col);
1345
1346                                         glTranslatef(mval[0], mval[1], 0);
1347
1348                                         glLineWidth(3.0);
1349
1350                                         UI_make_axis_color(col, col2, 'X');
1351                                         glColor3ubv((GLubyte *)col2);
1352
1353                                         drawArrow(RIGHT, 5, 10, 5);
1354                                         drawArrow(LEFT, 5, 10, 5);
1355
1356                                         UI_make_axis_color(col, col2, 'Y');
1357                                         glColor3ubv((GLubyte *)col2);
1358
1359                                         drawArrow(UP, 5, 10, 5);
1360                                         drawArrow(DOWN, 5, 10, 5);
1361                                         glLineWidth(1.0);
1362                                         break;
1363                                 }
1364                 }
1365
1366                 glPopMatrix();
1367         }
1368 }
1369
1370 static void drawTransformView(const struct bContext *C, struct ARegion *UNUSED(ar), void *arg)
1371 {
1372         TransInfo *t = arg;
1373
1374         drawConstraint(t);
1375         drawPropCircle(C, t);
1376         drawSnapping(C, t);
1377 }
1378
1379 #if 0
1380 static void drawTransformPixel(const struct bContext *UNUSED(C), struct ARegion *UNUSED(ar), void *UNUSED(arg))
1381 {
1382 //      TransInfo *t = arg;
1383 //
1384 //      drawHelpline(C, t->mval[0], t->mval[1], t);
1385 }
1386 #endif
1387
1388 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1389 {
1390         ToolSettings *ts = CTX_data_tool_settings(C);
1391         int constraint_axis[3] = {0, 0, 0};
1392         int proportional = 0;
1393         PropertyRNA *prop;
1394
1395         // Save back mode in case we're in the generic operator
1396         if ((prop= RNA_struct_find_property(op->ptr, "mode"))) {
1397                 RNA_property_enum_set(op->ptr, prop, t->mode);
1398         }
1399
1400         if ((prop= RNA_struct_find_property(op->ptr, "value"))) {
1401                 float *values= (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1402                 if (RNA_property_array_check(prop)) {
1403                         RNA_property_float_set_array(op->ptr, prop, values);
1404                 }
1405                 else {
1406                         RNA_property_float_set(op->ptr, prop, values[0]);
1407                 }
1408         }
1409
1410         /* convert flag to enum */
1411         switch (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
1412                 case (T_PROP_EDIT|T_PROP_CONNECTED):
1413                         proportional = PROP_EDIT_CONNECTED;
1414                         break;
1415                 case T_PROP_EDIT:
1416                         proportional = PROP_EDIT_ON;
1417                         break;
1418                 default:
1419                         proportional = PROP_EDIT_OFF;
1420         }
1421
1422         // If modal, save settings back in scene if not set as operator argument
1423         if (t->flag & T_MODAL) {
1424
1425                 /* save settings if not set in operator */
1426                 if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1427                     !RNA_property_is_set(op->ptr, prop))
1428                 {
1429                         if (t->obedit)
1430                                 ts->proportional = proportional;
1431                         else
1432                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1433                 }
1434
1435                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1436                     !RNA_property_is_set(op->ptr, prop))
1437                 {
1438                         ts->proportional_size = t->prop_size;
1439                 }
1440
1441                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1442                     !RNA_property_is_set(op->ptr, prop))
1443                 {
1444                         ts->prop_mode = t->prop_mode;
1445                 }
1446                 
1447                 /* do we check for parameter? */
1448                 if (t->modifiers & MOD_SNAP) {
1449                         ts->snap_flag |= SCE_SNAP;
1450                 }
1451                 else {
1452                         ts->snap_flag &= ~SCE_SNAP;
1453                 }
1454
1455                 if (t->spacetype == SPACE_VIEW3D) {
1456                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1457                             !RNA_property_is_set(op->ptr, prop))
1458                         {
1459                                 View3D *v3d = t->view;
1460
1461                                 v3d->twmode = t->current_orientation;
1462                         }
1463                 }
1464         }
1465         
1466         if (RNA_struct_find_property(op->ptr, "proportional")) {
1467                 RNA_enum_set(op->ptr, "proportional", proportional);
1468                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1469                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1470         }
1471
1472         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1473                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1474         }
1475
1476         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1477                 RNA_property_boolean_set(op->ptr, prop, t->flag & T_MIRROR);
1478         }
1479
1480         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1481                 /* constraint orientation can be global, event if user selects something else
1482                  * so use the orientation in the constraint if set
1483                  * */
1484                 if (t->con.mode & CON_APPLY) {
1485                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1486                 }
1487                 else {
1488                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1489                 }
1490
1491                 if (t->con.mode & CON_APPLY) {
1492                         if (t->con.mode & CON_AXIS0) {
1493                                 constraint_axis[0] = 1;
1494                         }
1495                         if (t->con.mode & CON_AXIS1) {
1496                                 constraint_axis[1] = 1;
1497                         }
1498                         if (t->con.mode & CON_AXIS2) {
1499                                 constraint_axis[2] = 1;
1500                         }
1501                 }
1502
1503                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
1504         }
1505 }
1506
1507 /* note: caller needs to free 't' on a 0 return */
1508 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1509 {
1510         int options = 0;
1511         PropertyRNA *prop;
1512
1513         t->context = C;
1514
1515         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1516
1517         t->state = TRANS_STARTING;
1518
1519         if ( (prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
1520                 if (RNA_property_boolean_get(op->ptr, prop)) {
1521                         options |= CTX_TEXTURE;
1522                 }
1523         }
1524         
1525         t->options = options;
1526
1527         t->mode = mode;
1528
1529         t->launch_event = event ? event->type : -1;
1530
1531         if (t->launch_event == EVT_TWEAK_R) {
1532                 t->launch_event = RIGHTMOUSE;
1533         }
1534         else if (t->launch_event == EVT_TWEAK_L) {
1535                 t->launch_event = LEFTMOUSE;
1536         }
1537
1538         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1539         // For manipulator only, so assume LEFTMOUSE
1540         if (t->launch_event == 0) {
1541                 t->launch_event = LEFTMOUSE;
1542         }
1543
1544         if (!initTransInfo(C, t, op, event)) {  /* internal data, mouse, vectors */
1545                 return 0;
1546         }
1547
1548         if (t->spacetype == SPACE_VIEW3D) {
1549                 //calc_manipulator_stats(curarea);
1550                 initTransformOrientation(C, t);
1551
1552                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1553                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1554                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1555                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1556         }
1557         else if (t->spacetype == SPACE_IMAGE) {
1558                 unit_m3(t->spacemtx);
1559                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1560                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1561                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1562         }
1563         else if (t->spacetype == SPACE_CLIP) {
1564                 unit_m3(t->spacemtx);
1565                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1566                 t->options |= CTX_MOVIECLIP;
1567         }
1568         else
1569                 unit_m3(t->spacemtx);
1570
1571         createTransData(C, t);                  // make TransData structs from selection
1572
1573         if (t->total == 0) {
1574                 postTrans(C, t);
1575                 return 0;
1576         }
1577
1578         /* Stupid code to have Ctrl-Click on manipulator work ok */
1579         if (event) {
1580                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1581                 wmKeyMapItem *kmi;
1582
1583                 for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1584                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
1585                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
1586                                     (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1587                                     (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
1588                                     ((kmi->type == OSKEY) &&                         event->oskey) )
1589                                 {
1590                                         t->modifiers |= MOD_SNAP_INVERT;
1591                                 }
1592                                 break;
1593                         }
1594                 }
1595
1596         }
1597
1598         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1599
1600         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1601         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1602         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1603         mode = t->mode;
1604
1605         calculatePropRatio(t);
1606         calculateCenter(t);
1607
1608         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1609
1610         switch (mode) {
1611         case TFM_TRANSLATION:
1612                 initTranslation(t);
1613                 break;
1614         case TFM_ROTATION:
1615                 initRotation(t);
1616                 break;
1617         case TFM_RESIZE:
1618                 initResize(t);
1619                 break;
1620         case TFM_TOSPHERE:
1621                 initToSphere(t);
1622                 break;
1623         case TFM_SHEAR:
1624                 initShear(t);
1625                 break;
1626         case TFM_WARP:
1627                 initWarp(t);
1628                 break;
1629         case TFM_SHRINKFATTEN:
1630                 initShrinkFatten(t);
1631                 break;
1632         case TFM_TILT:
1633                 initTilt(t);
1634                 break;
1635         case TFM_CURVE_SHRINKFATTEN:
1636                 initCurveShrinkFatten(t);
1637                 break;
1638         case TFM_TRACKBALL:
1639                 initTrackball(t);
1640                 break;
1641         case TFM_PUSHPULL:
1642                 initPushPull(t);
1643                 break;
1644         case TFM_CREASE:
1645                 initCrease(t);
1646                 break;
1647         case TFM_BONESIZE:
1648                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1649                         bArmature *arm= t->poseobj->data;
1650                         if (arm->drawtype==ARM_ENVELOPE)
1651                                 initBoneEnvelope(t);
1652                         else
1653                                 initBoneSize(t);
1654                 }
1655                 break;
1656         case TFM_BONE_ENVELOPE:
1657                 initBoneEnvelope(t);
1658                 break;
1659         case TFM_EDGE_SLIDE:
1660                 initEdgeSlide(t);
1661                 break;
1662         case TFM_BONE_ROLL:
1663                 initBoneRoll(t);
1664                 break;
1665         case TFM_TIME_TRANSLATE:
1666                 initTimeTranslate(t);
1667                 break;
1668         case TFM_TIME_SLIDE:
1669                 initTimeSlide(t);
1670                 break;
1671         case TFM_TIME_SCALE:
1672                 initTimeScale(t);
1673                 break;
1674         case TFM_TIME_DUPLICATE:
1675                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1676                  * so that duplicate-culling will work properly
1677                  */
1678                 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1679                         initTranslation(t);
1680                 else
1681                         initTimeTranslate(t);
1682                 t->mode = mode;
1683                 break;
1684         case TFM_TIME_EXTEND:
1685                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1686                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1687                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1688                  * depending on which editor this was called from
1689                  */
1690                 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1691                         initTranslation(t);
1692                 else
1693                         initTimeTranslate(t);
1694                 break;
1695         case TFM_BAKE_TIME:
1696                 initBakeTime(t);
1697                 break;
1698         case TFM_MIRROR:
1699                 initMirror(t);
1700                 break;
1701         case TFM_BEVEL:
1702                 initBevel(t);
1703                 break;
1704         case TFM_BWEIGHT:
1705                 initBevelWeight(t);
1706                 break;
1707         case TFM_ALIGN:
1708                 initAlign(t);
1709                 break;
1710         case TFM_SEQ_SLIDE:
1711                 initSeqSlide(t);
1712                 break;
1713         }
1714
1715         if (t->state == TRANS_CANCEL) {
1716                 postTrans(C, t);
1717                 return 0;
1718         }
1719
1720
1721         /* overwrite initial values if operator supplied a non-null vector */
1722         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
1723                 float values[4]= {0}; /* in case value isn't length 4, avoid uninitialized memory  */
1724
1725                 if (RNA_property_array_check(prop)) {
1726                         RNA_float_get_array(op->ptr, "value", values);
1727                 }
1728                 else {
1729                         values[0]= RNA_float_get(op->ptr, "value");
1730                 }
1731
1732                 copy_v4_v4(t->values, values);
1733                 copy_v4_v4(t->auto_values, values);
1734                 t->flag |= T_AUTOVALUES;
1735         }
1736
1737         /* Transformation axis from operator */
1738         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
1739                 RNA_property_float_get_array(op->ptr, prop, t->axis);
1740                 normalize_v3(t->axis);
1741                 copy_v3_v3(t->axis_orig, t->axis);
1742         }
1743
1744         /* Constraint init from operator */
1745         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
1746                 int constraint_axis[3];
1747
1748                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
1749
1750                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
1751                         t->con.mode |= CON_APPLY;
1752
1753                         if (constraint_axis[0]) {
1754                                 t->con.mode |= CON_AXIS0;
1755                         }
1756                         if (constraint_axis[1]) {
1757                                 t->con.mode |= CON_AXIS1;
1758                         }
1759                         if (constraint_axis[2]) {
1760                                 t->con.mode |= CON_AXIS2;
1761                         }
1762
1763                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1764                 }
1765         }
1766
1767         t->context = NULL;
1768
1769         return 1;
1770 }
1771
1772 void transformApply(bContext *C, TransInfo *t)
1773 {
1774         t->context = C;
1775
1776         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
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)