mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 06:52:00 -05:00
Add shareSound and shareCostume APIs with unit tests to the VM
This commit is contained in:
parent
529d271186
commit
1dcd174dea
2 changed files with 89 additions and 0 deletions
|
@ -935,6 +935,46 @@ class VirtualMachine extends EventEmitter {
|
|||
target.blocks.updateTargetSpecificBlocks(target.isStage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when costumes are dragged from editing target to another target.
|
||||
* Sets the newly added costume as the current costume.
|
||||
* @param {!number} costumeIndex Index of the costume of the editing target to share.
|
||||
* @param {!string} targetId Id of target to add the costume.
|
||||
* @return {Promise} Promise that resolves when the new costume has been loaded.
|
||||
*/
|
||||
shareCostumeToTarget (costumeIndex, targetId) {
|
||||
const originalCostume = this.editingTarget.getCostumes()[costumeIndex];
|
||||
const clone = Object.assign({}, originalCostume);
|
||||
const md5ext = `${clone.assetId}.${clone.dataFormat}`;
|
||||
return loadCostume(md5ext, clone, this.runtime).then(() => {
|
||||
const target = this.runtime.getTargetById(targetId);
|
||||
if (target) {
|
||||
target.addCostume(clone);
|
||||
target.setCostume(
|
||||
target.getCostumes().length - 1
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when sounds are dragged from editing target to another target.
|
||||
* @param {!number} soundIndex Index of the sound of the editing target to share.
|
||||
* @param {!string} targetId Id of target to add the sound.
|
||||
* @return {Promise} Promise that resolves when the new sound has been loaded.
|
||||
*/
|
||||
shareSoundToTarget (soundIndex, targetId) {
|
||||
const originalSound = this.editingTarget.getSounds()[soundIndex];
|
||||
const clone = Object.assign({}, originalSound);
|
||||
return loadSound(clone, this.runtime).then(() => {
|
||||
const target = this.runtime.getTargetById(targetId);
|
||||
if (target) {
|
||||
target.addSound(clone);
|
||||
this.emitTargetsUpdate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Repopulate the workspace with the blocks of the current editingTarget. This
|
||||
* allows us to get around bugs like gui#413.
|
||||
|
|
|
@ -369,6 +369,55 @@ test('reorderSound', t => {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('shareCostumeToTarget', t => {
|
||||
const vm = new VirtualMachine();
|
||||
const spr1 = new Sprite(null, vm.runtime);
|
||||
spr1.name = 'foo';
|
||||
const target1 = spr1.createClone();
|
||||
const costume1 = {name: 'costume1'};
|
||||
target1.addCostume(costume1);
|
||||
|
||||
const spr2 = new Sprite(null, vm.runtime);
|
||||
spr2.name = 'foo';
|
||||
const target2 = spr2.createClone();
|
||||
const costume2 = {name: 'another costume'};
|
||||
target2.addCostume(costume2);
|
||||
|
||||
vm.runtime.targets = [target1, target2];
|
||||
vm.editingTarget = vm.runtime.targets[0];
|
||||
vm.emitWorkspaceUpdate = () => null;
|
||||
|
||||
vm.shareCostumeToTarget(0, target2.id).then(() => {
|
||||
t.equal(target2.currentCostume, 1);
|
||||
t.equal(target2.getCostumes()[1].name, 'costume1');
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('shareSoundToTarget', t => {
|
||||
const vm = new VirtualMachine();
|
||||
const spr1 = new Sprite(null, vm.runtime);
|
||||
spr1.name = 'foo';
|
||||
const target1 = spr1.createClone();
|
||||
const sound1 = {name: 'sound1'};
|
||||
target1.addSound(sound1);
|
||||
|
||||
const spr2 = new Sprite(null, vm.runtime);
|
||||
spr2.name = 'foo';
|
||||
const target2 = spr2.createClone();
|
||||
const sound2 = {name: 'another sound'};
|
||||
target2.addSound(sound2);
|
||||
|
||||
vm.runtime.targets = [target1, target2];
|
||||
vm.editingTarget = vm.runtime.targets[0];
|
||||
vm.emitWorkspaceUpdate = () => null;
|
||||
|
||||
vm.shareSoundToTarget(0, target2.id).then(() => {
|
||||
t.equal(target2.getSounds()[1].name, 'sound1');
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('reorderTarget', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.emitTargetsUpdate = () => {};
|
||||
|
|
Loading…
Reference in a new issue