Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/animatable.scoop

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

1package scoop.surface
3import scoop.platform.time.MonotonicInstant
4import scoop.time.*
6public enum AnimationState {
7 Idle, Running, Paused, Finished, Cancelled, Closed
8}
10private enum AnimationPlayback {
11 Idle, Running(AnimationSegment), Paused(AnimationSegment, MonotonicInstant),
12 Finished(MotionRun), Cancelled, Closed
15/** Persistent value state. Active runs are owned and sampled by one timeline. */
16public 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 = 0L
23 public override val revision: Long get() {
24 timeline.checkThread()
25 return serial
26 }
27 private var frameEnabled = true
28 public var isFrameEnabled: Boolean
29 get() {
30 timeline.checkThread()
31 return frameEnabled
32 }
33 set(value) {
34 checkOpen()
35 if (frameEnabled == value)
36 return
37 frameEnabled = value
38 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.deadline
46 else -> None
47 }
48 private var current: T = initial
49 private var goal: T = initial
50 private var vector: AnimationVector = converter.toVector(initial)
51 private var speed = AnimationVector()
52 private var playback = AnimationPlayback.Idle
53 private var callback: ((T) -> Unit)? = None
54 init {
55 vector.check()
56 }
57 public val value: T get() {
58 timeline.checkThread();
59 observeAnimation(this)
60 return current
61 }
62 public val target: T get() {
63 timeline.checkThread();
64 observeAnimation(this)
65 return goal
66 }
67 public val velocity: AnimationVector get() {
68 timeline.checkThread();
69 observeAnimation(this)
70 return speed
71 }
72 public val state: AnimationState get() {
73 timeline.checkThread()
74 observeAnimation(this)
75 return when (playback) {
76 case Idle -> AnimationState.Idle
77 case Running(_) -> AnimationState.Running
78 case Paused(_, _) -> AnimationState.Paused
79 case Finished(_) -> AnimationState.Finished
80 case Cancelled -> AnimationState.Cancelled
81 case Closed -> AnimationState.Closed
82 }
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)? = None
91 ) {
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 = target
112 serial += 1L
113 callback = onFinished
114 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.Idle
123 callback = None
124 current = value
125 goal = value
126 vector = next
127 speed = AnimationVector()
128 serial += 1L
129 timeline.unschedule(this)
130 timeline.changed(requestsFrames)
131 }
132 public fun cancel() {
133 timeline.checkThread()
134 if (state == AnimationState.Closed)
135 return
136 sample(timeline.now())
137 playback = AnimationPlayback.Cancelled
138 serial += 1L
139 callback = None
140 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 += 1L
152 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 += 1L
169 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.run
180 case Paused(val segment, _) -> segment.run
181 case Finished(val value) -> value
182 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.run
190 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.finished
203 )
204 serial += 1L
205 current = next
206 vector = nextVector
207 speed = sampled.velocity
208 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 return
238 val repaint = requestsFrames
239 frameEnabled = false
240 playback = AnimationPlayback.Closed
241 serial += 1L
242 callback = None
243 speed = AnimationVector()
244 timeline.detached(this)
245 group?.remove(this)
246 timeline.changed(repaint)
247 }