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