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