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