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