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