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