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