metaballs were not scaling/rotating about their own centers when V3D_LOCAL was used.
[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         t->options = CTX_NONE;
1411
1412         t->mode = TFM_DUMMY;
1413
1414         initTransInfo(C, t, NULL, NULL);    // internal data, mouse, vectors
1415
1416         createTransData(C, t);              // make TransData structs from selection
1417
1418         t->around = centerMode;             // override userdefined mode
1419
1420         if (t->total == 0) {
1421                 success = FALSE;
1422         }
1423         else {
1424                 success = TRUE;
1425
1426                 calculateCenter(t);
1427
1428                 if (cent2d) {
1429                         copy_v2_v2_int(cent2d, t->center2d);
1430                 }
1431
1432                 if (cent3d) {
1433                         // Copy center from constraint center. Transform center can be local
1434                         copy_v3_v3(cent3d, t->con.center);
1435                 }
1436         }
1437
1438
1439         /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1440         special_aftertrans_update(C, t);
1441
1442         postTrans(C, t);
1443
1444         MEM_freeN(t);
1445
1446         return success;
1447 }
1448
1449 typedef enum {
1450         UP,
1451         DOWN,
1452         LEFT,
1453         RIGHT
1454 } ArrowDirection;
1455 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1456 {
1457         switch (d) {
1458                 case LEFT:
1459                         offset = -offset;
1460                         length = -length;
1461                         size = -size;
1462                 case RIGHT:
1463                         glBegin(GL_LINES);
1464                         glVertex2s(offset, 0);
1465                         glVertex2s(offset + length, 0);
1466                         glVertex2s(offset + length, 0);
1467                         glVertex2s(offset + length - size, -size);
1468                         glVertex2s(offset + length, 0);
1469                         glVertex2s(offset + length - size,  size);
1470                         glEnd();
1471                         break;
1472                 case DOWN:
1473                         offset = -offset;
1474                         length = -length;
1475                         size = -size;
1476                 case UP:
1477                         glBegin(GL_LINES);
1478                         glVertex2s(0, offset);
1479                         glVertex2s(0, offset + length);
1480                         glVertex2s(0, offset + length);
1481                         glVertex2s(-size, offset + length - size);
1482                         glVertex2s(0, offset + length);
1483                         glVertex2s(size, offset + length - size);
1484                         glEnd();
1485                         break;
1486         }
1487 }
1488
1489 static void drawArrowHead(ArrowDirection d, short size)
1490 {
1491         switch (d) {
1492                 case LEFT:
1493                         size = -size;
1494                 case RIGHT:
1495                         glBegin(GL_LINES);
1496                         glVertex2s(0, 0);
1497                         glVertex2s(-size, -size);
1498                         glVertex2s(0, 0);
1499                         glVertex2s(-size,  size);
1500                         glEnd();
1501                         break;
1502                 case DOWN:
1503                         size = -size;
1504                 case UP:
1505                         glBegin(GL_LINES);
1506                         glVertex2s(0, 0);
1507                         glVertex2s(-size, -size);
1508                         glVertex2s(0, 0);
1509                         glVertex2s(size, -size);
1510                         glEnd();
1511                         break;
1512         }
1513 }
1514
1515 static void drawArc(float size, float angle_start, float angle_end, int segments)
1516 {
1517         float delta = (angle_end - angle_start) / segments;
1518         float angle;
1519         int a;
1520
1521         glBegin(GL_LINE_STRIP);
1522
1523         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1524                 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1525         }
1526         glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1527
1528         glEnd();
1529 }
1530
1531 static int helpline_poll(bContext *C)
1532 {
1533         ARegion *ar = CTX_wm_region(C);
1534         
1535         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1536                 return 1;
1537         return 0;
1538 }
1539
1540 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1541 {
1542         TransInfo *t = (TransInfo *)customdata;
1543
1544         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1545                 float vecrot[3], cent[2];
1546                 int mval[2];
1547
1548                 mval[0] = x;
1549                 mval[1] = y;
1550
1551                 copy_v3_v3(vecrot, t->center);
1552                 if (t->flag & T_EDIT) {
1553                         Object *ob = t->obedit;
1554                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1555                 }
1556                 else if (t->flag & T_POSE) {
1557                         Object *ob = t->poseobj;
1558                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1559                 }
1560
1561                 projectFloatViewEx(t, vecrot, cent, V3D_PROJ_TEST_CLIP_ZERO);
1562
1563                 glPushMatrix();
1564
1565                 switch (t->helpline) {
1566                         case HLP_SPRING:
1567                                 UI_ThemeColor(TH_WIRE);
1568
1569                                 setlinestyle(3);
1570                                 glBegin(GL_LINE_STRIP);
1571                                 glVertex2iv(t->mval);
1572                                 glVertex2fv(cent);
1573                                 glEnd();
1574
1575                                 glTranslatef(mval[0], mval[1], 0);
1576                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1577
1578                                 setlinestyle(0);
1579                                 glLineWidth(3.0);
1580                                 drawArrow(UP, 5, 10, 5);
1581                                 drawArrow(DOWN, 5, 10, 5);
1582                                 glLineWidth(1.0);
1583                                 break;
1584                         case HLP_HARROW:
1585                                 UI_ThemeColor(TH_WIRE);
1586
1587                                 glTranslatef(mval[0], mval[1], 0);
1588
1589                                 glLineWidth(3.0);
1590                                 drawArrow(RIGHT, 5, 10, 5);
1591                                 drawArrow(LEFT, 5, 10, 5);
1592                                 glLineWidth(1.0);
1593                                 break;
1594                         case HLP_VARROW:
1595                                 UI_ThemeColor(TH_WIRE);
1596
1597                                 glTranslatef(mval[0], mval[1], 0);
1598
1599                                 glLineWidth(3.0);
1600                                 drawArrow(UP, 5, 10, 5);
1601                                 drawArrow(DOWN, 5, 10, 5);
1602                                 glLineWidth(1.0);
1603                                 break;
1604                         case HLP_ANGLE:
1605                         {
1606                                 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1607                                 float angle = atan2f(dy, dx);
1608                                 float dist = sqrtf(dx * dx + dy * dy);
1609                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1610                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1611                                 UI_ThemeColor(TH_WIRE);
1612
1613                                 setlinestyle(3);
1614                                 glBegin(GL_LINE_STRIP);
1615                                 glVertex2iv(t->mval);
1616                                 glVertex2fv(cent);
1617                                 glEnd();
1618
1619                                 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1620
1621                                 setlinestyle(0);
1622                                 glLineWidth(3.0);
1623                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1624                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1625
1626                                 glPushMatrix();
1627
1628                                 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1629                                 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1630
1631                                 drawArrowHead(DOWN, 5);
1632
1633                                 glPopMatrix();
1634
1635                                 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1636                                 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1637
1638                                 drawArrowHead(UP, 5);
1639
1640                                 glLineWidth(1.0);
1641                                 break;
1642                         }
1643                         case HLP_TRACKBALL:
1644                         {
1645                                 unsigned char col[3], col2[3];
1646                                 UI_GetThemeColor3ubv(TH_GRID, col);
1647
1648                                 glTranslatef(mval[0], mval[1], 0);
1649
1650                                 glLineWidth(3.0);
1651
1652                                 UI_make_axis_color(col, col2, 'X');
1653                                 glColor3ubv((GLubyte *)col2);
1654
1655                                 drawArrow(RIGHT, 5, 10, 5);
1656                                 drawArrow(LEFT, 5, 10, 5);
1657
1658                                 UI_make_axis_color(col, col2, 'Y');
1659                                 glColor3ubv((GLubyte *)col2);
1660
1661                                 drawArrow(UP, 5, 10, 5);
1662                                 drawArrow(DOWN, 5, 10, 5);
1663                                 glLineWidth(1.0);
1664                                 break;
1665                         }
1666                 }
1667
1668                 glPopMatrix();
1669         }
1670 }
1671
1672 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1673 {
1674         TransInfo *t = arg;
1675
1676         drawConstraint(t);
1677         drawPropCircle(C, t);
1678         drawSnapping(C, t);
1679
1680         /* edge slide, vert slide */
1681         drawEdgeSlide(C, t);
1682         drawVertSlide(C, t);
1683 }
1684
1685 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1686 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1687 {
1688         rcti rect;
1689         const char *printable = IFACE_("Auto Keying On");
1690         float      printable_size[2];
1691         int xco, yco;
1692
1693         ED_region_visible_rect(ar, &rect);
1694         
1695         BLF_width_and_height_default(printable, &printable_size[0], &printable_size[1]);
1696         
1697         xco = rect.xmax - (int)printable_size[0] - 10;
1698         yco = rect.ymax - (int)printable_size[1] - 10;
1699         
1700         /* warning text (to clarify meaning of overlays)
1701          * - original color was red to match the icon, but that clashes badly with a less nasty border
1702          */
1703         UI_ThemeColorShade(TH_TEXT_HI, -50);
1704 #ifdef WITH_INTERNATIONAL
1705         BLF_draw_default(xco, ar->winy - 17, 0.0f, printable, sizeof(printable));
1706 #else
1707         BLF_draw_default_ascii(xco, ar->winy - 17, 0.0f, printable, sizeof(printable));
1708 #endif
1709         
1710         /* autokey recording icon... */
1711         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1712         glEnable(GL_BLEND);
1713         
1714         xco -= (ICON_DEFAULT_WIDTH + 2);
1715         UI_icon_draw(xco, yco, ICON_REC);
1716         
1717         glDisable(GL_BLEND);
1718 }
1719
1720 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1721 {       
1722         TransInfo *t = arg;
1723         Scene *scene = t->scene;
1724         Object *ob = OBACT;
1725         
1726         /* draw autokeyframing hint in the corner 
1727          * - only draw if enabled (advanced users may be distracted/annoyed), 
1728          *   for objects that will be autokeyframed (no point ohterwise),
1729          *   AND only for the active region (as showing all is too overwhelming)
1730          */
1731         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1732                 if (ar == t->ar) {
1733                         if (t->flag & (T_OBJECT | T_POSE)) {
1734                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1735                                         drawAutoKeyWarning(t, ar);
1736                                 }
1737                         }
1738                 }
1739         }
1740 }
1741
1742 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1743 {
1744         ToolSettings *ts = CTX_data_tool_settings(C);
1745         int constraint_axis[3] = {0, 0, 0};
1746         int proportional = 0;
1747         PropertyRNA *prop;
1748
1749         // Save back mode in case we're in the generic operator
1750         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1751                 RNA_property_enum_set(op->ptr, prop, t->mode);
1752         }
1753
1754         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1755                 float *values = (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1756                 if (RNA_property_array_check(prop)) {
1757                         RNA_property_float_set_array(op->ptr, prop, values);
1758                 }
1759                 else {
1760                         RNA_property_float_set(op->ptr, prop, values[0]);
1761                 }
1762         }
1763
1764         /* convert flag to enum */
1765         switch (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) {
1766                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1767                         proportional = PROP_EDIT_CONNECTED;
1768                         break;
1769                 case T_PROP_EDIT:
1770                         proportional = PROP_EDIT_ON;
1771                         break;
1772                 default:
1773                         proportional = PROP_EDIT_OFF;
1774         }
1775
1776         // If modal, save settings back in scene if not set as operator argument
1777         if (t->flag & T_MODAL) {
1778
1779                 /* save settings if not set in operator */
1780                 if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1781                     !RNA_property_is_set(op->ptr, prop))
1782                 {
1783                         if (t->obedit)
1784                                 ts->proportional = proportional;
1785                         else if (t->options & CTX_MASK)
1786                                 ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1787                         else
1788                                 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1789                 }
1790
1791                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1792                     !RNA_property_is_set(op->ptr, prop))
1793                 {
1794                         ts->proportional_size = t->prop_size;
1795                 }
1796
1797                 if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1798                     !RNA_property_is_set(op->ptr, prop))
1799                 {
1800                         ts->prop_mode = t->prop_mode;
1801                 }
1802                 
1803                 /* do we check for parameter? */
1804                 if (t->modifiers & MOD_SNAP) {
1805                         ts->snap_flag |= SCE_SNAP;
1806                 }
1807                 else {
1808                         ts->snap_flag &= ~SCE_SNAP;
1809                 }
1810
1811                 if (t->spacetype == SPACE_VIEW3D) {
1812                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1813                             !RNA_property_is_set(op->ptr, prop))
1814                         {
1815                                 View3D *v3d = t->view;
1816
1817                                 v3d->twmode = t->current_orientation;
1818                         }
1819                 }
1820         }
1821         
1822         if (RNA_struct_find_property(op->ptr, "proportional")) {
1823                 RNA_enum_set(op->ptr, "proportional", proportional);
1824                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1825                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1826         }
1827
1828         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1829                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1830         }
1831
1832         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1833                 RNA_property_boolean_set(op->ptr, prop, t->flag & T_MIRROR);
1834         }
1835
1836         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1837                 /* constraint orientation can be global, event if user selects something else
1838                  * so use the orientation in the constraint if set
1839                  * */
1840                 if (t->con.mode & CON_APPLY) {
1841                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1842                 }
1843                 else {
1844                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1845                 }
1846
1847                 if (t->con.mode & CON_APPLY) {
1848                         if (t->con.mode & CON_AXIS0) {
1849                                 constraint_axis[0] = 1;
1850                         }
1851                         if (t->con.mode & CON_AXIS1) {
1852                                 constraint_axis[1] = 1;
1853                         }
1854                         if (t->con.mode & CON_AXIS2) {
1855                                 constraint_axis[2] = 1;
1856                         }
1857                 }
1858
1859                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
1860         }
1861 }
1862
1863 /* note: caller needs to free 't' on a 0 return */
1864 int initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event, int mode)
1865 {
1866         int options = 0;
1867         PropertyRNA *prop;
1868
1869         t->context = C;
1870
1871         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1872
1873         t->state = TRANS_STARTING;
1874
1875         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
1876                 if (RNA_property_boolean_get(op->ptr, prop)) {
1877                         options |= CTX_TEXTURE;
1878                 }
1879         }
1880         
1881         t->options = options;
1882
1883         t->mode = mode;
1884
1885         t->launch_event = event ? event->type : -1;
1886
1887         if (t->launch_event == EVT_TWEAK_R) {
1888                 t->launch_event = RIGHTMOUSE;
1889         }
1890         else if (t->launch_event == EVT_TWEAK_L) {
1891                 t->launch_event = LEFTMOUSE;
1892         }
1893
1894         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1895         // For manipulator only, so assume LEFTMOUSE
1896         if (t->launch_event == 0) {
1897                 t->launch_event = LEFTMOUSE;
1898         }
1899
1900         if (!initTransInfo(C, t, op, event)) {  /* internal data, mouse, vectors */
1901                 return 0;
1902         }
1903
1904         if (t->spacetype == SPACE_VIEW3D) {
1905                 //calc_manipulator_stats(curarea);
1906                 initTransformOrientation(C, t);
1907
1908                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1909                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1910                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1911                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1912         }
1913         else if (t->spacetype == SPACE_IMAGE) {
1914                 unit_m3(t->spacemtx);
1915                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1916                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1917                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1918         }
1919         else if (t->spacetype == SPACE_CLIP) {
1920                 unit_m3(t->spacemtx);
1921                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1922         }
1923         else if (t->spacetype == SPACE_NODE) {
1924                 unit_m3(t->spacemtx);
1925                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
1926                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1927                 /*t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);*/
1928         }
1929         else
1930                 unit_m3(t->spacemtx);
1931
1932         createTransData(C, t);          // make TransData structs from selection
1933
1934         if (t->total == 0) {
1935                 postTrans(C, t);
1936                 return 0;
1937         }
1938
1939         /* Stupid code to have Ctrl-Click on manipulator work ok */
1940         if (event) {
1941                 /* do this only for translation/rotation/resize due to only this
1942                  * moded are available from manipulator and doing such check could
1943                  * lead to keymap conflicts for other modes (see #31584)
1944                  */
1945                 if (ELEM3(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
1946                         wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1947                         wmKeyMapItem *kmi;
1948
1949                         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1950                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
1951                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
1952                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1953                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
1954                                             ((kmi->type == OSKEY) &&                         event->oskey) )
1955                                         {
1956                                                 t->modifiers |= MOD_SNAP_INVERT;
1957                                         }
1958                                         break;
1959                                 }
1960                         }
1961                 }
1962
1963         }
1964
1965         t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1966
1967         initSnapping(t, op); // Initialize snapping data AFTER mode flags
1968
1969         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1970         /* EVIL2: we gave as argument also texture space context bit... was cleared */
1971         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1972         mode = t->mode;
1973
1974         calculatePropRatio(t);
1975         calculateCenter(t);
1976
1977         initMouseInput(t, &t->mouse, t->center2d, t->imval);
1978
1979         switch (mode) {
1980                 case TFM_TRANSLATION:
1981                         initTranslation(t);
1982                         break;
1983                 case TFM_ROTATION:
1984                         initRotation(t);
1985                         break;
1986                 case TFM_RESIZE:
1987                         initResize(t);
1988                         break;
1989                 case TFM_SKIN_RESIZE:
1990                         initSkinResize(t);
1991                         break;
1992                 case TFM_TOSPHERE:
1993                         initToSphere(t);
1994                         break;
1995                 case TFM_SHEAR:
1996                         initShear(t);
1997                         break;
1998                 case TFM_WARP:
1999                         initWarp(t);
2000                         break;
2001                 case TFM_SHRINKFATTEN:
2002                         initShrinkFatten(t);
2003                         break;
2004                 case TFM_TILT:
2005                         initTilt(t);
2006                         break;
2007                 case TFM_CURVE_SHRINKFATTEN:
2008                         initCurveShrinkFatten(t);
2009                         break;
2010                 case TFM_MASK_SHRINKFATTEN:
2011                         initMaskShrinkFatten(t);
2012                         break;
2013                 case TFM_TRACKBALL:
2014                         initTrackball(t);
2015                         break;
2016                 case TFM_PUSHPULL:
2017                         initPushPull(t);
2018                         break;
2019                 case TFM_CREASE:
2020                         initCrease(t);
2021                         break;
2022                 case TFM_BONESIZE:
2023                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2024                         bArmature *arm = t->poseobj->data;
2025                         if (arm->drawtype == ARM_ENVELOPE)
2026                                 initBoneEnvelope(t);
2027                         else
2028                                 initBoneSize(t);
2029                 }
2030                 break;
2031                 case TFM_BONE_ENVELOPE:
2032                         initBoneEnvelope(t);
2033                         break;
2034                 case TFM_EDGE_SLIDE:
2035                         initEdgeSlide(t);
2036                         break;
2037                 case TFM_VERT_SLIDE:
2038                         initVertSlide(t);
2039                         break;
2040                 case TFM_BONE_ROLL:
2041                         initBoneRoll(t);
2042                         break;
2043                 case TFM_TIME_TRANSLATE:
2044                         initTimeTranslate(t);
2045                         break;
2046                 case TFM_TIME_SLIDE:
2047                         initTimeSlide(t);
2048                         break;
2049                 case TFM_TIME_SCALE:
2050                         initTimeScale(t);
2051                         break;
2052                 case TFM_TIME_DUPLICATE:
2053                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2054                          * so that duplicate-culling will work properly
2055                          */
2056                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2057                                 initTranslation(t);
2058                         else
2059                                 initTimeTranslate(t);
2060                         t->mode = mode;
2061                         break;
2062                 case TFM_TIME_EXTEND:
2063                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2064                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2065                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2066                          * depending on which editor this was called from
2067                          */
2068                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2069                                 initTranslation(t);
2070                         else
2071                                 initTimeTranslate(t);
2072                         break;
2073                 case TFM_BAKE_TIME:
2074                         initBakeTime(t);
2075                         break;
2076                 case TFM_MIRROR:
2077                         initMirror(t);
2078                         break;
2079                 case TFM_BEVEL:
2080                         initBevel(t);
2081                         break;
2082                 case TFM_BWEIGHT:
2083                         initBevelWeight(t);
2084                         break;
2085                 case TFM_ALIGN:
2086                         initAlign(t);
2087                         break;
2088                 case TFM_SEQ_SLIDE:
2089                         initSeqSlide(t);
2090                         break;
2091         }
2092
2093         if (t->state == TRANS_CANCEL) {
2094                 postTrans(C, t);
2095                 return 0;
2096         }
2097
2098
2099         /* overwrite initial values if operator supplied a non-null vector */
2100         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2101                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2102
2103                 if (RNA_property_array_check(prop)) {
2104                         RNA_float_get_array(op->ptr, "value", values);
2105                 }
2106                 else {
2107                         values[0] = RNA_float_get(op->ptr, "value");
2108                 }
2109
2110                 copy_v4_v4(t->values, values);
2111                 copy_v4_v4(t->auto_values, values);
2112                 t->flag |= T_AUTOVALUES;
2113         }
2114
2115         /* Transformation axis from operator */
2116         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2117                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2118                 normalize_v3(t->axis);
2119                 copy_v3_v3(t->axis_orig, t->axis);
2120         }
2121
2122         /* Constraint init from operator */
2123         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2124                 int constraint_axis[3];
2125
2126                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2127
2128                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2129                         t->con.mode |= CON_APPLY;
2130
2131                         if (constraint_axis[0]) {
2132                                 t->con.mode |= CON_AXIS0;
2133                         }
2134                         if (constraint_axis[1]) {
2135                                 t->con.mode |= CON_AXIS1;
2136                         }
2137                         if (constraint_axis[2]) {
2138                                 t->con.mode |= CON_AXIS2;
2139                         }
2140
2141                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2142                 }
2143         }
2144
2145         t->context = NULL;
2146
2147         return 1;
2148 }
2149
2150 void transformApply(bContext *C, TransInfo *t)
2151 {
2152         t->context = C;
2153
2154         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2155                 selectConstraint(t);
2156                 if (t->transform) {
2157                         t->transform(t, t->mval);  // calls recalcData()
2158                         viewRedrawForce(C, t);
2159                 }
2160                 t->redraw = TREDRAW_NOTHING;
2161         }
2162         else if (t->redraw & TREDRAW_SOFT) {
2163                 viewRedrawForce(C, t);
2164         }
2165
2166         /* If auto confirm is on, break after one pass */
2167         if (t->options & CTX_AUTOCONFIRM) {
2168                 t->state = TRANS_CONFIRM;
2169         }
2170
2171         t->context = NULL;
2172 }
2173
2174 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2175 {
2176         TransInfo *t = arg;
2177
2178         if (t->redraw & TREDRAW_SOFT) {
2179                 t->redraw |= TREDRAW_HARD;
2180                 transformApply((bContext *)C, t);
2181         }
2182 }
2183
2184 int transformEnd(bContext *C, TransInfo *t)
2185 {
2186         int exit_code = OPERATOR_RUNNING_MODAL;
2187
2188         t->context = C;
2189
2190         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2191                 /* handle restoring objects */
2192                 if (t->state == TRANS_CANCEL) {
2193                         /* exception, edge slide transformed UVs too */
2194                         if (t->mode == TFM_EDGE_SLIDE)
2195                                 doEdgeSlide(t, 0.0f);
2196                         
2197                         exit_code = OPERATOR_CANCELLED;
2198                         restoreTransObjects(t); // calls recalcData()
2199                 }
2200                 else {
2201                         exit_code = OPERATOR_FINISHED;
2202                 }
2203
2204                 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
2205                 special_aftertrans_update(C, t);
2206
2207                 /* free data */
2208                 postTrans(C, t);
2209
2210                 /* send events out for redraws */
2211                 viewRedrawPost(C, t);
2212
2213                 /*  Undo as last, certainly after special_trans_update! */
2214
2215                 if (t->state == TRANS_CANCEL) {
2216 //                      if (t->undostr) ED_undo_push(C, t->undostr);
2217                 }
2218                 else {
2219 //                      if (t->undostr) ED_undo_push(C, t->undostr);
2220 //                      else ED_undo_push(C, transform_to_undostr(t));
2221                 }
2222                 t->undostr = NULL;
2223
2224                 viewRedrawForce(C, t);
2225         }
2226
2227         t->context = NULL;
2228
2229         return exit_code;
2230 }
2231
2232 /* ************************** TRANSFORM LOCKS **************************** */
2233
2234 static void protectedTransBits(short protectflag, float *vec)
2235 {
2236         if (protectflag & OB_LOCK_LOCX)
2237                 vec[0] = 0.0f;
2238         if (protectflag & OB_LOCK_LOCY)
2239                 vec[1] = 0.0f;
2240         if (protectflag & OB_LOCK_LOCZ)
2241                 vec[2] = 0.0f;
2242 }
2243
2244 static void protectedSizeBits(short protectflag, float *size)
2245 {
2246         if (protectflag & OB_LOCK_SCALEX)
2247                 size[0] = 1.0f;
2248         if (protectflag & OB_LOCK_SCALEY)
2249                 size[1] = 1.0f;
2250         if (protectflag & OB_LOCK_SCALEZ)
2251                 size[2] = 1.0f;
2252 }
2253
2254 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
2255 {
2256         if (protectflag & OB_LOCK_ROTX)
2257                 eul[0] = oldeul[0];
2258         if (protectflag & OB_LOCK_ROTY)
2259                 eul[1] = oldeul[1];
2260         if (protectflag & OB_LOCK_ROTZ)
2261                 eul[2] = oldeul[2];
2262 }
2263
2264
2265 /* this function only does the delta rotation */
2266 /* axis-angle is usually internally stored as quats... */
2267 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2268 {
2269         /* check that protection flags are set */
2270         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2271                 return;
2272         
2273         if (protectflag & OB_LOCK_ROT4D) {
2274                 /* axis-angle getting limited as 4D entities that they are... */
2275                 if (protectflag & OB_LOCK_ROTW)
2276                         *angle = oldAngle;
2277                 if (protectflag & OB_LOCK_ROTX)
2278                         axis[0] = oldAxis[0];
2279                 if (protectflag & OB_LOCK_ROTY)
2280                         axis[1] = oldAxis[1];
2281                 if (protectflag & OB_LOCK_ROTZ)
2282                         axis[2] = oldAxis[2];
2283         }
2284         else {
2285                 /* axis-angle get limited with euler... */
2286                 float eul[3], oldeul[3];
2287                 
2288                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2289                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2290                 
2291                 if (protectflag & OB_LOCK_ROTX)
2292                         eul[0] = oldeul[0];
2293                 if (protectflag & OB_LOCK_ROTY)
2294                         eul[1] = oldeul[1];
2295                 if (protectflag & OB_LOCK_ROTZ)
2296                         eul[2] = oldeul[2];
2297                 
2298                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2299                 
2300                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2301                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2302                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2303                         axis[1] = 1.0f;
2304                 }
2305         }
2306 }
2307
2308 /* this function only does the delta rotation */
2309 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
2310 {
2311         /* check that protection flags are set */
2312         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2313                 return;
2314         
2315         if (protectflag & OB_LOCK_ROT4D) {
2316                 /* quaternions getting limited as 4D entities that they are... */
2317                 if (protectflag & OB_LOCK_ROTW)
2318                         quat[0] = oldquat[0];
2319                 if (protectflag & OB_LOCK_ROTX)
2320                         quat[1] = oldquat[1];
2321                 if (protectflag & OB_LOCK_ROTY)
2322                         quat[2] = oldquat[2];
2323                 if (protectflag & OB_LOCK_ROTZ)
2324                         quat[3] = oldquat[3];
2325         }
2326         else {
2327                 /* quaternions get limited with euler... (compatibility mode) */
2328                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2329                 float qlen;
2330
2331                 qlen = normalize_qt_qt(nquat, quat);
2332                 normalize_qt_qt(noldquat, oldquat);
2333
2334                 quat_to_eul(eul, nquat);
2335                 quat_to_eul(oldeul, noldquat);
2336
2337                 if (protectflag & OB_LOCK_ROTX)
2338                         eul[0] = oldeul[0];
2339                 if (protectflag & OB_LOCK_ROTY)
2340                         eul[1] = oldeul[1];
2341                 if (protectflag & OB_LOCK_ROTZ)
2342                         eul[2] = oldeul[2];
2343
2344                 eul_to_quat(quat, eul);
2345
2346                 /* restore original quat size */
2347                 mul_qt_fl(quat, qlen);
2348                 
2349                 /* quaternions flip w sign to accumulate rotations correctly */
2350                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2351                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2352                 {
2353                         mul_qt_fl(quat, -1.0f);
2354                 }
2355         }
2356 }
2357
2358 /* ******************* TRANSFORM LIMITS ********************** */
2359
2360 static void constraintTransLim(TransInfo *t, TransData *td)
2361 {
2362         if (td->con) {
2363                 bConstraintTypeInfo *ctiLoc = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
2364                 bConstraintTypeInfo *ctiDist = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
2365                 
2366                 bConstraintOb cob = {NULL};
2367                 bConstraint *con;
2368                 float ctime = (float)(t->scene->r.cfra);
2369                 
2370                 /* Make a temporary bConstraintOb for using these limit constraints
2371                  *  - they only care that cob->matrix is correctly set ;-)
2372                  *      - current space should be local
2373                  */
2374                 unit_m4(cob.matrix);
2375                 copy_v3_v3(cob.matrix[3], td->loc);
2376                 
2377                 /* Evaluate valid constraints */
2378                 for (con = td->con; con; con = con->next) {
2379                         bConstraintTypeInfo *cti = NULL;
2380                         ListBase targets = {NULL, NULL};
2381                         
2382                         /* only consider constraint if enabled */
2383                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2384                         if (con->enforce == 0.0f) continue;
2385                         
2386                         /* only use it if it's tagged for this purpose (and the right type) */
2387                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2388                                 bLocLimitConstraint *data = con->data;
2389                                 
2390                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2391                                         continue;
2392                                 cti = ctiLoc;
2393                         }
2394                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2395                                 bDistLimitConstraint *data = con->data;
2396                                 
2397                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2398                                         continue;
2399                                 cti = ctiDist;
2400                         }
2401                         
2402                         if (cti) {
2403                                 /* do space conversions */
2404                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2405                                         /* just multiply by td->mtx (this should be ok) */
2406                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2407                                 }
2408                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2409                                         /* skip... incompatable spacetype */
2410                                         continue;
2411                                 }
2412                                 
2413                                 /* get constraint targets if needed */
2414                                 BKE_get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2415                                 
2416                                 /* do constraint */
2417                                 cti->evaluate_constraint(con, &cob, &targets);
2418                                 
2419                                 /* convert spaces again */
2420                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2421                                         /* just multiply by td->smtx (this should be ok) */
2422                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2423                                 }
2424                                 
2425                                 /* free targets list */
2426                                 BLI_freelistN(&targets);
2427                         }
2428                 }
2429                 
2430                 /* copy results from cob->matrix */
2431                 copy_v3_v3(td->loc, cob.matrix[3]);
2432         }
2433 }
2434
2435 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2436 {
2437         /* Make a temporary bConstraintOb for use by limit constraints
2438          *  - they only care that cob->matrix is correctly set ;-)
2439          *      - current space should be local
2440          */
2441         memset(cob, 0, sizeof(bConstraintOb));
2442         if (td->ext) {
2443                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2444                         /* quats */
2445                         /* objects and bones do normalization first too, otherwise
2446                          * we don't necessarily end up with a rotation matrix, and
2447                          * then conversion back to quat gives a different result */
2448                         float quat[4];
2449                         normalize_qt_qt(quat, td->ext->quat);
2450                         quat_to_mat4(cob->matrix, quat);
2451                 }
2452                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2453                         /* axis angle */
2454                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2455                 }
2456                 else {
2457                         /* eulers */
2458                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2459                 }
2460         }
2461 }
2462
2463 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2464 {
2465         if (td->con) {
2466                 bConstraintTypeInfo *cti = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2467                 bConstraintOb cob;
2468                 bConstraint *con;
2469                 int do_limit = FALSE;
2470
2471                 /* Evaluate valid constraints */
2472                 for (con = td->con; con; con = con->next) {
2473                         /* only consider constraint if enabled */
2474                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2475                         if (con->enforce == 0.0f) continue;
2476
2477                         /* we're only interested in Limit-Rotation constraints */
2478                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2479                                 bRotLimitConstraint *data = con->data;
2480
2481                                 /* only use it if it's tagged for this purpose */
2482                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2483                                         continue;
2484
2485                                 /* skip incompatable spacetypes */
2486                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2487                                         continue;
2488
2489                                 /* only do conversion if necessary, to preserve quats and eulers */
2490                                 if (do_limit == FALSE) {
2491                                         constraintob_from_transdata(&cob, td);
2492                                         do_limit = TRUE;
2493                                 }
2494
2495                                 /* do space conversions */
2496                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2497                                         /* just multiply by td->mtx (this should be ok) */
2498                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2499                                 }
2500                                 
2501                                 /* do constraint */
2502                                 cti->evaluate_constraint(con, &cob, NULL);
2503                                 
2504                                 /* convert spaces again */
2505                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2506                                         /* just multiply by td->smtx (this should be ok) */
2507                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2508                                 }
2509                         }
2510                 }
2511                 
2512                 if (do_limit) {
2513                         /* copy results from cob->matrix */
2514                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2515                                 /* quats */
2516                                 mat4_to_quat(td->ext->quat, cob.matrix);
2517                         }
2518                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2519                                 /* axis angle */
2520                                 mat4_to_axis_angle(&td->ext->quat[1], &td->ext->quat[0], cob.matrix);
2521                         }
2522                         else {
2523                                 /* eulers */
2524                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2525                         }
2526                 }
2527         }
2528 }
2529
2530 static void constraintSizeLim(TransInfo *t, TransData *td)
2531 {
2532         if (td->con && td->ext) {
2533                 bConstraintTypeInfo *cti = BKE_get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2534                 bConstraintOb cob = {NULL};
2535                 bConstraint *con;
2536                 float size_sign[3], size_abs[3];
2537                 int i;
2538                 
2539                 /* Make a temporary bConstraintOb for using these limit constraints
2540                  *  - they only care that cob->matrix is correctly set ;-)
2541                  *      - current space should be local
2542                  */
2543                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2544                         /* scale val and reset size */
2545                         return; // TODO: fix this case
2546                 }
2547                 else {
2548                         /* Reset val if SINGLESIZE but using a constraint */
2549                         if (td->flag & TD_SINGLESIZE)
2550                                 return;
2551
2552                         /* separate out sign to apply back later */
2553                         for (i = 0; i < 3; i++) {
2554                                 size_sign[i] = signf(td->ext->size[i]);
2555                                 size_abs[i] = fabsf(td->ext->size[i]);
2556                         }
2557                         
2558                         size_to_mat4(cob.matrix, size_abs);
2559                 }
2560                 
2561                 /* Evaluate valid constraints */
2562                 for (con = td->con; con; con = con->next) {
2563                         /* only consider constraint if enabled */
2564                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2565                         if (con->enforce == 0.0f) continue;
2566                         
2567                         /* we're only interested in Limit-Scale constraints */
2568                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2569                                 bSizeLimitConstraint *data = con->data;
2570                                 
2571                                 /* only use it if it's tagged for this purpose */
2572                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2573                                         continue;
2574                                 
2575                                 /* do space conversions */
2576                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2577                                         /* just multiply by td->mtx (this should be ok) */
2578                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2579                                 }
2580                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2581                                         /* skip... incompatible spacetype */
2582                                         continue;
2583                                 }
2584                                 
2585                                 /* do constraint */
2586                                 cti->evaluate_constraint(con, &cob, NULL);
2587                                 
2588                                 /* convert spaces again */
2589                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2590                                         /* just multiply by td->smtx (this should be ok) */
2591                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2592                                 }
2593                         }
2594                 }
2595
2596                 /* copy results from cob->matrix */
2597                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2598                         /* scale val and reset size */
2599                         return; // TODO: fix this case
2600                 }
2601                 else {
2602                         /* Reset val if SINGLESIZE but using a constraint */
2603                         if (td->flag & TD_SINGLESIZE)
2604                                 return;
2605
2606                         /* extrace scale from matrix and apply back sign */
2607                         mat4_to_size(td->ext->size, cob.matrix);
2608                         mul_v3_v3(td->ext->size, size_sign);
2609                 }
2610         }
2611 }
2612
2613 /* ************************** WARP *************************** */
2614
2615 static void postInputWarp(TransInfo *t, float values[3])
2616 {
2617         mul_v3_fl(values, (float)(M_PI * 2));
2618
2619         if (t->customData) { /* non-null value indicates reversed input */
2620                 negate_v3(values);
2621         }
2622 }
2623
2624 void initWarp(TransInfo *t)
2625 {
2626         float max[3], min[3];
2627         int i;
2628         
2629         t->mode = TFM_WARP;
2630         t->transform = Warp;
2631         t->handleEvent = handleEventWarp;
2632         
2633         setInputPostFct(&t->mouse, postInputWarp);
2634         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2635         
2636         t->idx_max = 0;
2637         t->num.idx_max = 0;
2638         t->snap[0] = 0.0f;
2639         t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2640         t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2641         
2642         t->num.increment = 1.0f;
2643
2644         t->flag |= T_NO_CONSTRAINT;
2645         
2646         /* we need min/max in view space */
2647         for (i = 0; i < t->total; i++) {
2648                 float center[3];
2649                 copy_v3_v3(center, t->data[i].center);
2650                 mul_m3_v3(t->data[i].mtx, center);
2651                 mul_m4_v3(t->viewmat, center);
2652                 sub_v3_v3(center, t->viewmat[3]);
2653                 if (i) {
2654                         minmax_v3v3_v3(min, max, center);
2655                 }
2656                 else {
2657                         copy_v3_v3(max, center);
2658                         copy_v3_v3(min, center);
2659                 }
2660         }
2661
2662         mid_v3_v3v3(t->center, min, max);
2663
2664         if (max[0] == min[0])
2665                 max[0] += 0.1f;  /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2666         t->val = (max[0] - min[0]) / 2.0f; /* t->val is X dimension projected boundbox */
2667 }
2668
2669 int handleEventWarp(TransInfo *t, const wmEvent *event)
2670 {
2671         int status = 0;
2672         
2673         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2674                 // Use customData pointer to signal warp direction
2675                 if (t->customData == NULL)
2676                         t->customData = (void *)1;
2677                 else
2678                         t->customData = NULL;
2679                 
2680                 status = 1;
2681         }
2682         
2683         return status;
2684 }
2685
2686 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2687 {
2688         TransData *td = t->data;
2689         float vec[3], circumfac, dist, phi0, co, si, cursor[3], gcursor[3];
2690         const float *curs;
2691         int i;
2692         char str[MAX_INFO_LEN];
2693         
2694         curs = give_cursor(t->scene, t->view);
2695         /*
2696          * gcursor is the one used for helpline.
2697          * It has to be in the same space as the drawing loop
2698          * (that means it needs to be in the object's space when in edit mode and
2699          *  in global space in object mode)
2700          *
2701          * cursor is used for calculations.
2702          * It needs to be in view space, but we need to take object's offset
2703          * into account if in Edit mode.
2704          */
2705         copy_v3_v3(cursor, curs);
2706         copy_v3_v3(gcursor, cursor);
2707         if (t->flag & T_EDIT) {
2708                 sub_v3_v3(cursor, t->obedit->obmat[3]);
2709                 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2710                 mul_m3_v3(t->data->smtx, gcursor);
2711         }
2712         mul_m4_v3(t->viewmat, cursor);
2713         sub_v3_v3(cursor, t->viewmat[3]);
2714         
2715         /* amount of radians for warp */
2716         circumfac = t->values[0];
2717         
2718         snapGrid(t, &circumfac);
2719         applyNumInput(&t->num, &circumfac);
2720         
2721         /* header print for NumInput */
2722         if (hasNumInput(&t->num)) {
2723                 char c[NUM_STR_REP_LEN];
2724                 
2725                 outputNumInput(&(t->num), c);
2726                 
2727                 BLI_snprintf(str, MAX_INFO_LEN, IFACE_("Warp: %s"), c);
2728
2729                 circumfac = DEG2RADF(circumfac);
2730         }
2731         else {
2732                 /* default header print */
2733                 BLI_snprintf(str, MAX_INFO_LEN, IFACE_("Warp: %.3f"), RAD2DEGF(circumfac));
2734         }
2735         
2736         t->values[0] = circumfac;
2737
2738         circumfac /= 2; /* only need 180 on each side to make 360 */
2739         
2740         for (i = 0; i < t->total; i++, td++) {
2741                 float loc[3];
2742                 if (td->flag & TD_NOACTION)
2743                         break;
2744                 
2745                 if (td->flag & TD_SKIP)
2746                         continue;
2747                 
2748                 /* translate point to center, rotate in such a way that outline==distance */
2749                 copy_v3_v3(vec, td->iloc);
2750                 mul_m3_v3(td->mtx, vec);
2751                 mul_m4_v3(t->viewmat, vec);
2752                 sub_v3_v3(vec, t->viewmat[3]);
2753                 
2754                 dist = vec[0] - cursor[0];
2755                 
2756                 /* t->val is X dimension projected boundbox */
2757                 phi0 = (circumfac * dist / t->val);
2758                 
2759                 vec[1] = (vec[1] - cursor[1]);
2760                 
2761                 co = cosf(phi0);
2762                 si = sinf(phi0);
2763                 loc[0] = -si * vec[1] + cursor[0];
2764                 loc[1] = co * vec[1] + cursor[1];
2765                 loc[2] = vec[2];
2766                 
2767                 mul_m4_v3(t->viewinv, loc);
2768                 sub_v3_v3(loc, t->viewinv[3]);
2769                 mul_m3_v3(td->smtx, loc);
2770                 
2771                 sub_v3_v3(loc, td->iloc);
2772                 mul_v3_fl(loc, td->factor);
2773                 add_v3_v3v3(td->loc, td->iloc, loc);
2774         }
2775         
2776         recalcData(t);
2777         
2778         ED_area_headerprint(t->sa, str);
2779         
2780         return 1;
2781 }
2782
2783 /* ************************** SHEAR *************************** */
2784
2785 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2786 {
2787         mul_v3_fl(values, 0.05f);
2788 }
2789
2790 void initShear(TransInfo *t)
2791 {
2792         t->mode = TFM_SHEAR;
2793         t->transform = Shear;
2794         t->handleEvent = handleEventShear;
2795         
2796         setInputPostFct(&t->mouse, postInputShear);
2797         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2798         
2799         t->idx_max = 0;
2800         t->num.idx_max = 0;
2801         t->snap[0] = 0.0f;
2802         t->snap[1] = 0.1f;
2803         t->snap[2] = t->snap[1] * 0.1f;
2804         
2805         t->num.increment = 0.1f;
2806
2807         t->flag |= T_NO_CONSTRAINT;
2808 }
2809
2810 int handleEventShear(TransInfo *t, const wmEvent *event)
2811 {
2812         int status = 0;
2813         
2814         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2815                 // Use customData pointer to signal Shear direction
2816                 if (t->customData == NULL) {
2817                         initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2818                         t->customData = (void *)1;
2819                 }
2820                 else {
2821                         initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2822                         t->customData = NULL;
2823                 }
2824
2825                 status = 1;
2826         }
2827         else if (event->type == XKEY && event->val == KM_PRESS) {
2828                 initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2829                 t->customData = NULL;
2830                 
2831                 status = 1;
2832         }
2833         else if (event->type == YKEY && event->val == KM_PRESS) {
2834                 initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2835                 t->customData = (void *)1;
2836                 
2837                 status = 1;
2838         }
2839         
2840         return status;
2841 }
2842
2843
2844 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2845 {
2846         TransData *td = t->data;
2847         float vec[3];
2848         float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2849         float value;
2850         int i;
2851         char str[MAX_INFO_LEN];
2852         
2853         copy_m3_m4(persmat, t->viewmat);
2854         invert_m3_m3(persinv, persmat);
2855         
2856         value = t->values[0];
2857         
2858         snapGrid(t, &value);
2859         
2860         applyNumInput(&t->num, &value);
2861         
2862         /* header print for NumInput */
2863         if (hasNumInput(&t->num)) {
2864                 char c[NUM_STR_REP_LEN];
2865                 
2866                 outputNumInput(&(t->num), c);
2867                 
2868                 BLI_snprintf(str, MAX_INFO_LEN, IFACE_("Shear: %s %s"), c, t->proptext);
2869         }
2870         else {
2871                 /* default header print */
2872                 BLI_snprintf(str, MAX_INFO_LEN, IFACE_("Shear: %.3f %s (Press X or Y to set shear axis)"), value, t->proptext);
2873         }
2874         
2875         t->values[0] = value;
2876
2877         unit_m3(smat);
2878         
2879         // Custom data signals shear direction
2880         if (t->customData == NULL)
2881                 smat[1][0] = value;
2882         else
2883                 smat[0][1] = value;
2884         
2885         mul_m3_m3m3(tmat, smat, persmat);