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