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