Debian Codegraph @

src/com/github/weisj/jsvg/animation/value/AnimatedTransform.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }