gamepad button labels, default gamepad controls

This commit is contained in:
Alexey Nurgaliev 2023-09-02 13:34:24 +03:00
parent ed38f262a4
commit 63a686789c
2 changed files with 118 additions and 31 deletions

View file

@ -2478,65 +2478,99 @@ class EmulatorJS {
defaultControllers = { defaultControllers = {
0: { 0: {
0: { 0: {
'value': 'x' 'value': 'x',
'value2': 'BUTTON_2',
}, },
1: { 1: {
'value': 's' 'value': 's',
'value2': 'BUTTON_4',
}, },
2: { 2: {
'value': 'v' 'value': 'v',
'value2': 'SELECT',
}, },
3: { 3: {
'value': 'enter' 'value': 'enter',
'value2': 'START',
}, },
4: { 4: {
'value': 'arrowup' 'value': 'arrowup',
'value2': 'DPAD_UP',
}, },
5: { 5: {
'value': 'arrowdown' 'value': 'arrowdown',
'value2': 'DPAD_DOWN',
}, },
6: { 6: {
'value': 'arrowleft' 'value': 'arrowleft',
'value2': 'DPAD_LEFT',
}, },
7: { 7: {
'value': 'arrowright' 'value': 'arrowright',
'value2': 'DPAD_RIGHT',
}, },
8: { 8: {
'value': 'z' 'value': 'z',
'value2': 'BUTTON_1',
}, },
9: { 9: {
'value': 'a' 'value': 'a',
'value2': 'BUTTON_3',
}, },
10: { 10: {
'value': 'q' 'value': 'q',
'value2': 'LEFT_SHOULDER',
}, },
11: { 11: {
'value': 'e' 'value': 'e',
'value2': 'RIGHT_SHOULDER',
}, },
12: { 12: {
'value': 'e' 'value': 'e',
'value2': 'LEFT_SHOULDER_BOTTOM',
}, },
13: { 13: {
'value': 'w' 'value': 'w',
'value2': 'RIGHT_SHOULDER_BOTTOM',
},
14: {
'value2': 'LEFT_STICK',
},
15: {
'value2': 'RIGHT_STICK',
}, },
14: {},
15: {},
16: { 16: {
'value': 'h' 'value': 'h',
'value2': 'LEFT_STICK_X:+1',
}, },
17: { 17: {
'value': 'f' 'value': 'f',
'value2': 'LEFT_STICK_X:-1',
}, },
18: { 18: {
'value': 'g' 'value': 'g',
'value2': 'LEFT_STICK_Y:+1',
}, },
19: { 19: {
'value': 't' 'value': 't',
'value2': 'LEFT_STICK_Y:-1',
},
20: {
'value': 'l',
'value2': 'RIGHT_STICK_X:+1',
},
21: {
'value': 'j',
'value2': 'RIGHT_STICK_X:-1',
},
22: {
'value': 'k',
'value2': 'RIGHT_STICK_Y:+1',
},
23: {
'value': 'i',
'value2': 'RIGHT_STICK_Y:-1',
}, },
20: {'value': 'l'},
21: {'value': 'j'},
22: {'value': 'k'},
23: {'value': 'i'},
24: {}, 24: {},
25: {}, 25: {},
26: {}, 26: {},
@ -2592,7 +2626,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].value2 = (e.type === "axischanged" ? e.axis+":"+value : e.index); this.controls[player][num].value2 = e.label;
this.controlPopup.parentElement.parentElement.setAttribute("hidden", ""); this.controlPopup.parentElement.parentElement.setAttribute("hidden", "");
this.checkGamepadInputs(); this.checkGamepadInputs();
this.saveSettings(); this.saveSettings();
@ -2603,10 +2637,15 @@ class EmulatorJS {
for (let i=0; i<4; i++) { for (let i=0; i<4; i++) {
if (e.gamepadIndex !== i) continue; if (e.gamepadIndex !== i) continue;
for (let j=0; j<30; j++) { for (let j=0; j<30; j++) {
if (['buttonup', 'buttondown'].includes(e.type) && (this.controls[i][j] && this.controls[i][j].value2 === e.index)) { if (!this.controls[i][j] || this.controls[i][j].value2 === undefined) {
continue;
}
const controlValue = this.controls[i][j].value2;
if (['buttonup', 'buttondown'].includes(e.type) && (controlValue === e.label || controlValue === e.index)) {
this.gameManager.simulateInput(i, j, (e.type === 'buttonup' ? 0 : (special.includes(j) ? 0x7fff : 1))); this.gameManager.simulateInput(i, j, (e.type === 'buttonup' ? 0 : (special.includes(j) ? 0x7fff : 1)));
} else if (e.type === "axischanged") { } else if (e.type === "axischanged") {
if (this.controls[i][j] && typeof this.controls[i][j].value2 === 'string' && this.controls[i][j].value2.split(":")[0] === e.axis) { if (typeof controlValue === 'string' && controlValue.split(":")[0] === e.axis) {
if (special.includes(j)) { if (special.includes(j)) {
if (e.axis === 'LEFT_STICK_X') { if (e.axis === 'LEFT_STICK_X') {
if (e.value > 0) { if (e.value > 0) {
@ -2641,7 +2680,7 @@ class EmulatorJS {
this.gameManager.simulateInput(i, 22, 0); this.gameManager.simulateInput(i, 22, 0);
} }
} }
} else if (this.controls[i][j].value2 === e.axis+":"+value || value === 0) { } else if (value === 0 || controlValue === e.label || controlValue === `${e.axis}:${value}`) {
this.gameManager.simulateInput(i, j, ((value === 0) ? 0 : 1)); this.gameManager.simulateInput(i, j, ((value === 0) ? 0 : 1));
} }
} }

View file

@ -3,6 +3,24 @@ class GamepadHandler {
timeout; timeout;
listeners; listeners;
constructor() { constructor() {
this.buttonLabels = {
0: 'BUTTON_1',
1: 'BUTTON_2',
2: 'BUTTON_3',
3: 'BUTTON_4',
4: 'LEFT_SHOULDER',
5: 'RIGHT_SHOULDER',
6: 'LEFT_SHOULDER_BOTTOM',
7: 'RIGHT_SHOULDER_BOTTOM',
8: 'SELECT',
9: 'START',
10: 'LEFT_STICK',
11: 'RIGHT_STICK',
12: 'DPAD_UP',
13: 'DPAD_DOWN',
14: 'DPAD_LEFT',
15: 'DPAD_RIGHT',
};
this.gamepads = []; this.gamepads = [];
this.listeners = {}; this.listeners = {};
this.timeout = null; this.timeout = null;
@ -39,7 +57,13 @@ class GamepadHandler {
if (newVal !== val) { if (newVal !== val) {
const axis = ['LEFT_STICK_X', 'LEFT_STICK_Y', 'RIGHT_STICK_X', 'RIGHT_STICK_Y'][axisIndex]; const axis = ['LEFT_STICK_X', 'LEFT_STICK_Y', 'RIGHT_STICK_X', 'RIGHT_STICK_Y'][axisIndex];
if (!axis) return; if (!axis) return;
this.dispatchEvent('axischanged', {axis: axis, value: newVal, index: gamepad.index, gamepadIndex: gamepad.index}); this.dispatchEvent('axischanged', {
axis: axis,
value: newVal,
index: gamepad.index,
label: this.getAxisLabel(axis, newVal),
gamepadIndex: gamepad.index,
});
} }
gamepadToSave.axes[axisIndex] = newVal; gamepadToSave.axes[axisIndex] = newVal;
}) })
@ -56,9 +80,9 @@ class GamepadHandler {
gamepadToSave.buttons[buttonIndex] = {pressed:pressed2}; gamepadToSave.buttons[buttonIndex] = {pressed:pressed2};
if (pressed !== pressed2) { if (pressed !== pressed2) {
if (pressed2) { if (pressed2) {
this.dispatchEvent('buttondown', {index: buttonIndex, gamepadIndex: gamepad.index}); this.dispatchEvent('buttondown', {index: buttonIndex, label: this.getButtonLabel(buttonIndex), gamepadIndex: gamepad.index});
} else { } else {
this.dispatchEvent('buttonup', {index: buttonIndex, gamepadIndex: gamepad.index}); this.dispatchEvent('buttonup', {index: buttonIndex, label:this.getButtonLabel(buttonIndex), gamepadIndex: gamepad.index});
} }
} }
@ -97,6 +121,30 @@ class GamepadHandler {
on(name, cb) { on(name, cb) {
this.listeners[name.toLowerCase()] = cb; this.listeners[name.toLowerCase()] = cb;
} }
getButtonLabel(index) {
if (index === null || index === undefined) {
return null;
}
if (this.buttonLabels[index] === undefined) {
return `GAMEPAD_${index}`;
}
return this.buttonLabels[index];
}
getAxisLabel(axis, value) {
let valueLabel = null;
if (value > 0.5 || value < -0.5) {
if (value > 0) {
valueLabel = '+1';
} else {
valueLabel = '-1';
}
}
if (!axis || !valueLabel) {
return null;
}
return `${axis}:${valueLabel}`;
}
} }
window.GamepadHandler = GamepadHandler; window.GamepadHandler = GamepadHandler;