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