Skip to content

Commit 67b3db8

Browse files
committed
Merge pull request godotengine#104010 from aaronfranke/main-screen-auto-switch
Improve editor 2D/3D main screen auto-switching logic
2 parents fb39aa4 + d9e1f5d commit 67b3db8

File tree

6 files changed

+31
-24
lines changed

6 files changed

+31
-24
lines changed

editor/editor_main_screen.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,25 @@ EditorPlugin *EditorMainScreen::get_plugin_by_name(const String &p_plugin_name)
233233
return main_editor_plugins[p_plugin_name];
234234
}
235235

236+
bool EditorMainScreen::can_auto_switch_screens() const {
237+
if (selected_plugin == nullptr) {
238+
return true;
239+
}
240+
// Only allow auto-switching if the selected button is to the left of the Script button.
241+
for (int i = 0; i < button_hb->get_child_count(); i++) {
242+
Button *button = Object::cast_to<Button>(button_hb->get_child(i));
243+
if (button->get_text() == "Script") {
244+
// Selected button is at or after the Script button.
245+
return false;
246+
}
247+
if (button->get_text() == selected_plugin->get_plugin_name()) {
248+
// Selected button is before the Script button.
249+
return true;
250+
}
251+
}
252+
return false;
253+
}
254+
236255
VBoxContainer *EditorMainScreen::get_control() const {
237256
return main_screen_vbox;
238257
}

editor/editor_main_screen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class EditorMainScreen : public PanelContainer {
8181
int get_plugin_index(EditorPlugin *p_editor) const;
8282
EditorPlugin *get_selected_plugin() const;
8383
EditorPlugin *get_plugin_by_name(const String &p_plugin_name) const;
84+
bool can_auto_switch_screens() const;
8485

8586
VBoxContainer *get_control() const;
8687

editor/editor_node.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,11 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update
27332733
SceneTreeDock::get_singleton()->set_selection({ current_node });
27342734
InspectorDock::get_singleton()->update(current_node);
27352735
if (!inspector_only && !skip_main_plugin) {
2736-
skip_main_plugin = stay_in_script_editor_on_node_selected && !ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree();
2736+
if (!ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree()) {
2737+
skip_main_plugin = stay_in_script_editor_on_node_selected;
2738+
} else {
2739+
skip_main_plugin = !editor_main_screen->can_auto_switch_screens();
2740+
}
27372741
}
27382742
} else {
27392743
NodeDock::get_singleton()->set_node(nullptr);
@@ -2812,9 +2816,7 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update
28122816
if (!changing_scene) {
28132817
main_plugin->edit(current_obj);
28142818
}
2815-
}
2816-
2817-
else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
2819+
} else if (main_plugin != editor_plugin_screen) {
28182820
// Unedit previous plugin.
28192821
editor_plugin_screen->edit(nullptr);
28202822
active_plugins[editor_owner_id].erase(editor_plugin_screen);
@@ -4021,18 +4023,15 @@ void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) {
40214023
changing_scene = false;
40224024

40234025
if (get_edited_scene()) {
4024-
int current_tab = editor_main_screen->get_selected_index();
4025-
if (current_tab < 2) {
4026+
if (editor_main_screen->can_auto_switch_screens()) {
40264027
// Switch between 2D and 3D if currently in 2D or 3D.
40274028
Node *selected_node = SceneTreeDock::get_singleton()->get_tree_editor()->get_selected();
40284029
if (!selected_node) {
40294030
selected_node = get_edited_scene();
40304031
}
4031-
4032-
if (Object::cast_to<CanvasItem>(selected_node)) {
4033-
editor_main_screen->select(EditorMainScreen::EDITOR_2D);
4034-
} else if (Object::cast_to<Node3D>(selected_node)) {
4035-
editor_main_screen->select(EditorMainScreen::EDITOR_3D);
4032+
const int plugin_index = editor_main_screen->get_plugin_index(editor_data.get_handling_main_editor(selected_node));
4033+
if (plugin_index >= 0) {
4034+
editor_main_screen->select(plugin_index);
40364035
}
40374036
}
40384037
}
@@ -7904,6 +7903,7 @@ EditorNode::EditorNode() {
79047903

79057904
HBoxContainer *main_editor_button_hb = memnew(HBoxContainer);
79067905
main_editor_button_hb->set_mouse_filter(Control::MOUSE_FILTER_STOP);
7906+
main_editor_button_hb->set_name("EditorMainScreenButtons");
79077907
editor_main_screen->set_button_container(main_editor_button_hb);
79087908
title_bar->add_child(main_editor_button_hb);
79097909
title_bar->set_center_control(main_editor_button_hb);

editor/plugins/script_editor_plugin.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,15 +1810,6 @@ void ScriptEditor::_notification(int p_what) {
18101810
}
18111811
}
18121812

1813-
bool ScriptEditor::can_take_away_focus() const {
1814-
ScriptEditorBase *current = _get_current_editor();
1815-
if (current) {
1816-
return current->can_lose_focus_on_node_selection();
1817-
} else {
1818-
return true;
1819-
}
1820-
}
1821-
18221813
void ScriptEditor::_close_builtin_scripts_from_scene(const String &p_scene) {
18231814
for (int i = 0; i < tab_container->get_tab_count(); i++) {
18241815
ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));

editor/plugins/script_editor_plugin.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ class ScriptEditorBase : public VBoxContainer {
208208
virtual void add_callback(const String &p_function, const PackedStringArray &p_args) = 0;
209209
virtual void update_settings() = 0;
210210
virtual void set_debugger_active(bool p_active) = 0;
211-
virtual bool can_lose_focus_on_node_selection() { return true; }
212211
virtual void update_toggle_files_button() {}
213212

214213
virtual bool show_members_overview() = 0;
@@ -598,8 +597,6 @@ class ScriptEditor : public PanelContainer {
598597
void trigger_live_script_reload(const String &p_script_path);
599598
void trigger_live_script_reload_all();
600599

601-
bool can_take_away_focus() const;
602-
603600
VSplitContainer *get_left_list_split() { return list_split; }
604601

605602
void set_live_auto_reload_running_scripts(bool p_enabled);

editor/plugins/text_editor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class TextEditor : public ScriptEditorBase {
141141
virtual void tag_saved_version() override;
142142
virtual void update_settings() override;
143143
virtual bool show_members_overview() override;
144-
virtual bool can_lose_focus_on_node_selection() override { return true; }
145144
virtual void set_debugger_active(bool p_active) override;
146145
virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
147146
virtual void add_callback(const String &p_function, const PackedStringArray &p_args) override;

0 commit comments

Comments
 (0)