forked from TurboWarp/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointerlock.js
169 lines (154 loc) · 4.22 KB
/
pointerlock.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Name: Pointerlock
// ID: pointerlock
// Description: Adds blocks for mouse locking. Mouse x & y blocks will report the change since the previous frame while the pointer is locked. Replaces the pointerlock experiment.
// License: MIT AND MPL-2.0
(function (Scratch) {
"use strict";
if (!Scratch.extensions.unsandboxed) {
throw new Error("pointerlock extension must be run unsandboxed");
}
const vm = Scratch.vm;
const canvas = vm.runtime.renderer.canvas;
const mouse = vm.runtime.ioDevices.mouse;
let isLocked = false;
let isPointerLockEnabled = false;
let rect = canvas.getBoundingClientRect();
window.addEventListener("resize", () => {
rect = canvas.getBoundingClientRect();
});
const postMouseData = (e, isDown) => {
const { movementX, movementY } = e;
const { width, height } = rect;
const x = mouse._clientX + movementX;
const y = mouse._clientY - movementY;
mouse._clientX = x;
mouse._scratchX = mouse.runtime.stageWidth * (x / width - 0.5);
mouse._clientY = y;
mouse._scratchY = mouse.runtime.stageWidth * (y / height - 0.5);
if (typeof isDown === "boolean") {
const data = {
button: e.button,
isDown,
};
originalPostIOData(data);
}
};
const mouseDevice = vm.runtime.ioDevices.mouse;
const originalPostIOData = mouseDevice.postData.bind(mouseDevice);
mouseDevice.postData = (data) => {
if (!isPointerLockEnabled) {
return originalPostIOData(data);
}
};
document.addEventListener(
"mousedown",
(e) => {
// @ts-expect-error
if (canvas.contains(e.target)) {
if (isLocked) {
postMouseData(e, true);
} else if (isPointerLockEnabled) {
canvas.requestPointerLock();
}
}
},
true
);
document.addEventListener(
"mouseup",
(e) => {
if (isLocked) {
postMouseData(e, false);
// @ts-expect-error
} else if (isPointerLockEnabled && canvas.contains(e.target)) {
canvas.requestPointerLock();
}
},
true
);
document.addEventListener(
"mousemove",
(e) => {
if (isLocked) {
postMouseData(e);
}
},
true
);
document.addEventListener("pointerlockchange", () => {
isLocked = document.pointerLockElement === canvas;
});
document.addEventListener("pointerlockerror", (e) => {
console.error("Pointer lock error", e);
});
const oldStep = vm.runtime._step;
vm.runtime._step = function (...args) {
const ret = oldStep.call(this, ...args);
if (isPointerLockEnabled) {
const { width, height } = rect;
mouse._clientX = width / 2;
mouse._clientY = height / 2;
mouse._scratchX = 0;
mouse._scratchY = 0;
}
return ret;
};
vm.runtime.on("PROJECT_LOADED", () => {
isPointerLockEnabled = false;
if (isLocked) {
document.exitPointerLock();
}
});
class Pointerlock {
getInfo() {
return {
id: "pointerlock",
name: Scratch.translate("Pointerlock"),
blocks: [
{
opcode: "setLocked",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set pointer lock [enabled]"),
arguments: {
enabled: {
type: Scratch.ArgumentType.STRING,
defaultValue: "true",
menu: "enabled",
},
},
},
{
opcode: "isLocked",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("pointer locked?"),
},
],
menus: {
enabled: {
acceptReporters: true,
items: [
{
text: Scratch.translate("enabled"),
value: "true",
},
{
text: Scratch.translate("disabled"),
value: "false",
},
],
},
},
};
}
setLocked(args) {
isPointerLockEnabled = Scratch.Cast.toBoolean(args.enabled) === true;
if (!isPointerLockEnabled && isLocked) {
document.exitPointerLock();
}
}
isLocked() {
return isLocked;
}
}
Scratch.extensions.register(new Pointerlock());
})(Scratch);