Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_file_dialog.cpp
20852 views
1
/**************************************************************************/
2
/* editor_file_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_file_dialog.h"
32
33
#include "core/config/project_settings.h"
34
#include "editor/docks/filesystem_dock.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/file_system/dependency_editor.h"
37
#include "editor/settings/editor_settings.h"
38
#include "editor/themes/editor_scale.h"
39
40
void EditorFileDialog::_item_menu_id_pressed(int p_option) {
41
// Use dependency dialog to delete the entry in the editor, but only for project files.
42
if (p_option == ITEM_MENU_DELETE && get_access() == ACCESS_RESOURCES) {
43
const PackedInt32Array selected = get_file_item_list()->get_selected_items();
44
if (selected.is_empty()) {
45
return;
46
}
47
48
if (!dependency_remove_dialog) {
49
dependency_remove_dialog = memnew(DependencyRemoveDialog);
50
add_child(dependency_remove_dialog);
51
}
52
53
const Dictionary meta = get_file_item_list()->get_item_metadata(selected[0]);
54
const String delete_path = dir_access->get_current_dir().path_join(meta["name"]);
55
56
if (meta["dir"]) {
57
dependency_remove_dialog->show(Vector<String>{ delete_path }, Vector<String>());
58
} else {
59
dependency_remove_dialog->show(Vector<String>(), Vector<String>{ delete_path });
60
}
61
return;
62
}
63
FileDialog::_item_menu_id_pressed(p_option);
64
}
65
66
bool EditorFileDialog::_should_use_native_popup() const {
67
#ifdef ANDROID_ENABLED
68
// Native file dialog on Android, returns a file URI instead of a path and does not support res://, user://, or options. This requires editor-side changes to handle properly, so disabling it for now.
69
return false;
70
#else
71
return _can_use_native_popup() && (OS::get_singleton()->is_sandboxed() || EDITOR_GET("interface/editor/use_native_file_dialogs").operator bool());
72
#endif
73
}
74
75
bool EditorFileDialog::_should_hide_file(const String &p_file) const {
76
if (get_access() != FileDialog::ACCESS_RESOURCES) {
77
return false;
78
}
79
const String full_path = dir_access->get_current_dir().path_join(p_file);
80
return EditorFileSystem::_should_skip_directory(full_path);
81
}
82
83
Color EditorFileDialog::_get_folder_color(const String &p_path) const {
84
return FileSystemDock::get_dir_icon_color(p_path, FileDialog::_get_folder_color(p_path));
85
}
86
87
Vector2i EditorFileDialog::_get_list_mode_icon_size() const {
88
return Vector2i();
89
}
90
91
void EditorFileDialog::_bind_methods() {
92
#ifndef DISABLE_DEPRECATED
93
ClassDB::bind_method(D_METHOD("add_side_menu", "menu", "title"), &EditorFileDialog::add_side_menu, DEFVAL(""));
94
ClassDB::bind_method(D_METHOD("set_disable_overwrite_warning", "disable"), &EditorFileDialog::set_disable_overwrite_warning);
95
ClassDB::bind_method(D_METHOD("is_overwrite_warning_disabled"), &EditorFileDialog::is_overwrite_warning_disabled);
96
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_overwrite_warning", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_disable_overwrite_warning", "is_overwrite_warning_disabled");
97
#endif
98
}
99
100
void EditorFileDialog::_validate_property(PropertyInfo &p_property) const {
101
// Hide properties controlled by editor settings.
102
if (p_property.name == "use_native_dialog" || p_property.name == "show_hidden_files" || p_property.name == "display_mode") {
103
p_property.usage = PROPERTY_USAGE_NONE;
104
}
105
}
106
107
void EditorFileDialog::_dir_contents_changed() {
108
if (!EditorFileSystem::get_singleton()) {
109
return;
110
}
111
112
bool scan_required = false;
113
switch (get_access()) {
114
case FileDialog::ACCESS_RESOURCES: {
115
scan_required = true;
116
} break;
117
case FileDialog::ACCESS_USERDATA: {
118
// Directories within the project dir are unlikely to be accessed.
119
} break;
120
case FileDialog::ACCESS_FILESYSTEM: {
121
// Directories within the project dir may still be accessed.
122
const String localized_path = ProjectSettings::get_singleton()->localize_path(get_current_dir());
123
scan_required = localized_path.is_resource_file();
124
} break;
125
}
126
if (scan_required) {
127
EditorFileSystem::get_singleton()->scan_changes();
128
}
129
}
130
131
void EditorFileDialog::_notification(int p_what) {
132
switch (p_what) {
133
case NOTIFICATION_VISIBILITY_CHANGED: {
134
if (!is_visible()) {
135
// Synchronize back favorites and recent directories if they have changed.
136
if (favorites_changed) {
137
Vector<String> settings_favorites = EditorSettings::get_singleton()->get_favorites();
138
Vector<String> current_favorites = get_favorite_list();
139
LocalVector<String> to_erase;
140
141
// The favorite list in EditorSettings may have files in between. They need to be handled properly to preserve order.
142
for (const String &fav : settings_favorites) {
143
if (!fav.ends_with("/")) {
144
continue;
145
}
146
int64_t idx = current_favorites.find(fav);
147
if (idx == -1) {
148
to_erase.push_back(fav);
149
} else {
150
current_favorites.remove_at(idx);
151
}
152
}
153
for (const String &fav : to_erase) {
154
settings_favorites.erase(fav);
155
}
156
settings_favorites.append_array(current_favorites);
157
EditorSettings::get_singleton()->set_favorites(settings_favorites, false);
158
}
159
if (recents_changed) {
160
EditorSettings::get_singleton()->set_recent_dirs(get_recent_list(), false);
161
}
162
}
163
} break;
164
165
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
166
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("filesystem/file_dialog")) {
167
break;
168
}
169
set_show_hidden_files(EDITOR_GET("filesystem/file_dialog/show_hidden_files"));
170
set_display_mode((DisplayMode)EDITOR_GET("filesystem/file_dialog/display_mode").operator int());
171
} break;
172
}
173
}
174
175