Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/pointer.scoop

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

1package scoop.surface
3import scoop.graphics.Point
4import scoop.window.*
6internal fun InputRouter.pointerEvent(event: PointerEvent) {
7 when (event) {
8 case Cancelled(val reason, val time) -> {
9 if (reason == PointerCancelReason.SystemOperation)
10 cancelWindowInput(InputCancelReason.Window(reason), time)
11 else
12 cancelPointerInput(InputCancelReason.Window(reason), time)
13 return
14 }
15 case Wheel(val state, _, val phase, val momentum) -> prepareScroll(
16 phase,
17 momentum,
18 state.time
19 )
20 else -> {}
21 }
22 if (closed)
23 return
24 val state = eventState(event)
25 pointer = Some(state)
26 inside = true
27 val point = Point(state.logicalPosition.x, state.logicalPosition.y)
28 val target = hit(point)
29 val fallback =
30 if (target.size == 0L)
31 path(inputRoot)
32 else
33 target
34 val firstPress =
35 when (event) {
36 case Button(_, _, ElementState.Pressed, _) -> buttons.size == 0L
37 else -> false
38 }
39 val scroll =
40 when (event) {
41 case Wheel(_, _, val phase, val momentum) -> selectScroll(fallback, phase, momentum)
42 else -> None
43 }
44 val selected =
45 when (event) {
46 case Button(_, val button, ElementState.Pressed, _) -> {
47 if (buttons.size == 0L) {
48 pointerClaim = None
49 pressed = Some(fallback)
50 }
51 if (!hasButton(buttons, button))
52 buttons.add(button)
53 pressed ?: []
54 }
55 case Button(_, val button, ElementState.Released, _) -> {
56 val route: Array<InputScope> =
57 if (hasButton(buttons, button))
58 pressed ?: []
59 else
60 []
61 removeButton(buttons, button)
62 if (buttons.size == 0L) {
63 pressed = None
64 pointerClaim = None
65 }
66 route
67 }
68 case Left(_) -> {
69 inside = false
70 []
71 }
72 case Wheel(_, _, _, _) -> when (scroll) {
73 case Some(val route) -> scrollPath(route)
74 case None -> fallback
75 }
76 else -> pressed ?: fallback
77 }
78 // Freeze all coordinates before hover callbacks can change logical or published geometry.
79 val chain = deliveries(selected, Some(point))
80 val consumed = interceptPress(firstPress, chain, state, event)
81 refreshHover(state.time)
82 if (consumed)
83 return
84 val value = InputPointer(state, state.time)
85 when (event) {
86 case Moved(_) -> route(
87 chain,
88 InputEvent.Pointer(InputPointerEvent.Moved(value)),
89 state.time
90 )
91 case Button(_, val button, val state, val clicks) -> route(
92 chain,
93 InputEvent.Pointer(InputPointerEvent.Button(value, button, state, clicks)),
94 value.time
95 )
96 case Wheel(_, val delta, val phase, val momentum) -> {
97 val logical =
98 when (delta) {
99 case LineDelta(val x, val y) -> InputScrollDelta.Lines(x, y)
100 case PixelDelta(val x, val y) -> {
101 requireInput(
102 state.scaleFactor.isFinite() && state.scaleFactor > 0.0,
103 "invalid scroll scale"
104 )
105 InputScrollDelta.Pixels(x / state.scaleFactor, y / state.scaleFactor)
106 }
107 }
108 checkedScrollPoint(scrollVector(logical))
109 try {
110 route(
111 chain,
112 InputEvent.Pointer(InputPointerEvent.Wheel(value, logical, phase, momentum)),
113 state.time,
114 scroll
115 )
116 } finally {
117 finishScroll(scroll, phase, momentum)
118 }
119 }
120 else -> {}
121 }
124private fun InputRouter.interceptPress(
125 first: Boolean,
126 chain: Array<RouteDelivery>,
127 state: PointerState,
128 event: PointerEvent
129): Boolean {
130 if (!first || chain.size == 0L)
131 return false
132 val(button, clicks) =
133 when (event) {
134 case Button(_, val value, ElementState.Pressed, val count) -> (value, count)
135 else -> return false
136 }
137 val root = inputRoot
138 if (!root.isInputAllowed)
139 return false
140 try {
141 val consumed =
142 root.beforePress(
143 InputPress(
144 chain[0L].scope,
145 InputPressSource.Mouse(InputPointer(state, state.time), button, clicks)
146 )
147 )
148 if (consumed)
149 pressed = Some([])
150 return consumed
151 } catch (error: Throwable) {
152 pressed = Some([])
153 throw error
154 }
157internal fun InputRouter.reconcilePress(boundaryChange: Boolean = false) {
158 when (pressed) {
159 case Some(val route) -> {
160 if (route.size > 0L && (boundaryChange || !samePath(route, path(route[0L]))))
161 cancelPress(
162 if (boundaryChange)
163 InputCancelReason.BoundaryChanged
164 else
165 InputCancelReason.AreaChanged,
166 inputObservation(),
167 false
168 )
169 }
170 case None -> {}
171 }
174internal fun InputRouter.cancelPress(reason: InputCancelReason, time: InputTime, reset: Boolean) {
175 val old = pressed ?: []
176 pointerClaim = None
177 pressed =
178 if (reset)
179 None
180 else
181 Some([])
182 if (reset)
183 buttons.clear()
184 var failure: Throwable? = None
185 for (scope in old)
186 failure =
187 cleanupFailure(failure) {
188 notify(
189 scope,
190 InputEvent.Pointer(InputPointerEvent.Cancelled(reason, time)),
191 time,
192 Some(old[0L]),
193 terminal = true
194 )
195 }
196 throwFailure(failure)
199internal fun InputRouter.refreshHover(time: InputTime) {
200 val next: Array<InputScope> =
201 when (pointer) {
202 case Some(val state) -> if (inside && !closed)
203 hit(Point(state.logicalPosition.x, state.logicalPosition.y))
204 else
205 []
206 case None -> []
207 }
208 replaceHover(next, time)
211internal fun InputRouter.replaceHover(next: Array<InputScope>, time: InputTime) {
212 val old = hover
213 hover = next
214 val state = pointer
215 val entries: Array<RouteDelivery> =
216 when (state) {
217 case Some(val value) -> deliveries(
218 next,
219 Some(Point(value.logicalPosition.x, value.logicalPosition.y))
220 )
221 case None -> []
222 }
223 var failure: Throwable? = None
224 for (scope in old) {
225 if (!containsScope(next, scope))
226 failure =
227 cleanupFailure(failure) {
228 notify(
229 scope,
230 InputEvent.Pointer(InputPointerEvent.Left(time)),
231 time,
232 Some(old[0L]),
233 terminal = true
234 )
235 }
236 }
237 var index = entries.size
238 while (index > 0L) {
239 index -= 1L
240 val entry = entries[index]
241 if (!containsScope(old, entry.scope))
242 failure =
243 cleanupFailure(failure) {
244 notify(
245 entry.scope,
246 InputEvent.Pointer(InputPointerEvent.Entered(InputPointer(state!!, time))),
247 time,
248 Some(next[0L]),
249 entry.position
250 )
251 }
252 }
253 throwFailure(failure)
256private fun eventState(event: PointerEvent): PointerState =
257 when (event) {
258 case Entered(val state) -> state
259 case Left(val state) -> state
260 case Moved(val state) -> state
261 case Button(val state, _, _, _) -> state
262 case Wheel(val state, _, _, _) -> state
263 case Cancelled(_, _) -> throw IllegalStateException(Some("cancel has no pointer position"))
264 }
266internal fun hasButton(buttons: ArrayList<MouseButton>, button: MouseButton): Boolean {
267 for (value in buttons)
268 if (value == button)
269 return true
270 return false
273private fun removeButton(buttons: ArrayList<MouseButton>, button: MouseButton) {
274 var index = 0L
275 while (index < buttons.size) {
276 if (buttons[index] == button) {
277 buttons.removeAt(index)
278 return
279 }
280 index += 1L
281 }