-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
370 lines (312 loc) · 9.51 KB
/
main.js
File metadata and controls
370 lines (312 loc) · 9.51 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import Html from "https://unpkg.com/@datkat21/html";
const version_info = `v1.0.5`;
window.addEventListener("load", async () => {
function ready(svg) {
window.svg = svg;
const { GamepadListener } = window.gamepad;
const listener = new GamepadListener({
deadZone: 0.05,
precision: 3,
});
listener.on("gamepad:connected", (event) => {
console.log("Connected a new gamepad.");
setup();
});
listener.on("gamepad:button", (event) => {
const {
index, // Gamepad index: Number [0-3].
button, // Button index: Number [0-N].
value, // Current value: Number between 0 and 1. Float in analog mode, integer otherwise.
pressed, // Native GamepadButton pressed value: Boolean.
gamepad, // Native Gamepad object
} = event.detail;
handleInput(pressed === true ? buttonTypes.Press : buttonTypes.Rel, {
type: "btn",
button,
value,
});
});
listener.on("gamepad:axis", (event) => {
const {
index, // Gamepad index: Number [0-3].
axis, // Axis index: Number [0-N].
value, // Current value: Number between -1 and 1. Float in analog mode, integer otherwise.
gamepad, // Native Gamepad object
} = event.detail;
handleInput(buttonTypes.Hold, {
type: "axe",
axis,
value,
});
});
listener.start();
}
let setUp = false;
const buttonTypes = {
Press: 0,
0: "Press",
Hold: 1,
1: "Hold",
Rel: 2,
2: "Rel",
};
let axeValues = new Map();
function calcPercent(value, axeId) {
// value from 0 to 1 as a 5%
/*
0 -> 0
0.25-> 1.25
0.5 -> 2.5
1 -> 5
*/
const val = value * 5;
// Also to support axe multipliers like Dolphin
const axe = axesMapping.get(axeId);
const mul = axe.multipliers !== undefined ? axe.multipliers : [1, 1],
dzn = axe.deadZone !== undefined ? axe.deadZone : 0;
if (mul == undefined) return val;
let calc = val < 0 ? val * mul[0] : val * mul[1];
let res = 0;
if (calc > dzn[1]) res = calc;
if (calc < dzn[0]) res = calc;
return calc;
}
function handleInput(
type,
button = { value: null, type: null, button: null }
) {
if (button.type === "btn") {
const btn = mapping.get(button.button);
const s = svg.querySelector(`#${btn}`);
if (btn === undefined) return;
if (Array.isArray(btn)) {
for (let i = 0; i < btn.length; i++) {
const m = svg.querySelector(`#${btn[i]}`);
if (m === null) continue;
if (button.value === 1) {
m.classList.add("pressed");
} else {
m.classList.remove("pressed");
}
}
return;
}
if (s === null) return;
if (button.value === 1) {
svg.querySelector(`#${btn}`).classList.add("pressed");
} else {
svg.querySelector(`#${btn}`).classList.remove("pressed");
}
} else if (button.type === "axe") {
// axis
const axeLookup = button.axis;
const axe = axesMapping.get(axeLookup);
if (axe == undefined) return;
// Some axes may have their own handler functions.
if (axe.handler) {
return axe.handler(button);
}
const s = svg.querySelector(`#${axe.id}`);
if (s === null) return;
switch (axe.type) {
case "x":
axeValues.set(axeLookup, button.value);
const valueY = axeValues.get(axe.sibling) || 0;
if (type === buttonTypes.Hold) {
svg.querySelector(
`#${axe.id}`
).style.transform = `translate(${calcPercent(
button.value,
axeLookup
)}%, ${calcPercent(valueY, axe.sibling)}%)`;
}
break;
case "y":
axeValues.set(axeLookup, button.value);
const valueX = axeValues.get(axe.sibling) || 0;
if (type === buttonTypes.Hold) {
svg.querySelector(
`#${axe.id}`
).style.transform = `translate(${calcPercent(
valueX,
axe.sibling
)}%, ${calcPercent(button.value, axeLookup)}%)`;
}
break;
case "trigger":
if (button.value > 0.35) {
svg.querySelector(`#${axe.id}`).classList.add("pressed");
} else if (button.value < 0.35) {
svg.querySelector(`#${axe.id}`).classList.remove("pressed");
}
break;
}
}
}
function setup() {
if (setUp === true) return;
setUp = true;
document.querySelectorAll(".svg").forEach((s) => {
s.classList.remove("visible");
});
const svg = document.querySelector(`svg`);
svg.classList.add("visible");
window.svg = svg;
}
const settings = {
// Host controller type. (This only supports one controller type so mixing won't work)
type: null,
// The style of controller to use. Only "gcn" for the moment
style: null,
// The skin to use. Default is null (uses main one)
skin: null,
// Host controller index (0-3 for 4 controllers)
index: null,
};
const search = new URLSearchParams(location.search);
if (search.get("type") != null) {
settings.type = search.get("type");
}
if (search.get("style") != null) {
settings.style = search.get("style");
}
if (search.get("skin") != null) {
settings.skin = search.get("skin");
}
function showSetupModal(
queryParameter,
optionsToChoose,
backOption = null,
description = `Include the query parameter "${queryParameter}" with one of the following:`
) {
const s = new URLSearchParams(search.toString());
s.set(queryParameter, optionsToChoose[0]);
let backOptionHtml = "";
if (backOption !== null) {
const sr = new URLSearchParams(search.toString());
sr.delete(backOption);
backOptionHtml = `<a href="?${sr.toString()}">← Back to ${backOption}</a>`;
}
popupModal(
`${description}\n\n${optionsToChoose
.map((l) => {
const s = new URLSearchParams(search.toString());
s.set(queryParameter, l);
return `<a href="?${s.toString()}">${l}</a>`;
})
.join(
", "
)}\n\nExample: <a href="?${s.toString()}">${s.toString()}</a>\n\n${backOptionHtml}`,
"Setup"
);
}
if (settings.type === null) {
const typeList = await import("./types/type_list.js");
return showSetupModal(
"type",
typeList.default,
null,
"Select the controller variation:"
);
}
if (settings.style === null) {
const styleList = await import("./styles/style_list.js");
return showSetupModal(
"style",
styleList.default,
"type",
`Choose an overlay style:`
);
}
if (settings.skin === null) {
const styleData = await import(`./styles/${settings.style}/info.js`);
showSetupModal(
"skin",
Object.keys(styleData.default.skins),
"style",
`Select a skin:`
);
}
const mapping = new Map();
const axesMapping = new Map();
function popupModal(info, title = null) {
const div = new Html("div")
.styleJs({
position: "fixed",
top: "0",
left: "0",
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
gap: "1.5rem",
justifyContent: "center",
alignItems: "center",
backgroundColor: "#0009",
backgroundImage: "url(./assets/background.png)",
backgroundRepeat: "no-repeat",
backgroundSize: "cover",
backgroundPosition: "center",
})
.appendTo("body");
if (title !== null) {
div.append(new Html("h1").html(title.replace(/\n/g, "<br>")));
}
div.appendMany(
new Html("p").html(info.replace(/\n/g, "<br>")),
new Html("a")
.styleJs({
position: "absolute",
bottom: "0.8rem",
right: "0.8rem",
})
.attr({
href: "https://github.com/datkat21/ControllerOverlays",
target: "_blank",
})
.text(`ControllerOverlays ${version_info}`)
);
}
// Load the Type
const type = await import(`./types/${settings.type}.js`).catch(
(j) => undefined
);
if (type !== undefined) {
// Set up the controller inputs
if (typeof type.default === "function") {
type.default(mapping, axesMapping);
}
// Load the Style
const style = await import(`./styles/${settings.style}/info.js`).catch(
(j) => undefined
);
if (style !== undefined) {
// Set up the controller style
let svgToLoad = style.default.main;
if (settings.skin !== null) {
if (settings.skin in style.default.skins) {
svgToLoad = style.default.skins[settings.skin];
} else {
return popupModal(
`Skin does not exist!\n\nSkin list for ${
style.default.name
}:\n\n${Object.keys(style.default.skins).join(", ")}`
);
}
}
const svg = await fetch(`./styles/${settings.style}/${svgToLoad}`)
.then((t) => t.text())
.catch((u) => undefined);
if (svg === undefined) {
return popupModal("Failed to load controller SVG.");
}
Html.qs(".svg-placeholder").append(new Html('div').class('svg-wrapper').html(svg));
const svgHtml = document.querySelector("svg");
ready(svgHtml);
} else {
return popupModal("Unable to load controller style!");
}
} else {
return popupModal("Unable to load input type!");
}
});