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