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