From 9a65df4c12c619cff3e997c14a079bd3eb610c22 Mon Sep 17 00:00:00 2001 From: DD Date: Fri, 23 Feb 2018 16:24:18 -0500 Subject: [PATCH] Make index optional --- src/sprites/rendered-target.js | 40 ++++++++++++---------------------- src/virtual-machine.js | 4 ++-- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index aa1963480..ae02b72bd 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -401,22 +401,16 @@ class RenderedTarget extends Target { /** * Add a costume, taking care to avoid duplicate names. * @param {!object} costumeObject Object representing the costume. + * @param {?int} index Index at which to add costume */ - addCostume (costumeObject) { + addCostume (costumeObject, index) { const usedNames = this.sprite.costumes.map(costume => costume.name); costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames); - this.sprite.costumes.push(costumeObject); - } - - /** - * Add a costume at the given index, taking care to avoid duplicate names. - * @param {!object} costumeObject Object representing the costume. - * @param {!int} index Index at which to add costume - */ - addCostumeAt (costumeObject, index) { - const usedNames = this.sprite.costumes.map(costume => costume.name); - costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames); - this.sprite.costumes.splice(index, 0, costumeObject); + if (index) { + this.sprite.costumes.splice(index, 0, costumeObject); + } else { + this.sprite.costumes.push(costumeObject); + } } /** @@ -472,22 +466,16 @@ class RenderedTarget extends Target { /** * Add a sound, taking care to avoid duplicate names. * @param {!object} soundObject Object representing the sound. - * @param {!int} index Index at which to add costume + * @param {?int} index Index at which to add costume */ - addSoundAt (soundObject, index) { + addSound (soundObject, index) { const usedNames = this.sprite.sounds.map(sound => sound.name); soundObject.name = StringUtil.unusedName(soundObject.name, usedNames); - this.sprite.sounds.splice(index, 0, soundObject); - } - - /** - * Add a sound, taking care to avoid duplicate names. - * @param {!object} soundObject Object representing the sound. - */ - addSound (soundObject) { - const usedNames = this.sprite.sounds.map(sound => sound.name); - soundObject.name = StringUtil.unusedName(soundObject.name, usedNames); - this.sprite.sounds.push(soundObject); + if (index) { + this.sprite.sounds.splice(index, 0, soundObject); + } else { + this.sprite.sounds.push(soundObject); + } } /** diff --git a/src/virtual-machine.js b/src/virtual-machine.js index e2b18f2a3..f9fa45be3 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -337,7 +337,7 @@ class VirtualMachine extends EventEmitter { const clone = Object.assign({}, originalCostume); const md5ext = `${clone.assetId}.${clone.dataFormat}`; return loadCostume(md5ext, clone, this.runtime).then(() => { - this.editingTarget.addCostumeAt(clone, costumeIndex + 1); + this.editingTarget.addCostume(clone, costumeIndex + 1); this.editingTarget.setCostume(costumeIndex + 1); this.emitTargetsUpdate(); }); @@ -352,7 +352,7 @@ class VirtualMachine extends EventEmitter { const originalSound = this.editingTarget.getSounds()[soundIndex]; const clone = Object.assign({}, originalSound); return loadSound(clone, this.runtime).then(() => { - this.editingTarget.addSoundAt(clone, soundIndex + 1); + this.editingTarget.addSound(clone, soundIndex + 1); this.emitTargetsUpdate(); }); }