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