Merged changes in the trunk up to revision 36551.
[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                 int out[2] = {0, 0};
196 #if 0
197                 SpaceAction *sact = t->sa->spacedata.first;
198
199                 if (sact->flag & SACTION_DRAWTIME) {
200                         //vec[0] = vec[0]/((t->scene->r.frs_sec / t->scene->r.frs_sec_base));
201                         /* same as below */
202                         UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out+1);
203                 } 
204                 else
205 #endif
206                 {
207                         UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out+1);
208                 }
209
210                 adr[0]= out[0];
211                 adr[1]= out[1];
212         }
213         else if(ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
214                 int out[2] = {0, 0};
215
216                 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out+1);
217                 adr[0]= out[0];
218                 adr[1]= out[1];
219         }
220         else if(t->spacetype==SPACE_SEQ) { /* XXX not tested yet, but should work */
221                 int out[2] = {0, 0};
222
223                 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out+1);
224                 adr[0]= out[0];
225                 adr[1]= out[1];
226         }
227 }
228
229 void projectFloatView(TransInfo *t, float *vec, float *adr)
230 {
231         if (t->spacetype==SPACE_VIEW3D) {
232                 if(t->ar->regiontype == RGN_TYPE_WINDOW)
233                         project_float_noclip(t->ar, vec, adr);
234         }
235         else if(t->spacetype==SPACE_IMAGE) {
236                 int a[2];
237
238                 projectIntView(t, vec, a);
239                 adr[0]= a[0];
240                 adr[1]= a[1];
241         }
242         else if(ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
243                 int a[2];
244
245                 projectIntView(t, vec, a);
246                 adr[0]= a[0];
247                 adr[1]= a[1];
248         }
249 }
250
251 void applyAspectRatio(TransInfo *t, float *vec)
252 {
253         if ((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
254                 SpaceImage *sima= t->sa->spacedata.first;
255                 float aspx, aspy;
256
257                 if((sima->flag & SI_COORDFLOATS)==0) {
258                         int width, height;
259                         ED_space_image_size(sima, &width, &height);
260
261                         vec[0] *= width;
262                         vec[1] *= height;
263                 }
264
265                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
266                 vec[0] /= aspx;
267                 vec[1] /= aspy;
268         }
269 }
270
271 void removeAspectRatio(TransInfo *t, float *vec)
272 {
273         if ((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
274                 SpaceImage *sima= t->sa->spacedata.first;
275                 float aspx, aspy;
276
277                 if((sima->flag & SI_COORDFLOATS)==0) {
278                         int width, height;
279                         ED_space_image_size(sima, &width, &height);
280
281                         vec[0] /= width;
282                         vec[1] /= height;
283                 }
284
285                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
286                 vec[0] *= aspx;
287                 vec[1] *= aspy;
288         }
289 }
290
291 static void viewRedrawForce(const bContext *C, TransInfo *t)
292 {
293         if (t->spacetype == SPACE_VIEW3D)
294         {
295                 /* Do we need more refined tags? */
296                 if(t->flag & T_POSE)
297                         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
298                 else
299                         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
300                 
301                 /* for realtime animation record - send notifiers recognised by animation editors */
302                 // XXX: is this notifier a lame duck?
303                 if ((t->animtimer) && IS_AUTOKEY_ON(t->scene))
304                         WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
305                 
306         }
307         else if (t->spacetype == SPACE_ACTION) {
308                 //SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
309                 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
310         }
311         else if (t->spacetype == SPACE_IPO) {
312                 //SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
313                 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
314         }
315         else if (t->spacetype == SPACE_NLA) {
316                 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
317         }
318         else if(t->spacetype == SPACE_NODE)
319         {
320                 //ED_area_tag_redraw(t->sa);
321                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE_VIEW, NULL);
322         }
323         else if(t->spacetype == SPACE_SEQ)
324         {
325                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, NULL);
326         }
327         else if (t->spacetype==SPACE_IMAGE) {
328                 // XXX how to deal with lock?
329                 SpaceImage *sima= (SpaceImage*)t->sa->spacedata.first;
330                 if(sima->lock) WM_event_add_notifier(C, NC_GEOM|ND_DATA, t->obedit->data);
331                 else ED_area_tag_redraw(t->sa);
332         }
333 }
334
335 static void viewRedrawPost(bContext *C, TransInfo *t)
336 {
337         ED_area_headerprint(t->sa, NULL);
338         
339         if(t->spacetype == SPACE_VIEW3D) {
340                 /* if autokeying is enabled, send notifiers that keyframes were added */
341                 if (IS_AUTOKEY_ON(t->scene))
342                         WM_main_add_notifier(NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
343                 
344                 /* XXX temp, first hack to get auto-render in compositor work (ton) */
345                 WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM_DONE, CTX_data_scene(C));
346
347         }
348         
349 #if 0 // TRANSFORM_FIX_ME
350         if(t->spacetype==SPACE_VIEW3D) {
351                 allqueue(REDRAWBUTSOBJECT, 0);
352                 allqueue(REDRAWVIEW3D, 0);
353         }
354         else if(t->spacetype==SPACE_IMAGE) {
355                 allqueue(REDRAWIMAGE, 0);
356                 allqueue(REDRAWVIEW3D, 0);
357         }
358         else if(ELEM3(t->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_IPO)) {
359                 allqueue(REDRAWVIEW3D, 0);
360                 allqueue(REDRAWACTION, 0);
361                 allqueue(REDRAWNLA, 0);
362                 allqueue(REDRAWIPO, 0);
363                 allqueue(REDRAWTIME, 0);
364                 allqueue(REDRAWBUTSOBJECT, 0);
365         }
366
367         scrarea_queue_headredraw(curarea);
368 #endif
369 }
370
371 /* ************************** TRANSFORMATIONS **************************** */
372
373 void BIF_selectOrientation(void) {
374 #if 0 // TRANSFORM_FIX_ME
375         short val;
376         char *str_menu = BIF_menustringTransformOrientation("Orientation");
377         val= pupmenu(str_menu);
378         MEM_freeN(str_menu);
379
380         if(val >= 0) {
381                 G.vd->twmode = val;
382         }
383 #endif
384 }
385
386 static void view_editmove(unsigned short UNUSED(event))
387 {
388 #if 0 // TRANSFORM_FIX_ME
389         int refresh = 0;
390         /* Regular:   Zoom in */
391         /* Shift:     Scroll up */
392         /* Ctrl:      Scroll right */
393         /* Alt-Shift: Rotate up */
394         /* Alt-Ctrl:  Rotate right */
395
396         /* only work in 3D window for now
397          * In the end, will have to send to event to a 2D window handler instead
398          */
399         if (Trans.flag & T_2D_EDIT)
400                 return;
401
402         switch(event) {
403                 case WHEELUPMOUSE:
404
405                         if( G.qual & LR_SHIFTKEY ) {
406                                 if( G.qual & LR_ALTKEY ) {
407                                         G.qual &= ~LR_SHIFTKEY;
408                                         persptoetsen(PAD2);
409                                         G.qual |= LR_SHIFTKEY;
410                                 } else {
411                                         persptoetsen(PAD2);
412                                 }
413                         } else if( G.qual & LR_CTRLKEY ) {
414                                 if( G.qual & LR_ALTKEY ) {
415                                         G.qual &= ~LR_CTRLKEY;
416                                         persptoetsen(PAD4);
417                                         G.qual |= LR_CTRLKEY;
418                                 } else {
419                                         persptoetsen(PAD4);
420                                 }
421                         } else if(U.uiflag & USER_WHEELZOOMDIR)
422                                 persptoetsen(PADMINUS);
423                         else
424                                 persptoetsen(PADPLUSKEY);
425
426                         refresh = 1;
427                         break;
428                 case WHEELDOWNMOUSE:
429                         if( G.qual & LR_SHIFTKEY ) {
430                                 if( G.qual & LR_ALTKEY ) {
431                                         G.qual &= ~LR_SHIFTKEY;
432                                         persptoetsen(PAD8);
433                                         G.qual |= LR_SHIFTKEY;
434                                 } else {
435                                         persptoetsen(PAD8);
436                                 }
437                         } else if( G.qual & LR_CTRLKEY ) {
438                                 if( G.qual & LR_ALTKEY ) {
439                                         G.qual &= ~LR_CTRLKEY;
440                                         persptoetsen(PAD6);
441                                         G.qual |= LR_CTRLKEY;
442                                 } else {
443                                         persptoetsen(PAD6);
444                                 }
445                         } else if(U.uiflag & USER_WHEELZOOMDIR)
446                                 persptoetsen(PADPLUSKEY);
447                         else
448                                 persptoetsen(PADMINUS);
449
450                         refresh = 1;
451                         break;
452         }
453
454         if (refresh)
455                 setTransformViewMatrices(&Trans);
456 #endif
457 }
458
459 /* ************************************************* */
460
461 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
462 #define TFM_MODAL_CANCEL                1
463 #define TFM_MODAL_CONFIRM               2
464 #define TFM_MODAL_TRANSLATE             3
465 #define TFM_MODAL_ROTATE                4
466 #define TFM_MODAL_RESIZE                5
467 #define TFM_MODAL_SNAP_INV_ON   6
468 #define TFM_MODAL_SNAP_INV_OFF  7
469 #define TFM_MODAL_SNAP_TOGGLE   8
470 #define TFM_MODAL_AXIS_X                9
471 #define TFM_MODAL_AXIS_Y                10
472 #define TFM_MODAL_AXIS_Z                11
473 #define TFM_MODAL_PLANE_X               12
474 #define TFM_MODAL_PLANE_Y               13
475 #define TFM_MODAL_PLANE_Z               14
476 #define TFM_MODAL_CONS_OFF              15
477 #define TFM_MODAL_ADD_SNAP              16
478 #define TFM_MODAL_REMOVE_SNAP   17
479 /*      18 and 19 used by numinput, defined in transform.h
480  * */
481 #define TFM_MODAL_PROPSIZE_UP   20
482 #define TFM_MODAL_PROPSIZE_DOWN 21
483 #define TFM_MODAL_AUTOIK_LEN_INC 22
484 #define TFM_MODAL_AUTOIK_LEN_DEC 23
485
486 /* called in transform_ops.c, on each regeneration of keymaps */
487 wmKeyMap* transform_modal_keymap(wmKeyConfig *keyconf)
488 {
489         static EnumPropertyItem modal_items[] = {
490         {TFM_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
491         {TFM_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
492         {TFM_MODAL_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
493         {TFM_MODAL_ROTATE, "ROTATE", 0, "Rotate", ""},
494         {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
495         {TFM_MODAL_SNAP_INV_ON, "SNAP_INV_ON", 0, "Invert Snap On", ""},
496         {TFM_MODAL_SNAP_INV_OFF, "SNAP_INV_OFF", 0, "Invert Snap Off", ""},
497         {TFM_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap Toggle", ""},
498         {TFM_MODAL_AXIS_X, "AXIS_X", 0, "Orientation X axis", ""},
499         {TFM_MODAL_AXIS_Y, "AXIS_Y", 0, "Orientation Y axis", ""},
500         {TFM_MODAL_AXIS_Z, "AXIS_Z", 0, "Orientation Z axis", ""},
501         {TFM_MODAL_PLANE_X, "PLANE_X", 0, "Orientation X plane", ""},
502         {TFM_MODAL_PLANE_Y, "PLANE_Y", 0, "Orientation Y plane", ""},
503         {TFM_MODAL_PLANE_Z, "PLANE_Z", 0, "Orientation Z plane", ""},
504         {TFM_MODAL_CONS_OFF, "CONS_OFF", 0, "Remove Constraints", ""},
505         {TFM_MODAL_ADD_SNAP, "ADD_SNAP", 0, "Add Snap Point", ""},
506         {TFM_MODAL_REMOVE_SNAP, "REMOVE_SNAP", 0, "Remove Last Snap Point", ""},
507         {NUM_MODAL_INCREMENT_UP, "INCREMENT_UP", 0, "Numinput Increment Up", ""},
508         {NUM_MODAL_INCREMENT_DOWN, "INCREMENT_DOWN", 0, "Numinput Increment Down", ""},
509         {TFM_MODAL_PROPSIZE_UP, "PROPORTIONAL_SIZE_UP", 0, "Increase Proportional Influence", ""},
510         {TFM_MODAL_PROPSIZE_DOWN, "PROPORTIONAL_SIZE_DOWN", 0, "Decrease Poportional Influence", ""},
511         {TFM_MODAL_AUTOIK_LEN_INC, "AUTOIK_CHAIN_LEN_UP", 0, "Increase Max AutoIK Chain Length", ""},
512         {TFM_MODAL_AUTOIK_LEN_DEC, "AUTOIK_CHAIN_LEN_DOWN", 0, "Decrease Max AutoIK Chain Length", ""},
513         {0, NULL, 0, NULL, NULL}};
514         
515         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Transform Modal Map");
516         
517         /* this function is called for each spacetype, only needs to add map once */
518         if(keymap) return NULL;
519         
520         keymap= WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
521         
522         /* items for modal map */
523         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CANCEL);
524         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
525         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
526         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
527
528         WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, TFM_MODAL_TRANSLATE);
529         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, TFM_MODAL_ROTATE);
530         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, TFM_MODAL_RESIZE);
531         
532         WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_SNAP_TOGGLE);
533
534         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
535         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
536
537         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
538         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
539         
540         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, TFM_MODAL_ADD_SNAP);
541         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ALT, 0, TFM_MODAL_REMOVE_SNAP);
542
543         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
544         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
545         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
546         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
547         
548         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
549         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
550         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
551         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
552         
553         return keymap;
554 }
555
556
557 int transformEvent(TransInfo *t, wmEvent *event)
558 {
559         float mati[3][3]= MAT3_UNITY;
560         char cmode = constraintModeToChar(t);
561         int handled = 1;
562
563         t->redraw |= handleMouseInput(t, &t->mouse, event);
564
565         if (event->type == MOUSEMOVE)
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(-RAD2DEGF(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.0f / dist, (float)M_PI/4.0f);
1310                                         float spacing_angle = MIN2(5.0f / dist, (float)M_PI/12.0f);
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(RAD2DEGF(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(RAD2DEGF(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                 break;
1654         case TFM_BONE_ROLL:
1655                 initBoneRoll(t);
1656                 break;
1657         case TFM_TIME_TRANSLATE:
1658                 initTimeTranslate(t);
1659                 break;
1660         case TFM_TIME_SLIDE:
1661                 initTimeSlide(t);
1662                 break;
1663         case TFM_TIME_SCALE:
1664                 initTimeScale(t);
1665                 break;
1666         case TFM_TIME_DUPLICATE:
1667                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1668                  * so that duplicate-culling will work properly
1669                  */
1670                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1671                         initTranslation(t);
1672                 else
1673                         initTimeTranslate(t);
1674                 t->mode = mode;
1675                 break;
1676         case TFM_TIME_EXTEND:
1677                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1678                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1679                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1680                  * depending on which editor this was called from
1681                  */
1682                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1683                         initTranslation(t);
1684                 else
1685                         initTimeTranslate(t);
1686                 break;
1687         case TFM_BAKE_TIME:
1688                 initBakeTime(t);
1689                 break;
1690         case TFM_MIRROR:
1691                 initMirror(t);
1692                 break;
1693         case TFM_BEVEL:
1694                 initBevel(t);
1695                 break;
1696         case TFM_BWEIGHT:
1697                 initBevelWeight(t);
1698                 break;
1699         case TFM_ALIGN:
1700                 initAlign(t);
1701                 break;
1702         case TFM_SEQ_SLIDE:
1703                 initSeqSlide(t);
1704                 break;
1705         }
1706
1707         if(t->state == TRANS_CANCEL)
1708         {
1709                 postTrans(C, t);
1710                 return 0;
1711         }
1712
1713
1714         /* overwrite initial values if operator supplied a non-null vector */
1715         if (RNA_property_is_set(op->ptr, "value"))
1716         {
1717                 float values[4]= {0}; /* incase value isn't length 4, avoid uninitialized memory  */
1718                 RNA_float_get_array(op->ptr, "value", values);
1719                 QUATCOPY(t->values, values);
1720                 QUATCOPY(t->auto_values, values);
1721                 t->flag |= T_AUTOVALUES;
1722         }
1723
1724         /* Transformation axis from operator */
1725         if (RNA_struct_find_property(op->ptr, "axis") && RNA_property_is_set(op->ptr, "axis"))
1726         {
1727                 RNA_float_get_array(op->ptr, "axis", t->axis);
1728                 normalize_v3(t->axis);
1729                 copy_v3_v3(t->axis_orig, t->axis);
1730         }
1731
1732         /* Constraint init from operator */
1733         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1734         {
1735                 int constraint_axis[3];
1736
1737                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1738
1739                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1740                 {
1741                         t->con.mode |= CON_APPLY;
1742
1743                         if (constraint_axis[0]) {
1744                                 t->con.mode |= CON_AXIS0;
1745                         }
1746                         if (constraint_axis[1]) {
1747                                 t->con.mode |= CON_AXIS1;
1748                         }
1749                         if (constraint_axis[2]) {
1750                                 t->con.mode |= CON_AXIS2;
1751                         }
1752
1753                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1754                 }
1755         }
1756
1757         t->context = NULL;
1758
1759         return 1;
1760 }
1761
1762 void transformApply(bContext *C, TransInfo *t)
1763 {
1764         t->context = C;
1765
1766         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT)))
1767         {
1768                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
1769                         t->con.mode |= CON_SELECT;
1770
1771                 selectConstraint(t);
1772                 if (t->transform) {
1773                         t->transform(t, t->mval);  // calls recalcData()
1774                         viewRedrawForce(C, t);
1775                 }
1776                 t->redraw = TREDRAW_NOTHING;
1777         } else if (t->redraw & TREDRAW_SOFT) {
1778                 viewRedrawForce(C, t);
1779         }
1780
1781         /* If auto confirm is on, break after one pass */
1782         if (t->options & CTX_AUTOCONFIRM)
1783         {
1784                 t->state = TRANS_CONFIRM;
1785         }
1786
1787         if (BKE_ptcache_get_continue_physics())
1788         {
1789                 // TRANSFORM_FIX_ME
1790                 //do_screenhandlers(G.curscreen);
1791                 t->redraw |= TREDRAW_HARD;
1792         }
1793
1794         t->context = NULL;
1795 }
1796
1797 void drawTransformApply(const bContext *C, struct ARegion *UNUSED(ar), void *arg)
1798 {
1799         TransInfo *t = arg;
1800
1801         if (t->redraw & TREDRAW_SOFT) {
1802                 t->redraw |= TREDRAW_HARD;
1803                 transformApply((bContext *)C, t);
1804         }
1805 }
1806
1807 int transformEnd(bContext *C, TransInfo *t)
1808 {
1809         int exit_code = OPERATOR_RUNNING_MODAL;
1810
1811         t->context = C;
1812
1813         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING)
1814         {
1815                 /* handle restoring objects */
1816                 if(t->state == TRANS_CANCEL)
1817                 {
1818                         /* exception, edge slide transformed UVs too */
1819                         if(t->mode==TFM_EDGE_SLIDE)
1820                                 doEdgeSlide(t, 0.0f);
1821                         
1822                         exit_code = OPERATOR_CANCELLED;
1823                         restoreTransObjects(t); // calls recalcData()
1824                 }
1825                 else
1826                 {
1827                         exit_code = OPERATOR_FINISHED;
1828                 }
1829
1830                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
1831                 special_aftertrans_update(C, t);
1832
1833                 /* free data */
1834                 postTrans(C, t);
1835
1836                 /* send events out for redraws */
1837                 viewRedrawPost(C, t);
1838
1839                 /*  Undo as last, certainly after special_trans_update! */
1840
1841                 if(t->state == TRANS_CANCEL) {
1842 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1843                 }
1844                 else {
1845 //                      if(t->undostr) ED_undo_push(C, t->undostr);
1846 //                      else ED_undo_push(C, transform_to_undostr(t));
1847                 }
1848                 t->undostr= NULL;
1849
1850                 viewRedrawForce(C, t);
1851         }
1852
1853         t->context = NULL;
1854
1855         return exit_code;
1856 }
1857
1858 /* ************************** TRANSFORM LOCKS **************************** */
1859
1860 static void protectedTransBits(short protectflag, float *vec)
1861 {
1862         if(protectflag & OB_LOCK_LOCX)
1863                 vec[0]= 0.0f;
1864         if(protectflag & OB_LOCK_LOCY)
1865                 vec[1]= 0.0f;
1866         if(protectflag & OB_LOCK_LOCZ)
1867                 vec[2]= 0.0f;
1868 }
1869
1870 static void protectedSizeBits(short protectflag, float *size)
1871 {
1872         if(protectflag & OB_LOCK_SCALEX)
1873                 size[0]= 1.0f;
1874         if(protectflag & OB_LOCK_SCALEY)
1875                 size[1]= 1.0f;
1876         if(protectflag & OB_LOCK_SCALEZ)
1877                 size[2]= 1.0f;
1878 }
1879
1880 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
1881 {
1882         if(protectflag & OB_LOCK_ROTX)
1883                 eul[0]= oldeul[0];
1884         if(protectflag & OB_LOCK_ROTY)
1885                 eul[1]= oldeul[1];
1886         if(protectflag & OB_LOCK_ROTZ)
1887                 eul[2]= oldeul[2];
1888 }
1889
1890
1891 /* this function only does the delta rotation */
1892 /* axis-angle is usually internally stored as quats... */
1893 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
1894 {
1895         /* check that protection flags are set */
1896         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1897                 return;
1898         
1899         if (protectflag & OB_LOCK_ROT4D) {
1900                 /* axis-angle getting limited as 4D entities that they are... */
1901                 if (protectflag & OB_LOCK_ROTW)
1902                         *angle= oldAngle;
1903                 if (protectflag & OB_LOCK_ROTX)
1904                         axis[0]= oldAxis[0];
1905                 if (protectflag & OB_LOCK_ROTY)
1906                         axis[1]= oldAxis[1];
1907                 if (protectflag & OB_LOCK_ROTZ)
1908                         axis[2]= oldAxis[2];
1909         }
1910         else {
1911                 /* axis-angle get limited with euler... */
1912                 float eul[3], oldeul[3];
1913                 
1914                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,axis, *angle);
1915                 axis_angle_to_eulO( oldeul, EULER_ORDER_DEFAULT,oldAxis, oldAngle);
1916                 
1917                 if (protectflag & OB_LOCK_ROTX)
1918                         eul[0]= oldeul[0];
1919                 if (protectflag & OB_LOCK_ROTY)
1920                         eul[1]= oldeul[1];
1921                 if (protectflag & OB_LOCK_ROTZ)
1922                         eul[2]= oldeul[2];
1923                 
1924                 eulO_to_axis_angle( axis, angle,eul, EULER_ORDER_DEFAULT);
1925                 
1926                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
1927                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
1928                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
1929                         axis[1]= 1.0f;
1930                 }
1931         }
1932 }
1933
1934 /* this function only does the delta rotation */
1935 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
1936 {
1937         /* check that protection flags are set */
1938         if ((protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) == 0)
1939                 return;
1940         
1941         if (protectflag & OB_LOCK_ROT4D) {
1942                 /* quaternions getting limited as 4D entities that they are... */
1943                 if (protectflag & OB_LOCK_ROTW)
1944                         quat[0]= oldquat[0];
1945                 if (protectflag & OB_LOCK_ROTX)
1946                         quat[1]= oldquat[1];
1947                 if (protectflag & OB_LOCK_ROTY)
1948                         quat[2]= oldquat[2];
1949                 if (protectflag & OB_LOCK_ROTZ)
1950                         quat[3]= oldquat[3];
1951         }
1952         else {
1953                 /* quaternions get limited with euler... (compatability mode) */
1954                 float eul[3], oldeul[3], nquat[4], noldquat[4];
1955                 float qlen;
1956
1957                 qlen= normalize_qt_qt(nquat, quat);
1958                 normalize_qt_qt(noldquat, oldquat);
1959
1960                 quat_to_eul(eul, nquat);
1961                 quat_to_eul(oldeul, noldquat);
1962
1963                 if (protectflag & OB_LOCK_ROTX)
1964                         eul[0]= oldeul[0];
1965                 if (protectflag & OB_LOCK_ROTY)
1966                         eul[1]= oldeul[1];
1967                 if (protectflag & OB_LOCK_ROTZ)
1968                         eul[2]= oldeul[2];
1969
1970                 eul_to_quat( quat,eul);
1971
1972                 /* restore original quat size */
1973                 mul_qt_fl(quat, qlen);
1974                 
1975                 /* quaternions flip w sign to accumulate rotations correctly */
1976                 if ( (nquat[0]<0.0f && quat[0]>0.0f) || (nquat[0]>0.0f && quat[0]<0.0f) ) {
1977                         mul_qt_fl(quat, -1.0f);
1978                 }
1979         }
1980 }
1981
1982 /* ******************* TRANSFORM LIMITS ********************** */
1983
1984 static void constraintTransLim(TransInfo *UNUSED(t), TransData *td)
1985 {
1986         if (td->con) {
1987                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
1988                 bConstraintOb cob= {NULL};
1989                 bConstraint *con;
1990                 
1991                 /* Make a temporary bConstraintOb for using these limit constraints
1992                  *      - they only care that cob->matrix is correctly set ;-)
1993                  *      - current space should be local
1994                  */
1995                 unit_m4(cob.matrix);
1996                 VECCOPY(cob.matrix[3], td->loc);
1997                 
1998                 /* Evaluate valid constraints */
1999                 for (con= td->con; con; con= con->next) {
2000                         float tmat[4][4];
2001                         
2002                         /* only consider constraint if enabled */
2003                         if (con->flag & CONSTRAINT_DISABLE) continue;
2004                         if (con->enforce == 0.0f) continue;
2005                         
2006                         /* only use it if it's tagged for this purpose (and the right type) */
2007                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2008                                 bLocLimitConstraint *data= con->data;
2009                                 
2010                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2011                                         continue;
2012                                 
2013                                 /* do space conversions */
2014                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2015                                         /* just multiply by td->mtx (this should be ok) */
2016                                         copy_m4_m4(tmat, cob.matrix);
2017                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2018                                 }
2019                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2020                                         /* skip... incompatable spacetype */
2021                                         continue;
2022                                 }
2023                                 
2024                                 /* do constraint */
2025                                 cti->evaluate_constraint(con, &cob, NULL);
2026                                 
2027                                 /* convert spaces again */
2028                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2029                                         /* just multiply by td->mtx (this should be ok) */
2030                                         copy_m4_m4(tmat, cob.matrix);
2031                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2032                                 }
2033                         }
2034                 }
2035                 
2036                 /* copy results from cob->matrix */
2037                 VECCOPY(td->loc, cob.matrix[3]);
2038         }
2039 }
2040
2041 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2042 {
2043         /* Make a temporary bConstraintOb for use by limit constraints
2044          *      - they only care that cob->matrix is correctly set ;-)
2045          *      - current space should be local
2046          */
2047         memset(cob, 0, sizeof(bConstraintOb));
2048         if (td->ext)
2049         {
2050                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2051                         /* quats */
2052                         /* objects and bones do normalization first too, otherwise
2053                            we don't necessarily end up with a rotation matrix, and
2054                            then conversion back to quat gives a different result */
2055                         float quat[4];
2056                         normalize_qt_qt(quat, td->ext->quat);
2057                         quat_to_mat4(cob->matrix, quat);
2058                 }
2059                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2060                         /* axis angle */
2061                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2062                 }
2063                 else {
2064                         /* eulers */
2065                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2066                 }
2067         }
2068 }
2069
2070 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2071 {
2072         if (td->con) {
2073                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2074                 bConstraintOb cob;
2075                 bConstraint *con;
2076                 int dolimit = 0;
2077                 
2078                 /* Evaluate valid constraints */
2079                 for (con= td->con; con; con= con->next) {
2080                         /* only consider constraint if enabled */
2081                         if (con->flag & CONSTRAINT_DISABLE) continue;
2082                         if (con->enforce == 0.0f) continue;
2083                         
2084                         /* we're only interested in Limit-Rotation constraints */
2085                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2086                                 bRotLimitConstraint *data= con->data;
2087                                 float tmat[4][4];
2088                                 
2089                                 /* only use it if it's tagged for this purpose */
2090                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2091                                         continue;
2092
2093                                 /* skip incompatable spacetypes */
2094                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2095                                         continue;
2096
2097                                 /* only do conversion if necessary, to preserve quats and eulers */
2098                                 if(!dolimit) {
2099                                         constraintob_from_transdata(&cob, td);
2100                                         dolimit= 1;
2101                                 }
2102                                 
2103                                 /* do space conversions */
2104                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2105                                         /* just multiply by td->mtx (this should be ok) */
2106                                         copy_m4_m4(tmat, cob.matrix);
2107                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2108                                 }
2109                                 
2110                                 /* do constraint */
2111                                 cti->evaluate_constraint(con, &cob, NULL);
2112                                 
2113                                 /* convert spaces again */
2114                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2115                                         /* just multiply by td->mtx (this should be ok) */
2116                                         copy_m4_m4(tmat, cob.matrix);
2117                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2118                                 }
2119                         }
2120                 }
2121                 
2122                 if(dolimit) {
2123                         /* copy results from cob->matrix */
2124                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2125                                 /* quats */
2126                                 mat4_to_quat( td->ext->quat,cob.matrix);
2127                         }
2128                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2129                                 /* axis angle */
2130                                 mat4_to_axis_angle( &td->ext->quat[1], &td->ext->quat[0],cob.matrix);
2131                         }
2132                         else {
2133                                 /* eulers */
2134                                 mat4_to_eulO( td->ext->rot, td->ext->rotOrder,cob.matrix);
2135                         }
2136                 }
2137         }
2138 }
2139
2140 static void constraintSizeLim(TransInfo *t, TransData *td)
2141 {
2142         if (td->con && td->ext) {
2143                 bConstraintTypeInfo *cti= get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2144                 bConstraintOb cob= {NULL};
2145                 bConstraint *con;
2146                 
2147                 /* Make a temporary bConstraintOb for using these limit constraints
2148                  *      - they only care that cob->matrix is correctly set ;-)
2149                  *      - current space should be local
2150                  */
2151                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2152                         /* scale val and reset size */
2153                         return; // TODO: fix this case
2154                 }
2155                 else {
2156                         /* Reset val if SINGLESIZE but using a constraint */
2157                         if (td->flag & TD_SINGLESIZE)
2158                                 return;
2159                         
2160                         size_to_mat4( cob.matrix,td->ext->size);
2161                 }
2162                 
2163                 /* Evaluate valid constraints */
2164                 for (con= td->con; con; con= con->next) {
2165                         /* only consider constraint if enabled */
2166                         if (con->flag & CONSTRAINT_DISABLE) continue;
2167                         if (con->enforce == 0.0f) continue;
2168                         
2169                         /* we're only interested in Limit-Scale constraints */
2170                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2171                                 bSizeLimitConstraint *data= con->data;
2172                                 float tmat[4][4];
2173                                 
2174                                 /* only use it if it's tagged for this purpose */
2175                                 if ((data->flag2 & LIMIT_TRANSFORM)==0)
2176                                         continue;
2177                                 
2178                                 /* do space conversions */
2179                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2180                                         /* just multiply by td->mtx (this should be ok) */
2181                                         copy_m4_m4(tmat, cob.matrix);
2182                                         mul_m4_m3m4(cob.matrix, td->mtx, tmat);
2183                                 }
2184                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2185                                         /* skip... incompatable spacetype */
2186                                         continue;
2187                                 }
2188                                 
2189                                 /* do constraint */
2190                                 cti->evaluate_constraint(con, &cob, NULL);
2191                                 
2192                                 /* convert spaces again */
2193                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2194                                         /* just multiply by td->mtx (this should be ok) */
2195                                         copy_m4_m4(tmat, cob.matrix);
2196                                         mul_m4_m3m4(cob.matrix, td->smtx, tmat);
2197                                 }
2198                         }
2199                 }
2200                 
2201                 /* copy results from cob->matrix */
2202                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2203                         /* scale val and reset size */
2204                         return; // TODO: fix this case
2205                 }
2206                 else {
2207                         /* Reset val if SINGLESIZE but using a constraint */
2208                         if (td->flag & TD_SINGLESIZE)
2209                                 return;
2210                         
2211                         mat4_to_size( td->ext->size,cob.matrix);
2212                 }
2213         }
2214 }
2215
2216 /* ************************** WARP *************************** */
2217
2218 static void postInputWarp(TransInfo *t, float values[3])
2219 {
2220         mul_v3_fl(values, (float)(M_PI * 2));
2221
2222         if (t->customData) /* non-null value indicates reversed input */
2223         {
2224                 negate_v3(values);
2225         }
2226 }
2227
2228 void initWarp(TransInfo *t)
2229 {
2230         float max[3], min[3];
2231         int i;
2232         
2233         t->mode = TFM_WARP;
2234         t->transform = Warp;
2235         t->handleEvent = handleEventWarp;
2236         
2237         setInputPostFct(&t->mouse, postInputWarp);
2238         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2239         
2240         t->idx_max = 0;
2241         t->num.idx_max = 0;
2242         t->snap[0] = 0.0f;
2243         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2244         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2245         
2246         t->num.increment = 1.0f;
2247
2248         t->flag |= T_NO_CONSTRAINT;
2249         
2250         /* we need min/max in view space */
2251         for(i = 0; i < t->total; i++) {
2252                 float center[3];
2253                 copy_v3_v3(center, t->data[i].center);
2254                 mul_m3_v3(t->data[i].mtx, center);
2255                 mul_m4_v3(t->viewmat, center);
2256                 sub_v3_v3(center, t->viewmat[3]);
2257                 if (i)
2258                         minmax_v3v3_v3(min, max, center);
2259                 else {
2260                         copy_v3_v3(max, center);
2261                         copy_v3_v3(min, center);
2262                 }
2263         }
2264
2265         mid_v3_v3v3(t->center, min, max);
2266
2267         if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2268         t->val= (max[0]-min[0])/2.0f; /* t->val is X dimension projected boundbox */
2269 }
2270
2271 int handleEventWarp(TransInfo *t, wmEvent *event)
2272 {
2273         int status = 0;
2274         
2275         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2276         {
2277                 // Use customData pointer to signal warp direction
2278                 if      (t->customData == NULL)
2279                         t->customData = (void*)1;
2280                 else
2281                         t->customData = NULL;
2282                 
2283                 status = 1;
2284         }
2285         
2286         return status;
2287 }
2288
2289 int Warp(TransInfo *t, const short UNUSED(mval[2]))
2290 {
2291         TransData *td = t->data;
2292         float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2293         int i;
2294         char str[50];
2295         
2296         curs= give_cursor(t->scene, t->view);
2297         /*
2298          * gcursor is the one used for helpline.
2299          * It has to be in the same space as the drawing loop
2300          * (that means it needs to be in the object's space when in edit mode and
2301          *  in global space in object mode)
2302          *
2303          * cursor is used for calculations.
2304          * It needs to be in view space, but we need to take object's offset
2305          * into account if in Edit mode.
2306          */
2307         VECCOPY(cursor, curs);
2308         VECCOPY(gcursor, cursor);
2309         if (t->flag & T_EDIT) {
2310                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2311                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2312                 mul_m3_v3(t->data->smtx, gcursor);
2313         }
2314         mul_m4_v3(t->viewmat, cursor);
2315         sub_v3_v3(cursor, t->viewmat[3]);
2316         
2317         /* amount of radians for warp */
2318         circumfac = t->values[0];
2319         
2320         snapGrid(t, &circumfac);
2321         applyNumInput(&t->num, &circumfac);
2322         
2323         /* header print for NumInput */
2324         if (hasNumInput(&t->num)) {
2325                 char c[20];
2326                 
2327                 outputNumInput(&(t->num), c);
2328                 
2329                 sprintf(str, "Warp: %s", c);
2330
2331                 circumfac = DEG2RADF(circumfac);
2332         }
2333         else {
2334                 /* default header print */
2335                 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2336         }
2337         
2338         t->values[0] = circumfac;
2339
2340         circumfac /= 2; /* only need 180 on each side to make 360 */
2341         
2342         for(i = 0; i < t->total; i++, td++) {
2343                 float loc[3];
2344                 if (td->flag & TD_NOACTION)
2345                         break;
2346                 
2347                 if (td->flag & TD_SKIP)
2348                         continue;
2349                 
2350                 /* translate point to center, rotate in such a way that outline==distance */
2351                 VECCOPY(vec, td->iloc);
2352                 mul_m3_v3(td->mtx, vec);
2353                 mul_m4_v3(t->viewmat, vec);
2354                 sub_v3_v3(vec, t->viewmat[3]);
2355                 
2356                 dist= vec[0]-cursor[0];
2357                 
2358                 /* t->val is X dimension projected boundbox */
2359                 phi0= (circumfac*dist/t->val);
2360                 
2361                 vec[1]= (vec[1]-cursor[1]);
2362                 
2363                 co= (float)cos(phi0);
2364                 si= (float)sin(phi0);
2365                 loc[0]= -si*vec[1]+cursor[0];
2366                 loc[1]= co*vec[1]+cursor[1];
2367                 loc[2]= vec[2];
2368                 
2369                 mul_m4_v3(t->viewinv, loc);
2370                 sub_v3_v3(loc, t->viewinv[3]);
2371                 mul_m3_v3(td->smtx, loc);
2372                 
2373                 sub_v3_v3(loc, td->iloc);
2374                 mul_v3_fl(loc, td->factor);
2375                 add_v3_v3v3(td->loc, td->iloc, loc);
2376         }
2377         
2378         recalcData(t);
2379         
2380         ED_area_headerprint(t->sa, str);
2381         
2382         return 1;
2383 }
2384
2385 /* ************************** SHEAR *************************** */
2386
2387 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2388 {
2389         mul_v3_fl(values, 0.05f);
2390 }
2391
2392 void initShear(TransInfo *t)
2393 {
2394         t->mode = TFM_SHEAR;
2395         t->transform = Shear;
2396         t->handleEvent = handleEventShear;
2397         
2398         setInputPostFct(&t->mouse, postInputShear);
2399         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2400         
2401         t->idx_max = 0;
2402         t->num.idx_max = 0;
2403         t->snap[0] = 0.0f;
2404         t->snap[1] = 0.1f;
2405         t->snap[2] = t->snap[1] * 0.1f;
2406         
2407         t->num.increment = 0.1f;
2408
2409         t->flag |= T_NO_CONSTRAINT;
2410 }
2411
2412 int handleEventShear(TransInfo *t, wmEvent *event)
2413 {
2414         int status = 0;
2415         
2416         if (event->type == MIDDLEMOUSE && event->val==KM_PRESS)
2417         {
2418                 // Use customData pointer to signal Shear direction
2419                 if      (t->customData == NULL)
2420                 {
2421                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2422                         t->customData = (void*)1;
2423                 }
2424                 else
2425                 {
2426                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2427                         t->customData = NULL;
2428                 }
2429                 
2430                 status = 1;
2431         }
2432         
2433         return status;
2434 }
2435
2436
2437 int Shear(TransInfo *t, const short UNUSED(mval[2]))
2438 {
2439         TransData *td = t->data;
2440         float vec[3];
2441         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2442         float value;
2443         int i;
2444         char str[50];
2445         
2446         copy_m3_m4(persmat, t->viewmat);
2447         invert_m3_m3(persinv, persmat);
2448         
2449         value = t->values[0];
2450         
2451         snapGrid(t, &value);
2452         
2453         applyNumInput(&t->num, &value);
2454         
2455         /* header print for NumInput */
2456         if (hasNumInput(&t->num)) {
2457                 char c[20];
2458                 
2459                 outputNumInput(&(t->num), c);
2460                 
2461                 sprintf(str, "Shear: %s %s", c, t->proptext);
2462         }
2463         else {
2464                 /* default header print */
2465                 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2466         }
2467         
2468         unit_m3(smat);
2469         
2470         // Custom data signals shear direction
2471         if (t->customData == NULL)
2472                 smat[1][0] = value;
2473         else
2474                 smat[0][1] = value;
2475         
2476         mul_m3_m3m3(tmat, smat, persmat);
2477         mul_m3_m3m3(totmat, persinv, tmat);
2478         
2479         for(i = 0 ; i < t->total; i++, td++) {
2480                 if (td->flag & TD_NOACTION)
2481                         break;
2482                 
2483                 if (td->flag & TD_SKIP)
2484                         continue;
2485                 
2486                 if (t->obedit) {
2487                         float mat3[3][3];
2488                         mul_m3_m3m3(mat3, totmat, td->mtx);
2489                         mul_m3_m3m3(tmat, td->smtx, mat3);
2490                 }
2491                 else {
2492                         copy_m3_m3(tmat, totmat);
2493                 }
2494                 sub_v3_v3v3(vec, td->center, t->center);
2495                 
2496                 mul_m3_v3(tmat, vec);
2497                 
2498                 add_v3_v3(vec, t->center);
2499                 sub_v3_v3(vec, td->center);
2500                 
2501                 mul_v3_fl(vec, td->factor);
2502                 
2503                 add_v3_v3v3(td->loc, td->iloc, vec);
2504         }
2505         
2506         recalcData(t);
2507         
2508         ED_area_headerprint(t->sa, str);
2509
2510         return 1;
2511 }
2512
2513 /* ************************** RESIZE *************************** */
2514
2515 void initResize(TransInfo *t)
2516 {
2517         t->mode = TFM_RESIZE;
2518         t->transform = Resize;
2519         
2520         initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2521         
2522         t->flag |= T_NULL_ONE;
2523         t->num.flag |= NUM_NULL_ONE;
2524         t->num.flag |= NUM_AFFECT_ALL;
2525         if (!t->obedit) {
2526                 t->flag |= T_NO_ZERO;
2527                 t->num.flag |= NUM_NO_ZERO;
2528         }
2529         
2530         t->idx_max = 2;
2531         t->num.idx_max = 2;
2532         t->snap[0] = 0.0f;
2533         t->snap[1] = 0.1f;
2534         t->snap[2] = t->snap[1] * 0.1f;
2535
2536         t->num.increment = t->snap[1];
2537 }
2538
2539 static void headerResize(TransInfo *t, float vec[3], char *str) {
2540         char tvec[60];
2541         char *spos= str;
2542         if (hasNumInput(&t->num)) {
2543                 outputNumInput(&(t->num), tvec);
2544         }
2545         else {
2546                 sprintf(&tvec[0], "%.4f", vec[0]);
2547                 sprintf(&tvec[20], "%.4f", vec[1]);
2548                 sprintf(&tvec[40], "%.4f", vec[2]);
2549         }
2550         
2551         if (t->con.mode & CON_APPLY) {
2552                 switch(t->num.idx_max) {
2553                 case 0:
2554                         spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2555                         break;
2556                 case 1:
2557                         spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2558                         break;
2559                 case 2:
2560                         spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2561                 }
2562         }
2563         else {
2564                 if (t->flag & T_2D_EDIT)
2565                         spos += sprintf(spos, "Scale X: %s   Y: %s%s %s", &tvec[0], &tvec[20], t->con.text, t->proptext);
2566                 else
2567                         spos += sprintf(spos, "Scale X: %s   Y: %s  Z: %s%s %s", &tvec[0], &tvec[20], &tvec[40], t->con.text, t->proptext);
2568         }
2569         
2570         if (t->flag & (T_PROP_EDIT|T_PROP_CONNECTED)) {
2571                 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2572         }
2573
2574         (void)spos;
2575 }
2576
2577 #define SIGN(a)         (a<-FLT_EPSILON?1:a>FLT_EPSILON?2:3)
2578 #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)
2579
2580 /* smat is reference matrix, only scaled */
2581 static void TransMat3ToSize( float mat[][3], float smat[][3], float *size)
2582 {
2583         float vec[3];
2584         
2585         copy_v3_v3(vec, mat[0]);
2586         size[0]= normalize_v3(vec);
2587         copy_v3_v3(vec, mat[1]);
2588         size[1]= normalize_v3(vec);
2589         copy_v3_v3(vec, mat[2]);
2590         size[2]= normalize_v3(vec);
2591         
2592         /* first tried with dotproduct... but the sign flip is crucial */
2593         if( VECSIGNFLIP(mat[0], smat[0]) ) size[0]= -size[0];
2594         if( VECSIGNFLIP(mat[1], smat[1]) ) size[1]= -size[1];
2595         if( VECSIGNFLIP(mat[2], smat[2]) ) size[2]= -size[2];
2596 }
2597
2598
2599 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3]) {
2600         float tmat[3][3], smat[3][3], center[3];
2601         float vec[3];
2602         
2603         if (t->flag & T_EDIT) {
2604                 mul_m3_m3m3(smat, mat, td->mtx);
2605                 mul_m3_m3m3(tmat, td->smtx, smat);
2606         }
2607         else {
2608                 copy_m3_m3(tmat, mat);
2609         }
2610         
2611         if (t->con.applySize) {
2612                 t->con.applySize(t, td, tmat);
2613         }
2614         
2615         /* local constraint shouldn't alter center */
2616         if (t->around == V3D_LOCAL) {
2617                 if (t->flag & T_OBJECT) {
2618                         VECCOPY(center, td->center);
2619                 }
2620                 else if (t->flag & T_EDIT) {
2621                         
2622                         if(t->around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2623                                 VECCOPY(center, td->center);
2624                         }
2625                         else {
2626                                 VECCOPY(center, t->center);
2627                         }
2628                 }
2629                 else {
2630                         VECCOPY(center, t->center);
2631                 }
2632         }
2633         else {
2634                 VECCOPY(center, t->center);
2635         }
2636         
2637         if (td->ext) {
2638                 float fsize[3];
2639                 
2640                 if (t->flag & (T_OBJECT|T_TEXTURE|T_POSE)) {
2641                         float obsizemat[3][3];
2642                         // Reorient the size mat to fit the oriented object.
2643                         mul_m3_m3m3(obsizemat, tmat, td->axismtx);
2644                         //print_m3("obsizemat", obsizemat);
2645                         TransMat3ToSize(obsizemat, td->axismtx, fsize);
2646                         //print_v3("fsize", fsize);
2647                 }
2648                 else {
2649                         mat3_to_size( fsize,tmat);
2650                 }
2651                 
2652                 protectedSizeBits(td->protectflag, fsize);
2653                 
2654                 if ((t->flag & T_V3D_ALIGN)==0) {       // align mode doesn't resize objects itself
2655                         if((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)){
2656                                 /* scale val and reset size */
2657                                  *td->val = td->ival * (1 + (fsize[0] - 1) * td->factor);
2658                                 
2659                                 td->ext->size[0] = td->ext->isize[0];
2660                                 td->ext->size[1] = td->ext->isize[1];
2661                                 td->ext->size[2] = td->ext->isize[2];
2662                          }
2663                         else {
2664                                 /* Reset val if SINGLESIZE but using a constraint */
2665                                 if (td->flag & TD_SINGLESIZE)
2666                                          *td->val = td->ival;
2667                                 
2668                                 td->ext->size[0] = td->ext->isize[0] * (1 + (fsize[0] - 1) * td->factor);
2669                                 td->ext->size[1] = td->ext->isize[1] * (1 + (fsize[1] - 1) * td->factor);
2670                                 td->ext->size[2] = td->ext->isize[2] * (1 + (fsize[2] - 1) * td->factor);
2671                         }
2672                 }
2673                 
2674                 constraintSizeLim(t, td);
2675         }
2676         
2677         /* For individual element center, Editmode need to use iloc */
2678         if (t->flag & T_POINTS)
2679                 sub_v3_v3v3(vec, td->iloc, center);
2680         else
2681                 sub_v3_v3v3(vec, td->center, center);
2682         
2683         mul_m3_v3(tmat, vec);
2684         
2685         add_v3_v3(vec, center);
2686         if (t->flag & T_POINTS)
2687                 sub_v3_v3(vec, td->iloc);
2688         else
2689                 sub_v3_v3(vec, td->center);
2690         
2691         mul_v3_fl(vec, td->factor);
2692         
2693         if (t->flag & (T_OBJECT|T_POSE)) {
2694                 mul_m3_v3(td->smtx, vec);
2695         }
2696         
2697         protectedTransBits(td->protectflag, vec);
2698         add_v3_v3v3(td->loc, td->iloc, vec);
2699         
2700         constraintTransLim(t, td);
2701 }
2702
2703 int Resize(TransInfo *t, const short mval[2])
2704 {
2705         TransData *td;
2706         float size[3], mat[3][3];
2707         float ratio;
2708         int i;
2709         char str[200];
2710         
2711         /* for manipulator, center handle, the scaling can't be done relative to center */
2712         if( (t->flag & T_USES_MANIPULATOR) && t->con.mode==0)
2713         {
2714                 ratio = 1.0f - ((t->imval[0] - mval[0]) + (t->imval[1] - mval[1]))/100.0f;
2715         }
2716         else
2717         {
2718                 ratio = t->values[0];
2719         }
2720         
2721         size[0] = size[1] = size[2] = ratio;
2722         
2723         snapGrid(t, size);
2724         
2725         if (hasNumInput(&t->num)) {
2726                 applyNumInput(&t->num, size);
2727                 constraintNumInput(t, size);
2728         }
2729         
2730         applySnapping(t, size);
2731         
2732         if (t->flag & T_AUTOVALUES)
2733         {
2734                 VECCOPY(size, t->auto_values);
2735         }
2736         
2737         VECCOPY(t->values, size);
2738         
2739         size_to_mat3( mat,size);
2740         
2741         if (t->con.applySize) {
2742                 t->con.applySize(t, NULL, mat);
2743         }
2744         
2745         copy_m3_m3(t->mat, mat);        // used in manipulator
2746         
2747         headerResize(t, size, str);
2748         
2749         for(i = 0, td=t->data; i < t->total; i++, td++) {
2750                 if (td->flag & TD_NOACTION)
2751                         break;
2752                 
2753                 if (td->flag & TD_SKIP)
2754                         continue;
2755                 
2756                 ElementResize(t, td, mat);
2757         }
2758         
2759         /* evil hack - redo resize if cliping needed */
2760         if (t->flag & T_CLIP_UV && clipUVTransform(t, size, 1)) {
2761                 size_to_mat3( mat,size);
2762                 
2763                 if (t->con.applySize)
2764                         t->con.applySize(t, NULL, mat);
2765                 
2766                 for(i = 0, td=t->data; i < t->total; i++, td++)
2767                         ElementResize(t, td, mat);
2768         }
2769         
2770         recalcData(t);
2771         
2772         ED_area_headerprint(t->sa, str);
2773         
2774         return 1;
2775 }
2776
2777 /* ************************** TOSPHERE *************************** */
2778
2779 void initToSphere(TransInfo *t)
2780 {
2781         TransData *td = t->data;
2782         int i;
2783         
2784         t->mode = TFM_TOSPHERE;
2785         t->transform = ToSphere;
2786         
2787         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2788         
2789         t->idx_max = 0;
2790         t->num.idx_max = 0;
2791         t->snap[0] = 0.0f;
2792         t->snap[1] = 0.1f;
2793         t->snap[2] = t->snap[1] * 0.1f;
2794         
2795         t->num.increment = t->snap[1];
2796
2797         t->num.flag |= NUM_NULL_ONE | NUM_NO_NEGATIVE;
2798         t->flag |= T_NO_CONSTRAINT;
2799         
2800         // Calculate average radius
2801         for(i = 0 ; i < t->total; i++, td++) {
2802                 t->val += len_v3v3(t->center, td->iloc);
2803         }
2804         
2805         t->val /= (float)t->total;
2806 }
2807
2808 int ToSphere(TransInfo *t, const short UNUSED(mval[2]))
2809 {
2810         float vec[3];
2811         float ratio, radius;
2812         int i;
2813         char str[64];
2814         TransData *td = t->data;
2815         
2816         ratio = t->values[0];
2817         
2818         snapGrid(t, &ratio);
2819         
2820         applyNumInput(&t->num, &ratio);
2821         
2822         if (ratio < 0)
2823                 ratio = 0.0f;
2824         else if (ratio > 1)
2825                 ratio = 1.0f;
2826         
2827         t->values[0] = ratio;
2828
2829         /* header print for NumInput */
2830         if (hasNumInput(&t->num)) {
2831                 char c[20];
2832                 
2833                 outputNumInput(&(t->num), c);
2834                 
2835                 sprintf(str, "To Sphere: %s %s", c, t->proptext);
2836         }
2837         else {
2838                 /* default header print */
2839                 sprintf(str, "To Sphere: %.4f %s", ratio, t->proptext);
2840         }
2841         
2842         
2843         for(i = 0 ; i < t->total; i++, td++) {
2844                 float tratio;
2845                 if (td->flag & TD_NOACTION)
2846                         break;
2847                 
2848                 if (td->flag & TD_SKIP)
2849                         continue;
2850                 
2851                 sub_v3_v3v3(vec, td->iloc, t->center);
2852                 
2853                 radius = normalize_v3(vec);
2854                 
2855                 tratio = ratio * td->factor;
2856                 
2857                 mul_v3_fl(vec, radius * (1.0f - tratio) + t->val * tratio);
2858                 
2859                 add_v3_v3v3(td->loc, t->center, vec);
2860         }
2861         
2862         
2863         recalcData(t);
2864         
2865         ED_area_headerprint(t->sa, str);
2866         
2867         return 1;
2868 }
2869
2870 /* ************************** ROTATION *************************** */
2871
2872
2873 static void postInputRotation(TransInfo *t, float values[3])
2874 {
2875         if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
2876                 t->con.applyRot(t, NULL, t->axis, values);
2877         }
2878 }
2879
2880 void initRotation(TransInfo *t)
2881 {
2882         t->mode = TFM_ROTATION;
2883         t->transform = Rotation;
2884         
2885         setInputPostFct(&t->mouse, postInputRotation);
2886         initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
2887         
2888         t->ndof.axis = 16;
2889         /* Scale down and flip input for rotation */
2890         t->ndof.factor[0] = -0.2f;
2891         
2892         t->idx_max = 0;
2893         t->num.idx_max = 0;
2894         t->snap[0] = 0.0f;
2895         t->snap[1] = (float)((5.0/180)*M_PI);
2896         t->snap[2] = t->snap[1] * 0.2f;
2897         
2898         t->num.increment = 1.0f;
2899
2900         if (t->flag & T_2D_EDIT)
2901                 t->flag |= T_NO_CONSTRAINT;
2902
2903         negate_v3_v3(t->axis, t->viewinv[2]);
2904         normalize_v3(t->axis);
2905
2906         copy_v3_v3(t->axis_orig, t->axis);
2907 }
2908
2909 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around) {
2910         float vec[3], totmat[3][3], smat[3][3];
2911         float eul[3], fmat[3][3], quat[4];
2912         float *center = t->center;
2913         
2914         /* local constraint shouldn't alter center */
2915         if (around == V3D_LOCAL) {
2916                 if (t->flag & (T_OBJECT|T_POSE)) {
2917                         center = td->center;
2918                 }
2919                 else {
2920                         if(around==V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
2921                                 center = td->center;
2922                         }
2923                 }
2924         }
2925         
2926         if (t->flag & T_POINTS) {
2927                 mul_m3_m3m3(totmat, mat, td->mtx);
2928                 mul_m3_m3m3(smat, td->smtx, totmat);
2929                 
2930                 sub_v3_v3v3(vec, td->iloc, center);
2931                 mul_m3_v3(smat, vec);
2932                 
2933                 add_v3_v3v3(td->loc, vec, center);
2934                 
2935                 sub_v3_v3v3(vec,td->loc,td->iloc);
2936                 protectedTransBits(td->protectflag, vec);
2937                 add_v3_v3v3(td->loc, td->iloc, vec);
2938                 
2939                 
2940                 if(td->flag & TD_USEQUAT) {
2941                         mul_serie_m3(fmat, td->mtx, mat, td->smtx, NULL, NULL, NULL, NULL, NULL);
2942                         mat3_to_quat( quat,fmat);       // Actual transform
2943                         
2944                         if(td->ext->quat){
2945                                 mul_qt_qtqt(td->ext->quat, quat, td->ext->iquat);
2946                                 
2947                                 /* is there a reason not to have this here? -jahka */
2948                                 protectedQuaternionBits(td->protectflag, td->ext->quat, td->ext->iquat);
2949                         }
2950                 }
2951         }
2952         /**
2953          * HACK WARNING
2954          *
2955          * This is some VERY ugly special case to deal with pose mode.
2956          *
2957          * The problem is that mtx and smtx include each bone orientation.
2958          *
2959          * That is needed to rotate each bone properly, HOWEVER, to calculate
2960          * the translation component, we only need the actual armature object's
2961          * matrix (and inverse). That is not all though. Once the proper translation
2962          * has been computed, it has to be converted back into the bone's space.
2963          */
2964         else if (t->flag & T_POSE) {
2965                 float pmtx[3][3], imtx[3][3];
2966                 
2967                 // Extract and invert armature object matrix
2968                 copy_m3_m4(pmtx, t->poseobj->obmat);
2969                 invert_m3_m3(imtx, pmtx);
2970