Merge pull request #68990 from timothyqiu/editor-gradient-preview-plugin

[3.x] Add Gradient resource preview generator
This commit is contained in:
Rémi Verschelde 2022-11-22 08:45:13 +01:00
commit cb0b20be7f
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 42 additions and 1 deletions

View file

@ -7065,6 +7065,7 @@ EditorNode::EditorNode() {
resource_preview->add_preview_generator(Ref<EditorMeshPreviewPlugin>(memnew(EditorMeshPreviewPlugin)));
resource_preview->add_preview_generator(Ref<EditorBitmapPreviewPlugin>(memnew(EditorBitmapPreviewPlugin)));
resource_preview->add_preview_generator(Ref<EditorFontPreviewPlugin>(memnew(EditorFontPreviewPlugin)));
resource_preview->add_preview_generator(Ref<EditorGradientPreviewPlugin>(memnew(EditorGradientPreviewPlugin)));
{
Ref<SpatialMaterialConversionPlugin> spatial_mat_convert;

View file

@ -86,7 +86,7 @@ void EditorResourcePicker::_update_resource_preview(const String &p_path, const
if (p_preview.is_valid()) {
preview_rect->set_margin(MARGIN_LEFT, assign_button->get_icon()->get_width() + assign_button->get_stylebox("normal")->get_default_margin(MARGIN_LEFT) + get_constant("hseparation", "Button"));
if (type == "GradientTexture") {
if (type == "GradientTexture" || type == "Gradient") {
preview_rect->set_stretch_mode(TextureRect::STRETCH_SCALE);
assign_button->set_custom_minimum_size(Size2(1, 1));
} else {

View file

@ -931,3 +931,32 @@ EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {
VS::get_singleton()->free(canvas);
VS::get_singleton()->free(viewport);
}
///////////////////////////////////////////////////////////////////////////
static const real_t GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR = 4.0;
bool EditorGradientPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Gradient");
}
bool EditorGradientPreviewPlugin::generate_small_preview_automatically() const {
return true;
}
Ref<Texture> EditorGradientPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size) const {
Ref<Gradient> gradient = p_from;
if (gradient.is_valid()) {
Ref<GradientTexture> ptex = Ref<GradientTexture>(memnew(GradientTexture));
ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE);
ptex->set_gradient(gradient);
Ref<ImageTexture> image = Ref<ImageTexture>(memnew(ImageTexture));
image->create_from_image(ptex->get_data());
return image;
}
return Ref<Texture>();
}
EditorGradientPreviewPlugin::EditorGradientPreviewPlugin() {
}

View file

@ -174,4 +174,15 @@ public:
~EditorFontPreviewPlugin();
};
class EditorGradientPreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorGradientPreviewPlugin, EditorResourcePreviewGenerator);
public:
virtual bool handles(const String &p_type) const;
virtual bool generate_small_preview_automatically() const;
virtual Ref<Texture> generate(const Ref<Resource> &p_from, const Size2 &p_size) const;
EditorGradientPreviewPlugin();
};
#endif // EDITOR_PREVIEW_PLUGINS_H