2016-04-18 17:20:30 -04:00
|
|
|
var EventEmitter = require('events');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var Runtime = require('./engine/runtime');
|
2016-08-31 13:56:05 -04:00
|
|
|
var sb2import = require('./import/sb2import');
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles connections between blocks, stage, and extensions.
|
|
|
|
*
|
|
|
|
* @author Andrew Sliwinski <ascii@media.mit.edu>
|
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
var VirtualMachine = function () {
|
2016-04-18 17:20:30 -04:00
|
|
|
var instance = this;
|
|
|
|
// Bind event emitter and runtime to VM instance
|
|
|
|
EventEmitter.call(instance);
|
|
|
|
/**
|
2016-08-31 12:08:54 -04:00
|
|
|
* VM runtime, to store blocks, I/O devices, sprites/targets, etc.
|
|
|
|
* @type {!Runtime}
|
2016-04-18 17:20:30 -04:00
|
|
|
*/
|
2016-09-20 15:07:05 -04:00
|
|
|
instance.runtime = new Runtime();
|
2016-08-31 12:08:54 -04:00
|
|
|
/**
|
|
|
|
* The "currently editing"/selected target ID for the VM.
|
|
|
|
* Block events from any Blockly workspace are routed to this target.
|
|
|
|
* @type {!string}
|
|
|
|
*/
|
|
|
|
instance.editingTarget = null;
|
2016-06-21 15:30:27 -04:00
|
|
|
// Runtime emits are passed along as VM emits.
|
2016-11-23 15:43:05 -05:00
|
|
|
instance.runtime.on(Runtime.SCRIPT_GLOW_ON, function (glowData) {
|
|
|
|
instance.emit(Runtime.SCRIPT_GLOW_ON, glowData);
|
2016-06-21 15:30:27 -04:00
|
|
|
});
|
2016-11-23 15:43:05 -05:00
|
|
|
instance.runtime.on(Runtime.SCRIPT_GLOW_OFF, function (glowData) {
|
|
|
|
instance.emit(Runtime.SCRIPT_GLOW_OFF, glowData);
|
2016-06-21 15:30:27 -04:00
|
|
|
});
|
2016-11-23 15:43:05 -05:00
|
|
|
instance.runtime.on(Runtime.BLOCK_GLOW_ON, function (glowData) {
|
|
|
|
instance.emit(Runtime.BLOCK_GLOW_ON, glowData);
|
2016-06-21 15:30:27 -04:00
|
|
|
});
|
2016-11-23 15:43:05 -05:00
|
|
|
instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (glowData) {
|
|
|
|
instance.emit(Runtime.BLOCK_GLOW_OFF, glowData);
|
2016-06-21 15:30:27 -04:00
|
|
|
});
|
2016-11-23 15:47:49 -05:00
|
|
|
instance.runtime.on(Runtime.PROJECT_RUN_START, function () {
|
|
|
|
instance.emit(Runtime.PROJECT_RUN_START);
|
|
|
|
});
|
|
|
|
instance.runtime.on(Runtime.PROJECT_RUN_STOP, function () {
|
|
|
|
instance.emit(Runtime.PROJECT_RUN_STOP);
|
2016-07-07 19:42:38 -04:00
|
|
|
});
|
2016-11-23 15:43:05 -05:00
|
|
|
instance.runtime.on(Runtime.VISUAL_REPORT, function (visualReport) {
|
|
|
|
instance.emit(Runtime.VISUAL_REPORT, visualReport);
|
|
|
|
});
|
|
|
|
instance.runtime.on(Runtime.SPRITE_INFO_REPORT, function (spriteInfo) {
|
|
|
|
instance.emit(Runtime.SPRITE_INFO_REPORT, spriteInfo);
|
2016-10-26 13:27:12 -04:00
|
|
|
});
|
2016-09-13 17:49:45 -04:00
|
|
|
|
|
|
|
this.blockListener = this.blockListener.bind(this);
|
2016-10-13 17:15:49 -04:00
|
|
|
this.flyoutBlockListener = this.flyoutBlockListener.bind(this);
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-06-21 15:30:27 -04:00
|
|
|
/**
|
|
|
|
* Inherit from EventEmitter
|
|
|
|
*/
|
|
|
|
util.inherits(VirtualMachine, EventEmitter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start running the VM - do this before anything else.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.start = function () {
|
|
|
|
this.runtime.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Green flag" handler - start all threads starting with a green flag.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.greenFlag = function () {
|
|
|
|
this.runtime.greenFlag();
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Set whether the VM is in "turbo mode."
|
|
|
|
* When true, loops don't yield to redraw.
|
|
|
|
* @param {Boolean} turboModeOn Whether turbo mode should be set.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.setTurboMode = function (turboModeOn) {
|
|
|
|
this.runtime.turboMode = !!turboModeOn;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether the VM is in 2.0 "compatibility mode."
|
|
|
|
* When true, ticks go at 2.0 speed (30 TPS).
|
|
|
|
* @param {Boolean} compatibilityModeOn Whether compatibility mode is set.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.setCompatibilityMode = function (compatibilityModeOn) {
|
|
|
|
this.runtime.setCompatibilityMode(!!compatibilityModeOn);
|
|
|
|
};
|
|
|
|
|
2016-06-21 15:30:27 -04:00
|
|
|
/**
|
|
|
|
* Stop all threads and running activities.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.stopAll = function () {
|
|
|
|
this.runtime.stopAll();
|
|
|
|
};
|
|
|
|
|
2016-10-17 13:43:38 -04:00
|
|
|
/**
|
|
|
|
* Clear out current running project data.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.clear = function () {
|
|
|
|
this.runtime.dispose();
|
|
|
|
this.editingTarget = null;
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
};
|
|
|
|
|
2016-06-21 15:30:27 -04:00
|
|
|
/**
|
|
|
|
* Get data for playground. Data comes back in an emitted event.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.getPlaygroundData = function () {
|
2016-09-21 16:31:23 -04:00
|
|
|
var instance = this;
|
|
|
|
// Only send back thread data for the current editingTarget.
|
2016-10-23 12:41:45 -04:00
|
|
|
var threadData = this.runtime.threads.filter(function (thread) {
|
2016-10-23 17:55:31 -04:00
|
|
|
return thread.target === instance.editingTarget;
|
2016-09-21 16:31:23 -04:00
|
|
|
});
|
|
|
|
// Remove the target key, since it's a circular reference.
|
2016-10-23 12:41:45 -04:00
|
|
|
var filteredThreadData = JSON.stringify(threadData, function (key, value) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (key === 'target') return;
|
2016-09-21 16:31:23 -04:00
|
|
|
return value;
|
|
|
|
}, 2);
|
2016-06-21 15:30:27 -04:00
|
|
|
this.emit('playgroundData', {
|
2016-08-31 12:08:54 -04:00
|
|
|
blocks: this.editingTarget.blocks,
|
2016-09-21 16:31:23 -04:00
|
|
|
threads: filteredThreadData
|
2016-06-21 15:30:27 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
/**
|
|
|
|
* Post I/O data to the virtual devices.
|
|
|
|
* @param {?string} device Name of virtual I/O device.
|
|
|
|
* @param {Object} data Any data object to post to the I/O device.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.postIOData = function (device, data) {
|
|
|
|
if (this.runtime.ioDevices[device]) {
|
|
|
|
this.runtime.ioDevices[device].postData(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-31 12:28:09 -04:00
|
|
|
/**
|
|
|
|
* Load a project from a Scratch 2.0 JSON representation.
|
2016-09-12 11:05:16 -04:00
|
|
|
* @param {?string} json JSON string representing the project.
|
2016-08-31 12:28:09 -04:00
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.loadProject = function (json) {
|
2016-10-17 13:43:38 -04:00
|
|
|
this.clear();
|
2016-08-31 13:56:05 -04:00
|
|
|
// @todo: Handle other formats, e.g., Scratch 1.4, Scratch 3.0.
|
|
|
|
sb2import(json, this.runtime);
|
2016-10-20 11:44:01 -04:00
|
|
|
// Select the first target for editing, e.g., the first sprite.
|
|
|
|
this.editingTarget = this.runtime.targets[1];
|
2016-08-31 12:28:09 -04:00
|
|
|
// Update the VM user's knowledge of targets and blocks on the workspace.
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
this.emitWorkspaceUpdate();
|
2016-09-08 09:40:53 -04:00
|
|
|
this.runtime.setEditingTarget(this.editingTarget);
|
2016-08-31 12:28:09 -04:00
|
|
|
};
|
|
|
|
|
2016-10-20 11:44:01 -04:00
|
|
|
/**
|
|
|
|
* Add a single sprite from the "Sprite2" (i.e., SB2 sprite) format.
|
|
|
|
* @param {?string} json JSON string representing the sprite.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.addSprite2 = function (json) {
|
|
|
|
// Select new sprite.
|
|
|
|
this.editingTarget = sb2import(json, this.runtime, true);
|
|
|
|
// Update the VM user's knowledge of targets and blocks on the workspace.
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
this.emitWorkspaceUpdate();
|
|
|
|
this.runtime.setEditingTarget(this.editingTarget);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a costume to the current editing target.
|
|
|
|
* @param {!Object} costumeObject Object representing the costume.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.addCostume = function (costumeObject) {
|
|
|
|
this.editingTarget.sprite.costumes.push(costumeObject);
|
|
|
|
// Switch to the costume.
|
|
|
|
this.editingTarget.setCostume(
|
|
|
|
this.editingTarget.sprite.costumes.length - 1
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a backdrop to the stage.
|
|
|
|
* @param {!Object} backdropObject Object representing the backdrop.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.addBackdrop = function (backdropObject) {
|
|
|
|
var stage = this.runtime.getTargetForStage();
|
|
|
|
stage.sprite.costumes.push(backdropObject);
|
|
|
|
// Switch to the backdrop.
|
|
|
|
stage.setCostume(stage.sprite.costumes.length - 1);
|
|
|
|
};
|
|
|
|
|
2016-10-26 13:27:24 -04:00
|
|
|
/**
|
|
|
|
* Rename a sprite.
|
|
|
|
* @param {string} targetId ID of a target whose sprite to rename.
|
|
|
|
* @param {string} newName New name of the sprite.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.renameSprite = function (targetId, newName) {
|
|
|
|
var target = this.runtime.getTargetById(targetId);
|
|
|
|
if (target) {
|
|
|
|
if (!target.isSprite()) {
|
|
|
|
throw new Error('Cannot rename non-sprite targets.');
|
|
|
|
}
|
|
|
|
var sprite = target.sprite;
|
|
|
|
if (!sprite) {
|
|
|
|
throw new Error('No sprite associated with this target.');
|
|
|
|
}
|
|
|
|
sprite.name = newName;
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
} else {
|
|
|
|
throw new Error('No target with the provided id.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a sprite and all its clones.
|
|
|
|
* @param {string} targetId ID of a target whose sprite to delete.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.deleteSprite = function (targetId) {
|
|
|
|
var target = this.runtime.getTargetById(targetId);
|
|
|
|
if (target) {
|
|
|
|
if (!target.isSprite()) {
|
|
|
|
throw new Error('Cannot delete non-sprite targets.');
|
|
|
|
}
|
|
|
|
var sprite = target.sprite;
|
|
|
|
if (!sprite) {
|
|
|
|
throw new Error('No sprite associated with this target.');
|
|
|
|
}
|
|
|
|
var currentEditingTarget = this.editingTarget;
|
|
|
|
for (var i = 0; i < sprite.clones.length; i++) {
|
|
|
|
var clone = sprite.clones[i];
|
|
|
|
this.runtime.stopForTarget(sprite.clones[i]);
|
|
|
|
this.runtime.disposeTarget(sprite.clones[i]);
|
|
|
|
// Ensure editing target is switched if we are deleting it.
|
|
|
|
if (clone === currentEditingTarget) {
|
|
|
|
this.setEditingTarget(this.runtime.targets[0].id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sprite object should be deleted by GC.
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
} else {
|
|
|
|
throw new Error('No target with the provided id.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-20 15:07:05 -04:00
|
|
|
/**
|
|
|
|
* Set the renderer for the VM/runtime
|
|
|
|
* @param {!RenderWebGL} renderer The renderer to attach
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.attachRenderer = function (renderer) {
|
|
|
|
this.runtime.attachRenderer(renderer);
|
|
|
|
};
|
|
|
|
|
2016-09-13 17:49:45 -04:00
|
|
|
/**
|
|
|
|
* Handle a Blockly event for the current editing target.
|
|
|
|
* @param {!Blockly.Event} e Any Blockly event.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.blockListener = function (e) {
|
|
|
|
if (this.editingTarget) {
|
2016-10-13 17:15:49 -04:00
|
|
|
this.editingTarget.blocks.blocklyListen(e, this.runtime);
|
2016-09-13 17:49:45 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-13 17:15:49 -04:00
|
|
|
/**
|
|
|
|
* Handle a Blockly event for the flyout.
|
|
|
|
* @param {!Blockly.Event} e Any Blockly event.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.flyoutBlockListener = function (e) {
|
|
|
|
this.runtime.flyoutBlocks.blocklyListen(e, this.runtime);
|
|
|
|
};
|
|
|
|
|
2016-08-31 12:28:09 -04:00
|
|
|
/**
|
|
|
|
* Set an editing target. An editor UI can use this function to switch
|
|
|
|
* between editing different targets, sprites, etc.
|
|
|
|
* After switching the editing target, the VM may emit updates
|
|
|
|
* to the list of targets and any attached workspace blocks
|
|
|
|
* (see `emitTargetsUpdate` and `emitWorkspaceUpdate`).
|
|
|
|
* @param {string} targetId Id of target to set as editing.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.setEditingTarget = function (targetId) {
|
2016-09-02 09:47:27 -04:00
|
|
|
// Has the target id changed? If not, exit.
|
2016-10-23 17:55:31 -04:00
|
|
|
if (targetId === this.editingTarget.id) {
|
2016-08-31 12:28:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var target = this.runtime.getTargetById(targetId);
|
|
|
|
if (target) {
|
|
|
|
this.editingTarget = target;
|
|
|
|
// Emit appropriate UI updates.
|
|
|
|
this.emitTargetsUpdate();
|
|
|
|
this.emitWorkspaceUpdate();
|
2016-09-08 09:40:53 -04:00
|
|
|
this.runtime.setEditingTarget(target);
|
2016-08-31 12:28:09 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit metadata about available targets.
|
|
|
|
* An editor UI could use this to display a list of targets and show
|
|
|
|
* the currently editing one.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.emitTargetsUpdate = function () {
|
|
|
|
this.emit('targetsUpdate', {
|
|
|
|
// [[target id, human readable target name], ...].
|
2016-09-15 19:37:12 -04:00
|
|
|
targetList: this.runtime.targets.filter(function (target) {
|
|
|
|
// Don't report clones.
|
|
|
|
return !target.hasOwnProperty('isOriginal') || target.isOriginal;
|
2016-10-23 12:41:45 -04:00
|
|
|
}).map(function (target) {
|
2016-12-05 18:02:51 -05:00
|
|
|
return target.toJSON();
|
2016-08-31 12:28:09 -04:00
|
|
|
}),
|
|
|
|
// Currently editing target id.
|
2016-10-17 13:43:38 -04:00
|
|
|
editingTarget: this.editingTarget ? this.editingTarget.id : null
|
2016-08-31 12:28:09 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit an Blockly/scratch-blocks compatible XML representation
|
|
|
|
* of the current editing target's blocks.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.emitWorkspaceUpdate = function () {
|
|
|
|
this.emit('workspaceUpdate', {
|
2016-10-24 11:02:19 -04:00
|
|
|
xml: this.editingTarget.blocks.toXML()
|
2016-08-31 12:28:09 -04:00
|
|
|
});
|
|
|
|
};
|
2016-09-20 10:29:47 -04:00
|
|
|
|
2016-10-26 13:27:12 -04:00
|
|
|
/**
|
|
|
|
* Post/edit sprite info for the current editing target.
|
|
|
|
* @param {object} data An object with sprite info data to set.
|
|
|
|
*/
|
|
|
|
VirtualMachine.prototype.postSpriteInfo = function (data) {
|
|
|
|
this.editingTarget.postSpriteInfo(data);
|
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = VirtualMachine;
|