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