Skip to content

Commit ff2b5a5

Browse files
committed
Merge pull request #97075 from KoBeWi/better_new_folder_(not_to_be_confused_with_new_better_folder)
Rework creating new folders in editor
2 parents ae872a4 + ad99c79 commit ff2b5a5

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"
@@ -3075,33 +3076,51 @@ void EditorFileSystem::move_group_file(const String &p_path, const String &p_new
30753076
}
30763077
}
30773078

3078-
void EditorFileSystem::add_new_directory(const String &p_path) {
3079-
String path = p_path.get_base_dir();
3080-
EditorFileSystemDirectory *parent = filesystem;
3081-
int base = p_path.count("/");
3082-
int max_bit = base + 1;
3079+
Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
3080+
Error err;
3081+
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
3082+
if (!p_base_path.is_empty()) {
3083+
err = da->change_dir(p_base_path);
3084+
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
3085+
}
30833086

3084-
while (path != "res://") {
3085-
EditorFileSystemDirectory *dir = get_filesystem_path(path);
3086-
if (dir) {
3087-
parent = dir;
3088-
break;
3089-
}
3090-
path = path.get_base_dir();
3091-
base--;
3087+
if (da->dir_exists(p_path)) {
3088+
return ERR_ALREADY_EXISTS;
3089+
}
3090+
3091+
err = da->make_dir_recursive(p_path);
3092+
if (err != OK) {
3093+
return err;
30923094
}
30933095

3094-
for (int i = base; i < max_bit; i++) {
3096+
const String path = da->get_current_dir();
3097+
EditorFileSystemDirectory *parent = get_filesystem_path(path);
3098+
ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
3099+
3100+
const PackedStringArray folders = p_path.trim_prefix(path).trim_suffix("/").split("/");
3101+
bool first = true;
3102+
3103+
for (const String &folder : folders) {
3104+
const int current = parent->find_dir_index(folder);
3105+
if (current > -1) {
3106+
parent = parent->get_subdir(current);
3107+
continue;
3108+
}
3109+
30953110
EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
30963111
efd->parent = parent;
3097-
efd->name = p_path.get_slice("/", i);
3112+
efd->name = folder;
30983113
parent->subdirs.push_back(efd);
30993114

3100-
if (i == base) {
3115+
if (first) {
31013116
parent->subdirs.sort_custom<DirectoryComparator>();
3117+
first = false;
31023118
}
31033119
parent = efd;
31043120
}
3121+
3122+
emit_signal(SNAME("filesystem_changed"));
3123+
return OK;
31053124
}
31063125

31073126
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
@@ -649,8 +649,7 @@ void FileSystemDock::_notification(int p_what) {
649649
}
650650

651651
if (do_redraw) {
652-
_update_file_list(true);
653-
_update_tree(get_uncollapsed_paths());
652+
update_all();
654653
}
655654

656655
if (EditorThemeManager::is_generated_theme_outdated()) {
@@ -1343,13 +1342,7 @@ void FileSystemDock::_fs_changed() {
13431342
scanning_vb->hide();
13441343
split_box->show();
13451344

1346-
if (tree->is_visible()) {
1347-
_update_tree(get_uncollapsed_paths());
1348-
}
1349-
1350-
if (file_list_vb->is_visible()) {
1351-
_update_file_list(true);
1352-
}
1345+
update_all();
13531346

13541347
if (!select_after_scan.is_empty()) {
13551348
_navigate_to_path(select_after_scan);
@@ -1361,15 +1354,6 @@ void FileSystemDock::_fs_changed() {
13611354
set_process(false);
13621355
}
13631356

1364-
void FileSystemDock::_directory_created(const String &p_path) {
1365-
if (!DirAccess::exists(p_path)) {
1366-
return;
1367-
}
1368-
EditorFileSystem::get_singleton()->add_new_directory(p_path);
1369-
_update_tree(get_uncollapsed_paths());
1370-
_update_file_list(true);
1371-
}
1372-
13731357
void FileSystemDock::_set_scanning_mode() {
13741358
button_hist_prev->set_disabled(true);
13751359
button_hist_next->set_disabled(true);
@@ -2820,6 +2804,16 @@ void FileSystemDock::fix_dependencies(const String &p_for_file) {
28202804
deps_editor->edit(p_for_file);
28212805
}
28222806

2807+
void FileSystemDock::update_all() {
2808+
if (tree->is_visible()) {
2809+
_update_tree(get_uncollapsed_paths());
2810+
}
2811+
2812+
if (file_list_vb->is_visible()) {
2813+
_update_file_list(true);
2814+
}
2815+
}
2816+
28232817
void FileSystemDock::focus_on_path() {
28242818
current_path_line_edit->grab_focus();
28252819
current_path_line_edit->select_all();
@@ -3241,9 +3235,7 @@ void FileSystemDock::_folder_color_index_pressed(int p_index, PopupMenu *p_menu)
32413235
}
32423236

32433237
_update_folder_colors_setting();
3244-
3245-
_update_tree(get_uncollapsed_paths());
3246-
_update_file_list(true);
3238+
update_all();
32473239

32483240
emit_signal(SNAME("folder_color_changed"));
32493241
}
@@ -3951,8 +3943,7 @@ void FileSystemDock::set_file_sort(FileSortOption p_file_sort) {
39513943
file_sort = p_file_sort;
39523944

39533945
// Update everything needed.
3954-
_update_tree(get_uncollapsed_paths());
3955-
_update_file_list(true);
3946+
update_all();
39563947
}
39573948

39583949
void FileSystemDock::_file_sort_popup(int p_id) {
@@ -4342,7 +4333,6 @@ FileSystemDock::FileSystemDock() {
43424333

43434334
make_dir_dialog = memnew(DirectoryCreateDialog);
43444335
add_child(make_dir_dialog);
4345-
make_dir_dialog->connect("dir_created", callable_mp(this, &FileSystemDock::_directory_created));
43464336

43474337
make_scene_dialog = memnew(SceneCreateDialog);
43484338
add_child(make_scene_dialog);

editor/filesystem_dock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ class FileSystemDock : public VBoxContainer {
270270
void _toggle_file_display();
271271
void _set_file_display(bool p_active);
272272
void _fs_changed();
273-
void _directory_created(const String &p_path);
274273

275274
void _select_file(const String &p_path, bool p_select_in_favorites = false);
276275
void _tree_activate_file();
@@ -417,6 +416,7 @@ class FileSystemDock : public VBoxContainer {
417416
ScriptCreateDialog *get_script_create_dialog() const;
418417

419418
void fix_dependencies(const String &p_for_file);
419+
void update_all();
420420

421421
int get_h_split_offset() const { return split_box_offset_h; }
422422
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)