mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Allow specmap to have methods to branch into multiple sb3 opcodes
This commit is contained in:
parent
41955913aa
commit
78199c41a5
2 changed files with 51 additions and 20 deletions
|
@ -447,6 +447,24 @@ const sb2import = function (json, runtime, optForceSprite) {
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Parse a single SB2 JSON-formatted block and its children.
|
||||||
* @param {!object} sb2block SB2 JSON-formatted block.
|
* @param {!object} sb2block SB2 JSON-formatted block.
|
||||||
|
@ -456,14 +474,11 @@ const sb2import = function (json, runtime, optForceSprite) {
|
||||||
* @return {object} Scratch VM format block, or null if unsupported object.
|
* @return {object} Scratch VM format block, or null if unsupported object.
|
||||||
*/
|
*/
|
||||||
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions) {
|
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions) {
|
||||||
// First item in block object is the old opcode (e.g., 'forward:').
|
const blockMetadata = specMapBlock(sb2block);
|
||||||
const oldOpcode = sb2block[0];
|
if (!blockMetadata) {
|
||||||
// Convert the block using the specMap. See sb2specmap.js.
|
return;
|
||||||
if (!oldOpcode || !specMap[oldOpcode]) {
|
|
||||||
log.warn('Couldn\'t find SB2 block: ', oldOpcode);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
const blockMetadata = specMap[oldOpcode];
|
const oldOpcode = sb2block[0];
|
||||||
// If the block is from an extension, record it.
|
// If the block is from an extension, record it.
|
||||||
const dotIndex = blockMetadata.opcode.indexOf('.');
|
const dotIndex = blockMetadata.opcode.indexOf('.');
|
||||||
if (dotIndex >= 0) {
|
if (dotIndex >= 0) {
|
||||||
|
|
|
@ -652,19 +652,35 @@ const specMap = {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
'whenSensorGreaterThan': {
|
'whenSensorGreaterThan': ([, sensor]) => {
|
||||||
opcode: 'event_whengreaterthan',
|
if (sensor === 'video motion') {
|
||||||
argMap: [
|
return {
|
||||||
{
|
opcode: 'videoSensing.whenMotionGreaterThan',
|
||||||
type: 'field',
|
argMap: [
|
||||||
fieldName: 'WHENGREATERTHANMENU'
|
// skip the first arg, since we converted to a video specific sensing block
|
||||||
},
|
{},
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
inputOp: 'math_number',
|
inputOp: 'math_number',
|
||||||
inputName: 'VALUE'
|
inputName: 'REFERENCE'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
opcode: 'event_whengreaterthan',
|
||||||
|
argMap: [
|
||||||
|
{
|
||||||
|
type: 'field',
|
||||||
|
fieldName: 'WHENGREATERTHANMENU'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
inputOp: 'math_number',
|
||||||
|
inputName: 'VALUE'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
},
|
},
|
||||||
'whenIReceive': {
|
'whenIReceive': {
|
||||||
opcode: 'event_whenbroadcastreceived',
|
opcode: 'event_whenbroadcastreceived',
|
||||||
|
|
Loading…
Reference in a new issue