Parcourir la source

Merge pull request #53025 from Paulb23/textfile-ux

Rémi Verschelde il y a 4 ans
Parent
commit
3e1b630461

+ 3 - 0
editor/editor_export.cpp

@@ -451,6 +451,9 @@ void EditorExportPlatform::_export_find_resources(EditorFileSystemDirectory *p_d
 	}
 
 	for (int i = 0; i < p_dir->get_file_count(); i++) {
+		if (p_dir->get_file_type(i) == "TextFile") {
+			continue;
+		}
 		p_paths.insert(p_dir->get_file_path(i));
 	}
 }

+ 24 - 2
editor/editor_file_system.cpp

@@ -862,6 +862,9 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess
 			} else {
 				//new or modified time
 				fi->type = ResourceLoader::get_resource_type(path);
+				if (fi->type == "" && textfile_extensions.has(ext)) {
+					fi->type = "TextFile";
+				}
 				fi->uid = ResourceLoader::get_resource_uid(path);
 				fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
 				fi->deps = _get_dependencies(path);
@@ -984,6 +987,9 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const
 					fi->modified_time = FileAccess::get_modified_time(path);
 					fi->import_modified_time = 0;
 					fi->type = ResourceLoader::get_resource_type(path);
+					if (fi->type == "" && textfile_extensions.has(ext)) {
+						fi->type = "TextFile";
+					}
 					fi->script_class_name = _get_global_script_class(fi->type, path, &fi->script_class_extends, &fi->script_class_icon_path);
 					fi->import_valid = ResourceLoader::is_import_valid(path);
 					fi->import_group_file = ResourceLoader::get_import_group_file(path);
@@ -1539,6 +1545,9 @@ void EditorFileSystem::update_file(const String &p_file) {
 	}
 
 	String type = ResourceLoader::get_resource_type(p_file);
+	if (type == "" && textfile_extensions.has(p_file.get_extension())) {
+		type = "TextFile";
+	}
 	ResourceUID::ID uid = ResourceLoader::get_resource_uid(p_file);
 
 	if (cpos == -1) {
@@ -1556,7 +1565,7 @@ void EditorFileSystem::update_file(const String &p_file) {
 		EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
 		fi->file = file_name;
 		fi->import_modified_time = 0;
-		fi->import_valid = ResourceLoader::is_import_valid(p_file);
+		fi->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
 
 		if (idx == fs->files.size()) {
 			fs->files.push_back(fi);
@@ -1577,7 +1586,7 @@ void EditorFileSystem::update_file(const String &p_file) {
 	fs->files[cpos]->import_group_file = ResourceLoader::get_import_group_file(p_file);
 	fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
 	fs->files[cpos]->deps = _get_dependencies(p_file);
-	fs->files[cpos]->import_valid = ResourceLoader::is_import_valid(p_file);
+	fs->files[cpos]->import_valid = type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
 
 	if (uid != ResourceUID::INVALID_ID) {
 		if (ResourceUID::get_singleton()->has_id(uid)) {
@@ -1740,6 +1749,9 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
 		fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
 		fs->files[cpos]->deps = _get_dependencies(file);
 		fs->files[cpos]->type = importer->get_resource_type();
+		if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
+			fs->files[cpos]->type = "TextFile";
+		}
 		fs->files[cpos]->import_valid = err == OK;
 
 		//if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
@@ -2329,6 +2341,7 @@ void EditorFileSystem::_bind_methods() {
 void EditorFileSystem::_update_extensions() {
 	valid_extensions.clear();
 	import_extensions.clear();
+	textfile_extensions.clear();
 
 	List<String> extensionsl;
 	ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
@@ -2336,6 +2349,15 @@ void EditorFileSystem::_update_extensions() {
 		valid_extensions.insert(E);
 	}
 
+	const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+	for (const String &E : textfile_ext) {
+		if (valid_extensions.has(E)) {
+			continue;
+		}
+		valid_extensions.insert(E);
+		textfile_extensions.insert(E);
+	}
+
 	extensionsl.clear();
 	ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
 	for (const String &E : extensionsl) {

+ 1 - 0
editor/editor_file_system.h

@@ -190,6 +190,7 @@ class EditorFileSystem : public Node {
 
 	void _delete_internal_files(String p_file);
 
+	Set<String> textfile_extensions;
 	Set<String> valid_extensions;
 	Set<String> import_extensions;
 

+ 27 - 1
editor/editor_node.cpp

@@ -741,6 +741,21 @@ void EditorNode::_notification(int p_what) {
 				main_editor_buttons.write[i]->add_theme_font_size_override("font_size", gui_base->get_theme_font_size(SNAME("main_button_font_size"), SNAME("EditorFonts")));
 			}
 
+			Set<String> updated_textfile_extensions;
+			bool extensions_match = true;
+			const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+			for (const String &E : textfile_ext) {
+				updated_textfile_extensions.insert(E);
+				if (extensions_match && !textfile_extensions.has(E)) {
+					extensions_match = false;
+				}
+			}
+
+			if (!extensions_match || updated_textfile_extensions.size() < textfile_extensions.size()) {
+				textfile_extensions = updated_textfile_extensions;
+				EditorFileSystem::get_singleton()->scan();
+			}
+
 			_update_update_spinner();
 		} break;
 
@@ -1114,7 +1129,13 @@ Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_d
 	dependency_errors.clear();
 
 	Error err;
-	RES res = ResourceLoader::load(p_resource, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
+
+	RES res;
+	if (ResourceLoader::exists(p_resource, "")) {
+		res = ResourceLoader::load(p_resource, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
+	} else if (textfile_extensions.has(p_resource.get_extension())) {
+		res = ScriptEditor::get_singleton()->open_file(p_resource);
+	}
 	ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN);
 
 	if (!p_ignore_broken_deps && dependency_errors.has(p_resource)) {
@@ -5995,6 +6016,11 @@ EditorNode::EditorNode() {
 	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle", PROPERTY_USAGE_DEFAULT));
 	EDITOR_DEF("run/auto_save/save_before_running", true);
 
+	const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+	for (const String &E : textfile_ext) {
+		textfile_extensions.insert(E);
+	}
+
 	theme_base = memnew(Control);
 	add_child(theme_base);
 	theme_base->set_anchors_and_offsets_preset(Control::PRESET_WIDE);

+ 1 - 0
editor/editor_node.h

@@ -542,6 +542,7 @@ private:
 
 	String import_reload_fn;
 
+	Set<String> textfile_extensions;
 	Set<FileDialog *> file_dialogs;
 	Set<EditorFileDialog *> editor_file_dialogs;
 

+ 1 - 0
editor/editor_settings.cpp

@@ -460,6 +460,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
 	// FileSystem
 	EDITOR_SETTING(Variant::INT, PROPERTY_HINT_RANGE, "docks/filesystem/thumbnail_size", 64, "32,128,16")
 	_initial_set("docks/filesystem/always_show_folders", true);
+	_initial_set("docks/filesystem/textfile_extensions", "txt,md,cfg,ini,log,json,yml,yaml,toml");
 
 	// Property editor
 	_initial_set("docks/property_editor/auto_refresh_interval", 0.2); //update 5 times per second by default

+ 11 - 1
editor/filesystem_dock.cpp

@@ -106,7 +106,7 @@ bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
 		List<FileInfo> file_list;
 		for (int i = 0; i < p_dir->get_file_count(); i++) {
 			String file_type = p_dir->get_file_type(i);
-			if (_is_file_type_disabled_by_feature_profile(file_type)) {
+			if (file_type != "TextFile" && _is_file_type_disabled_by_feature_profile(file_type)) {
 				// If type is disabled, file won't be displayed.
 				continue;
 			}
@@ -1954,6 +1954,13 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
 		case FILE_NEW_RESOURCE: {
 			new_resource_dialog->popup_create(true);
 		} break;
+		case FILE_NEW_TEXTFILE: {
+			String fpath = path;
+			if (!fpath.ends_with("/")) {
+				fpath = fpath.get_base_dir();
+			}
+			ScriptEditor::get_singleton()->open_text_file_create_dialog(fpath);
+		} break;
 	}
 }
 
@@ -2473,6 +2480,7 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<Str
 			p_popup->add_icon_item(get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons")), TTR("New Scene..."), FILE_NEW_SCENE);
 			p_popup->add_icon_item(get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), TTR("New Script..."), FILE_NEW_SCRIPT);
 			p_popup->add_icon_item(get_theme_icon(SNAME("Object"), SNAME("EditorIcons")), TTR("New Resource..."), FILE_NEW_RESOURCE);
+			p_popup->add_icon_item(get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons")), TTR("New TextFile..."), FILE_NEW_TEXTFILE);
 			p_popup->add_separator();
 		}
 
@@ -2513,6 +2521,7 @@ void FileSystemDock::_tree_rmb_empty(const Vector2 &p_pos) {
 	tree_popup->add_icon_item(get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons")), TTR("New Scene..."), FILE_NEW_SCENE);
 	tree_popup->add_icon_item(get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), TTR("New Script..."), FILE_NEW_SCRIPT);
 	tree_popup->add_icon_item(get_theme_icon(SNAME("Object"), SNAME("EditorIcons")), TTR("New Resource..."), FILE_NEW_RESOURCE);
+	tree_popup->add_icon_item(get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons")), TTR("New TextFile..."), FILE_NEW_TEXTFILE);
 	tree_popup->set_position(tree->get_global_position() + p_pos);
 	tree_popup->popup();
 }
@@ -2558,6 +2567,7 @@ void FileSystemDock::_file_list_rmb_pressed(const Vector2 &p_pos) {
 	file_list_popup->add_icon_item(get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons")), TTR("New Scene..."), FILE_NEW_SCENE);
 	file_list_popup->add_icon_item(get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), TTR("New Script..."), FILE_NEW_SCRIPT);
 	file_list_popup->add_icon_item(get_theme_icon(SNAME("Object"), SNAME("EditorIcons")), TTR("New Resource..."), FILE_NEW_RESOURCE);
+	file_list_popup->add_icon_item(get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons")), TTR("New TextFile..."), FILE_NEW_TEXTFILE);
 	file_list_popup->add_separator();
 	file_list_popup->add_icon_item(get_theme_icon(SNAME("Filesystem"), SNAME("EditorIcons")), TTR("Open in File Manager"), FILE_SHOW_IN_EXPLORER);
 	file_list_popup->set_position(files->get_global_position() + p_pos);

+ 1 - 0
editor/filesystem_dock.h

@@ -101,6 +101,7 @@ private:
 		FILE_SHOW_IN_EXPLORER,
 		FILE_COPY_PATH,
 		FILE_NEW_RESOURCE,
+		FILE_NEW_TEXTFILE,
 		FOLDER_EXPAND_ALL,
 		FOLDER_COLLAPSE_ALL,
 	};

+ 22 - 8
editor/inspector_dock.cpp

@@ -204,11 +204,24 @@ void InspectorDock::_load_resource(const String &p_type) {
 		load_resource_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
 	}
 
+	const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+	for (int i = 0; i < textfile_ext.size(); i++) {
+		load_resource_dialog->add_filter("*." + textfile_ext[i] + " ; " + textfile_ext[i].to_upper());
+	}
+
 	load_resource_dialog->popup_file_dialog();
 }
 
 void InspectorDock::_resource_file_selected(String p_file) {
-	RES res = ResourceLoader::load(p_file);
+	RES res;
+	if (ResourceLoader::exists(p_file, "")) {
+		res = ResourceLoader::load(p_file);
+	} else {
+		const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+		if (textfile_ext.has(p_file.get_extension())) {
+			res = ScriptEditor::get_singleton()->open_file(p_file);
+		}
+	}
 
 	if (res.is_null()) {
 		warning_dialog->set_text(TTR("Failed to load resource."));
@@ -469,18 +482,19 @@ void InspectorDock::update(Object *p_object) {
 
 	const bool is_object = p_object != nullptr;
 	const bool is_resource = is_object && p_object->is_class("Resource");
+	const bool is_text_file = is_object && p_object->is_class("TextFile");
 	const bool is_node = is_object && p_object->is_class("Node");
 
-	object_menu->set_disabled(!is_object);
-	search->set_editable(is_object);
-	resource_save_button->set_disabled(!is_resource);
-	open_docs_button->set_disabled(!is_resource && !is_node);
+	object_menu->set_disabled(!is_object || is_text_file);
+	search->set_editable(is_object && !is_text_file);
+	resource_save_button->set_disabled(!is_resource || is_text_file);
+	open_docs_button->set_disabled(is_text_file || (!is_resource && !is_node));
 
 	PopupMenu *resource_extra_popup = resource_extra_button->get_popup();
-	resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_COPY), !is_resource);
-	resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_MAKE_BUILT_IN), !is_resource);
+	resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_COPY), !is_resource || is_text_file);
+	resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_MAKE_BUILT_IN), !is_resource || is_text_file);
 
-	if (!is_object) {
+	if (!is_object || is_text_file) {
 		warning->hide();
 		editor_path->clear_path();
 		return;

+ 56 - 24
editor/plugins/script_editor_plugin.cpp

@@ -1030,35 +1030,22 @@ void ScriptEditor::_file_dialog_action(String p_file) {
 			}
 			file->close();
 			memdelete(file);
-			[[fallthrough]];
-		}
-		case FILE_OPEN: {
-			List<String> extensions;
-			ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
-			if (extensions.find(p_file.get_extension())) {
-				Ref<Script> scr = ResourceLoader::load(p_file);
-				if (!scr.is_valid()) {
-					editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
-					file_dialog_option = -1;
-					return;
-				}
 
-				edit(scr);
-				file_dialog_option = -1;
-				return;
-			}
-
-			Error error;
-			Ref<TextFile> text_file = _load_text_file(p_file, &error);
-			if (error != OK) {
-				editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
+			if (EditorFileSystem::get_singleton()) {
+				const Vector<String> textfile_extensions = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+				if (textfile_extensions.has(p_file.get_extension())) {
+					EditorFileSystem::get_singleton()->update_file(p_file);
+				}
 			}
 
-			if (text_file.is_valid()) {
-				edit(text_file);
-				file_dialog_option = -1;
+			if (!open_textfile_after_create) {
 				return;
 			}
+			[[fallthrough]];
+		}
+		case FILE_OPEN: {
+			open_file(p_file);
+			file_dialog_option = -1;
 		} break;
 		case FILE_SAVE_AS: {
 			ScriptEditorBase *current = _get_current_editor();
@@ -1133,8 +1120,13 @@ void ScriptEditor::_menu_option(int p_option) {
 			file_dialog_option = FILE_NEW_TEXTFILE;
 
 			file_dialog->clear_filters();
+			const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+			for (int i = 0; i < textfile_ext.size(); i++) {
+				file_dialog->add_filter("*." + textfile_ext[i] + " ; " + textfile_ext[i].to_upper());
+			}
 			file_dialog->popup_file_dialog();
 			file_dialog->set_title(TTR("New Text File..."));
+			open_textfile_after_create = true;
 		} break;
 		case FILE_OPEN: {
 			file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
@@ -1148,6 +1140,11 @@ void ScriptEditor::_menu_option(int p_option) {
 				file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
 			}
 
+			const Vector<String> textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false);
+			for (int i = 0; i < textfile_ext.size(); i++) {
+				file_dialog->add_filter("*." + textfile_ext[i] + " ; " + textfile_ext[i].to_upper());
+			}
+
 			file_dialog->popup_file_dialog();
 			file_dialog->set_title(TTR("Open File"));
 			return;
@@ -2436,6 +2433,41 @@ void ScriptEditor::open_script_create_dialog(const String &p_base_name, const St
 	script_create_dialog->config(p_base_name, p_base_path);
 }
 
+void ScriptEditor::open_text_file_create_dialog(const String &p_base_path, const String &p_base_name) {
+	file_dialog->set_current_file(p_base_name);
+	file_dialog->set_current_dir(p_base_path);
+	_menu_option(FILE_NEW_TEXTFILE);
+	open_textfile_after_create = false;
+}
+
+RES ScriptEditor::open_file(const String &p_file) {
+	List<String> extensions;
+	ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
+	if (extensions.find(p_file.get_extension())) {
+		Ref<Script> scr = ResourceLoader::load(p_file);
+		if (!scr.is_valid()) {
+			editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
+			return RES();
+		}
+
+		edit(scr);
+		return scr;
+	}
+
+	Error error;
+	Ref<TextFile> text_file = _load_text_file(p_file, &error);
+	if (error != OK) {
+		editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
+		return RES();
+	}
+
+	if (text_file.is_valid()) {
+		edit(text_file);
+		return text_file;
+	}
+	return RES();
+}
+
 void ScriptEditor::_editor_stop() {
 	for (int i = 0; i < tab_container->get_child_count(); i++) {
 		ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));

+ 3 - 0
editor/plugins/script_editor_plugin.h

@@ -366,6 +366,7 @@ class ScriptEditor : public PanelContainer {
 	void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);
 	void _res_saved_callback(const Ref<Resource> &p_res);
 
+	bool open_textfile_after_create = true;
 	bool trim_trailing_whitespace_on_save;
 	bool use_space_indentation;
 	bool convert_indent_on_save;
@@ -472,6 +473,8 @@ public:
 	bool is_scripts_panel_toggled();
 	void apply_scripts() const;
 	void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
+	void open_text_file_create_dialog(const String &p_base_path, const String &p_base_name = "");
+	RES open_file(const String &p_file);
 
 	void ensure_select_current();
 

+ 3 - 0
editor/project_export.cpp

@@ -752,6 +752,9 @@ bool ProjectExportDialog::_fill_tree(EditorFileSystemDirectory *p_dir, TreeItem
 		if (p_only_scenes && type != "PackedScene") {
 			continue;
 		}
+		if (type == "TextFile") {
+			continue;
+		}
 
 		TreeItem *file = include_files->create_item(p_item);
 		file->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);