Add sprite name incrementing and reserved naming

This commit is contained in:
Paul Kaplan 2017-03-20 12:52:57 -04:00
parent 052ecef91c
commit 8bbb395b35
3 changed files with 46 additions and 4 deletions

View file

@ -3,6 +3,9 @@ var util = require('util');
var Runtime = require('./engine/runtime');
var sb2import = require('./import/sb2import');
var StringUtil = require('./util/string-util');
var RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
/**
* Handles connections between blocks, stage, and extensions.
@ -204,8 +207,14 @@ VirtualMachine.prototype.renameSprite = function (targetId, newName) {
if (!sprite) {
throw new Error('No sprite associated with this target.');
}
if (newName) {
sprite.name = newName;
if (newName && RESERVED_NAMES.indexOf(newName) === -1) {
var names = this.runtime.targets.filter(function (runtimeTarget) {
return runtimeTarget.isSprite();
}).map(function (runtimeTarget) {
return runtimeTarget.sprite.name;
});
sprite.name = StringUtil.unusedName(newName, names);
}
this.emitTargetsUpdate();
} else {

View file

@ -26,7 +26,7 @@ test('complex', function (t) {
var targets = data.targetList;
for (var i in targets) {
if (targets[i].isStage === true) continue;
if (targets[i].name === 'test') continue;
if (targets[i].name.match(/test/)) continue;
vm.setEditingTarget(targets[i].id);
vm.renameSprite(targets[i].id, 'test');

View file

@ -50,7 +50,7 @@ test('renameSprite sets the sprite name', function (t) {
t.end();
});
test('renameSprite does not set sprite names to an empty string ', function (t) {
test('renameSprite does not set sprite names to an empty string', function (t) {
var vm = new VirtualMachine();
var fakeTarget = {
sprite: {name: 'original'},
@ -61,3 +61,36 @@ test('renameSprite does not set sprite names to an empty string ', function (t)
t.equal(fakeTarget.sprite.name, 'original');
t.end();
});
test('renameSprite does not set sprite names to reserved names', function (t) {
var vm = new VirtualMachine();
var fakeTarget = {
sprite: {name: 'original'},
isSprite: () => true
};
vm.runtime.getTargetById = () => (fakeTarget);
vm.renameSprite('id', '_mouse_');
t.equal(fakeTarget.sprite.name, 'original');
t.end();
});
test('renameSprite increments from existing sprite names', function (t) {
var vm = new VirtualMachine();
vm.emitTargetsUpdate = () => {};
vm.runtime.targets = [{
id: 'id1',
isSprite: () => true,
sprite: {
name: 'this name'
}
}, {
id: 'id2',
isSprite: () => true,
sprite: {
name: 'that name'
}
}];
vm.renameSprite('id1', 'that name');
t.equal(vm.runtime.targets[0].sprite.name, 'that name2');
t.end();
});