Skip to content

Commit ad99c79

Browse files
committed
Rework creating new folders in editor
1 parent a75bace commit ad99c79

File tree

6 files changed

+53
-52
lines changed

6 files changed

+53
-52
lines changed

editor/directory_create_dialog.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "directory_create_dialog.h"
3232

3333
#include "core/io/dir_access.h"
34+
#include "editor/editor_file_system.h"
3435
#include "editor/editor_node.h"
3536
#include "editor/gui/editor_validation_panel.h"
3637
#include "editor/themes/editor_scale.h"
@@ -100,15 +101,7 @@ void DirectoryCreateDialog::ok_pressed() {
100101
const String error = _validate_path(path);
101102
ERR_FAIL_COND_MSG(!error.is_empty(), error);
102103

103-
Error err;
104-
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
105-
106-
err = da->change_dir(base_dir);
107-
ERR_FAIL_COND_MSG(err != OK, "Cannot open directory '" + base_dir + "'.");
108-
109-
print_verbose("Making folder " + path + " in " + base_dir);
110-
err = da->make_dir_recursive(path);
111-
104+
Error err = EditorFileSystem::get_singleton()->make_dir_recursive(path, base_dir);
112105
if (err == OK) {
113106
emit_signal(SNAME("dir_created"), base_dir.path_join(path));
114107
} else {

editor/editor_file_system.cpp

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/config/project_settings.h"
3434
#include "core/extension/gdextension_manager.h"
35+
#include "core/io/dir_access.h"
3536
#include "core/io/file_access.h"
3637
#include "core/io/resource_saver.h"
3738
#include "core/object/worker_thread_pool.h"
@@ -3067,33 +3068,51 @@ void EditorFileSystem::move_group_file(const String &p_path, const String &p_new
30673068
}
30683069
}
30693070

3070-
void EditorFileSystem::add_new_directory(const String &p_path) {
3071-
String path = p_path.get_base_dir();
3072-
EditorFileSystemDirectory *parent = filesystem;
3073-
int base = p_path.count("/");
3074-
int max_bit = base + 1;
3071+
Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
3072+
Error err;
3073+
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
3074+
if (!p_base_path.is_empty()) {
3075+
err = da->change_dir(p_base_path);
3076+
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
3077+
}
30753078

3076-
while (path != "res://") {
3077-
EditorFileSystemDirectory *dir = get_filesystem_path(path);
3078-
if (dir) {
3079-
parent = dir;
3080-
break;
3081-
}
3082-
path = path.get_base_dir();
3083-
base--;
3079+
if (da->dir_exists(p_path)) {
3080+
return ERR_ALREADY_EXISTS;
3081+
}
3082+
3083+
err = da->make_dir_recursive(p_path);
3084+
if (err != OK) {
3085+
return err;
30843086
}
30853087

3086-
for (int i = base; i < max_bit; i++) {
3088+
const String path = da->get_current_dir();
3089+
EditorFileSystemDirectory *parent = get_filesystem_path(path);
3090+
ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
3091+
3092+
const PackedStringArray folders = p_path.trim_prefix(path).trim_suffix("/").split("/");
3093+
bool first = true;
3094+
3095+
for (const String &folder : folders) {
3096+
const int current = parent->find_dir_index(folder);
3097+
if (current > -1) {
3098+
parent = parent->get_subdir(current);
3099+
continue;
3100+
}
3101+
30873102
EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
30883103
efd->parent = parent;
3089-
efd->name = p_path.get_slice("/", i);
3104+
efd->name = folder;
30903105
parent->subdirs.push_back(efd);
30913106

3092-
if (i == base) {
3107+
if (first) {
30933108
parent->subdirs.sort_custom<DirectoryComparator>();
3109+
first = false;
30943110
}
30953111
parent = efd;
30963112
}
3113+
3114+
emit_signal(SNAME("filesystem_changed"));
3115+
return OK;
30973116
}
30983117

30993118
ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {

editor/editor_file_system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class EditorFileSystem : public Node {
370370
bool is_group_file(const String &p_path) const;
371371
void move_group_file(const String &p_path, const String &p_new_path);
372372

373-
void add_new_directory(const String &p_path);
373+
Error make_dir_recursive(const String &p_path, const String &p_base_path = String());
374374

375375
static bool _should_skip_directory(const String &p_path);
376376

editor/filesystem_dock.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,7 @@ void FileSystemDock::_notification(int p_what) {
647647
}
648648

649649
if (do_redraw) {
650-
_update_file_list(true);
651-
_update_tree(get_uncollapsed_paths());
650+
update_all();
652651
}
653652

654653
if (EditorThemeManager::is_generated_theme_outdated()) {
@@ -1300,13 +1299,7 @@ void FileSystemDock::_fs_changed() {
13001299
scanning_vb->hide();
13011300
split_box->show();
13021301

1303-
if (tree->is_visible()) {
1304-
_update_tree(get_uncollapsed_paths());
1305-
}
1306-
1307-
if (file_list_vb->is_visible()) {
1308-
_update_file_list(true);
1309-
}
1302+
update_all();
13101303

13111304
if (!select_after_scan.is_empty()) {
13121305
_navigate_to_path(select_after_scan);
@@ -1318,15 +1311,6 @@ void FileSystemDock::_fs_changed() {
13181311
set_process(false);
13191312
}
13201313

1321-
void FileSystemDock::_directory_created(const String &p_path) {
1322-
if (!DirAccess::exists(p_path)) {
1323-
return;
1324-
}
1325-
EditorFileSystem::get_singleton()->add_new_directory(p_path);
1326-
_update_tree(get_uncollapsed_paths());
1327-
_update_file_list(true);
1328-
}
1329-
13301314
void FileSystemDock::_set_scanning_mode() {
13311315
button_hist_prev->set_disabled(true);
13321316
button_hist_next->set_disabled(true);
@@ -2697,6 +2681,16 @@ void FileSystemDock::fix_dependencies(const String &p_for_file) {
26972681
deps_editor->edit(p_for_file);
26982682
}
26992683

2684+
void FileSystemDock::update_all() {
2685+
if (tree->is_visible()) {
2686+
_update_tree(get_uncollapsed_paths());
2687+
}
2688+
2689+
if (file_list_vb->is_visible()) {
2690+
_update_file_list(true);
2691+
}
2692+
}
2693+
27002694
void FileSystemDock::focus_on_path() {
27012695
current_path_line_edit->grab_focus();
27022696
current_path_line_edit->select_all();
@@ -3118,9 +3112,7 @@ void FileSystemDock::_folder_color_index_pressed(int p_index, PopupMenu *p_menu)
31183112
}
31193113

31203114
_update_folder_colors_setting();
3121-
3122-
_update_tree(get_uncollapsed_paths());
3123-
_update_file_list(true);
3115+
update_all();
31243116

31253117
emit_signal(SNAME("folder_color_changed"));
31263118
}
@@ -3793,8 +3785,7 @@ void FileSystemDock::set_file_sort(FileSortOption p_file_sort) {
37933785
file_sort = p_file_sort;
37943786

37953787
// Update everything needed.
3796-
_update_tree(get_uncollapsed_paths());
3797-
_update_file_list(true);
3788+
update_all();
37983789
}
37993790

38003791
void FileSystemDock::_file_sort_popup(int p_id) {
@@ -4184,7 +4175,6 @@ FileSystemDock::FileSystemDock() {
41844175

41854176
make_dir_dialog = memnew(DirectoryCreateDialog);
41864177
add_child(make_dir_dialog);
4187-
make_dir_dialog->connect("dir_created", callable_mp(this, &FileSystemDock::_directory_created));
41884178

41894179
make_scene_dialog = memnew(SceneCreateDialog);
41904180
add_child(make_scene_dialog);

editor/filesystem_dock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ class FileSystemDock : public VBoxContainer {
260260
void _toggle_file_display();
261261
void _set_file_display(bool p_active);
262262
void _fs_changed();
263-
void _directory_created(const String &p_path);
264263

265264
void _select_file(const String &p_path, bool p_select_in_favorites = false);
266265
void _tree_activate_file();
@@ -405,6 +404,7 @@ class FileSystemDock : public VBoxContainer {
405404
ScriptCreateDialog *get_script_create_dialog() const;
406405

407406
void fix_dependencies(const String &p_for_file);
407+
void update_all();
408408

409409
int get_h_split_offset() const { return split_box_offset_h; }
410410
void set_h_split_offset(int p_offset) { split_box_offset_h = p_offset; }

editor/gui/editor_dir_dialog.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ void EditorDirDialog::_make_dir_confirm(const String &p_path) {
191191
}
192192

193193
new_dir_path = p_path + "/";
194-
EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes.
195194
}
196195

197196
void EditorDirDialog::_bind_methods() {

0 commit comments

Comments
 (0)