-
-
Save ivikash/710ef27c2990b1c8e7410836f444ee36 to your computer and use it in GitHub Desktop.
Gamepad - Chrome DevTools integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
let gamepad = null; | |
let loopInterval = null; | |
window.addEventListener("gamepadconnected", connectHandler); | |
window.addEventListener("gamepaddisconnected", disconnectHandler); | |
function connectHandler(e) { | |
if (!gamepad) { | |
addGamepad(e.gamepad); | |
} | |
} | |
function disconnectHandler(e) { | |
if(gamepad) { | |
removeGamepad(e.gamepad); | |
} | |
} | |
function addGamepad(newGamepad) { | |
console.log('connected', newGamepad); | |
gamepad = newGamepad; | |
loopInterval = setInterval(() => { | |
updateStatus(); | |
gameLoop(); | |
}, 50); | |
} | |
function removeGamepad(removeGamepad) { | |
console.log('disconnected', removeGamepad); | |
gamepad = null; | |
clearInterval(loopInterval); | |
} | |
function updateStatus() { | |
if(!gamepad) { | |
return; | |
} | |
scanGamepads(); | |
} | |
function scanGamepads() { | |
var gamepads = navigator.getGamepads(); | |
for (var i = 0; i < gamepads.length; i++) { | |
if (gamepads[i] && gamepad.index === gamepads[i].index){ | |
gamepad = gamepads[i]; | |
} | |
} | |
} | |
const tree = UI.panels.elements._treeOutlines[0]; | |
let currentPropertyIdx = 0; | |
function gameLoop() { | |
if (leftJoystickLeft()) { | |
tree.selectedTreeElement.collapseOrAscend(false); | |
currentPropertyIdx = 0; | |
} | |
if (leftJoystickRight()) { | |
if (!tree.selectedTreeElement.revealed()) { | |
tree.selectedTreeElement.reveal(); | |
} else { | |
tree.selectedTreeElement.descendOrExpand(false); | |
} | |
currentPropertyIdx = 0; | |
} | |
if (leftJoystickTop()) { | |
tree.selectPrevious(); | |
currentPropertyIdx = 0; | |
} | |
if (leftJoystickBottom()) { | |
tree.selectNext(); | |
currentPropertyIdx = 0; | |
} | |
if (buttonX()) { | |
tree.selectedTreeElement._node.removeNode(); | |
} | |
} | |
const leftJoystickLeft = () => gamepad && gamepad.axes[0] === -1; | |
const leftJoystickRight = () => gamepad && gamepad.axes[0] === 1; | |
const leftJoystickTop = () => gamepad && gamepad.axes[1] === -1; | |
const leftJoystickBottom = () => gamepad && gamepad.axes[1] === 1; | |
const buttonX = () => gamepad && gamepad.buttons[0].pressed; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment