2017-04-17 15:10:04 -04:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const Sequencer = require('./sequencer');
|
|
|
|
const Blocks = require('./blocks');
|
|
|
|
const Thread = require('./thread');
|
|
|
|
const util = require('util');
|
2016-04-18 18:03:18 -04:00
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
// Virtual I/O devices.
|
2017-04-17 15:10:04 -04:00
|
|
|
const Clock = require('../io/clock');
|
|
|
|
const Keyboard = require('../io/keyboard');
|
|
|
|
const Mouse = require('../io/mouse');
|
2016-08-15 21:37:36 -04:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const defaultBlockPackages = {
|
2016-10-24 11:02:19 -04:00
|
|
|
scratch3_control: require('../blocks/scratch3_control'),
|
|
|
|
scratch3_event: require('../blocks/scratch3_event'),
|
|
|
|
scratch3_looks: require('../blocks/scratch3_looks'),
|
|
|
|
scratch3_motion: require('../blocks/scratch3_motion'),
|
|
|
|
scratch3_operators: require('../blocks/scratch3_operators'),
|
2017-01-19 14:26:38 -05:00
|
|
|
scratch3_pen: require('../blocks/scratch3_pen'),
|
2016-12-21 15:18:38 -05:00
|
|
|
scratch3_sound: require('../blocks/scratch3_sound'),
|
2016-10-24 11:02:19 -04:00
|
|
|
scratch3_sensing: require('../blocks/scratch3_sensing'),
|
|
|
|
scratch3_data: require('../blocks/scratch3_data'),
|
|
|
|
scratch3_procedures: require('../blocks/scratch3_procedures')
|
2016-05-02 12:20:27 -04:00
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
/**
|
2016-08-11 11:11:27 -04:00
|
|
|
* Manages targets, scripts, and the sequencer.
|
2016-12-23 10:39:19 -05:00
|
|
|
* @constructor
|
2016-04-18 17:20:30 -04:00
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const Runtime = function () {
|
2016-04-18 18:03:18 -04:00
|
|
|
// Bind event emitter
|
2016-04-26 09:49:52 -04:00
|
|
|
EventEmitter.call(this);
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-04-26 15:51:14 -04:00
|
|
|
/**
|
2016-06-29 13:48:30 -04:00
|
|
|
* Target management and storage.
|
2016-08-31 12:03:41 -04:00
|
|
|
* @type {Array.<!Target>}
|
2016-04-26 15:51:14 -04:00
|
|
|
*/
|
2016-08-31 12:03:41 -04:00
|
|
|
this.targets = [];
|
2016-04-26 14:20:44 -04:00
|
|
|
|
2016-04-26 15:51:14 -04:00
|
|
|
/**
|
|
|
|
* A list of threads that are currently running in the VM.
|
|
|
|
* Threads are added when execution starts and pruned when execution ends.
|
|
|
|
* @type {Array.<Thread>}
|
|
|
|
*/
|
|
|
|
this.threads = [];
|
|
|
|
|
2016-04-26 14:20:44 -04:00
|
|
|
/** @type {!Sequencer} */
|
2016-04-26 16:50:49 -04:00
|
|
|
this.sequencer = new Sequencer(this);
|
2016-05-02 12:20:27 -04:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Storage container for flyout blocks.
|
|
|
|
* These will execute on `_editingTarget.`
|
|
|
|
* @type {!Blocks}
|
|
|
|
*/
|
2016-10-13 17:15:49 -04:00
|
|
|
this.flyoutBlocks = new Blocks();
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Currently known editing target for the VM.
|
|
|
|
* @type {?Target}
|
|
|
|
*/
|
|
|
|
this._editingTarget = null;
|
|
|
|
|
2016-05-02 12:20:27 -04:00
|
|
|
/**
|
|
|
|
* Map to look up a block primitive's implementation function by its opcode.
|
|
|
|
* This is a two-step lookup: package name first, then primitive name.
|
|
|
|
* @type {Object.<string, Function>}
|
|
|
|
*/
|
|
|
|
this._primitives = {};
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map to look up hat blocks' metadata.
|
|
|
|
* Keys are opcode for hat, values are metadata objects.
|
|
|
|
* @type {Object.<string, Object>}
|
|
|
|
*/
|
2016-08-23 18:12:32 -04:00
|
|
|
this._hats = {};
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently known values for edge-activated hats.
|
|
|
|
* Keys are block ID for the hat; values are the currently known values.
|
|
|
|
* @type {Object.<string, *>}
|
|
|
|
*/
|
2016-08-29 10:26:26 -04:00
|
|
|
this._edgeActivatedHatValues = {};
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of script block IDs that were glowing during the previous frame.
|
|
|
|
* @type {!Array.<!string>}
|
|
|
|
*/
|
|
|
|
this._scriptGlowsPreviousFrame = [];
|
|
|
|
|
2016-11-23 15:47:49 -05:00
|
|
|
/**
|
|
|
|
* Number of threads running during the previous frame
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._threadCount = 0;
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Currently known number of clones, used to enforce clone limit.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._cloneCounter = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the project is in "turbo mode."
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
this.turboMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the project is in "compatibility mode" (30 TPS).
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
this.compatibilityMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the current runtime stepping interval, set
|
|
|
|
* by a `setInterval`.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
|
|
|
this._steppingInterval = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current length of a step.
|
|
|
|
* Changes as mode switches, and used by the sequencer to calculate
|
|
|
|
* WORK_TIME.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
|
|
|
this.currentStepTime = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether any primitive has requested a redraw.
|
|
|
|
* Affects whether `Sequencer.stepThreads` will yield
|
|
|
|
* after stepping each thread.
|
|
|
|
* Reset on every frame.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.redrawRequested = false;
|
|
|
|
|
|
|
|
// Register all given block packages.
|
2016-05-02 12:20:27 -04:00
|
|
|
this._registerBlockPackages();
|
2016-08-15 21:37:36 -04:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
// Register and initialize "IO devices", containers for processing
|
|
|
|
// I/O related data.
|
|
|
|
/** @type {Object.<string, Object>} */
|
2016-08-15 21:37:36 -04:00
|
|
|
this.ioDevices = {
|
2016-10-24 11:02:19 -04:00
|
|
|
clock: new Clock(),
|
|
|
|
keyboard: new Keyboard(this),
|
|
|
|
mouse: new Mouse(this)
|
2016-08-15 21:37:36 -04:00
|
|
|
};
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Inherit from EventEmitter
|
|
|
|
*/
|
|
|
|
util.inherits(Runtime, EventEmitter);
|
|
|
|
|
2016-10-04 18:19:52 -04:00
|
|
|
/**
|
|
|
|
* Width of the stage, in pixels.
|
|
|
|
* @const {number}
|
|
|
|
*/
|
|
|
|
Runtime.STAGE_WIDTH = 480;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Height of the stage, in pixels.
|
|
|
|
* @const {number}
|
|
|
|
*/
|
|
|
|
Runtime.STAGE_HEIGHT = 360;
|
|
|
|
|
2016-05-02 13:09:38 -04:00
|
|
|
/**
|
2016-09-08 09:40:53 -04:00
|
|
|
* Event name for glowing a script.
|
2016-05-02 13:09:38 -04:00
|
|
|
* @const {string}
|
|
|
|
*/
|
2016-11-23 15:43:05 -05:00
|
|
|
Runtime.SCRIPT_GLOW_ON = 'SCRIPT_GLOW_ON';
|
2016-05-02 13:09:38 -04:00
|
|
|
|
|
|
|
/**
|
2016-09-08 09:40:53 -04:00
|
|
|
* Event name for unglowing a script.
|
2016-05-02 13:09:38 -04:00
|
|
|
* @const {string}
|
|
|
|
*/
|
2016-11-23 15:43:05 -05:00
|
|
|
Runtime.SCRIPT_GLOW_OFF = 'SCRIPT_GLOW_OFF';
|
2016-05-02 13:09:38 -04:00
|
|
|
|
|
|
|
/**
|
2016-09-08 09:40:53 -04:00
|
|
|
* Event name for glowing a block.
|
2016-05-02 13:09:38 -04:00
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
Runtime.BLOCK_GLOW_ON = 'BLOCK_GLOW_ON';
|
|
|
|
|
|
|
|
/**
|
2016-09-08 09:40:53 -04:00
|
|
|
* Event name for unglowing a block.
|
2016-05-02 13:09:38 -04:00
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
Runtime.BLOCK_GLOW_OFF = 'BLOCK_GLOW_OFF';
|
|
|
|
|
2016-11-23 15:47:49 -05:00
|
|
|
/**
|
|
|
|
* Event name for glowing the green flag
|
|
|
|
* @const {string}
|
|
|
|
*/
|
2016-11-24 10:36:30 -05:00
|
|
|
Runtime.PROJECT_RUN_START = 'PROJECT_RUN_START';
|
2016-11-23 15:47:49 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event name for unglowing the green flag
|
|
|
|
* @const {string}
|
|
|
|
*/
|
2016-11-24 10:36:30 -05:00
|
|
|
Runtime.PROJECT_RUN_STOP = 'PROJECT_RUN_STOP';
|
2016-11-23 15:47:49 -05:00
|
|
|
|
2016-07-07 19:42:38 -04:00
|
|
|
/**
|
|
|
|
* Event name for visual value report.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
Runtime.VISUAL_REPORT = 'VISUAL_REPORT';
|
|
|
|
|
2016-10-26 13:27:12 -04:00
|
|
|
/**
|
|
|
|
* Event name for sprite info report.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
Runtime.SPRITE_INFO_REPORT = 'SPRITE_INFO_REPORT';
|
|
|
|
|
2016-04-18 18:03:18 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* How rapidly we try to step threads by default, in ms.
|
2016-04-18 18:03:18 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Runtime.THREAD_STEP_INTERVAL = 1000 / 60;
|
2016-04-18 18:03:18 -04:00
|
|
|
|
2016-04-26 16:50:49 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* In compatibility mode, how rapidly we try to step threads, in ms.
|
2016-04-26 16:50:49 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Runtime.THREAD_STEP_INTERVAL_COMPATIBILITY = 1000 / 30;
|
2016-04-26 16:50:49 -04:00
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
|
|
|
* How many clones can be created at a time.
|
|
|
|
* @const {number}
|
|
|
|
*/
|
|
|
|
Runtime.MAX_CLONES = 300;
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2016-04-26 16:50:49 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-05-02 12:20:27 -04:00
|
|
|
/**
|
|
|
|
* Register default block packages with this runtime.
|
|
|
|
* @todo Prefix opcodes with package name.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Runtime.prototype._registerBlockPackages = function () {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const packageName in defaultBlockPackages) {
|
2016-05-02 12:20:27 -04:00
|
|
|
if (defaultBlockPackages.hasOwnProperty(packageName)) {
|
2016-05-02 14:54:32 -04:00
|
|
|
// @todo pass a different runtime depending on package privilege?
|
2017-04-17 15:10:04 -04:00
|
|
|
const packageObject = new (defaultBlockPackages[packageName])(this);
|
2016-08-23 18:12:32 -04:00
|
|
|
// Collect primitives from package.
|
|
|
|
if (packageObject.getPrimitives) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const packagePrimitives = packageObject.getPrimitives();
|
|
|
|
for (const op in packagePrimitives) {
|
2016-08-23 18:12:32 -04:00
|
|
|
if (packagePrimitives.hasOwnProperty(op)) {
|
|
|
|
this._primitives[op] =
|
|
|
|
packagePrimitives[op].bind(packageObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Collect hat metadata from package.
|
|
|
|
if (packageObject.getHats) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const packageHats = packageObject.getHats();
|
|
|
|
for (const hatName in packageHats) {
|
2016-08-23 18:12:32 -04:00
|
|
|
if (packageHats.hasOwnProperty(hatName)) {
|
|
|
|
this._hats[hatName] = packageHats[hatName];
|
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the function associated with the given opcode.
|
|
|
|
* @param {!string} opcode The opcode to look up.
|
|
|
|
* @return {Function} The function which implements the opcode.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.getOpcodeFunction = function (opcode) {
|
|
|
|
return this._primitives[opcode];
|
|
|
|
};
|
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
/**
|
|
|
|
* Return whether an opcode represents a hat block.
|
|
|
|
* @param {!string} opcode The opcode to look up.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {boolean} True if the op is known to be a hat.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
|
|
|
Runtime.prototype.getIsHat = function (opcode) {
|
2016-08-29 10:03:21 -04:00
|
|
|
return this._hats.hasOwnProperty(opcode);
|
2016-08-23 18:12:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-08-29 10:26:26 -04:00
|
|
|
* Return whether an opcode represents an edge-activated hat block.
|
2016-08-23 18:12:32 -04:00
|
|
|
* @param {!string} opcode The opcode to look up.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {boolean} True if the op is known to be a edge-activated hat.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
2016-08-29 10:26:26 -04:00
|
|
|
Runtime.prototype.getIsEdgeActivatedHat = function (opcode) {
|
2016-08-29 10:03:21 -04:00
|
|
|
return this._hats.hasOwnProperty(opcode) &&
|
2016-08-29 10:26:26 -04:00
|
|
|
this._hats[opcode].edgeActivated;
|
2016-08-23 18:12:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-08-29 10:26:26 -04:00
|
|
|
* Update an edge-activated hat block value.
|
2016-08-23 18:12:32 -04:00
|
|
|
* @param {!string} blockId ID of hat to store value for.
|
2016-08-29 10:26:26 -04:00
|
|
|
* @param {*} newValue Value to store for edge-activated hat.
|
|
|
|
* @return {*} The old value for the edge-activated hat.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
2016-08-29 10:26:26 -04:00
|
|
|
Runtime.prototype.updateEdgeActivatedValue = function (blockId, newValue) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const oldValue = this._edgeActivatedHatValues[blockId];
|
2016-08-29 10:26:26 -04:00
|
|
|
this._edgeActivatedHatValues[blockId] = newValue;
|
2016-08-23 18:12:32 -04:00
|
|
|
return oldValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-08-29 10:26:26 -04:00
|
|
|
* Clear all edge-activaed hat values.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
2016-08-29 10:26:26 -04:00
|
|
|
Runtime.prototype.clearEdgeActivatedValues = function () {
|
|
|
|
this._edgeActivatedHatValues = {};
|
2016-08-23 18:12:32 -04:00
|
|
|
};
|
|
|
|
|
2017-01-27 13:44:48 -05:00
|
|
|
/**
|
|
|
|
* Attach the audio engine
|
|
|
|
* @param {!AudioEngine} audioEngine The audio engine to attach
|
|
|
|
*/
|
|
|
|
Runtime.prototype.attachAudioEngine = function (audioEngine) {
|
|
|
|
this.audioEngine = audioEngine;
|
|
|
|
};
|
|
|
|
|
2016-09-20 15:07:05 -04:00
|
|
|
/**
|
|
|
|
* Attach the renderer
|
|
|
|
* @param {!RenderWebGL} renderer The renderer to attach
|
|
|
|
*/
|
|
|
|
Runtime.prototype.attachRenderer = function (renderer) {
|
|
|
|
this.renderer = renderer;
|
|
|
|
};
|
|
|
|
|
2017-01-04 18:37:55 -05:00
|
|
|
/**
|
2017-01-27 13:44:48 -05:00
|
|
|
* Attach the storage module
|
|
|
|
* @param {!ScratchStorage} storage The storage module to attach
|
2017-01-04 18:37:55 -05:00
|
|
|
*/
|
2017-01-27 13:44:48 -05:00
|
|
|
Runtime.prototype.attachStorage = function (storage) {
|
|
|
|
this.storage = storage;
|
2017-01-04 18:37:55 -05:00
|
|
|
};
|
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-04-26 16:50:49 -04:00
|
|
|
/**
|
|
|
|
* Create a thread and push it to the list of threads.
|
2016-09-15 19:37:12 -04:00
|
|
|
* @param {!string} id ID of block that starts the stack.
|
|
|
|
* @param {!Target} target Target to run thread on.
|
2016-08-23 15:47:21 -04:00
|
|
|
* @return {!Thread} The newly created thread.
|
2016-04-26 16:50:49 -04:00
|
|
|
*/
|
2016-09-15 19:37:12 -04:00
|
|
|
Runtime.prototype._pushThread = function (id, target) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const thread = new Thread(id);
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.target = target;
|
2016-06-09 17:08:30 -04:00
|
|
|
thread.pushStack(id);
|
2016-04-26 16:50:49 -04:00
|
|
|
this.threads.push(thread);
|
2016-08-23 15:47:21 -04:00
|
|
|
return thread;
|
2016-04-26 16:50:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a thread from the list of threads.
|
2016-04-29 17:36:09 -04:00
|
|
|
* @param {?Thread} thread Thread object to remove from actives
|
2016-04-26 16:50:49 -04:00
|
|
|
*/
|
2016-04-29 17:36:09 -04:00
|
|
|
Runtime.prototype._removeThread = function (thread) {
|
2016-10-13 23:00:46 -04:00
|
|
|
// Inform sequencer to stop executing that thread.
|
|
|
|
this.sequencer.retireThread(thread);
|
|
|
|
// Remove from the list.
|
2017-04-17 15:10:04 -04:00
|
|
|
const i = this.threads.indexOf(thread);
|
2016-05-02 13:09:38 -04:00
|
|
|
if (i > -1) {
|
|
|
|
this.threads.splice(i, 1);
|
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
};
|
|
|
|
|
2016-11-10 15:05:49 -05:00
|
|
|
/**
|
|
|
|
* Restart a thread in place, maintaining its position in the list of threads.
|
|
|
|
* This is used by `startHats` to and is necessary to ensure 2.0-like execution order.
|
|
|
|
* Test project: https://scratch.mit.edu/projects/130183108/
|
|
|
|
* @param {!Thread} thread Thread object to restart.
|
|
|
|
*/
|
|
|
|
Runtime.prototype._restartThread = function (thread) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const newThread = new Thread(thread.topBlock);
|
2016-11-10 15:05:49 -05:00
|
|
|
newThread.target = thread.target;
|
|
|
|
newThread.pushStack(thread.topBlock);
|
2017-04-17 15:10:04 -04:00
|
|
|
const i = this.threads.indexOf(thread);
|
2016-11-10 15:05:49 -05:00
|
|
|
if (i > -1) {
|
|
|
|
this.threads[i] = newThread;
|
|
|
|
} else {
|
|
|
|
this.threads.push(thread);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-29 10:01:31 -04:00
|
|
|
/**
|
|
|
|
* Return whether a thread is currently active/running.
|
|
|
|
* @param {?Thread} thread Thread object to check.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {boolean} True if the thread is active/running.
|
2016-08-29 10:01:31 -04:00
|
|
|
*/
|
|
|
|
Runtime.prototype.isActiveThread = function (thread) {
|
|
|
|
return this.threads.indexOf(thread) > -1;
|
|
|
|
};
|
|
|
|
|
2016-04-29 17:31:04 -04:00
|
|
|
/**
|
2016-08-11 11:11:27 -04:00
|
|
|
* Toggle a script.
|
|
|
|
* @param {!string} topBlockId ID of block that starts the script.
|
2016-04-29 17:31:04 -04:00
|
|
|
*/
|
2016-08-11 11:11:27 -04:00
|
|
|
Runtime.prototype.toggleScript = function (topBlockId) {
|
|
|
|
// Remove any existing thread.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.threads.length; i++) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (this.threads[i].topBlock === topBlockId) {
|
2016-04-29 17:31:04 -04:00
|
|
|
this._removeThread(this.threads[i]);
|
2016-04-29 17:39:18 -04:00
|
|
|
return;
|
2016-04-29 17:31:04 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 11:11:27 -04:00
|
|
|
// Otherwise add it.
|
2016-09-15 19:37:12 -04:00
|
|
|
this._pushThread(topBlockId, this._editingTarget);
|
2016-04-29 17:31:04 -04:00
|
|
|
};
|
|
|
|
|
2016-04-29 17:58:31 -04:00
|
|
|
/**
|
2016-08-23 18:12:32 -04:00
|
|
|
* Run a function `f` for all scripts in a workspace.
|
|
|
|
* `f` will be called with two parameters:
|
2016-08-29 10:18:49 -04:00
|
|
|
* - the top block ID of the script.
|
|
|
|
* - the target that owns the script.
|
2016-08-23 18:12:32 -04:00
|
|
|
* @param {!Function} f Function to call for each script.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {Target=} optTarget Optionally, a target to restrict to.
|
2016-04-29 17:58:31 -04:00
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
Runtime.prototype.allScriptsDo = function (f, optTarget) {
|
2017-04-17 15:10:04 -04:00
|
|
|
let targets = this.targets;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (optTarget) {
|
|
|
|
targets = [optTarget];
|
2016-04-29 17:58:31 -04:00
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let t = targets.length - 1; t >= 0; t--) {
|
|
|
|
const target = targets[t];
|
|
|
|
const scripts = target.blocks.getScripts();
|
|
|
|
for (let j = 0; j < scripts.length; j++) {
|
|
|
|
const topBlockId = scripts[j];
|
2016-08-29 10:18:49 -04:00
|
|
|
f(topBlockId, target);
|
2016-05-03 17:39:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
/**
|
2016-08-29 10:26:26 -04:00
|
|
|
* Start all relevant hats.
|
|
|
|
* @param {!string} requestedHatOpcode Opcode of hats to start.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {object=} optMatchFields Optionally, fields to match on the hat.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {Target=} optTarget Optionally, a target to restrict to.
|
2016-08-29 10:26:26 -04:00
|
|
|
* @return {Array.<Thread>} List of threads started by this function.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
2016-08-29 10:26:26 -04:00
|
|
|
Runtime.prototype.startHats = function (requestedHatOpcode,
|
2016-10-23 17:55:31 -04:00
|
|
|
optMatchFields, optTarget) {
|
2016-08-29 10:18:49 -04:00
|
|
|
if (!this._hats.hasOwnProperty(requestedHatOpcode)) {
|
|
|
|
// No known hat with this opcode.
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
const instance = this;
|
|
|
|
const newThreads = [];
|
2017-02-08 05:05:48 -05:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const opts in optMatchFields) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!optMatchFields.hasOwnProperty(opts)) continue;
|
2017-02-08 05:05:48 -05:00
|
|
|
optMatchFields[opts] = optMatchFields[opts].toUpperCase();
|
|
|
|
}
|
|
|
|
|
2016-08-29 10:18:49 -04:00
|
|
|
// Consider all scripts, looking for hats with opcode `requestedHatOpcode`.
|
2017-04-17 15:10:04 -04:00
|
|
|
this.allScriptsDo((topBlockId, target) => {
|
|
|
|
const blocks = target.blocks;
|
|
|
|
const block = blocks.getBlock(topBlockId);
|
|
|
|
const potentialHatOpcode = block.opcode;
|
2016-08-29 10:18:49 -04:00
|
|
|
if (potentialHatOpcode !== requestedHatOpcode) {
|
2016-08-23 18:12:32 -04:00
|
|
|
// Not the right hat.
|
|
|
|
return;
|
|
|
|
}
|
2016-12-23 09:38:18 -05:00
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
// Match any requested fields.
|
|
|
|
// For example: ensures that broadcasts match.
|
|
|
|
// This needs to happen before the block is evaluated
|
|
|
|
// (i.e., before the predicate can be run) because "broadcast and wait"
|
2016-08-29 10:26:26 -04:00
|
|
|
// needs to have a precise collection of started threads.
|
2017-04-17 15:10:04 -04:00
|
|
|
let hatFields = blocks.getFields(block);
|
2016-12-23 09:38:18 -05:00
|
|
|
|
|
|
|
// If no fields are present, check inputs (horizontal blocks)
|
|
|
|
if (Object.keys(hatFields).length === 0) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const hatInputs = blocks.getInputs(block);
|
|
|
|
for (const input in hatInputs) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!hatInputs.hasOwnProperty(input)) continue;
|
2017-04-17 15:10:04 -04:00
|
|
|
const id = hatInputs[input].block;
|
|
|
|
const inpBlock = blocks.getBlock(id);
|
|
|
|
const fields = blocks.getFields(inpBlock);
|
2016-12-23 09:38:18 -05:00
|
|
|
hatFields = Object.assign(fields, hatFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 17:55:31 -04:00
|
|
|
if (optMatchFields) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const matchField in optMatchFields) {
|
2017-02-08 05:05:48 -05:00
|
|
|
if (hatFields[matchField].value.toUpperCase() !==
|
2016-10-23 17:55:31 -04:00
|
|
|
optMatchFields[matchField]) {
|
2016-08-23 18:12:32 -04:00
|
|
|
// Field mismatch.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-23 09:38:18 -05:00
|
|
|
|
2016-08-29 10:18:49 -04:00
|
|
|
// Look up metadata for the relevant hat.
|
2017-04-17 15:10:04 -04:00
|
|
|
const hatMeta = instance._hats[requestedHatOpcode];
|
2016-08-29 10:18:49 -04:00
|
|
|
if (hatMeta.restartExistingThreads) {
|
2016-08-29 10:26:26 -04:00
|
|
|
// If `restartExistingThreads` is true, we should stop
|
|
|
|
// any existing threads starting with the top block.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < instance.threads.length; i++) {
|
2016-09-15 19:37:12 -04:00
|
|
|
if (instance.threads[i].topBlock === topBlockId &&
|
2016-10-23 17:55:31 -04:00
|
|
|
instance.threads[i].target === target) {
|
2016-11-10 15:05:49 -05:00
|
|
|
instance._restartThread(instance.threads[i]);
|
|
|
|
return;
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
2016-08-29 10:18:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-29 10:26:26 -04:00
|
|
|
// If `restartExistingThreads` is false, we should
|
|
|
|
// give up if any threads with the top block are running.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let j = 0; j < instance.threads.length; j++) {
|
2016-09-15 19:37:12 -04:00
|
|
|
if (instance.threads[j].topBlock === topBlockId &&
|
2016-10-23 17:55:31 -04:00
|
|
|
instance.threads[j].target === target) {
|
2016-08-29 10:18:49 -04:00
|
|
|
// Some thread is already running.
|
|
|
|
return;
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 10:18:49 -04:00
|
|
|
// Start the thread with this top block.
|
2016-09-15 19:37:12 -04:00
|
|
|
newThreads.push(instance._pushThread(topBlockId, target));
|
2016-10-23 17:55:31 -04:00
|
|
|
}, optTarget);
|
2016-08-23 18:12:32 -04:00
|
|
|
return newThreads;
|
|
|
|
};
|
|
|
|
|
2016-10-17 13:43:38 -04:00
|
|
|
/**
|
|
|
|
* Dispose all targets. Return to clean state.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.dispose = function () {
|
|
|
|
this.stopAll();
|
|
|
|
this.targets.map(this.disposeTarget, this);
|
|
|
|
};
|
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
|
|
|
* Dispose of a target.
|
2016-10-17 13:53:49 -04:00
|
|
|
* @param {!Target} disposingTarget Target to dispose of.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-17 13:43:38 -04:00
|
|
|
Runtime.prototype.disposeTarget = function (disposingTarget) {
|
2017-04-17 15:10:04 -04:00
|
|
|
this.targets = this.targets.filter(target => {
|
2016-10-17 13:43:38 -04:00
|
|
|
if (disposingTarget !== target) return true;
|
|
|
|
// Allow target to do dispose actions.
|
|
|
|
target.dispose();
|
|
|
|
// Remove from list of targets.
|
|
|
|
return false;
|
|
|
|
});
|
2016-09-15 19:37:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop any threads acting on the target.
|
|
|
|
* @param {!Target} target Target to stop threads for.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {Thread=} optThreadException Optional thread to skip.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
Runtime.prototype.stopForTarget = function (target, optThreadException) {
|
2016-09-15 19:37:12 -04:00
|
|
|
// Stop any threads on the target.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.threads.length; i++) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (this.threads[i] === optThreadException) {
|
2016-10-13 23:00:46 -04:00
|
|
|
continue;
|
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
if (this.threads[i].target === target) {
|
2016-09-15 19:37:12 -04:00
|
|
|
this._removeThread(this.threads[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
/**
|
|
|
|
* Start all threads that start with the green flag.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.greenFlag = function () {
|
2016-09-15 19:37:12 -04:00
|
|
|
this.stopAll();
|
2016-08-23 18:12:32 -04:00
|
|
|
this.ioDevices.clock.resetProjectTimer();
|
2016-08-29 10:26:26 -04:00
|
|
|
this.clearEdgeActivatedValues();
|
2016-09-21 16:31:07 -04:00
|
|
|
// Inform all targets of the green flag.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
2016-09-21 16:31:07 -04:00
|
|
|
this.targets[i].onGreenFlag();
|
|
|
|
}
|
2016-08-29 10:26:26 -04:00
|
|
|
this.startHats('event_whenflagclicked');
|
2016-08-23 18:12:32 -04:00
|
|
|
};
|
|
|
|
|
2016-04-29 17:58:31 -04:00
|
|
|
/**
|
2016-09-15 19:37:12 -04:00
|
|
|
* Stop "everything."
|
2016-04-29 17:58:31 -04:00
|
|
|
*/
|
|
|
|
Runtime.prototype.stopAll = function () {
|
2016-09-15 19:37:12 -04:00
|
|
|
// Dispose all clones.
|
2017-04-17 15:10:04 -04:00
|
|
|
const newTargets = [];
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
2016-10-27 16:28:39 -04:00
|
|
|
this.targets[i].onStopAll();
|
2016-09-15 19:37:12 -04:00
|
|
|
if (this.targets[i].hasOwnProperty('isOriginal') &&
|
|
|
|
!this.targets[i].isOriginal) {
|
|
|
|
this.targets[i].dispose();
|
|
|
|
} else {
|
|
|
|
newTargets.push(this.targets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.targets = newTargets;
|
|
|
|
// Dispose all threads.
|
2017-04-17 15:10:04 -04:00
|
|
|
const threadsCopy = this.threads.slice();
|
2016-05-03 14:11:37 -04:00
|
|
|
while (threadsCopy.length > 0) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const poppedThread = threadsCopy.pop();
|
2016-06-07 20:59:34 -04:00
|
|
|
this._removeThread(poppedThread);
|
2016-04-29 17:58:31 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 16:50:49 -04:00
|
|
|
/**
|
|
|
|
* Repeatedly run `sequencer.stepThreads` and filter out
|
|
|
|
* inactive threads after each iteration.
|
|
|
|
*/
|
|
|
|
Runtime.prototype._step = function () {
|
2016-08-29 10:26:26 -04:00
|
|
|
// Find all edge-activated hats, and add them to threads to be evaluated.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const hatType in this._hats) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!this._hats.hasOwnProperty(hatType)) continue;
|
2017-04-17 15:10:04 -04:00
|
|
|
const hat = this._hats[hatType];
|
2016-08-29 10:26:26 -04:00
|
|
|
if (hat.edgeActivated) {
|
|
|
|
this.startHats(hatType);
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
this.redrawRequested = false;
|
2017-04-17 15:10:04 -04:00
|
|
|
const doneThreads = this.sequencer.stepThreads();
|
2016-11-28 10:49:05 -05:00
|
|
|
this._updateGlows(doneThreads);
|
|
|
|
this._setThreadCount(this.threads.length + doneThreads.length);
|
2016-10-26 11:32:15 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
// @todo: Only render when this.redrawRequested or clones rendered.
|
|
|
|
this.renderer.draw();
|
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Set the current editing target known by the runtime.
|
|
|
|
* @param {!Target} editingTarget New editing target.
|
|
|
|
*/
|
2016-09-08 09:40:53 -04:00
|
|
|
Runtime.prototype.setEditingTarget = function (editingTarget) {
|
|
|
|
this._editingTarget = editingTarget;
|
2016-10-17 23:23:16 -04:00
|
|
|
// Script glows must be cleared.
|
|
|
|
this._scriptGlowsPreviousFrame = [];
|
|
|
|
this._updateGlows();
|
2016-10-26 13:27:12 -04:00
|
|
|
this.spriteInfoReport(editingTarget);
|
2016-09-08 09:40:53 -04:00
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Set whether we are in 30 TPS compatibility mode.
|
|
|
|
* @param {boolean} compatibilityModeOn True iff in compatibility mode.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.setCompatibilityMode = function (compatibilityModeOn) {
|
|
|
|
this.compatibilityMode = compatibilityModeOn;
|
|
|
|
if (this._steppingInterval) {
|
2016-12-23 10:39:19 -05:00
|
|
|
clearInterval(this._steppingInterval);
|
2016-10-17 23:23:16 -04:00
|
|
|
this.start();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-20 11:42:16 -04:00
|
|
|
* Emit glows/glow clears for scripts after a single tick.
|
2016-10-17 23:23:16 -04:00
|
|
|
* Looks at `this.threads` and notices which have turned on/off new glows.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {Array.<Thread>=} optExtraThreads Optional list of inactive threads.
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
Runtime.prototype._updateGlows = function (optExtraThreads) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const searchThreads = [];
|
2016-10-17 23:23:16 -04:00
|
|
|
searchThreads.push.apply(searchThreads, this.threads);
|
2016-10-23 17:55:31 -04:00
|
|
|
if (optExtraThreads) {
|
|
|
|
searchThreads.push.apply(searchThreads, optExtraThreads);
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
// Set of scripts that request a glow this frame.
|
2017-04-17 15:10:04 -04:00
|
|
|
const requestedGlowsThisFrame = [];
|
2016-09-08 09:40:53 -04:00
|
|
|
// Final set of scripts glowing during this frame.
|
2017-04-17 15:10:04 -04:00
|
|
|
const finalScriptGlows = [];
|
2016-09-08 09:40:53 -04:00
|
|
|
// Find all scripts that should be glowing.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < searchThreads.length; i++) {
|
|
|
|
const thread = searchThreads[i];
|
|
|
|
const target = thread.target;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (target === this._editingTarget) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const blockForThread = thread.blockGlowInFrame;
|
2016-10-17 23:23:16 -04:00
|
|
|
if (thread.requestScriptGlowInFrame) {
|
2017-04-17 15:10:04 -04:00
|
|
|
let script = target.blocks.getTopLevelScript(blockForThread);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (!script) {
|
|
|
|
// Attempt to find in flyout blocks.
|
|
|
|
script = this.flyoutBlocks.getTopLevelScript(
|
|
|
|
blockForThread
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (script) {
|
|
|
|
requestedGlowsThisFrame.push(script);
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compare to previous frame.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let j = 0; j < this._scriptGlowsPreviousFrame.length; j++) {
|
|
|
|
const previousFrameGlow = this._scriptGlowsPreviousFrame[j];
|
2016-09-08 09:40:53 -04:00
|
|
|
if (requestedGlowsThisFrame.indexOf(previousFrameGlow) < 0) {
|
|
|
|
// Glow turned off.
|
|
|
|
this.glowScript(previousFrameGlow, false);
|
|
|
|
} else {
|
|
|
|
// Still glowing.
|
|
|
|
finalScriptGlows.push(previousFrameGlow);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let k = 0; k < requestedGlowsThisFrame.length; k++) {
|
|
|
|
const currentFrameGlow = requestedGlowsThisFrame[k];
|
2016-09-08 09:40:53 -04:00
|
|
|
if (this._scriptGlowsPreviousFrame.indexOf(currentFrameGlow) < 0) {
|
|
|
|
// Glow turned on.
|
|
|
|
this.glowScript(currentFrameGlow, true);
|
|
|
|
finalScriptGlows.push(currentFrameGlow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._scriptGlowsPreviousFrame = finalScriptGlows;
|
|
|
|
};
|
|
|
|
|
2016-11-23 15:47:49 -05:00
|
|
|
/**
|
|
|
|
* Emit run start/stop after each tick. Emits when `this.threads.length` goes
|
|
|
|
* between non-zero and zero
|
2016-11-24 10:36:30 -05:00
|
|
|
*
|
|
|
|
* @param {number} threadCount The new threadCount
|
2016-11-23 15:47:49 -05:00
|
|
|
*/
|
|
|
|
Runtime.prototype._setThreadCount = function (threadCount) {
|
|
|
|
if (this._threadCount === 0 && threadCount > 0) {
|
|
|
|
this.emit(Runtime.PROJECT_RUN_START);
|
|
|
|
}
|
|
|
|
if (this._threadCount > 0 && threadCount === 0) {
|
|
|
|
this.emit(Runtime.PROJECT_RUN_STOP);
|
|
|
|
}
|
|
|
|
this._threadCount = threadCount;
|
|
|
|
};
|
|
|
|
|
2016-09-15 13:51:40 -04:00
|
|
|
/**
|
|
|
|
* "Quiet" a script's glow: stop the VM from generating glow/unglow events
|
|
|
|
* about that script. Use when a script has just been deleted, but we may
|
|
|
|
* still be tracking glow data about it.
|
|
|
|
* @param {!string} scriptBlockId Id of top-level block in script to quiet.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.quietGlow = function (scriptBlockId) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const index = this._scriptGlowsPreviousFrame.indexOf(scriptBlockId);
|
2016-09-15 13:51:40 -04:00
|
|
|
if (index > -1) {
|
|
|
|
this._scriptGlowsPreviousFrame.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* Emit feedback for block glowing (used in the sequencer).
|
|
|
|
* @param {?string} blockId ID for the block to update glow
|
|
|
|
* @param {boolean} isGlowing True to turn on glow; false to turn off.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.glowBlock = function (blockId, isGlowing) {
|
|
|
|
if (isGlowing) {
|
2016-11-23 15:43:05 -05:00
|
|
|
this.emit(Runtime.BLOCK_GLOW_ON, {id: blockId});
|
2016-05-02 18:09:02 -04:00
|
|
|
} else {
|
2016-11-23 15:43:05 -05:00
|
|
|
this.emit(Runtime.BLOCK_GLOW_OFF, {id: blockId});
|
2016-05-02 18:09:02 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-23 15:47:21 -04:00
|
|
|
/**
|
|
|
|
* Emit feedback for script glowing.
|
|
|
|
* @param {?string} topBlockId ID for the top block to update glow
|
|
|
|
* @param {boolean} isGlowing True to turn on glow; false to turn off.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.glowScript = function (topBlockId, isGlowing) {
|
|
|
|
if (isGlowing) {
|
2016-11-23 15:43:05 -05:00
|
|
|
this.emit(Runtime.SCRIPT_GLOW_ON, {id: topBlockId});
|
2016-08-23 15:47:21 -04:00
|
|
|
} else {
|
2016-11-23 15:43:05 -05:00
|
|
|
this.emit(Runtime.SCRIPT_GLOW_OFF, {id: topBlockId});
|
2016-08-23 15:47:21 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-07 19:42:38 -04:00
|
|
|
/**
|
|
|
|
* Emit value for reporter to show in the blocks.
|
|
|
|
* @param {string} blockId ID for the block.
|
|
|
|
* @param {string} value Value to show associated with the block.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.visualReport = function (blockId, value) {
|
2016-11-23 15:43:05 -05:00
|
|
|
this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)});
|
2016-07-07 19:42:38 -04:00
|
|
|
};
|
|
|
|
|
2016-10-26 13:27:12 -04:00
|
|
|
/**
|
2016-11-30 13:21:00 -05:00
|
|
|
* Emit a sprite info report if the provided target is the original sprite
|
2016-10-26 13:27:12 -04:00
|
|
|
* @param {!Target} target Target to report sprite info for.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.spriteInfoReport = function (target) {
|
2016-11-30 13:21:00 -05:00
|
|
|
if (!target.isOriginal) return;
|
|
|
|
|
2016-12-05 18:02:51 -05:00
|
|
|
this.emit(Runtime.SPRITE_INFO_REPORT, target.toJSON());
|
2016-10-26 13:27:12 -04:00
|
|
|
};
|
|
|
|
|
2016-06-29 13:48:30 -04:00
|
|
|
/**
|
2016-09-15 19:37:12 -04:00
|
|
|
* Get a target by its id.
|
|
|
|
* @param {string} targetId Id of target to find.
|
|
|
|
* @return {?Target} The target, if found.
|
2016-06-29 13:48:30 -04:00
|
|
|
*/
|
2016-09-15 19:37:12 -04:00
|
|
|
Runtime.prototype.getTargetById = function (targetId) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
2016-10-23 17:55:31 -04:00
|
|
|
if (target.id === targetId) {
|
2016-06-29 13:48:30 -04:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-31 12:18:24 -04:00
|
|
|
/**
|
2016-09-15 19:37:12 -04:00
|
|
|
* Get the first original (non-clone-block-created) sprite given a name.
|
|
|
|
* @param {string} spriteName Name of sprite to look for.
|
|
|
|
* @return {?Target} Target representing a sprite of the given name.
|
2016-08-31 12:18:24 -04:00
|
|
|
*/
|
2016-09-15 19:37:12 -04:00
|
|
|
Runtime.prototype.getSpriteTargetByName = function (spriteName) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
2016-10-23 17:55:31 -04:00
|
|
|
if (target.sprite && target.sprite.name === spriteName) {
|
2016-08-31 12:18:24 -04:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-03 09:35:57 -05:00
|
|
|
/**
|
|
|
|
* Get a target by its drawable id.
|
|
|
|
* @param {number} drawableID drawable id of target to find
|
|
|
|
* @return {?Target} The target, if found
|
|
|
|
*/
|
|
|
|
Runtime.prototype.getTargetByDrawableId = function (drawableID) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
2017-03-03 09:35:57 -05:00
|
|
|
if (target.drawableID === drawableID) return target;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
|
|
|
* Update the clone counter to track how many clones are created.
|
|
|
|
* @param {number} changeAmount How many clones have been created/destroyed.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.changeCloneCounter = function (changeAmount) {
|
|
|
|
this._cloneCounter += changeAmount;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether there are clones available.
|
|
|
|
* @return {boolean} True until the number of clones hits Runtime.MAX_CLONES.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.clonesAvailable = function () {
|
|
|
|
return this._cloneCounter < Runtime.MAX_CLONES;
|
|
|
|
};
|
|
|
|
|
2016-09-08 09:40:27 -04:00
|
|
|
/**
|
|
|
|
* Get a target representing the Scratch stage, if one exists.
|
|
|
|
* @return {?Target} The target, if found.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.getTargetForStage = function () {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
2016-09-08 09:40:27 -04:00
|
|
|
if (target.isStage) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Tell the runtime to request a redraw.
|
|
|
|
* Use after a clone/sprite has completed some visible operation on the stage.
|
|
|
|
*/
|
|
|
|
Runtime.prototype.requestRedraw = function () {
|
|
|
|
this.redrawRequested = true;
|
|
|
|
};
|
|
|
|
|
2016-04-26 16:50:49 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Set up timers to repeatedly step in a browser.
|
2016-04-26 16:50:49 -04:00
|
|
|
*/
|
|
|
|
Runtime.prototype.start = function () {
|
2017-04-17 15:10:04 -04:00
|
|
|
let interval = Runtime.THREAD_STEP_INTERVAL;
|
2016-10-26 11:32:15 -04:00
|
|
|
if (this.compatibilityMode) {
|
2016-10-17 23:23:16 -04:00
|
|
|
interval = Runtime.THREAD_STEP_INTERVAL_COMPATIBILITY;
|
|
|
|
}
|
|
|
|
this.currentStepTime = interval;
|
2017-04-17 15:10:04 -04:00
|
|
|
this._steppingInterval = setInterval(() => {
|
2016-04-26 16:50:49 -04:00
|
|
|
this._step();
|
2017-04-17 15:10:04 -04:00
|
|
|
}, interval);
|
2016-04-26 16:50:49 -04:00
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Runtime;
|