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