Ported: Make create folder popup support nested folders

- timothyqiu
d6672d8fe4
This commit is contained in:
Relintai 2023-05-01 12:58:21 +02:00
parent c850fde373
commit a1f241f63b
4 changed files with 257 additions and 49 deletions

View File

@ -0,0 +1,178 @@
/**************************************************************************/
/* directory_create_dialog.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "directory_create_dialog.h"
#include "core/os/dir_access.h"
#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/label.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/panel_container.h"
static String sanitize_input(const String &p_path) {
String path = p_path.strip_edges();
if (path.ends_with("/")) {
path = path.left(path.length() - 1);
}
return path;
}
String DirectoryCreateDialog::_validate_path(const String &p_path) const {
if (p_path.empty()) {
return TTR("Folder name cannot be empty.");
}
const Vector<String> parts = p_path.split("/");
for (int i = 0; i < parts.size(); i++) {
const String part = parts[i];
if (part.empty()) {
return TTR("Folder name cannot be empty.");
}
if (p_path.find("\\") != -1 || p_path.find(":") != -1 || p_path.find("*") != -1 ||
p_path.find("|") != -1 || p_path.find(">") != -1 || p_path.ends_with(".") || p_path.ends_with(" ")) {
return TTR("Folder name contains invalid characters.");
}
}
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
da->change_dir(base_dir);
if (da->file_exists(p_path)) {
return TTR("File with that name already exists.");
}
if (da->dir_exists(p_path)) {
return TTR("Folder with that name already exists.");
}
return String();
}
void DirectoryCreateDialog::_on_dir_path_changed(const String &p_text) {
const String path = sanitize_input(p_text);
const String error = _validate_path(path);
if (error.empty()) {
status_label->add_theme_color_override("font_color", get_theme_color("success_color", "Editor"));
if (path.find("/") != -1) {
status_label->set_text(TTR("Using slashes in folder names will create subfolders recursively."));
} else {
status_label->set_text(TTR("Folder name is valid."));
}
} else {
status_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
status_label->set_text(error);
}
get_ok()->set_disabled(!error.empty());
}
void DirectoryCreateDialog::ok_pressed() {
const String path = sanitize_input(dir_path->get_text());
// The OK button should be disabled if the path is invalid, but just in case.
const String error = _validate_path(path);
ERR_FAIL_COND_MSG(!error.empty(), error);
Error err;
DirAccessRef 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);
if (err == OK) {
emit_signal("dir_created");
} else {
EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
}
hide();
}
void DirectoryCreateDialog::_post_popup() {
ConfirmationDialog::_post_popup();
label->set_text(vformat(TTR("Create new folder in %s:"), base_dir));
minimum_size_changed();
dir_path->grab_focus();
}
void DirectoryCreateDialog::config(const String &p_base_dir) {
base_dir = p_base_dir;
label->set_text(""); // Set the correct text later in _post_popup to avoid GH-47005.
dir_path->set_text("new folder");
dir_path->select_all();
_on_dir_path_changed(dir_path->get_text());
}
void DirectoryCreateDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_dir_path_changed"), &DirectoryCreateDialog::_on_dir_path_changed);
ADD_SIGNAL(MethodInfo("dir_created"));
}
void DirectoryCreateDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
status_panel->add_theme_style_override("panel", get_theme_stylebox("bg", "Tree"));
} break;
}
}
DirectoryCreateDialog::DirectoryCreateDialog() {
set_title(TTR("Create Folder"));
set_custom_minimum_size(Size2i(480, 0) * EDSCALE);
VBoxContainer *vb = memnew(VBoxContainer);
add_child(vb);
label = memnew(Label);
label->set_autowrap(true);
vb->add_child(label);
dir_path = memnew(LineEdit);
dir_path->connect("text_changed", this, "_on_dir_path_changed");
vb->add_child(dir_path);
register_text_enter(dir_path);
Control *spacing = memnew(Control);
spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
vb->add_child(spacing);
status_panel = memnew(PanelContainer);
status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
vb->add_child(status_panel);
status_label = memnew(Label);
status_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
status_panel->add_child(status_label);
}

View File

@ -0,0 +1,68 @@
#ifndef DIRECTORY_CREATE_DIALOG_H
#define DIRECTORY_CREATE_DIALOG_H
/**************************************************************************/
/* directory_create_dialog.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "scene/gui/dialogs.h"
class Label;
class LineEdit;
class PanelContainer;
class DirectoryCreateDialog : public ConfirmationDialog {
GDCLASS(DirectoryCreateDialog, ConfirmationDialog);
String base_dir;
Label *label = nullptr;
LineEdit *dir_path = nullptr;
PanelContainer *status_panel = nullptr;
Label *status_label = nullptr;
String _validate_path(const String &p_path) const;
void _on_dir_path_changed(const String &p_text);
protected:
static void _bind_methods();
void _notification(int p_what);
virtual void ok_pressed();
virtual void _post_popup();
public:
void config(const String &p_base_dir);
DirectoryCreateDialog();
};
#endif // DIRECTORY_CREATE_DIALOG_H

View File

@ -52,6 +52,7 @@
#include "core/variant/dictionary.h" #include "core/variant/dictionary.h"
#include "editor/create_dialog.h" #include "editor/create_dialog.h"
#include "editor/dependency_editor.h" #include "editor/dependency_editor.h"
#include "editor/directory_create_dialog.h"
#include "editor/editor_data.h" #include "editor/editor_data.h"
#include "editor/editor_dir_dialog.h" #include "editor/editor_dir_dialog.h"
#include "editor/editor_file_system.h" #include "editor/editor_file_system.h"
@ -1348,38 +1349,6 @@ void FileSystemDock::_save_scenes_after_move(const RBMap<String, String> &p_rena
editor->save_scene_list(new_filenames); editor->save_scene_list(new_filenames);
} }
void FileSystemDock::_make_dir_confirm() {
String dir_name = make_dir_dialog_text->get_text().strip_edges();
if (dir_name.length() == 0) {
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
return;
} else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1 || dir_name.find("*") != -1 ||
dir_name.find("|") != -1 || dir_name.find(">") != -1 || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters."));
return;
}
String directory = path;
if (!directory.ends_with("/")) {
directory = directory.get_base_dir();
}
print_verbose("Making folder " + dir_name + " in " + directory);
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
Error err = da->change_dir(directory);
if (err == OK) {
err = da->make_dir(dir_name);
}
memdelete(da);
if (err == OK) {
print_verbose("FileSystem: calling rescan.");
_rescan();
} else {
EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
}
}
void FileSystemDock::_make_scene_confirm() { void FileSystemDock::_make_scene_confirm() {
String scene_name = make_scene_dialog_text->get_text().strip_edges(); String scene_name = make_scene_dialog_text->get_text().strip_edges();
@ -1914,10 +1883,12 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
} break; } break;
case FILE_NEW_FOLDER: { case FILE_NEW_FOLDER: {
make_dir_dialog_text->set_text("new folder"); String directory = path;
make_dir_dialog_text->select_all(); if (!directory.ends_with("/")) {
make_dir_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE); directory = directory.get_base_dir();
make_dir_dialog_text->grab_focus(); }
make_dir_dialog->config(directory);
make_dir_dialog->popup_centered(Size2(1, 1));
} break; } break;
case FILE_NEW_SCENE: { case FILE_NEW_SCENE: {
@ -2822,7 +2793,6 @@ void FileSystemDock::_bind_methods() {
ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history); ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history);
ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed); ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileSystemDock::_tree_multi_selected); ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileSystemDock::_tree_multi_selected);
ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileSystemDock::_make_dir_confirm);
ClassDB::bind_method(D_METHOD("_make_scene_confirm"), &FileSystemDock::_make_scene_confirm); ClassDB::bind_method(D_METHOD("_make_scene_confirm"), &FileSystemDock::_make_scene_confirm);
ClassDB::bind_method(D_METHOD("_resource_created"), &FileSystemDock::_resource_created); ClassDB::bind_method(D_METHOD("_resource_created"), &FileSystemDock::_resource_created);
ClassDB::bind_method(D_METHOD("_move_operation_confirm", "to_path", "overwrite"), &FileSystemDock::_move_operation_confirm, DEFVAL(false)); ClassDB::bind_method(D_METHOD("_move_operation_confirm", "to_path", "overwrite"), &FileSystemDock::_move_operation_confirm, DEFVAL(false));
@ -3037,16 +3007,9 @@ FileSystemDock::FileSystemDock(EditorNode *p_editor) {
duplicate_dialog->register_text_enter(duplicate_dialog_text); duplicate_dialog->register_text_enter(duplicate_dialog_text);
duplicate_dialog->connect("confirmed", this, "_duplicate_operation_confirm"); duplicate_dialog->connect("confirmed", this, "_duplicate_operation_confirm");
make_dir_dialog = memnew(ConfirmationDialog); make_dir_dialog = memnew(DirectoryCreateDialog);
make_dir_dialog->set_title(TTR("Create Folder"));
VBoxContainer *make_folder_dialog_vb = memnew(VBoxContainer);
make_dir_dialog->add_child(make_folder_dialog_vb);
make_dir_dialog_text = memnew(LineEdit);
make_folder_dialog_vb->add_margin_child(TTR("Name:"), make_dir_dialog_text);
add_child(make_dir_dialog); add_child(make_dir_dialog);
make_dir_dialog->register_text_enter(make_dir_dialog_text); make_dir_dialog->connect("dir_created", this, "_rescan");
make_dir_dialog->connect("confirmed", this, "_make_dir_confirm");
make_scene_dialog = memnew(ConfirmationDialog); make_scene_dialog = memnew(ConfirmationDialog);
make_scene_dialog->set_title(TTR("Create Scene")); make_scene_dialog->set_title(TTR("Create Scene"));

View File

@ -68,6 +68,7 @@ class Texture;
class TextureRect; class TextureRect;
class Tree; class Tree;
class TreeItem; class TreeItem;
class DirectoryCreateDialog;
class FileSystemDock : public VBoxContainer { class FileSystemDock : public VBoxContainer {
GDCLASS(FileSystemDock, VBoxContainer); GDCLASS(FileSystemDock, VBoxContainer);
@ -166,8 +167,7 @@ private:
LineEdit *rename_dialog_text; LineEdit *rename_dialog_text;
ConfirmationDialog *duplicate_dialog; ConfirmationDialog *duplicate_dialog;
LineEdit *duplicate_dialog_text; LineEdit *duplicate_dialog_text;
ConfirmationDialog *make_dir_dialog; DirectoryCreateDialog *make_dir_dialog;
LineEdit *make_dir_dialog_text;
ConfirmationDialog *make_scene_dialog; ConfirmationDialog *make_scene_dialog;
LineEdit *make_scene_dialog_text; LineEdit *make_scene_dialog_text;
ConfirmationDialog *overwrite_dialog; ConfirmationDialog *overwrite_dialog;
@ -244,7 +244,6 @@ private:
void _folder_removed(String p_folder); void _folder_removed(String p_folder);
void _resource_created() const; void _resource_created() const;
void _make_dir_confirm();
void _make_scene_confirm(); void _make_scene_confirm();
void _rename_operation_confirm(); void _rename_operation_confirm();
void _duplicate_operation_confirm(); void _duplicate_operation_confirm();