From 5e1116da4ce052acc64f45ba4bd71310b4826aea Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 8 Aug 2017 07:55:21 -0300 Subject: [PATCH] Added proper local transform snapping, closes #4985 --- editor/plugins/spatial_editor_plugin.cpp | 40 +++++++++++++++++++++--- editor/plugins/spatial_editor_plugin.h | 2 ++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 50d2f193ed5..0dba1f12a23 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -1190,18 +1190,48 @@ void SpatialEditorViewport::_sinput(const Ref &p_event) { motion = motion_mask.dot(motion) * motion_mask; } + //set_message("Translating: "+motion); + + List &selection = editor_selection->get_selected_node_list(); + float snap = 0; if (_edit.snap || spatial_editor->is_snap_enabled()) { snap = spatial_editor->get_translate_snap(); - motion.snap(Vector3(snap, snap, snap)); + bool local_coords = spatial_editor->are_local_coords_enabled(); + + if (local_coords) { + bool multiple = false; + Spatial *node = NULL; + for (List::Element *E = selection.front(); E; E = E->next()) { + + Spatial *sp = E->get()->cast_to(); + if (!sp) { + continue; + } + if (node) { + multiple = true; + break; + } else { + node = sp; + } + } + + if (multiple) { + motion.snap(Vector3(snap, snap, snap)); + } else { + Basis b = node->get_global_transform().basis.orthonormalized(); + Vector3 local_motion = b.inverse().xform(motion); + local_motion.snap(Vector3(snap, snap, snap)); + motion = b.xform(local_motion); + } + + } else { + motion.snap(Vector3(snap, snap, snap)); + } } - //set_message("Translating: "+motion); - - List &selection = editor_selection->get_selected_node_list(); - for (List::Element *E = selection.front(); E; E = E->next()) { Spatial *sp = E->get()->cast_to(); diff --git a/editor/plugins/spatial_editor_plugin.h b/editor/plugins/spatial_editor_plugin.h index 9b626054c07..e9857f8b0c9 100644 --- a/editor/plugins/spatial_editor_plugin.h +++ b/editor/plugins/spatial_editor_plugin.h @@ -514,6 +514,8 @@ public: float get_rotate_snap() const { return snap_rotate->get_text().to_double(); } float get_scale_snap() const { return snap_scale->get_text().to_double(); } + bool are_local_coords_enabled() const { return transform_menu->get_popup()->is_item_checked(transform_menu->get_popup()->get_item_index(SpatialEditor::MENU_TRANSFORM_LOCAL_COORDS)); } + Ref get_move_gizmo(int idx) const { return move_gizmo[idx]; } Ref get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; }