Skip to content

Commit

Permalink
[WIP] Add "Follow Selection" in the 3D editor by using Center Selecti…
Browse files Browse the repository at this point in the history
…on 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.
  • Loading branch information
Calinou committed Nov 21, 2024
1 parent 9e60984 commit e36131a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2364,7 +2364,9 @@ void Node3DEditorViewport::_sinput(const Ref<InputEvent> &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);
Expand Down Expand Up @@ -2519,6 +2521,9 @@ void Node3DEditorViewport::_nav_pan(Ref<InputEventWithModifiers> 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<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e36131a

Please sign in to comment.