From ce17fdbd30ffaa400197f7474bab0dc4a5d531b8 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Wed, 30 Nov 2016 13:16:49 -0500 Subject: [PATCH 1/3] Include costume data in sprite info reports Also emit the report on all costume changes --- src/engine/runtime.js | 1 + src/sprites/rendered-target.js | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 5c7b788c4..912c2923c 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -734,6 +734,7 @@ Runtime.prototype.spriteInfoReport = function (target) { x: target.x, y: target.y, direction: target.direction, + costume: target.getCurrentCostume(), visible: target.visible, rotationStyle: target.rotationStyle }); diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 50ad7e8f5..04779d46d 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -329,6 +329,7 @@ RenderedTarget.prototype.setCostume = function (index) { this.runtime.requestRedraw(); } } + this.runtime.spriteInfoReport(this); }; /** @@ -370,6 +371,14 @@ RenderedTarget.prototype.getCostumeIndexByName = function (costumeName) { return -1; }; +/** + * Get a costume of this rendered target by id. + * @return {object} current costume + */ +RenderedTarget.prototype.getCurrentCostume = function () { + return this.sprite.costumes[this.currentCostume]; +}; + /** * Update all drawable properties for this rendered target. * Use when a batch has changed, e.g., when the drawable is first created. From adaf2df743d2301306b89cf6ecd19a61c04d142f Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Wed, 30 Nov 2016 13:21:00 -0500 Subject: [PATCH 2/3] Emit sprite info report for all sprites This allows costume data to reach listeners even when the sprite isn't the editing target. Filter out non-editing target reports in the playground to match previous behavior. --- playground/playground.js | 1 + src/engine/runtime.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/playground/playground.js b/playground/playground.js index cd9f8dff0..75a9f518e 100644 --- a/playground/playground.js +++ b/playground/playground.js @@ -154,6 +154,7 @@ window.onload = function() { }); vm.on('SPRITE_INFO_REPORT', function(data) { + if (data.id !== selectedTarget.value) return; // Not the editingTarget document.getElementById('sinfo-x').value = data.x; document.getElementById('sinfo-y').value = data.y; document.getElementById('sinfo-direction').value = data.direction; diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 912c2923c..0e772eb44 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -723,14 +723,14 @@ Runtime.prototype.visualReport = function (blockId, value) { }; /** - * Emit a sprite info report if the provided target is the editing target. + * Emit a sprite info report if the provided target is the original sprite * @param {!Target} target Target to report sprite info for. */ Runtime.prototype.spriteInfoReport = function (target) { - if (target !== this._editingTarget) { - return; - } + if (!target.isOriginal) return; + this.emit(Runtime.SPRITE_INFO_REPORT, { + id: target.id, x: target.x, y: target.y, direction: target.direction, From e9da046969ced01e7a4642356a7584889cdc5dc8 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Mon, 5 Dec 2016 18:02:51 -0500 Subject: [PATCH 3/3] Report full sprite info in targetsUpdate We need more than just the name for the initial render, so send everything consistent with sprite info reports. --- playground/playground.js | 6 +++--- src/engine/runtime.js | 10 +--------- src/index.js | 2 +- src/sprites/rendered-target.js | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/playground/playground.js b/playground/playground.js index 75a9f518e..81efb53b4 100644 --- a/playground/playground.js +++ b/playground/playground.js @@ -121,13 +121,13 @@ window.onload = function() { // Generate new select box. for (var i = 0; i < data.targetList.length; i++) { var targetOption = document.createElement('option'); - targetOption.setAttribute('value', data.targetList[i][0]); + targetOption.setAttribute('value', data.targetList[i].id); // If target id matches editingTarget id, select it. - if (data.targetList[i][0] == data.editingTarget) { + if (data.targetList[i].id == data.editingTarget) { targetOption.setAttribute('selected', 'selected'); } targetOption.appendChild( - document.createTextNode(data.targetList[i][1]) + document.createTextNode(data.targetList[i].name) ); selectedTarget.appendChild(targetOption); } diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 0e772eb44..a404ce421 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -729,15 +729,7 @@ Runtime.prototype.visualReport = function (blockId, value) { Runtime.prototype.spriteInfoReport = function (target) { if (!target.isOriginal) return; - this.emit(Runtime.SPRITE_INFO_REPORT, { - id: target.id, - x: target.x, - y: target.y, - direction: target.direction, - costume: target.getCurrentCostume(), - visible: target.visible, - rotationStyle: target.rotationStyle - }); + this.emit(Runtime.SPRITE_INFO_REPORT, target.toJSON()); }; /** diff --git a/src/index.js b/src/index.js index 4ed724f39..24e1d67b7 100644 --- a/src/index.js +++ b/src/index.js @@ -304,7 +304,7 @@ VirtualMachine.prototype.emitTargetsUpdate = function () { // Don't report clones. return !target.hasOwnProperty('isOriginal') || target.isOriginal; }).map(function (target) { - return [target.id, target.getName()]; + return target.toJSON(); }), // Currently editing target id. editingTarget: this.editingTarget ? this.editingTarget.id : null diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 04779d46d..08ee727f8 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -379,6 +379,14 @@ RenderedTarget.prototype.getCurrentCostume = function () { return this.sprite.costumes[this.currentCostume]; }; +/** + * Get full costume list + * @return {object[]} list of costumes + */ +RenderedTarget.prototype.getCostumes = function () { + return this.sprite.costumes; +}; + /** * Update all drawable properties for this rendered target. * Use when a batch has changed, e.g., when the drawable is first created. @@ -654,6 +662,25 @@ RenderedTarget.prototype.postSpriteInfo = function (data) { } }; +/** + * Serialize sprite info, used when emitting events about the sprite + * @returns {object} sprite data as a simple object + */ +RenderedTarget.prototype.toJSON = function () { + return { + id: this.id, + name: this.getName(), + isStage: this.isStage, + x: this.x, + y: this.y, + direction: this.direction, + costume: this.getCurrentCostume(), + costumeCount: this.getCostumes().length, + visible: this.visible, + rotationStyle: this.rotationStyle + }; +}; + /** * Dispose, destroying any run-time properties. */