Skip to content

Commit

Permalink
use gamepad control labels, default gamepad controls
Browse files Browse the repository at this point in the history
  • Loading branch information
n-at committed Sep 2, 2023
1 parent e99eab3 commit 55a0784
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 301 deletions.
33 changes: 28 additions & 5 deletions assets/netplay-ui-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@
// Controls
///////////////////////////////////////////////////////////////////////////

const ButtonLabels = {
0: 'BUTTON_1',
1: 'BUTTON_2',
2: 'BUTTON_3',
3: 'BUTTON_4',
4: 'LEFT_TOP_SHOULDER',
5: 'RIGHT_TOP_SHOULDER',
6: 'LEFT_BOTTOM_SHOULDER',
7: 'RIGHT_BOTTOM_SHOULDER',
8: 'SELECT',
9: 'START',
10: 'LEFT_STICK',
11: 'RIGHT_STICK',
12: 'DPAD_UP',
13: 'DPAD_DOWN',
14: 'DPAD_LEFT',
15: 'DPAD_RIGHT',
};

let gamepadId = null;
let gamepadPrevState = {};

Expand Down Expand Up @@ -134,7 +153,11 @@
const pressedButtons = [];
gamepad.buttons.forEach((button, buttonIdx) => {
if (button.pressed) {
pressedButtons.push(buttonIdx.toString());
if (ButtonLabels[buttonIdx] !== undefined) {
pressedButtons.push(ButtonLabels[buttonIdx]);
} else {
pressedButtons.push(`GAMEPAD_${buttonIdx}`);
}
}
});
gamepad.axes.forEach((axisValue, axisIdx) => {
Expand All @@ -146,7 +169,7 @@
if (value === 0) {
return;
}
pressedButtons.push(`${name}:${value}`);
pressedButtons.push(`${name}:${value < 0 ? '-1' : '+1'}`);
});

//map state
Expand Down Expand Up @@ -184,7 +207,7 @@
// Control scheme
///////////////////////////////////////////////////////////////////////////

const controlSchemeMapping = {
const ControlSchemeMapping = {
0: 'b',
1: 'y',
2: 'select',
Expand Down Expand Up @@ -238,7 +261,7 @@

function saveControlScheme() {
for (let buttonId in window.ControlScheme) {
const buttonName = controlSchemeMapping[buttonId];
const buttonName = ControlSchemeMapping[buttonId];

const keyboardInput = document.querySelector(`input.keyboard[data-btn="${buttonName}"]`);
if (keyboardInput) {
Expand Down Expand Up @@ -270,7 +293,7 @@

function renderFormControls() {
for (let buttonId in window.ControlScheme) {
const buttonName = controlSchemeMapping[buttonId];
const buttonName = ControlSchemeMapping[buttonId];

const keyboardInput = document.querySelector(`input.keyboard[data-btn="${buttonName}"]`);
if (keyboardInput) {
Expand Down
27 changes: 25 additions & 2 deletions assets/platform-settings-controls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
(() => {

const ButtonLabels = {
0: 'BUTTON_1',
1: 'BUTTON_2',
2: 'BUTTON_3',
3: 'BUTTON_4',
4: 'LEFT_TOP_SHOULDER',
5: 'RIGHT_TOP_SHOULDER',
6: 'LEFT_BOTTOM_SHOULDER',
7: 'RIGHT_BOTTOM_SHOULDER',
8: 'SELECT',
9: 'START',
10: 'LEFT_STICK',
11: 'RIGHT_STICK',
12: 'DPAD_UP',
13: 'DPAD_DOWN',
14: 'DPAD_LEFT',
15: 'DPAD_RIGHT',
};

let modalKeyboard, modalGamepad;

window.addEventListener('load', () => {
Expand Down Expand Up @@ -83,7 +102,11 @@

gamepad.buttons.forEach((button, buttonIdx) => {
if (button.pressed) {
value = buttonIdx;
if (ButtonLabels[buttonIdx] !== undefined) {
value = ButtonLabels[buttonIdx];
} else {
value = `GAMEPAD_${buttonIdx}`;
}
active = true;
}
});
Expand All @@ -99,7 +122,7 @@
}

active = true;
value = `${axisName}:${Math.round(axisValue)}`;
value = `${axisName}:${axisValue > 0 ? '+1' : '-1'}`;
});

if (active) {
Expand Down
Loading

0 comments on commit 55a0784

Please sign in to comment.