Debian Codegraph @

editor/gui/editor_dir_dialog.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_dir_dialog.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "editor_dir_dialog.h"
32
33 #include "editor/docks/filesystem_dock.h"
34 #include "editor/file_system/editor_file_system.h"
35 #include "editor/gui/directory_create_dialog.h"
36 #include "editor/themes/editor_theme_manager.h"
37 #include "scene/gui/box_container.h"
38 #include "scene/gui/tree.h"
39 #include "servers/display/display_server.h"
40
41 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_icon_and_font, TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
42 updating = true;
43
44 const String path = p_dir->get_path();
45
46 p_item->set_metadata(0, path);
47 p_item->set_icon(0, tree->get_editor_theme_icon(SNAME("Folder")));
48
49 if (!p_item->get_parent()) {
50 p_item->set_text(0, "res://");
51 p_item->set_icon_modulate(0, p_default_folder_color);
52 } else {
53 if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
54 p_item->set_collapsed(true);
55 }
56
57 p_item->set_text(0, p_dir->get_name());
58
59 if (p_assigned_folder_colors.has(path)) {
60 const Color &folder_color = p_folder_colors[p_assigned_folder_colors[path]];
61 p_item->set_icon_modulate(0, p_is_dark_icon_and_font ? folder_color : folder_color * FileSystemDock::ITEM_COLOR_SCALE);
62 p_item->set_custom_bg_color(0, Color(folder_color, p_is_dark_icon_and_font ? FileSystemDock::ITEM_ALPHA_MIN : FileSystemDock::ITEM_ALPHA_MAX));
63 } else {
64 TreeItem *parent_item = p_item->get_parent();
65 Color parent_bg_color = parent_item->get_custom_bg_color(0);
66 if (parent_bg_color != Color()) {
67 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);
68 p_item->set_icon_modulate(0, parent_item->get_icon_modulate(0));
69 } else {
70 p_item->set_icon_modulate(0, p_default_folder_color);
71 }
72 }
73 }
74
75 if (path == new_dir_path || !p_item->get_parent()) {
76 p_item->select(0);
77 }
78
79 updating = false;
80 for (int i = 0; i < p_dir->get_subdir_count(); i++) {
81 TreeItem *ti = tree->create_item(p_item);
82 _update_dir(p_default_folder_color, p_assigned_folder_colors, p_folder_colors, p_is_dark_icon_and_font, ti, p_dir->get_subdir(i));
83 }
84 }
85
86 void EditorDirDialog::config(const Vector<String> &p_paths) {
87 ERR_FAIL_COND(p_paths.is_empty());
88
89 if (p_paths.size() == 1) {
90 String path = p_paths[0];
91 if (path.ends_with("/")) {
92 path = path.substr(0, path.length() - 1);
93 }
94 // TRANSLATORS: %s is the file name that will be moved or duplicated.
95 set_title(vformat(TTR("Move/Duplicate: %s"), path.get_file()));
96 } else {
97 // TRANSLATORS: %d is the number of files that will be moved or duplicated.
98 set_title(vformat(TTRN("Move/Duplicate %d Item", "Move/Duplicate %d Items", p_paths.size()), p_paths.size()));
99 }
100 }
101
102 void EditorDirDialog::reload(const String &p_path) {
103 if (!is_visible()) {
104 must_reload = true;
105 return;
106 }
107
108 tree->clear();
109 TreeItem *root = tree->create_item();
110 _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_icon_and_font(), root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
111 _item_collapsed(root);
112 new_dir_path.clear();
113 must_reload = false;
114 }
115
116 void EditorDirDialog::_notification(int p_what) {
117 switch (p_what) {
118 case NOTIFICATION_ENTER_TREE: {
119 FileSystemDock::get_singleton()->connect("folder_color_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
120 EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
121 reload();
122 } break;
123
124 case NOTIFICATION_EXIT_TREE: {
125 EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
126 FileSystemDock::get_singleton()->disconnect("folder_color_changed", callable_mp(this, &EditorDirDialog::reload));
127 } break;
128
129 case NOTIFICATION_VISIBILITY_CHANGED: {
130 if (must_reload && is_visible()) {
131 reload();
132 }
133 } break;
134 }
135 }
136
137 void EditorDirDialog::_item_collapsed(Object *p_item) {
138 TreeItem *item = Object::cast_to<TreeItem>(p_item);
139
140 if (updating) {
141 return;
142 }
143
144 if (item->is_collapsed()) {
145 opened_paths.erase(item->get_metadata(0));
146 } else {
147 opened_paths.insert(item->get_metadata(0));
148 }
149 }
150
151 void EditorDirDialog::_item_activated() {
152 TreeItem *ti = tree->get_selected();
153 ERR_FAIL_NULL(ti);
154 if (ti->get_child_count() > 0) {
155 ti->set_collapsed(!ti->is_collapsed());
156 }
157 }
158
159 void EditorDirDialog::_copy_pressed() {
160 TreeItem *ti = tree->get_selected();
161 ERR_FAIL_NULL(ti);
162
163 hide();
164 emit_signal(SNAME("copy_pressed"), ti->get_metadata(0));
165 }
166
167 void EditorDirDialog::ok_pressed() {
168 TreeItem *ti = tree->get_selected();
169 ERR_FAIL_NULL(ti);
170
171 hide();
172 emit_signal(SNAME("move_pressed"), ti->get_metadata(0));
173 }
174
175 void EditorDirDialog::_make_dir() {
176 TreeItem *ti = tree->get_selected();
177 ERR_FAIL_NULL(ti);
178 const String &directory = ti->get_metadata(0);
179 makedialog->config(directory, callable_mp(this, &EditorDirDialog::_make_dir_confirm).bind(directory), DirectoryCreateDialog::MODE_DIRECTORY, "new folder");
180 makedialog->popup_centered();
181 }
182
183 void EditorDirDialog::_make_dir_confirm(const String &p_path, const String &p_base_dir) {
184 FileSystemDock::get_singleton()->create_directory(p_path, p_base_dir);
185
186 // Multiple level of directories can be created at once.
187 String base_dir = p_path.get_base_dir();
188 while (true) {
189 opened_paths.insert(base_dir + "/");
190 if (base_dir == "res://") {
191 break;
192 }
193 base_dir = base_dir.get_base_dir();
194 }
195
196 new_dir_path = p_path + "/";
197 }
198
199 void EditorDirDialog::_bind_methods() {
200 ADD_SIGNAL(MethodInfo("copy_pressed", PropertyInfo(Variant::STRING, "dir")));
201 ADD_SIGNAL(MethodInfo("move_pressed", PropertyInfo(Variant::STRING, "dir")));
202 }
203
204 EditorDirDialog::EditorDirDialog() {
205 set_hide_on_ok(false);
206
207 VBoxContainer *vb = memnew(VBoxContainer);
208 add_child(vb);
209
210 HBoxContainer *hb = memnew(HBoxContainer);
211 vb->add_child(hb);
212
213 hb->add_child(memnew(Label(TTR("Choose target directory:"))));
214 hb->add_spacer();
215
216 makedir = memnew(Button(TTR("Create Folder")));
217 hb->add_child(makedir);
218 makedir->connect(SceneStringName(pressed), callable_mp(this, &EditorDirDialog::_make_dir));
219
220 tree = memnew(Tree);
221 tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
222 tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
223 vb->add_child(tree);
224 tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
225 tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED);
226
227 set_ok_button_text(TTR("Move"));
228
229 copy = add_button(TTR("Copy"), !DisplayServer::get_singleton()->get_swap_cancel_ok());
230 copy->connect(SceneStringName(pressed), callable_mp(this, &EditorDirDialog::_copy_pressed));
231
232 makedialog = memnew(DirectoryCreateDialog);
233 add_child(makedialog);
234 }