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