Add Gradient resource preview generator

Co-Authored-By: Haoyu Qiu <timothyqiu32@gmail.com>
This commit is contained in:
Hendrik Brucker 2022-11-22 13:25:54 +08:00 committed by Relintai
parent cfc4f0f050
commit b85a3cbce3
4 changed files with 42 additions and 1 deletions

View File

@ -6971,6 +6971,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

@ -118,7 +118,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_theme_stylebox("normal")->get_default_margin(MARGIN_LEFT) + get_theme_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

@ -956,3 +956,32 @@ EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {
RS::get_singleton()->free(canvas);
RS::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

@ -183,3 +183,14 @@ public:
~EditorFontPreviewPlugin();
};
#endif // EDITORPREVIEWPLUGINS_H
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();
};