scoop:scoop.ui:0.1.0
src/scroll/inertia.scoop
Selected production source · src/scroll/inertia.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.*5
import scoop.surface.*6
import scoop.time.Duration8
/** One released route consumes one trajectory; it does not dispatch synthetic input. */9
internal 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 = false16
private var finishing = false17
private var previous = Point(0.0, 0.0)18
private var blockedHorizontal = false19
private var blockedVertical = false20
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 error42
}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 None49
return inputWindowVector(50
final.scope,51
origin,52
Point(53
if (blockedHorizontal)54
0.055
else56
local.x,57
if (blockedVertical)58
0.059
else60
local.y61
)62
)63
}65
fun advance() {66
if (closed)67
return68
for (entry in entries)69
if (70
!entry.active ||71
entry.viewport.motionReduced ||72
!entry.viewport.physics.inertia.enabled73
) {74
stop()75
return76
}77
if (!finishing && !applyStep())78
stop()79
}81
private fun applyStep(): Boolean {82
val point = animation.value83
val step = filter(Point(point.x - previous.x, point.y - previous.y)) ?: return false84
previous = point85
var remaining = step86
if (remaining != Point(0.0, 0.0)) {87
for (entry in entries) {88
val local = inputLocalVector(entry.scope, origin, remaining) ?: return false89
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 false94
remaining = Point(remaining.x - used.x, remaining.y - used.y)95
}96
val rest = inputLocalVector(final.scope, origin, remaining) ?: return false97
val requested = inputLocalVector(final.scope, origin, step) ?: return false98
val horizontal = !blockedHorizontal && abs(rest.x) > max(1.0, abs(requested.x)) * 1.0e-999
val vertical = !blockedVertical && abs(rest.y) > max(1.0, abs(requested.y)) * 1.0e-9100
if (horizontal || vertical) {101
val speed = animation.velocity102
val velocity =103
inputLocalVector(final.scope, origin, Point(speed.x, speed.y)) ?: return false104
blockedHorizontal = blockedHorizontal || horizontal105
blockedVertical = blockedVertical || vertical106
final.viewport.edge.kick(107
rest,108
velocity,109
horizontal && final.axes.horizontal,110
vertical && final.axes.vertical111
)112
}113
}114
val speed = animation.velocity115
val next = filter(Point(speed.x, speed.y)) ?: return false116
if (next == Point(0.0, 0.0) || animation.state == AnimationState.Finished) {117
finishing = true118
animation.cancel()119
for (entry in entries)120
entry.viewport.requestFrame()121
}122
return true123
}125
fun complete() {126
if (finishing)127
stop(true)128
}130
fun stop(keepPending: Boolean = false) {131
if (closed)132
return133
closed = true134
if (animation.state != AnimationState.Closed)135
animation.cancel()136
var index = 0L137
while (index < root.scrollInertias.size) {138
if (root.scrollInertias[index] === this)139
root.scrollInertias.removeAt(index)140
else141
index += 1L142
}143
for (entry in entries) {144
val viewport = entry.viewport145
viewport.inertia = None146
val state = viewport.state147
if (!keepPending && viewport.active) {148
state.request = ScrollRequest.Position(state.current.offset)149
state.revision += 1UL150
}151
state.changed()152
viewport.requestFrame()153
}154
}155
}157
internal fun startScrollInertia(158
root: UiRoot,159
entries: Array<ScrollInputEntry>,160
release: ScrollRelease161
) {162
val selected = ArrayList<ScrollInputEntry>()163
for (entry in entries) {164
if (165
!entry.active || entry.viewport.motionReduced || !entry.viewport.physics.inertia.enabled166
)167
break168
selected.add(entry)169
}170
if (selected.size == 0L)171
return172
val first = selected[0L]173
val local = inputLocalVector(first.scope, release.position, release.velocity) ?: return174
val window =175
inputWindowVector(first.scope, release.position, scrollAxisPoint(local, first.axes)) ?:176
return177
val options = first.viewport.physics.inertia178
val velocity = scrollInertiaVelocity(window, options)179
if (velocity == Point(0.0, 0.0))180
return181
val seconds = hypot(velocity.x, velocity.y) / options.deceleration182
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)187
}