-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.mjs
52 lines (45 loc) · 1.39 KB
/
util.mjs
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
// Register a callback function for a key press event
const keypressCallbacks = {};
export function registerKeypress(key, callback) {
if ($.isEmptyObject(keypressCallbacks)) {
$(window).on("keydown", (e) => {
const noActiveFocus = e.target === document.body;
if (noActiveFocus && keypressCallbacks.hasOwnProperty(e.key)) {
e.preventDefault();
keypressCallbacks[e.key]();
}
});
}
keypressCallbacks[key] = callback;
}
export function createShaderManager(canvasControls, shaderList, toggleCallback, changeCallback) {
let shaderIndex = 0;
const toggleShader = () => {
const stopped = canvasControls.toggle();
if (toggleCallback !== undefined) {
toggleCallback(stopped);
}
};
const changeShader = async () => {
// Wrap index
shaderIndex = ((shaderIndex % shaderList.length) + shaderList.length) % shaderList.length;
const entry = shaderList[shaderIndex];
let error = null;
try {
await canvasControls.changeShader(entry.file);
} catch (err) {
console.warn(err);
error = err;
}
if (changeCallback !== undefined) {
changeCallback(shaderIndex, entry, error);
}
};
return {
toggleShader: toggleShader,
nextShader: () => changeShader(++shaderIndex),
prevShader: () => changeShader(--shaderIndex),
setShader: changeShader,
apiSetIndex: (i) => (shaderIndex = i),
};
}