scoop:scoop.ui:0.1.0
src/scroll/inertia_options.scoop
Selected production source · src/scroll/inertia_options.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.*6
/** Direct-contact coast parameters in window-logical units and seconds. */7
public class ScrollInertiaOptions public constructor(8
public val enabled: Boolean = true,9
public val deceleration: Double = 2400.0,10
public val minVelocity: Double = 50.0,11
public val maxVelocity: Double = 8000.012
) {13
init {14
requireUi(deceleration.isFinite() && deceleration > 0.0, "invalid scroll deceleration")15
requireUi(maxVelocity.isFinite() && maxVelocity > 0.0, "invalid maximum scroll velocity")16
requireUi(17
minVelocity.isFinite() && minVelocity >= 0.0 && minVelocity <= maxVelocity,18
"invalid minimum scroll velocity"19
)20
val seconds = maxVelocity / deceleration21
requireUi(22
seconds.isFinite() &&23
seconds *24
1000000000.0 <25
Long.MAX_VALUE.toDouble() &&26
(maxVelocity * (seconds / 2.0)).isFinite(),27
"scroll inertia exceeds representable time or distance"28
)29
}30
}32
internal fun sameScrollInertia(a: ScrollInertiaOptions, b: ScrollInertiaOptions): Boolean =33
a.enabled ==34
b.enabled &&35
a.deceleration ==36
b.deceleration &&37
a.minVelocity ==38
b.minVelocity &&39
a.maxVelocity ==40
b.maxVelocity42
internal fun scrollInertiaVelocity(value: Point, options: ScrollInertiaOptions): Point {43
val scale = max(abs(value.x), abs(value.y))44
if (!options.enabled || scale == 0.0 || !scale.isFinite())45
return Point(0.0, 0.0)46
val length = hypot(value.x / scale, value.y / scale)47
if (scale < options.minVelocity / length)48
return Point(0.0, 0.0)49
if (scale > options.maxVelocity / length) {50
val factor = (options.maxVelocity / scale) / length51
return Point(value.x * factor, value.y * factor)52
}53
return value54
}