Handle more cases of deleting costumes

This commit is contained in:
Paul Kaplan 2017-05-18 13:56:14 -04:00
parent 30a0c0d251
commit 423da12c3c
2 changed files with 46 additions and 6 deletions

View file

@ -401,10 +401,10 @@ class RenderedTarget extends Target {
.slice(0, index)
.concat(this.sprite.costumes.slice(index + 1));
if (index === originalCostumeCount - 1) {
if (index === this.currentCostume && index === originalCostumeCount - 1) {
this.setCostume(index - 1);
} else {
this.setCostume(index);
} else if (index < this.currentCostume) {
this.setCostume(this.currentCostume - 1);
}
this.runtime.requestTargetsUpdate(this);

View file

@ -102,6 +102,8 @@ test('deleteCostume', t => {
const o1 = {id: 1};
const o2 = {id: 2};
const o3 = {id: 3};
const o4 = {id: 4};
const o5 = {id: 5};
const s = new Sprite();
const r = new Runtime();
@ -110,20 +112,26 @@ test('deleteCostume', t => {
const renderer = new FakeRenderer();
a.renderer = renderer;
// Deleting costume keeps costume index at 0
// x* Costume 1 * Costume 2
// Costume 2 => Costume 3
// Costume 3
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
// Costume 1 Costume 1
// x* Costume 2 => * Costume 3
// Costume 3
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
// Costume 1 Costume 1
// Costume 2 => * Costume 2
// x* Costume 3
a.sprite.costumes = [o1, o2, o3];
a.setCostume(2);
a.deleteCostume(2);
@ -137,6 +145,38 @@ test('deleteCostume', t => {
t.deepEqual(a.sprite.costumes, [o1]);
t.equals(a.currentCostume, 0);
// Costume 1 Costume 1
// x Costume 2 Costume 3
// Costume 3 => * Costume 4
// * Costume 4 Costume 5
// Costume 5
a.sprite.costumes = [o1, o2, o3, o4, o5];
a.setCostume(3);
a.deleteCostume(1);
t.deepEqual(a.sprite.costumes, [o1, o3, o4, o5]);
t.equals(a.currentCostume, 2);
// Costume 1 Costume 1
// * Costume 2 * Costume 2
// Costume 3 => Costume 3
// x Costume 4 Costume 5
// Costume 5
a.sprite.costumes = [o1, o2, o3, o4, o5];
a.setCostume(1);
a.deleteCostume(3);
t.deepEqual(a.sprite.costumes, [o1, o2, o3, o5]);
t.equals(a.currentCostume, 1);
// Costume 1 Costume 1
// * Costume 2 * Costume 2
// Costume 3 => Costume 3
// Costume 4 Costume 4
// x Costume 5
a.sprite.costumes = [o1, o2, o3, o4, o5];
a.setCostume(1);
a.deleteCostume(4);
t.deepEqual(a.sprite.costumes, [o1, o2, o3, o4]);
t.equals(a.currentCostume, 1);
t.end();
});