-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointerlock.js
144 lines (131 loc) · 3.61 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
(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 => {
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);
} 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 => {
// eslint-disable-next-line no-console
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;
};
class Pointerlock {
getInfo () {
return {
id: 'pointerlock',
name: 'Pointerlock',
blocks: [
{
opcode: 'setLocked',
blockType: Scratch.BlockType.COMMAND,
text: 'set pointer lock [enabled]',
arguments: {
enabled: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'true',
menu: 'enabled'
}
}
},
{
opcode: 'isLocked',
blockType: Scratch.BlockType.BOOLEAN,
text: 'is pointer locked?'
}
],
menus: {
enabled: {
acceptReporters: true,
items: [
{
text: 'enabled',
value: 'true'
},
{
text: 'disabled',
value: 'false'
}
]
}
}
};
}
setLocked (args) {
isPointerLockEnabled = args.enabled === 'true';
if (!isPointerLockEnabled && isLocked) {
document.exitPointerLock();
}
}
isLocked () {
return isLocked;
}
}
Scratch.extensions.register(new Pointerlock());
})(Scratch);