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