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