Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/group.scoop

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

1package scoop.surface
3import scoop.graphics.Color
4import scoop.graphics.Point
5import scoop.graphics.Size
7/** Owns a set of values on the same timeline with an independent frame gate. */
8public class AnimationGroup internal constructor(
9 private val timeline: AnimationTimeline,
10 initial: Boolean
11): Closeable {
12 private val members = ArrayList<AnimationEntry>()
13 private var closed = false
14 private var enabled = initial
15 public val isClosed: Boolean get() {
16 timeline.checkThread()
17 return closed
18 }
19 public var isFrameEnabled: Boolean
20 get() {
21 timeline.checkThread()
22 return enabled
23 }
24 set(value) {
25 checkOpen()
26 if (enabled == value)
27 return
28 enabled = value
29 if (value) {
30 val time = timeline.now()
31 for (member in members.toArray())
32 member.sample(time)
33 }
34 timeline.changed(needsFrame())
35 }
36 public fun value(initial: Double): Animatable<Double> =
37 value(initial, AnimationConverters.DoubleValue)
38 public fun value(initial: Point): Animatable<Point> =
39 value(initial, AnimationConverters.PointValue)
40 public fun value(initial: Size): Animatable<Size> =
41 value(initial, AnimationConverters.SizeValue)
42 public fun value(initial: Color): Animatable<Color> =
43 value(initial, AnimationConverters.ColorValue)
44 public fun <T> value(initial: T, converter: AnimationConverter<T>): Animatable<T> {
45 checkOpen()
46 return timeline.createValue(initial, converter, Some(this))
47 }
48 internal fun add(entry: AnimationEntry) {
49 members.add(entry)
50 }
51 internal fun remove(value: Closeable) {
52 removeAnimationEntry(members, value)
53 }
54 private fun needsFrame(): Boolean {
55 for (member in members)
56 if (member.frameEnabled())
57 return true
58 return false
59 }
60 private fun checkOpen() {
61 timeline.checkOpen()
62 if (closed)
63 throw IllegalStateException(Some("animation group is closed"))
64 }
65 public override fun close() {
66 timeline.checkThread()
67 if (closed)
68 return
69 val repaint = needsFrame()
70 closed = true
71 enabled = false
72 var failure: Throwable? = None
73 for (member in members.toArray())
74 failure = cleanupFailure(failure) { member.owner.close() }
75 members.clear()
76 timeline.detachedGroup(this)
77 throwFailure(cleanupFailure(failure) { timeline.changed(repaint) })
78 }