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