Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/plan.scoop

Selected production source · src/shared/animation/plan.scoop

1package scoop.surface
3import scoop.math.max
4import scoop.math.min
5import scoop.time.Duration
7public enum AnimationSpec {
8 Tween(val duration: Duration, val easing: Easing = Easing.Linear),
9 Spring(
10 val stiffness: Double = 150.0,
11 val dampingRatio: Double = 0.8,
12 val displacementThreshold: Double = 0.001,
13 val velocityThreshold: Double = 0.001
14 )
17public enum RepeatMode {
18 Restart, Reverse
21public class Repeat public constructor(
22 public val iterations: Long? = None,
23 public val mode: RepeatMode = RepeatMode.Restart
26internal struct MotionSample(val value: AnimationVector, val velocity: AnimationVector)
28internal struct RunSample(
29 val value: AnimationVector,
30 val velocity: AnimationVector,
31 val target: AnimationVector,
32 val finished: Boolean
35internal interface MotionPlan {
36 val duration: Double
37 fun sample(seconds: Double): MotionSample
40private class TweenPlan(
41 private val from: AnimationVector,
42 private val to: AnimationVector,
43 public override val duration: Double,
44 private val easing: Easing
45): MotionPlan {
46 public override fun sample(seconds: Double): MotionSample {
47 if (seconds >= duration)
48 return MotionSample(to, AnimationVector())
49 val fraction = min(1.0, max(0.0, seconds / duration))
50 val distance = to.subtract(from)
51 return MotionSample(
52 from.add(distance.scale(easing.transform(fraction))),
53 distance.scale(easing.slope(fraction) / duration)
54 )
55 }
58internal fun durationSeconds(value: Duration): Double =
59 value.floorSeconds.toDouble() + value.nanosecondsOfSecond.toDouble() / 1000000000.0
61internal fun motionPlan(
62 from: AnimationVector,
63 to: AnimationVector,
64 velocity: AnimationVector,
65 spec: AnimationSpec
66): MotionPlan {
67 from.check();
68 to.check();
69 velocity.check();
70 to.subtract(from).check()
71 return when (spec) {
72 case Tween(val duration, val easing) -> {
73 requireAnimation(duration >= Duration.ZERO, "negative animation duration")
74 TweenPlan(from, to, durationSeconds(duration), easing)
75 }
76 case Spring(val stiffness, val damping, val displacement, val speed) -> springPlan(
77 from,
78 to,
79 velocity,
80 stiffness,
81 damping,
82 displacement,
83 speed
84 )
85 }
88/** A whole repeated run is sampled directly; missed cycles never run callback loops. */
89internal class MotionRun(
90 val origin: AnimationVector,
91 val destination: AnimationVector,
92 velocity: AnimationVector,
93 val spec: AnimationSpec,
94 val repeat: Repeat?
95) {
96 private val first = motionPlan(origin, destination, velocity, spec)
97 private val forward = motionPlan(origin, destination, AnimationVector(), spec)
98 private val backward = motionPlan(destination, origin, AnimationVector(), spec)
99 private val iterations: Long? =
100 when (repeat) {
101 case Some(val value) -> value.iterations;
102 case None -> Some(1L)
103 }
104 private val reverse =
105 when (repeat) {
106 case Some(val value) -> value.mode == RepeatMode.Reverse;
107 case None -> false
108 }
109 val duration: Double? get() =
110 when (iterations) {
111 case Some(val count) -> Some(first.duration + (count - 1L).toDouble() * forward.duration)
112 case None -> None
113 }
114 init {
115 when (iterations) {
116 case Some(val value) -> requireAnimation(value > 0L, "repeat count must be positive");
117 case None -> {}
118 }
119 when (repeat) {
120 case Some(_) -> requireAnimation(
121 first.duration > 0.0 && forward.duration > 0.0,
122 "zero duration cannot repeat"
123 );
124 case None -> {}
125 }
126 }
127 fun sample(seconds: Double): RunSample {
128 when (iterations) {
129 case Some(val count) -> {
130 val end = duration!!
131 if (seconds >= end) {
132 val finalValue =
133 if (reverse && count % 2L == 0L)
134 origin
135 else
136 destination
137 return RunSample(finalValue, AnimationVector(), finalValue, true)
138 }
139 }
140 case None -> {}
141 }
142 if (seconds < first.duration) {
143 val value = first.sample(seconds)
144 return RunSample(value.value, value.velocity, destination, false)
145 }
146 val elapsed = seconds - first.duration
147 val position = elapsed % forward.duration
148 val reversed = reverse && elapsed % (2.0 * forward.duration) < forward.duration
149 val plan =
150 if (reversed)
151 backward
152 else
153 forward
154 val value = plan.sample(position)
155 return RunSample(
156 value.value,
157 value.velocity,
158 if (reversed)
159 origin
160 else
161 destination,
162 false
163 )
164 }