Răsfoiți Sursa

Merge pull request #9465 from Zylann/curve_preview

Added Curve preview thumbnail
Rémi Verschelde 8 ani în urmă
părinte
comite
1a2ea9ba1b

+ 73 - 0
editor/plugins/curve_editor_plugin.cpp

@@ -774,6 +774,8 @@ CurveEditorPlugin::CurveEditorPlugin(EditorNode *p_node) {
 
 	_toggle_button = _editor_node->add_bottom_panel_item(get_name(), _view);
 	_toggle_button->hide();
+
+	get_resource_previewer()->add_preview_generator(memnew(CurvePreviewGenerator));
 }
 
 CurveEditorPlugin::~CurveEditorPlugin() {
@@ -845,3 +847,74 @@ void CurveEditorPlugin::_bind_methods() {
 
 	ClassDB::bind_method(D_METHOD("_curve_texture_changed"), &CurveEditorPlugin::_curve_texture_changed);
 }
+
+//-----------------------------------
+// Preview generator
+
+bool CurvePreviewGenerator::handles(const String &p_type) const {
+	return p_type == "Curve";
+}
+
+Ref<Texture> CurvePreviewGenerator::generate(const Ref<Resource> &p_from) {
+
+	Ref<Curve> curve_ref = p_from;
+	ERR_FAIL_COND_V(curve_ref.is_null(), Ref<Texture>());
+	Curve &curve = **curve_ref;
+
+	int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
+	thumbnail_size *= EDSCALE;
+	Ref<Image> img_ref;
+	img_ref.instance();
+	Image &im = **img_ref;
+
+	im.create(thumbnail_size, thumbnail_size, 0, Image::FORMAT_RGBA8);
+
+	im.lock();
+
+	Color bg_color(0.1, 0.1, 0.1, 1.0);
+	for (int i = 0; i < thumbnail_size; i++) {
+		for (int j = 0; j < thumbnail_size; j++) {
+			im.put_pixel(i, j, bg_color);
+		}
+	}
+
+	Color line_color(0.8, 0.8, 0.8, 1.0);
+	float range_y = curve.get_max_value() - curve.get_min_value();
+
+	int prev_y = 0;
+	for (int x = 0; x < im.get_width(); ++x) {
+
+		float t = static_cast<float>(x) / im.get_width();
+		float v = (curve.interpolate_baked(t) - curve.get_min_value()) / range_y;
+		int y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height());
+
+		// Plot point
+		if (y >= 0 && y < im.get_height()) {
+			im.put_pixel(x, y, line_color);
+		}
+
+		// Plot vertical line to fix discontinuity (not 100% correct but enough for a preview)
+		if (x != 0 && Math::abs(y - prev_y) > 1) {
+			int y0, y1;
+			if (y < prev_y) {
+				y0 = y;
+				y1 = prev_y;
+			} else {
+				y0 = prev_y;
+				y1 = y;
+			}
+			for (int ly = y0; ly < y1; ++ly) {
+				im.put_pixel(x, ly, line_color);
+			}
+		}
+
+		prev_y = y;
+	}
+
+	im.unlock();
+
+	Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
+
+	ptex->create_from_image(img_ref, 0);
+	return ptex;
+}

+ 7 - 0
editor/plugins/curve_editor_plugin.h

@@ -143,4 +143,11 @@ private:
 	ToolButton *_toggle_button;
 };
 
+class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
+	GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator)
+public:
+	bool handles(const String &p_type) const;
+	Ref<Texture> generate(const Ref<Resource> &p_from);
+};
+
 #endif // CURVE_EDITOR_PLUGIN_H