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 "core/io/dir_access.h" |
34 | #include "core/os/keyboard.h" |
35 | #include "core/os/os.h" |
36 | #include "editor/editor_file_system.h" |
37 | #include "editor/editor_scale.h" |
38 | #include "editor/editor_settings.h" |
39 | #include "scene/gui/check_box.h" |
40 | #include "scene/gui/tree.h" |
41 | #include "servers/display_server.h" |
42 | |
43 | void EditorDirDialog::_update_dir(TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) { |
44 | updating = true; |
45 | |
46 | String path = p_dir->get_path(); |
47 | |
48 | p_item->set_metadata(0, p_dir->get_path()); |
49 | p_item->set_icon(0, tree->get_editor_theme_icon(SNAME("Folder" ))); |
50 | p_item->set_icon_modulate(0, tree->get_theme_color(SNAME("folder_icon_color" ), SNAME("FileDialog" ))); |
51 | |
52 | if (!p_item->get_parent()) { |
53 | p_item->set_text(0, "res://" ); |
54 | } else { |
55 | if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) { |
56 | p_item->set_collapsed(true); |
57 | } |
58 | |
59 | p_item->set_text(0, p_dir->get_name()); |
60 | } |
61 | |
62 | updating = false; |
63 | for (int i = 0; i < p_dir->get_subdir_count(); i++) { |
64 | TreeItem *ti = tree->create_item(p_item); |
65 | _update_dir(ti, p_dir->get_subdir(i)); |
66 | } |
67 | } |
68 | |
69 | void EditorDirDialog::reload(const String &p_path) { |
70 | if (!is_visible()) { |
71 | must_reload = true; |
72 | return; |
73 | } |
74 | |
75 | tree->clear(); |
76 | TreeItem *root = tree->create_item(); |
77 | _update_dir(root, EditorFileSystem::get_singleton()->get_filesystem(), p_path); |
78 | _item_collapsed(root); |
79 | must_reload = false; |
80 | } |
81 | |
82 | bool EditorDirDialog::is_copy_pressed() const { |
83 | return copy->is_pressed(); |
84 | } |
85 | |
86 | void EditorDirDialog::_notification(int p_what) { |
87 | switch (p_what) { |
88 | case NOTIFICATION_ENTER_TREE: { |
89 | EditorFileSystem::get_singleton()->connect("filesystem_changed" , callable_mp(this, &EditorDirDialog::reload).bind("" )); |
90 | reload(); |
91 | |
92 | if (!tree->is_connected("item_collapsed" , callable_mp(this, &EditorDirDialog::_item_collapsed))) { |
93 | tree->connect("item_collapsed" , callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED); |
94 | } |
95 | |
96 | if (!EditorFileSystem::get_singleton()->is_connected("filesystem_changed" , callable_mp(this, &EditorDirDialog::reload))) { |
97 | EditorFileSystem::get_singleton()->connect("filesystem_changed" , callable_mp(this, &EditorDirDialog::reload).bind("" )); |
98 | } |
99 | } break; |
100 | |
101 | case NOTIFICATION_EXIT_TREE: { |
102 | if (EditorFileSystem::get_singleton()->is_connected("filesystem_changed" , callable_mp(this, &EditorDirDialog::reload))) { |
103 | EditorFileSystem::get_singleton()->disconnect("filesystem_changed" , callable_mp(this, &EditorDirDialog::reload)); |
104 | } |
105 | } break; |
106 | |
107 | case NOTIFICATION_VISIBILITY_CHANGED: { |
108 | if (must_reload && is_visible()) { |
109 | reload(); |
110 | } |
111 | } break; |
112 | } |
113 | } |
114 | |
115 | void EditorDirDialog::_copy_toggled(bool p_pressed) { |
116 | if (p_pressed) { |
117 | set_ok_button_text(TTR("Copy" )); |
118 | } else { |
119 | set_ok_button_text(TTR("Move" )); |
120 | } |
121 | } |
122 | |
123 | void EditorDirDialog::_item_collapsed(Object *p_item) { |
124 | TreeItem *item = Object::cast_to<TreeItem>(p_item); |
125 | |
126 | if (updating) { |
127 | return; |
128 | } |
129 | |
130 | if (item->is_collapsed()) { |
131 | opened_paths.erase(item->get_metadata(0)); |
132 | } else { |
133 | opened_paths.insert(item->get_metadata(0)); |
134 | } |
135 | } |
136 | |
137 | void EditorDirDialog::_item_activated() { |
138 | _ok_pressed(); // From AcceptDialog. |
139 | } |
140 | |
141 | void EditorDirDialog::ok_pressed() { |
142 | TreeItem *ti = tree->get_selected(); |
143 | if (!ti) { |
144 | return; |
145 | } |
146 | |
147 | String dir = ti->get_metadata(0); |
148 | emit_signal(SNAME("dir_selected" ), dir); |
149 | hide(); |
150 | } |
151 | |
152 | void EditorDirDialog::_make_dir() { |
153 | TreeItem *ti = tree->get_selected(); |
154 | if (!ti) { |
155 | mkdirerr->set_text(TTR("Please select a base directory first." )); |
156 | mkdirerr->popup_centered(); |
157 | return; |
158 | } |
159 | |
160 | makedialog->popup_centered(Size2(250, 80)); |
161 | makedirname->grab_focus(); |
162 | } |
163 | |
164 | void EditorDirDialog::_make_dir_confirm() { |
165 | TreeItem *ti = tree->get_selected(); |
166 | if (!ti) { |
167 | return; |
168 | } |
169 | |
170 | String dir = ti->get_metadata(0); |
171 | |
172 | Ref<DirAccess> d = DirAccess::open(dir); |
173 | ERR_FAIL_COND_MSG(d.is_null(), "Cannot open directory '" + dir + "'." ); |
174 | |
175 | const String stripped_dirname = makedirname->get_text().strip_edges(); |
176 | |
177 | if (d->dir_exists(stripped_dirname)) { |
178 | mkdirerr->set_text(TTR("Could not create folder. File with that name already exists." )); |
179 | mkdirerr->popup_centered(); |
180 | return; |
181 | } |
182 | |
183 | Error err = d->make_dir(stripped_dirname); |
184 | if (err != OK) { |
185 | mkdirerr->popup_centered(Size2(250, 80) * EDSCALE); |
186 | } else { |
187 | opened_paths.insert(dir); |
188 | EditorFileSystem::get_singleton()->scan_changes(); // We created a dir, so rescan changes. |
189 | } |
190 | makedirname->set_text("" ); // reset label |
191 | } |
192 | |
193 | void EditorDirDialog::_bind_methods() { |
194 | ADD_SIGNAL(MethodInfo("dir_selected" , PropertyInfo(Variant::STRING, "dir" ))); |
195 | } |
196 | |
197 | EditorDirDialog::EditorDirDialog() { |
198 | set_title(TTR("Choose a Directory" )); |
199 | set_hide_on_ok(false); |
200 | |
201 | VBoxContainer *vb = memnew(VBoxContainer); |
202 | add_child(vb); |
203 | |
204 | tree = memnew(Tree); |
205 | vb->add_child(tree); |
206 | tree->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
207 | tree->connect("item_activated" , callable_mp(this, &EditorDirDialog::_item_activated)); |
208 | |
209 | copy = memnew(CheckBox); |
210 | vb->add_child(copy); |
211 | copy->set_text(TTR("Copy File(s)" )); |
212 | copy->connect("toggled" , callable_mp(this, &EditorDirDialog::_copy_toggled)); |
213 | |
214 | makedir = add_button(TTR("Create Folder" ), DisplayServer::get_singleton()->get_swap_cancel_ok(), "makedir" ); |
215 | makedir->connect("pressed" , callable_mp(this, &EditorDirDialog::_make_dir)); |
216 | |
217 | makedialog = memnew(ConfirmationDialog); |
218 | makedialog->set_title(TTR("Create Folder" )); |
219 | add_child(makedialog); |
220 | |
221 | VBoxContainer *makevb = memnew(VBoxContainer); |
222 | makedialog->add_child(makevb); |
223 | |
224 | makedirname = memnew(LineEdit); |
225 | makevb->add_margin_child(TTR("Name:" ), makedirname); |
226 | makedialog->register_text_enter(makedirname); |
227 | makedialog->connect("confirmed" , callable_mp(this, &EditorDirDialog::_make_dir_confirm)); |
228 | |
229 | mkdirerr = memnew(AcceptDialog); |
230 | mkdirerr->set_text(TTR("Could not create folder." )); |
231 | add_child(mkdirerr); |
232 | |
233 | set_ok_button_text(TTR("Move" )); |
234 | } |
235 | |