Scoop API

scoop:scoop.ui:0.1.0

src/interaction/contact_transform_geometry.scoop

Selected production source · src/interaction/contact_transform_geometry.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.math.*
5import scoop.surface.InputArea
6import scoop.window.*
8internal class ContactTransformPair(first: ContactState, second: ContactState) {
9 val firstId = first.id
10 val secondId = second.id
11 val ids = [first.id, second.id]
12 val input = TransformGestureInput.Contacts(first.id, first.kind, second.id, second.kind)
13 var firstPointer = first.pointer
14 var secondPointer = second.pointer
15 var previousFirst = first.pointer
16 var previousSecond = second.pointer
17 var sample: TransformGestureSample? = None
18 val active: Boolean get() =
19 when (sample) {
20 case Some(_) -> true
21 case None -> false
22 }
23 fun contains(id: ContactId): Boolean = firstId == id || secondId == id
24 fun record(value: ContactState) {
25 if (value.id == firstId)
26 firstPointer = value.pointer
27 else
28 secondPointer = value.pointer
29 }
30 fun advance() {
31 previousFirst = firstPointer
32 previousSecond = secondPointer
33 }
36internal struct ContactTransformFrame(val position: Point, val span: Double, val angle: Double) {
37 val valid: Boolean get() = span.isFinite() && span > 0.0 && angle.isFinite()
40internal fun contactTransformFrame(
41 area: InputArea,
42 first: PointerState,
43 second: PointerState
44): ContactTransformFrame? {
45 val a = area.toLocal(Point(first.logicalPosition.x, first.logicalPosition.y)) ?: return None
46 val b = area.toLocal(Point(second.logicalPosition.x, second.logicalPosition.y)) ?: return None
47 val dx = b.x - a.x
48 val dy = b.y - a.y
49 return Some(
50 ContactTransformFrame(
51 Point(a.x / 2.0 + b.x / 2.0, a.y / 2.0 + b.y / 2.0),
52 hypot(dx, dy),
53 atan2(dy, dx)
54 )
55 )
58internal fun contactRotation(previous: Double, current: Double): Double {
59 val delta = current - previous
60 return if (delta > PI)
61 delta - 2.0 * PI
62 else
63 if (delta < -PI)
64 delta + 2.0 * PI
65 else
66 delta