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