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