diff options
author | Marc Gilleron <marc.gilleron@gmail.com> | 2017-07-02 01:46:44 +0200 |
---|---|---|
committer | Marc Gilleron <marc.gilleron@gmail.com> | 2017-07-02 01:52:47 +0200 |
commit | c4564233a8080a8213d86202fd655db2c9e9f40f (patch) | |
tree | 75c84f0fdc59cfc82e2004547f552b11fa8ea4ab /editor/plugins | |
parent | 0ffb70f0bbfa14d9201b4349ac23911450b9d6b2 (diff) |
Added Curve preview thumbnail
Diffstat (limited to 'editor/plugins')
-rw-r--r-- | editor/plugins/curve_editor_plugin.cpp | 73 | ||||
-rw-r--r-- | editor/plugins/curve_editor_plugin.h | 7 |
2 files changed, 80 insertions, 0 deletions
diff --git a/editor/plugins/curve_editor_plugin.cpp b/editor/plugins/curve_editor_plugin.cpp index d729bb7b76..c74eaf21a1 100644 --- a/editor/plugins/curve_editor_plugin.cpp +++ b/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; +} diff --git a/editor/plugins/curve_editor_plugin.h b/editor/plugins/curve_editor_plugin.h index a4952dfb28..040c298a92 100644 --- a/editor/plugins/curve_editor_plugin.h +++ b/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 |