2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_dir_dialog.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "editor_dir_dialog.h"
|
2017-03-18 23:45:45 +01:00
|
|
|
#include "editor/editor_file_system.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#include "os/keyboard.h"
|
|
|
|
#include "os/os.h"
|
2015-03-21 18:33:32 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void EditorDirDialog::_update_dir(TreeItem *p_item) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
updating = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
p_item->clear_children();
|
|
|
|
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
|
|
|
String cdir = p_item->get_metadata(0);
|
|
|
|
|
|
|
|
da->change_dir(cdir);
|
|
|
|
da->list_dir_begin();
|
2017-03-19 00:36:26 +01:00
|
|
|
String p = da->get_next();
|
2015-03-21 18:33:32 +01:00
|
|
|
|
2015-06-06 14:44:38 +02:00
|
|
|
List<String> dirs;
|
2015-03-21 18:33:32 +01:00
|
|
|
bool ishidden;
|
|
|
|
bool show_hidden = EditorSettings::get_singleton()->get("file_dialog/show_hidden_files");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (p != "") {
|
2015-03-21 18:33:32 +01:00
|
|
|
|
|
|
|
ishidden = da->current_is_hidden();
|
|
|
|
|
|
|
|
if (show_hidden || !ishidden) {
|
|
|
|
if (da->current_is_dir() && !p.begins_with(".")) {
|
2015-06-06 14:44:38 +02:00
|
|
|
dirs.push_back(p);
|
2015-03-21 18:33:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
p = da->get_next();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-06-06 14:44:38 +02:00
|
|
|
dirs.sort();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
|
2015-06-06 14:44:38 +02:00
|
|
|
TreeItem *ti = tree->create_item(p_item);
|
2017-03-19 00:36:26 +01:00
|
|
|
ti->set_text(0, E->get());
|
|
|
|
ti->set_icon(0, get_icon("Folder", "EditorIcons"));
|
2015-06-06 14:44:38 +02:00
|
|
|
ti->set_collapsed(true);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(da);
|
2017-03-19 00:36:26 +01:00
|
|
|
updating = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::reload() {
|
|
|
|
|
2016-06-19 00:28:33 +02:00
|
|
|
if (!is_visible()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
must_reload = true;
|
2016-06-19 00:28:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
tree->clear();
|
|
|
|
TreeItem *root = tree->create_item();
|
2017-03-19 00:36:26 +01:00
|
|
|
root->set_metadata(0, "res://");
|
|
|
|
root->set_icon(0, get_icon("Folder", "EditorIcons"));
|
|
|
|
root->set_text(0, "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_dir(root);
|
|
|
|
_item_collapsed(root);
|
2017-03-19 00:36:26 +01:00
|
|
|
must_reload = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_notification(int p_what) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
reload();
|
2016-06-30 16:12:14 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (!tree->is_connected("item_collapsed", this, "_item_collapsed")) {
|
|
|
|
tree->connect("item_collapsed", this, "_item_collapsed", varray(), CONNECT_DEFERRED);
|
2016-06-30 16:12:14 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", this, "reload")) {
|
|
|
|
EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "reload");
|
2016-06-30 16:12:14 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-06-19 00:28:33 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2016-06-19 00:28:33 +02:00
|
|
|
if (must_reload && is_visible()) {
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void EditorDirDialog::_item_collapsed(Object *_p_item) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
TreeItem *p_item = _p_item->cast_to<TreeItem>();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (updating || p_item->is_collapsed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
TreeItem *ci = p_item->get_children();
|
2017-03-19 00:36:26 +01:00
|
|
|
while (ci) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String p = ci->get_metadata(0);
|
|
|
|
if (p == "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
String pp = p_item->get_metadata(0);
|
2017-03-19 00:36:26 +01:00
|
|
|
ci->set_metadata(0, pp.plus_file(ci->get_text(0)));
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_dir(ci);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
ci = ci->get_next();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void EditorDirDialog::set_current_path(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
reload();
|
|
|
|
String p = p_path;
|
|
|
|
if (p.begins_with("res://"))
|
2017-03-19 00:36:26 +01:00
|
|
|
p = p.replace_first("res://", "");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<String> dirs = p.split("/", false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
TreeItem *r = tree->get_root();
|
|
|
|
for (int i = 0; i < dirs.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String d = dirs[i];
|
|
|
|
TreeItem *p = r->get_children();
|
2017-03-19 00:36:26 +01:00
|
|
|
while (p) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p->get_text(0) == d)
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-19 00:36:26 +01:00
|
|
|
p = p->get_next();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!p);
|
|
|
|
String pp = p->get_metadata(0);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (pp == "") {
|
|
|
|
p->set_metadata(0, String(r->get_metadata(0)).plus_file(d));
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_dir(p);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
updating = true;
|
2016-09-06 00:27:15 +02:00
|
|
|
p->set_collapsed(false);
|
2017-03-19 00:36:26 +01:00
|
|
|
updating = false;
|
2016-09-06 00:27:15 +02:00
|
|
|
_item_collapsed(p);
|
2017-03-19 00:36:26 +01:00
|
|
|
r = p;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r->select(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::ok_pressed() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!ti)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String dir = ti->get_metadata(0);
|
2017-03-19 00:36:26 +01:00
|
|
|
emit_signal("dir_selected", dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_make_dir() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2016-06-30 23:23:39 +02:00
|
|
|
if (!ti) {
|
|
|
|
mkdirerr->set_text("Please select a base directory first");
|
|
|
|
mkdirerr->popup_centered_minsize();
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2016-06-30 23:23:39 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
makedialog->popup_centered_minsize(Size2(250, 80));
|
2016-06-30 23:23:39 +02:00
|
|
|
makedirname->grab_focus();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_make_dir_confirm() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!ti)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String dir = ti->get_metadata(0);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
DirAccess *d = DirAccess::open(dir);
|
|
|
|
ERR_FAIL_COND(!d);
|
|
|
|
Error err = d->make_dir(makedirname->get_text());
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (err != OK) {
|
|
|
|
mkdirerr->popup_centered_minsize(Size2(250, 80));
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2016-09-06 00:27:15 +02:00
|
|
|
set_current_path(dir.plus_file(makedirname->get_text()));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-05-01 11:38:24 +02:00
|
|
|
makedirname->set_text(""); // reset label
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_item_collapsed"), &EditorDirDialog::_item_collapsed);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_make_dir"), &EditorDirDialog::_make_dir);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_make_dir_confirm"), &EditorDirDialog::_make_dir_confirm);
|
|
|
|
ObjectTypeDB::bind_method(_MD("reload"), &EditorDirDialog::reload);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorDirDialog::EditorDirDialog() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
updating = false;
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
set_title(TTR("Choose a Directory"));
|
2015-12-14 12:43:56 +01:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
tree = memnew(Tree);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(tree);
|
|
|
|
set_child_rect(tree);
|
2017-03-19 00:36:26 +01:00
|
|
|
tree->connect("item_activated", this, "_ok");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
makedir = add_button(TTR("Create Folder"), OS::get_singleton()->get_swap_ok_cancel() ? true : false, "makedir");
|
|
|
|
makedir->connect("pressed", this, "_make_dir");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
makedialog = memnew(ConfirmationDialog);
|
2016-05-04 03:25:37 +02:00
|
|
|
makedialog->set_title(TTR("Create Folder"));
|
2015-12-14 12:43:56 +01:00
|
|
|
add_child(makedialog);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
VBoxContainer *makevb = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
makedialog->add_child(makevb);
|
|
|
|
makedialog->set_child_rect(makevb);
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
makedirname = memnew(LineEdit);
|
|
|
|
makevb->add_margin_child(TTR("Name:"), makedirname);
|
2014-02-10 02:10:30 +01:00
|
|
|
makedialog->register_text_enter(makedirname);
|
2017-03-19 00:36:26 +01:00
|
|
|
makedialog->connect("confirmed", this, "_make_dir_confirm");
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
mkdirerr = memnew(AcceptDialog);
|
2016-05-04 03:25:37 +02:00
|
|
|
mkdirerr->set_text(TTR("Could not create folder."));
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(mkdirerr);
|
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
get_ok()->set_text(TTR("Choose"));
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
must_reload = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|