Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/timeline.scoop

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

1package scoop.surface
3import scoop.platform.time.MonotonicInstant
4import scoop.graphics.Color
5import scoop.graphics.Point
6import scoop.graphics.Size
7import scoop.platform.concurrent.currentThreadId
8import scoop.platform.time.MonotonicTimeSource
9import scoop.time.*
11internal class AnimationEntry(
12 val owner: Closeable,
13 val sample: (MonotonicInstant) -> Unit,
14 val frameEnabled: () -> Boolean,
15 val deadline: () -> MonotonicInstant?
18private class AnimationCompletion(
19 val time: MonotonicInstant,
20 val frameEnabled: () -> Boolean,
21 val callback: () -> Unit
24/** Shared owner-thread animation clock, driven by Surface frames or explicit offline sampling. */
25public class AnimationTimeline public constructor(
26 private val invalidate: () -> Unit = {}
27): Closeable {
28 private val thread = currentThreadId()
29 private var closed = false
30 private var frame: MonotonicInstant? = None
31 private var previous: MonotonicInstant? = None
32 private val owned = ArrayList<AnimationEntry>()
33 private val active = ArrayList<AnimationEntry>()
34 private val groups = ArrayList<AnimationGroup>()
35 private val completions = ArrayList<AnimationCompletion>()
36 internal var scheduleChanged: () -> Unit = {}
37 public val isClosed: Boolean get() = closed
38 public val hasWork: Boolean get() {
39 checkThread();
40 return active.size > 0L || completions.size > 0L
41 }
42 public val hasFrameWork: Boolean get() {
43 checkThread()
44 for (entry in active)
45 if (entry.frameEnabled())
46 return true
47 for (entry in completions)
48 if (entry.frameEnabled())
49 return true
50 return false
51 }
53 public fun value(initial: Double): Animatable<Double> =
54 value(initial, AnimationConverters.DoubleValue)
55 public fun value(initial: Point): Animatable<Point> =
56 value(initial, AnimationConverters.PointValue)
57 public fun value(initial: Size): Animatable<Size> =
58 value(initial, AnimationConverters.SizeValue)
59 public fun value(initial: Color): Animatable<Color> =
60 value(initial, AnimationConverters.ColorValue)
61 public fun <T> value(initial: T, converter: AnimationConverter<T>): Animatable<T> {
62 return createValue(initial, converter, None)
63 }
64 public fun group(frameEnabled: Boolean = true): AnimationGroup {
65 checkOpen()
66 val result = AnimationGroup(this, frameEnabled)
67 groups.add(result)
68 return result
69 }
70 internal fun <T> createValue(
71 initial: T,
72 converter: AnimationConverter<T>,
73 group: AnimationGroup?
74 ): Animatable<T> {
75 checkOpen()
76 val result = Animatable(this, initial, converter, group)
77 val entry =
78 AnimationEntry(
79 result,
80 { time -> result.sample(time) },
81 { result.requestsFrames },
82 { result.deadline }
83 )
84 owned.add(entry)
85 group?.add(entry)
86 return result
87 }
88 /** Samples all values, delivers completions, then runs the block at the same frame time. */
89 public fun advanceTo(time: MonotonicInstant, block: () -> Unit = {}) {
90 val repaint = hasFrameWork
91 withFrame(time, block)
92 if (!closed && repaint)
93 invalidate()
94 }
95 internal fun <T> withFrame(time: MonotonicInstant, block: () -> T): T {
96 checkOpen()
97 if (frame != None)
98 throw IllegalStateException(Some("animation sampling is not reentrant"))
99 val instant = checkTime(time)
100 frame = Some(instant)
101 try {
102 for (animation in active.toArray()) {
103 if (closed)
104 break
105 animation.sample(instant)
106 }
107 val callbacks = completions.toArray()
108 completions.clear()
109 for (completion in callbacks) {
110 if (closed)
111 break
112 completion.callback()
113 }
114 return block()
115 } finally {
116 frame = None
117 if (!closed)
118 scheduleChanged()
119 }
120 }
121 internal fun nextDeadline(includeFrameWork: Boolean): MonotonicInstant? {
122 checkThread()
123 var next: MonotonicInstant? = None
124 for (completion in completions)
125 if (next == None || completion.time < next!!)
126 next = Some(completion.time)
127 for (entry in active) {
128 if (includeFrameWork || !entry.frameEnabled()) {
129 when (entry.deadline()) {
130 case Some(val time) -> if (next == None || time < next!!)
131 next = Some(time)
132 case None -> {}
133 }
134 }
135 }
136 return next
137 }
138 internal fun now(): MonotonicInstant {
139 checkOpen()
140 when (frame) {
141 case Some(val value) -> return value;
142 case None -> {}
143 }
144 return checkTime(MonotonicTimeSource.now())
145 }
146 private fun checkTime(time: MonotonicInstant): MonotonicInstant {
147 val instant = time.normalized()
148 when (previous) {
149 case Some(val value) -> requireAnimation(
150 instant >= value,
151 "animation time moved backwards"
152 );
153 case None -> {}
154 }
155 previous = Some(instant)
156 return instant
157 }
158 internal fun changed(frameEnabled: Boolean) {
159 if (!closed) {
160 if (frameEnabled)
161 invalidate()
162 scheduleChanged()
163 }
164 }
165 internal fun schedule(animation: Closeable) {
166 checkOpen()
167 for (existing in active)
168 if (existing.owner === animation)
169 return
170 for (entry in owned) {
171 if (entry.owner === animation) {
172 active.add(entry)
173 return
174 }
175 }
176 throw IllegalStateException(Some("animation is not owned by this timeline"))
177 }
178 internal fun unschedule(animation: Closeable) {
179 removeAnimationEntry(active, animation)
180 }
181 internal fun detached(animation: Closeable) {
182 unschedule(animation);
183 removeAnimationEntry(owned, animation)
184 }
185 internal fun detachedGroup(group: AnimationGroup) {
186 for (index in 0L ..< groups.size) {
187 if (groups[index] === group) {
188 groups.removeAt(index)
189 return
190 }
191 }
192 }
193 internal fun finished(
194 time: MonotonicInstant,
195 frameEnabled: () -> Boolean,
196 callback: () -> Unit
197 ) {
198 if (!closed)
199 completions.add(AnimationCompletion(time, frameEnabled, callback))
200 }
201 internal fun checkThread() {
202 if (thread != currentThreadId())
203 throw IllegalStateException(Some("animation requires its owner thread"))
204 }
205 internal fun checkOpen() {
206 checkThread()
207 if (closed)
208 throw IllegalStateException(Some("animation timeline is closed"))
209 }
210 public override fun close() {
211 checkThread()
212 if (closed)
213 return
214 closed = true
215 scheduleChanged = {}
216 completions.clear()
217 for (group in groups.toArray())
218 group.close()
219 for (animation in owned.toArray())
220 animation.owner.close()
221 active.clear()
222 owned.clear()
223 }
226internal fun removeAnimationEntry(values: ArrayList<AnimationEntry>, wanted: Closeable) {
227 var index = 0L
228 while (index < values.size) {
229 if (values[index].owner === wanted) {
230 values.removeAt(index);
231 return
232 }
233 index += 1L
234 }