Scoop API

scoop:scoop.ui:0.1.0

src/scroll/inertia.scoop

Selected production source · src/scroll/inertia.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.*
5import scoop.surface.*
6import scoop.time.Duration
8/** One released route consumes one trajectory; it does not dispatch synthetic input. */
9internal class ScrollInertia(
10 private val root: UiRoot,
11 private val entries: Array<ScrollInputEntry>,
12 private val origin: Point,
13 private val animation: Animatable<Point>
14) {
15 private var closed = false
16 private var finishing = false
17 private var previous = Point(0.0, 0.0)
18 private var blockedHorizontal = false
19 private var blockedVertical = false
20 private val final: ScrollInputEntry get() = entries[entries.size - 1L]
22 fun start(distance: Point, duration: Duration) {
23 for (entry in entries)
24 entry.viewport.cancelInertia()
25 root.scrollInertias.add(this)
26 for (entry in entries) {
27 entry.viewport.inertia = Some(this)
28 entry.viewport.state.changed()
29 }
30 try {
31 animation.snapTo(Point(0.0, 0.0))
32 animation.animateTo(
33 distance,
34 AnimationSpec.Tween(
35 duration,
36 Easing.cubicBezier(1.0 / 3.0, 2.0 / 3.0, 2.0 / 3.0, 1.0)
37 )
38 )
39 } catch (error: Throwable) {
40 stop()
41 throw error
42 }
43 }
45 private fun filter(amount: Point): Point? {
46 if (!blockedHorizontal && !blockedVertical)
47 return Some(amount)
48 val local = inputLocalVector(final.scope, origin, amount) ?: return None
49 return inputWindowVector(
50 final.scope,
51 origin,
52 Point(
53 if (blockedHorizontal)
54 0.0
55 else
56 local.x,
57 if (blockedVertical)
58 0.0
59 else
60 local.y
61 )
62 )
63 }
65 fun advance() {
66 if (closed)
67 return
68 for (entry in entries)
69 if (
70 !entry.active ||
71 entry.viewport.motionReduced ||
72 !entry.viewport.physics.inertia.enabled
73 ) {
74 stop()
75 return
76 }
77 if (!finishing && !applyStep())
78 stop()
79 }
81 private fun applyStep(): Boolean {
82 val point = animation.value
83 val step = filter(Point(point.x - previous.x, point.y - previous.y)) ?: return false
84 previous = point
85 var remaining = step
86 if (remaining != Point(0.0, 0.0)) {
87 for (entry in entries) {
88 val local = inputLocalVector(entry.scope, origin, remaining) ?: return false
89 val wanted = scrollAxisPoint(local, entry.axes)
90 val consumed = entry.viewport.state.consumeInput(Point(-wanted.x, -wanted.y), false)
91 val used =
92 inputWindowVector(entry.scope, origin, Point(-consumed.x, -consumed.y)) ?:
93 return false
94 remaining = Point(remaining.x - used.x, remaining.y - used.y)
95 }
96 val rest = inputLocalVector(final.scope, origin, remaining) ?: return false
97 val requested = inputLocalVector(final.scope, origin, step) ?: return false
98 val horizontal = !blockedHorizontal && abs(rest.x) > max(1.0, abs(requested.x)) * 1.0e-9
99 val vertical = !blockedVertical && abs(rest.y) > max(1.0, abs(requested.y)) * 1.0e-9
100 if (horizontal || vertical) {
101 val speed = animation.velocity
102 val velocity =
103 inputLocalVector(final.scope, origin, Point(speed.x, speed.y)) ?: return false
104 blockedHorizontal = blockedHorizontal || horizontal
105 blockedVertical = blockedVertical || vertical
106 final.viewport.edge.kick(
107 rest,
108 velocity,
109 horizontal && final.axes.horizontal,
110 vertical && final.axes.vertical
111 )
112 }
113 }
114 val speed = animation.velocity
115 val next = filter(Point(speed.x, speed.y)) ?: return false
116 if (next == Point(0.0, 0.0) || animation.state == AnimationState.Finished) {
117 finishing = true
118 animation.cancel()
119 for (entry in entries)
120 entry.viewport.requestFrame()
121 }
122 return true
123 }
125 fun complete() {
126 if (finishing)
127 stop(true)
128 }
130 fun stop(keepPending: Boolean = false) {
131 if (closed)
132 return
133 closed = true
134 if (animation.state != AnimationState.Closed)
135 animation.cancel()
136 var index = 0L
137 while (index < root.scrollInertias.size) {
138 if (root.scrollInertias[index] === this)
139 root.scrollInertias.removeAt(index)
140 else
141 index += 1L
142 }
143 for (entry in entries) {
144 val viewport = entry.viewport
145 viewport.inertia = None
146 val state = viewport.state
147 if (!keepPending && viewport.active) {
148 state.request = ScrollRequest.Position(state.current.offset)
149 state.revision += 1UL
150 }
151 state.changed()
152 viewport.requestFrame()
153 }
154 }
157internal fun startScrollInertia(
158 root: UiRoot,
159 entries: Array<ScrollInputEntry>,
160 release: ScrollRelease
161) {
162 val selected = ArrayList<ScrollInputEntry>()
163 for (entry in entries) {
164 if (
165 !entry.active || entry.viewport.motionReduced || !entry.viewport.physics.inertia.enabled
166 )
167 break
168 selected.add(entry)
169 }
170 if (selected.size == 0L)
171 return
172 val first = selected[0L]
173 val local = inputLocalVector(first.scope, release.position, release.velocity) ?: return
174 val window =
175 inputWindowVector(first.scope, release.position, scrollAxisPoint(local, first.axes)) ?:
176 return
177 val options = first.viewport.physics.inertia
178 val velocity = scrollInertiaVelocity(window, options)
179 if (velocity == Point(0.0, 0.0))
180 return
181 val seconds = hypot(velocity.x, velocity.y) / options.deceleration
182 val distance = Point(velocity.x * (seconds / 2.0), velocity.y * (seconds / 2.0))
183 val duration = Duration.nanoseconds(ceil(seconds * 1000000000.0).toLong())
184 val motion =
185 ScrollInertia(root, selected.toArray(), release.position, first.viewport.inertiaAnimation)
186 motion.start(distance, duration)