From ad99c7947274685b8e3acc3f1ba3f18f66b1f769 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 16 Sep 2024 15:52:51 +0200 Subject: [PATCH] Rework creating new folders in editor --- editor/directory_create_dialog.cpp | 11 ++----- editor/editor_file_system.cpp | 53 ++++++++++++++++++++---------- editor/editor_file_system.h | 2 +- editor/filesystem_dock.cpp | 38 ++++++++------------- editor/filesystem_dock.h | 2 +- editor/gui/editor_dir_dialog.cpp | 1 - 6 files changed, 54 insertions(+), 53 deletions(-) diff --git a/editor/directory_create_dialog.cpp b/editor/directory_create_dialog.cpp index 604531f109d..46baa2c6e1f 100644 --- a/editor/directory_create_dialog.cpp +++ b/editor/directory_create_dialog.cpp @@ -31,6 +31,7 @@ #include "directory_create_dialog.h" #include "core/io/dir_access.h" +#include "editor/editor_file_system.h" #include "editor/editor_node.h" #include "editor/gui/editor_validation_panel.h" #include "editor/themes/editor_scale.h" @@ -100,15 +101,7 @@ void DirectoryCreateDialog::ok_pressed() { const String error = _validate_path(path); ERR_FAIL_COND_MSG(!error.is_empty(), error); - Error err; - Ref da = DirAccess::create(DirAccess::ACCESS_RESOURCES); - - err = da->change_dir(base_dir); - ERR_FAIL_COND_MSG(err != OK, "Cannot open directory '" + base_dir + "'."); - - print_verbose("Making folder " + path + " in " + base_dir); - err = da->make_dir_recursive(path); - + Error err = EditorFileSystem::get_singleton()->make_dir_recursive(path, base_dir); if (err == OK) { emit_signal(SNAME("dir_created"), base_dir.path_join(path)); } else { diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 87053acfb61..f168deec04b 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/extension/gdextension_manager.h" +#include "core/io/dir_access.h" #include "core/io/file_access.h" #include "core/io/resource_saver.h" #include "core/object/worker_thread_pool.h" @@ -3067,33 +3068,51 @@ void EditorFileSystem::move_group_file(const String &p_path, const String &p_new } } -void EditorFileSystem::add_new_directory(const String &p_path) { - String path = p_path.get_base_dir(); - EditorFileSystemDirectory *parent = filesystem; - int base = p_path.count("/"); - int max_bit = base + 1; - - while (path != "res://") { - EditorFileSystemDirectory *dir = get_filesystem_path(path); - if (dir) { - parent = dir; - break; - } - path = path.get_base_dir(); - base--; +Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) { + Error err; + Ref da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + if (!p_base_path.is_empty()) { + err = da->change_dir(p_base_path); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'."); } - for (int i = base; i < max_bit; i++) { + if (da->dir_exists(p_path)) { + return ERR_ALREADY_EXISTS; + } + + err = da->make_dir_recursive(p_path); + if (err != OK) { + return err; + } + + const String path = da->get_current_dir(); + EditorFileSystemDirectory *parent = get_filesystem_path(path); + ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND); + + const PackedStringArray folders = p_path.trim_prefix(path).trim_suffix("/").split("/"); + bool first = true; + + for (const String &folder : folders) { + const int current = parent->find_dir_index(folder); + if (current > -1) { + parent = parent->get_subdir(current); + continue; + } + EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory); efd->parent = parent; - efd->name = p_path.get_slice("/", i); + efd->name = folder; parent->subdirs.push_back(efd); - if (i == base) { + if (first) { parent->subdirs.sort_custom(); + first = false; } parent = efd; } + + emit_signal(SNAME("filesystem_changed")); + return OK; } ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) { diff --git a/editor/editor_file_system.h b/editor/editor_file_system.h index be299800d8b..e02127cb13c 100644 --- a/editor/editor_file_system.h +++ b/editor/editor_file_system.h @@ -370,7 +370,7 @@ public: bool is_group_file(const String &p_path) const; void move_group_file(const String &p_path, const String &p_new_path); - void add_new_directory(const String &p_path); + Error make_dir_recursive(const String &p_path, const String &p_base_path = String()); static bool _should_skip_directory(const String &p_path); diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index faaab4aeeca..d31a5372ba8 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -647,8 +647,7 @@ void FileSystemDock::_notification(int p_what) { } if (do_redraw) { - _update_file_list(true); - _update_tree(get_uncollapsed_paths()); + update_all(); } if (EditorThemeManager::is_generated_theme_outdated()) { @@ -1300,13 +1299,7 @@ void FileSystemDock::_fs_changed() { scanning_vb->hide(); split_box->show(); - if (tree->is_visible()) { - _update_tree(get_uncollapsed_paths()); - } - - if (file_list_vb->is_visible()) { - _update_file_list(true); - } + update_all(); if (!select_after_scan.is_empty()) { _navigate_to_path(select_after_scan); @@ -1318,15 +1311,6 @@ void FileSystemDock::_fs_changed() { set_process(false); } -void FileSystemDock::_directory_created(const String &p_path) { - if (!DirAccess::exists(p_path)) { - return; - } - EditorFileSystem::get_singleton()->add_new_directory(p_path); - _update_tree(get_uncollapsed_paths()); - _update_file_list(true); -} - void FileSystemDock::_set_scanning_mode() { button_hist_prev->set_disabled(true); button_hist_next->set_disabled(true); @@ -2697,6 +2681,16 @@ void FileSystemDock::fix_dependencies(const String &p_for_file) { deps_editor->edit(p_for_file); } +void FileSystemDock::update_all() { + if (tree->is_visible()) { + _update_tree(get_uncollapsed_paths()); + } + + if (file_list_vb->is_visible()) { + _update_file_list(true); + } +} + void FileSystemDock::focus_on_path() { current_path_line_edit->grab_focus(); current_path_line_edit->select_all(); @@ -3118,9 +3112,7 @@ void FileSystemDock::_folder_color_index_pressed(int p_index, PopupMenu *p_menu) } _update_folder_colors_setting(); - - _update_tree(get_uncollapsed_paths()); - _update_file_list(true); + update_all(); emit_signal(SNAME("folder_color_changed")); } @@ -3793,8 +3785,7 @@ void FileSystemDock::set_file_sort(FileSortOption p_file_sort) { file_sort = p_file_sort; // Update everything needed. - _update_tree(get_uncollapsed_paths()); - _update_file_list(true); + update_all(); } void FileSystemDock::_file_sort_popup(int p_id) { @@ -4184,7 +4175,6 @@ FileSystemDock::FileSystemDock() { make_dir_dialog = memnew(DirectoryCreateDialog); add_child(make_dir_dialog); - make_dir_dialog->connect("dir_created", callable_mp(this, &FileSystemDock::_directory_created)); make_scene_dialog = memnew(SceneCreateDialog); add_child(make_scene_dialog); diff --git a/editor/filesystem_dock.h b/editor/filesystem_dock.h index 907f843523e..3f30a3fcdd0 100644 --- a/editor/filesystem_dock.h +++ b/editor/filesystem_dock.h @@ -260,7 +260,6 @@ private: void _toggle_file_display(); void _set_file_display(bool p_active); void _fs_changed(); - void _directory_created(const String &p_path); void _select_file(const String &p_path, bool p_select_in_favorites = false); void _tree_activate_file(); @@ -405,6 +404,7 @@ public: ScriptCreateDialog *get_script_create_dialog() const; void fix_dependencies(const String &p_for_file); + void update_all(); int get_h_split_offset() const { return split_box_offset_h; } void set_h_split_offset(int p_offset) { split_box_offset_h = p_offset; } diff --git a/editor/gui/editor_dir_dialog.cpp b/editor/gui/editor_dir_dialog.cpp index e64761d5b52..b677ba10988 100644 --- a/editor/gui/editor_dir_dialog.cpp +++ b/editor/gui/editor_dir_dialog.cpp @@ -191,7 +191,6 @@ void EditorDirDialog::_make_dir_confirm(const String &p_path) { } new_dir_path = p_path + "/"; - EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes. } void EditorDirDialog::_bind_methods() {