improve unique sprite- and costume names

pull/3/merge
jmoenig 2014-07-24 11:40:36 +02:00
rodzic 1b382458c8
commit 75d9adbfe2
3 zmienionych plików z 48 dodań i 34 usunięć

54
gui.js
Wyświetl plik

@ -69,7 +69,7 @@ SpeechBubbleMorph*/
// Global stuff ////////////////////////////////////////////////////////
modules.gui = '2014-July-18';
modules.gui = '2014-July-24';
// Declarations
@ -1011,11 +1011,13 @@ IDE_Morph.prototype.createSpriteBar = function () {
this.spriteBar.add(nameField);
nameField.drawNew();
nameField.accept = function () {
myself.currentSprite.setName(nameField.getValue());
};
this.spriteBar.reactToEdit = function () {
myself.currentSprite.setName(nameField.getValue());
var newName = nameField.getValue();
myself.currentSprite.setName(
myself.newSpriteName(newName, myself.currentSprite)
);
nameField.setContents(myself.currentSprite.name);
};
this.spriteBar.reactToEdit = nameField.accept;
// padlock
padlock = new ToggleMorph(
@ -1780,8 +1782,7 @@ IDE_Morph.prototype.addNewSprite = function () {
var sprite = new SpriteMorph(this.globalVariables),
rnd = Process.prototype.reportRandom;
sprite.name = sprite.name
+ (this.corral.frame.contents.children.length + 1);
sprite.name = this.newSpriteName(sprite.name);
sprite.setCenter(this.stage.center());
this.stage.add(sprite);
@ -1802,8 +1803,7 @@ IDE_Morph.prototype.paintNewSprite = function () {
cos = new Costume(),
myself = this;
sprite.name = sprite.name +
(this.corral.frame.contents.children.length + 1);
sprite.name = this.newSpriteName(sprite.name);
sprite.setCenter(this.stage.center());
this.stage.add(sprite);
this.sprites.add(sprite);
@ -1824,7 +1824,7 @@ IDE_Morph.prototype.paintNewSprite = function () {
IDE_Morph.prototype.duplicateSprite = function (sprite) {
var duplicate = sprite.fullCopy();
duplicate.name = sprite.name + '(2)';
duplicate.name = this.newSpriteName(sprite.name);
duplicate.setPosition(this.world().hand.position());
this.stage.add(duplicate);
duplicate.keepWithin(this.stage);
@ -1855,6 +1855,23 @@ IDE_Morph.prototype.removeSprite = function (sprite) {
this.selectSprite(this.currentSprite);
};
IDE_Morph.prototype.newSpriteName = function (name, ignoredSprite) {
var ix = name.indexOf('('),
stem = (ix < 0) ? name : name.substring(0, ix),
count = 1,
newName = stem,
all = this.sprites.asArray().filter(
function (each) {return each !== ignoredSprite; }
).map(
function (each) {return each.name; }
);
while (contains(all, newName)) {
count += 1;
newName = stem + '(' + count + ')';
}
return newName;
};
// IDE_Morph menus
IDE_Morph.prototype.userMenu = function () {
@ -5540,7 +5557,10 @@ CostumeIconMorph.prototype.renameCostume = function () {
null,
function (answer) {
if (answer && (answer !== costume.name)) {
costume.name = wardrobe.sprite.newCostumeName(answer);
costume.name = wardrobe.sprite.newCostumeName(
answer,
costume
);
costume.version = Date.now();
ide.hasChangedMedia = true;
}
@ -5555,16 +5575,8 @@ CostumeIconMorph.prototype.renameCostume = function () {
CostumeIconMorph.prototype.duplicateCostume = function () {
var wardrobe = this.parentThatIsA(WardrobeMorph),
ide = this.parentThatIsA(IDE_Morph),
newcos = this.object.copy(),
split = newcos.name.split(" ");
if (split[split.length - 1] === "copy") {
newcos.name += " 2";
} else if (isNaN(split[split.length - 1])) {
newcos.name = newcos.name + " copy";
} else {
split[split.length - 1] = Number(split[split.length - 1]) + 1;
newcos.name = split.join(" ");
}
newcos = this.object.copy();
newcos.name = wardrobe.sprite.newCostumeName(newcos.name);
wardrobe.sprite.addCostume(newcos);
wardrobe.updateList();
if (ide) {

Wyświetl plik

@ -2226,3 +2226,4 @@ ______
140724
------
* Objects: fixed “lost sprites bug” - ensure duplicated sprites keep wearing their current costume through save and re-load
* GUI, Objects: improve unique sprite- and costume names

Wyświetl plik

@ -4094,20 +4094,21 @@ SpriteMorph.prototype.reactToDropOf = function (morph, hand) {
// SpriteMorph screenshots
SpriteMorph.prototype.newCostumeName = function (name) {
function stemOf(aName) {
var ix = aName.indexOf('(');
if (ix < 0) {return aName; }
return aName.substring(0, ix);
SpriteMorph.prototype.newCostumeName = function (name, ignoredCostume) {
var ix = name.indexOf('('),
stem = (ix < 0) ? name : name.substring(0, ix),
count = 1,
newName = stem,
all = this.costumes.asArray().filter(
function (each) {return each !== ignoredCostume; }
).map(
function (each) {return each.name; }
);
while (contains(all, newName)) {
count += 1;
newName = stem + '(' + count + ')';
}
var stem = stemOf(name),
similar = this.costumes.asArray().filter(function (eachCostume) {
return stemOf(eachCostume.name) === stem;
}).length;
return stem + (similar ? '(' + (similar + 1) + ')' : '');
return newName;
};
SpriteMorph.prototype.doScreenshot = function (imgSource, data) {