2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* editor_dir_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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "editor_dir_dialog.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
#include "editor/directory_create_dialog.h"
|
2017-03-05 14:21:25 +01:00
|
|
|
#include "editor/editor_file_system.h"
|
2024-04-14 07:09:49 +02:00
|
|
|
#include "editor/filesystem_dock.h"
|
|
|
|
#include "editor/themes/editor_theme_manager.h"
|
2023-12-24 06:59:32 +01:00
|
|
|
#include "scene/gui/box_container.h"
|
2023-03-25 20:58:37 +01:00
|
|
|
#include "scene/gui/tree.h"
|
2020-03-03 14:36:29 +01:00
|
|
|
#include "servers/display_server.h"
|
2020-02-21 18:28:45 +01:00
|
|
|
|
2024-04-14 07:09:49 +02:00
|
|
|
void EditorDirDialog::_update_dir(const Color &p_default_folder_color, const Dictionary &p_assigned_folder_colors, const HashMap<String, Color> &p_folder_colors, bool p_is_dark_theme, TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
2015-03-21 18:33:32 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
const String path = p_dir->get_path();
|
2015-03-21 18:33:32 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
p_item->set_metadata(0, path);
|
2023-08-13 02:33:39 +02:00
|
|
|
p_item->set_icon(0, tree->get_editor_theme_icon(SNAME("Folder")));
|
2015-03-21 18:33:32 +01:00
|
|
|
|
2017-09-03 05:22:54 +02:00
|
|
|
if (!p_item->get_parent()) {
|
|
|
|
p_item->set_text(0, "res://");
|
2024-04-14 07:09:49 +02:00
|
|
|
p_item->set_icon_modulate(0, p_default_folder_color);
|
2017-09-03 05:22:54 +02:00
|
|
|
} else {
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
|
2017-09-03 05:22:54 +02:00
|
|
|
p_item->set_collapsed(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-09-03 05:22:54 +02:00
|
|
|
|
|
|
|
p_item->set_text(0, p_dir->get_name());
|
2024-04-14 07:09:49 +02:00
|
|
|
|
|
|
|
if (p_assigned_folder_colors.has(path)) {
|
|
|
|
const Color &folder_color = p_folder_colors[p_assigned_folder_colors[path]];
|
|
|
|
p_item->set_icon_modulate(0, p_is_dark_theme ? folder_color : folder_color * FileSystemDock::ITEM_COLOR_SCALE);
|
|
|
|
p_item->set_custom_bg_color(0, Color(folder_color, p_is_dark_theme ? FileSystemDock::ITEM_ALPHA_MIN : FileSystemDock::ITEM_ALPHA_MAX));
|
|
|
|
} else {
|
|
|
|
TreeItem *parent_item = p_item->get_parent();
|
|
|
|
Color parent_bg_color = parent_item->get_custom_bg_color(0);
|
|
|
|
if (parent_bg_color != Color()) {
|
|
|
|
p_item->set_custom_bg_color(0, p_assigned_folder_colors.has(parent_item->get_metadata(0)) ? parent_bg_color.darkened(FileSystemDock::ITEM_BG_DARK_SCALE) : parent_bg_color);
|
|
|
|
p_item->set_icon_modulate(0, parent_item->get_icon_modulate(0));
|
|
|
|
} else {
|
|
|
|
p_item->set_icon_modulate(0, p_default_folder_color);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
if (path == new_dir_path || !p_item->get_parent()) {
|
|
|
|
p_item->select(0);
|
|
|
|
}
|
|
|
|
|
2017-09-03 05:22:54 +02:00
|
|
|
updating = false;
|
|
|
|
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
|
2015-06-06 14:44:38 +02:00
|
|
|
TreeItem *ti = tree->create_item(p_item);
|
2024-04-14 07:09:49 +02:00
|
|
|
_update_dir(p_default_folder_color, p_assigned_folder_colors, p_folder_colors, p_is_dark_theme, ti, p_dir->get_subdir(i));
|
2015-06-06 14:44:38 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
void EditorDirDialog::config(const Vector<String> &p_paths) {
|
|
|
|
ERR_FAIL_COND(p_paths.is_empty());
|
|
|
|
|
|
|
|
if (p_paths.size() == 1) {
|
|
|
|
String path = p_paths[0];
|
|
|
|
if (path.ends_with("/")) {
|
|
|
|
path = path.substr(0, path.length() - 1);
|
|
|
|
}
|
|
|
|
// TRANSLATORS: %s is the file name that will be moved or duplicated.
|
|
|
|
set_title(vformat(TTR("Move/Duplicate: %s"), path.get_file()));
|
|
|
|
} else {
|
|
|
|
// TRANSLATORS: %d is the number of files that will be moved or duplicated.
|
|
|
|
set_title(vformat(TTRN("Move/Duplicate %d Item", "Move/Duplicate %d Items", p_paths.size()), p_paths.size()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:49:11 +02:00
|
|
|
void EditorDirDialog::reload(const String &p_path) {
|
2020-03-06 18:00:16 +01:00
|
|
|
if (!is_visible()) {
|
2017-03-05 16:44:50 +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();
|
2024-04-14 07:09:49 +02:00
|
|
|
_update_dir(tree->get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")), FileSystemDock::get_singleton()->get_assigned_folder_colors(), FileSystemDock::get_singleton()->get_folder_colors(), EditorThemeManager::is_dark_theme(), root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
|
2014-02-10 02:10:30 +01:00
|
|
|
_item_collapsed(root);
|
2023-12-24 06:59:32 +01:00
|
|
|
new_dir_path.clear();
|
2017-03-05 16:44:50 +01:00
|
|
|
must_reload = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_notification(int p_what) {
|
2022-02-16 00:52:32 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2024-04-14 07:09:49 +02:00
|
|
|
FileSystemDock::get_singleton()->connect("folder_color_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
|
|
|
|
|
2022-07-28 22:56:41 +02:00
|
|
|
EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
|
2016-06-19 00:28:33 +02:00
|
|
|
reload();
|
2022-02-16 00:52:32 +01:00
|
|
|
|
|
|
|
if (!tree->is_connected("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed))) {
|
2022-07-28 22:56:41 +02:00
|
|
|
tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED);
|
2022-02-16 00:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
|
2022-07-28 22:56:41 +02:00
|
|
|
EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
|
2022-02-16 00:52:32 +01:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed", callable_mp(this, &EditorDirDialog::reload))) {
|
|
|
|
EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (must_reload && is_visible()) {
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
} break;
|
2016-06-19 00:28:33 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 18:52:50 +02:00
|
|
|
void EditorDirDialog::_item_collapsed(Object *p_item) {
|
2017-08-24 22:58:51 +02:00
|
|
|
TreeItem *item = Object::cast_to<TreeItem>(p_item);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (updating) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (item->is_collapsed()) {
|
2017-09-03 05:22:54 +02:00
|
|
|
opened_paths.erase(item->get_metadata(0));
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-09-03 05:22:54 +02:00
|
|
|
opened_paths.insert(item->get_metadata(0));
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:49:16 +01:00
|
|
|
void EditorDirDialog::_item_activated() {
|
2023-12-24 06:59:32 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
|
|
|
ERR_FAIL_NULL(ti);
|
|
|
|
if (ti->get_child_count() > 0) {
|
|
|
|
ti->set_collapsed(!ti->is_collapsed());
|
|
|
|
}
|
2020-02-27 22:49:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
void EditorDirDialog::_copy_pressed() {
|
2017-03-05 16:44:50 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2023-12-24 06:59:32 +01:00
|
|
|
ERR_FAIL_NULL(ti);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
hide();
|
2023-12-24 06:59:32 +01:00
|
|
|
emit_signal(SNAME("copy_pressed"), ti->get_metadata(0));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
void EditorDirDialog::ok_pressed() {
|
2017-03-05 16:44:50 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2023-12-24 06:59:32 +01:00
|
|
|
ERR_FAIL_NULL(ti);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
hide();
|
|
|
|
emit_signal(SNAME("move_pressed"), ti->get_metadata(0));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
void EditorDirDialog::_make_dir() {
|
2017-03-05 16:44:50 +01:00
|
|
|
TreeItem *ti = tree->get_selected();
|
2023-12-24 06:59:32 +01:00
|
|
|
ERR_FAIL_NULL(ti);
|
|
|
|
makedialog->config(ti->get_metadata(0));
|
|
|
|
makedialog->popup_centered();
|
|
|
|
}
|
2022-04-05 19:09:52 +02:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
void EditorDirDialog::_make_dir_confirm(const String &p_path) {
|
|
|
|
// Multiple level of directories can be created at once.
|
|
|
|
String base_dir = p_path.get_base_dir();
|
|
|
|
while (true) {
|
|
|
|
opened_paths.insert(base_dir + "/");
|
|
|
|
if (base_dir == "res://") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base_dir = base_dir.get_base_dir();
|
2022-03-24 23:27:33 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
new_dir_path = p_path + "/";
|
|
|
|
EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes.
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDirDialog::_bind_methods() {
|
2023-12-24 06:59:32 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("copy_pressed", PropertyInfo(Variant::STRING, "dir")));
|
|
|
|
ADD_SIGNAL(MethodInfo("move_pressed", PropertyInfo(Variant::STRING, "dir")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorDirDialog::EditorDirDialog() {
|
2015-12-14 12:43:56 +01:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
|
2023-03-25 20:58:37 +01:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
|
|
|
add_child(vb);
|
2017-01-10 05:49:55 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
HBoxContainer *hb = memnew(HBoxContainer);
|
|
|
|
vb->add_child(hb);
|
|
|
|
|
|
|
|
hb->add_child(memnew(Label(TTR("Choose target directory:"))));
|
|
|
|
hb->add_spacer();
|
|
|
|
|
|
|
|
makedir = memnew(Button(TTR("Create Folder")));
|
|
|
|
hb->add_child(makedir);
|
|
|
|
makedir->connect("pressed", callable_mp(this, &EditorDirDialog::_make_dir));
|
|
|
|
|
2023-03-25 20:58:37 +01:00
|
|
|
tree = memnew(Tree);
|
|
|
|
vb->add_child(tree);
|
|
|
|
tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
2020-02-27 22:49:16 +01:00
|
|
|
tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
set_ok_button_text(TTR("Move"));
|
2023-03-25 20:58:37 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
copy = add_button(TTR("Copy"), !DisplayServer::get_singleton()->get_swap_cancel_ok());
|
|
|
|
copy->connect("pressed", callable_mp(this, &EditorDirDialog::_copy_pressed));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-12-24 06:59:32 +01:00
|
|
|
makedialog = memnew(DirectoryCreateDialog);
|
2015-12-14 12:43:56 +01:00
|
|
|
add_child(makedialog);
|
2023-12-24 06:59:32 +01:00
|
|
|
makedialog->connect("dir_created", callable_mp(this, &EditorDirDialog::_make_dir_confirm));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|