mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-14 01:09:51 -04:00
commit
6f9da87abe
4 changed files with 51 additions and 8 deletions
|
@ -585,12 +585,17 @@ class RenderedTarget extends Target {
|
||||||
/**
|
/**
|
||||||
* Delete a sound by index.
|
* Delete a sound by index.
|
||||||
* @param {number} index Sound index to be deleted
|
* @param {number} index Sound index to be deleted
|
||||||
|
* @return {object} The deleted sound object, or null if no sound was deleted.
|
||||||
*/
|
*/
|
||||||
deleteSound (index) {
|
deleteSound (index) {
|
||||||
this.sprite.sounds = this.sprite.sounds
|
// Make sure the sound index is not out of bounds
|
||||||
.slice(0, index)
|
if (index < 0 || index >= this.sprite.sounds.length) {
|
||||||
.concat(this.sprite.sounds.slice(index + 1));
|
return null;
|
||||||
|
}
|
||||||
|
// Delete the sound at the given index
|
||||||
|
const deletedSound = this.sprite.sounds.splice(index, 1)[0];
|
||||||
this.runtime.requestTargetsUpdate(this);
|
this.runtime.requestTargetsUpdate(this);
|
||||||
|
return deletedSound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -675,9 +675,20 @@ class VirtualMachine extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* Delete a sound from the current editing target.
|
* Delete a sound from the current editing target.
|
||||||
* @param {int} soundIndex - the index of the sound to be removed.
|
* @param {int} soundIndex - the index of the sound to be removed.
|
||||||
|
* @return {?Function} A function to restore the sound that was deleted,
|
||||||
|
* or null, if no sound was deleted.
|
||||||
*/
|
*/
|
||||||
deleteSound (soundIndex) {
|
deleteSound (soundIndex) {
|
||||||
this.editingTarget.deleteSound(soundIndex);
|
const target = this.editingTarget;
|
||||||
|
const deletedSound = this.editingTarget.deleteSound(soundIndex);
|
||||||
|
if (deletedSound) {
|
||||||
|
const restoreFun = () => {
|
||||||
|
target.addSound(deletedSound);
|
||||||
|
this.emitTargetsUpdate();
|
||||||
|
};
|
||||||
|
return restoreFun;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -211,8 +211,9 @@ test('deleteSound', t => {
|
||||||
const renderer = new FakeRenderer();
|
const renderer = new FakeRenderer();
|
||||||
a.renderer = renderer;
|
a.renderer = renderer;
|
||||||
|
|
||||||
a.deleteSound(0);
|
const firstDeleted = a.deleteSound(0);
|
||||||
t.deepEqual(a.sprite.sounds, [o2, o3]);
|
t.deepEqual(a.sprite.sounds, [o2, o3]);
|
||||||
|
t.deepEqual(firstDeleted, o1);
|
||||||
|
|
||||||
// Allows deleting the only sound
|
// Allows deleting the only sound
|
||||||
a.sprite.sounds = [o1];
|
a.sprite.sounds = [o1];
|
||||||
|
|
|
@ -1,9 +1,35 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
const VirtualMachine = require('../../src/virtual-machine.js');
|
const VirtualMachine = require('../../src/virtual-machine');
|
||||||
const Sprite = require('../../src/sprites/sprite.js');
|
const Sprite = require('../../src/sprites/sprite');
|
||||||
const Variable = require('../../src/engine/variable.js');
|
const Variable = require('../../src/engine/variable');
|
||||||
const adapter = require('../../src/engine/adapter');
|
const adapter = require('../../src/engine/adapter');
|
||||||
const events = require('../fixtures/events.json');
|
const events = require('../fixtures/events.json');
|
||||||
|
const Runtime = require('../../src/engine/runtime');
|
||||||
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
||||||
|
|
||||||
|
test('deleteSound returns function after deleting or null if nothing was deleted', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
const sprite = new Sprite();
|
||||||
|
sprite.sounds = [{id: 1}, {id: 2}, {id: 3}];
|
||||||
|
const rt = new Runtime();
|
||||||
|
const target = new RenderedTarget(sprite, rt);
|
||||||
|
vm.editingTarget = target;
|
||||||
|
|
||||||
|
const addFun = vm.deleteSound(1);
|
||||||
|
t.equal(sprite.sounds.length, 2);
|
||||||
|
t.equal(sprite.sounds[0].id, 1);
|
||||||
|
t.equal(sprite.sounds[1].id, 3);
|
||||||
|
t.type(addFun, 'function');
|
||||||
|
|
||||||
|
const noAddFun = vm.deleteSound(2);
|
||||||
|
t.equal(sprite.sounds.length, 2);
|
||||||
|
t.equal(sprite.sounds[0].id, 1);
|
||||||
|
t.equal(sprite.sounds[1].id, 3);
|
||||||
|
t.equal(noAddFun, null);
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
test('addSprite throws on invalid string', t => {
|
test('addSprite throws on invalid string', t => {
|
||||||
const vm = new VirtualMachine();
|
const vm = new VirtualMachine();
|
||||||
|
|
Loading…
Reference in a new issue