Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/contact_route.scoop

Selected production source · src/shared/input/contact_route.scoop

1package scoop.surface
3import scoop.graphics.Point
4import scoop.window.*
6internal class InputContactRoute(
7 var latest: ContactState,
8 var scopes: Array<InputScope>,
9 val boundaryRevision: ULong
10) {
11 val origin = latest
12 var previous = latest
13 var claimant: InputScope? = None
14 var movementStarted = false
15 var cancelled = false
16 var ending = false
19internal fun InputRouter.findContact(id: ContactId): InputContactRoute? {
20 for (route in contactRoutes)
21 if (route.latest.id == id)
22 return Some(route)
23 return None
26private fun InputRouter.removeContact(route: InputContactRoute) {
27 for (index in 0L ..< contactRoutes.size)
28 if (contactRoutes[index] === route) {
29 contactRoutes.removeAt(index)
30 return
31 }
34internal fun InputRouter.contactAllowed(route: InputContactRoute): Boolean =
35 !closed &&
36 !route.cancelled &&
37 route.boundaryRevision ==
38 boundaryRevision &&
39 route.scopes.size >
40 0L &&
41 samePath(route.scopes, path(route.scopes[0L]))
43internal fun InputRouter.contactUsable(route: InputContactRoute): Boolean =
44 contactAllowed(route) &&
45 deliveries(route.scopes, Some(contactPosition(route.latest))).size ==
46 route.scopes.size
48internal fun InputRouter.contactDeliveryAllowed(
49 route: InputContactRoute?,
50 scope: InputScope
51): Boolean =
52 when (route) {
53 case Some(val value) -> contactAllowed(value) && containsScope(value.scopes, scope)
54 case None -> true
55 }
57private fun contactPosition(value: ContactState): Point =
58 Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)
60internal fun InputRouter.contactEvent(event: ContactEvent) {
61 when (event) {
62 case Cancelled(val id, val reason, val time) -> {
63 when (findContact(id)) {
64 case Some(val current) -> cancelContact(
65 current,
66 InputCancelReason.Window(reason),
67 time
68 )
69 case None -> {}
70 }
71 return
72 }
73 else -> {}
74 }
75 val(value, began, ending, input) =
76 when (event) {
77 case Began(val state) -> (state, true, false, InputContactEvent.Began(state))
78 case Moved(val state) -> (state, false, false, InputContactEvent.Moved(state))
79 case Ended(val state) -> (state, false, true, InputContactEvent.Ended(state))
80 case Cancelled(_, _, _) -> return
81 }
82 if (began) {
83 when (findContact(value.id)) {
84 case Some(val old) -> cancelContact(
85 old,
86 InputCancelReason.ContactInterrupted,
87 value.pointer.time
88 )
89 case None -> {}
90 }
91 }
92 if (closed)
93 return
94 val point = contactPosition(value)
95 val current =
96 if (began) {
97 val target = hit(point)
98 val scopes =
99 if (target.size > 0L)
100 target
101 else
102 path(inputRoot)
103 if (scopes.size == 0L)
104 return
105 val route = InputContactRoute(value, scopes, boundaryRevision)
106 contactRoutes.add(route)
107 route
108 } else
109 findContact(value.id) ?: return
110 current.previous = current.latest
111 current.latest = value
112 current.ending = ending
113 if (!contactAllowed(current)) {
114 cancelContact(current, contactInvalidReason(current), value.pointer.time)
115 return
116 }
117 val chain = deliveries(current.scopes, Some(point))
118 if (chain.size == 0L) {
119 cancelContact(current, InputCancelReason.AreaChanged, value.pointer.time)
120 return
121 }
122 if (began) {
123 try {
124 if (
125 inputRoot.beforePress(InputPress(chain[0L].scope, InputPressSource.Contact(value)))
126 ) {
127 removeContact(current)
128 return
129 }
130 } catch (error: Throwable) {
131 removeContact(current)
132 throw error
133 }
134 }
135 try {
136 route(chain, InputEvent.Contact(input), value.pointer.time, contact = Some(current))
137 } finally {
138 if (!contactUsable(current))
139 cancelContact(current, contactInvalidReason(current), value.pointer.time)
140 else
141 if (ending)
142 removeContact(current)
143 }
146private fun InputRouter.contactInvalidReason(route: InputContactRoute): InputCancelReason =
147 if (route.boundaryRevision != boundaryRevision)
148 InputCancelReason.BoundaryChanged
149 else
150 if (closed)
151 InputCancelReason.Closed
152 else
153 InputCancelReason.AreaChanged
155internal fun InputRouter.cancelContact(
156 current: InputContactRoute,
157 reason: InputCancelReason,
158 time: InputTime
159) {
160 if (current.cancelled)
161 return
162 current.cancelled = true
163 removeContact(current)
164 var failure: Throwable? = None
165 for (scope in current.scopes)
166 failure =
167 cleanupFailure(failure) {
168 notify(
169 scope,
170 InputEvent.Contact(
171 InputContactEvent.Cancelled(current.latest.id, reason, time)
172 ),
173 time,
174 Some(current.scopes[0L]),
175 terminal = true
176 )
177 }
178 throwFailure(failure)
181internal fun InputRouter.cancelContacts(reason: InputCancelReason, time: InputTime) {
182 var failure: Throwable? = None
183 for (current in contactRoutes.toArray())
184 failure = cleanupFailure(failure) { cancelContact(current, reason, time) }
185 throwFailure(failure)
188internal fun InputRouter.reconcileContacts() {
189 var failure: Throwable? = None
190 for (current in contactRoutes.toArray())
191 if (!contactUsable(current))
192 failure =
193 cleanupFailure(failure) {
194 cancelContact(current, contactInvalidReason(current), inputObservation())
195 }
196 throwFailure(failure)
199internal fun InputRouter.cancelWindowInput(reason: InputCancelReason, time: InputTime) {
200 val failure = cleanupFailure(None) { cancelPointerInput(reason, time) }
201 throwFailure(cleanupFailure(failure) { cancelContacts(reason, time) })