| •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 |
|
| 25 |
import org.jetbrains.annotations.NotNull; |
| 26 |
|
| 27 |
import com.github.weisj.jsvg.animation.Track; |
| 28 |
import com.github.weisj.jsvg.attributes.value.FloatListValue; |
| 29 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 30 |
|
| 31 |
public final class AnimatedFloatList implements FloatListValue { |
| 32 |
|
| 33 |
private final @NotNull Track track; |
| 34 |
|
| 35 |
private final @NotNull FloatListValue initial; |
| 36 |
private final float @NotNull [] @NotNull [] values; |
| 37 |
private float[] cache; |
| 38 |
|
| 39 |
private long timestamp = 0; |
| 40 |
private Track.InterpolationProgress progressCacheKey = null; |
| 41 |
|
| 42 |
public AnimatedFloatList(@NotNull Track track, @NotNull FloatListValue initial, |
| 43 |
float @NotNull [] @NotNull [] values) { |
| 44 |
this.track = track; |
| 45 |
this.initial = initial; |
| 46 |
this.values = values; |
| 47 |
} |
| 48 |
|
| 49 |
public @NotNull FloatListValue initial() { |
| 50 |
return initial; |
| 51 |
} |
| 52 |
|
| 53 |
public boolean isDirty(long timestamp) { |
| 54 |
return this.timestamp != timestamp; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public float @NotNull [] get(@NotNull MeasureContext context) { |
| 59 |
long ts = context.timestamp(); |
| 60 |
if (ts != this.timestamp) { |
| 61 |
this.timestamp = ts; |
| 62 |
Track.InterpolationProgress progress = track.interpolationProgress(ts, values.length); |
| 63 |
|
| 64 |
if (progress.equals(progressCacheKey)) { |
| 65 |
return cache; |
| 66 |
} |
| 67 |
progressCacheKey = progress; |
| 68 |
|
| 69 |
if (progress.isInitial()) { |
| 70 |
cache = initial.get(context); |
| 71 |
return cache; |
| 72 |
} |
| 73 |
|
| 74 |
int index = progress.iterationIndex(); |
| 75 |
float[] start = values[index]; |
| 76 |
float[] end = index == values.length - 1 ? null : values[index + 1]; |
| 77 |
|
| 78 |
float fraction = progress.indexProgress(); |
| 79 |
cache = track.floatListInterpolator().interpolate(initial.get(context), start, end, fraction, cache); |
| 80 |
} |
| 81 |
return cache; |
| 82 |
} |
| 83 |
} |