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