diff --git a/extensions/gamepad.js b/extensions/gamepad.js index c476387163..efa0f333f5 100644 --- a/extensions/gamepad.js +++ b/extensions/gamepad.js @@ -71,6 +71,22 @@ return axisValue; }; + /** + * @param {Gamepad.id} id + * @returns {string} + */ + const matchVendor = (id) => { + return id.match(/vendor:\s*(\w+)/i)[1]; + }; + + /** + * @param {Gamepad.id} id + * @returns {string} + */ + const matchProduct = (id) => { + return id.match(/product:\s*(\w+)/i)[1]; + }; + class GamepadExtension { getInfo() { return { @@ -89,6 +105,26 @@ } } }, + { + opcode: 'gamepadDetail', + blockType: Scratch.BlockType.REPORTER, + text: 'get [d] of pad [i]', + arguments: { + d: { + type: Scratch.ArgumentType.STRING, + defaultValue: 'id', + menu: 'detailMenu' + }, + i: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: '1', + menu: 'padMenu' + } + } + }, + + '---', + { opcode: 'buttonDown', blockType: Scratch.BlockType.BOOLEAN, @@ -272,6 +308,10 @@ } ], }, + detailMenu: { + acceptReporters: true, + items: ['id', 'vendor', 'product', 'mapping'] + }, buttonMenu: { acceptReporters: true, items: [ @@ -402,6 +442,18 @@ return getGamepads(pad).length > 0; } + gamepadDetail ({d, i}) { + for (const gamepad of getGamepads(i)) { + switch (d) { + case 'mapping': return gamepad.mapping; + case 'vendor': return matchVendor(gamepad.id); + case 'product': return matchProduct(gamepad.id); + case 'id': return gamepad.id; + } + } + return 'not connected'; + } + buttonDown ({b, i}) { for (const gamepad of getGamepads(i)) { if (isButtonPressed(gamepad, b)) {