whitespace only, no functional change mixed tabs/spaces --> tabs.
[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                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
565                         t->con.mode |= CON_SELECT;
566
567                 t->mval[0] = event->x - t->ar->winrct.xmin;
568                 t->mval[1] = event->y - t->ar->winrct.ymin;
569
570                 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
571                 t->redraw |= TREDRAW_HARD;
572
573
574                 if (t->state == TRANS_STARTING) {
575                         t->state = TRANS_RUNNING;
576                 }
577
578                 applyMouseInput(t, &t->mouse, t->mval, t->values);
579         }
580
581         /* handle modal keymap first */
582         if (event->type == EVT_MODAL_MAP) {
583                 switch (event->val) {
584                         case TFM_MODAL_CANCEL:
585                                 t->state = TRANS_CANCEL;
586                                 break;
587                         case TFM_MODAL_CONFIRM:
588                                 t->state = TRANS_CONFIRM;
589                                 break;
590                         case TFM_MODAL_TRANSLATE:
591                                 /* only switch when... */
592                                 if( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
593                                         resetTransRestrictions(t);
594                                         restoreTransObjects(t);
595                                         initTranslation(t);
596                                         initSnapping(t, NULL); // need to reinit after mode change
597                                         t->redraw |= TREDRAW_HARD;
598                                 }
599                                 break;
600                         case TFM_MODAL_ROTATE:
601                                 /* only switch when... */
602                                 if(!(t->options & CTX_TEXTURE)) {
603                                         if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
604                                                 
605                                                 resetTransRestrictions(t);
606                                                 
607                                                 if (t->mode == TFM_ROTATION) {
608                                                         restoreTransObjects(t);
609                                                         initTrackball(t);
610                                                 }
611                                                 else {
612                                                         restoreTransObjects(t);
613                                                         initRotation(t);
614                                                 }
615                                                 initSnapping(t, NULL); // need to reinit after mode change
616                                                 t->redraw |= TREDRAW_HARD;
617                                         }
618                                 }
619                                 break;
620                         case TFM_MODAL_RESIZE:
621                                 /* only switch when... */
622                                 if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
623                                         resetTransRestrictions(t);
624                                         restoreTransObjects(t);
625                                         initResize(t);
626                                         initSnapping(t, NULL); // need to reinit after mode change
627                                         t->redraw |= TREDRAW_HARD;
628                                 }
629                                 break;
630                                 
631                         case TFM_MODAL_SNAP_INV_ON:
632                                 t->modifiers |= MOD_SNAP_INVERT;
633                                 t->redraw |= TREDRAW_HARD;
634                                 break;
635                         case TFM_MODAL_SNAP_INV_OFF:
636                                 t->modifiers &= ~MOD_SNAP_INVERT;
637                                 t->redraw |= TREDRAW_HARD;
638                                 break;
639                         case TFM_MODAL_SNAP_TOGGLE:
640                                 t->modifiers ^= MOD_SNAP;
641                                 t->redraw |= TREDRAW_HARD;
642                                 break;
643                         case TFM_MODAL_AXIS_X:
644                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
645                                         if (cmode == 'X') {
646                                                 stopConstraint(t);
647                                         }
648                                         else {
649                                                 if (t->flag & T_2D_EDIT) {
650                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
651                                                 }
652                                                 else {
653                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS0), "along %s X");
654                                                 }
655                                         }
656                                         t->redraw |= TREDRAW_HARD;
657                                 }
658                                 break;
659                         case TFM_MODAL_AXIS_Y:
660                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
661                                         if (cmode == 'Y') {
662                                                 stopConstraint(t);
663                                         }
664                                         else {
665                                                 if (t->flag & T_2D_EDIT) {
666                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
667                                                 }
668                                                 else {
669                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS1), "along %s Y");
670                                                 }
671                                         }
672                                         t->redraw |= TREDRAW_HARD;
673                                 }
674                                 break;
675                         case TFM_MODAL_AXIS_Z:
676                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
677                                         if (cmode == 'Z') {
678                                                 stopConstraint(t);
679                                         }
680                                         else {
681                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS2), "along %s Z");
682                                         }
683                                         t->redraw |= TREDRAW_HARD;
684                                 }
685                                 break;
686                         case TFM_MODAL_PLANE_X:
687                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
688                                         if (cmode == 'X') {
689                                                 stopConstraint(t);
690                                         }
691                                         else {
692                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
693                                         }
694                                         t->redraw |= TREDRAW_HARD;
695                                 }
696                                 break;
697                         case TFM_MODAL_PLANE_Y:
698                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
699                                         if (cmode == 'Y') {
700                                                 stopConstraint(t);
701                                         }
702                                         else {
703                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
704                                         }
705                                         t->redraw |= TREDRAW_HARD;
706                                 }
707                                 break;
708                         case TFM_MODAL_PLANE_Z:
709                                 if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))== 0) {
710                                         if (cmode == 'Z') {
711                                                 stopConstraint(t);
712                                         }
713                                         else {
714                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
715                                         }
716                                         t->redraw |= TREDRAW_HARD;
717                                 }
718                                 break;
719                         case TFM_MODAL_CONS_OFF:
720                                 if ((t->flag & T_NO_CONSTRAINT)==0) {
721                                         stopConstraint(t);
722                                         t->redraw |= TREDRAW_HARD;
723                                 }
724                                 break;
725                         case TFM_MODAL_ADD_SNAP:
726                                 addSnapPoint(t);
727                                 t->redraw |= TREDRAW_HARD;
728                                 break;
729                         case TFM_MODAL_REMOVE_SNAP:
730                                 removeSnapPoint(t);
731                                 t->redraw |= TREDRAW_HARD;
732                                 break;
733                         case TFM_MODAL_PROPSIZE_UP:
734                                 if(t->flag & T_PROP_EDIT) {
735                                         t->prop_size*= 1.1f;
736                                         if(t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
737                                                 t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
738                                         calculatePropRatio(t);
739                                 }
740                                 t->redraw |= TREDRAW_HARD;
741                                 break;
742                         case TFM_MODAL_PROPSIZE_DOWN:
743                                 if (t->flag & T_PROP_EDIT) {
744                                         t->prop_size*= 0.90909090f;
745                                         calculatePropRatio(t);
746                                 }
747                                 t->redraw |= TREDRAW_HARD;
748                                 break;
749                         case TFM_MODAL_AUTOIK_LEN_INC:
750                                 if (t->flag & T_AUTOIK)
751                                         transform_autoik_update(t, 1);
752                                 t->redraw |= TREDRAW_HARD;
753                                 break;
754                         case TFM_MODAL_AUTOIK_LEN_DEC:
755                                 if (t->flag & T_AUTOIK) 
756                                         transform_autoik_update(t, -1);
757                                 t->redraw |= TREDRAW_HARD;
758                                 break;
759                         default:
760                                 handled = 0;
761                                 break;
762                 }
763
764                 // Modal numinput events
765                 t->redraw |= handleNumInput(&(t->num), event);
766         }
767         /* else do non-mapped events */
768         else if (event->val==KM_PRESS) {
769                 switch (event->type){
770                 case RIGHTMOUSE:
771                         t->state = TRANS_CANCEL;
772                         break;
773                 /* enforce redraw of transform when modifiers are used */
774                 case LEFTSHIFTKEY:
775                 case RIGHTSHIFTKEY:
776                         t->modifiers |= MOD_CONSTRAINT_PLANE;
777                         t->redraw |= TREDRAW_HARD;
778                         break;
779
780                 case SPACEKEY:
781                         if ((t->spacetype==SPACE_VIEW3D) && event->alt) {
782 #if 0 // TRANSFORM_FIX_ME
783                                 short mval[2];
784
785                                 getmouseco_sc(mval);
786                                 BIF_selectOrientation();
787                                 calc_manipulator_stats(curarea);
788                                 copy_m3_m4(t->spacemtx, G.vd->twmat);
789                                 warp_pointer(mval[0], mval[1]);
790 #endif
791                         }
792                         else {
793                                 t->state = TRANS_CONFIRM;
794                         }
795                         break;
796
797                 case MIDDLEMOUSE:
798                         if ((t->flag & T_NO_CONSTRAINT)==0) {
799                                 /* exception for switching to dolly, or trackball, in camera view */
800                                 if (t->flag & T_CAMERA) {
801                                         if (t->mode==TFM_TRANSLATION)
802                                                 setLocalConstraint(t, (CON_AXIS2), "along local Z");
803                                         else if (t->mode==TFM_ROTATION) {
804                                                 restoreTransObjects(t);
805                                                 initTrackball(t);
806                                         }
807                                 }
808                                 else {
809                                         t->modifiers |= MOD_CONSTRAINT_SELECT;
810                                         if (t->con.mode & CON_APPLY) {
811                                                 stopConstraint(t);
812                                         }
813                                         else {
814                                                 if (event->shift) {
815                                                         initSelectConstraint(t, t->spacemtx);
816                                                 }
817                                                 else {
818                                                         /* bit hackish... but it prevents mmb select to print the orientation from menu */
819                                                         strcpy(t->spacename, "global");
820                                                         initSelectConstraint(t, mati);
821                                                 }
822                                                 postSelectConstraint(t);
823                                         }
824                                 }
825                                 t->redraw |= TREDRAW_HARD;
826                         }
827                         break;
828                 case ESCKEY:
829                         t->state = TRANS_CANCEL;
830                         break;
831                 case PADENTER:
832                 case RETKEY:
833                         t->state = TRANS_CONFIRM;
834                         break;
835                 case GKEY:
836                         /* only switch when... */
837                         if( ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
838                                 resetTransRestrictions(t);
839                                 restoreTransObjects(t);
840                                 initTranslation(t);
841                                 initSnapping(t, NULL); // need to reinit after mode change
842                                 t->redraw |= TREDRAW_HARD;
843                         }
844                         break;
845                 case SKEY:
846                         /* only switch when... */
847                         if( ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
848                                 resetTransRestrictions(t);
849                                 restoreTransObjects(t);
850                                 initResize(t);
851                                 initSnapping(t, NULL); // need to reinit after mode change
852                                 t->redraw |= TREDRAW_HARD;
853                         }
854                         break;
855                 case RKEY:
856                         /* only switch when... */
857                         if(!(t->options & CTX_TEXTURE)) {
858                                 if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
859
860                                         resetTransRestrictions(t);
861
862                                         if (t->mode == TFM_ROTATION) {
863                                                 restoreTransObjects(t);
864                                                 initTrackball(t);
865                                         }
866                                         else {
867                                                 restoreTransObjects(t);
868                                                 initRotation(t);
869                                         }
870                                         initSnapping(t, NULL); // need to reinit after mode change
871                                         t->redraw |= TREDRAW_HARD;
872                                 }
873                         }
874                         break;
875                 case CKEY:
876                         if (event->alt) {
877                                 t->flag ^= T_PROP_CONNECTED;
878                                 sort_trans_data_dist(t);
879                                 calculatePropRatio(t);
880                                 t->redraw= 1;
881                         }
882                         else {
883                                 stopConstraint(t);
884                                 t->redraw |= TREDRAW_HARD;
885                         }
886                         break;
887                 case XKEY:
888                         if ((t->flag & T_NO_CONSTRAINT)==0) {
889                                 if (t->flag & T_2D_EDIT) {
890                                         if (cmode == 'X') {
891                                                 stopConstraint(t);
892                                         } else {
893                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
894                                         }
895                                 } else {
896                                         if (cmode == 'X') {
897                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
898                                                         stopConstraint(t);
899                                                 } else {
900                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
901                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
902                                                                 setUserConstraint(t, orientation, (CON_AXIS0), "along %s X");
903                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
904                                                                 setUserConstraint(t, orientation, (CON_AXIS1|CON_AXIS2), "locking %s X");
905                                                 }
906                                         } else {
907                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
908                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along %s X");
909                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
910                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1|CON_AXIS2), "locking %s X");
911                                         }
912                                 }
913                                 t->redraw |= TREDRAW_HARD;
914                         }
915                         break;
916                 case YKEY:
917                         if ((t->flag & T_NO_CONSTRAINT)==0) {
918                                 if (t->flag & T_2D_EDIT) {
919                                         if (cmode == 'Y') {
920                                                 stopConstraint(t);
921                                         } else {
922                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
923                                         }
924                                 } else {
925                                         if (cmode == 'Y') {
926                                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
927                                                         stopConstraint(t);
928                                                 } else {
929                                                         short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
930                                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
931                                                                 setUserConstraint(t, orientation, (CON_AXIS1), "along %s Y");
932                                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
933                                                                 setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS2), "locking %s Y");
934                                                 }
935                                         } else {
936                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
937                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along %s Y");
938                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
939                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS2), "locking %s Y");
940                                         }
941                                 }
942                                 t->redraw |= TREDRAW_HARD;
943                         }
944                         break;
945                 case ZKEY:
946                         if ((t->flag & (T_NO_CONSTRAINT|T_2D_EDIT))==0) {
947                                 if (cmode == 'Z') {
948                                         if (t->con.orientation != V3D_MANIP_GLOBAL) {
949                                                 stopConstraint(t);
950                                         } else {
951                                                 short orientation = t->current_orientation != V3D_MANIP_GLOBAL ? t->current_orientation : V3D_MANIP_LOCAL;
952                                                 if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
953                                                         setUserConstraint(t, orientation, (CON_AXIS2), "along %s Z");
954                                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
955                                                         setUserConstraint(t, orientation, (CON_AXIS0|CON_AXIS1), "locking %s Z");
956                                         }
957                                 } else {
958                                         if ((t->modifiers & MOD_CONSTRAINT_PLANE) == 0)
959                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS2), "along %s Z");
960                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
961                                                 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0|CON_AXIS1), "locking %s Z");
962                                 }
963                                 t->redraw |= TREDRAW_HARD;
964                         }
965                         break;
966                 case OKEY:
967                         if (t->flag & T_PROP_EDIT && event->shift) {
968                                 t->prop_mode = (t->prop_mode + 1) % 6;
969                                 calculatePropRatio(t);
970                                 t->redraw |= TREDRAW_HARD;
971                         }
972                         break;
973                 case PADPLUSKEY:
974                         if(event->alt && t->flag & T_PROP_EDIT) {
975                                 t->prop_size *= 1.1f;
976                                 if(t->spacetype==SPACE_VIEW3D && t->persp != RV3D_ORTHO)
977                                         t->prop_size= MIN2(t->prop_size, ((View3D *)t->view)->far);
978                                 calculatePropRatio(t);
979                         }
980                         t->redraw= 1;
981                         break;
982                 case PAGEUPKEY:
983                 case WHEELDOWNMOUSE:
984                         if (t->flag & T_AUTOIK) {
985                                 transform_autoik_update(t, 1);
986                         }
987                         else view_editmove(event->type);
988                         t->redraw= 1;
989                         break;
990                 case PADMINUS:
991                         if(event->alt && t->flag & T_PROP_EDIT) {
992                                 t->prop_size*= 0.90909090f;
993                                 calculatePropRatio(t);
994                         }
995                         t->redraw= 1;
996                         break;
997                 case PAGEDOWNKEY:
998                 case WHEELUPMOUSE:
999                         if (t->flag & T_AUTOIK) {
1000                                 transform_autoik_update(t, -1);
1001                         }
1002                         else view_editmove(event->type);
1003                         t->redraw= 1;
1004                         break;
1005 //              case NDOFMOTION:
1006 //            viewmoveNDOF(1);
1007   //         break;
1008                 default:
1009                         handled = 0;
1010                         break;
1011                 }
1012
1013                 // Numerical input events
1014                 t->redraw |= handleNumInput(&(t->num), event);
1015
1016                 // NDof input events
1017                 switch(handleNDofInput(&(t->ndof), event))
1018                 {
1019                         case NDOF_CONFIRM:
1020                                 if ((t->options & CTX_NDOF) == 0)
1021                                 {
1022                                         /* Confirm on normal transform only */
1023                                         t->state = TRANS_CONFIRM;
1024                                 }
1025                                 break;
1026                         case NDOF_CANCEL:
1027                                 if (t->options & CTX_NDOF)
1028                                 {
1029                                         /* Cancel on pure NDOF transform */
1030                                         t->state = TRANS_CANCEL;
1031                                 }
1032                                 else
1033                                 {
1034                                         /* Otherwise, just redraw, NDof input was cancelled */
1035                                         t->redraw |= TREDRAW_HARD;
1036                                 }
1037                                 break;
1038                         case NDOF_NOMOVE:
1039                                 if (t->options & CTX_NDOF)
1040                                 {
1041                                         /* Confirm on pure NDOF transform */
1042                                         t->state = TRANS_CONFIRM;
1043                                 }
1044                                 break;
1045                         case NDOF_REFRESH:
1046                                 t->redraw |= TREDRAW_HARD;
1047                                 break;
1048                         default:
1049                                 handled = 0;
1050                                 break;
1051                 }
1052
1053                 // Snapping events
1054                 t->redraw |= handleSnapping(t, event);
1055
1056         }
1057         else if (event->val==KM_RELEASE) {
1058                 switch (event->type){
1059                 case LEFTSHIFTKEY:
1060                 case RIGHTSHIFTKEY:
1061                         t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1062                         t->redraw |= TREDRAW_HARD;
1063                         break;
1064
1065                 case MIDDLEMOUSE:
1066                         if ((t->flag & T_NO_CONSTRAINT)==0) {
1067                                 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1068                                 postSelectConstraint(t);
1069                                 t->redraw |= TREDRAW_HARD;
1070                         }
1071                         break;
1072 //              case LEFTMOUSE:
1073 //              case RIGHTMOUSE:
1074 //                      if(WM_modal_tweak_exit(event, t->event_type))
1075 ////                    if (t->options & CTX_TWEAK)
1076 //                              t->state = TRANS_CONFIRM;
1077 //                      break;
1078                 default:
1079                         handled = 0;
1080                         break;
1081                 }
1082
1083                 /* confirm transform if launch key is released after mouse move */
1084                 if (t->flag & T_RELEASE_CONFIRM)
1085                 {
1086                         /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1087                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE))
1088                         {
1089                                 t->state = TRANS_CONFIRM;
1090                         }
1091                 }
1092         }
1093
1094         // Per transform event, if present
1095         if (t->handleEvent)
1096                 t->redraw |= t->handleEvent(t, event);
1097
1098         if (handled || t->redraw)
1099                 return 0;
1100         else
1101                 return OPERATOR_PASS_THROUGH;
1102 }
1103
1104 int calculateTransformCenter(bContext *C, int centerMode, float *vec)
1105 {
1106         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1107         int success = 1;
1108
1109         t->state = TRANS_RUNNING;
1110
1111         t->options = CTX_NONE;
1112
1113         t->mode = TFM_DUMMY;
1114
1115         initTransInfo(C, t, NULL, NULL);        // internal data, mouse, vectors
1116
1117         createTransData(C, t);                          // make TransData structs from selection
1118
1119         t->around = centerMode;                         // override userdefined mode
1120
1121         if (t->total == 0) {
1122                 success = 0;
1123         }
1124         else {
1125                 success = 1;
1126
1127                 calculateCenter(t);
1128
1129                 // Copy center from constraint center. Transform center can be local
1130                 VECCOPY(vec, t->con.center);
1131         }
1132
1133
1134         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1135         special_aftertrans_update(C, t);
1136
1137         postTrans(C, t);
1138
1139         MEM_freeN(t);
1140
1141         return success;
1142 }
1143
1144 typedef enum {
1145         UP,
1146         DOWN,
1147         LEFT,
1148         RIGHT
1149 } ArrowDirection;
1150 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1151 {
1152         switch(d)
1153         {
1154                 case LEFT:
1155                         offset = -offset;
1156                         length = -length;
1157                         size = -size;
1158                 case RIGHT:
1159                         glBegin(GL_LINES);
1160                         glVertex2s( offset, 0);
1161                         glVertex2s( offset + length, 0);
1162                         glVertex2s( offset + length, 0);
1163                         glVertex2s( offset + length - size, -size);
1164                         glVertex2s( offset + length, 0);
1165                         glVertex2s( offset + length - size,  size);
1166                         glEnd();
1167                         break;
1168                 case DOWN:
1169                         offset = -offset;
1170                         length = -length;
1171                         size = -size;
1172                 case UP:
1173                         glBegin(GL_LINES);
1174                         glVertex2s( 0, offset);
1175                         glVertex2s( 0, offset + length);
1176                         glVertex2s( 0, offset + length);
1177                         glVertex2s(-size, offset + length - size);
1178                         glVertex2s( 0, offset + length);
1179                         glVertex2s( size, offset + length - size);
1180                         glEnd();
1181                         break;
1182         }
1183 }
1184
1185 static void drawArrowHead(ArrowDirection d, short size)
1186 {
1187         switch(d)
1188         {
1189                 case LEFT:
1190                         size = -size;
1191                 case RIGHT:
1192                         glBegin(GL_LINES);
1193                         glVertex2s( 0, 0);
1194                         glVertex2s( -size, -size);
1195                         glVertex2s( 0, 0);
1196                         glVertex2s( -size,  size);
1197                         glEnd();
1198                         break;
1199                 case DOWN:
1200                         size = -size;
1201                 case UP:
1202                         glBegin(GL_LINES);
1203                         glVertex2s( 0, 0);
1204                         glVertex2s(-size, -size);
1205                         glVertex2s( 0, 0);
1206                         glVertex2s( size, -size);
1207                         glEnd();
1208                         break;
1209         }
1210 }
1211
1212 static void drawArc(float size, float angle_start, float angle_end, int segments)
1213 {
1214         float delta = (angle_end - angle_start) / segments;
1215         float angle;
1216
1217         glBegin(GL_LINE_STRIP);
1218
1219         for( angle = angle_start; angle < angle_end; angle += delta)
1220         {
1221                 glVertex2f( cosf(angle) * size, sinf(angle) * size);
1222         }
1223         glVertex2f( cosf(angle_end) * size, sinf(angle_end) * size);
1224
1225         glEnd();
1226 }
1227
1228 static int helpline_poll(bContext *C)
1229 {
1230         ARegion *ar= CTX_wm_region(C);
1231         
1232         if(ar && ar->regiontype==RGN_TYPE_WINDOW)
1233                 return 1;
1234         return 0;
1235 }
1236
1237 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1238 {
1239         TransInfo *t = (TransInfo*)customdata;
1240
1241         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR))
1242         {
1243                 float vecrot[3], cent[2];
1244                 int mval[2];
1245
1246                 mval[0]= x;
1247                 mval[1]= y;
1248
1249                 VECCOPY(vecrot, t->center);
1250                 if(t->flag & T_EDIT) {
1251                         Object *ob= t->obedit;
1252                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1253                 }
1254                 else if(t->flag & T_POSE) {
1255                         Object *ob=t->poseobj;
1256                         if(ob) mul_m4_v3(ob->obmat, vecrot);
1257                 }
1258
1259                 projectFloatView(t, vecrot, cent);      // no overflow in extreme cases
1260
1261                 glPushMatrix();
1262
1263                 switch(t->helpline)
1264                 {
1265                         case HLP_SPRING:
1266                                 UI_ThemeColor(TH_WIRE);
1267
1268                                 setlinestyle(3);
1269                                 glBegin(GL_LINE_STRIP);
1270                                 glVertex2sv(t->mval);
1271                                 glVertex2fv(cent);
1272                                 glEnd();
1273
1274                                 glTranslatef(mval[0], mval[1], 0);
1275                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1276
1277                                 setlinestyle(0);
1278                                 glLineWidth(3.0);
1279                                 drawArrow(UP, 5, 10, 5);
1280                                 drawArrow(DOWN, 5, 10, 5);
1281                                 glLineWidth(1.0);
1282                                 break;
1283                         case HLP_HARROW:
1284                                 UI_ThemeColor(TH_WIRE);
1285
1286                                 glTranslatef(mval[0], mval[1], 0);
1287
1288                                 glLineWidth(3.0);
1289                                 drawArrow(RIGHT, 5, 10, 5);
1290                                 drawArrow(LEFT, 5, 10, 5);
1291                                 glLineWidth(1.0);
1292                                 break;
1293                         case HLP_VARROW:
1294                                 UI_ThemeColor(TH_WIRE);
1295
1296                                 glTranslatef(mval[0], mval[1], 0);
1297
1298                                 glLineWidth(3.0);
1299                                 glBegin(GL_LINES);
1300                                 drawArrow(UP, 5, 10, 5);
1301                                 drawArrow(DOWN, 5, 10, 5);
1302                                 glLineWidth(1.0);
1303                                 break;
1304                         case HLP_ANGLE:
1305                                 {
1306                                         float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1307                                         float angle = atan2f(dy, dx);
1308                                         float dist = sqrtf(dx*dx + dy*dy);
1309                                         float delta_angle = MIN2(15.0f / dist, (float)M_PI/4.0f);
1310                                         float spacing_angle = MIN2(5.0f / dist, (float)M_PI/12.0f);
1311                                         UI_ThemeColor(TH_WIRE);
1312
1313                                         setlinestyle(3);
1314                                         glBegin(GL_LINE_STRIP);
1315                                         glVertex2sv(t->mval);
1316                                         glVertex2fv(cent);
1317                                         glEnd();
1318
1319                                         glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1320
1321                                         setlinestyle(0);
1322                                         glLineWidth(3.0);
1323                                         drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1324                                         drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1325
1326                                         glPushMatrix();
1327
1328                                         glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1329                                         glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1330
1331                                         drawArrowHead(DOWN, 5);
1332
1333                                         glPopMatrix();
1334
1335                                         glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1336                                         glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1337
1338                                         drawArrowHead(UP, 5);
1339
1340                                         glLineWidth(1.0);
1341                                         break;
1342                                 }
1343                                 case HLP_TRACKBALL:
1344                                 {
1345                                         unsigned char col[3], col2[3];
1346                                         UI_GetThemeColor3ubv(TH_GRID, col);
1347
1348                                         glTranslatef(mval[0], mval[1], 0);
1349
1350                                         glLineWidth(3.0);
1351
1352                                         UI_make_axis_color(col, col2, 'X');
1353                                         glColor3ubv((GLubyte *)col2);
1354
1355                                         drawArrow(RIGHT, 5, 10, 5);
1356                                         drawArrow(LEFT, 5, 10, 5);
1357
1358                                         UI_make_axis_color(col, col2, 'Y');
1359                                         glColor3ubv((GLubyte *)col2);
1360
1361                                         drawArrow(UP, 5, 10, 5);
1362                                         drawArrow(DOWN, 5, 10, 5);
1363                                         glLineWidth(1.0);
1364                                         break;
1365                                 }
1366                 }
1367
1368                 glPopMatrix();
1369         }
1370 }
1371
1372 static void drawTransformView(const struct bContext *C, struct ARegion *UNUSED(ar), void *arg)
1373 {
1374         TransInfo *t = arg;
1375
1376         drawConstraint(t);
1377         drawPropCircle(C, t);
1378         drawSnapping(C, t);
1379 }
1380
1381 #if 0
1382 static void drawTransformPixel(const struct bContext *UNUSED(C), struct ARegion *UNUSED(ar), void *UNUSED(arg))
1383 {
1384 //      TransInfo *t = arg;
1385 //
1386 //      drawHelpline(C, t->mval[0], t->mval[1], t);
1387 }
1388 #endif
1389
1390 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1391 {
1392         ToolSettings *ts = CTX_data_tool_settings(C);
1393         int constraint_axis[3] = {0, 0, 0};
1394         int proportional = 0;
1395
1396         if (RNA_struct_find_property(op->ptr, "value"))
1397         {
1398                 if (t->flag & T_AUTOVALUES)
1399                 {
1400                         RNA_float_set_array(op->ptr, "value", t->auto_values);
1401                 }
1402                 else
1403                 {
1404                         RNA_float_set_array(op->ptr, "value", t->values);
1405                 }
1406         }
1407
1408         /* convert flag to enum */
1409         switch(t->flag & (T_PROP_EDIT|T_PROP_CONNECTED))
1410         {
1411         case (T_PROP_EDIT|T_PROP_CONNECTED):
1412                 proportional = PROP_EDIT_CONNECTED;
1413                 break;
1414         case T_PROP_EDIT:
1415                 proportional = PROP_EDIT_ON;
1416                 break;
1417         default:
1418                 proportional = PROP_EDIT_OFF;
1419         }
1420
1421         // If modal, save settings back in scene if not set as operator argument
1422         if (t->flag & T_MODAL) {
1423
1424                 /* save settings if not set in operator */
1425                 if (RNA_struct_find_property(op->ptr, "proportional") && !RNA_property_is_set(op->ptr, "proportional")) {
1426                         if (t->obedit)
1427                                 ts->proportional = proportional;
1428                         else
1429                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1430                 }
1431
1432                 if (RNA_struct_find_property(op->ptr, "proportional_size") && !RNA_property_is_set(op->ptr, "proportional_size")) {
1433                         ts->proportional_size = t->prop_size;
1434                 }
1435                         
1436                 if (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") && !RNA_property_is_set(op->ptr, "proportional_edit_falloff")) {
1437                         ts->prop_mode = t->prop_mode;
1438                 }
1439                 
1440                 /* do we check for parameter? */
1441                 if (t->modifiers & MOD_SNAP) {
1442                         ts->snap_flag |= SCE_SNAP;
1443                 } else {
1444                         ts->snap_flag &= ~SCE_SNAP;
1445                 }
1446
1447                 if(t->spacetype == SPACE_VIEW3D) {
1448                         if (RNA_struct_find_property(op->ptr, "constraint_orientation") && !RNA_property_is_set(op->ptr, "constraint_orientation")) {
1449                                 View3D *v3d = t->view;
1450         
1451                                 v3d->twmode = t->current_orientation;
1452                         }
1453                 }
1454         }
1455         
1456         if (RNA_struct_find_property(op->ptr, "proportional"))
1457         {
1458                 RNA_enum_set(op->ptr, "proportional", proportional);
1459                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1460                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1461         }
1462
1463         if (RNA_struct_find_property(op->ptr, "axis"))
1464         {
1465                 RNA_float_set_array(op->ptr, "axis", t->axis);
1466         }
1467
1468         if (RNA_struct_find_property(op->ptr, "mirror"))
1469         {
1470                 RNA_boolean_set(op->ptr, "mirror", t->flag & T_MIRROR);
1471         }
1472
1473         if (RNA_struct_find_property(op->ptr, "constraint_axis"))
1474         {
1475                 /* constraint orientation can be global, event if user selects something else
1476                  * so use the orientation in the constraint if set
1477                  * */
1478                 if (t->con.mode & CON_APPLY) {
1479                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1480                 } else {
1481                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1482                 }
1483
1484                 if (t->con.mode & CON_APPLY)
1485                 {
1486                         if (t->con.mode & CON_AXIS0) {
1487                                 constraint_axis[0] = 1;
1488                         }
1489                         if (t->con.mode & CON_AXIS1) {
1490                                 constraint_axis[1] = 1;
1491                         }
1492                         if (t->con.mode & CON_AXIS2) {
1493                                 constraint_axis[2] = 1;
1494                         }
1495                 }
1496
1497                 RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1498         }
1499 }
1500
1501 /* note: caller needs to free 't' on a 0 return */
1502 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1503 {
1504         int options = 0;
1505
1506         t->context = C;
1507
1508         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1509
1510         t->state = TRANS_STARTING;
1511
1512         if(RNA_struct_find_property(op->ptr, "texture_space"))
1513                 if(RNA_boolean_get(op->ptr, "texture_space"))
1514                         options |= CTX_TEXTURE;
1515         
1516         t->options = options;
1517
1518         t->mode = mode;
1519
1520         t->launch_event = event ? event->type : -1;
1521
1522         if (t->launch_event == EVT_TWEAK_R)
1523         {
1524                 t->launch_event = RIGHTMOUSE;
1525         }
1526         else if (t->launch_event == EVT_TWEAK_L)
1527         {
1528                 t->launch_event = LEFTMOUSE;
1529         }
1530
1531         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1532         // For manipulator only, so assume LEFTMOUSE
1533         if (t->launch_event == 0)
1534         {
1535                 t->launch_event = LEFTMOUSE;
1536         }
1537
1538         if (!initTransInfo(C, t, op, event))                                    // internal data, mouse, vectors
1539         {
1540                 return 0;
1541         }
1542
1543         if(t->spacetype == SPACE_VIEW3D)
1544         {
1545                 //calc_manipulator_stats(curarea);
1546                 initTransformOrientation(C, t);
1547
1548                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1549                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1550                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1551                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1552         }
1553         else if(t->spacetype == SPACE_IMAGE) {
1554                 unit_m3(t->spacemtx);
1555                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1556                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1557         }
1558         else
1559                 unit_m3(t->spacemtx);
1560
1561         createTransData(C, t);                  // make TransData structs from selection
1562
1563         if (t->total == 0) {
1564                 postTrans(C, t);
1565                 return 0;
1566         }
1567
1568         /* Stupid code to have Ctrl-Click on manipulator work ok */
1569         if(event)
1570         {
1571                 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1572                 wmKeyMapItem *kmi;
1573
1574                 for (kmi = keymap->items.first; kmi; kmi = kmi->next)
1575                 {
1576                         if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS)
1577                         {
1578                                 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
1579                                         (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1580                                         (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
1581                                         (kmi->type == OSKEY && event->oskey)) {
1582                                         t->modifiers |= MOD_SNAP_INVERT;
1583                                 }
1584                                 break;
1585                         }
1586                 }
1587
1588         }
1589
1590         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1591
1592         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1593         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1594         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1595         mode = t->mode;
1596
1597         calculatePropRatio(t);
1598         calculateCenter(t);
1599
1600         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1601
1602         switch (mode) {
1603         case TFM_TRANSLATION:
1604                 initTranslation(t);
1605                 break;
1606         case TFM_ROTATION:
1607                 initRotation(t);
1608                 break;
1609         case TFM_RESIZE:
1610                 initResize(t);
1611                 break;
1612         case TFM_TOSPHERE:
1613                 initToSphere(t);
1614                 break;
1615         case TFM_SHEAR:
1616                 initShear(t);
1617                 break;
1618         case TFM_WARP:
1619                 initWarp(t);
1620                 break;
1621         case TFM_SHRINKFATTEN:
1622                 initShrinkFatten(t);
1623                 break;
1624         case TFM_TILT:
1625                 initTilt(t);
1626                 break;
1627         case TFM_CURVE_SHRINKFATTEN:
1628                 initCurveShrinkFatten(t);
1629                 break;
1630         case TFM_TRACKBALL:
1631                 initTrackball(t);
1632                 break;
1633         case TFM_PUSHPULL:
1634                 initPushPull(t);
1635                 break;
1636         case TFM_CREASE:
1637                 initCrease(t);
1638                 break;
1639         case TFM_BONESIZE:
1640                 {       /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1641                         bArmature *arm= t->poseobj->data;
1642                         if(arm->drawtype==ARM_ENVELOPE)
1643                                 initBoneEnvelope(t);
1644                         else
1645                                 initBoneSize(t);
1646                 }
1647                 break;
1648         case TFM_BONE_ENVELOPE:
1649                 initBoneEnvelope(t);
1650                 break;
1651         case TFM_EDGE_SLIDE:
1652                 initEdgeSlide(t);
1653                 break;
1654         case TFM_BONE_ROLL:
1655                 initBoneRoll(t);
1656                 break;
1657         case TFM_TIME_TRANSLATE:
1658                 initTimeTranslate(t);
1659                 break;
1660         case TFM_TIME_SLIDE:
1661                 initTimeSlide(t);
1662                 break;
1663         case TFM_TIME_SCALE:
1664                 initTimeScale(t);
1665                 break;
1666         case TFM_TIME_DUPLICATE:
1667                 /* same as TFM_TIME_EXTEND, but we need the mode info for later 
1668                  * so that duplicate-culling will work properly
1669                  */
1670                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1671                         initTranslation(t);
1672                 else
1673                         initTimeTranslate(t);
1674                 t->mode = mode;
1675                 break;
1676         case TFM_TIME_EXTEND:
1677                 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1678                  * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1679                  * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1680                  * depending on which editor this was called from
1681                  */
1682                 if ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)
1683                         initTranslation(t);
1684                 else
1685                         initTimeTranslate(t);
1686                 break;
1687         case TFM_BAKE_TIME:
1688                 initBakeTime(t);
1689                 break;
1690         case TFM_MIRROR:
1691                 initMirror(t);
1692                 break;
1693         case TFM_BEVEL:
1694                 initBevel(t);
1695                 break;
1696         case TFM_BWEIGHT:
1697                 initBevelWeight(t);
1698                 break;
1699         case TFM_ALIGN:
1700                 initAlign(t);
1701                 break;
1702         case TFM_SEQ_SLIDE:
1703                 initSeqSlide(t);
1704                 break;
1705         }
1706
1707         if(t->state == TRANS_CANCEL)
1708         {
1709                 postTrans(C, t);
1710                 return 0;
1711         }
1712
1713
1714         /* overwrite initial values if operator supplied a non-null vector */
1715         if (RNA_property_is_set(op->ptr, "value"))
1716         {
1717                 float values[4]= {0}; /* incase value isn't length 4, avoid uninitialized memory  */
1718                 RNA_float_get_array(op->ptr, "value", values);
1719                 QUATCOPY(t->values, values);
1720                 QUATCOPY(t->auto_values, values);
1721                 t->flag |= T_AUTOVALUES;
1722         }
1723
1724         /* Transformation axis from operator */
1725         if (RNA_struct_find_property(op->ptr, "axis") && RNA_property_is_set(op->ptr, "axis"))
1726         {
1727                 RNA_float_get_array(op->ptr, "axis", t->axis);
1728                 normalize_v3(t->axis);
1729                 copy_v3_v3(t->axis_orig, t->axis);
1730         }
1731
1732         /* Constraint init from operator */
1733         if (RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_axis"))
1734         {
1735                 int constraint_axis[3];
1736
1737                 RNA_boolean_get_array(op->ptr, "constraint_axis", constraint_axis);
1738
1739                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2])
1740                 {
1741                         t->con.mode |= CON_APPLY;
1742
1743                         if (constraint_axis[0]) {
1744                                 t->con.mode |= CON_AXIS0;
1745                         }
1746                         if (constraint_axis[1]) {
1747                                 t->con.mode |= CON_AXIS1;
1748                         }
1749                         if (constraint_axis[2]) {
1750                                 t->con.mode |= CON_AXIS2;
1751                         }
1752
1753                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
1754                 }
1755         }
1756
1757         t->context = NULL;
1758
1759         return 1;
1760 }
1761
1762 void transformApply(bContext *C, TransInfo *t)
1763 {
1764         t->context = C;
1765
1766         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT)))
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, 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, 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, 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, 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                      &nb