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