Scoop API

scoop:scoop.ui:0.1.0

src/interaction/contact_transform.scoop

Selected production source · src/interaction/contact_transform.scoop

1package scoop.ui
3import scoop.math.*
4import scoop.surface.*
5import scoop.window.*
7/** A fixed contact pair shares the same scope claim and transform event stream. */
8internal class ContactTransform(
9 private val area: InputArea,
10 private val read: () -> TransformGestureOptions,
11 private val receive: (TransformGestureEvent) -> Unit,
12 private val interruptNative: () -> Unit
13): Closeable {
14 private val contacts = ArrayList<ContactId>()
15 private var waiting: ContactState? = None
16 private var pair: ContactTransformPair? = None
17 private var blocked = false
18 private var moved = false
19 private var closed = false
21 val active: Boolean get() = pair?.active ?: false
23 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 return
36 when (event) {
37 case Cancelled(val id, val reason, _) -> {
38 forget(id)
39 val current = pair ?: return
40 if (current.contains(id))
41 cancel(TransformGestureCancelReason.Input(reason), Some(id))
42 }
43 case Began(val value) -> {
44 if (context.phase == RoutedPhase.Notification)
45 return
46 rememberContact(contacts, value.id)
47 if (contacts.size > 2L) {
48 cancel(TransformGestureCancelReason.AdditionalContact)
49 return
50 }
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 return
68 when (event) {
69 case Began(val value) -> {
70 if (blocked || contacts.size != 2L)
71 return
72 val first = waiting ?: return
73 if (first.id == value.id)
74 return
75 pair = Some(ContactTransformPair(first, value))
76 waiting = None
77 }
78 case Moved(val value) -> {
79 moved = true
80 val current = current(value.id) ?: return
81 current.record(value)
82 val sample = sample(current, value.pointer.time) ?: return
83 val starting = !current.active
84 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 return
92 }
93 }
94 current.sample = Some(sample)
95 current.advance()
96 context.consume()
97 receive(
98 if (starting)
99 TransformGestureEvent.Started(sample)
100 else
101 TransformGestureEvent.Changed(sample)
102 )
103 }
104 case Ended(val value) -> {
105 val current = current(value.id) ?: return
106 if (!current.active) {
107 cancel(TransformGestureCancelReason.Explicit)
108 return
109 }
110 current.record(value)
111 val sample = sample(current, value.pointer.time) ?: return
112 pair = None
113 waiting = None
114 blocked = contacts.size > 0L
115 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) -> value
131 case None -> {
132 cancel(TransformGestureCancelReason.GeometryUnavailable)
133 return None
134 }
135 }
136 val next =
137 when (
138 val value = contactTransformFrame(area, current.firstPointer, current.secondPointer)
139 ) {
140 case Some(val value) -> value
141 case None -> {
142 cancel(TransformGestureCancelReason.GeometryUnavailable)
143 return None
144 }
145 }
146 if (!previous.valid || !next.valid) {
147 cancel(TransformGestureCancelReason.InvalidTransform)
148 return None
149 }
150 val options = read()
151 val scaleDelta =
152 if (options.magnify)
153 next.span / previous.span
154 else
155 1.0
156 val rotationDelta =
157 if (options.rotate)
158 contactRotation(previous.angle, next.angle)
159 else
160 0.0
161 if (!current.active) {
162 val distance =
163 max(
164 if (options.magnify)
165 abs(next.span - previous.span) / 2.0
166 else
167 0.0,
168 min(previous.span, next.span) / 2.0 * abs(rotationDelta)
169 )
170 if (!distance.isFinite()) {
171 cancel(TransformGestureCancelReason.InvalidTransform)
172 return None
173 }
174 if (distance == 0.0 || distance < options.contactThreshold)
175 return None
176 }
177 val result =
178 transformSample(
179 next.position,
180 current.sample,
181 scaleDelta,
182 rotationDelta,
183 time,
184 current.input
185 )
186 when (result) {
187 case Some(_) -> {}
188 case None -> cancel(TransformGestureCancelReason.InvalidTransform)
189 }
190 return result
191 }
193 private fun current(id: ContactId): ContactTransformPair? {
194 val current = pair ?: return None
195 return if (!blocked && current.contains(id))
196 Some(current)
197 else
198 None
199 }
201 private fun same(value: ContactTransformPair): Boolean =
202 when (pair) {
203 case Some(val current) -> current === value
204 case None -> false
205 }
207 private fun forget(id: ContactId) {
208 forgetContact(contacts, id)
209 if (waiting?.id == Some(id))
210 waiting = None
211 if (contacts.size == 0L)
212 blocked = false
213 }
215 fun finish(event: InputContactEvent) {
216 if (closed)
217 return
218 when (event) {
219 case Moved(val value) -> {
220 if (!moved && !blocked && (pair?.contains(value.id) ?: false))
221 cancel(TransformGestureCancelReason.PreventedMovement)
222 moved = false
223 }
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? = None
235 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 = pair
246 pair = None
247 waiting = None
248 blocked = contacts.size > 0L
249 val value = current ?: return
250 val snapshot = value.sample ?: return
251 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 return
259 closed = true
260 val failure = cleanupUi(None) { cancel(TransformGestureCancelReason.Closed) }
261 contacts.clear()
262 waiting = None
263 blocked = false
264 throwUi(failure)
265 }