diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 34c008a1e..2708ebe7e 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -104,6 +104,13 @@ class Runtime extends EventEmitter { */ this._cloneCounter = 0; + /** + * Flag to emit a targets update at the end of a step. When target data + * changes, this flag is set to true. + * @type {boolean} + */ + this._refreshTargets = false; + /** * Whether the project is in "turbo mode." * @type {Boolean} @@ -226,11 +233,11 @@ class Runtime extends EventEmitter { } /** - * Event name for sprite info report. + * Event name for targets update report. * @const {string} */ - static get SPRITE_INFO_REPORT () { - return 'SPRITE_INFO_REPORT'; + static get TARGETS_UPDATE () { + return 'TARGETS_UPDATE'; } /** @@ -638,6 +645,7 @@ class Runtime extends EventEmitter { * inactive threads after each iteration. */ _step () { + this._refreshTargets = false; // Find all edge-activated hats, and add them to threads to be evaluated. for (const hatType in this._hats) { if (!this._hats.hasOwnProperty(hatType)) continue; @@ -655,6 +663,7 @@ class Runtime extends EventEmitter { // @todo: Only render when this.redrawRequested or clones rendered. this.renderer.draw(); } + if (this._refreshTargets) this.emit(Runtime.TARGETS_UPDATE); } /** @@ -673,7 +682,7 @@ class Runtime extends EventEmitter { // Script glows must be cleared. this._scriptGlowsPreviousFrame = []; this._updateGlows(); - this.spriteInfoReport(editingTarget); + this.requestTargetsUpdate(editingTarget); } /** @@ -809,16 +818,6 @@ class Runtime extends EventEmitter { this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)}); } - /** - * Emit a sprite info report if the provided target is the original sprite - * @param {!Target} target Target to report sprite info for. - */ - spriteInfoReport (target) { - if (!target.isOriginal) return; - - this.emit(Runtime.SPRITE_INFO_REPORT, target.toJSON()); - } - /** * Get a target by its id. * @param {string} targetId Id of target to find. @@ -896,6 +895,16 @@ class Runtime extends EventEmitter { this.redrawRequested = true; } + /** + * Emit a targets update at the end of the step if the provided target is + * the original sprite + * @param {!Target} target Target requesting the targets update + */ + requestTargetsUpdate (target) { + if (!target.isOriginal) return; + this._refreshTargets = true; + } + /** * Set up timers to repeatedly step in a browser. */ diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 1ffc00a1e..8489b37cb 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -197,7 +197,7 @@ class RenderedTarget extends Target { this.y = y; } this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY); - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -240,7 +240,7 @@ class RenderedTarget extends Target { this.runtime.requestRedraw(); } } - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -250,7 +250,7 @@ class RenderedTarget extends Target { setDraggable (draggable) { if (this.isStage) return; this.draggable = !!draggable; - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -287,7 +287,7 @@ class RenderedTarget extends Target { this.runtime.requestRedraw(); } } - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -386,7 +386,7 @@ class RenderedTarget extends Target { this.runtime.requestRedraw(); } } - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -411,7 +411,7 @@ class RenderedTarget extends Target { this.runtime.requestRedraw(); } } - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** @@ -483,7 +483,7 @@ class RenderedTarget extends Target { this.runtime.requestRedraw(); } } - this.runtime.spriteInfoReport(this); + this.runtime.requestTargetsUpdate(this); } /** diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 719bf6cc7..79c16d2fc 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -19,42 +19,41 @@ class VirtualMachine extends EventEmitter { constructor () { super(); - const instance = this; /** * VM runtime, to store blocks, I/O devices, sprites/targets, etc. * @type {!Runtime} */ - instance.runtime = new Runtime(); + this.runtime = new Runtime(); /** * 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; + this.editingTarget = null; // Runtime emits are passed along as VM emits. - instance.runtime.on(Runtime.SCRIPT_GLOW_ON, glowData => { - instance.emit(Runtime.SCRIPT_GLOW_ON, glowData); + this.runtime.on(Runtime.SCRIPT_GLOW_ON, glowData => { + this.emit(Runtime.SCRIPT_GLOW_ON, glowData); }); - instance.runtime.on(Runtime.SCRIPT_GLOW_OFF, glowData => { - instance.emit(Runtime.SCRIPT_GLOW_OFF, glowData); + this.runtime.on(Runtime.SCRIPT_GLOW_OFF, glowData => { + this.emit(Runtime.SCRIPT_GLOW_OFF, glowData); }); - instance.runtime.on(Runtime.BLOCK_GLOW_ON, glowData => { - instance.emit(Runtime.BLOCK_GLOW_ON, glowData); + this.runtime.on(Runtime.BLOCK_GLOW_ON, glowData => { + this.emit(Runtime.BLOCK_GLOW_ON, glowData); }); - instance.runtime.on(Runtime.BLOCK_GLOW_OFF, glowData => { - instance.emit(Runtime.BLOCK_GLOW_OFF, glowData); + this.runtime.on(Runtime.BLOCK_GLOW_OFF, glowData => { + this.emit(Runtime.BLOCK_GLOW_OFF, glowData); }); - instance.runtime.on(Runtime.PROJECT_RUN_START, () => { - instance.emit(Runtime.PROJECT_RUN_START); + this.runtime.on(Runtime.PROJECT_RUN_START, () => { + this.emit(Runtime.PROJECT_RUN_START); }); - instance.runtime.on(Runtime.PROJECT_RUN_STOP, () => { - instance.emit(Runtime.PROJECT_RUN_STOP); + this.runtime.on(Runtime.PROJECT_RUN_STOP, () => { + this.emit(Runtime.PROJECT_RUN_STOP); }); - instance.runtime.on(Runtime.VISUAL_REPORT, visualReport => { - instance.emit(Runtime.VISUAL_REPORT, visualReport); + this.runtime.on(Runtime.VISUAL_REPORT, visualReport => { + this.emit(Runtime.VISUAL_REPORT, visualReport); }); - instance.runtime.on(Runtime.SPRITE_INFO_REPORT, spriteInfo => { - instance.emit(Runtime.SPRITE_INFO_REPORT, spriteInfo); + this.runtime.on(Runtime.TARGETS_UPDATE, () => { + this.emitTargetsUpdate(); }); this.blockListener = this.blockListener.bind(this);