Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transform calculations for drag-moving CanvasItems in editor #82667

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2062,23 +2062,26 @@ 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;
}

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;
Comment on lines +2074 to +2076
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it so this transform is calculated just like in many different places in this file. The last part (viewport->get_transform() *) don't make much sense to me, something seems off. The viewport here is a Control and according to the docs in _gui_input the events should be relative to such control, so applying its own transform on top makes no sense. Maybe the docs lie, maybe here things are incorrect. For now leaving it calculated as in other places as it seems like a potential pandora box (changing it here, would require changes in 20 different places and so on). For potential another PR.


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);

Expand All @@ -2092,8 +2095,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;
}
Expand Down
Loading