Move data to blocks files

This commit is contained in:
DD 2017-11-15 17:53:43 -05:00
parent 757dccd565
commit 0958db2618
6 changed files with 62 additions and 29 deletions

View file

@ -242,6 +242,15 @@ class Scratch3LooksBlocks {
};
}
getMonitored () {
return {
size: {isSpriteSpecific: true},
costumeorder: {isSpriteSpecific: true},
backdroporder: {},
backdropname: {}
};
}
say (args, util) {
// @TODO in 2.0 calling say/think resets the right/left bias of the bubble
this._updateBubble(util.target, 'say', String(args.MESSAGE));

View file

@ -38,6 +38,14 @@ class Scratch3MotionBlocks {
};
}
getMonitored () {
return {
xposition: {isSpriteSpecific: true},
yposition: {isSpriteSpecific: true},
direction: {isSpriteSpecific: true}
};
}
moveSteps (args, util) {
const steps = Cast.toNumber(args.STEPS);
const radians = MathUtil.degToRad(90 - util.target.direction);

View file

@ -49,6 +49,16 @@ class Scratch3SensingBlocks {
};
}
getMonitored () {
return {
answer: {},
loudness: {},
timer: {},
of: {},
current: {}
};
}
_onAnswer (answer) {
this._answer = answer;
const questionObj = this._questionList.shift();

View file

@ -103,6 +103,12 @@ class Scratch3SoundBlocks {
};
}
getMonitored () {
return {
volume: {}
};
}
playSound (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) {

View file

@ -176,6 +176,13 @@ class Runtime extends EventEmitter {
*/
this._refreshTargets = false;
/**
* Map to look up all monitor block information by opcode.
* @type {object}
* @private
*/
this.monitorBlockInfo = {};
/**
* Ordered map of all monitors, which are MonitorReporter objects.
*/
@ -225,6 +232,9 @@ class Runtime extends EventEmitter {
// Register all given block packages.
this._registerBlockPackages();
// Populate monitorBlockInfo
this._registerMonitorInfo();
// Register and initialize "IO devices", containers for processing
// I/O related data.
/** @type {Object.<string, Object>} */
@ -451,6 +461,22 @@ class Runtime extends EventEmitter {
this.emit(Runtime.EXTENSION_ADDED, categoryInfo.blocks.concat(categoryInfo.menus));
}
/**
* Populate this.monitorBlockInfo
*/
_registerMonitorInfo () {
for (const packageName in defaultBlockPackages) {
if (defaultBlockPackages.hasOwnProperty(packageName)) {
// @todo pass a different runtime depending on package privilege?
const packageObject = new (defaultBlockPackages[packageName])(this);
// Collect monitored from package.
if (packageObject.getMonitored) {
this.monitorBlockInfo = Object.assign({}, this.monitorBlockInfo, packageObject.getMonitored());
}
}
}
}
/**
* Build the scratch-blocks JSON for a menu. Note that scratch-blocks treats menus as a special kind of block.
* @param {string} menuName - the name of the menu

View file

@ -575,44 +575,18 @@ class VirtualMachine extends EventEmitter {
* @param {!Blockly.Event} e Any Blockly event.
*/
monitorBlockListener (e) {
const tempMonitoredBlocks = [
'volume',
'tempo',
'answer',
'loudness',
'videoon',
'timer',
'of',
'current',
'username',
'xposition',
'yposition',
'direction',
'size',
'backdropname',
'costumeorder',
'backdroporder'
];
const tempMonitoredPerSpriteBlocks = [
'xposition',
'yposition',
'direction',
'size',
'costumeorder'
];
// Filter events by type, since monitor blocks only need to listen to these events.
// Monitor blocks shouldn't be destroyed when flyout blocks are deleted.
if (['create', 'change'].indexOf(e.type) !== -1) {
// TEMPORARY ----
let blockType = e.blockId.split('_');
blockType = blockType[blockType.length - 1];
if (tempMonitoredBlocks.indexOf(blockType) === -1) {
if (!this.runtime.monitorBlockInfo.hasOwnProperty(blockType)) {
return;
}
if (tempMonitoredPerSpriteBlocks.indexOf(blockType) !== -1) {
if (!this.runtime.monitorBlockInfo.hasOwnProperty(blockType) ||
this.runtime.monitorBlockInfo[blockType].isSpriteSpecific) {
e.isSpriteSpecific = true;
}
// -----
this.runtime.monitorBlocks.blocklyListen(e, this.runtime);
}
}