Fix bug with duplicate control inputs

This commit is contained in:
Ethan O'Brien 2024-01-23 23:52:01 -06:00
parent 88ec215493
commit 7cf0c2d484
3 changed files with 36 additions and 101 deletions

BIN
BtnTest.gba Normal file

Binary file not shown.

View file

@ -2390,6 +2390,23 @@ class EmulatorJS {
{id: 29, label: this.localization('SLOW MOTION')}, {id: 29, label: this.localization('SLOW MOTION')},
{id: 28, label: this.localization('REWIND')} {id: 28, label: this.localization('REWIND')}
); );
let nums = [];
for (let i=0; i<buttons.length; i++) {
nums.push(buttons[i].id);
}
for (let i=0; i<30; i++) {
if (!nums.includes(i)) {
delete this.defaultControllers[0][i];
delete this.defaultControllers[1][i];
delete this.defaultControllers[2][i];
delete this.defaultControllers[3][i];
delete this.controls[0][i];
delete this.controls[1][i];
delete this.controls[2][i];
delete this.controls[3][i];
}
}
//if (_this.statesSupported === false) { //if (_this.statesSupported === false) {
// delete buttons[24]; // delete buttons[24];
// delete buttons[25]; // delete buttons[25];
@ -2872,22 +2889,24 @@ class EmulatorJS {
setupKeys() { setupKeys() {
for (let i=0; i<4; i++) { for (let i=0; i<4; i++) {
for (let j=0; j<30; j++) { for (let j=0; j<30; j++) {
if (this.controls[i][j] && this.keyMap) { if (this.controls[i][j]) {
this.controls[i][j].value = Number(this.keyLookup(this.controls[i][j])); this.controls[i][j].value = parseInt(this.keyLookup(this.controls[i][j].value));
if(this.controls[i][j].value === -1){ if (this.controls[i][j].value === -1 && this.debug) {
console.warn("Invalid key for control "+j+" player "+i+" with value "+this.keyMap[this.keyLookup(this.defaultControllers[i][j])]); delete this.controls[i][j].value;
console.warn("Invalid key for control "+j+" player "+i);
} }
} }
} }
} }
} }
keyLookup(controllerkey) { keyLookup(controllerkey) {
for (var key in this.keyMap) { if (controllerkey === undefined) return 0;
if (this.keyMap[key] === controllerkey.value || key === controllerkey.value) { if (typeof controllerkey === "number") return controllerkey;
return key; controllerkey = controllerkey.toString().toLowerCase()
} else if (controllerkey.value === undefined) { const values = Object.values(this.keyMap);
return 0; if (values.includes(controllerkey)) {
} const index = values.indexOf(controllerkey);
return Object.keys(this.keyMap)[index];
} }
return -1; return -1;
} }

View file

@ -104,88 +104,7 @@
<input type = file id = input> <input type = file id = input>
Drag ROM file or click here Drag ROM file or click here
</div> </div>
<script> <script>
input.onchange = async () => {
const url = new Blob([input.files[0]])
const parts = input.files[0].name.split(".")
const core = await (async (ext) => {
if (["fds", "nes", "unif", "unf"].includes(ext))
return "nes"
if (["smc", "fig", "sfc", "gd3", "gd7", "dx2", "bsx", "swc"].includes(ext))
return "snes"
if (["z64", "n64"].includes(ext))
return "n64"
if (["pce"].includes(ext))
return "pce"
if (["ngp", "ngc"].includes(ext))
return "ngp"
if (["ws", "wsc"].includes(ext))
return "ws"
if (["col", "cv"].includes(ext))
return "coleco"
if (["d64"].includes(ext))
return "vice_x64"
if (["nds", "gba", "gb", "z64", "n64"].includes(ext))
return ext
return await new Promise(resolve => {
const cores = {
"Nintendo 64": "n64",
"Nintendo Game Boy": "gb",
"Nintendo Game Boy Advance": "gba",
"Nintendo DS": "nds",
"Nintendo Entertainment System": "nes",
"Super Nintendo Entertainment System": "snes",
"PlayStation": "psx",
"Virtual Boy": "vb",
"Sega Mega Drive": "segaMD",
"Sega Master System": "segaMS",
"Sega CD": "segaCD",
"Atari Lynx": "lynx",
"Sega 32X": "sega32x",
"Atari Jaguar": "jaguar",
"Sega Game Gear": "segaGG",
"Sega Saturn": "segaSaturn",
"Atari 7800": "atari7800",
"Atari 2600": "atari2600",
"NEC TurboGrafx-16/SuperGrafx/PC Engine": "pce",
"NEC PC-FX": "pcfx",
"SNK NeoGeo Pocket (Color)": "ngp",
"Bandai WonderSwan (Color)": "ws",
"ColecoVision": "coleco",
"Commodore 64": "vice_x64"
}
const button = document.createElement("button")
const select = document.createElement("select")
for (const type in cores) {
const option = document.createElement("option")
option.value = cores[type]
option.textContent = type
select.appendChild(option)
}
button.onclick = () => resolve(select[select.selectedIndex].value)
button.textContent = "Load game"
box.innerHTML = ""
box.appendChild(select)
box.appendChild(button)
})
})(parts.pop())
const div = document.createElement("div") const div = document.createElement("div")
const sub = document.createElement("div") const sub = document.createElement("div")
const script = document.createElement("script") const script = document.createElement("script")
@ -193,25 +112,22 @@
sub.id = "game" sub.id = "game"
div.id = "display" div.id = "display"
const top = document.getElementById("top"); const topp = document.getElementById("top");
top.remove(); topp.remove();
box.remove() box.remove()
div.appendChild(sub) div.appendChild(sub)
document.body.appendChild(div) document.body.appendChild(div)
window.EJS_player = "#game"; window.EJS_player = "#game";
window.EJS_gameName = parts.shift();
window.EJS_biosUrl = ""; window.EJS_biosUrl = "";
window.EJS_gameUrl = url; window.EJS_gameUrl = "BtnTest.gba";
window.EJS_core = core; window.EJS_core = "gba";
window.EJS_pathtodata = "data/"; window.EJS_pathtodata = "data/";
window.EJS_startOnLoaded = true; window.EJS_startOnLoaded = true;
window.EJS_DEBUG_XX = true;
script.src = "data/loader.js"; script.src = "data/loader.js";
document.body.appendChild(script); document.body.appendChild(script);
}
box.ondragover = () => box.setAttribute("drag", true);
box.ondragleave = () => box.removeAttribute("drag");
</script> </script>
</body> </body>
</html> </html>