scoop:scoop.surface:0.1.0
src/shared/animation/animatable.scoop
Selected production source · src/shared/animation/animatable.scoop
1
package scoop.surface3
import scoop.platform.time.MonotonicInstant4
import scoop.time.*6
public enum AnimationState {7
Idle, Running, Paused, Finished, Cancelled, Closed8
}10
private enum AnimationPlayback {11
Idle, Running(AnimationSegment), Paused(AnimationSegment, MonotonicInstant),12
Finished(MotionRun), Cancelled, Closed13
}15
/** Persistent value state. Active runs are owned and sampled by one timeline. */16
public class Animatable<T> internal constructor(17
private val timeline: AnimationTimeline,18
initial: T,19
private val converter: AnimationConverter<T>,20
private val group: AnimationGroup?21
): Closeable, AnimationSource {22
private var serial = 0L23
public override val revision: Long get() {24
timeline.checkThread()25
return serial26
}27
private var frameEnabled = true28
public var isFrameEnabled: Boolean29
get() {30
timeline.checkThread()31
return frameEnabled32
}33
set(value) {34
checkOpen()35
if (frameEnabled == value)36
return37
frameEnabled = value38
if (value)39
sample(timeline.now())40
timeline.changed(requestsFrames)41
}42
internal val requestsFrames: Boolean get() = frameEnabled && (group?.isFrameEnabled ?: true)43
internal val deadline: MonotonicInstant? get() =44
when (playback) {45
case Running(val segment) -> segment.deadline46
else -> None47
}48
private var current: T = initial49
private var goal: T = initial50
private var vector: AnimationVector = converter.toVector(initial)51
private var speed = AnimationVector()52
private var playback = AnimationPlayback.Idle53
private var callback: ((T) -> Unit)? = None54
init {55
vector.check()56
}57
public val value: T get() {58
timeline.checkThread();59
observeAnimation(this)60
return current61
}62
public val target: T get() {63
timeline.checkThread();64
observeAnimation(this)65
return goal66
}67
public val velocity: AnimationVector get() {68
timeline.checkThread();69
observeAnimation(this)70
return speed71
}72
public val state: AnimationState get() {73
timeline.checkThread()74
observeAnimation(this)75
return when (playback) {76
case Idle -> AnimationState.Idle77
case Running(_) -> AnimationState.Running78
case Paused(_, _) -> AnimationState.Paused79
case Finished(_) -> AnimationState.Finished80
case Cancelled -> AnimationState.Cancelled81
case Closed -> AnimationState.Closed82
}83
}84
/** Retargets from the current time and retains the current vector velocity for a spring. */85
public fun animateTo(86
target: T,87
spec: AnimationSpec = AnimationSpec.Tween(Duration.milliseconds(300L)),88
repeat: Repeat? = None,89
initialVelocity: AnimationVector? = None,90
onFinished: ((T) -> Unit)? = None91
) {92
checkOpen()93
val destination = converter.toVector(target)94
destination.check()95
when (initialVelocity) {96
case Some(val value) -> {97
value.check()98
when (spec) {99
case Spring(_, _, _, _) -> {}100
else -> throw IllegalArgumentException(101
Some("an explicit initial velocity requires a spring")102
)103
}104
}105
case None -> {}106
}107
val now = timeline.now()108
sample(now)109
val run = MotionRun(vector, destination, initialVelocity ?: speed, spec, repeat)110
playback = AnimationPlayback.Running(AnimationSegment(run, now))111
goal = target112
serial += 1L113
callback = onFinished114
timeline.schedule(this)115
sample(now)116
timeline.changed(requestsFrames)117
}118
public fun snapTo(value: T) {119
checkOpen()120
val next = converter.toVector(value)121
next.check()122
playback = AnimationPlayback.Idle123
callback = None124
current = value125
goal = value126
vector = next127
speed = AnimationVector()128
serial += 1L129
timeline.unschedule(this)130
timeline.changed(requestsFrames)131
}132
public fun cancel() {133
timeline.checkThread()134
if (state == AnimationState.Closed)135
return136
sample(timeline.now())137
playback = AnimationPlayback.Cancelled138
serial += 1L139
callback = None140
speed = AnimationVector()141
timeline.unschedule(this)142
timeline.changed(requestsFrames)143
}144
public fun pause() {145
checkOpen()146
val now = timeline.now()147
sample(now)148
when (playback) {149
case Running(val segment) -> {150
playback = AnimationPlayback.Paused(segment, now)151
serial += 1L152
timeline.unschedule(this)153
timeline.changed(requestsFrames)154
}155
else -> {}156
}157
}158
public fun resume() {159
checkOpen()160
when (playback) {161
case Paused(val segment, val paused) -> {162
val now = timeline.now()163
playback =164
AnimationPlayback.Running(165
AnimationSegment(segment.run, segment.start + (now - paused))166
)167
timeline.schedule(this)168
serial += 1L169
timeline.changed(requestsFrames)170
}171
else -> {}172
}173
}174
/** Starts a new full segment toward the previous segment's origin. */175
public fun reverse() {176
checkOpen()177
val run =178
when (playback) {179
case Running(val segment) -> segment.run180
case Paused(val segment, _) -> segment.run181
case Finished(val value) -> value182
else -> throw IllegalStateException(Some("no animation segment to reverse"))183
}184
animateTo(converter.fromVector(run.origin), run.spec, run.repeat, onFinished = callback)185
}186
internal fun sample(time: MonotonicInstant) {187
when (playback) {188
case Running(val segment) -> {189
val run = segment.run190
val sampled = run.sample(durationSeconds(time - segment.start))191
sampled.value.check()192
sampled.velocity.check()193
val next = converter.fromVector(sampled.value)194
val nextVector = converter.toVector(next)195
if (196
nextVector !=197
vector ||198
sampled.velocity !=199
speed ||200
sampled.target !=201
converter.toVector(goal) ||202
sampled.finished203
)204
serial += 1L205
current = next206
vector = nextVector207
speed = sampled.velocity208
goal = converter.fromVector(sampled.target)209
if (sampled.finished) {210
playback = AnimationPlayback.Finished(run)211
timeline.unschedule(this)212
when (callback) {213
case Some(val handler) -> timeline.finished(time, { requestsFrames }) {214
when (playback) {215
case Finished(val currentRun) -> {216
if (currentRun === run)217
handler(current)218
}219
else -> {}220
}221
}222
case None -> {}223
}224
}225
}226
else -> {}227
}228
}229
private fun checkOpen() {230
timeline.checkOpen()231
if (state == AnimationState.Closed)232
throw IllegalStateException(Some("animation is closed"))233
}234
public override fun close() {235
timeline.checkThread()236
if (state == AnimationState.Closed)237
return238
val repaint = requestsFrames239
frameEnabled = false240
playback = AnimationPlayback.Closed241
serial += 1L242
callback = None243
speed = AnimationVector()244
timeline.detached(this)245
group?.remove(this)246
timeline.changed(repaint)247
}248
}