mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 23:07:12 -05:00
Move renaming and adding into rendered-target with tests
This commit is contained in:
parent
ef16a7d574
commit
f96a92b4d1
4 changed files with 89 additions and 48 deletions
|
@ -1,5 +1,6 @@
|
||||||
const log = require('../util/log');
|
const log = require('../util/log');
|
||||||
const MathUtil = require('../util/math-util');
|
const MathUtil = require('../util/math-util');
|
||||||
|
const StringUtil = require('../util/string-util');
|
||||||
const Target = require('../engine/target');
|
const Target = require('../engine/target');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -391,6 +392,28 @@ class RenderedTarget extends Target {
|
||||||
this.runtime.requestTargetsUpdate(this);
|
this.runtime.requestTargetsUpdate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a costume, taking care to avoid duplicate names.
|
||||||
|
* @param {!object} costumeObject Object representing the costume.
|
||||||
|
*/
|
||||||
|
addCostume (costumeObject) {
|
||||||
|
const usedNames = this.sprite.costumes.map(costume => costume.name);
|
||||||
|
costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames);
|
||||||
|
this.sprite.costumes.push(costumeObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a costume, taking care to avoid duplicate names.
|
||||||
|
* @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.sprite.costumes
|
||||||
|
.filter((costume, index) => costumeIndex !== index)
|
||||||
|
.map(costume => costume.name);
|
||||||
|
this.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a costume by index.
|
* Delete a costume by index.
|
||||||
* @param {number} index Costume index to be deleted
|
* @param {number} index Costume index to be deleted
|
||||||
|
@ -414,6 +437,28 @@ class RenderedTarget extends Target {
|
||||||
this.runtime.requestTargetsUpdate(this);
|
this.runtime.requestTargetsUpdate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a sound, taking care to avoid duplicate names.
|
||||||
|
* @param {!object} soundObject Object representing the sound.
|
||||||
|
*/
|
||||||
|
addSound (soundObject) {
|
||||||
|
const usedNames = this.sprite.sounds.map(sound => sound.name);
|
||||||
|
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames);
|
||||||
|
this.sprite.sounds.push(soundObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a sound, taking care to avoid duplicate names.
|
||||||
|
* @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.sprite.sounds
|
||||||
|
.filter((sound, index) => soundIndex !== index)
|
||||||
|
.map(sound => sound.name);
|
||||||
|
this.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
|
|
@ -280,7 +280,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
addCostume (md5ext, costumeObject) {
|
addCostume (md5ext, costumeObject) {
|
||||||
loadCostume(md5ext, costumeObject, this.runtime).then(() => {
|
loadCostume(md5ext, costumeObject, this.runtime).then(() => {
|
||||||
this.editingTarget.sprite.costumes.push(costumeObject);
|
this.editingTarget.addCostume(costumeObject);
|
||||||
this.editingTarget.setCostume(
|
this.editingTarget.setCostume(
|
||||||
this.editingTarget.sprite.costumes.length - 1
|
this.editingTarget.sprite.costumes.length - 1
|
||||||
);
|
);
|
||||||
|
@ -293,10 +293,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
* @param {string} newName - the desired new name of the costume (will be modified if already in use).
|
* @param {string} newName - the desired new name of the costume (will be modified if already in use).
|
||||||
*/
|
*/
|
||||||
renameCostume (costumeIndex, newName) {
|
renameCostume (costumeIndex, newName) {
|
||||||
const usedNames = this.editingTarget.sprite.costumes
|
this.editingTarget.renameCostume(costumeIndex, newName);
|
||||||
.filter((costume, index) => costumeIndex !== index)
|
|
||||||
.map(costume => costume.name);
|
|
||||||
this.editingTarget.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames);
|
|
||||||
this.emitTargetsUpdate();
|
this.emitTargetsUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +312,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
addSound (soundObject) {
|
addSound (soundObject) {
|
||||||
return loadSound(soundObject, this.runtime).then(() => {
|
return loadSound(soundObject, this.runtime).then(() => {
|
||||||
this.editingTarget.sprite.sounds.push(soundObject);
|
this.editingTarget.addSound(soundObject);
|
||||||
this.emitTargetsUpdate();
|
this.emitTargetsUpdate();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -326,10 +323,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
* @param {string} newName - the desired new name of the sound (will be modified if already in use).
|
* @param {string} newName - the desired new name of the sound (will be modified if already in use).
|
||||||
*/
|
*/
|
||||||
renameSound (soundIndex, newName) {
|
renameSound (soundIndex, newName) {
|
||||||
const usedNames = this.editingTarget.sprite.sounds
|
this.editingTarget.renameSound(soundIndex, newName);
|
||||||
.filter((sound, index) => soundIndex !== index)
|
|
||||||
.map(sound => sound.name);
|
|
||||||
this.editingTarget.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames);
|
|
||||||
this.emitTargetsUpdate();
|
this.emitTargetsUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -355,3 +355,43 @@ test('#toJSON returns the sounds and costumes', t => {
|
||||||
t.same(a.toJSON().costumes, costumes);
|
t.same(a.toJSON().costumes, costumes);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('#addSound does not duplicate names', t => {
|
||||||
|
const spr = new Sprite();
|
||||||
|
const a = new RenderedTarget(spr, null);
|
||||||
|
a.sprite.sounds = [{name: 'first'}];
|
||||||
|
a.addSound({name: 'first'});
|
||||||
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'first2'}]);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('#addCostume does not duplicate names', t => {
|
||||||
|
const spr = new Sprite();
|
||||||
|
const a = new RenderedTarget(spr, null);
|
||||||
|
a.sprite.costumes = [{name: 'first'}];
|
||||||
|
a.addCostume({name: 'first'});
|
||||||
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'first2'}]);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('#renameSound does not duplicate names', t => {
|
||||||
|
const spr = new Sprite();
|
||||||
|
const a = new RenderedTarget(spr, null);
|
||||||
|
a.sprite.sounds = [{name: 'first'}, {name: 'second'}];
|
||||||
|
a.renameSound(0, 'first'); // Shouldn't increment the name, noop
|
||||||
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'second'}]);
|
||||||
|
a.renameSound(1, 'first');
|
||||||
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'first2'}]);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('#renameCostume does not duplicate names', t => {
|
||||||
|
const spr = new Sprite();
|
||||||
|
const a = new RenderedTarget(spr, null);
|
||||||
|
a.sprite.costumes = [{name: 'first'}, {name: 'second'}];
|
||||||
|
a.renameCostume(0, 'first'); // Shouldn't increment the name, noop
|
||||||
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'second'}]);
|
||||||
|
a.renameCostume(1, 'first');
|
||||||
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'first2'}]);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
|
@ -109,41 +109,3 @@ 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