scoop:scoop.surface:0.1.0
src/shared/input/contact_consumption.scoop
Selected production source · src/shared/input/contact_consumption.scoop
1
package scoop.surface3
import scoop.graphics.Point4
import scoop.graphics.native.CanvasGeometry5
import scoop.window.ContactState7
/** Sharing begins with the claimant, separately for each movement delivery. */8
internal class ContactConsumption(private val route: InputContactRoute) {9
private var shared: ScrollConsumption? = None11
val remaining: Point? get() =12
when (shared) {13
case Some(val value) -> Some(scrollVector(value.remaining))14
case None -> None15
}17
fun consume(18
receiver: InputScope,19
geometry: CanvasGeometry?,20
apply: (Point) -> Point,21
notifyEmpty: Boolean = false22
) {23
val value =24
when (shared) {25
case Some(val current) -> current26
case None -> {27
if (!sameScope(route.claimant, Some(receiver)))28
throw IllegalStateException(29
Some("contact movement must be opened by its owner")30
)31
val start =32
position(33
if (route.movementStarted)34
route.previous35
else36
route.origin37
)38
val end = position(route.latest)39
val delta = Point(end.x - start.x, end.y - start.y)40
checkedScrollPoint(delta)41
val current =42
ScrollConsumption(InputScrollDelta.Pixels(delta.x, delta.y), start, None)43
shared = Some(current)44
route.movementStarted = true45
current46
}47
}48
val anchor =49
when (geometry) {50
case Some(val current) -> Some(51
current.toLocal(value.point) ?:52
throw IllegalArgumentException(53
Some("contact movement transformation overflow")54
)55
)56
case None -> None57
}58
value.consume(1.0, geometry, anchor, apply, notifyEmpty)59
}60
}62
internal fun contactConsumption(event: InputEvent, route: InputContactRoute?): ContactConsumption? {63
val current = route ?: return None64
return when (event) {65
case Contact(InputContactEvent.Moved(_)) -> Some(ContactConsumption(current))66
case Contact(InputContactEvent.Ended(_)) -> Some(ContactConsumption(current))67
else -> None68
}69
}71
private fun position(value: ContactState): Point =72
Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)