Make rendered targets in charge of deleting their own sounds and costumes

This commit is contained in:
Paul Kaplan 2017-05-16 11:01:50 -04:00
parent 71081502fe
commit a608ca4fed
3 changed files with 100 additions and 11 deletions

View file

@ -389,6 +389,39 @@ class RenderedTarget extends Target {
this.runtime.requestTargetsUpdate(this); this.runtime.requestTargetsUpdate(this);
} }
/**
* Delete a costume by index.
* @param {number} index Costume index to be deleted
*/
deleteCostume (index) {
const originalCostumeCount = this.sprite.costumes.length;
if (originalCostumeCount === 1) return;
this.sprite.costumes = this.sprite.costumes
.slice(0, index)
.concat(this.sprite.costumes.slice(index + 1));
if (index === originalCostumeCount - 1) {
this.setCostume(index - 1);
} else {
this.setCostume(index);
}
this.runtime.requestTargetsUpdate(this);
}
/**
* Delete a sound by index.
* @param {number} index Sound index to be deleted
*/
deleteSound (index) {
if (this.sprite.sounds.length === 1) return;
this.sprite.sounds = this.sprite.sounds
.slice(0, index)
.concat(this.sprite.sounds.slice(index + 1));
this.runtime.requestTargetsUpdate(this);
}
/** /**
* Update the rotation style. * Update the rotation style.
* @param {!string} rotationStyle New rotation style. * @param {!string} rotationStyle New rotation style.

View file

@ -291,13 +291,7 @@ class VirtualMachine extends EventEmitter {
* @param {int} costumeIndex - the index of the costume to be removed. * @param {int} costumeIndex - the index of the costume to be removed.
*/ */
deleteCostume (costumeIndex) { deleteCostume (costumeIndex) {
this.editingTarget.sprite.costumes = this.editingTarget.sprite.costumes this.editingTarget.deleteCostume(costumeIndex);
.slice(0, costumeIndex)
.concat(this.editingTarget.sprite.costumes.slice(costumeIndex + 1));
if (costumeIndex === this.editingTarget.currentCostume) {
this.editingTarget.setCostume(costumeIndex - 1);
}
this.emitTargetsUpdate();
} }
/** /**
@ -317,10 +311,7 @@ class VirtualMachine extends EventEmitter {
* @param {int} soundIndex - the index of the sound to be removed. * @param {int} soundIndex - the index of the sound to be removed.
*/ */
deleteSound (soundIndex) { deleteSound (soundIndex) {
this.editingTarget.sprite.sounds = this.editingTarget.sprite.sounds this.editingTarget.deleteSound(soundIndex);
.slice(0, soundIndex)
.concat(this.editingTarget.sprite.sounds.slice(soundIndex + 1));
this.emitTargetsUpdate();
} }
/** /**

View file

@ -98,6 +98,71 @@ test('setCostume', t => {
t.end(); t.end();
}); });
test('deleteCostume', t => {
const o1 = {id: 1};
const o2 = {id: 2};
const o3 = {id: 3};
const s = new Sprite();
const r = new Runtime();
s.costumes = [o1, o2, o3];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
// Deleting costume keeps costume index at 0
a.setCostume(0);
a.deleteCostume(0);
t.deepEqual(a.sprite.costumes, [o2, o3]);
t.equals(a.currentCostume, 0);
// Deleting a costume in the middle maintains current costume index
a.sprite.costumes = [o1, o2, o3];
a.setCostume(1);
a.deleteCostume(1);
t.deepEqual(a.sprite.costumes, [o1, o3]);
t.equals(a.currentCostume, 1);
// Deleting last costume selects previous costume
a.sprite.costumes = [o1, o2, o3];
a.setCostume(2);
a.deleteCostume(2);
t.deepEqual(a.sprite.costumes, [o1, o2]);
t.equals(a.currentCostume, 1);
// Refuses to delete only costume
a.sprite.costumes = [o1];
a.setCostume(0);
a.deleteCostume(0);
t.deepEqual(a.sprite.costumes, [o1]);
t.equals(a.currentCostume, 0);
t.end();
});
test('deleteSound', t => {
const o1 = {id: 1};
const o2 = {id: 2};
const o3 = {id: 3};
const s = new Sprite();
const r = new Runtime();
s.sounds = [o1, o2, o3];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
a.deleteSound(0);
t.deepEqual(a.sprite.sounds, [o2, o3]);
// Refuses to delete only sound
a.sprite.sounds = [o1];
a.deleteSound(0);
t.deepEqual(a.sprite.sounds, [o1]);
t.end();
});
test('setRotationStyle', t => { test('setRotationStyle', t => {
const s = new Sprite(); const s = new Sprite();
const r = new Runtime(); const r = new Runtime();