diff --git a/doc/classes/EditorResourcePreviewGenerator.xml b/doc/classes/EditorResourcePreviewGenerator.xml index 6592ffd1a865..76b944593337 100644 --- a/doc/classes/EditorResourcePreviewGenerator.xml +++ b/doc/classes/EditorResourcePreviewGenerator.xml @@ -9,6 +9,14 @@ + + + + + If this function returns true the generator will call [method generate] or [method generate_from_path] for small previews too. + By default it returns false. + + @@ -35,6 +43,14 @@ Care must be taken because this function is always called from a thread (not the main thread). + + + + + If this function returns true the generator will automatically generate the small previews from the normal preview texture generated by the methods [method generate] or [method generate_from_path]. + By default it returns false. + + diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 173333dac9e6..2baad8c904d0 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -71,7 +71,21 @@ Ref EditorResourcePreviewGenerator::generate_from_path(const String &p_ return generate(res, p_size); } -bool EditorResourcePreviewGenerator::should_generate_small_preview() const { +bool EditorResourcePreviewGenerator::generate_small_preview_automatically() const { + + if (get_script_instance() && get_script_instance()->has_method("generate_small_preview_automatically")) { + return get_script_instance()->call("generate_small_preview_automatically"); + } + + return false; +} + +bool EditorResourcePreviewGenerator::can_generate_small_preview() const { + + if (get_script_instance() && get_script_instance()->has_method("can_generate_small_preview")) { + return get_script_instance()->call("can_generate_small_preview"); + } + return false; } @@ -80,6 +94,8 @@ void EditorResourcePreviewGenerator::_bind_methods() { ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::STRING, "type"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture), "generate", PropertyInfo(Variant::OBJECT, "from", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::VECTOR2, "size"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture), "generate_from_path", PropertyInfo(Variant::STRING, "path", PROPERTY_HINT_FILE), PropertyInfo(Variant::VECTOR2, "size"))); + ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "generate_small_preview_automatically")); + ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "can_generate_small_preview")); } EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() { @@ -154,16 +170,27 @@ void EditorResourcePreview::_generate_preview(Ref &r_texture, Ref< } r_texture = generated; - if (r_texture.is_valid() && preview_generators[i]->should_generate_small_preview()) { - int small_thumbnail_size = EditorNode::get_singleton()->get_theme_base()->get_icon("Object", "EditorIcons")->get_width(); // Kind of a workaround to retrieve the default icon size - small_thumbnail_size *= EDSCALE; + int small_thumbnail_size = EditorNode::get_singleton()->get_theme_base()->get_icon("Object", "EditorIcons")->get_width(); // Kind of a workaround to retrieve the default icon size + small_thumbnail_size *= EDSCALE; + if (preview_generators[i]->can_generate_small_preview()) { + Ref generated_small; + if (p_item.resource.is_valid()) { + generated_small = preview_generators[i]->generate(p_item.resource, Vector2(small_thumbnail_size, small_thumbnail_size)); + } else { + generated_small = preview_generators[i]->generate_from_path(p_item.path, Vector2(small_thumbnail_size, small_thumbnail_size)); + } + r_small_texture = generated_small; + } + + if (!r_small_texture.is_valid() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) { Ref small_image = r_texture->get_data(); small_image = small_image->duplicate(); small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC); r_small_texture.instance(); r_small_texture->create_from_image(small_image); } + break; } diff --git a/editor/editor_resource_preview.h b/editor/editor_resource_preview.h index 9b9223a818f6..e0fd54c92447 100644 --- a/editor/editor_resource_preview.h +++ b/editor/editor_resource_preview.h @@ -48,7 +48,8 @@ public: virtual Ref generate(const RES &p_from, const Size2 p_size) const; virtual Ref generate_from_path(const String &p_path, const Size2 p_size) const; - virtual bool should_generate_small_preview() const; + virtual bool generate_small_preview_automatically() const; + virtual bool can_generate_small_preview() const; EditorResourcePreviewGenerator(); }; diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 58d796872338..28e57ac48ab3 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -78,7 +78,7 @@ bool EditorTexturePreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Texture"); } -bool EditorTexturePreviewPlugin::should_generate_small_preview() const { +bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const { return true; } @@ -186,7 +186,7 @@ Ref EditorImagePreviewPlugin::generate(const RES &p_from, const Size2 p EditorImagePreviewPlugin::EditorImagePreviewPlugin() { } -bool EditorImagePreviewPlugin::should_generate_small_preview() const { +bool EditorImagePreviewPlugin::generate_small_preview_automatically() const { return true; } //////////////////////////////////////////////////////////////////////////// @@ -250,7 +250,7 @@ Ref EditorBitmapPreviewPlugin::generate(const RES &p_from, const Size2 return ptex; } -bool EditorBitmapPreviewPlugin::should_generate_small_preview() const { +bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const { return true; } @@ -317,7 +317,7 @@ bool EditorMaterialPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Material"); //any material } -bool EditorMaterialPreviewPlugin::should_generate_small_preview() const { +bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const { return true; } diff --git a/editor/plugins/editor_preview_plugins.h b/editor/plugins/editor_preview_plugins.h index ed2c003a0a9f..16b1f3082b4b 100644 --- a/editor/plugins/editor_preview_plugins.h +++ b/editor/plugins/editor_preview_plugins.h @@ -39,7 +39,7 @@ class EditorTexturePreviewPlugin : public EditorResourcePreviewGenerator { GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator) public: virtual bool handles(const String &p_type) const; - virtual bool should_generate_small_preview() const; + virtual bool generate_small_preview_automatically() const; virtual Ref generate(const RES &p_from, const Size2 p_size) const; EditorTexturePreviewPlugin(); @@ -49,7 +49,7 @@ class EditorImagePreviewPlugin : public EditorResourcePreviewGenerator { GDCLASS(EditorImagePreviewPlugin, EditorResourcePreviewGenerator) public: virtual bool handles(const String &p_type) const; - virtual bool should_generate_small_preview() const; + virtual bool generate_small_preview_automatically() const; virtual Ref generate(const RES &p_from, const Size2 p_size) const; EditorImagePreviewPlugin(); @@ -59,7 +59,7 @@ class EditorBitmapPreviewPlugin : public EditorResourcePreviewGenerator { GDCLASS(EditorBitmapPreviewPlugin, EditorResourcePreviewGenerator) public: virtual bool handles(const String &p_type) const; - virtual bool should_generate_small_preview() const; + virtual bool generate_small_preview_automatically() const; virtual Ref generate(const RES &p_from, const Size2 p_size) const; EditorBitmapPreviewPlugin(); @@ -98,7 +98,7 @@ protected: public: virtual bool handles(const String &p_type) const; - virtual bool should_generate_small_preview() const; + virtual bool generate_small_preview_automatically() const; virtual Ref generate(const RES &p_from, const Size2 p_size) const; EditorMaterialPreviewPlugin();