| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.animation.value; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.Rectangle2D; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.animation.Track; |
| 31 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 32 |
import com.github.weisj.jsvg.paint.impl.PredefinedPaints; |
| 33 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 34 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 35 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 36 |
|
| 37 |
public class AnimatedPaint implements SVGPaint { |
| 38 |
|
| 39 |
private final @NotNull Track track; |
| 40 |
private final @NotNull SVGPaint initial; |
| 41 |
private final @NotNull SVGPaint @NotNull [] values; |
| 42 |
|
| 43 |
private SVGPaint current; |
| 44 |
private long currentTimestamp = -1; |
| 45 |
|
| 46 |
public AnimatedPaint(@NotNull Track track, @NotNull SVGPaint initial, @NotNull SVGPaint @NotNull [] values) { |
| 47 |
this.track = track; |
| 48 |
this.initial = initial; |
| 49 |
this.values = values; |
| 50 |
} |
| 51 |
|
| 52 |
public @NotNull AnimatedPaint derive(@NotNull SVGPaint initialValue) { |
| 53 |
if (this.initial != PredefinedPaints.INHERITED) return this; |
| 54 |
return new AnimatedPaint(track, initialValue, values); |
| 55 |
} |
| 56 |
|
| 57 |
private @NotNull SVGPaint current(@NotNull MeasureContext context) { |
| 58 |
long timestamp = context.timestamp(); |
| 59 |
if (currentTimestamp == timestamp) return current; |
| 60 |
currentTimestamp = timestamp; |
| 61 |
current = computeCurrent(timestamp); |
| 62 |
return current; |
| 63 |
} |
| 64 |
|
| 65 |
private @NotNull SVGPaint computeCurrent(long timestamp) { |
| 66 |
Track.InterpolationProgress progress = track.interpolationProgress(timestamp, values.length); |
| 67 |
if (progress.isInitial()) return initial; |
| 68 |
int i = progress.iterationIndex(); |
| 69 |
|
| 70 |
assert i >= 0; |
| 71 |
assert values.length > 0; |
| 72 |
if (i >= values.length - 1) { |
| 73 |
return values[values.length - 1]; |
| 74 |
} |
| 75 |
|
| 76 |
SVGPaint start = values[i]; |
| 77 |
SVGPaint end = values[i + 1]; |
| 78 |
|
| 79 |
return track.paintInterpolator().interpolate(initial, start, end, progress.indexProgress()); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void fillShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape, |
| 84 |
@Nullable Rectangle2D bounds) { |
| 85 |
current(context.measureContext()).fillShape(output, context, shape, bounds); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void drawShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape, |
| 90 |
@Nullable Rectangle2D bounds) { |
| 91 |
current(context.measureContext()).drawShape(output, context, shape, bounds); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean isVisible(@NotNull RenderContext context) { |
| 96 |
return current(context.measureContext()).isVisible(context); |
| 97 |
} |
| 98 |
} |