| •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.geom.AffineTransform; |
| 25 |
import java.util.Objects; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
|
| 29 |
import com.github.weisj.jsvg.animation.Track; |
| 30 |
import com.github.weisj.jsvg.attributes.transform.TransformPart; |
| 31 |
import com.github.weisj.jsvg.attributes.value.ConstantLengthTransform; |
| 32 |
import com.github.weisj.jsvg.attributes.value.TransformValue; |
| 33 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 34 |
|
| 35 |
public final class AnimatedTransform implements TransformValue { |
| 36 |
|
| 37 |
private final @NotNull Track track; |
| 38 |
private final @NotNull TransformValue initial; |
| 39 |
private final @NotNull TransformPart @NotNull [] values; |
| 40 |
|
| 41 |
private AffineTransform current; |
| 42 |
private long currentTimestamp = -1; |
| 43 |
|
| 44 |
public AnimatedTransform(@NotNull Track track, @NotNull TransformValue initial, |
| 45 |
@NotNull TransformPart @NotNull [] values) { |
| 46 |
this.track = track; |
| 47 |
this.initial = initial; |
| 48 |
this.values = values; |
| 49 |
} |
| 50 |
|
| 51 |
public @NotNull TransformValue initial() { |
| 52 |
return initial; |
| 53 |
} |
| 54 |
|
| 55 |
public @NotNull AnimatedTransform derive(@NotNull TransformValue initialValue) { |
| 56 |
if (this.initial != ConstantLengthTransform.INHERITED) return this; |
| 57 |
return new AnimatedTransform(track, initialValue, values); |
| 58 |
} |
| 59 |
|
| 60 |
private @NotNull AffineTransform current(@NotNull MeasureContext context) { |
| 61 |
long timestamp = context.timestamp(); |
| 62 |
if (currentTimestamp == timestamp) return current; |
| 63 |
currentTimestamp = timestamp; |
| 64 |
current = computeCurrent(context, timestamp); |
| 65 |
return current; |
| 66 |
} |
| 67 |
|
| 68 |
private @NotNull AffineTransform computeCurrent(@NotNull MeasureContext context, long timestamp) { |
| 69 |
Track.InterpolationProgress progress = track.interpolationProgress(timestamp, values.length); |
| 70 |
if (progress.isInitial()) return Objects.requireNonNull(initial).get(context); |
| 71 |
int i = progress.iterationIndex(); |
| 72 |
|
| 73 |
assert i >= 0; |
| 74 |
assert values.length > 0; |
| 75 |
if (i >= values.length - 1) { |
| 76 |
AffineTransform transform = new AffineTransform(); |
| 77 |
values[values.length - 1].applyToTransform(transform, context); |
| 78 |
return transform; |
| 79 |
} |
| 80 |
|
| 81 |
TransformPart start = values[i]; |
| 82 |
TransformPart end = values[i + 1]; |
| 83 |
float t = progress.indexProgress(); |
| 84 |
return track.transformInterpolator().interpolate(context, initial, start, end, t); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public @NotNull AffineTransform get(@NotNull MeasureContext context) { |
| 89 |
return current(context); |
| 90 |
} |
| 91 |
} |