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