scoop:scoop.surface:0.1.0
src/shared/animation/plan.scoop
Selected production source · src/shared/animation/plan.scoop
1
package scoop.surface3
import scoop.math.max4
import scoop.math.min5
import scoop.time.Duration7
public 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.00114
)15
}17
public enum RepeatMode {18
Restart, Reverse19
}21
public class Repeat public constructor(22
public val iterations: Long? = None,23
public val mode: RepeatMode = RepeatMode.Restart24
)26
internal struct MotionSample(val value: AnimationVector, val velocity: AnimationVector)28
internal struct RunSample(29
val value: AnimationVector,30
val velocity: AnimationVector,31
val target: AnimationVector,32
val finished: Boolean33
)35
internal interface MotionPlan {36
val duration: Double37
fun sample(seconds: Double): MotionSample38
}40
private class TweenPlan(41
private val from: AnimationVector,42
private val to: AnimationVector,43
public override val duration: Double,44
private val easing: Easing45
): 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
}56
}58
internal fun durationSeconds(value: Duration): Double =59
value.floorSeconds.toDouble() + value.nanosecondsOfSecond.toDouble() / 1000000000.061
internal fun motionPlan(62
from: AnimationVector,63
to: AnimationVector,64
velocity: AnimationVector,65
spec: AnimationSpec66
): 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
speed84
)85
}86
}88
/** A whole repeated run is sampled directly; missed cycles never run callback loops. */89
internal 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 -> false108
}109
val duration: Double? get() =110
when (iterations) {111
case Some(val count) -> Some(first.duration + (count - 1L).toDouble() * forward.duration)112
case None -> None113
}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
origin135
else136
destination137
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.duration147
val position = elapsed % forward.duration148
val reversed = reverse && elapsed % (2.0 * forward.duration) < forward.duration149
val plan =150
if (reversed)151
backward152
else153
forward154
val value = plan.sample(position)155
return RunSample(156
value.value,157
value.velocity,158
if (reversed)159
origin160
else161
destination,162
false163
)164
}165
}