From 2904d1906902e38bc3f738d4b81d0d777266e299 Mon Sep 17 00:00:00 2001 From: TechnoPorg Date: Sat, 14 Jan 2023 13:02:34 -0700 Subject: [PATCH] Make EditorResourceConversionPlugin usable. Previously, EditorResourceConversionPlugin nominally existed in scripting, but there was no way for a user to actually register one. This PR adds methods to EditorPlugin for registering/unregistering EditorResourceConversionPlugins. Additionally, it documents EditorResourceConversionPlugin with descriptions and a basic example. --- doc/classes/EditorPlugin.xml | 15 ++++++++++++ .../EditorResourceConversionPlugin.xml | 23 +++++++++++++++++++ editor/editor_plugin.cpp | 11 +++++++++ editor/editor_plugin.h | 4 ++++ 4 files changed, 53 insertions(+) diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index 2124a97751a..370be8e9f34 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -467,6 +467,14 @@ See [method add_inspector_plugin] for an example of how to register a plugin. + + + + + Registers a new [EditorResourceConversionPlugin]. Resource conversion plugins are used to add custom resource converters to the editor inspector. + See [EditorResourceConversionPlugin] for an example of how to create a resource conversion plugin. + + @@ -632,6 +640,13 @@ Removes a gizmo plugin registered by [method add_node_3d_gizmo_plugin]. + + + + + Removes a resource conversion plugin registered by [method add_resource_conversion_plugin]. + + diff --git a/doc/classes/EditorResourceConversionPlugin.xml b/doc/classes/EditorResourceConversionPlugin.xml index c40bb1d91e1..6bdfbbe40c5 100644 --- a/doc/classes/EditorResourceConversionPlugin.xml +++ b/doc/classes/EditorResourceConversionPlugin.xml @@ -1,8 +1,28 @@ + Plugin for adding custom converters from one resource format to another in the editor resource picker context menu; for example, converting a [StandardMaterial3D] to a [ShaderMaterial]. + [EditorResourceConversionPlugin] is invoked when the context menu is brought up for a resource in the editor inspector. Relevant conversion plugins will appear as menu options to convert the given resource to a target type. + Below shows an example of a basic plugin that will convert an [ImageTexture] to a [PortableCompressedTexture2D]. + [codeblocks] + [gdscript] + extends EditorResourceConversionPlugin + + func _handles(resource : Resource): + return resource is ImageTexture + + func _converts_to(): + return "PortableCompressedTexture2D" + + func _convert(itex : Resource): + var ptex = PortableCompressedTexture2D.new() + ptex.create_from_image(itex.get_image(), PortableCompressedTexture2D.COMPRESSION_MODE_LOSSLESS) + return ptex + [/gdscript] + [/codeblocks] + To use an [EditorResourceConversionPlugin], register it using the [method EditorPlugin.add_resource_conversion_plugin] method first. @@ -11,17 +31,20 @@ + Takes an input [Resource] and converts it to the type given in [method _converts_to]. The returned [Resource] is the result of the conversion, and the input [Resource] remains unchanged. + Returns the class name of the target type of [Resource] that this plugin converts source resources to. + Called to determine whether a particular [Resource] can be converted to the target resource type by this plugin. diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index a5d1ddb2fa2..47658ced621 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -45,6 +45,7 @@ #include "editor/inspector_dock.h" #include "editor/plugins/canvas_item_editor_plugin.h" #include "editor/plugins/editor_debugger_plugin.h" +#include "editor/plugins/editor_resource_conversion_plugin.h" #include "editor/plugins/node_3d_editor_plugin.h" #include "editor/plugins/script_editor_plugin.h" #include "editor/project_settings_editor.h" @@ -856,6 +857,14 @@ void EditorPlugin::remove_debugger_plugin(const Ref &p_plu EditorDebuggerNode::get_singleton()->remove_debugger_plugin(p_plugin); } +void EditorPlugin::add_resource_conversion_plugin(const Ref &p_plugin) { + EditorNode::get_singleton()->add_resource_conversion_plugin(p_plugin); +} + +void EditorPlugin::remove_resource_conversion_plugin(const Ref &p_plugin) { + EditorNode::get_singleton()->remove_resource_conversion_plugin(p_plugin); +} + void EditorPlugin::_editor_project_settings_changed() { emit_signal(SNAME("project_settings_changed")); } @@ -911,6 +920,8 @@ void EditorPlugin::_bind_methods() { ClassDB::bind_method(D_METHOD("remove_node_3d_gizmo_plugin", "plugin"), &EditorPlugin::remove_node_3d_gizmo_plugin); ClassDB::bind_method(D_METHOD("add_inspector_plugin", "plugin"), &EditorPlugin::add_inspector_plugin); ClassDB::bind_method(D_METHOD("remove_inspector_plugin", "plugin"), &EditorPlugin::remove_inspector_plugin); + ClassDB::bind_method(D_METHOD("add_resource_conversion_plugin", "plugin"), &EditorPlugin::add_resource_conversion_plugin); + ClassDB::bind_method(D_METHOD("remove_resource_conversion_plugin", "plugin"), &EditorPlugin::remove_resource_conversion_plugin); ClassDB::bind_method(D_METHOD("set_input_event_forwarding_always_enabled"), &EditorPlugin::set_input_event_forwarding_always_enabled); ClassDB::bind_method(D_METHOD("set_force_draw_over_forwarding_enabled"), &EditorPlugin::set_force_draw_over_forwarding_enabled); diff --git a/editor/editor_plugin.h b/editor/editor_plugin.h index 33081e867a1..36c778a1bec 100644 --- a/editor/editor_plugin.h +++ b/editor/editor_plugin.h @@ -48,6 +48,7 @@ class EditorInspector; class EditorInspectorPlugin; class EditorNode3DGizmoPlugin; class EditorPaths; +class EditorResourceConversionPlugin; class EditorResourcePreview; class EditorSceneFormatImporter; class EditorScenePostImportPlugin; @@ -307,6 +308,9 @@ public: void add_debugger_plugin(const Ref &p_plugin); void remove_debugger_plugin(const Ref &p_plugin); + void add_resource_conversion_plugin(const Ref &p_plugin); + void remove_resource_conversion_plugin(const Ref &p_plugin); + void enable_plugin(); void disable_plugin();