SB2 Import functionality for broadcast blocks with pluggable inputs.

This commit is contained in:
Karishma Chadha 2017-12-13 17:15:18 -05:00
parent c0e4ae455c
commit 154987fbb0
2 changed files with 38 additions and 9 deletions

View file

@ -170,12 +170,17 @@ const generateVariableIdGetter = (function () {
const globalBroadcastMsgStateGenerator = (function () { const globalBroadcastMsgStateGenerator = (function () {
let broadcastMsgNameMap = {}; let broadcastMsgNameMap = {};
const emptyStringName = uid();
return function (topLevel) { return function (topLevel) {
if (topLevel) broadcastMsgNameMap = {}; if (topLevel) broadcastMsgNameMap = {};
return { return {
broadcastMsgMapUpdater: function (name) { broadcastMsgMapUpdater: function (name) {
name = name.toLowerCase();
if (name === '') {
name = emptyStringName;
}
broadcastMsgNameMap[name] = `broadcastMsgId-${name}`; broadcastMsgNameMap[name] = `broadcastMsgId-${name}`;
return broadcastMsgNameMap[name]; return {name: name, id: broadcastMsgNameMap[name]};
}, },
globalBroadcastMsgs: broadcastMsgNameMap globalBroadcastMsgs: broadcastMsgNameMap
}; };
@ -494,15 +499,33 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
if (shadowObscured) { if (shadowObscured) {
fieldValue = '#990000'; fieldValue = '#990000';
} }
} else if (expectedArg.inputOp === 'event_broadcast_menu') {
fieldName = 'BROADCAST_OPTION';
if (shadowObscured) {
fieldValue = '';
}
} else if (shadowObscured) { } else if (shadowObscured) {
// Filled drop-down menu. // Filled drop-down menu.
fieldValue = ''; fieldValue = '';
} }
const fields = {}; const fields = {};
fields[fieldName] = { fields[fieldName] = {
name: fieldName, name: fieldName
value: fieldValue
}; };
// event_broadcast_menus have some extra properties to add to the
// field and a different value than the rest
if (expectedArg.inputOp === 'event_broadcast_menu') {
if (!shadowObscured) {
const broadcastInfo = addBroadcastMsg(fieldValue);
fields[fieldName].id = broadcastInfo.id;
// Re-assign the value, because the name could have changed
// if the scratch2 message was an empty string
fields[fieldName].value = broadcastInfo.name;
}
fields[fieldName].variableType = expectedArg.variableType;
} else {
fields[fieldName].value = fieldValue;
}
activeBlock.children.push({ activeBlock.children.push({
id: inputUid, id: inputUid,
opcode: expectedArg.inputOp, opcode: expectedArg.inputOp,
@ -530,8 +553,12 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg); activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg);
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') { } else if (expectedArg.fieldName === 'BROADCAST_OPTION') {
// add the name in this field to the broadcast msg name map // add the name in this field to the broadcast msg name map
const broadcastId = addBroadcastMsg(providedArg); const broadcastInfo = addBroadcastMsg(providedArg);
activeBlock.fields[expectedArg.fieldName].id = broadcastId; activeBlock.fields[expectedArg.fieldName].id = broadcastInfo.id;
// Need to reassign field value using the sb3 name from broadcastInfo
// because the sb2 message name (e.g. providedArg) could have changed
// if the original (providedArg) was an empty string
activeBlock.fields[expectedArg.fieldName].value = broadcastInfo.name;
} }
const varType = expectedArg.variableType; const varType = expectedArg.variableType;
if (typeof varType === 'string') { if (typeof varType === 'string') {

View file

@ -665,8 +665,9 @@ const specMap = {
opcode: 'event_broadcast', opcode: 'event_broadcast',
argMap: [ argMap: [
{ {
type: 'field', type: 'input',
fieldName: 'BROADCAST_OPTION', inputOp: 'event_broadcast_menu',
inputName: 'BROADCAST_INPUT',
variableType: Variable.BROADCAST_MESSAGE_TYPE variableType: Variable.BROADCAST_MESSAGE_TYPE
} }
] ]
@ -675,8 +676,9 @@ const specMap = {
opcode: 'event_broadcastandwait', opcode: 'event_broadcastandwait',
argMap: [ argMap: [
{ {
type: 'field', type: 'input',
fieldName: 'BROADCAST_OPTION', inputOp: 'event_broadcast_menu',
inputName: 'BROADCAST_INPUT',
variableType: Variable.BROADCAST_MESSAGE_TYPE variableType: Variable.BROADCAST_MESSAGE_TYPE
} }
] ]