From 64c8484066d8ce3e34fe18d3730e86a802250d9b Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Fri, 18 May 2018 10:11:59 -0400 Subject: [PATCH] Refactor so that layer group is optionally passed into createClone (and initDrawable) instead of isStage property getting stored on a sprite. --- src/serialization/sb2.js | 5 +++-- src/serialization/sb3.js | 5 +++-- src/sprites/rendered-target.js | 10 ++++------ src/sprites/sprite.js | 16 ++++++++-------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 516d9e797..e7a720406 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -15,6 +15,7 @@ const StringUtil = require('../util/string-util'); const specMap = require('./sb2_specmap'); const Variable = require('../engine/variable'); const MonitorRecord = require('../engine/monitor-record'); +const StageLayering = require('../engine/stage-layering'); const {loadCostume} = require('../import/load-costume.js'); const {loadSound} = require('../import/load-sound.js'); @@ -327,7 +328,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip) // Blocks container for this object. const blocks = new Blocks(); // @todo: For now, load all Scratch objects (stage/sprites) as a Sprite. - const sprite = new Sprite(blocks, runtime, topLevel /* whether this sprite is a stge or not */); + const sprite = new Sprite(blocks, runtime); // Sprite/stage name from JSON. if (object.hasOwnProperty('objName')) { sprite.name = topLevel ? 'Stage' : object.objName; @@ -399,7 +400,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip) } // Create the first clone, and load its run-state from JSON. - const target = sprite.createClone(); + const target = sprite.createClone(topLevel ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER); const getVariableId = generateVariableIdGetter(target.id, topLevel); diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index 06b237789..cfd661af1 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -8,6 +8,7 @@ const vmPackage = require('../../package.json'); const Blocks = require('../engine/blocks'); const Sprite = require('../sprites/sprite'); const Variable = require('../engine/variable'); +const StageLayering = require('../engine/stage-layering'); const log = require('../util/log'); const uid = require('../util/uid'); @@ -684,7 +685,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) { const blocks = new Blocks(); // @todo: For now, load all Scratch objects (stage/sprites) as a Sprite. - const sprite = new Sprite(blocks, runtime, object.isStage); + const sprite = new Sprite(blocks, runtime); // Sprite/stage name from JSON. if (object.hasOwnProperty('name')) { @@ -779,7 +780,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) { // process has been completed. }); // Create the first clone, and load its run-state from JSON. - const target = sprite.createClone(); + const target = sprite.createClone(object.isStage ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER); // Load target properties from JSON. if (object.hasOwnProperty('tempo')) { target.tempo = object.tempo; diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 5514c8e99..eb3c2234d 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -156,13 +156,11 @@ class RenderedTarget extends Target { /** * Create a drawable with the this.renderer. - * @param {boolean} isStage Whether the drawable should be initialized as - * the stage or a sprite + * @param {boolean} layerGroup The layer group this drawable should be added to */ - initDrawable (isStage) { + initDrawable (layerGroup) { if (this.renderer) { - this.drawableID = this.renderer.createDrawable(isStage ? - StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER); + this.drawableID = this.renderer.createDrawable(layerGroup); } // If we're a clone, start the hats. if (!this.isOriginal) { @@ -920,7 +918,7 @@ class RenderedTarget extends Target { newClone.effects = JSON.parse(JSON.stringify(this.effects)); newClone.variables = JSON.parse(JSON.stringify(this.variables)); newClone.lists = JSON.parse(JSON.stringify(this.lists)); - newClone.initDrawable(false); // this,sprite is not a stage if we're calling makeClone on it + newClone.initDrawable(StageLayering.SPRITE_LAYER); // TODO should sprite clones be in their own layer group? newClone.updateAllDrawableProperties(); // Place behind the current target. newClone.goBehindOther(this); diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index 173ea4c19..de735ecc3 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -3,6 +3,7 @@ const Blocks = require('../engine/blocks'); const {loadSoundFromAsset} = require('../import/load-sound'); const {loadCostumeFromAsset} = require('../import/load-costume'); const StringUtil = require('../util/string-util'); +const StageLayering = require('../engine/stage-layering'); class Sprite { /** @@ -10,10 +11,9 @@ class Sprite { * All clones of a sprite have shared blocks, shared costumes, shared variables. * @param {?Blocks} blocks Shared blocks object for all clones of sprite. * @param {Runtime} runtime Reference to the runtime. - * @param {boolean=} isStage Whether or not this sprite is a stage * @constructor */ - constructor (blocks, runtime, isStage) { + constructor (blocks, runtime) { this.runtime = runtime; if (!blocks) { // Shared set of blocks for all clones. @@ -47,10 +47,6 @@ class Sprite { * @type {Array.} */ this.clones = []; - - // Needed for figuring out whether the associated drawable should - // go in the background layer or sprite layer - this.isStage = isStage || false; } /** @@ -100,15 +96,19 @@ class Sprite { /** * Create a clone of this sprite. + * @param {string=} optLayerGroup Optional layer group the clone's drawable should be added to + * Defaults to the sprite layer group * @returns {!RenderedTarget} Newly created clone. */ - createClone () { + createClone (optLayerGroup) { const newClone = new RenderedTarget(this, this.runtime); newClone.isOriginal = this.clones.length === 0; this.clones.push(newClone); newClone.initAudio(); if (newClone.isOriginal) { - newClone.initDrawable(this.isStage); + // Default to the sprite layer group if optLayerGroup is not provided + const layerGroup = typeof optLayerGroup === 'string' ? optLayerGroup : StageLayering.SPRITE_LAYER; + newClone.initDrawable(layerGroup); this.runtime.fireTargetWasCreated(newClone); } else { this.runtime.fireTargetWasCreated(newClone, this.clones[0]);