From fa7ee5c37e1b8b9b16690c2dfa6c71b845363b56 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Wed, 9 Apr 2025 03:21:10 -0700 Subject: [PATCH] [4.3] Improve editor 2D/3D main screen auto-switching logic --- editor/editor_node.cpp | 72 +++++++++++++++---------- editor/editor_node.h | 2 + editor/plugins/script_editor_plugin.cpp | 9 ---- editor/plugins/script_editor_plugin.h | 3 -- editor/plugins/text_editor.h | 1 - 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 4cd3924a9cf3..4e720397484e 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2549,7 +2549,11 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update SceneTreeDock::get_singleton()->set_selection({ current_node }); InspectorDock::get_singleton()->update(current_node); if (!inspector_only && !skip_main_plugin) { - skip_main_plugin = stay_in_script_editor_on_node_selected && !ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree(); + if (!ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree()) { + skip_main_plugin = stay_in_script_editor_on_node_selected; + } else { + skip_main_plugin = !_can_auto_switch_main_screens(); + } } } else { NodeDock::get_singleton()->set_node(nullptr); @@ -2629,9 +2633,7 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update if (!changing_scene) { main_plugin->edit(current_obj); } - } - - else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) { + } else if (main_plugin != editor_plugin_screen) { // Unedit previous plugin. editor_plugin_screen->edit(nullptr); active_plugins[editor_owner_id].erase(editor_plugin_screen); @@ -3839,32 +3841,13 @@ void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) { changing_scene = false; - int current_tab = -1; - for (int i = 0; i < editor_table.size(); i++) { - if (editor_plugin_screen == editor_table[i]) { - current_tab = i; - break; - } - } - - if (p_state.has("editor_index")) { - int index = p_state["editor_index"]; - if (current_tab < 2) { // If currently in spatial/2d, only switch to spatial/2d. If currently in script, stay there. - if (index < 2 || !get_edited_scene()) { - editor_select(index); - } - } - } - if (get_edited_scene()) { - if (current_tab < 2) { - Node *editor_node = SceneTreeDock::get_singleton()->get_tree_editor()->get_selected(); - editor_node = editor_node == nullptr ? get_edited_scene() : editor_node; - - if (Object::cast_to(editor_node)) { - editor_select(EDITOR_2D); - } else if (Object::cast_to(editor_node)) { - editor_select(EDITOR_3D); + if (_can_auto_switch_main_screens()) { + Node *selected_node = SceneTreeDock::get_singleton()->get_tree_editor()->get_selected(); + selected_node = selected_node == nullptr ? get_edited_scene() : selected_node; + const int plugin_index = _get_plugin_index(editor_data.get_handling_main_editor(selected_node)); + if (plugin_index >= 0) { + editor_select(plugin_index); } } } @@ -3891,6 +3874,36 @@ void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) { RenderingServer::get_singleton()->sdfgi_reset(); } +bool EditorNode::_can_auto_switch_main_screens() const { + if (editor_plugin_screen == nullptr) { + return true; + } + // Only allow auto-switching if the selected button is to the left of the Script button. + for (int i = 0; i < main_editor_button_hb->get_child_count(); i++) { + Button *button = Object::cast_to