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