Merge pull request #82667 from kleonc/canvas-item-editor-drag-moving-transforms-fix

Fix transform calculations for drag-moving CanvasItems in editor
This commit is contained in:
Rémi Verschelde 2023-10-22 12:11:28 +02:00
commit 6bf936c4cc
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 9 additions and 6 deletions

View File

@ -2057,8 +2057,8 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
drag_to = transform.affine_inverse().xform(m->get_position());
Point2 previous_pos;
if (drag_selection.size() == 1) {
Transform2D xform = drag_selection[0]->get_global_transform_with_canvas() * drag_selection[0]->get_transform().affine_inverse();
previous_pos = xform.xform(drag_selection[0]->_edit_get_position());
Transform2D parent_xform = drag_selection[0]->get_global_transform_with_canvas() * drag_selection[0]->get_transform().affine_inverse();
previous_pos = parent_xform.xform(drag_selection[0]->_edit_get_position());
} else {
previous_pos = _get_encompassing_rect_from_list(drag_selection).position;
}
@ -2066,14 +2066,17 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
Point2 drag_delta = drag_to - drag_from;
if (drag_selection.size() == 1 && (drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y)) {
const CanvasItem *selected = drag_selection.front()->get();
drag_delta = selected->get_transform().affine_inverse().basis_xform(drag_delta);
Transform2D parent_xform = selected->get_global_transform_with_canvas() * selected->get_transform().affine_inverse();
Transform2D unscaled_transform = (transform * parent_xform * selected->_edit_get_transform()).orthonormalized();
Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
drag_delta = simple_xform.affine_inverse().basis_xform(drag_delta);
if (drag_type == DRAG_MOVE_X) {
drag_delta.y = 0;
} else {
drag_delta.x = 0;
}
drag_delta = selected->get_transform().basis_xform(drag_delta);
drag_delta = simple_xform.basis_xform(drag_delta);
}
Point2 new_pos = snap_point(previous_pos + drag_delta, SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL | SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES, 0, nullptr, drag_selection);
@ -2087,8 +2090,8 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
}
for (CanvasItem *ci : drag_selection) {
Transform2D xform = ci->get_global_transform_with_canvas().affine_inverse() * ci->get_transform();
ci->_edit_set_position(ci->_edit_get_position() + xform.xform(new_pos) - xform.xform(previous_pos));
Transform2D parent_xform_inv = ci->get_transform() * ci->get_global_transform_with_canvas().affine_inverse();
ci->_edit_set_position(ci->_edit_get_position() + parent_xform_inv.basis_xform(new_pos - previous_pos));
}
return true;
}