diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-07-03 23:28:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-03 23:28:23 +0200 |
commit | 1a2ea9ba1b82689eb55aafd53cd22c44591ed1cd (patch) | |
tree | 4676239afcab2e8faef9ffa544d6b93e00e41dcf /editor/plugins | |
parent | 5c1995ef495d662f7197ab45d01bf0e816e4cf12 (diff) | |
parent | c4564233a8080a8213d86202fd655db2c9e9f40f (diff) |
Merge pull request #9465 from Zylann/curve_preview
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 |