Skip to content

Commit

Permalink
Merge pull request #1020 from gnarf/sb2-spec-when-video-motion
Browse files Browse the repository at this point in the history
Allow specmap to have methods to branch into multiple sb3 opcodes
  • Loading branch information
mzgoddard committed Apr 6, 2018
2 parents 25f1c1a + 78199c4 commit 0591b47
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
29 changes: 22 additions & 7 deletions src/serialization/sb2.js
Expand Up @@ -469,6 +469,24 @@ const sb2import = function (json, runtime, optForceSprite, zip) {
}));
};

/**
* Given the sb2 block, inspect the specmap for a translation method or object.
* @param {!object} block a sb2 formatted block
* @return {object} specmap block to parse this opcode
*/
const specMapBlock = function (block) {
const opcode = block[0];
const mapped = opcode && specMap[opcode];
if (!mapped) {
log.warn(`Couldn't find SB2 block: ${opcode}`);
return null;
}
if (typeof mapped === 'function') {
return mapped(block);
}
return mapped;
};

/**
* Parse a single SB2 JSON-formatted block and its children.
* @param {!object} sb2block SB2 JSON-formatted block.
Expand All @@ -478,14 +496,11 @@ const sb2import = function (json, runtime, optForceSprite, zip) {
* @return {object} Scratch VM format block, or null if unsupported object.
*/
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions) {
// First item in block object is the old opcode (e.g., 'forward:').
const oldOpcode = sb2block[0];
// Convert the block using the specMap. See sb2specmap.js.
if (!oldOpcode || !specMap[oldOpcode]) {
log.warn('Couldn\'t find SB2 block: ', oldOpcode);
return null;
const blockMetadata = specMapBlock(sb2block);
if (!blockMetadata) {
return;
}
const blockMetadata = specMap[oldOpcode];
const oldOpcode = sb2block[0];
// If the block is from an extension, record it.
const dotIndex = blockMetadata.opcode.indexOf('.');
if (dotIndex >= 0) {
Expand Down
42 changes: 29 additions & 13 deletions src/serialization/sb2_specmap.js
Expand Up @@ -652,19 +652,35 @@ const specMap = {
}
]
},
'whenSensorGreaterThan': {
opcode: 'event_whengreaterthan',
argMap: [
{
type: 'field',
fieldName: 'WHENGREATERTHANMENU'
},
{
type: 'input',
inputOp: 'math_number',
inputName: 'VALUE'
}
]
'whenSensorGreaterThan': ([, sensor]) => {
if (sensor === 'video motion') {
return {
opcode: 'videoSensing.whenMotionGreaterThan',
argMap: [
// skip the first arg, since we converted to a video specific sensing block
{},
{
type: 'input',
inputOp: 'math_number',
inputName: 'REFERENCE'
}
]
};
}
return {
opcode: 'event_whengreaterthan',
argMap: [
{
type: 'field',
fieldName: 'WHENGREATERTHANMENU'
},
{
type: 'input',
inputOp: 'math_number',
inputName: 'VALUE'
}
]
};
},
'whenIReceive': {
opcode: 'event_whenbroadcastreceived',
Expand Down

0 comments on commit 0591b47

Please sign in to comment.