diff --git a/editor/scene/scene_create_dialog.cpp b/editor/scene/scene_create_dialog.cpp index 4ab9ab2ed7..30d20736fd 100644 --- a/editor/scene/scene_create_dialog.cpp +++ b/editor/scene/scene_create_dialog.cpp @@ -64,10 +64,10 @@ void SceneCreateDialog::_notification(int p_what) { } } -void SceneCreateDialog::config(const String &p_dir) { +void SceneCreateDialog::config(const String &p_dir, const String &p_scene_name) { directory = p_dir; root_name_edit->set_text(""); - scene_name_edit->set_text(""); + scene_name_edit->set_text(p_scene_name.get_basename()); callable_mp((Control *)scene_name_edit, &Control::grab_focus).call_deferred(false); validation_panel->update(); @@ -159,6 +159,10 @@ String SceneCreateDialog::get_scene_path() const { return scene_name; } +String SceneCreateDialog::get_root_name() const { + return root_name; +} + Node *SceneCreateDialog::create_scene_root() { ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr); RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int(); diff --git a/editor/scene/scene_create_dialog.h b/editor/scene/scene_create_dialog.h index 3252682ee8..68375eccbc 100644 --- a/editor/scene/scene_create_dialog.h +++ b/editor/scene/scene_create_dialog.h @@ -89,9 +89,10 @@ protected: void _notification(int p_what); public: - void config(const String &p_dir); + void config(const String &p_dir, const String &p_scene_name = ""); String get_scene_path() const; + String get_root_name() const; Node *create_scene_root(); SceneCreateDialog(); diff --git a/editor/settings/editor_autoload_settings.cpp b/editor/settings/editor_autoload_settings.cpp index 3174ba4975..3613125d55 100644 --- a/editor/settings/editor_autoload_settings.cpp +++ b/editor/settings/editor_autoload_settings.cpp @@ -39,7 +39,10 @@ #include "editor/editor_string_names.h" #include "editor/editor_undo_redo_manager.h" #include "editor/gui/editor_file_dialog.h" +#include "editor/scene/scene_create_dialog.h" #include "editor/settings/project_settings_editor.h" +#include "scene/gui/button.h" +#include "scene/gui/tree.h" #include "scene/main/window.h" #include "scene/resources/packed_scene.h" @@ -49,47 +52,23 @@ void EditorAutoloadSettings::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { List afn; - ResourceLoader::get_recognized_extensions_for_type("Script", &afn); ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn); for (const String &E : afn) { - file_dialog->add_filter("*." + E); + scene_file_dialog->add_filter("*." + E); } - browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder"))); - } break; + ResourceLoader::get_recognized_extensions_for_type("Script", &afn); - case NOTIFICATION_TRANSLATION_CHANGED: { - if (!error_message->get_text().is_empty()) { - _autoload_text_changed(autoload_add_name->get_text()); + for (const String &E : afn) { + autoload_file_dialog->add_filter("*." + E); } } break; case NOTIFICATION_THEME_CHANGED: { - browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder"))); - add_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add"))); - } break; - - case NOTIFICATION_VISIBILITY_CHANGED: { - FileSystemDock *dock = FileSystemDock::get_singleton(); - - if (dock != nullptr) { - ScriptCreateDialog *dialog = dock->get_script_create_dialog(); - - if (dialog != nullptr) { - Callable script_created = callable_mp(this, &EditorAutoloadSettings::_script_created); - - if (is_visible_in_tree()) { - if (!dialog->is_connected(SNAME("script_created"), script_created)) { - dialog->connect("script_created", script_created); - } - } else { - if (dialog->is_connected(SNAME("script_created"), script_created)) { - dialog->disconnect("script_created", script_created); - } - } - } - } + browse_button->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse"))); + create_script_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add"))); + create_scene_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add"))); } break; } } @@ -152,25 +131,6 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin return true; } -void EditorAutoloadSettings::_autoload_add() { - if (autoload_add_path->get_text().is_empty()) { - ScriptCreateDialog *dialog = FileSystemDock::get_singleton()->get_script_create_dialog(); - String fpath = path; - if (!fpath.ends_with("/")) { - fpath = fpath.get_base_dir(); - } - dialog->config("Node", fpath.path_join(vformat("%s.gd", autoload_add_name->get_text())), false, false); - dialog->popup_centered(); - } else { - if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_text())) { - autoload_add_path->set_text(""); - } - - autoload_add_name->set_text(""); - add_autoload->set_disabled(true); - } -} - void EditorAutoloadSettings::_autoload_selected() { TreeItem *ti = tree->get_selected(); @@ -287,9 +247,6 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); switch (p_button) { - case BUTTON_OPEN: { - _autoload_open(ti->get_text(1)); - } break; case BUTTON_MOVE_UP: case BUTTON_MOVE_DOWN: { TreeItem *swap = nullptr; @@ -359,36 +316,6 @@ void EditorAutoloadSettings::_autoload_open(const String &fpath) { ProjectSettingsEditor::get_singleton()->hide(); } -void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) { - // Convert the file name to PascalCase, which is the convention for classes in GDScript. - const String class_name = p_path.get_file().get_basename().to_pascal_case(); - - // If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name. - // The prefix is subjective, but it provides better UX than leaving the Add button disabled :) - const String prefix = ClassDB::class_exists(class_name) ? "Global" : ""; - - autoload_add_name->set_text(prefix + class_name); - add_autoload->set_disabled(false); -} - -void EditorAutoloadSettings::_autoload_text_submitted(const String &p_name) { - if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) { - _autoload_add(); - } -} - -void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) { - add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr)); -} - -void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) { - String error_string; - bool is_name_valid = _autoload_name_is_valid(p_name, &error_string); - add_autoload->set_disabled(!is_name_valid); - error_message->set_text(error_string); - error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid); -} - Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { Node *n = nullptr; if (ResourceLoader::get_resource_type(p_path) == "PackedScene") { @@ -426,6 +353,20 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { return n; } +void EditorAutoloadSettings::_create_script_autoload() { + if (!script_create_dialog) { + script_create_dialog = memnew(ScriptCreateDialog); + add_child(script_create_dialog); + script_create_dialog->connect("script_created", callable_mp(this, &EditorAutoloadSettings::_script_created)); + } + script_create_dialog->config("Node", "res://new_autoload_script.gd", false, false); + script_create_dialog->popup_centered(); +} + +void EditorAutoloadSettings::_create_scene_autoload() { + scene_file_dialog->set_current_file("new_autoload_scene.tscn"); + scene_file_dialog->popup_file_dialog(); +} void EditorAutoloadSettings::init_autoloads() { for (AutoloadInfo &info : autoload_cache) { @@ -458,6 +399,56 @@ void EditorAutoloadSettings::init_autoloads() { } } +void EditorAutoloadSettings::_autoload_file_selected(const String &p_path) { + _add_autoload(p_path.get_file().get_basename(), p_path); +} + +void EditorAutoloadSettings::_scene_file_selected(const String &p_path) { + if (!scene_create_dialog) { + scene_create_dialog = memnew(SceneCreateDialog); + add_child(scene_create_dialog); + scene_create_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorAutoloadSettings::_scene_created)); + } + scene_create_dialog->config(p_path.get_base_dir(), p_path.get_file().get_basename()); + scene_create_dialog->popup_centered(); +} + +void EditorAutoloadSettings::_script_created(Ref