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