Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/docks/filesystem_dock.h
21152 views
1
/**************************************************************************/
2
/* filesystem_dock.h */
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
#pragma once
32
33
#include "editor/docks/editor_dock.h"
34
#include "editor/file_system/dependency_editor.h"
35
#include "editor/file_system/editor_file_system.h"
36
#include "editor/file_system/file_info.h"
37
#include "editor/script/script_create_dialog.h"
38
#include "editor/script/script_editor_plugin.h"
39
#include "scene/gui/box_container.h"
40
#include "scene/gui/control.h"
41
#include "scene/gui/dialogs.h"
42
#include "scene/gui/item_list.h"
43
#include "scene/gui/menu_button.h"
44
#include "scene/gui/split_container.h"
45
#include "scene/gui/tree.h"
46
47
class CreateDialog;
48
class DependencyEditor;
49
class DependencyEditorOwners;
50
class DependencyRemoveDialog;
51
class EditorDirDialog;
52
class HBoxContainer;
53
class LineEdit;
54
class ProgressBar;
55
class SceneCreateDialog;
56
class ShaderCreateDialog;
57
class DirectoryCreateDialog;
58
class EditorResourceTooltipPlugin;
59
class VBoxContainer;
60
61
class FileSystemTree : public Tree {
62
virtual Control *make_custom_tooltip(const String &p_text) const;
63
};
64
65
class FileSystemList : public ItemList {
66
GDCLASS(FileSystemList, ItemList);
67
68
bool popup_edit_committed = true;
69
VBoxContainer *popup_editor_vb = nullptr;
70
Popup *popup_editor = nullptr;
71
LineEdit *line_editor = nullptr;
72
73
virtual Control *make_custom_tooltip(const String &p_text) const override;
74
void _line_editor_submit(const String &p_text);
75
void _text_editor_popup_modal_close();
76
77
protected:
78
static void _bind_methods();
79
80
public:
81
bool edit_selected();
82
String get_edit_text();
83
84
FileSystemList();
85
};
86
87
class FileSystemDock : public EditorDock {
88
GDCLASS(FileSystemDock, EditorDock);
89
90
public:
91
enum FileListDisplayMode {
92
FILE_LIST_DISPLAY_THUMBNAILS,
93
FILE_LIST_DISPLAY_LIST
94
};
95
96
enum DisplayMode {
97
DISPLAY_MODE_TREE_ONLY,
98
DISPLAY_MODE_VSPLIT,
99
DISPLAY_MODE_HSPLIT,
100
};
101
102
enum Overwrite {
103
OVERWRITE_UNDECIDED,
104
OVERWRITE_REPLACE,
105
OVERWRITE_RENAME,
106
};
107
108
private:
109
enum FileMenu {
110
FILE_MENU_OPEN,
111
FILE_MENU_INHERIT,
112
FILE_MENU_MAIN_SCENE,
113
FILE_MENU_INSTANTIATE,
114
FILE_MENU_ADD_FAVORITE,
115
FILE_MENU_REMOVE_FAVORITE,
116
FILE_MENU_SHOW_IN_FILESYSTEM,
117
FILE_MENU_DEPENDENCIES,
118
FILE_MENU_OWNERS,
119
FILE_MENU_MOVE,
120
FILE_MENU_RENAME,
121
FILE_MENU_REMOVE,
122
FILE_MENU_DUPLICATE,
123
FILE_MENU_REIMPORT,
124
FILE_MENU_NEW,
125
FILE_MENU_SHOW_IN_EXPLORER,
126
FILE_MENU_OPEN_EXTERNAL,
127
FILE_MENU_OPEN_IN_TERMINAL,
128
FILE_MENU_COPY_PATH,
129
FILE_MENU_COPY_ABSOLUTE_PATH,
130
FILE_MENU_COPY_UID,
131
FILE_MENU_EXPAND_ALL,
132
FILE_MENU_COLLAPSE_ALL,
133
FILE_MENU_NEW_RESOURCE,
134
FILE_MENU_NEW_TEXTFILE,
135
FILE_MENU_NEW_FOLDER,
136
FILE_MENU_NEW_SCRIPT,
137
FILE_MENU_NEW_SCENE,
138
FILE_MENU_RUN_SCRIPT,
139
FILE_MENU_MAX,
140
// Extra shortcuts that don't exist in the menu.
141
EXTRA_FOCUS_PATH,
142
EXTRA_FOCUS_FILTER,
143
144
CONVERT_BASE_ID = 1000,
145
};
146
147
HashMap<String, TreeItem *> folder_map;
148
HashMap<String, Color> folder_colors;
149
Dictionary assigned_folder_colors;
150
151
FileSortOption file_sort = FileSortOption::FILE_SORT_NAME;
152
153
VBoxContainer *scanning_vb = nullptr;
154
ProgressBar *scanning_progress = nullptr;
155
SplitContainer *split_box = nullptr;
156
MarginContainer *tree_mc = nullptr;
157
MarginContainer *files_mc = nullptr;
158
VBoxContainer *file_list_vb = nullptr;
159
160
int split_box_offset_h = 0;
161
int split_box_offset_v = 0;
162
163
HashSet<String> favorites;
164
165
Button *button_toggle_display_mode = nullptr;
166
Button *button_file_list_display_mode = nullptr;
167
Button *button_hist_next = nullptr;
168
Button *button_hist_prev = nullptr;
169
LineEdit *current_path_line_edit = nullptr;
170
171
HBoxContainer *toolbar_hbc = nullptr;
172
HBoxContainer *toolbar2_hbc = nullptr;
173
LineEdit *tree_search_box = nullptr;
174
MenuButton *tree_button_sort = nullptr;
175
176
LineEdit *file_list_search_box = nullptr;
177
MenuButton *file_list_button_sort = nullptr;
178
179
PackedStringArray searched_tokens;
180
Vector<String> uncollapsed_paths_before_search;
181
182
HBoxContainer *path_hb = nullptr;
183
184
FileListDisplayMode file_list_display_mode;
185
DisplayMode display_mode;
186
DisplayMode old_display_mode;
187
188
bool horizontal = false;
189
190
PopupMenu *file_list_popup = nullptr;
191
PopupMenu *tree_popup = nullptr;
192
193
DependencyEditor *deps_editor = nullptr;
194
DependencyEditorOwners *owners_editor = nullptr;
195
DependencyRemoveDialog *remove_dialog = nullptr;
196
197
EditorDirDialog *move_dialog = nullptr;
198
DirectoryCreateDialog *make_dir_dialog = nullptr;
199
200
ConfirmationDialog *overwrite_dialog = nullptr;
201
ScrollContainer *overwrite_dialog_scroll = nullptr;
202
Label *overwrite_dialog_header = nullptr;
203
Label *overwrite_dialog_footer = nullptr;
204
Label *overwrite_dialog_file_list = nullptr;
205
206
ConfirmationDialog *conversion_dialog = nullptr;
207
ConfirmationDialog *move_confirm_dialog = nullptr;
208
209
SceneCreateDialog *make_scene_dialog = nullptr;
210
ScriptCreateDialog *make_script_dialog = nullptr;
211
ShaderCreateDialog *make_shader_dialog = nullptr;
212
CreateDialog *new_resource_dialog = nullptr;
213
214
String confirm_move_to_dir;
215
bool confirm_to_copy = false;
216
217
bool always_show_folders = false;
218
int thumbnail_size_setting = 0;
219
220
bool editor_is_dark_icon_and_font = false;
221
222
class FileOrFolder {
223
public:
224
String path;
225
bool is_file = false;
226
227
FileOrFolder() {}
228
FileOrFolder(const String &p_path, bool p_is_file) :
229
path(p_path),
230
is_file(p_is_file) {}
231
};
232
FileOrFolder to_rename;
233
FileOrFolder to_duplicate;
234
Vector<FileOrFolder> to_move;
235
String to_move_path;
236
bool to_move_or_copy = false;
237
238
Vector<String> to_convert;
239
int selected_conversion_id = 0;
240
241
Vector<String> history;
242
int history_pos;
243
int history_max_size;
244
245
String current_path = "res://";
246
String select_after_scan;
247
String main_scene_path;
248
249
bool updating_tree = false;
250
int tree_update_id;
251
FileSystemTree *tree = nullptr;
252
FileSystemList *files = nullptr;
253
bool import_dock_needs_update = false;
254
TreeItem *resources_item = nullptr;
255
TreeItem *favorites_item = nullptr;
256
Control *had_focus = nullptr;
257
258
bool holding_branch = false;
259
Vector<TreeItem *> tree_items_selected_on_drag_begin;
260
PackedInt32Array list_items_selected_on_drag_begin;
261
262
LocalVector<Ref<EditorResourceTooltipPlugin>> tooltip_plugins;
263
264
HashSet<String> cached_valid_conversion_targets;
265
266
Vector<String> prev_selection;
267
268
void _update_selection_changed();
269
270
void _tree_mouse_exited();
271
void _reselect_items_selected_on_drag_begin(bool reset = false);
272
273
Ref<Texture2D> _get_tree_item_icon(bool p_is_valid, const String &p_file_type, const String &p_icon_path);
274
void _create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites, bool p_unfold_path = false);
275
void _update_tree(const Vector<String> &p_uncollapsed_paths = Vector<String>(), bool p_uncollapse_root = false, bool p_scroll_to_selected = true);
276
void _navigate_to_path(const String &p_path, bool p_select_in_favorites = false, bool p_grab_focus = false);
277
bool _update_filtered_items(TreeItem *p_tree_item = nullptr);
278
void _append_favorite_items();
279
280
void _file_list_gui_input(Ref<InputEvent> p_event);
281
void _tree_gui_input(Ref<InputEvent> p_event);
282
283
HashSet<String> _get_valid_conversions_for_file_paths(const Vector<String> &p_paths);
284
285
void _update_file_list(bool p_keep_selection);
286
void _toggle_file_display();
287
void _set_file_display(bool p_active);
288
void _fs_changed();
289
290
void _select_file(const String &p_path, bool p_select_in_favorites = false, bool p_navigate = true);
291
void _tree_activate_file();
292
void _file_list_activate_file(int p_idx);
293
void _file_multi_selected(int p_index, bool p_selected);
294
void _tree_multi_selected(Object *p_item, int p_column, bool p_selected);
295
296
bool _get_imported_files(const String &p_path, String &r_extension, Vector<String> &r_files) const;
297
void _update_import_dock();
298
299
void _get_all_items_in_dir(EditorFileSystemDirectory *p_efsd, Vector<String> &r_files, Vector<String> &r_folders) const;
300
void _find_file_owners(EditorFileSystemDirectory *p_efsd, const HashSet<String> &p_renames, HashSet<String> &r_file_owners) const;
301
void _try_move_item(const FileOrFolder &p_item, const String &p_new_path, HashMap<String, String> &p_file_renames, HashMap<String, String> &p_folder_renames);
302
void _try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const;
303
void _before_move(HashMap<String, ResourceUID::ID> &r_uids, HashSet<String> &r_file_owners) const;
304
void _update_dependencies_after_move(const HashMap<String, String> &p_renames, const HashSet<String> &p_file_owners) const;
305
void _update_resource_paths_after_move(const HashMap<String, String> &p_renames, const HashMap<String, ResourceUID::ID> &p_uids) const;
306
void _update_favorites_after_move(const HashMap<String, String> &p_files_renames, const HashMap<String, String> &p_folders_renames) const;
307
void _update_project_settings_after_move(const HashMap<String, String> &p_renames, const HashMap<String, String> &p_folders_renames);
308
String _get_unique_name(const FileOrFolder &p_entry, const String &p_at_path);
309
310
void _update_folder_colors_setting();
311
312
void _resource_removed(const Ref<Resource> &p_resource);
313
void _file_removed(const String &p_file);
314
void _folder_removed(const String &p_folder);
315
316
void _resource_created();
317
void _script_or_shader_created(const Ref<Resource> &p_resource);
318
void _make_scene_confirm();
319
void _rename_operation_confirm();
320
void _duplicate_operation_confirm(const String &p_path);
321
void _move_confirm();
322
void _overwrite_dialog_action(bool p_overwrite);
323
void _convert_dialog_action();
324
Vector<String> _check_existing();
325
void _move_operation_confirm(const String &p_to_path, bool p_copy = false, Overwrite p_overwrite = OVERWRITE_UNDECIDED);
326
327
void _tree_rmb_option(int p_option);
328
void _file_list_rmb_option(int p_option);
329
void _generic_rmb_option_selected(int p_option);
330
void _file_option(int p_option, const Vector<String> &p_selected);
331
int _get_menu_option_from_key(const Ref<InputEventKey> &p_key);
332
333
void _fw_history();
334
void _bw_history();
335
void _update_history();
336
void _push_to_history();
337
338
void _set_scanning_mode();
339
void _rescan();
340
341
void _change_split_mode();
342
void _split_dragged(int p_offset);
343
344
void _search_changed(const String &p_text, const Control *p_from);
345
bool _matches_all_search_tokens(const String &p_text);
346
347
MenuButton *_create_file_menu_button();
348
void _file_sort_popup(int p_id);
349
350
void _folder_color_index_pressed(int p_index, PopupMenu *p_menu);
351
void _file_and_folders_fill_popup(PopupMenu *p_popup, const Vector<String> &p_paths, bool p_display_path_dependent_options = true);
352
void _tree_rmb_select(const Vector2 &p_pos, MouseButton p_button);
353
void _file_list_item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index);
354
void _file_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index);
355
void _tree_empty_click(const Vector2 &p_pos, MouseButton p_button);
356
void _tree_empty_selected();
357
358
void _search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items);
359
360
void _set_current_path_line_edit_text(const String &p_path);
361
362
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
363
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
364
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
365
void _get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const;
366
367
void _preview_invalidated(const String &p_path);
368
void _file_list_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_index, const String &p_filename);
369
void _tree_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_update_id, ObjectID p_item);
370
Ref<Texture2D> _apply_thumbnail_filter(const Ref<Texture2D> &p_thumbnail, const String &p_file_path) const;
371
372
void _update_display_mode(bool p_force = false);
373
374
Vector<String> _tree_get_selected(bool remove_self_inclusion = true, bool p_include_unselected_cursor = false) const;
375
Vector<String> _file_list_get_selected() const;
376
377
bool _is_file_type_disabled_by_feature_profile(const StringName &p_class);
378
379
void _feature_profile_changed();
380
void _project_settings_changed();
381
static Vector<String> _remove_self_included_paths(Vector<String> selected_strings);
382
383
private:
384
inline static FileSystemDock *singleton = nullptr;
385
386
public:
387
static FileSystemDock *get_singleton() { return singleton; }
388
static DependencyEditorOwners *get_owners_dialog() { return singleton->owners_editor; }
389
390
protected:
391
void _notification(int p_what);
392
static void _bind_methods();
393
394
virtual void update_layout(EditorDock::DockLayout p_layout) override;
395
virtual void save_layout_to_config(Ref<ConfigFile> &p_layout, const String &p_section) const override;
396
virtual void load_layout_from_config(const Ref<ConfigFile> &p_layout, const String &p_section) override;
397
398
public:
399
static constexpr double ITEM_COLOR_SCALE = 1.75;
400
static constexpr double ITEM_ALPHA_MIN = 0.1;
401
static constexpr double ITEM_ALPHA_MAX = 0.15;
402
static constexpr double ITEM_BG_DARK_SCALE = 0.3;
403
404
static Color get_dir_icon_color(const String &p_dir_path, const Color &p_default);
405
406
const HashMap<String, Color> &get_folder_colors() const;
407
Dictionary get_assigned_folder_colors() const;
408
409
Vector<String> get_selected_paths() const;
410
Vector<String> get_uncollapsed_paths() const;
411
412
String get_current_path() const;
413
String get_current_directory() const;
414
String get_folder_path_at_mouse_position() const;
415
416
void navigate_to_path(const String &p_path);
417
void focus_on_path();
418
void focus_on_filter();
419
void create_directory(const String &p_path, const String &p_base_dir);
420
421
ScriptCreateDialog *get_script_create_dialog() const;
422
423
void fix_dependencies(const String &p_for_file);
424
void update_all();
425
426
int get_h_split_offset() const { return split_box_offset_h; }
427
void set_h_split_offset(int p_offset) { split_box_offset_h = p_offset; }
428
int get_v_split_offset() const { return split_box_offset_v; }
429
void set_v_split_offset(int p_offset) { split_box_offset_v = p_offset; }
430
void select_file(const String &p_file);
431
432
void set_display_mode(DisplayMode p_display_mode);
433
DisplayMode get_display_mode() const { return display_mode; }
434
435
void set_file_sort(FileSortOption p_file_sort);
436
FileSortOption get_file_sort() const { return file_sort; }
437
438
void set_file_list_display_mode(FileListDisplayMode p_mode);
439
FileListDisplayMode get_file_list_display_mode() const { return file_list_display_mode; }
440
441
Tree *get_tree_control() { return tree; }
442
443
void add_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin);
444
void remove_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin);
445
Control *create_tooltip_for_path(const String &p_path) const;
446
447
FileSystemDock();
448
~FileSystemDock();
449
};
450
451
VARIANT_ENUM_CAST(FileSystemDock::Overwrite);
452
453