style cleanup, brackets in else/if, some indentation.
[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                         exit_code = OPERATOR_FINISHED;
1832                 }
1833
1834                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1835                 special_aftertrans_update(C, t);
1836
1837                 /* free data */
1838                 postTrans(C, t);
1839
1840                 /* send events out for redraws */
1841                 viewRedrawPost(C, t);
1842
1843                 /*  Undo as last, certainly after special_trans_update! */
1844
1845                 if(t->state == TRANS_CANCEL) {
1846 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1847                 }
1848                 else {
1849 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1850 //                      else ED_undo_push(C, transform_to_undostr(t));
1851                 }
1852                 t->undostr= NULL;
1853
1854                 viewRedrawForce(C, t);
1855         }
1856
1857         t->context = NULL;
1858
1859         return exit_code;
1860 }
1861
1862 /* ************************** TRANSFORM LOCKS **************************** */
1863
1864 static void protectedTransBits(short protectflag, float *vec)
1865 {
1866         if(protectflag & OB_LOCK_LOCX)
1867                 vec[0]= 0.0f;
1868         if(protectflag & OB_LOCK_LOCY)
1869                 vec[1]= 0.0f;
1870         if(protectflag & OB_LOCK_LOCZ)
1871                 vec[2]= 0.0f;
1872 }
1873
1874 static void protectedSizeBits(short protectflag, float *size)
1875 {
1876         if(protectflag & OB_LOCK_SCALEX)
1877                 size[0]= 1.0f;
1878         if(protectflag & OB_LOCK_SCALEY)
1879                 size[1]= 1.0f;
1880         if(protectflag & OB_LOCK_SCALEZ)
1881                 size[2]= 1.0f;
1882 }
1883
1884 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1885 {
1886         if(protectflag & OB_LOCK_ROTX)
1887                 eul[0]= oldeul[0];
1888         if(protectflag & OB_LOCK_ROTY)
1889                 eul[1]= oldeul[1];
1890         if(protectflag & OB_LOCK_ROTZ)
1891                 eul[2]= oldeul[2];
1892 }
1893
1894
1895 /* this function only does the delta rotation */
1896 /* axis-angle is usually internally stored as quats... */
1897 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1898 {
1899         /* check that protection flags are set */
1900         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1901                 return;
1902         
1903         if (protectflag & OB_LOCK_ROT4D) {
1904                 /* axis-angle getting limited as 4D entities that they are... */
1905                 if (protectflag & OB_LOCK_ROTW)
1906                         *angle= oldAngle;
1907                 if (protectflag & OB_LOCK_ROTX)
1908                         axis[0]= oldAxis[0];
1909                 if (protectflag & OB_LOCK_ROTY)
1910                         axis[1]= oldAxis[1];
1911                 if (protectflag & OB_LOCK_ROTZ)
1912                         axis[2]= oldAxis[2];
1913         }
1914         else {
1915                 /* axis-angle get limited with euler... */
1916                 float eul[3], oldeul[3];
1917                 
1918                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1919                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1920                 
1921                 if (protectflag & OB_LOCK_ROTX)
1922                         eul[0]= oldeul[0];
1923                 if (protectflag & OB_LOCK_ROTY)
1924                         eul[1]= oldeul[1];
1925                 if (protectflag & OB_LOCK_ROTZ)
1926                         eul[2]= oldeul[2];
1927                 
1928                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1929                 
1930                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1931                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1932                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1933                         axis[1]= 1.0f;
1934                 }
1935         }
1936 }
1937
1938 /* this function only does the delta rotation */
1939 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1940 {
1941         /* check that protection flags are set */
1942         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1943                 return;
1944         
1945         if (protectflag & OB_LOCK_ROT4D) {
1946                 /* quaternions getting limited as 4D entities that they are... */
1947                 if (protectflag & OB_LOCK_ROTW)
1948                         quat[0]= oldquat[0];
1949                 if (protectflag & OB_LOCK_ROTX)
1950                         quat[1]= oldquat[1];
1951                 if (protectflag & OB_LOCK_ROTY)
1952                         quat[2]= oldquat[2];
1953                 if (protectflag & OB_LOCK_ROTZ)
1954                         quat[3]= oldquat[3];
1955         }
1956         else {
1957                 /* quaternions get limited with euler... (compatibility mode) */
1958                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1959                 float qlen;
1960
1961                 qlen= normalize_qt_qt(nquat, quat);
1962                 normalize_qt_qt(noldquat, oldquat);
1963
1964                 quat_to_eul(eul, nquat);
1965                 quat_to_eul(oldeul, noldquat);
1966
1967                 if (protectflag & OB_LOCK_ROTX)
1968                         eul[0]= oldeul[0];
1969                 if (protectflag & OB_LOCK_ROTY)
1970                         eul[1]= oldeul[1];
1971                 if (protectflag & OB_LOCK_ROTZ)
1972                         eul[2]= oldeul[2];
1973
1974                 eul_to_quat( quat,eul);
1975
1976                 /* restore original quat size */
1977                 mul_qt_fl(quat, qlen);
1978                 
1979                 /* quaternions flip w sign to accumulate rotations correctly */
1980                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
1981                         mul_qt_fl(quat, -1.0f);
1982                 }
1983         }
1984 }
1985
1986 /* ******************* TRANSFORM LIMITS ********************** */
1987
1988 static void constraintTransLim(TransInfo *t, TransData *td)
1989 {
1990         if (td->con) {
1991                 bConstraintTypeInfo *ctiLoc= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
1992                 bConstraintTypeInfo *ctiDist= get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
1993                 
1994                 bConstraintOb cob= {NULL};
1995                 bConstraint *con;
1996                 float ctime = (float)(t->scene->r.cfra);
1997                 
1998                 /* Make a temporary bConstraintOb for using these limit constraints
1999                  *      - they only care that cob->matrix is correctly set ;-)
2000                  *      - current space should be local
2001                  */
2002                 unit_m4(cob.matrix);
2003                 copy_v3_v3(cob.matrix[3], td->loc);
2004                 
2005                 /* Evaluate valid constraints */
2006                 for (con= td->con; con; con= con->next) {
2007                         bConstraintTypeInfo *cti = NULL;
2008                         ListBase targets = {NULL, NULL};
2009                         float tmat[4][4];
2010                         
2011                         /* only consider constraint if enabled */
2012                         if (con->flag & CONSTRAINT_DISABLE) continue;
2013                         if (con->enforce == 0.0f) continue;
2014                         
2015                         /* only use it if it's tagged for this purpose (and the right type) */
2016                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2017                                 bLocLimitConstraint *data= con->data;
2018                                 
2019                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2020                                         continue;
2021                                 cti = ctiLoc;
2022                         }
2023                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2024                                 bDistLimitConstraint *data= con->data;
2025                                 
2026                                 if ((data->flag & LIMITDIST_TRANSFORM)==0)
2027                                         continue;
2028                                 cti = ctiDist;
2029                         }
2030                         
2031                         if (cti) {
2032                                 /* do space conversions */
2033                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2034                                         /* just multiply by td->mtx (this should be ok) */
2035                                         copy_m4_m4(tmat, cob.matrix);
2036                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2037                                 }
2038                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2039                                         /* skip... incompatable spacetype */
2040                                         continue;
2041                                 }
2042                                 
2043                                 /* get constraint targets if needed */
2044                                 get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2045                                 
2046                                 /* do constraint */
2047                                 cti->evaluate_constraint(con, &cob, &targets);
2048                                 
2049                                 /* convert spaces again */
2050                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2051                                         /* just multiply by td->mtx (this should be ok) */
2052                                         copy_m4_m4(tmat, cob.matrix);
2053                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2054                                 }
2055                                 
2056                                 /* free targets list */
2057                                 BLI_freelistN(&targets);
2058                         }
2059                 }
2060                 
2061                 /* copy results from cob->matrix */
2062                 copy_v3_v3(td->loc, cob.matrix[3]);
2063         }
2064 }
2065
2066 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2067 {
2068         /* Make a temporary bConstraintOb for use by limit constraints
2069          *      - they only care that cob->matrix is correctly set ;-)
2070          *      - current space should be local
2071          */
2072         memset(cob, 0, sizeof(bConstraintOb));
2073         if (td->ext)
2074         {
2075                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2076                         /* quats */
2077                         /* objects and bones do normalization first too, otherwise
2078                          * we don't necessarily end up with a rotation matrix, and
2079                          * then conversion back to quat gives a different result */
2080                         float quat[4];
2081                         normalize_qt_qt(quat, td->ext->quat);
2082                         quat_to_mat4(cob->matrix, quat);
2083                 }
2084                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2085                         /* axis angle */
2086                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2087                 }
2088                 else {
2089                         /* eulers */
2090                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2091                 }
2092         }
2093 }
2094
2095 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2096 {
2097         if (td->con) {
2098                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2099                 bConstraintOb cob;
2100                 bConstraint *con;
2101                 int dolimit = 0;
2102                 
2103                 /* Evaluate valid constraints */
2104                 for (con= td->con; con; con= con->next) {
2105                         /* only consider constraint if enabled */
2106                         if (con->flag & CONSTRAINT_DISABLE) continue;
2107                         if (con->enforce == 0.0f) continue;
2108                         
2109                         /* we're only interested in Limit-Rotation constraints */
2110                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2111                                 bRotLimitConstraint *data= con->data;
2112                                 float tmat[4][4];
2113                                 
2114                                 /* only use it if it's tagged for this purpose */
2115                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2116                                         continue;
2117
2118                                 /* skip incompatable spacetypes */
2119                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2120                                         continue;
2121
2122                                 /* only do conversion if necessary, to preserve quats and eulers */
2123                                 if(!dolimit) {
2124                                         constraintob_from_transdata(&cob, td);
2125                                         dolimit= 1;
2126                                 }
2127                                 
2128                                 /* do space conversions */
2129                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2130                                         /* just multiply by td->mtx (this should be ok) */
2131                                         copy_m4_m4(tmat, cob.matrix);
2132                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2133                                 }
2134                                 
2135                                 /* do constraint */
2136                                 cti->evaluate_constraint(con, &cob, NULL);
2137                                 
2138                                 /* convert spaces again */
2139                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2140                                         /* just multiply by td->mtx (this should be ok) */
2141                                         copy_m4_m4(tmat, cob.matrix);
2142                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2143                                 }
2144                         }
2145                 }
2146                 
2147                 if(dolimit) {
2148                         /* copy results from cob->matrix */
2149                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2150                                 /* quats */
2151                                 mat4_to_quat( td->ext->quat,cob.matrix);
2152                         }
2153                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2154                                 /* axis angle */
2155                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2156                         }
2157                         else {
2158                                 /* eulers */
2159                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2160                         }
2161                 }
2162         }
2163 }
2164
2165 static void constraintSizeLim(TransInfo *t, TransData *td)
2166 {
2167         if (td->con && td->ext) {
2168                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2169                 bConstraintOb cob= {NULL};
2170                 bConstraint *con;
2171                 
2172                 /* Make a temporary bConstraintOb for using these limit constraints
2173                  *      - they only care that cob->matrix is correctly set ;-)
2174                  *      - current space should be local
2175                  */
2176                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2177                         /* scale val and reset size */
2178                         return; // TODO: fix this case
2179                 }
2180                 else {
2181                         /* Reset val if SINGLESIZE but using a constraint */
2182                         if (td->flag & TD_SINGLESIZE)
2183                                 return;
2184                         
2185                         size_to_mat4( cob.matrix,td->ext->size);
2186                 }
2187                 
2188                 /* Evaluate valid constraints */
2189                 for (con= td->con; con; con= con->next) {
2190                         /* only consider constraint if enabled */
2191                         if (con->flag & CONSTRAINT_DISABLE) continue;
2192                         if (con->enforce == 0.0f) continue;
2193                         
2194                         /* we're only interested in Limit-Scale constraints */
2195                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2196                                 bSizeLimitConstraint *data= con->data;
2197                                 float tmat[4][4];
2198                                 
2199                                 /* only use it if it's tagged for this purpose */
2200                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2201                                         continue;
2202                                 
2203                                 /* do space conversions */
2204                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2205                                         /* just multiply by td->mtx (this should be ok) */
2206                                         copy_m4_m4(tmat, cob.matrix);
2207                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2208                                 }
2209                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2210                                         /* skip... incompatable spacetype */
2211                                         continue;
2212                                 }
2213                                 
2214                                 /* do constraint */
2215                                 cti->evaluate_constraint(con, &cob, NULL);
2216                                 
2217                                 /* convert spaces again */
2218                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2219                                         /* just multiply by td->mtx (this should be ok) */
2220                                         copy_m4_m4(tmat, cob.matrix);
2221                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2222                                 }
2223                         }
2224                 }
2225                 
2226                 /* copy results from cob->matrix */
2227                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2228                         /* scale val and reset size */
2229                         return; // TODO: fix this case
2230                 }
2231                 else {
2232                         /* Reset val if SINGLESIZE but using a constraint */
2233                         if (td->flag & TD_SINGLESIZE)
2234                                 return;
2235                         
2236                         mat4_to_size( td->ext->size,cob.matrix);
2237                 }
2238         }
2239 }
2240
2241 /* ************************** WARP *************************** */
2242
2243 static void postInputWarp(TransInfo *t, float values[3])
2244 {
2245         mul_v3_fl(values, (float)(M_PI * 2));
2246
2247         if (t->customData) /* non-null value indicates reversed input */
2248         {
2249                 negate_v3(values);
2250         }
2251 }
2252
2253 void initWarp(TransInfo *t)
2254 {
2255         float max[3], min[3];
2256         int i;
2257         
2258         t->mode = TFM_WARP;
2259         t->transform = Warp;
2260         t->handleEvent = handleEventWarp;
2261         
2262         setInputPostFct(&t->mouse, postInputWarp);
2263         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2264         
2265         t->idx_max = 0;
2266         t->num.idx_max = 0;
2267         t->snap[0] = 0.0f;
2268         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2269         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2270         
2271         t->num.increment = 1.0f;
2272
2273         t->flag |= T_NO_CONSTRAINT;
2274         
2275         /* we need min/max in view space */
2276         for(i = 0; i < t->total; i++) {
2277                 float center[3];
2278                 copy_v3_v3(center, t->data[i].center);
2279                 mul_m3_v3(t->data[i].mtx, center);
2280                 mul_m4_v3(t->viewmat, center);
2281                 sub_v3_v3(center, t->viewmat[3]);
2282                 if (i)
2283                         minmax_v3v3_v3(min, max, center);
2284                 else {
2285                         copy_v3_v3(max, center);
2286                         copy_v3_v3(min, center);
2287                 }
2288         }
2289
2290         mid_v3_v3v3(t->center, min, max);
2291
2292         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2293         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2294 }
2295
2296 int handleEventWarp(TransInfo *t, wmEvent *event)
2297 {
2298         int status = 0;
2299         
2300         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2301         {
2302                 // Use customData pointer to signal warp direction
2303                 if      (t->customData == NULL)
2304                         t->customData = (void*)1;
2305                 else
2306                         t->customData = NULL;
2307                 
2308                 status = 1;
2309         }
2310         
2311         return status;
2312 }
2313
2314 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2315 {
2316         TransData *td = t->data;
2317         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2318         int i;
2319         char str[50];
2320         
2321         curs= give_cursor(t->scene, t->view);
2322         /*
2323          * gcursor is the one used for helpline.
2324          * It has to be in the same space as the drawing loop
2325          * (that means it needs to be in the object's space when in edit mode and
2326          *  in global space in object mode)
2327          *
2328          * cursor is used for calculations.
2329          * It needs to be in view space, but we need to take object's offset
2330          * into account if in Edit mode.
2331          */
2332         copy_v3_v3(cursor, curs);
2333         copy_v3_v3(gcursor, cursor);
2334         if (t->flag & T_EDIT) {
2335                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2336                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2337                 mul_m3_v3(t->data->smtx, gcursor);
2338         }
2339         mul_m4_v3(t->viewmat, cursor);
2340         sub_v3_v3(cursor, t->viewmat[3]);
2341         
2342         /* amount of radians for warp */
2343         circumfac = t->values[0];
2344         
2345         snapGrid(t, &circumfac);
2346         applyNumInput(&t->num, &circumfac);
2347         
2348         /* header print for NumInput */
2349         if (hasNumInput(&t->num)) {
2350                 char c[20];
2351                 
2352                 outputNumInput(&(t->num), c);
2353                 
2354                 sprintf(str, "Warp: %s", c);
2355
2356                 circumfac = DEG2RADF(circumfac);
2357         }
2358         else {
2359                 /* default header print */
2360                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2361         }
2362         
2363         t->values[0] = circumfac;
2364
2365         circumfac /= 2; /* only need 180 on each side to make 360 */
2366         
2367         for(i = 0; i < t->total; i++, td++) {
2368                 float loc[3];
2369                 if (td->flag & TD_NOACTION)
2370                         break;
2371                 
2372                 if (td->flag & TD_SKIP)
2373                         continue;
2374                 
2375                 /* translate point to center, rotate in such a way that outline==distance */
2376                 copy_v3_v3(vec, td->iloc);
2377                 mul_m3_v3(td->mtx, vec);
2378                 mul_m4_v3(t->viewmat, vec);
2379                 sub_v3_v3(vec, t->viewmat[3]);
2380                 
2381                 dist= vec[0]-cursor[0];
2382                 
2383                 /* t->val is X dimension projected boundbox */
2384                 phi0= (circumfac*dist/t->val);
2385                 
2386                 vec[1]= (vec[1]-cursor[1]);
2387                 
2388                 co= (float)cos(phi0);
2389                 si= (float)sin(phi0);
2390                 loc[0]= -si*vec[1]+cursor[0];
2391                 loc[1]= co*vec[1]+cursor[1];
2392                 loc[2]= vec[2];
2393                 
2394                 mul_m4_v3(t->viewinv, loc);
2395                 sub_v3_v3(loc, t->viewinv[3]);
2396                 mul_m3_v3(td->smtx, loc);
2397                 
2398                 sub_v3_v3(loc, td->iloc);
2399                 mul_v3_fl(loc, td->factor);
2400                 add_v3_v3v3(td->loc, td->iloc, loc);
2401         }
2402         
2403         recalcData(t);
2404         
2405         ED_area_headerprint(t->sa, str);
2406         
2407         return 1;
2408 }
2409
2410 /* ************************** SHEAR *************************** */
2411
2412 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2413 {
2414         mul_v3_fl(values, 0.05f);
2415 }
2416
2417 void initShear(TransInfo *t)
2418 {
2419         t->mode = TFM_SHEAR;
2420         t->transform = Shear;
2421         t->handleEvent = handleEventShear;
2422         
2423         setInputPostFct(&t->mouse, postInputShear);
2424         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2425         
2426         t->idx_max = 0;
2427         t->num.idx_max = 0;
2428         t->snap[0] = 0.0f;
2429         t->snap[1] = 0.1f;
2430         t->snap[2] = t->snap[1] * 0.1f;
2431         
2432         t->num.increment = 0.1f;
2433
2434         t->flag |= T_NO_CONSTRAINT;
2435 }
2436
2437 int handleEventShear(TransInfo *t, wmEvent *event)
2438 {
2439         int status = 0;
2440         
2441         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2442         {
2443                 // Use customData pointer to signal Shear direction
2444                 if      (t->customData == NULL)
2445                 {
2446                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2447                         t->customData = (void*)1;
2448                 }
2449                 else {
2450                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2451                         t->customData = NULL;
2452                 }
2453                 
2454                 status = 1;
2455         }
2456         
2457         return status;
2458 }
2459
2460
2461 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2462 {
2463         TransData *td = t->data;
2464         float vec[3];
2465         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2466         float value;
2467         int i;
2468         char str[50];
2469         
2470         copy_m3_m4(persmat, t->viewmat);
2471         invert_m3_m3(persinv, persmat);
2472         
2473         value = t->values[0];
2474         
2475         snapGrid(t, &value);
2476         
2477         applyNumInput(&t->num, &value);
2478         
2479         /* header print for NumInput */
2480         if (hasNumInput(&t->num)) {
2481                 char c[20];
2482                 
2483                 outputNumInput(&(t->num), c);
2484                 
2485                 sprintf(str, "Shear: %s %s", c, t->proptext);
2486         }
2487         else {
2488                 /* default header print */
2489                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2490         }
2491         
2492         unit_m3(smat);
2493         
2494         // Custom data signals shear direction
2495         if (t->customData == NULL)
2496                 smat[1][0] = value;
2497         else
2498                 smat[0][1] = value;
2499         
2500         mul_m3_m3m3(tmat, smat, persmat);
2501         mul_m3_m3m3(totmat, persinv, tmat);
2502         
2503         for(i = 0 ; i < t->total; i++, td++) {
2504                 if (td->flag & TD_NOACTION)
2505                         break;
2506                 
2507                 if (td->flag & TD_SKIP)
2508                         continue;
2509                 
2510                 if (t->obedit) {
2511                         float mat3[3][3];
2512                         mul_m3_m3m3(mat3, totmat, td->mtx);
2513                         mul_m3_m3m3(tmat, td->smtx, mat3);
2514                 }
2515                 else {
2516                         copy_m3_m3(tmat, totmat);
2517                 }
2518                 sub_v3_v3v3(vec, td->center, t->center);
2519                 
2520                 mul_m3_v3(tmat, vec);
2521                 
2522                 add_v3_v3(vec, t->center);
2523                 sub_v3_v3(vec, td->center);
2524                 
2525                 mul_v3_fl(vec, td->factor);
2526                 
2527                 add_v3_v3v3(td->loc, td->iloc, vec);
2528         }
2529         
2530         recalcData(t);
2531         
2532         ED_area_headerprint(t->sa, str);
2533
2534         return 1;
2535 }
2536
2537 /* ************************** RESIZE *************************** */
2538
2539 void initResize(TransInfo *t)
2540 {
2541         t->mode = TFM_RESIZE;
2542         t->transform = Resize;
2543         
2544         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2545         
2546         t->flag |= T_NULL_ONE;
2547         t->num.flag |= NUM_NULL_ONE;
2548         t->num.flag |= NUM_AFFECT_ALL;
2549         if (!t->obedit) {
2550                 t->flag |= T_NO_ZERO;
2551                 t->num.flag |= NUM_NO_ZERO;
2552         }
2553         
2554         t->idx_max = 2;
2555         t->num.idx_max = 2;
2556         t->snap[0] = 0.0f;
2557         t->snap[1] = 0.1f;
2558         t->snap[2] = t->snap[1] * 0.1f;
2559
2560         t->num.increment = t->snap[1];
2561 }
2562
2563 static void headerResize(TransInfo *t, float vec[3], char *str)
2564 {
2565         char tvec[60];
2566         char *spos= str;
2567         if (hasNumInput(&t->num)) {
2568                 outputNumInput(&(t->num), tvec);
2569         }
2570         else {
2571                 BLI_snprintf(&tvec[0],  20, "%.4f", vec[0]);
2572                 BLI_snprintf(&tvec[20], 20, "%.4f", vec[1]);
2573                 BLI_snprintf(&tvec[40], 20, "%.4f", vec[2]);
2574         }
2575         
2576         if (t->con.mode & CON_APPLY) {
2577                 switch(t->num.idx_max) {
2578                 case 0:
2579                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2580                         break;
2581                 case 1:
2582                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2583                         break;
2584                 case 2:
2585                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2586                 }
2587         }
2588         else {
2589                 if (t->flag & T_2D_EDIT)
2590                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2591                 else
2592                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2593         }
2594         
2595         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2596                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2597         }
2598
2599         (void)spos;
2600 }
2601
2602 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2603 #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)
2604
2605 /* smat is reference matrix, only scaled */
2606 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2607 {
2608         float vec[3];
2609         
2610         copy_v3_v3(vec, mat[0]);
2611         size[0]= normalize_v3(vec);
2612         copy_v3_v3(vec, mat[1]);
2613         size[1]= normalize_v3(vec);
2614         copy_v3_v3(vec, mat[2]);
2615         size[2]= normalize_v3(vec);
2616         
2617         /* first tried with dotproduct... but the sign flip is crucial */
2618         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2619         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2620         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2621 }
2622
2623
2624 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3])
2625 {
2626         float tmat[3][3], smat[3][3], center[3];
2627         float vec[3];
2628         
2629         if (t->flag & T_EDIT) {
2630                 mul_m3_m3m3(smat, mat, td->mtx);
2631                 mul_m3_m3m3(tmat, td->smtx, smat);
2632         }
2633         else {
2634                 copy_m3_m3(tmat, mat);
2635         }
2636         
2637         if (t->con.applySize) {
2638                 t->con.applySize(t, td, tmat);
2639         }
2640         
2641         /* local constraint shouldn't alter center */
2642         if ((t->around == V3D_LOCAL) &&
2643                 (   (t->flag & (T_OBJECT|T_POSE)) ||
2644                     ((t->flag & T_EDIT) && (t->settings->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE))) ||
2645                     (t->obedit && t->obedit->type == OB_ARMATURE))
2646                 )
2647         {
2648                 copy_v3_v3(center, td->center);
2649         }
2650         else {
2651                 copy_v3_v3(center, t->center);
2652         }
2653
2654         if (td->ext) {
2655                 float fsize[3];
2656                 
2657                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2658                         float obsizemat[3][3];
2659                         // Reorient the size mat to fit the oriented object.
2660                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2661                         //print_m3("obsizemat", obsizemat);
2662                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2663                         //print_v3("fsize", fsize);
2664                 }
2665                 else {
2666                         mat3_to_size( fsize,tmat);
2667                 }
2668                 
2669                 protectedSizeBits(td->protectflag, fsize);
2670                 
2671                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2672                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2673                                 /* scale val and reset size */
2674                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2675                                 
2676                                 td->ext->size[0] = td->ext->isize[0];
2677                                 td->ext->size[1] = td->ext->isize[1];
2678                                 td->ext->size[2] = td->ext->isize[2];
2679                          }
2680                         else {
2681                                 /* Reset val if SINGLESIZE but using a constraint */
2682                                 if (td->flag & TD_SINGLESIZE)
2683                                          *td->val = td->ival;
2684                                 
2685                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2686                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2687                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2688                         }
2689                 }
2690                 
2691                 constraintSizeLim(t, td);
2692         }
2693         
2694         /* For individual element center, Editmode need to use iloc */
2695         if (t->flag & T_POINTS)
2696                 sub_v3_v3v3(vec, td->iloc, center);
2697         else
2698                 sub_v3_v3v3(vec, td->center, center);
2699         
2700         mul_m3_v3(tmat, vec);
2701         
2702         add_v3_v3(vec, center);
2703         if (t->flag & T_POINTS)
2704                 sub_v3_v3(vec, td->iloc);
2705         else
2706                 sub_v3_v3(vec, td->center);
2707         
2708         mul_v3_fl(vec, td->factor);
2709         
2710         if (t->flag & (T_OBJECT|T_POSE)) {
2711                 mul_m3_v3(td->smtx, vec);
2712         }
2713         
2714         protectedTransBits(td->protectflag, vec);
2715         add_v3_v3v3(td->loc, td->iloc, vec);
2716         
2717         constraintTransLim(t, td);
2718 }
2719
2720 int Resize(TransInfo *t, const int mval[2])
2721 {
2722         TransData *td;
2723         float size[3], mat[3][3];
2724         float ratio;
2725         int i;
2726         char str[200];
2727         
2728         /* for manipulator, center handle, the scaling can't be done relative to center */
2729         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2730         {
2731                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2732         }
2733         else {
2734                 ratio = t->values[0];
2735         }
2736         
2737         size[0] = size[1] = size[2] = ratio;
2738         
2739         snapGrid(t, size);
2740         
2741         if (hasNumInput(&t->num)) {
2742                 applyNumInput(&t->num, size);
2743                 constraintNumInput(t, size);
2744         }
2745         
2746         applySnapping(t, size);
2747         
2748         if (t->flag & T_AUTOVALUES)
2749         {
2750                 copy_v3_v3(size, t->auto_values);
2751         }
2752         
2753         copy_v3_v3(t->values, size);
2754         
2755         size_to_mat3( mat,size);
2756         
2757         if (t->con.applySize) {
2758                 t->con.applySize(t, NULL, mat);
2759         }
2760         
2761         copy_m3_m3(t->mat, mat);        // used in manipulator
2762         
2763         headerResize(t, size, str);
2764         
2765         for(i = 0, td=t->data; i < t->total; i++, td++) {
2766                 if (td->flag & TD_NOACTION)
2767                         break;
2768                 
2769                 if (td->flag & TD_SKIP)
2770                         continue;
2771                 
2772                 ElementResize(t, td, mat);
2773         }
2774         
2775         /* evil hack - redo resize if cliping needed */
2776         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2777                 size_to_mat3( mat,size);
2778                 
2779                 if (t->con.applySize)
2780                         t->con.applySize(t, NULL, mat);
2781                 
2782                 for(i = 0, td=t->data; i < t->total; i++, td++)
2783                         ElementResize(t, td, mat);
2784         }
2785         
2786         recalcData(t);
2787         
2788         ED_area_headerprint(t->sa, str);
2789         
2790         return 1;
2791 }
2792
2793 /* ************************** TOSPHERE *************************** */
2794
2795 void initToSphere(TransInfo *t)
2796 {
2797         TransData *td = t->data;
2798         int i;
2799         
2800         t->mode = TFM_TOSPHERE;
2801         t->transform = ToSphere;
2802         
2803         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2804         
2805         t->idx_max = 0;
2806         t->num.idx_max = 0;
2807         t->snap[0] = 0.0f;
2808         t->snap[1] = 0.1f;
2809         t->snap[2] = t->snap[1] * 0.1f;
2810         
2811         t->num.increment = t->snap[1];
2812
2813         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2814         t->flag |= T_NO_CONSTRAINT;
2815         
2816         // Calculate average radius
2817         for(i = 0 ; i < t->total; i++, td++) {
2818                 t->val += len_v3v3(t->center, td->iloc);
2819         }
2820         
2821         t->val /= (float)t->total;
2822 }
2823
2824 int ToSphere(TransInfo *t, const int UNUSED(mval[2]))
2825 {
2826         float vec[3];
2827         float ratio, radius;
2828         int i;
2829         char str[64];
2830         TransData *td = t->data;
2831         
2832         ratio = t->values[0];
2833         
2834         snapGrid(t, &ratio);
2835         
2836         applyNumInput(&t->num, &ratio);
2837         
2838         if (ratio < 0)
2839                 ratio = 0.0f;
2840         else if (ratio > 1)
2841                 ratio = 1.0f;
2842         
2843         t->values[0] = ratio;
2844
2845         /* header print for NumInput */
2846         if (hasNumInput(&t->num)) {
2847                 char c[20];
2848                 
2849                 outputNumInput(&(t->num), c);
2850                 
2851                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2852         }
2853         else {
2854                 /* default header print */
2855                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2856         }
2857         
2858         
2859         for(i = 0 ; i < t->total; i++, td++) {
2860                 float tratio;
2861                 if (td->flag & TD_NOACTION)
2862                         break;
2863                 
2864                 if (td->flag & TD_SKIP)
2865                         continue;
2866                 
2867                 sub_v3_v3v3(vec, td->iloc, t->center);
2868                 
2869                 radius = normalize_v3(vec);
2870                 
2871                 tratio = ratio * td->factor;
2872                 
2873                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2874                 
2875                 add_v3_v3v3(td->loc, t->center, vec);
2876         }
2877         
2878         
2879         recalcData(t);
2880         
2881         ED_area_headerprint(t->sa, str);
2882         
2883         return 1;
2884 }
2885
2886 /* ************************** ROTATION *************************** */
2887
2888
2889 static void postInputRotation(TransInfo *t, float values[3])
2890 {
2891         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2892                 t->con.applyRot(t, NULL, t->axis, values);
2893         }
2894 }
2895
2896 void initRotation(TransInfo *t)
2897 {
2898         t->mode = TFM_ROTATION;
2899         t->transform = Rotation;
2900         
2901         setInputPostFct(&t->mouse, postInputRotation);
2902         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2903         
2904         t->idx_max = 0;
2905         t->num.idx_max = 0;
2906         t->snap[0] = 0.0f;
2907         t->snap[1] = (float)((5.0/180)*M_PI);
2908         t->snap[2] = t->snap[1] * 0.2f;
2909         
2910         t->num.increment = 1.0f;
2911
2912         if (t->flag & T_2D_EDIT)
2913                 t->flag |= T_NO_CONSTRAINT;
2914
2915         negate_v3_v3(t->axis, t->viewinv[2]);
2916         normalize_v3(t->axis);
2917
2918         copy_v3_v3(t->axis_orig, t->axis);
2919 }
2920
2921 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around)
2922 {
2923         float vec[3], totmat[3][3], smat[3][3];
2924         float eul[3], fmat[3][3], quat[4];
2925         float *center = t->center;
2926
2927         /* local constraint shouldn't alter center */
2928         if (around == V3D_LOCAL) {
2929                 if (    (t->flag & (T_OBJECT|T_POSE)) ||
2930                     (t->settings->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_FACE)) ||
2931                         (t->obedit && t->obedit->type == OB_ARMATURE))
2932                 {
2933                         center = td->center;
2934                 }
2935         }
2936
2937         if (t->flag & T_POINTS) {
2938                 mul_m3_m3m3(totmat, mat, td->mtx);
2939                 mul_m3_m3m3(smat, td->smtx, totmat);
2940                 
2941                 sub_v3_v3v3(vec, td->iloc, center);
2942                 mul_m3_v3(smat, vec);
2943                 
2944                 add_v3_v3v3(td->loc, vec, center);
2945                 
2946                 sub_v3_v3v3(vec,td->loc,td->iloc);
2947                 protectedTransBits(td->protectflag, vec);
2948                 add_v3_v3v3(td->loc, td->iloc, vec);
2949                 
2950                 
2951                 if(td->flag & TD_USEQUAT) {
2952                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, NULL, NULL, NULL, NULL, NULL);
2953                         mat3_to_quat( quat,fmat);       // Actual transform
2954                         
2955                         if(td->ext->quat) {
2956                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2957                                 
2958                                 /* is there a reason not to have this here? -jahka */
2959                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2960                         }
2961                 }
2962         }
2963         /**
2964          * HACK WARNING
2965          *
2966          * This is some VERY ugly special case to deal with pose mode.
2967          *
2968          * The problem is that mtx and smtx include each bone orientation.
2969          *
2970          * That is needed to rotate each bone properly, HOWEVER, to calculate
2971          * the translation component, we only need the actual armature object's
2972          * matrix (and inverse). That is not all though. Once the proper translation
2973          * has been computed, it has to be converted back into the bone's space.
2974          */
2975         else if (t->flag & T_POSE) {