Added Curve preview thumbnail
This commit is contained in:
parent
0ffb70f0bb
commit
c4564233a8
2 changed files with 80 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue