2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_dir_dialog.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "editor_dir_dialog.h"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/os.h"
|
2017-03-05 21:21:25 +08:00
|
|
|
#include "editor/editor_file_system.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "editor/editor_settings.h"
|
2017-09-03 11:22:54 +08:00
|
|
|
#include "editor_scale.h"
|
|
|
|
void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
updating = true;
|
2015-03-22 01:33:32 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
String path = p_dir->get_path();
|
2015-03-22 01:33:32 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
p_item->set_metadata(0, p_dir->get_path());
|
|
|
|
p_item->set_icon(0, get_icon("Folder", "EditorIcons"));
|
2015-03-22 01:33:32 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
if (!p_item->get_parent()) {
|
|
|
|
p_item->set_text(0, "res://");
|
|
|
|
} else {
|
2015-03-22 01:33:32 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
if (!opened_paths.has(path) && (p_select_path == String() || !p_select_path.begins_with(path))) {
|
|
|
|
p_item->set_collapsed(true);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-09-03 11:22:54 +08:00
|
|
|
|
|
|
|
p_item->set_text(0, p_dir->get_name());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
//this should be handled by EditorFileSystem already
|
|
|
|
//bool show_hidden = EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files");
|
|
|
|
updating = false;
|
|
|
|
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
|
2015-06-06 20:44:38 +08:00
|
|
|
|
|
|
|
TreeItem *ti = tree->create_item(p_item);
|
2017-09-03 11:22:54 +08:00
|
|
|
_update_dir(ti, p_dir->get_subdir(i));
|
2015-06-06 20:44:38 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-15 01:49:11 +08:00
|
|
|
void EditorDirDialog::reload(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-01-13 21:45:50 +08:00
|
|
|
if (!is_visible_in_tree()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
must_reload = true;
|
2016-06-19 06:28:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
tree->clear();
|
|
|
|
TreeItem *root = tree->create_item();
|
2017-09-15 01:49:11 +08:00
|
|
|
_update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
|
2014-02-10 09:10:30 +08:00
|
|
|
_item_collapsed(root);
|
2017-03-05 23:44:50 +08:00
|
|
|
must_reload = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_notification(int p_what) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2017-09-03 11:22:54 +08:00
|
|
|
EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
|
2014-02-10 09:10:30 +08:00
|
|
|
reload();
|
2016-06-30 22:12:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!tree->is_connected("item_collapsed", this, "_item_collapsed")) {
|
|
|
|
tree->connect("item_collapsed", this, "_item_collapsed", varray(), CONNECT_DEFERRED);
|
2016-06-30 22:12:14 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
|
|
|
|
EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
|
2016-06-30 22:12:14 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-06-19 06:28:33 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2019-10-29 07:40:36 +08:00
|
|
|
if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
|
|
|
|
EditorFileSystem::get_singleton()->disconnect("filesystem_changed", this, "reload");
|
|
|
|
}
|
2017-09-03 11:22:54 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2017-01-13 21:45:50 +08:00
|
|
|
if (must_reload && is_visible_in_tree()) {
|
2016-06-19 06:28:33 +08:00
|
|
|
reload();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-08-13 00:52:50 +08:00
|
|
|
void EditorDirDialog::_item_collapsed(Object *p_item) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
TreeItem *item = Object::cast_to<TreeItem>(p_item);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
if (updating)
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
if (item->is_collapsed())
|
|
|
|
opened_paths.erase(item->get_metadata(0));
|
|
|
|
else
|
|
|
|
opened_paths.insert(item->get_metadata(0));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::ok_pressed() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!ti)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String dir = ti->get_metadata(0);
|
2017-03-05 23:44:50 +08:00
|
|
|
emit_signal("dir_selected", dir);
|
2014-02-10 09:10:30 +08:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_make_dir() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2016-07-01 05:23:39 +08:00
|
|
|
if (!ti) {
|
2019-03-25 08:54:29 +08:00
|
|
|
mkdirerr->set_text(TTR("Please select a base directory first."));
|
2016-07-01 05:23:39 +08:00
|
|
|
mkdirerr->popup_centered_minsize();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2016-07-01 05:23:39 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
makedialog->popup_centered_minsize(Size2(250, 80));
|
2016-07-01 05:23:39 +08:00
|
|
|
makedirname->grab_focus();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_make_dir_confirm() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!ti)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String dir = ti->get_metadata(0);
|
2016-07-01 05:23:39 +08:00
|
|
|
|
2017-09-03 11:22:54 +08:00
|
|
|
DirAccessRef d = DirAccess::open(dir);
|
2019-09-25 16:28:50 +08:00
|
|
|
ERR_FAIL_COND_MSG(!d, "Cannot open directory '" + dir + "'.");
|
2014-02-10 09:10:30 +08:00
|
|
|
Error err = d->make_dir(makedirname->get_text());
|
2016-07-01 05:23:39 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (err != OK) {
|
2017-09-03 11:22:54 +08:00
|
|
|
mkdirerr->popup_centered_minsize(Size2(250, 80) * EDSCALE);
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-09-03 11:22:54 +08:00
|
|
|
opened_paths.insert(dir);
|
|
|
|
//reload(dir.plus_file(makedirname->get_text()));
|
|
|
|
EditorFileSystem::get_singleton()->scan_changes(); //we created a dir, so rescan changes
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-05-01 17:38:24 +08:00
|
|
|
makedirname->set_text(""); // reset label
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_item_collapsed"), &EditorDirDialog::_item_collapsed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_make_dir"), &EditorDirDialog::_make_dir);
|
|
|
|
ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &EditorDirDialog::_make_dir_confirm);
|
2017-09-03 11:22:54 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("reload"), &EditorDirDialog::reload, DEFVAL(""));
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorDirDialog::EditorDirDialog() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
updating = false;
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2016-05-04 09:25:37 +08:00
|
|
|
set_title(TTR("Choose a Directory"));
|
2015-12-14 19:43:56 +08:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
tree = memnew(Tree);
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(tree);
|
2017-01-10 12:49:55 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
tree->connect("item_activated", this, "_ok");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2019-06-26 21:08:25 +08:00
|
|
|
makedir = add_button(TTR("Create Folder"), OS::get_singleton()->get_swap_ok_cancel(), "makedir");
|
2017-03-05 23:44:50 +08:00
|
|
|
makedir->connect("pressed", this, "_make_dir");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
makedialog = memnew(ConfirmationDialog);
|
2016-05-04 09:25:37 +08:00
|
|
|
makedialog->set_title(TTR("Create Folder"));
|
2015-12-14 19:43:56 +08:00
|
|
|
add_child(makedialog);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VBoxContainer *makevb = memnew(VBoxContainer);
|
2014-02-10 09:10:30 +08:00
|
|
|
makedialog->add_child(makevb);
|
2017-01-14 19:26:56 +08:00
|
|
|
//makedialog->set_child_rect(makevb);
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
makedirname = memnew(LineEdit);
|
|
|
|
makevb->add_margin_child(TTR("Name:"), makedirname);
|
2014-02-10 09:10:30 +08:00
|
|
|
makedialog->register_text_enter(makedirname);
|
2017-03-05 23:44:50 +08:00
|
|
|
makedialog->connect("confirmed", this, "_make_dir_confirm");
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
mkdirerr = memnew(AcceptDialog);
|
2016-05-04 09:25:37 +08:00
|
|
|
mkdirerr->set_text(TTR("Could not create folder."));
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(mkdirerr);
|
|
|
|
|
2016-05-04 09:25:37 +08:00
|
|
|
get_ok()->set_text(TTR("Choose"));
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
must_reload = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|