scoop:scoop.ui:0.1.0
src/interaction/contact_transform.scoop
Selected production source · src/interaction/contact_transform.scoop
1
package scoop.ui3
import scoop.math.*4
import scoop.surface.*5
import scoop.window.*7
/** A fixed contact pair shares the same scope claim and transform event stream. */8
internal class ContactTransform(9
private val area: InputArea,10
private val read: () -> TransformGestureOptions,11
private val receive: (TransformGestureEvent) -> Unit,12
private val interruptNative: () -> Unit13
): Closeable {14
private val contacts = ArrayList<ContactId>()15
private var waiting: ContactState? = None16
private var pair: ContactTransformPair? = None17
private var blocked = false18
private var moved = false19
private var closed = false21
val active: Boolean get() = pair?.active ?: false23
private fun allowed(): Boolean {24
val options = read()25
return !closed &&26
!area.isClosed &&27
area.scope.isInputAllowed &&28
options.enabled &&29
options.contacts &&30
(options.magnify || options.rotate)31
}33
fun observe(context: RoutedEventContext, event: InputContactEvent) {34
if (closed)35
return36
when (event) {37
case Cancelled(val id, val reason, _) -> {38
forget(id)39
val current = pair ?: return40
if (current.contains(id))41
cancel(TransformGestureCancelReason.Input(reason), Some(id))42
}43
case Began(val value) -> {44
if (context.phase == RoutedPhase.Notification)45
return46
rememberContact(contacts, value.id)47
if (contacts.size > 2L) {48
cancel(TransformGestureCancelReason.AdditionalContact)49
return50
}51
if (contacts.size == 1L && !blocked && allowed()) {52
interruptNative()53
if (allowed())54
waiting = Some(value)55
}56
}57
case Moved(val value) -> {58
if (waiting?.id == Some(value.id))59
waiting = Some(value)60
}61
else -> {}62
}63
}65
fun handle(context: RoutedEventContext, event: InputContactEvent) {66
if (!allowed())67
return68
when (event) {69
case Began(val value) -> {70
if (blocked || contacts.size != 2L)71
return72
val first = waiting ?: return73
if (first.id == value.id)74
return75
pair = Some(ContactTransformPair(first, value))76
waiting = None77
}78
case Moved(val value) -> {79
moved = true80
val current = current(value.id) ?: return81
current.record(value)82
val sample = sample(current, value.pointer.time) ?: return83
val starting = !current.active84
if (starting) {85
val acquired = area.scope.claimContacts(current.ids)86
if (!acquired || !same(current) || !allowed()) {87
if (acquired)88
release(current)89
if (same(current))90
cancel(TransformGestureCancelReason.Explicit)91
return92
}93
}94
current.sample = Some(sample)95
current.advance()96
context.consume()97
receive(98
if (starting)99
TransformGestureEvent.Started(sample)100
else101
TransformGestureEvent.Changed(sample)102
)103
}104
case Ended(val value) -> {105
val current = current(value.id) ?: return106
if (!current.active) {107
cancel(TransformGestureCancelReason.Explicit)108
return109
}110
current.record(value)111
val sample = sample(current, value.pointer.time) ?: return112
pair = None113
waiting = None114
blocked = contacts.size > 0L115
context.consume()116
var failure = cleanupUi(None) { release(current, Some(value.id)) }117
failure = cleanupUi(failure) { receive(TransformGestureEvent.Ended(sample)) }118
throwUi(failure)119
}120
else -> {}121
}122
}124
private fun sample(current: ContactTransformPair, time: InputTime): TransformGestureSample? {125
val previous =126
when (127
val value =128
contactTransformFrame(area, current.previousFirst, current.previousSecond)129
) {130
case Some(val value) -> value131
case None -> {132
cancel(TransformGestureCancelReason.GeometryUnavailable)133
return None134
}135
}136
val next =137
when (138
val value = contactTransformFrame(area, current.firstPointer, current.secondPointer)139
) {140
case Some(val value) -> value141
case None -> {142
cancel(TransformGestureCancelReason.GeometryUnavailable)143
return None144
}145
}146
if (!previous.valid || !next.valid) {147
cancel(TransformGestureCancelReason.InvalidTransform)148
return None149
}150
val options = read()151
val scaleDelta =152
if (options.magnify)153
next.span / previous.span154
else155
1.0156
val rotationDelta =157
if (options.rotate)158
contactRotation(previous.angle, next.angle)159
else160
0.0161
if (!current.active) {162
val distance =163
max(164
if (options.magnify)165
abs(next.span - previous.span) / 2.0166
else167
0.0,168
min(previous.span, next.span) / 2.0 * abs(rotationDelta)169
)170
if (!distance.isFinite()) {171
cancel(TransformGestureCancelReason.InvalidTransform)172
return None173
}174
if (distance == 0.0 || distance < options.contactThreshold)175
return None176
}177
val result =178
transformSample(179
next.position,180
current.sample,181
scaleDelta,182
rotationDelta,183
time,184
current.input185
)186
when (result) {187
case Some(_) -> {}188
case None -> cancel(TransformGestureCancelReason.InvalidTransform)189
}190
return result191
}193
private fun current(id: ContactId): ContactTransformPair? {194
val current = pair ?: return None195
return if (!blocked && current.contains(id))196
Some(current)197
else198
None199
}201
private fun same(value: ContactTransformPair): Boolean =202
when (pair) {203
case Some(val current) -> current === value204
case None -> false205
}207
private fun forget(id: ContactId) {208
forgetContact(contacts, id)209
if (waiting?.id == Some(id))210
waiting = None211
if (contacts.size == 0L)212
blocked = false213
}215
fun finish(event: InputContactEvent) {216
if (closed)217
return218
when (event) {219
case Moved(val value) -> {220
if (!moved && !blocked && (pair?.contains(value.id) ?: false))221
cancel(TransformGestureCancelReason.PreventedMovement)222
moved = false223
}224
case Ended(val value) -> {225
if (!blocked && (pair?.contains(value.id) ?: false))226
cancel(TransformGestureCancelReason.PreventedEnd, Some(value.id))227
forget(value.id)228
}229
else -> {}230
}231
}233
private fun release(current: ContactTransformPair, except: ContactId? = None) {234
var failure: Throwable? = None235
for (id in current.ids)236
failure =237
cleanupUi(failure) {238
if (!area.isClosed && except != Some(id))239
area.scope.cancelContactClaim(id)240
}241
throwUi(failure)242
}244
fun cancel(reason: TransformGestureCancelReason, except: ContactId? = None) {245
val current = pair246
pair = None247
waiting = None248
blocked = contacts.size > 0L249
val value = current ?: return250
val snapshot = value.sample ?: return251
var failure = cleanupUi(None) { release(value, except) }252
failure = cleanupUi(failure) { receive(TransformGestureEvent.Cancelled(snapshot, reason)) }253
throwUi(failure)254
}256
public override fun close() {257
if (closed)258
return259
closed = true260
val failure = cleanupUi(None) { cancel(TransformGestureCancelReason.Closed) }261
contacts.clear()262
waiting = None263
blocked = false264
throwUi(failure)265
}266
}