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