Renamed EditorResourcePreviewGenerator.should_generate_small_preview() to generate_small_preview_automatically()

Added can_generate_small_preview() so the generator uses generate() or generate_from_path() if it returns true
Added can_generate_small_preview() and generate_small_preview_automatically() to the scripting languages
This commit is contained in:
MrCdK 2019-05-20 10:45:12 +02:00
parent a940b4cf64
commit f8a9844d80
5 changed files with 57 additions and 13 deletions

View file

@ -9,6 +9,14 @@
<tutorials> <tutorials>
</tutorials> </tutorials>
<methods> <methods>
<method name="can_generate_small_preview" qualifiers="virtual">
<return type="bool">
</return>
<description>
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.
</description>
</method>
<method name="generate" qualifiers="virtual"> <method name="generate" qualifiers="virtual">
<return type="Texture"> <return type="Texture">
</return> </return>
@ -35,6 +43,14 @@
Care must be taken because this function is always called from a thread (not the main thread). Care must be taken because this function is always called from a thread (not the main thread).
</description> </description>
</method> </method>
<method name="generate_small_preview_automatically" qualifiers="virtual">
<return type="bool">
</return>
<description>
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.
</description>
</method>
<method name="handles" qualifiers="virtual"> <method name="handles" qualifiers="virtual">
<return type="bool"> <return type="bool">
</return> </return>

View file

@ -71,7 +71,21 @@ Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String &p_
return generate(res, p_size); 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; 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(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", 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(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() { EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() {
@ -154,16 +170,27 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
} }
r_texture = generated; 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
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;
small_thumbnail_size *= EDSCALE;
if (preview_generators[i]->can_generate_small_preview()) {
Ref<Texture> 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<Image> small_image = r_texture->get_data(); Ref<Image> small_image = r_texture->get_data();
small_image = small_image->duplicate(); small_image = small_image->duplicate();
small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC); small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
r_small_texture.instance(); r_small_texture.instance();
r_small_texture->create_from_image(small_image); r_small_texture->create_from_image(small_image);
} }
break; break;
} }

View file

@ -48,7 +48,8 @@ public:
virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const; virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
virtual Ref<Texture> generate_from_path(const String &p_path, const Size2 p_size) const; virtual Ref<Texture> 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(); EditorResourcePreviewGenerator();
}; };

View file

@ -78,7 +78,7 @@ bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Texture"); return ClassDB::is_parent_class(p_type, "Texture");
} }
bool EditorTexturePreviewPlugin::should_generate_small_preview() const { bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const {
return true; return true;
} }
@ -186,7 +186,7 @@ Ref<Texture> EditorImagePreviewPlugin::generate(const RES &p_from, const Size2 p
EditorImagePreviewPlugin::EditorImagePreviewPlugin() { EditorImagePreviewPlugin::EditorImagePreviewPlugin() {
} }
bool EditorImagePreviewPlugin::should_generate_small_preview() const { bool EditorImagePreviewPlugin::generate_small_preview_automatically() const {
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
@ -250,7 +250,7 @@ Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from, const Size2
return ptex; return ptex;
} }
bool EditorBitmapPreviewPlugin::should_generate_small_preview() const { bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {
return true; return true;
} }
@ -317,7 +317,7 @@ bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Material"); //any material 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; return true;
} }

View file

@ -39,7 +39,7 @@ class EditorTexturePreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator) GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator)
public: public:
virtual bool handles(const String &p_type) const; virtual bool handles(const String &p_type) const;
virtual bool should_generate_small_preview() const; virtual bool generate_small_preview_automatically() const;
virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const; virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
EditorTexturePreviewPlugin(); EditorTexturePreviewPlugin();
@ -49,7 +49,7 @@ class EditorImagePreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorImagePreviewPlugin, EditorResourcePreviewGenerator) GDCLASS(EditorImagePreviewPlugin, EditorResourcePreviewGenerator)
public: public:
virtual bool handles(const String &p_type) const; virtual bool handles(const String &p_type) const;
virtual bool should_generate_small_preview() const; virtual bool generate_small_preview_automatically() const;
virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const; virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
EditorImagePreviewPlugin(); EditorImagePreviewPlugin();
@ -59,7 +59,7 @@ class EditorBitmapPreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorBitmapPreviewPlugin, EditorResourcePreviewGenerator) GDCLASS(EditorBitmapPreviewPlugin, EditorResourcePreviewGenerator)
public: public:
virtual bool handles(const String &p_type) const; virtual bool handles(const String &p_type) const;
virtual bool should_generate_small_preview() const; virtual bool generate_small_preview_automatically() const;
virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const; virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
EditorBitmapPreviewPlugin(); EditorBitmapPreviewPlugin();
@ -98,7 +98,7 @@ protected:
public: public:
virtual bool handles(const String &p_type) const; virtual bool handles(const String &p_type) const;
virtual bool should_generate_small_preview() const; virtual bool generate_small_preview_automatically() const;
virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const; virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
EditorMaterialPreviewPlugin(); EditorMaterialPreviewPlugin();