Broadcast message functionality (works with creating new messages, and switching back and forth between them as well). This commit includes a temporary workaround for the issue where the default broadcast message, 'message1' wasn't triggering a var_create event (filed in LLK/scratch-blocks#1258).

This commit is contained in:
Karishma Chadha 2017-11-21 16:48:48 -05:00
parent d5acdbe983
commit 0a15190b85
5 changed files with 52 additions and 4 deletions

View file

@ -56,14 +56,20 @@ class Scratch3EventBlocks {
}
broadcast (args, util) {
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
args.BROADCAST_OPTION.name);
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
util.startHats('event_whenbroadcastreceived', {
BROADCAST_OPTION: broadcastOption
});
}
broadcastAndWait (args, util) {
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
args.BROADCAST_OPTION.name);
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
// Have we run before, starting threads?
if (!util.stackFrame.startedThreads) {
// No - start hats for this broadcast.

View file

@ -365,7 +365,8 @@ class Blocks {
case 'field':
// Update block value
if (!block.fields[args.name]) return;
if (args.name === 'VARIABLE' || args.name === 'LIST') {
if (args.name === 'VARIABLE' || args.name === 'LIST' ||
args.name === 'BROADCAST_OPTION') {
// Get variable name using the id in args.value.
const variable = optRuntime.getEditingTarget().lookupVariableById(args.value);
if (variable) {

View file

@ -171,6 +171,19 @@ const execute = function (sequencer, thread) {
if (!fields.hasOwnProperty(fieldName)) continue;
if (fieldName === 'VARIABLE' || fieldName === 'LIST') {
argValues[fieldName] = fields[fieldName].id;
} else if (fieldName === 'BROADCAST_OPTION') {
// TODO KARISHMA (scratch-blocks #1258) this will change when we
// upstream blockly issue #1395 is fixed.
// we need both the id and the name here, because
// this object eventually gets passed to scratch3_event.broadcast
// which will create the message for the first time if one with the
// given id doesn't exist
// (e.g. default broadcast message, 'message1')
argValues[fieldName] = {
id: fields[fieldName].id,
name: fields[fieldName].value
};
} else {
argValues[fieldName] = fields[fieldName].value;
}

View file

@ -92,6 +92,23 @@ class Target extends EventEmitter {
return newVariable;
}
/**
* Look up a broadcast message object, and create it if one doesn't exist.
* @param {string} id Id of the variable.
* @param {string} name Name of the variable.
* @return {!Variable} Variable object.
*/
lookupOrCreateBroadcastMsg (id, name) {
debugger;
const broadcastMsg = this.lookupVariableById(id);
if (broadcastMsg) return broadcastMsg;
// No variable with this name exists - create it locally.
const newBroadcastMsg = new Variable(id, name,
Variable.BROADCAST_MESSAGE_TYPE, false);
this.variables[id] = newBroadcastMsg;
return newBroadcastMsg;
}
/**
* Look up a variable object.
* Search begins for local variables; then look for globals.
@ -134,7 +151,7 @@ class Target extends EventEmitter {
* dictionary of variables.
* @param {string} id Id of variable
* @param {string} name Name of variable.
* @param {string} type Type of variable, '' or 'list'
* @param {string} type Type of variable, '', 'broadcast_msg', or 'list'
*/
createVariable (id, name, type) {
if (!this.variables.hasOwnProperty(id)) {

View file

@ -25,6 +25,9 @@ class Variable {
case Variable.LIST_TYPE:
this.value = [];
break;
case Variable.BROADCAST_MESSAGE_TYPE:
this.value = this.name;
break;
default:
throw new Error(`Invalid variable type: ${this.type}`);
}
@ -51,6 +54,14 @@ class Variable {
static get LIST_TYPE () {
return 'list';
}
/**
* Type representation for list variables.
* @const {string}
*/
static get BROADCAST_MESSAGE_TYPE () {
return 'broadcast_msg';
}
}
module.exports = Variable;