mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Merge pull request #634 from paulkaplan/sound-costume-renaming
Add public API for costume and sound renaming
This commit is contained in:
commit
eb04fcab57
2 changed files with 64 additions and 0 deletions
|
@ -287,6 +287,19 @@ class VirtualMachine extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a costume on the current editing target.
|
||||||
|
* @param {int} costumeIndex - the index of the costume to be renamed.
|
||||||
|
* @param {string} newName - the desired new name of the costume (will be modified if already in use).
|
||||||
|
*/
|
||||||
|
renameCostume (costumeIndex, newName) {
|
||||||
|
const usedNames = this.editingTarget.sprite.costumes
|
||||||
|
.filter((costume, index) => costumeIndex !== index)
|
||||||
|
.map(costume => costume.name);
|
||||||
|
this.editingTarget.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||||
|
this.emitTargetsUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a costume from the current editing target.
|
* Delete a costume from the current editing target.
|
||||||
* @param {int} costumeIndex - the index of the costume to be removed.
|
* @param {int} costumeIndex - the index of the costume to be removed.
|
||||||
|
@ -307,6 +320,19 @@ class VirtualMachine extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a sound on the current editing target.
|
||||||
|
* @param {int} soundIndex - the index of the sound to be renamed.
|
||||||
|
* @param {string} newName - the desired new name of the sound (will be modified if already in use).
|
||||||
|
*/
|
||||||
|
renameSound (soundIndex, newName) {
|
||||||
|
const usedNames = this.editingTarget.sprite.sounds
|
||||||
|
.filter((sound, index) => soundIndex !== index)
|
||||||
|
.map(sound => sound.name);
|
||||||
|
this.editingTarget.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||||
|
this.emitTargetsUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
|
@ -109,3 +109,41 @@ test('renameSprite does not increment when renaming to the same name', t => {
|
||||||
t.equal(vm.runtime.targets[0].sprite.name, 'this name');
|
t.equal(vm.runtime.targets[0].sprite.name, 'this name');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('renameSound sets the sound name', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
vm.editingTarget = {
|
||||||
|
sprite: {
|
||||||
|
sounds: [{name: 'first'}, {name: 'second'}]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
vm.renameSound(0, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.sounds[0].name, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.sounds[1].name, 'second');
|
||||||
|
// Make sure renaming to same name doesn't increment
|
||||||
|
vm.renameSound(0, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.sounds[0].name, 'hello');
|
||||||
|
// But renaming to used name does increment
|
||||||
|
vm.renameSound(1, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.sounds[1].name, 'hello2');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('renameCostume sets the costume name', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
vm.editingTarget = {
|
||||||
|
sprite: {
|
||||||
|
costumes: [{name: 'first'}, {name: 'second'}]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
vm.renameCostume(0, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.costumes[0].name, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.costumes[1].name, 'second');
|
||||||
|
// Make sure renaming to same name doesn't increment
|
||||||
|
vm.renameCostume(0, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.costumes[0].name, 'hello');
|
||||||
|
// But renaming to used name does increment
|
||||||
|
vm.renameCostume(1, 'hello');
|
||||||
|
t.equal(vm.editingTarget.sprite.costumes[1].name, 'hello2');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue