1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-15 13:51:40 +00:00

Add interpolation modes to Gradient

- Linear, Constant, and Cubic interpolation modes
- Partial backport of #53321
This commit is contained in:
FireForge
2022-05-12 12:51:56 -05:00
parent 288370609c
commit f17c5fa7bd
6 changed files with 110 additions and 41 deletions

View File

@@ -52,6 +52,11 @@ GradientEdit::GradientEdit() {
add_child(popup);
gradient_cache.instance();
preview_texture.instance();
preview_texture->set_width(1024);
checker = Ref<ImageTexture>(memnew(ImageTexture));
Ref<Image> img = memnew(Image(checker_bg_png));
checker->create_from_image(img, ImageTexture::FLAG_REPEAT);
@@ -315,46 +320,10 @@ void GradientEdit::_notification(int p_what) {
_draw_checker(0, 0, total_w, h);
//Draw color ramp
Gradient::Point prev;
prev.offset = 0;
if (points.size() == 0) {
prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points
} else {
prev.color = points[0].color; //Extend color of first point to the beginning.
}
for (int i = -1; i < points.size(); i++) {
Gradient::Point next;
//If there is no next point
if (i + 1 == points.size()) {
if (points.size() == 0) {
next.color = Color(0, 0, 0); //Draw black rectangle if we have no points
} else {
next.color = points[i].color; //Extend color of last point to the end.
}
next.offset = 1;
} else {
next = points[i + 1];
}
if (prev.offset == next.offset) {
prev = next;
continue;
}
Vector<Vector2> points;
Vector<Color> colors;
points.push_back(Vector2(prev.offset * total_w, h));
points.push_back(Vector2(prev.offset * total_w, 0));
points.push_back(Vector2(next.offset * total_w, 0));
points.push_back(Vector2(next.offset * total_w, h));
colors.push_back(prev.color);
colors.push_back(prev.color);
colors.push_back(next.color);
colors.push_back(next.color);
draw_primitive(points, colors, Vector<Point2>());
prev = next;
}
gradient_cache->set_points(points);
gradient_cache->set_interpolation_mode(interpolation_mode);
preview_texture->set_gradient(gradient_cache);
draw_texture_rect(preview_texture, Rect2(0, 0, total_w, h));
//Draw point markers
for (int i = 0; i < points.size(); i++) {
@@ -482,6 +451,14 @@ Vector<Gradient::Point> &GradientEdit::get_points() {
return points;
}
void GradientEdit::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
interpolation_mode = p_interp_mode;
}
Gradient::InterpolationMode GradientEdit::get_interpolation_mode() {
return interpolation_mode;
}
void GradientEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &GradientEdit::_gui_input);
ClassDB::bind_method(D_METHOD("_color_changed"), &GradientEdit::_color_changed);