Browse Source

Preview the color animation in the animation editor

(cherry picked from commit 735c8396b0cba475b4c74c5e1e42a74ddc89e324)
trollodel 4 years ago
parent
commit
802ba1d07a
1 changed files with 45 additions and 25 deletions
  1. 45 25
      editor/animation_track_editor_plugins.cpp

+ 45 - 25
editor/animation_track_editor_plugins.cpp

@@ -94,47 +94,67 @@ void AnimationTrackEditColor::draw_key_link(int p_index, float p_pixels_sec, int
 	Ref<Font> font = get_font("font", "Label");
 	int fh = (font->get_height() * 0.8);
 
+	fh /= 3;
+
 	int x_from = p_x + fh / 2 - 1;
 	int x_to = p_next_x - fh / 2 + 1;
-	fh /= 3;
+	x_from = MAX(x_from, p_clip_left);
+	x_to = MIN(x_to, p_clip_right);
+
+	int y_from = (get_size().height - fh) / 2;
 
 	if (x_from > p_clip_right || x_to < p_clip_left) {
 		return;
 	}
 
-	Color color = get_animation()->track_get_key_value(get_track(), p_index);
-	Color color_next = get_animation()->track_get_key_value(get_track(), p_index + 1);
+	Vector<Color> color_samples;
+	color_samples.push_back(get_animation()->track_get_key_value(get_track(), p_index));
 
-	if (x_from < p_clip_left) {
-		float c = float(p_clip_left - x_from) / (x_to - x_from);
-		color = color.linear_interpolate(color_next, c);
-		x_from = p_clip_left;
-	}
+	if (get_animation()->track_get_type(get_track()) == Animation::TYPE_VALUE) {
+		if (get_animation()->track_get_interpolation_type(get_track()) != Animation::INTERPOLATION_NEAREST &&
+				(get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CONTINUOUS ||
+						get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CAPTURE) &&
+				!Math::is_zero_approx(get_animation()->track_get_key_transition(get_track(), p_index))) {
+			float start_time = get_animation()->track_get_key_time(get_track(), p_index);
+			float end_time = get_animation()->track_get_key_time(get_track(), p_index + 1);
 
-	if (x_to > p_clip_right) {
-		float c = float(p_clip_right - x_from) / (x_to - x_from);
-		color_next = color.linear_interpolate(color_next, c);
-		x_to = p_clip_right;
-	}
+			Color color_next = get_animation()->value_track_interpolate(get_track(), end_time);
 
-	int y_from = (get_size().height - fh) / 2;
+			if (!color_samples[0].is_equal_approx(color_next)) {
+				color_samples.resize(1 + (x_to - x_from) / 64); // Make a color sample every 64 px.
+				for (int i = 1; i < color_samples.size(); i++) {
+					float j = i;
+					color_samples.write[i] = get_animation()->value_track_interpolate(
+							get_track(),
+							Math::lerp(start_time, end_time, j / color_samples.size()));
+				}
+			}
+			color_samples.push_back(color_next);
+		} else {
+			color_samples.push_back(color_samples[0]);
+		}
+	} else {
+		color_samples.push_back(get_animation()->track_get_key_value(get_track(), p_index + 1));
+	}
 
-	Vector<Vector2> points;
-	Vector<Color> colors;
+	for (int i = 0; i < color_samples.size() - 1; i++) {
+		Vector<Vector2> points;
+		Vector<Color> colors;
 
-	points.push_back(Vector2(x_from, y_from));
-	colors.push_back(color);
+		points.push_back(Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from));
+		colors.push_back(color_samples[i]);
 
-	points.push_back(Vector2(x_to, y_from));
-	colors.push_back(color_next);
+		points.push_back(Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from));
+		colors.push_back(color_samples[i + 1]);
 
-	points.push_back(Vector2(x_to, y_from + fh));
-	colors.push_back(color_next);
+		points.push_back(Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from + fh));
+		colors.push_back(color_samples[i + 1]);
 
-	points.push_back(Vector2(x_from, y_from + fh));
-	colors.push_back(color);
+		points.push_back(Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from + fh));
+		colors.push_back(color_samples[i]);
 
-	draw_primitive(points, colors, Vector<Vector2>());
+		draw_primitive(points, colors, Vector<Vector2>());
+	}
 }
 
 void AnimationTrackEditColor::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {