-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpointerevents.ts
56 lines (51 loc) · 1.54 KB
/
pointerevents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace pointerevents {
export interface PointerEventMessage {
type: "pointerdown" | "pointerup" | "pointermove"
x: number
y: number
buttons: number
}
export interface WheelEventMessage {
type: "wheel"
dx: number
dy: number
dz: number
}
const contexts: {
click: (x: number, y: number) => void
move: (x: number, y: number) => void
wheel: (dx: number, dy: number) => void
}[] = []
//% shim=TD_NOOP
export function pushContext(
click: (x: number, y: number) => void,
move: (x: number, y: number) => void,
wheel: (dx: number, dy: number) => void
) {
contexts.push({ click, move, wheel })
setup()
}
//% shim=TD_NOOP
export function popContext() {
contexts.pop()
}
//% shim=TD_NOOP
function setup() {
control.simmessages.onReceived("pointer-events", data => {
const ctx = contexts[contexts.length - 1]
if (!ctx) return
const msg = JSON.parse(data.toString())
// down event!
if (msg.type === "pointerdown") {
const m = msg as PointerEventMessage
ctx.click(m.x, m.y)
} else if (msg.type === "pointermove") {
const m = msg as PointerEventMessage
ctx.move(m.x, m.y)
} else if (msg.type === "wheel") {
const m = msg as WheelEventMessage
ctx.wheel(m.dx, m.dy)
}
})
}
}