Skip to content

Commit d9e1f5d

Browse files
aaronfrankekitbdev
andcommitted
Improve 2D/3D main screen auto-switching logic
Co-authored-by: Kit Bishop <[email protected]>
1 parent a8598cd commit d9e1f5d

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
@@ -2719,7 +2719,11 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update
27192719
SceneTreeDock::get_singleton()->set_selection({ current_node });
27202720
InspectorDock::get_singleton()->update(current_node);
27212721
if (!inspector_only && !skip_main_plugin) {
2722-
skip_main_plugin = stay_in_script_editor_on_node_selected && !ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree();
2722+
if (!ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree()) {
2723+
skip_main_plugin = stay_in_script_editor_on_node_selected;
2724+
} else {
2725+
skip_main_plugin = !editor_main_screen->can_auto_switch_screens();
2726+
}
27232727
}
27242728
} else {
27252729
NodeDock::get_singleton()->set_node(nullptr);
@@ -2798,9 +2802,7 @@ void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update
27982802
if (!changing_scene) {
27992803
main_plugin->edit(current_obj);
28002804
}
2801-
}
2802-
2803-
else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
2805+
} else if (main_plugin != editor_plugin_screen) {
28042806
// Unedit previous plugin.
28052807
editor_plugin_screen->edit(nullptr);
28062808
active_plugins[editor_owner_id].erase(editor_plugin_screen);
@@ -3998,18 +4000,15 @@ void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) {
39984000
changing_scene = false;
39994001

40004002
if (get_edited_scene()) {
4001-
int current_tab = editor_main_screen->get_selected_index();
4002-
if (current_tab < 2) {
4003+
if (editor_main_screen->can_auto_switch_screens()) {
40034004
// Switch between 2D and 3D if currently in 2D or 3D.
40044005
Node *selected_node = SceneTreeDock::get_singleton()->get_tree_editor()->get_selected();
40054006
if (!selected_node) {
40064007
selected_node = get_edited_scene();
40074008
}
4008-
4009-
if (Object::cast_to<CanvasItem>(selected_node)) {
4010-
editor_main_screen->select(EditorMainScreen::EDITOR_2D);
4011-
} else if (Object::cast_to<Node3D>(selected_node)) {
4012-
editor_main_screen->select(EditorMainScreen::EDITOR_3D);
4009+
const int plugin_index = editor_main_screen->get_plugin_index(editor_data.get_handling_main_editor(selected_node));
4010+
if (plugin_index >= 0) {
4011+
editor_main_screen->select(plugin_index);
40134012
}
40144013
}
40154014
}
@@ -7710,6 +7709,7 @@ EditorNode::EditorNode() {
77107709

77117710
HBoxContainer *main_editor_button_hb = memnew(HBoxContainer);
77127711
main_editor_button_hb->set_mouse_filter(Control::MOUSE_FILTER_STOP);
7712+
main_editor_button_hb->set_name("EditorMainScreenButtons");
77137713
editor_main_screen->set_button_container(main_editor_button_hb);
77147714
title_bar->add_child(main_editor_button_hb);
77157715
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
@@ -1849,15 +1849,6 @@ void ScriptEditor::_notification(int p_what) {
18491849
}
18501850
}
18511851

1852-
bool ScriptEditor::can_take_away_focus() const {
1853-
ScriptEditorBase *current = _get_current_editor();
1854-
if (current) {
1855-
return current->can_lose_focus_on_node_selection();
1856-
} else {
1857-
return true;
1858-
}
1859-
}
1860-
18611852
void ScriptEditor::_close_builtin_scripts_from_scene(const String &p_scene) {
18621853
for (int i = 0; i < tab_container->get_tab_count(); i++) {
18631854
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;
@@ -589,8 +588,6 @@ class ScriptEditor : public PanelContainer {
589588
void trigger_live_script_reload(const String &p_script_path);
590589
void trigger_live_script_reload_all();
591590

592-
bool can_take_away_focus() const;
593-
594591
VSplitContainer *get_left_list_split() { return list_split; }
595592

596593
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)