From e36131a57a0dcf2e3cf7fb268eed287fb445bca4 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 21 Nov 2024 03:08:08 +0100 Subject: [PATCH] [WIP] Add "Follow Selection" in the 3D editor by using Center Selection twice When pressing the Center Selection shortcut twice, you will begin following the current selection. This also applies to selection changes. The effect is undone by pressing the Center Selection shortcut another time, or by panning/using freelook on the 3D editor camera. (Orbiting does not undo the effect.) TODO: - Add a visual indicator when following is enabled. - Modify tooltips and possibly menu action names to document the functionality. --- editor/plugins/node_3d_editor_plugin.cpp | 13 +++++++++++++ editor/plugins/node_3d_editor_plugin.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 810d1674ca17..cfdadd070a4c 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2364,7 +2364,9 @@ void Node3DEditorViewport::_sinput(const Ref &p_event) { _menu_option(VIEW_CENTER_TO_ORIGIN); } if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) { + print_line("Centering to selection. Times focused consecutively: ", times_focused_consecutively); _menu_option(VIEW_CENTER_TO_SELECTION); + times_focused_consecutively += 1; } if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", p_event)) { _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW); @@ -2519,6 +2521,9 @@ void Node3DEditorViewport::_nav_pan(Ref p_event, const translation *= cursor.distance / DISTANCE_DEFAULT; camera_transform.translate_local(translation); cursor.pos = camera_transform.origin; + + print_line("Pan: Resetting times focused consecutively to 0."); + times_focused_consecutively = 0; } void Node3DEditorViewport::_nav_zoom(Ref p_event, const Vector2 &p_relative) { @@ -2781,6 +2786,9 @@ void Node3DEditorViewport::_update_freelook(real_t delta) { const Vector3 motion = direction * speed * delta; cursor.pos += motion; cursor.eye_pos += motion; + + print_line("Freelook: Resetting times focused consecutively to 0."); + times_focused_consecutively = 0; } void Node3DEditorViewport::set_message(const String &p_message, float p_time) { @@ -2892,6 +2900,11 @@ void Node3DEditorViewport::_notification(int p_what) { _update_navigation_controls_visibility(); _update_freelook(delta); + if (times_focused_consecutively >= 2 && times_focused_consecutively % 2 == 0) { + print_line("[Follow] Centering to selection. Times focused consecutively: ", times_focused_consecutively); + _menu_option(VIEW_CENTER_TO_SELECTION); + } + Node *scene_root = SceneTreeDock::get_singleton()->get_editor_data()->get_edited_scene_root(); if (previewing_cinema && scene_root != nullptr) { Camera3D *cam = scene_root->get_viewport()->get_camera_3d(); diff --git a/editor/plugins/node_3d_editor_plugin.h b/editor/plugins/node_3d_editor_plugin.h index d35fcb765377..f6555b93425b 100644 --- a/editor/plugins/node_3d_editor_plugin.h +++ b/editor/plugins/node_3d_editor_plugin.h @@ -525,6 +525,8 @@ class Node3DEditorViewport : public Control { void focus_selection(); + int times_focused_consecutively = 0; + void assign_pending_data_pointers( Node3D *p_preview_node, AABB *p_preview_bounds,