Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block to get details of gamepads #881

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions extensions/gamepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -272,6 +308,10 @@
}
],
},
detailMenu: {
acceptReporters: true,
items: ['id', 'vendor', 'product', 'mapping']
},
buttonMenu: {
acceptReporters: true,
items: [
Expand Down Expand Up @@ -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)) {
Expand Down