From 447a4b6bcf4770dafff456978e2a9840f77d2c5f Mon Sep 17 00:00:00 2001 From: kobewi Date: Sat, 16 Sep 2023 14:11:09 +0200 Subject: [PATCH] Fix rotated 2D movement gizmo --- editor/plugins/canvas_item_editor_plugin.cpp | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index a6ea76943fb..52af0adb6d5 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -2056,27 +2056,31 @@ bool CanvasItemEditor::_gui_input_move(const Ref &p_event) { if (drag_type == DRAG_MOVE || drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) { // Move the nodes - if (m.is_valid()) { + if (m.is_valid() && !drag_selection.is_empty()) { _restore_canvas_item_state(drag_selection, true); drag_to = transform.affine_inverse().xform(m->get_position()); Point2 previous_pos; - if (!drag_selection.is_empty()) { - 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()); + 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()); + } 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); + + if (drag_type == DRAG_MOVE_X) { + drag_delta.y = 0; } else { - previous_pos = _get_encompassing_rect_from_list(drag_selection).position; + drag_delta.x = 0; } + drag_delta = selected->get_transform().basis_xform(drag_delta); } - - Point2 new_pos = snap_point(previous_pos + (drag_to - drag_from), SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL | SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES, 0, nullptr, drag_selection); - - if (drag_type == DRAG_MOVE_X) { - new_pos.y = previous_pos.y; - } else if (drag_type == DRAG_MOVE_Y) { - new_pos.x = previous_pos.x; - } + 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); bool single_axis = m->is_shift_pressed(); if (single_axis) {