scoop:scoop.surface:0.1.0
src/shared/input/contact_route.scoop
Selected production source · src/shared/input/contact_route.scoop
1
package scoop.surface3
import scoop.graphics.Point4
import scoop.window.*6
internal class InputContactRoute(7
var latest: ContactState,8
var scopes: Array<InputScope>,9
val boundaryRevision: ULong10
) {11
val origin = latest12
var previous = latest13
var claimant: InputScope? = None14
var movementStarted = false15
var cancelled = false16
var ending = false17
}19
internal fun InputRouter.findContact(id: ContactId): InputContactRoute? {20
for (route in contactRoutes)21
if (route.latest.id == id)22
return Some(route)23
return None24
}26
private fun InputRouter.removeContact(route: InputContactRoute) {27
for (index in 0L ..< contactRoutes.size)28
if (contactRoutes[index] === route) {29
contactRoutes.removeAt(index)30
return31
}32
}34
internal 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]))43
internal fun InputRouter.contactUsable(route: InputContactRoute): Boolean =44
contactAllowed(route) &&45
deliveries(route.scopes, Some(contactPosition(route.latest))).size ==46
route.scopes.size48
internal fun InputRouter.contactDeliveryAllowed(49
route: InputContactRoute?,50
scope: InputScope51
): Boolean =52
when (route) {53
case Some(val value) -> contactAllowed(value) && containsScope(value.scopes, scope)54
case None -> true55
}57
private fun contactPosition(value: ContactState): Point =58
Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)60
internal 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
time68
)69
case None -> {}70
}71
return72
}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(_, _, _) -> return81
}82
if (began) {83
when (findContact(value.id)) {84
case Some(val old) -> cancelContact(85
old,86
InputCancelReason.ContactInterrupted,87
value.pointer.time88
)89
case None -> {}90
}91
}92
if (closed)93
return94
val point = contactPosition(value)95
val current =96
if (began) {97
val target = hit(point)98
val scopes =99
if (target.size > 0L)100
target101
else102
path(inputRoot)103
if (scopes.size == 0L)104
return105
val route = InputContactRoute(value, scopes, boundaryRevision)106
contactRoutes.add(route)107
route108
} else109
findContact(value.id) ?: return110
current.previous = current.latest111
current.latest = value112
current.ending = ending113
if (!contactAllowed(current)) {114
cancelContact(current, contactInvalidReason(current), value.pointer.time)115
return116
}117
val chain = deliveries(current.scopes, Some(point))118
if (chain.size == 0L) {119
cancelContact(current, InputCancelReason.AreaChanged, value.pointer.time)120
return121
}122
if (began) {123
try {124
if (125
inputRoot.beforePress(InputPress(chain[0L].scope, InputPressSource.Contact(value)))126
) {127
removeContact(current)128
return129
}130
} catch (error: Throwable) {131
removeContact(current)132
throw error133
}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
else141
if (ending)142
removeContact(current)143
}144
}146
private fun InputRouter.contactInvalidReason(route: InputContactRoute): InputCancelReason =147
if (route.boundaryRevision != boundaryRevision)148
InputCancelReason.BoundaryChanged149
else150
if (closed)151
InputCancelReason.Closed152
else153
InputCancelReason.AreaChanged155
internal fun InputRouter.cancelContact(156
current: InputContactRoute,157
reason: InputCancelReason,158
time: InputTime159
) {160
if (current.cancelled)161
return162
current.cancelled = true163
removeContact(current)164
var failure: Throwable? = None165
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 = true176
)177
}178
throwFailure(failure)179
}181
internal fun InputRouter.cancelContacts(reason: InputCancelReason, time: InputTime) {182
var failure: Throwable? = None183
for (current in contactRoutes.toArray())184
failure = cleanupFailure(failure) { cancelContact(current, reason, time) }185
throwFailure(failure)186
}188
internal fun InputRouter.reconcileContacts() {189
var failure: Throwable? = None190
for (current in contactRoutes.toArray())191
if (!contactUsable(current))192
failure =193
cleanupFailure(failure) {194
cancelContact(current, contactInvalidReason(current), inputObservation())195
}196
throwFailure(failure)197
}199
internal fun InputRouter.cancelWindowInput(reason: InputCancelReason, time: InputTime) {200
val failure = cleanupFailure(None) { cancelPointerInput(reason, time) }201
throwFailure(cleanupFailure(failure) { cancelContacts(reason, time) })202
}