From 7faeed71284b24be2f2c273381976540d55e434e Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Mon, 5 Feb 2018 14:35:52 -0500 Subject: [PATCH] Update blocks after sound/costume rename. --- src/engine/blocks.js | 74 ++++++++++++++++++++++++++++++++++ src/sprites/rendered-target.js | 26 +++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 25fb0c17..48f5815e 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -577,6 +577,80 @@ class Blocks { } } + /** + * Update blocks after a sound, costume, or backdrop gets renamed. + * Any block referring to the old name of the asset should get updated + * to refer to the new name. + * @param {string} oldName The old name of the asset that was renamed. + * @param {string} newName The new name of the asset that was renamed. + * @param {string} assetType String representation of the kind of asset + * that was renamed. This can be one of 'costume', 'sound', or 'backdrop'. + */ + updateAssetName (oldName, newName, assetType) { + let getAssetField; + if (assetType === 'costume') { + getAssetField = this._getCostumeField.bind(this); + } else if (assetType === 'sound') { + getAssetField = this._getSoundField.bind(this); + } else if (assetType === 'backdrop') { + getAssetField = this._getBackdropField.bind(this); + } else { + return; + } + const blocks = this._blocks; + for (const blockId in blocks) { + const assetField = getAssetField(blockId); + if (assetField && assetField.value === oldName) { + assetField.value = newName; + } + } + } + + /** + * Helper function to retrieve a costume menu field from a block given its id. + * @param {string} blockId A unique identifier for a block + * @return {?object} The costume menu field of the block with the given block id. + * Null if either a block with the given id doesn't exist or if a costume menu field + * does not exist on the block with the given id. + */ + _getCostumeField (blockId) { + const block = this.getBlock(blockId); + if (block && block.fields.hasOwnProperty('COSTUME')) { + return block.fields.COSTUME; + } + return null; + } + + /** + * Helper function to retrieve a sound menu field from a block given its id. + * @param {string} blockId A unique identifier for a block + * @return {?object} The sound menu field of the block with the given block id. + * Null, if either a block with the given id doesn't exist or if a sound menu field + * does not exist on the block with the given id. + */ + _getSoundField (blockId) { + const block = this.getBlock(blockId); + if (block && block.fields.hasOwnProperty('SOUND_MENU')) { + return block.fields.SOUND_MENU; + } + return null; + } + + /** + * Helper function to retrieve a backdrop menu field from a block given its id. + * @param {string} blockId A unique identifier for a block + * @return {?object} The backdrop menu field of the block with the given block id. + * Null, if either a block with the given id doesn't exist or if a backdrop menu field + * does not exist on the block with the given id. + */ + _getBackdropField (blockId) { + const block = this.getBlock(blockId); + if (block && block.fields.hasOwnProperty('BACKDROP')) { + return block.fields.BACKDROP; + } + return null; + } + // --------------------------------------------------------------------- /** diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 515a30a0..da9af1cc 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -417,7 +417,26 @@ class RenderedTarget extends Target { const usedNames = this.sprite.costumes .filter((costume, index) => costumeIndex !== index) .map(costume => costume.name); - this.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames); + const oldName = this.sprite.costumes[costumeIndex].name; + const newUnusedName = StringUtil.unusedName(newName, usedNames); + this.sprite.costumes[costumeIndex].name = newUnusedName; + + if (this.isStage) { + this.blocks.updateAssetName(oldName, newUnusedName, 'backdrop'); + // Since this is a backdrop, run through the blocks of the other targets + // and update the backdrop name there as well + const targets = this.runtime.targets; + for (let i = 0; i < targets.length; i++) { + const currTarget = targets[i]; + if (currTarget.id === this.id) { + continue; + } + currTarget.blocks.updateAssetName(oldName, newUnusedName, 'backdrop'); + } + } else { + this.blocks.updateAssetName(oldName, newUnusedName, 'costume'); + } + } /** @@ -462,7 +481,10 @@ class RenderedTarget extends Target { const usedNames = this.sprite.sounds .filter((sound, index) => soundIndex !== index) .map(sound => sound.name); - this.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames); + const oldName = this.sprite.sounds[soundIndex].name; + const newUnusedName = StringUtil.unusedName(newName, usedNames); + this.sprite.sounds[soundIndex].name = newUnusedName; + this.blocks.updateAssetName(oldName, newUnusedName, 'sound'); } /**