Skip to content

Commit

Permalink
Merge pull request #68990 from timothyqiu/editor-gradient-preview-plugin
Browse files Browse the repository at this point in the history
[3.x] Add Gradient resource preview generator
  • Loading branch information
akien-mga committed Nov 22, 2022
2 parents f3ed588 + ea46617 commit cb0b20b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_resource_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions editor/plugins/editor_preview_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
11 changes: 11 additions & 0 deletions editor/plugins/editor_preview_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ class EditorFontPreviewPlugin : public EditorResourcePreviewGenerator {
~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

0 comments on commit cb0b20b

Please sign in to comment.