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