2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/transform/transform.c
29 * \ingroup edtransform
44 #include "MEM_guardedalloc.h"
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 */
55 #include "RNA_access.h"
58 #include "BIF_glutil.h"
63 #include "BKE_bmesh.h"
64 #include "BKE_context.h"
65 #include "BKE_constraint.h"
66 #include "BKE_global.h"
67 #include "BKE_particle.h"
68 #include "BKE_pointcache.h"
73 #include "ED_keyframing.h"
74 #include "ED_screen.h"
75 #include "ED_space_api.h"
76 #include "ED_markers.h"
77 #include "ED_view3d.h"
82 #include "UI_view2d.h"
87 #include "BLI_blenlib.h"
88 #include "BLI_utildefines.h"
89 #include "BLI_ghash.h"
90 #include "BLI_linklist.h"
91 #include "BLI_smallhash.h"
92 #include "BLI_array.h"
94 #include "UI_interface_icons.h"
95 #include "UI_resources.h"
97 #include "transform.h"
99 static void drawTransformApply(const struct bContext *C, ARegion *ar, void *arg);
100 static int doEdgeSlide(TransInfo *t, float perc);
102 /* ************************** SPACE DEPENDANT CODE **************************** */
104 void setTransformViewMatrices(TransInfo *t)
106 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
107 RegionView3D *rv3d = t->ar->regiondata;
109 copy_m4_m4(t->viewmat, rv3d->viewmat);
110 copy_m4_m4(t->viewinv, rv3d->viewinv);
111 copy_m4_m4(t->persmat, rv3d->persmat);
112 copy_m4_m4(t->persinv, rv3d->persinv);
113 t->persp = rv3d->persp;
120 t->persp = RV3D_ORTHO;
123 calculateCenter2D(t);
126 static void convertViewVec2D(View2D *v2d, float r_vec[3], int dx, int dy)
130 divx = BLI_rcti_size_x(&v2d->mask);
131 divy = BLI_rcti_size_y(&v2d->mask);
133 r_vec[0] = BLI_rctf_size_x(&v2d->cur) * dx / divx;
134 r_vec[1] = BLI_rctf_size_y(&v2d->cur) * dy / divy;
138 static void convertViewVec2D_mask(View2D *v2d, float r_vec[3], int dx, int dy)
143 divx = BLI_rcti_size_x(&v2d->mask);
144 divy = BLI_rcti_size_y(&v2d->mask);
146 mulx = BLI_rctf_size_x(&v2d->cur);
147 muly = BLI_rctf_size_y(&v2d->cur);
149 /* difference with convertViewVec2D */
150 /* clamp w/h, mask only */
151 if (mulx / divx < muly / divy) {
161 r_vec[0] = mulx * dx / divx;
162 r_vec[1] = muly * dy / divy;
166 void convertViewVec(TransInfo *t, float r_vec[3], int dx, int dy)
168 if ((t->spacetype == SPACE_VIEW3D) && (t->ar->regiontype == RGN_TYPE_WINDOW)) {
169 const float mval_f[2] = {(float)dx, (float)dy};
170 ED_view3d_win_to_delta(t->ar, mval_f, r_vec);
172 else if (t->spacetype == SPACE_IMAGE) {
175 if (t->options & CTX_MASK) {
177 convertViewVec2D_mask(t->view, r_vec, dx, dy);
178 ED_space_image_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
181 convertViewVec2D(t->view, r_vec, dx, dy);
182 ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
188 else if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
189 convertViewVec2D(t->view, r_vec, dx, dy);
191 else if (ELEM(t->spacetype, SPACE_NODE, SPACE_SEQ)) {
192 convertViewVec2D(&t->ar->v2d, r_vec, dx, dy);
194 else if (t->spacetype == SPACE_CLIP) {
197 if (t->options & CTX_MASK) {
198 convertViewVec2D_mask(t->view, r_vec, dx, dy);
201 convertViewVec2D(t->view, r_vec, dx, dy);
204 if (t->options & CTX_MOVIECLIP) {
205 ED_space_clip_get_aspect_dimension_aware(t->sa->spacedata.first, &aspx, &aspy);
207 else if (t->options & CTX_MASK) {
208 /* TODO - NOT WORKING, this isnt so bad since its only display aspect */
209 ED_space_clip_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
212 /* should never happen, quiet warnings */
221 printf("%s: called in an invalid context\n", __func__);
226 void projectIntView(TransInfo *t, const float vec[3], int adr[2])
228 if (t->spacetype == SPACE_VIEW3D) {
229 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
230 if (ED_view3d_project_int_global(t->ar, vec, adr, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
231 adr[0] = (int)2140000000.0f; /* this is what was done in 2.64, perhaps we can be smarter? */
232 adr[1] = (int)2140000000.0f;
236 else if (t->spacetype == SPACE_IMAGE) {
237 SpaceImage *sima = t->sa->spacedata.first;
239 if (t->options & CTX_MASK) {
240 /* not working quite right, TODO (see below too) */
244 ED_space_image_get_aspect(sima, &aspx, &aspy);
251 BKE_mask_coord_to_image(sima->image, &sima->iuser, v, v);
256 ED_image_point_pos__reverse(sima, t->ar, v, v);
262 float aspx, aspy, v[2];
264 ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
265 v[0] = vec[0] / aspx;
266 v[1] = vec[1] / aspy;
268 UI_view2d_to_region_no_clip(t->view, v[0], v[1], adr, adr + 1);
271 else if (t->spacetype == SPACE_ACTION) {
274 SpaceAction *sact = t->sa->spacedata.first;
276 if (sact->flag & SACTION_DRAWTIME) {
277 //vec[0] = vec[0]/((t->scene->r.frs_sec / t->scene->r.frs_sec_base));
279 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out + 1);
284 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out + 1);
290 else if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
293 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out + 1);
297 else if (t->spacetype == SPACE_SEQ) { /* XXX not tested yet, but should work */
300 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], out, out + 1);
304 else if (t->spacetype == SPACE_CLIP) {
305 SpaceClip *sc = t->sa->spacedata.first;
307 if (t->options & CTX_MASK) {
308 /* not working quite right, TODO (see above too) */
312 ED_space_clip_get_aspect(sc, &aspx, &aspy);
319 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, v, v);
324 ED_clip_point_stable_pos__reverse(sc, t->ar, v, v);
329 else if (t->options & CTX_MOVIECLIP) {
330 float v[2], aspx, aspy;
333 ED_space_clip_get_aspect_dimension_aware(t->sa->spacedata.first, &aspx, &aspy);
338 UI_view2d_to_region_no_clip(t->view, v[0], v[1], adr, adr + 1);
344 else if (t->spacetype == SPACE_NODE) {
345 UI_view2d_to_region_no_clip((View2D *)t->view, vec[0], vec[1], adr, adr + 1);
349 void projectFloatView(TransInfo *t, const float vec[3], float adr[2])
351 switch (t->spacetype) {
354 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
355 if (ED_view3d_project_float_global(t->ar, vec, adr, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
356 /* XXX, 2.64 and prior did this, weak! */
357 adr[0] = t->ar->winx / 2.0f;
358 adr[1] = t->ar->winy / 2.0f;
370 projectIntView(t, vec, a);
380 void applyAspectRatio(TransInfo *t, float vec[2])
382 if ((t->spacetype == SPACE_IMAGE) && (t->mode == TFM_TRANSLATION)) {
383 SpaceImage *sima = t->sa->spacedata.first;
386 if ((sima->flag & SI_COORDFLOATS) == 0) {
388 ED_space_image_get_size(sima, &width, &height);
394 ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
398 else if ((t->spacetype == SPACE_CLIP) && (t->mode == TFM_TRANSLATION)) {
399 if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
400 SpaceClip *sc = t->sa->spacedata.first;
404 if (t->options & CTX_MOVIECLIP) {
405 ED_space_clip_get_aspect_dimension_aware(sc, &aspx, &aspy);
410 else if (t->options & CTX_MASK) {
411 ED_space_clip_get_aspect(sc, &aspx, &aspy);
420 void removeAspectRatio(TransInfo *t, float vec[2])
422 if ((t->spacetype == SPACE_IMAGE) && (t->mode == TFM_TRANSLATION)) {
423 SpaceImage *sima = t->sa->spacedata.first;
426 if ((sima->flag & SI_COORDFLOATS) == 0) {
428 ED_space_image_get_size(sima, &width, &height);
434 ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
438 else if ((t->spacetype == SPACE_CLIP) && (t->mode == TFM_TRANSLATION)) {
439 if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
440 SpaceClip *sc = t->sa->spacedata.first;
441 float aspx = 1.0f, aspy = 1.0f;
443 if (t->options & CTX_MOVIECLIP) {
444 ED_space_clip_get_aspect_dimension_aware(sc, &aspx, &aspy);
446 else if (t->options & CTX_MASK) {
447 ED_space_clip_get_aspect(sc, &aspx, &aspy);
456 static void viewRedrawForce(const bContext *C, TransInfo *t)
458 if (t->spacetype == SPACE_VIEW3D) {
459 /* Do we need more refined tags? */
460 if (t->flag & T_POSE)
461 WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
463 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
465 /* for realtime animation record - send notifiers recognised by animation editors */
466 // XXX: is this notifier a lame duck?
467 if ((t->animtimer) && IS_AUTOKEY_ON(t->scene))
468 WM_event_add_notifier(C, NC_OBJECT | ND_KEYS, NULL);
471 else if (t->spacetype == SPACE_ACTION) {
472 //SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
473 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
475 else if (t->spacetype == SPACE_IPO) {
476 //SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
477 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
479 else if (t->spacetype == SPACE_NLA) {
480 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
482 else if (t->spacetype == SPACE_NODE) {
483 //ED_area_tag_redraw(t->sa);
484 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_NODE_VIEW, NULL);
486 else if (t->spacetype == SPACE_SEQ) {
487 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, NULL);
489 else if (t->spacetype == SPACE_IMAGE) {
490 if (t->options & CTX_MASK) {
491 Mask *mask = CTX_data_edit_mask(C);
493 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
496 // XXX how to deal with lock?
497 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
498 if (sima->lock) WM_event_add_notifier(C, NC_GEOM | ND_DATA, t->obedit->data);
499 else ED_area_tag_redraw(t->sa);
502 else if (t->spacetype == SPACE_CLIP) {
503 SpaceClip *sc = (SpaceClip *)t->sa->spacedata.first;
505 if (ED_space_clip_check_show_trackedit(sc)) {
506 MovieClip *clip = ED_space_clip_get_clip(sc);
508 /* objects could be parented to tracking data, so send this for viewport refresh */
509 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
511 WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
513 else if (ED_space_clip_check_show_maskedit(sc)) {
514 Mask *mask = CTX_data_edit_mask(C);
516 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
521 static void viewRedrawPost(bContext *C, TransInfo *t)
523 ED_area_headerprint(t->sa, NULL);
525 if (t->spacetype == SPACE_VIEW3D) {
526 /* if autokeying is enabled, send notifiers that keyframes were added */
527 if (IS_AUTOKEY_ON(t->scene))
528 WM_main_add_notifier(NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
530 /* redraw UV editor */
531 if (t->mode == TFM_EDGE_SLIDE && (t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT))
532 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
534 /* XXX temp, first hack to get auto-render in compositor work (ton) */
535 WM_event_add_notifier(C, NC_SCENE | ND_TRANSFORM_DONE, CTX_data_scene(C));
539 #if 0 // TRANSFORM_FIX_ME
540 if (t->spacetype == SPACE_VIEW3D) {
541 allqueue(REDRAWBUTSOBJECT, 0);
542 allqueue(REDRAWVIEW3D, 0);
544 else if (t->spacetype == SPACE_IMAGE) {
545 allqueue(REDRAWIMAGE, 0);
546 allqueue(REDRAWVIEW3D, 0);
548 else if (ELEM3(t->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_IPO)) {
549 allqueue(REDRAWVIEW3D, 0);
550 allqueue(REDRAWACTION, 0);
551 allqueue(REDRAWNLA, 0);
552 allqueue(REDRAWIPO, 0);
553 allqueue(REDRAWTIME, 0);
554 allqueue(REDRAWBUTSOBJECT, 0);
557 scrarea_queue_headredraw(curarea);
561 /* ************************** TRANSFORMATIONS **************************** */
563 void BIF_selectOrientation(void)
565 #if 0 // TRANSFORM_FIX_ME
567 char *str_menu = BIF_menustringTransformOrientation("Orientation");
568 val = pupmenu(str_menu);
577 static void view_editmove(unsigned short UNUSED(event))
579 #if 0 // TRANSFORM_FIX_ME
581 /* Regular: Zoom in */
582 /* Shift: Scroll up */
583 /* Ctrl: Scroll right */
584 /* Alt-Shift: Rotate up */
585 /* Alt-Ctrl: Rotate right */
587 /* only work in 3D window for now
588 * In the end, will have to send to event to a 2D window handler instead
590 if (Trans.flag & T_2D_EDIT)
596 if (G.qual & LR_SHIFTKEY) {
597 if (G.qual & LR_ALTKEY) {
598 G.qual &= ~LR_SHIFTKEY;
600 G.qual |= LR_SHIFTKEY;
606 else if (G.qual & LR_CTRLKEY) {
607 if (G.qual & LR_ALTKEY) {
608 G.qual &= ~LR_CTRLKEY;
610 G.qual |= LR_CTRLKEY;
616 else if (U.uiflag & USER_WHEELZOOMDIR)
617 persptoetsen(PADMINUS);
619 persptoetsen(PADPLUSKEY);
624 if (G.qual & LR_SHIFTKEY) {
625 if (G.qual & LR_ALTKEY) {
626 G.qual &= ~LR_SHIFTKEY;
628 G.qual |= LR_SHIFTKEY;
634 else if (G.qual & LR_CTRLKEY) {
635 if (G.qual & LR_ALTKEY) {
636 G.qual &= ~LR_CTRLKEY;
638 G.qual |= LR_CTRLKEY;
644 else if (U.uiflag & USER_WHEELZOOMDIR)
645 persptoetsen(PADPLUSKEY);
647 persptoetsen(PADMINUS);
654 setTransformViewMatrices(&Trans);
658 /* ************************************************* */
660 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
661 #define TFM_MODAL_CANCEL 1
662 #define TFM_MODAL_CONFIRM 2
663 #define TFM_MODAL_TRANSLATE 3
664 #define TFM_MODAL_ROTATE 4
665 #define TFM_MODAL_RESIZE 5
666 #define TFM_MODAL_SNAP_INV_ON 6
667 #define TFM_MODAL_SNAP_INV_OFF 7
668 #define TFM_MODAL_SNAP_TOGGLE 8
669 #define TFM_MODAL_AXIS_X 9
670 #define TFM_MODAL_AXIS_Y 10
671 #define TFM_MODAL_AXIS_Z 11
672 #define TFM_MODAL_PLANE_X 12
673 #define TFM_MODAL_PLANE_Y 13
674 #define TFM_MODAL_PLANE_Z 14
675 #define TFM_MODAL_CONS_OFF 15
676 #define TFM_MODAL_ADD_SNAP 16
677 #define TFM_MODAL_REMOVE_SNAP 17
678 /* 18 and 19 used by numinput, defined in transform.h
680 #define TFM_MODAL_PROPSIZE_UP 20
681 #define TFM_MODAL_PROPSIZE_DOWN 21
682 #define TFM_MODAL_AUTOIK_LEN_INC 22
683 #define TFM_MODAL_AUTOIK_LEN_DEC 23
685 #define TFM_MODAL_EDGESLIDE_UP 24
686 #define TFM_MODAL_EDGESLIDE_DOWN 25
688 /* called in transform_ops.c, on each regeneration of keymaps */
689 wmKeyMap *transform_modal_keymap(wmKeyConfig *keyconf)
691 static EnumPropertyItem modal_items[] = {
692 {TFM_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
693 {TFM_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
694 {TFM_MODAL_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
695 {TFM_MODAL_ROTATE, "ROTATE", 0, "Rotate", ""},
696 {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
697 {TFM_MODAL_SNAP_INV_ON, "SNAP_INV_ON", 0, "Invert Snap On", ""},
698 {TFM_MODAL_SNAP_INV_OFF, "SNAP_INV_OFF", 0, "Invert Snap Off", ""},
699 {TFM_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap Toggle", ""},
700 {TFM_MODAL_AXIS_X, "AXIS_X", 0, "Orientation X axis", ""},
701 {TFM_MODAL_AXIS_Y, "AXIS_Y", 0, "Orientation Y axis", ""},
702 {TFM_MODAL_AXIS_Z, "AXIS_Z", 0, "Orientation Z axis", ""},
703 {TFM_MODAL_PLANE_X, "PLANE_X", 0, "Orientation X plane", ""},
704 {TFM_MODAL_PLANE_Y, "PLANE_Y", 0, "Orientation Y plane", ""},
705 {TFM_MODAL_PLANE_Z, "PLANE_Z", 0, "Orientation Z plane", ""},
706 {TFM_MODAL_CONS_OFF, "CONS_OFF", 0, "Remove Constraints", ""},
707 {TFM_MODAL_ADD_SNAP, "ADD_SNAP", 0, "Add Snap Point", ""},
708 {TFM_MODAL_REMOVE_SNAP, "REMOVE_SNAP", 0, "Remove Last Snap Point", ""},
709 {NUM_MODAL_INCREMENT_UP, "INCREMENT_UP", 0, "Numinput Increment Up", ""},
710 {NUM_MODAL_INCREMENT_DOWN, "INCREMENT_DOWN", 0, "Numinput Increment Down", ""},
711 {TFM_MODAL_PROPSIZE_UP, "PROPORTIONAL_SIZE_UP", 0, "Increase Proportional Influence", ""},
712 {TFM_MODAL_PROPSIZE_DOWN, "PROPORTIONAL_SIZE_DOWN", 0, "Decrease Proportional Influence", ""},
713 {TFM_MODAL_AUTOIK_LEN_INC, "AUTOIK_CHAIN_LEN_UP", 0, "Increase Max AutoIK Chain Length", ""},
714 {TFM_MODAL_AUTOIK_LEN_DEC, "AUTOIK_CHAIN_LEN_DOWN", 0, "Decrease Max AutoIK Chain Length", ""},
715 {TFM_MODAL_EDGESLIDE_UP, "EDGESLIDE_EDGE_NEXT", 0, "Select next Edge Slide Edge", ""},
716 {TFM_MODAL_EDGESLIDE_DOWN, "EDGESLIDE_PREV_NEXT", 0, "Select previous Edge Slide Edge", ""},
717 {0, NULL, 0, NULL, NULL}
720 wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "Transform Modal Map");
722 /* this function is called for each spacetype, only needs to add map once */
723 if (keymap && keymap->modal_items) return NULL;
725 keymap = WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
727 /* items for modal map */
728 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_CANCEL);
729 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
730 WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
731 WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
733 WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, TFM_MODAL_TRANSLATE);
734 WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, TFM_MODAL_ROTATE);
735 WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, TFM_MODAL_RESIZE);
737 WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_SNAP_TOGGLE);
739 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
740 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
742 WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
743 WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
745 WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, TFM_MODAL_ADD_SNAP);
746 WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ALT, 0, TFM_MODAL_REMOVE_SNAP);
748 WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
749 WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
750 WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
751 WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
753 WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_UP);
754 WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_DOWN);
756 WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
757 WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
758 WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
759 WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
764 static void transform_event_xyz_constraint(TransInfo *t, short key_type, char cmode)
766 if (!(t->flag & T_NO_CONSTRAINT)) {
767 int constraint_axis, constraint_plane;
768 int edit_2d = (t->flag & T_2D_EDIT);
769 char msg1[] = "along _";
770 char msg2[] = "along %s _";
771 char msg3[] = "locking %s _";
778 constraint_axis = CON_AXIS0;
782 constraint_axis = CON_AXIS1;
786 constraint_axis = CON_AXIS2;
792 msg1[sizeof(msg1) - 2] = axis;
793 msg2[sizeof(msg2) - 2] = axis;
794 msg3[sizeof(msg3) - 2] = axis;
795 constraint_plane = ((CON_AXIS0 | CON_AXIS1 | CON_AXIS2) & (~constraint_axis));
797 if (edit_2d && (key_type != ZKEY)) {
802 setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg1);
807 if (t->con.orientation != V3D_MANIP_GLOBAL) {
811 short orientation = (t->current_orientation != V3D_MANIP_GLOBAL ?
812 t->current_orientation : V3D_MANIP_LOCAL);
813 if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
814 setUserConstraint(t, orientation, constraint_axis, msg2);
815 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
816 setUserConstraint(t, orientation, constraint_plane, msg3);
820 if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
821 setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg2);
822 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
823 setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_plane, msg3);
826 t->redraw |= TREDRAW_HARD;
830 int transformEvent(TransInfo *t, wmEvent *event)
832 float mati[3][3] = MAT3_UNITY;
833 char cmode = constraintModeToChar(t);
836 t->redraw |= handleMouseInput(t, &t->mouse, event);
838 if (event->type == MOUSEMOVE) {
839 if (t->modifiers & MOD_CONSTRAINT_SELECT)
840 t->con.mode |= CON_SELECT;
842 copy_v2_v2_int(t->mval, event->mval);
844 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
845 t->redraw |= TREDRAW_HARD;
848 if (t->state == TRANS_STARTING) {
849 t->state = TRANS_RUNNING;
852 applyMouseInput(t, &t->mouse, t->mval, t->values);
854 // Snapping mouse move events
855 t->redraw |= handleSnapping(t, event);
858 /* handle modal keymap first */
859 if (event->type == EVT_MODAL_MAP) {
860 switch (event->val) {
861 case TFM_MODAL_CANCEL:
862 t->state = TRANS_CANCEL;
864 case TFM_MODAL_CONFIRM:
865 t->state = TRANS_CONFIRM;
867 case TFM_MODAL_TRANSLATE:
868 /* only switch when... */
869 if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
870 resetTransRestrictions(t);
871 restoreTransObjects(t);
873 initSnapping(t, NULL); // need to reinit after mode change
874 t->redraw |= TREDRAW_HARD;
876 else if (t->mode == TFM_TRANSLATION) {
877 if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
878 restoreTransObjects(t);
880 t->flag ^= T_ALT_TRANSFORM;
881 t->redraw |= TREDRAW_HARD;
885 case TFM_MODAL_ROTATE:
886 /* only switch when... */
887 if (!(t->options & CTX_TEXTURE) && !(t->options & (CTX_MOVIECLIP | CTX_MASK))) {
888 if (ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
890 resetTransRestrictions(t);
892 if (t->mode == TFM_ROTATION) {
893 restoreTransObjects(t);
897 restoreTransObjects(t);
900 initSnapping(t, NULL); // need to reinit after mode change
901 t->redraw |= TREDRAW_HARD;
905 case TFM_MODAL_RESIZE:
906 /* only switch when... */
907 if (ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
908 resetTransRestrictions(t);
909 restoreTransObjects(t);
911 initSnapping(t, NULL); // need to reinit after mode change
912 t->redraw |= TREDRAW_HARD;
914 else if (t->mode == TFM_RESIZE) {
915 if (t->options & CTX_MOVIECLIP) {
916 restoreTransObjects(t);
918 t->flag ^= T_ALT_TRANSFORM;
919 t->redraw |= TREDRAW_HARD;
924 case TFM_MODAL_SNAP_INV_ON:
925 t->modifiers |= MOD_SNAP_INVERT;
926 t->redraw |= TREDRAW_HARD;
928 case TFM_MODAL_SNAP_INV_OFF:
929 t->modifiers &= ~MOD_SNAP_INVERT;
930 t->redraw |= TREDRAW_HARD;
932 case TFM_MODAL_SNAP_TOGGLE:
933 t->modifiers ^= MOD_SNAP;
934 t->redraw |= TREDRAW_HARD;
936 case TFM_MODAL_AXIS_X:
937 if ((t->flag & T_NO_CONSTRAINT) == 0) {
942 if (t->flag & T_2D_EDIT) {
943 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), "along X");
946 setUserConstraint(t, t->current_orientation, (CON_AXIS0), "along %s X");
949 t->redraw |= TREDRAW_HARD;
952 case TFM_MODAL_AXIS_Y:
953 if ((t->flag & T_NO_CONSTRAINT) == 0) {
958 if (t->flag & T_2D_EDIT) {
959 setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), "along Y");
962 setUserConstraint(t, t->current_orientation, (CON_AXIS1), "along %s Y");
965 t->redraw |= TREDRAW_HARD;
968 case TFM_MODAL_AXIS_Z:
969 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
974 setUserConstraint(t, t->current_orientation, (CON_AXIS2), "along %s Z");
976 t->redraw |= TREDRAW_HARD;
979 case TFM_MODAL_PLANE_X:
980 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
985 setUserConstraint(t, t->current_orientation, (CON_AXIS1 | CON_AXIS2), "locking %s X");
987 t->redraw |= TREDRAW_HARD;
990 case TFM_MODAL_PLANE_Y:
991 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
996 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS2), "locking %s Y");
998 t->redraw |= TREDRAW_HARD;
1001 case TFM_MODAL_PLANE_Z:
1002 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1007 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS1), "locking %s Z");
1009 t->redraw |= TREDRAW_HARD;
1012 case TFM_MODAL_CONS_OFF:
1013 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1015 t->redraw |= TREDRAW_HARD;
1018 case TFM_MODAL_ADD_SNAP:
1020 t->redraw |= TREDRAW_HARD;
1022 case TFM_MODAL_REMOVE_SNAP:
1024 t->redraw |= TREDRAW_HARD;
1026 case TFM_MODAL_PROPSIZE_UP:
1027 if (t->flag & T_PROP_EDIT) {
1028 t->prop_size *= 1.1f;
1029 if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1030 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1031 calculatePropRatio(t);
1033 t->redraw |= TREDRAW_HARD;
1035 case TFM_MODAL_PROPSIZE_DOWN:
1036 if (t->flag & T_PROP_EDIT) {
1037 t->prop_size *= 0.90909090f;
1038 calculatePropRatio(t);
1040 t->redraw |= TREDRAW_HARD;
1042 case TFM_MODAL_EDGESLIDE_UP:
1043 case TFM_MODAL_EDGESLIDE_DOWN:
1044 t->redraw |= TREDRAW_HARD;
1046 case TFM_MODAL_AUTOIK_LEN_INC:
1047 if (t->flag & T_AUTOIK)
1048 transform_autoik_update(t, 1);
1049 t->redraw |= TREDRAW_HARD;
1051 case TFM_MODAL_AUTOIK_LEN_DEC:
1052 if (t->flag & T_AUTOIK)
1053 transform_autoik_update(t, -1);
1054 t->redraw |= TREDRAW_HARD;
1061 // Modal numinput events
1062 t->redraw |= handleNumInput(&(t->num), event);
1064 /* else do non-mapped events */
1065 else if (event->val == KM_PRESS) {
1066 switch (event->type) {
1068 t->state = TRANS_CANCEL;
1070 /* enforce redraw of transform when modifiers are used */
1073 t->modifiers |= MOD_CONSTRAINT_PLANE;
1074 t->redraw |= TREDRAW_HARD;
1078 if ((t->spacetype == SPACE_VIEW3D) && event->alt) {
1079 #if 0 // TRANSFORM_FIX_ME
1082 getmouseco_sc(mval);
1083 BIF_selectOrientation();
1084 calc_manipulator_stats(curarea);
1085 copy_m3_m4(t->spacemtx, G.vd->twmat);
1086 warp_pointer(mval[0], mval[1]);
1090 t->state = TRANS_CONFIRM;
1095 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1096 /* exception for switching to dolly, or trackball, in camera view */
1097 if (t->flag & T_CAMERA) {
1098 if (t->mode == TFM_TRANSLATION)
1099 setLocalConstraint(t, (CON_AXIS2), "along local Z");
1100 else if (t->mode == TFM_ROTATION) {
1101 restoreTransObjects(t);
1106 t->modifiers |= MOD_CONSTRAINT_SELECT;
1107 if (t->con.mode & CON_APPLY) {
1112 initSelectConstraint(t, t->spacemtx);
1115 /* bit hackish... but it prevents mmb select to print the orientation from menu */
1116 strcpy(t->spacename, "global");
1117 initSelectConstraint(t, mati);
1119 postSelectConstraint(t);
1122 t->redraw |= TREDRAW_HARD;
1126 t->state = TRANS_CANCEL;
1130 t->state = TRANS_CONFIRM;
1133 /* only switch when... */
1134 if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
1135 resetTransRestrictions(t);
1136 restoreTransObjects(t);
1138 initSnapping(t, NULL); // need to reinit after mode change
1139 t->redraw |= TREDRAW_HARD;
1143 /* only switch when... */
1144 if (ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
1145 resetTransRestrictions(t);
1146 restoreTransObjects(t);
1148 initSnapping(t, NULL); // need to reinit after mode change
1149 t->redraw |= TREDRAW_HARD;
1153 /* only switch when... */
1154 if (!(t->options & CTX_TEXTURE)) {
1155 if (ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
1157 resetTransRestrictions(t);
1159 if (t->mode == TFM_ROTATION) {
1160 restoreTransObjects(t);
1164 restoreTransObjects(t);
1167 initSnapping(t, NULL); // need to reinit after mode change
1168 t->redraw |= TREDRAW_HARD;
1174 t->flag ^= T_PROP_CONNECTED;
1175 sort_trans_data_dist(t);
1176 calculatePropRatio(t);
1181 t->redraw |= TREDRAW_HARD;
1187 transform_event_xyz_constraint(t, event->type, cmode);
1190 if (t->flag & T_PROP_EDIT && event->shift) {
1191 t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1192 calculatePropRatio(t);
1193 t->redraw |= TREDRAW_HARD;
1197 if (event->alt && t->flag & T_PROP_EDIT) {
1198 t->prop_size *= 1.1f;
1199 if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1200 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1201 calculatePropRatio(t);
1206 case WHEELDOWNMOUSE:
1207 if (t->flag & T_AUTOIK) {
1208 transform_autoik_update(t, 1);
1210 else view_editmove(event->type);
1214 if (event->alt && t->flag & T_PROP_EDIT) {
1215 t->prop_size *= 0.90909090f;
1216 calculatePropRatio(t);
1222 if (t->flag & T_AUTOIK) {
1223 transform_autoik_update(t, -1);
1225 else view_editmove(event->type);
1230 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1231 t->flag |= T_ALT_TRANSFORM;
1232 t->redraw |= TREDRAW_HARD;
1241 // Numerical input events
1242 t->redraw |= handleNumInput(&(t->num), event);
1244 // Snapping key events
1245 t->redraw |= handleSnapping(t, event);
1248 else if (event->val == KM_RELEASE) {
1249 switch (event->type) {
1252 t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1253 t->redraw |= TREDRAW_HARD;
1257 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1258 t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1259 postSelectConstraint(t);
1260 t->redraw |= TREDRAW_HARD;
1265 // if (WM_modal_tweak_exit(event, t->event_type))
1266 //// if (t->options & CTX_TWEAK)
1267 // t->state = TRANS_CONFIRM;
1271 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1272 t->flag &= ~T_ALT_TRANSFORM;
1273 t->redraw |= TREDRAW_HARD;
1282 /* confirm transform if launch key is released after mouse move */
1283 if (t->flag & T_RELEASE_CONFIRM) {
1284 /* XXX Keyrepeat bug in Xorg fucks this up, will test when fixed */
1285 if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1286 t->state = TRANS_CONFIRM;
1293 // Per transform event, if present
1295 t->redraw |= t->handleEvent(t, event);
1297 if (handled || t->redraw) {
1301 return OPERATOR_PASS_THROUGH;
1305 int calculateTransformCenter(bContext *C, int centerMode, float cent3d[3], int cent2d[2])
1307 TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1310 t->state = TRANS_RUNNING;
1312 t->options = CTX_NONE;
1314 t->mode = TFM_DUMMY;
1316 initTransInfo(C, t, NULL, NULL); // internal data, mouse, vectors
1318 createTransData(C, t); // make TransData structs from selection
1320 t->around = centerMode; // override userdefined mode
1322 if (t->total == 0) {
1331 copy_v2_v2_int(cent2d, t->center2d);
1335 // Copy center from constraint center. Transform center can be local
1336 copy_v3_v3(cent3d, t->con.center);
1341 /* aftertrans does insert ipos and action channels, and clears base flags, doesnt read transdata */
1342 special_aftertrans_update(C, t);
1357 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1366 glVertex2s(offset, 0);
1367 glVertex2s(offset + length, 0);
1368 glVertex2s(offset + length, 0);
1369 glVertex2s(offset + length - size, -size);
1370 glVertex2s(offset + length, 0);
1371 glVertex2s(offset + length - size, size);
1380 glVertex2s(0, offset);
1381 glVertex2s(0, offset + length);
1382 glVertex2s(0, offset + length);
1383 glVertex2s(-size, offset + length - size);
1384 glVertex2s(0, offset + length);
1385 glVertex2s(size, offset + length - size);
1391 static void drawArrowHead(ArrowDirection d, short size)
1399 glVertex2s(-size, -size);
1401 glVertex2s(-size, size);
1409 glVertex2s(-size, -size);
1411 glVertex2s(size, -size);
1417 static void drawArc(float size, float angle_start, float angle_end, int segments)
1419 float delta = (angle_end - angle_start) / segments;
1423 glBegin(GL_LINE_STRIP);
1425 for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1426 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1428 glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1433 static int helpline_poll(bContext *C)
1435 ARegion *ar = CTX_wm_region(C);
1437 if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1442 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1444 TransInfo *t = (TransInfo *)customdata;
1446 if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1447 float vecrot[3], cent[2];
1453 copy_v3_v3(vecrot, t->center);
1454 if (t->flag & T_EDIT) {
1455 Object *ob = t->obedit;
1456 if (ob) mul_m4_v3(ob->obmat, vecrot);
1458 else if (t->flag & T_POSE) {
1459 Object *ob = t->poseobj;
1460 if (ob) mul_m4_v3(ob->obmat, vecrot);
1463 projectFloatView(t, vecrot, cent); // no overflow in extreme cases
1467 switch (t->helpline) {
1469 UI_ThemeColor(TH_WIRE);
1472 glBegin(GL_LINE_STRIP);
1473 glVertex2iv(t->mval);
1477 glTranslatef(mval[0], mval[1], 0);
1478 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1482 drawArrow(UP, 5, 10, 5);
1483 drawArrow(DOWN, 5, 10, 5);
1487 UI_ThemeColor(TH_WIRE);
1489 glTranslatef(mval[0], mval[1], 0);
1492 drawArrow(RIGHT, 5, 10, 5);
1493 drawArrow(LEFT, 5, 10, 5);
1497 UI_ThemeColor(TH_WIRE);
1499 glTranslatef(mval[0], mval[1], 0);
1503 drawArrow(UP, 5, 10, 5);
1504 drawArrow(DOWN, 5, 10, 5);
1509 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1510 float angle = atan2f(dy, dx);
1511 float dist = sqrtf(dx * dx + dy * dy);
1512 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1513 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1514 UI_ThemeColor(TH_WIRE);
1517 glBegin(GL_LINE_STRIP);
1518 glVertex2iv(t->mval);
1522 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1526 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1527 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1531 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1532 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1534 drawArrowHead(DOWN, 5);
1538 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1539 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1541 drawArrowHead(UP, 5);
1548 unsigned char col[3], col2[3];
1549 UI_GetThemeColor3ubv(TH_GRID, col);
1551 glTranslatef(mval[0], mval[1], 0);
1555 UI_make_axis_color(col, col2, 'X');
1556 glColor3ubv((GLubyte *)col2);
1558 drawArrow(RIGHT, 5, 10, 5);
1559 drawArrow(LEFT, 5, 10, 5);
1561 UI_make_axis_color(col, col2, 'Y');
1562 glColor3ubv((GLubyte *)col2);
1564 drawArrow(UP, 5, 10, 5);
1565 drawArrow(DOWN, 5, 10, 5);
1575 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1580 drawPropCircle(C, t);
1582 drawNonPropEdge(C, t);
1585 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1586 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1588 const char printable[] = "Auto Keying On";
1589 float printable_size[2];
1592 BLF_width_and_height_default(printable, &printable_size[0], &printable_size[1]);
1594 xco = ar->winx - (int)printable_size[0] - 10;
1595 yco = ar->winy - (int)printable_size[1] - 10;
1597 /* warning text (to clarify meaning of overlays)
1598 * - original color was red to match the icon, but that clashes badly with a less nasty border
1600 UI_ThemeColorShade(TH_TEXT_HI, -50);
1601 BLF_draw_default_ascii(xco, ar->winy - 17, 0.0f, printable, sizeof(printable));
1603 /* autokey recording icon... */
1604 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1607 xco -= (ICON_DEFAULT_WIDTH + 2);
1608 UI_icon_draw(xco, yco, ICON_REC);
1610 glDisable(GL_BLEND);
1613 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1616 Scene *scene = t->scene;
1619 /* draw autokeyframing hint in the corner
1620 * - only draw if enabled (advanced users may be distracted/annoyed),
1621 * for objects that will be autokeyframed (no point ohterwise),
1622 * AND only for the active region (as showing all is too overwhelming)
1624 if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1626 if (t->flag & (T_OBJECT | T_POSE)) {
1627 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1628 drawAutoKeyWarning(t, ar);
1635 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1637 ToolSettings *ts = CTX_data_tool_settings(C);
1638 int constraint_axis[3] = {0, 0, 0};
1639 int proportional = 0;
1642 // Save back mode in case we're in the generic operator
1643 if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1644 RNA_property_enum_set(op->ptr, prop, t->mode);
1647 if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1648 float *values = (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1649 if (RNA_property_array_check(prop)) {
1650 RNA_property_float_set_array(op->ptr, prop, values);
1653 RNA_property_float_set(op->ptr, prop, values[0]);
1657 /* convert flag to enum */
1658 switch (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) {
1659 case (T_PROP_EDIT | T_PROP_CONNECTED):
1660 proportional = PROP_EDIT_CONNECTED;
1663 proportional = PROP_EDIT_ON;
1666 proportional = PROP_EDIT_OFF;
1669 // If modal, save settings back in scene if not set as operator argument
1670 if (t->flag & T_MODAL) {
1672 /* save settings if not set in operator */
1673 if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1674 !RNA_property_is_set(op->ptr, prop))
1677 ts->proportional = proportional;
1678 else if (t->options & CTX_MASK)
1679 ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1681 ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1684 if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1685 !RNA_property_is_set(op->ptr, prop))
1687 ts->proportional_size = t->prop_size;
1690 if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1691 !RNA_property_is_set(op->ptr, prop))
1693 ts->prop_mode = t->prop_mode;
1696 /* do we check for parameter? */
1697 if (t->modifiers & MOD_SNAP) {
1698 ts->snap_flag |= SCE_SNAP;
1701 ts->snap_flag &= ~SCE_SNAP;
1704 if (t->spacetype == SPACE_VIEW3D) {
1705 if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1706 !RNA_property_is_set(op->ptr, prop))
1708 View3D *v3d = t->view;
1710 v3d->twmode = t->current_orientation;
1715 if (RNA_struct_find_property(op->ptr, "proportional")) {
1716 RNA_enum_set(op->ptr, "proportional", proportional);
1717 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1718 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1721 if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1722 RNA_property_float_set_array(op->ptr, prop, t->axis);
1725 if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1726 RNA_property_boolean_set(op->ptr, prop, t->flag & T_MIRROR);
1729 if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1730 /* constraint orientation can be global, event if user selects something else
1731 * so use the orientation in the constraint if set
1733 if (t->con.mode & CON_APPLY) {
1734 RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1737 RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1740 if (t->con.mode & CON_APPLY) {
1741 if (t->con.mode & CON_AXIS0) {
1742 constraint_axis[0] = 1;
1744 if (t->con.mode & CON_AXIS1) {
1745 constraint_axis[1] = 1;
1747 if (t->con.mode & CON_AXIS2) {
1748 constraint_axis[2] = 1;
1752 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
1756 /* note: caller needs to free 't' on a 0 return */
1757 int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int mode)
1764 /* added initialize, for external calls to set stuff in TransInfo, like undo string */
1766 t->state = TRANS_STARTING;
1768 if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
1769 if (RNA_property_boolean_get(op->ptr, prop)) {
1770 options |= CTX_TEXTURE;
1774 t->options = options;
1778 t->launch_event = event ? event->type : -1;
1780 if (t->launch_event == EVT_TWEAK_R) {
1781 t->launch_event = RIGHTMOUSE;
1783 else if (t->launch_event == EVT_TWEAK_L) {
1784 t->launch_event = LEFTMOUSE;
1787 // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
1788 // For manipulator only, so assume LEFTMOUSE
1789 if (t->launch_event == 0) {
1790 t->launch_event = LEFTMOUSE;
1793 if (!initTransInfo(C, t, op, event)) { /* internal data, mouse, vectors */
1797 if (t->spacetype == SPACE_VIEW3D) {
1798 //calc_manipulator_stats(curarea);
1799 initTransformOrientation(C, t);
1801 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
1802 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1803 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1804 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1806 else if (t->spacetype == SPACE_IMAGE) {
1807 unit_m3(t->spacemtx);
1808 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1809 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
1810 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
1812 else if (t->spacetype == SPACE_CLIP) {
1813 unit_m3(t->spacemtx);
1814 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1816 else if (t->spacetype == SPACE_NODE) {
1817 unit_m3(t->spacemtx);
1818 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
1819 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
1820 /*t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);*/
1823 unit_m3(t->spacemtx);
1825 createTransData(C, t); // make TransData structs from selection
1827 if (t->total == 0) {
1832 /* Stupid code to have Ctrl-Click on manipulator work ok */
1834 /* do this only for translation/rotation/resize due to only this
1835 * moded are available from manipulator and doing such check could
1836 * lead to keymap conflicts for other modes (see #31584)
1838 if (ELEM3(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
1839 wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
1842 for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1843 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
1844 if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) && event->ctrl) ||
1845 (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
1846 (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) && event->alt) ||
1847 ((kmi->type == OSKEY) && event->oskey) )
1849 t->modifiers |= MOD_SNAP_INVERT;
1858 initSnapping(t, op); // Initialize snapping data AFTER mode flags
1860 /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
1861 /* EVIL2: we gave as argument also texture space context bit... was cleared */
1862 /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
1865 calculatePropRatio(t);
1868 initMouseInput(t, &t->mouse, t->center2d, t->imval);
1871 case TFM_TRANSLATION:
1880 case TFM_SKIN_RESIZE:
1892 case TFM_SHRINKFATTEN:
1893 initShrinkFatten(t);
1898 case TFM_CURVE_SHRINKFATTEN:
1899 initCurveShrinkFatten(t);
1901 case TFM_MASK_SHRINKFATTEN:
1902 initMaskShrinkFatten(t);
1914 { /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
1915 bArmature *arm = t->poseobj->data;
1916 if (arm->drawtype == ARM_ENVELOPE)
1917 initBoneEnvelope(t);
1922 case TFM_BONE_ENVELOPE:
1923 initBoneEnvelope(t);
1925 case TFM_EDGE_SLIDE:
1931 case TFM_TIME_TRANSLATE:
1932 initTimeTranslate(t);
1934 case TFM_TIME_SLIDE:
1937 case TFM_TIME_SCALE:
1940 case TFM_TIME_DUPLICATE:
1941 /* same as TFM_TIME_EXTEND, but we need the mode info for later
1942 * so that duplicate-culling will work properly
1944 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1947 initTimeTranslate(t);
1950 case TFM_TIME_EXTEND:
1951 /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
1952 * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
1953 * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
1954 * depending on which editor this was called from
1956 if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
1959 initTimeTranslate(t);
1981 if (t->state == TRANS_CANCEL) {
1987 /* overwrite initial values if operator supplied a non-null vector */
1988 if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
1989 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory */
1991 if (RNA_property_array_check(prop)) {
1992 RNA_float_get_array(op->ptr, "value", values);
1995 values[0] = RNA_float_get(op->ptr, "value");
1998 copy_v4_v4(t->values, values);
1999 copy_v4_v4(t->auto_values, values);
2000 t->flag |= T_AUTOVALUES;
2003 /* Transformation axis from operator */
2004 if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2005 RNA_property_float_get_array(op->ptr, prop, t->axis);
2006 normalize_v3(t->axis);
2007 copy_v3_v3(t->axis_orig, t->axis);
2010 /* Constraint init from operator */
2011 if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2012 int constraint_axis[3];
2014 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2016 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2017 t->con.mode |= CON_APPLY;
2019 if (constraint_axis[0]) {
2020 t->con.mode |= CON_AXIS0;
2022 if (constraint_axis[1]) {
2023 t->con.mode |= CON_AXIS1;
2025 if (constraint_axis[2]) {
2026 t->con.mode |= CON_AXIS2;
2029 setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2038 void transformApply(bContext *C, TransInfo *t)
2042 if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2043 selectConstraint(t);
2045 t->transform(t, t->mval); // calls recalcData()
2046 viewRedrawForce(C, t);
2048 t->redraw = TREDRAW_NOTHING;
2050 else if (t->redraw & TREDRAW_SOFT) {
2051 viewRedrawForce(C, t);
2054 /* If auto confirm is on, break after one pass */
2055 if (t->options & CTX_AUTOCONFIRM) {
2056 t->state = TRANS_CONFIRM;
2059 if (BKE_ptcache_get_continue_physics()) {
2061 //do_screenhandlers(G.curscreen);
2062 t->redraw |= TREDRAW_HARD;
2068 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2072 if (t->redraw & TREDRAW_SOFT) {
2073 t->redraw |= TREDRAW_HARD;
2074 transformApply((bContext *)C, t);
2078 int transformEnd(bContext *C, TransInfo *t)
2080 int exit_code = OPERATOR_RUNNING_MODAL;
2084 if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2085 /* handle restoring objects */
2086 if (t->state == TRANS_CANCEL) {
2087 /* exception, edge slide transformed UVs too */
2088 if (t->mode == TFM_EDGE_SLIDE)
2089 doEdgeSlide(t, 0.0f);
2091 exit_code = OPERATOR_CANCELLED;
2092 restoreTransObjects(t); // calls recalcData()
2095 exit_code = OPERATOR_FINISHED;
2098 /* aftertrans does insert keyframes, and clears base flags, doesnt read transdata */
2099 special_aftertrans_update(C, t);
2104 /* send events out for redraws */
2105 viewRedrawPost(C, t);
2107 /* Undo as last, certainly after special_trans_update! */
2109 if (t->state == TRANS_CANCEL) {
2110 // if (t->undostr) ED_undo_push(C, t->undostr);
2113 // if (t->undostr) ED_undo_push(C, t->undostr);
2114 // else ED_undo_push(C, transform_to_undostr(t));
2118 viewRedrawForce(C, t);
2126 /* ************************** TRANSFORM LOCKS **************************** */
2128 static void protectedTransBits(short protectflag, float *vec)
2130 if (protectflag & OB_LOCK_LOCX)
2132 if (protectflag & OB_LOCK_LOCY)
2134 if (protectflag & OB_LOCK_LOCZ)
2138 static void protectedSizeBits(short protectflag, float *size)
2140 if (protectflag & OB_LOCK_SCALEX)
2142 if (protectflag & OB_LOCK_SCALEY)
2144 if (protectflag & OB_LOCK_SCALEZ)
2148 static void protectedRotateBits(short protectflag, float *eul, float *oldeul)
2150 if (protectflag & OB_LOCK_ROTX)
2152 if (protectflag & OB_LOCK_ROTY)
2154 if (protectflag & OB_LOCK_ROTZ)
2159 /* this function only does the delta rotation */
2160 /* axis-angle is usually internally stored as quats... */
2161 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2163 /* check that protection flags are set */
2164 if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2167 if (protectflag & OB_LOCK_ROT4D) {
2168 /* axis-angle getting limited as 4D entities that they are... */
2169 if (protectflag & OB_LOCK_ROTW)
2171 if (protectflag & OB_LOCK_ROTX)
2172 axis[0] = oldAxis[0];
2173 if (protectflag & OB_LOCK_ROTY)
2174 axis[1] = oldAxis[1];
2175 if (protectflag & OB_LOCK_ROTZ)
2176 axis[2] = oldAxis[2];
2179 /* axis-angle get limited with euler... */
2180 float eul[3], oldeul[3];
2182 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2183 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2185 if (protectflag & OB_LOCK_ROTX)
2187 if (protectflag & OB_LOCK_ROTY)
2189 if (protectflag & OB_LOCK_ROTZ)
2192 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2194 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2195 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2196 /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2202 /* this function only does the delta rotation */
2203 static void protectedQuaternionBits(short protectflag, float *quat, float *oldquat)
2205 /* check that protection flags are set */
2206 if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2209 if (protectflag & OB_LOCK_ROT4D) {
2210 /* quaternions getting limited as 4D entities that they are... */
2211 if (protectflag & OB_LOCK_ROTW)
2212 quat[0] = oldquat[0];
2213 if (protectflag & OB_LOCK_ROTX)
2214 quat[1] = oldquat[1];
2215 if (protectflag & OB_LOCK_ROTY)
2216 quat[2] = oldquat[2];
2217 if (protectflag & OB_LOCK_ROTZ)
2218 quat[3] = oldquat[3];
2221 /* quaternions get limited with euler... (compatibility mode) */
2222 float eul[3], oldeul[3], nquat[4], noldquat[4];
2225 qlen = normalize_qt_qt(nquat, quat);
2226 normalize_qt_qt(noldquat, oldquat);
2228 quat_to_eul(eul, nquat);
2229 quat_to_eul(oldeul, noldquat);
2231 if (protectflag & OB_LOCK_ROTX)
2233 if (protectflag & OB_LOCK_ROTY)
2235 if (protectflag & OB_LOCK_ROTZ)
2238 eul_to_quat(quat, eul);
2240 /* restore original quat size */
2241 mul_qt_fl(quat, qlen);
2243 /* quaternions flip w sign to accumulate rotations correctly */
2244 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2245 (nquat[0] > 0.0f && quat[0] < 0.0f))
2247 mul_qt_fl(quat, -1.0f);
2252 /* ******************* TRANSFORM LIMITS ********************** */
2254 static void constraintTransLim(TransInfo *t, TransData *td)
2257 bConstraintTypeInfo *ctiLoc = get_constraint_typeinfo(CONSTRAINT_TYPE_LOCLIMIT);
2258 bConstraintTypeInfo *ctiDist = get_constraint_typeinfo(CONSTRAINT_TYPE_DISTLIMIT);
2260 bConstraintOb cob = {NULL};
2262 float ctime = (float)(t->scene->r.cfra);
2264 /* Make a temporary bConstraintOb for using these limit constraints
2265 * - they only care that cob->matrix is correctly set ;-)
2266 * - current space should be local
2268 unit_m4(cob.matrix);
2269 copy_v3_v3(cob.matrix[3], td->loc);
2271 /* Evaluate valid constraints */
2272 for (con = td->con; con; con = con->next) {
2273 bConstraintTypeInfo *cti = NULL;
2274 ListBase targets = {NULL, NULL};
2276 /* only consider constraint if enabled */
2277 if (con->flag & CONSTRAINT_DISABLE) continue;
2278 if (con->enforce == 0.0f) continue;
2280 /* only use it if it's tagged for this purpose (and the right type) */
2281 if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2282 bLocLimitConstraint *data = con->data;
2284 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2288 else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2289 bDistLimitConstraint *data = con->data;
2291 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2297 /* do space conversions */
2298 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2299 /* just multiply by td->mtx (this should be ok) */
2300 mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2302 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2303 /* skip... incompatable spacetype */
2307 /* get constraint targets if needed */
2308 get_constraint_targets_for_solving(con, &cob, &targets, ctime);
2311 cti->evaluate_constraint(con, &cob, &targets);
2313 /* convert spaces again */
2314 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2315 /* just multiply by td->smtx (this should be ok) */
2316 mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2319 /* free targets list */
2320 BLI_freelistN(&targets);
2324 /* copy results from cob->matrix */
2325 copy_v3_v3(td->loc, cob.matrix[3]);
2329 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2331 /* Make a temporary bConstraintOb for use by limit constraints
2332 * - they only care that cob->matrix is correctly set ;-)
2333 * - current space should be local
2335 memset(cob, 0, sizeof(bConstraintOb));
2337 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2339 /* objects and bones do normalization first too, otherwise
2340 * we don't necessarily end up with a rotation matrix, and
2341 * then conversion back to quat gives a different result */
2343 normalize_qt_qt(quat, td->ext->quat);
2344 quat_to_mat4(cob->matrix, quat);
2346 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2348 axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2352 eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2357 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2360 bConstraintTypeInfo *cti = get_constraint_typeinfo(CONSTRAINT_TYPE_ROTLIMIT);
2363 int do_limit = FALSE;
2365 /* Evaluate valid constraints */
2366 for (con = td->con; con; con = con->next) {
2367 /* only consider constraint if enabled */
2368 if (con->flag & CONSTRAINT_DISABLE) continue;
2369 if (con->enforce == 0.0f) continue;
2371 /* we're only interested in Limit-Rotation constraints */
2372 if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2373 bRotLimitConstraint *data = con->data;
2375 /* only use it if it's tagged for this purpose */
2376 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2379 /* skip incompatable spacetypes */
2380 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2383 /* only do conversion if necessary, to preserve quats and eulers */
2384 if (do_limit == FALSE) {
2385 constraintob_from_transdata(&cob, td);
2389 /* do space conversions */
2390 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2391 /* just multiply by td->mtx (this should be ok) */
2392 mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2396 cti->evaluate_constraint(con, &cob, NULL);
2398 /* convert spaces again */
2399 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2400 /* just multiply by td->smtx (this should be ok) */
2401 mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2407 /* copy results from cob->matrix */
2408 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2410 mat4_to_quat(td->ext->quat, cob.matrix);
2412 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2414 mat4_to_axis_angle(&td->ext->quat[1], &td->ext->quat[0], cob.matrix);
2418 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2424 static void constraintSizeLim(TransInfo *t, TransData *td)
2426 if (td->con && td->ext) {
2427 bConstraintTypeInfo *cti = get_constraint_typeinfo(CONSTRAINT_TYPE_SIZELIMIT);
2428 bConstraintOb cob = {NULL};
2430 float size_sign[3], size_abs[3];
2433 /* Make a temporary bConstraintOb for using these limit constraints
2434 * - they only care that cob->matrix is correctly set ;-)
2435 * - current space should be local
2437 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2438 /* scale val and reset size */
2439 return; // TODO: fix this case
2442 /* Reset val if SINGLESIZE but using a constraint */
2443 if (td->flag & TD_SINGLESIZE)
2446 /* separate out sign to apply back later */
2447 for (i = 0; i < 3; i++) {
2448 size_sign[i] = signf(td->ext->size[i]);
2449 size_abs[i] = fabsf(td->ext->size[i]);
2452 size_to_mat4(cob.matrix, size_abs);
2455 /* Evaluate valid constraints */
2456 for (con = td->con; con; con = con->next) {
2457 /* only consider constraint if enabled */
2458 if (con->flag & CONSTRAINT_DISABLE) continue;
2459 if (con->enforce == 0.0f) continue;
2461 /* we're only interested in Limit-Scale constraints */
2462 if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2463 bSizeLimitConstraint *data = con->data;
2465 /* only use it if it's tagged for this purpose */
2466 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2469 /* do space conversions */
2470 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2471 /* just multiply by td->mtx (this should be ok) */
2472 mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2474 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2475 /* skip... incompatible spacetype */
2480 cti->evaluate_constraint(con, &cob, NULL);
2482 /* convert spaces again */
2483 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2484 /* just multiply by td->smtx (this should be ok) */
2485 mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2490 /* copy results from cob->matrix */
2491 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2492 /* scale val and reset size */
2493 return; // TODO: fix this case
2496 /* Reset val if SINGLESIZE but using a constraint */
2497 if (td->flag & TD_SINGLESIZE)
2500 /* extrace scale from matrix and apply back sign */
2501 mat4_to_size(td->ext->size, cob.matrix);
2502 mul_v3_v3(td->ext->size, size_sign);
2507 /* ************************** WARP *************************** */
2509 static void postInputWarp(TransInfo *t, float values[3])
2511 mul_v3_fl(values, (float)(M_PI * 2));
2513 if (t->customData) { /* non-null value indicates reversed input */
2518 void initWarp(TransInfo *t)
2520 float max[3], min[3];
2524 t->transform = Warp;
2525 t->handleEvent = handleEventWarp;
2527 setInputPostFct(&t->mouse, postInputWarp);
2528 initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO);
2533 t->snap[1] = 5.0f / 180.0f * (float)M_PI;
2534 t->snap[2] = 1.0f / 180.0f * (float)M_PI;
2536 t->num.increment = 1.0f;
2538 t->flag |= T_NO_CONSTRAINT;
2540 /* we need min/max in view space */
2541 for (i = 0; i < t->total; i++) {
2543 copy_v3_v3(center, t->data[i].center);
2544 mul_m3_v3(t->data[i].mtx, center);
2545 mul_m4_v3(t->viewmat, center);
2546 sub_v3_v3(center, t->viewmat[3]);
2548 minmax_v3v3_v3(min, max, center);
2551 copy_v3_v3(max, center);
2552 copy_v3_v3(min, center);
2556 mid_v3_v3v3(t->center, min, max);
2558 if (max[0] == min[0]) max[0] += 0.1f; /* not optimal, but flipping is better than invalid garbage (i.e. division by zero!) */
2559 t->val = (max[0] - min[0]) / 2.0f; /* t->val is X dimension projected boundbox */
2562 int handleEventWarp(TransInfo *t, wmEvent *event)
2566 if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2567 // Use customData pointer to signal warp direction
2568 if (t->customData == NULL)
2569 t->customData = (void *)1;
2571 t->customData = NULL;
2579 int Warp(TransInfo *t, const int UNUSED(mval[2]))
2581 TransData *td = t->data;
2582 float vec[3], circumfac, dist, phi0, co, si, *curs, cursor[3], gcursor[3];
2586 curs = give_cursor(t->scene, t->view);
2588 * gcursor is the one used for helpline.
2589 * It has to be in the same space as the drawing loop
2590 * (that means it needs to be in the object's space when in edit mode and
2591 * in global space in object mode)
2593 * cursor is used for calculations.
2594 * It needs to be in view space, but we need to take object's offset
2595 * into account if in Edit mode.
2597 copy_v3_v3(cursor, curs);
2598 copy_v3_v3(gcursor, cursor);
2599 if (t->flag & T_EDIT) {
2600 sub_v3_v3(cursor, t->obedit->obmat[3]);
2601 sub_v3_v3(gcursor, t->obedit->obmat[3]);
2602 mul_m3_v3(t->data->smtx, gcursor);
2604 mul_m4_v3(t->viewmat, cursor);
2605 sub_v3_v3(cursor, t->viewmat[3]);
2607 /* amount of radians for warp */
2608 circumfac = t->values[0];
2610 snapGrid(t, &circumfac);
2611 applyNumInput(&t->num, &circumfac);
2613 /* header print for NumInput */
2614 if (hasNumInput(&t->num)) {
2615 char c[NUM_STR_REP_LEN];
2617 outputNumInput(&(t->num), c);
2619 sprintf(str, "Warp: %s", c);
2621 circumfac = DEG2RADF(circumfac);
2624 /* default header print */
2625 sprintf(str, "Warp: %.3f", RAD2DEGF(circumfac));
2628 t->values[0] = circumfac;
2630 circumfac /= 2; /* only need 180 on each side to make 360 */
2632 for (i = 0; i < t->total; i++, td++) {
2634 if (td->flag & TD_NOACTION)
2637 if (td->flag & TD_SKIP)
2640 /* translate point to center, rotate in such a way that outline==distance */
2641 copy_v3_v3(vec, td->iloc);
2642 mul_m3_v3(td->mtx, vec);
2643 mul_m4_v3(t->viewmat, vec);
2644 sub_v3_v3(vec, t->viewmat[3]);
2646 dist = vec[0] - cursor[0];
2648 /* t->val is X dimension projected boundbox */
2649 phi0 = (circumfac * dist / t->val);
2651 vec[1] = (vec[1] - cursor[1]);
2653 co = (float)cos(phi0);
2654 si = (float)sin(phi0);
2655 loc[0] = -si * vec[1] + cursor[0];
2656 loc[1] = co * vec[1] + cursor[1];
2659 mul_m4_v3(t->viewinv, loc);
2660 sub_v3_v3(loc, t->viewinv[3]);
2661 mul_m3_v3(td->smtx, loc);
2663 sub_v3_v3(loc, td->iloc);
2664 mul_v3_fl(loc, td->factor);
2665 add_v3_v3v3(td->loc, td->iloc, loc);
2670 ED_area_headerprint(t->sa, str);
2675 /* ************************** SHEAR *************************** */
2677 static void postInputShear(TransInfo *UNUSED(t), float values[3])
2679 mul_v3_fl(values, 0.05f);
2682 void initShear(TransInfo *t)
2684 t->mode = TFM_SHEAR;
2685 t->transform = Shear;
2686 t->handleEvent = handleEventShear;
2688 setInputPostFct(&t->mouse, postInputShear);
2689 initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2695 t->snap[2] = t->snap[1] * 0.1f;
2697 t->num.increment = 0.1f;
2699 t->flag |= T_NO_CONSTRAINT;
2702 int handleEventShear(TransInfo *t, wmEvent *event)
2706 if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2707 // Use customData pointer to signal Shear direction
2708 if (t->customData == NULL) {
2709 initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE);
2710 t->customData = (void *)1;
2713 initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE);
2714 t->customData = NULL;
2724 int Shear(TransInfo *t, const int UNUSED(mval[2]))
2726 TransData *td = t->data;
2728 float smat[3][3], tmat[3][3], totmat[3][3], persmat[3][3], persinv[3][3];
2733 copy_m3_m4(persmat, t->viewmat);
2734 invert_m3_m3(persinv, persmat);
2736 value = t->values[0];
2738 snapGrid(t, &value);
2740 applyNumInput(&t->num, &value);
2742 /* header print for NumInput */
2743 if (hasNumInput(&t->num)) {
2744 char c[NUM_STR_REP_LEN];
2746 outputNumInput(&(t->num), c);
2748 sprintf(str, "Shear: %s %s", c, t->proptext);
2751 /* default header print */
2752 sprintf(str, "Shear: %.3f %s", value, t->proptext);
2755 t->values[0] = value;
2759 // Custom data signals shear direction
2760 if (t->customData == NULL)
2765 mul_m3_m3m3(tmat, smat, persmat);
2766 mul_m3_m3m3(totmat, persinv, tmat);
2768 for (i = 0; i < t->total; i++, td++) {
2769 if (td->flag & TD_NOACTION)
2772 if (td->flag & TD_SKIP)
2777 mul_m3_m3m3(mat3, totmat, td->mtx);
2778 mul_m3_m3m3(tmat, td->smtx, mat3);
2781 copy_m3_m3(tmat, totmat);
2783 sub_v3_v3v3(vec, td->center, t->center);
2785 mul_m3_v3(tmat, vec);
2787 add_v3_v3(vec, t->center);
2788 sub_v3_v3(vec, td->center);
2790 mul_v3_fl(vec, td->factor);
2792 add_v3_v3v3(td->loc, td->iloc, vec);
2797 ED_area_headerprint(t->sa, str);
2802 /* ************************** RESIZE *************************** */
2804 void initResize(TransInfo *t)
2806 t->mode = TFM_RESIZE;
2807 t->transform = Resize;
2809 initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP);
2811 t->flag |= T_NULL_ONE;
2812 t->num.flag |= NUM_NULL_ONE;
2813 t->num.flag |= NUM_AFFECT_ALL;
2815 t->flag |= T_NO_ZERO;
2816 t->num.flag |= NUM_NO_ZERO;
2823 t->snap[2] = t->snap[1] * 0.1f;
2825 t->num.increment = t->snap[1];
2828 static void headerResize(TransInfo *t, float vec[3], char *str)
2830 char tvec[NUM_STR_REP_LEN * 3];
2832 if (hasNumInput(&t->num)) {
2833 outputNumInput(&(t->num), tvec);
2836 BLI_snprintf(&tvec[0], NUM_STR_REP_LEN, "%.4f", vec[0]);
2837 BLI_snprintf(&tvec[NUM_STR_REP_LEN], NUM_STR_REP_LEN, "%.4f", vec[1]);
2838 BLI_snprintf(&tvec[NUM_STR_REP_LEN * 2], NUM_STR_REP_LEN, "%.4f", vec[2]);
2841 if (t->con.mode & CON_APPLY) {
2842 switch (t->num.idx_max) {
2844 spos += sprintf(spos, "Scale: %s%s %s", &tvec[0], t->con.text, t->proptext);
2847 spos += sprintf(spos, "Scale: %s : %s%s %s", &tvec[0], &tvec[NUM_STR_REP_LEN],
2848 t->con.text, t->proptext);
2851 spos += sprintf(spos, "Scale: %s : %s : %s%s %s", &tvec[0], &tvec[NUM_STR_REP_LEN],
2852 &tvec[NUM_STR_REP_LEN * 2], t->con.text, t->proptext);
2856 if (t->flag & T_2D_EDIT) {
2857 spos += sprintf(spos, "Scale X: %s Y: %s%s %s", &tvec[0], &tvec[NUM_STR_REP_LEN],
2858 t->con.text, t->proptext);
2861 spos += sprintf(spos, "Scale X: %s Y: %s Z: %s%s %s", &tvec[0], &tvec[NUM_STR_REP_LEN],
2862 &tvec[NUM_STR_REP_LEN * 2], t->con.text, t->proptext);
2866 if (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) {
2867 spos += sprintf(spos, " Proportional size: %.2f", t->prop_size);
2873 /* FLT_EPSILON is too small [#29633], 0.0000001f starts to flip */
2874 #define TX_FLIP_EPS 0.00001f
2875 BLI_INLINE int tx_sign(const float a)
2877 return (a < -TX_FLIP_EPS ? 1 : a > TX_FLIP_EPS ? 2 : 3);
2879 BLI_INLINE int tx_vec_sign_flip(const float a[3], const float b[3])
2881 return ((tx_sign(a[0]) & tx_sign(b[0])) == 0 ||
2882 (tx_sign(a[1]) & tx_sign(b[1])) == 0 ||
2883 (tx_sign(a[2]) & tx_sign(b[2])) == 0);
2886 /* smat is reference matrix, only scaled */
2887 static void TransMat3ToSize(float mat[3][3], float smat[3][3], float size[3])
2891 copy_v3_v3(vec, mat[0]);
2892 size[0] = normalize_v3(vec);
2893 copy_v3_v3(vec, mat[1]);
2894 size[1] = normalize_v3(vec);
2895 copy_v3_v3(vec, mat[2]);
2896 size[2] = normalize_v3(vec);
2898 /* first tried with dotproduct... but the sign flip is crucial */
2899 if (tx_vec_sign_flip(mat[0], smat[0]) ) size[0] = -size[0];
2900 if (tx_vec_sign_flip(mat[1], smat[1]) ) size[1] = -size[1];
2901 if (tx_vec_sign_flip(mat[2], smat[2]) ) size[2] = -size[2];
2905 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3])
2907 float tmat[3][3], smat[3][3], center[3];
2910 if (t->flag & T_EDIT) {
2911 mul_m3_m3m3(smat, mat, td->mtx);
2912 mul_m3_m3m3(tmat, td->smtx, smat);
2915 copy_m3_m3(tmat, mat);
2918 if (t->con.applySize) {
2919 t->con.applySize(t, td, tmat);
2922 /* local constraint shouldn't alter center */
2923 if ((t->around == V3D_LOCAL) &&
2924 ( (t->flag & (T_OBJECT | T_POSE)) ||
2925 ((t->flag & T_EDIT) && (t->settings->selectmode & (SCE_SELECT_EDGE | SCE_SELECT_FACE))) ||