From b85a3cbce35bf67d746c57d979236e511a2b2627 Mon Sep 17 00:00:00 2001 From: Hendrik Brucker Date: Tue, 22 Nov 2022 13:25:54 +0800 Subject: [PATCH] Add Gradient resource preview generator Co-Authored-By: Haoyu Qiu --- editor/editor_node.cpp | 1 + editor/editor_resource_picker.cpp | 2 +- editor/plugins/editor_preview_plugins.cpp | 29 +++++++++++++++++++++++ editor/plugins/editor_preview_plugins.h | 11 +++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 2c5ec6cae..a2d0e0541 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -6971,6 +6971,7 @@ EditorNode::EditorNode() { resource_preview->add_preview_generator(Ref(memnew(EditorMeshPreviewPlugin))); resource_preview->add_preview_generator(Ref(memnew(EditorBitmapPreviewPlugin))); resource_preview->add_preview_generator(Ref(memnew(EditorFontPreviewPlugin))); + resource_preview->add_preview_generator(Ref(memnew(EditorGradientPreviewPlugin))); { Ref spatial_mat_convert; diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 2855e70ef..0ee81ef3b 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -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 { diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 07171a860..1c16dcbda 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -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 EditorGradientPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { + Ref gradient = p_from; + if (gradient.is_valid()) { + Ref ptex = Ref(memnew(GradientTexture)); + ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE); + ptex->set_gradient(gradient); + + Ref image = Ref(memnew(ImageTexture)); + image->create_from_image(ptex->get_data()); + return image; + } + return Ref(); +} + +EditorGradientPreviewPlugin::EditorGradientPreviewPlugin() { +} diff --git a/editor/plugins/editor_preview_plugins.h b/editor/plugins/editor_preview_plugins.h index 46d475a61..df3137e48 100644 --- a/editor/plugins/editor_preview_plugins.h +++ b/editor/plugins/editor_preview_plugins.h @@ -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 generate(const Ref &p_from, const Size2 &p_size) const; + + EditorGradientPreviewPlugin(); +}; +