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