Fix controller issues (#683)

* fix control issues

* clean up
This commit is contained in:
Allan Niles 2023-09-28 16:33:58 -06:00
parent 5b62df4b3e
commit 1b433e6858
2 changed files with 60 additions and 69 deletions

View file

@ -389,9 +389,12 @@ class EmulatorJS {
adBlocked(url, del){ adBlocked(url, del){
if (del){ if (del){
document.querySelector('div[class="ejs_ad_iframe"]').remove(); document.querySelector('div[class="ejs_ad_iframe"]').remove();
}else{ } else {
document.querySelector('iframe[src="'+this.config.adUrl+'"]').src = url; try {
document.querySelector('div[class="ejs_ad_iframe"]').remove();
} catch(e) {}
this.config.adUrl = url; this.config.adUrl = url;
this.setupAds(this.config.adUrl, this.config.adSize[0], this.config.adSize[1]);
} }
} }
functions = {}; functions = {};
@ -1887,6 +1890,7 @@ class EmulatorJS {
}, },
"Clear": () => { "Clear": () => {
this.controls = {0:{},1:{},2:{},3:{}}; this.controls = {0:{},1:{},2:{},3:{}};
this.setupKeys();
this.checkGamepadInputs(); this.checkGamepadInputs();
this.saveSettings(); this.saveSettings();
}, },
@ -2371,8 +2375,7 @@ class EmulatorJS {
textBox2.value = ""; textBox2.value = "";
textBox1.value = ""; textBox1.value = "";
if (this.controls[i][k] && this.controls[i][k].value !== undefined) { if (this.controls[i][k] && this.controls[i][k].value !== undefined) {
let value = this.controls[i][k].value.toString(); let value = this.keyMap[this.controls[i][k].value];
if (value === " ") value = "space";
value = this.localization(value); value = this.localization(value);
textBox2.value = value; textBox2.value = value;
} }
@ -2391,8 +2394,7 @@ class EmulatorJS {
}) })
if (this.controls[i][k] && this.controls[i][k].value) { if (this.controls[i][k] && this.controls[i][k].value) {
let value = this.controls[i][k].value.toString(); let value = this.keyMap[this.controls[i][k].value];
if (value === " ") value = "space";
value = this.localization(value); value = this.localization(value);
textBox2.value = value; textBox2.value = value;
} }
@ -2469,7 +2471,7 @@ class EmulatorJS {
if (!this.controls[player][num]) { if (!this.controls[player][num]) {
this.controls[player][num] = {}; this.controls[player][num] = {};
} }
this.controls[player][num].value = ""; this.controls[player][num].value = 0;
this.controls[player][num].value2 = ""; this.controls[player][num].value2 = "";
this.controlPopup.parentElement.parentElement.setAttribute("hidden", ""); this.controlPopup.parentElement.parentElement.setAttribute("hidden", "");
this.checkGamepadInputs(); this.checkGamepadInputs();
@ -2491,123 +2493,109 @@ class EmulatorJS {
0: { 0: {
0: { 0: {
'value': 'x', 'value': 'x',
'value2': 'BUTTON_2', 'value2': 'BUTTON_2'
'keycode': 88
}, },
1: { 1: {
'value': 's', 'value': 's',
'value2': 'BUTTON_4', 'value2': 'BUTTON_4'
'keycode': 83
}, },
2: { 2: {
'value': 'v', 'value': 'v',
'value2': 'SELECT', 'value2': 'SELECT'
'keycode': 86
}, },
3: { 3: {
'value': 'enter', 'value': 'enter',
'value2': 'START', 'value2': 'START'
'keycode': 13
}, },
4: { 4: {
'value': 'up arrow', 'value': 'up arrow',
'value2': 'DPAD_UP', 'value2': 'DPAD_UP'
'keycode': 38
}, },
5: { 5: {
'value': 'arrowdown', 'value': 'down arrow',
'value2': 'DPAD_DOWN', 'value2': 'DPAD_DOWN'
'keycode': 40
}, },
6: { 6: {
'value': 'arrowleft', 'value': 'left arrow',
'value2': 'DPAD_LEFT', 'value2': 'DPAD_LEFT'
'keycode': 37
}, },
7: { 7: {
'value': 'arrowright', 'value': 'right arrow',
'value2': 'DPAD_RIGHT', 'value2': 'DPAD_RIGHT'
'keycode': 39
}, },
8: { 8: {
'value': 'z', 'value': 'z',
'value2': 'BUTTON_1', 'value2': 'BUTTON_1'
'keycode': 90
}, },
9: { 9: {
'value': 'a', 'value': 'a',
'value2': 'BUTTON_3', 'value2': 'BUTTON_3'
'keycode': 65
}, },
10: { 10: {
'value': 'q', 'value': 'q',
'value2': 'LEFT_TOP_SHOULDER', 'value2': 'LEFT_TOP_SHOULDER'
'keycode': 81
}, },
11: { 11: {
'value': 'e', 'value': 'e',
'value2': 'RIGHT_TOP_SHOULDER', 'value2': 'RIGHT_TOP_SHOULDER'
'keycode': 69
}, },
12: { 12: {
'value': 'e', 'value': 'tab',
'value2': 'LEFT_BOTTOM_SHOULDER', 'value2': 'LEFT_BOTTOM_SHOULDER'
'keycode': 69
}, },
13: { 13: {
'value': 'w', 'value': 'r',
'value2': 'RIGHT_BOTTOM_SHOULDER', 'value2': 'RIGHT_BOTTOM_SHOULDER'
'keycode': 87
}, },
14: { 14: {
'value': '',
'value2': 'LEFT_STICK', 'value2': 'LEFT_STICK',
}, },
15: { 15: {
'value': '',
'value2': 'RIGHT_STICK', 'value2': 'RIGHT_STICK',
}, },
16: { 16: {
'value': 'h', 'value': 'h',
'value2': 'LEFT_STICK_X:+1', 'value2': 'LEFT_STICK_X:+1'
'keycode': 72
}, },
17: { 17: {
'value': 'f', 'value': 'f',
'value2': 'LEFT_STICK_X:-1', 'value2': 'LEFT_STICK_X:-1'
'keycode': 70
}, },
18: { 18: {
'value': 'g', 'value': 'g',
'value2': 'LEFT_STICK_Y:+1', 'value2': 'LEFT_STICK_Y:+1'
'keycode': 71
}, },
19: { 19: {
'value': 't', 'value': 't',
'value2': 'LEFT_STICK_Y:-1', 'value2': 'LEFT_STICK_Y:-1'
'keycode': 84
}, },
20: { 20: {
'value': 'l', 'value': 'l',
'value2': 'RIGHT_STICK_X:+1', 'value2': 'RIGHT_STICK_X:+1'
'keycode': 76
}, },
21: { 21: {
'value': 'j', 'value': 'j',
'value2': 'RIGHT_STICK_X:-1', 'value2': 'RIGHT_STICK_X:-1'
'keycode': '74'
}, },
22: { 22: {
'value': 'k', 'value': 'k',
'value2': 'RIGHT_STICK_Y:+1', 'value2': 'RIGHT_STICK_Y:+1'
'keycode': '75'
}, },
23: { 23: {
'value': 'i', 'value': 'i',
'value2': 'RIGHT_STICK_Y:-1', 'value2': 'RIGHT_STICK_Y:-1'
'keycode': '73' },
24: {
'value': '1'
},
25: {
'value': '2'
},
26: {
'value': '3'
}, },
24: {},
25: {},
26: {},
27: {}, 27: {},
28: {}, 28: {},
29: {}, 29: {},
@ -2617,6 +2605,7 @@ class EmulatorJS {
3: {} 3: {}
} }
keyMap = { keyMap = {
0: '',
8: 'backspace', 8: 'backspace',
9: 'tab', 9: 'tab',
13: 'enter', 13: 'enter',
@ -2721,21 +2710,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.controls[i][j].value === this.keyMap[this.keyLookup(this.controls[i][j])]) { if (this.controls[i][j] && this.keyMap) {
this.controls[i][j].keycode = Number(this.keyLookup(this.controls[i][j])); this.controls[i][j].value = Number(this.keyLookup(this.controls[i][j]));
if(this.controls[i][j].value === -1){
console.warn("Invalid key for control "+j+" player "+i+" with value "+this.keyMap[this.keyLookup(this.defaultControllers[i][j])]);
}
} }
} }
} }
} }
keyLookup(controllerkey){ keyLookup(controllerkey){
for (var key in this.keyMap) { for (var key in this.keyMap) {
if (this.keyMap[key] === controllerkey.value) { if (this.keyMap[key] === controllerkey.value || key === controllerkey.value) {
return key; return key;
}else if (controllerkey.keycode !== undefined) { } else if (controllerkey.value === undefined) {
return controllerkey.keycode; return 0;
} }
} }
return 0; return -1;
} }
keyChange(e) { keyChange(e) {
if (e.repeat) return; if (e.repeat) return;
@ -2746,8 +2738,7 @@ class EmulatorJS {
if (!this.controls[player][num]) { if (!this.controls[player][num]) {
this.controls[player][num] = {}; this.controls[player][num] = {};
} }
this.controls[player][num].value = e.key.toLowerCase(); this.controls[player][num].value = e.keyCode;
this.controls[player][num].keycode = e.keyCode;
this.controlPopup.parentElement.parentElement.setAttribute("hidden", ""); this.controlPopup.parentElement.parentElement.setAttribute("hidden", "");
this.checkGamepadInputs(); this.checkGamepadInputs();
this.saveSettings(); this.saveSettings();
@ -2758,7 +2749,7 @@ class EmulatorJS {
const special = [16, 17, 18, 19, 20, 21, 22, 23]; const special = [16, 17, 18, 19, 20, 21, 22, 23];
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.controls[i][j].keycode === e.keyCode) { if (this.controls[i][j] && this.controls[i][j].value === e.keyCode) {
this.gameManager.simulateInput(i, j, (e.type === 'keyup' ? 0 : (special.includes(j) ? 0x7fff : 1))); this.gameManager.simulateInput(i, j, (e.type === 'keyup' ? 0 : (special.includes(j) ? 0x7fff : 1)));
} }
} }

File diff suppressed because one or more lines are too long