From 78199c41a52c629e2f0f1940fcb926e2117e4fa2 Mon Sep 17 00:00:00 2001 From: Corey Frang Date: Thu, 5 Apr 2018 15:39:16 -0400 Subject: [PATCH] Allow specmap to have methods to branch into multiple sb3 opcodes --- src/serialization/sb2.js | 29 ++++++++++++++++------ src/serialization/sb2_specmap.js | 42 ++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 5d41f2ccdb..47937f9b19 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -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. * @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. */ 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) { diff --git a/src/serialization/sb2_specmap.js b/src/serialization/sb2_specmap.js index e302b68c49..93a26a8a66 100644 --- a/src/serialization/sb2_specmap.js +++ b/src/serialization/sb2_specmap.js @@ -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',