diff options
| author | jmoenig <jens@moenig.org> | 2014-07-24 11:40:36 +0200 |
|---|---|---|
| committer | jmoenig <jens@moenig.org> | 2014-07-24 11:40:36 +0200 |
| commit | 75d9adbfe26d0303f1986f3140307d2f196c73ec (patch) | |
| tree | 6c395812ce78d5a3039e2a3f125515000a6de86e /gui.js | |
| parent | 1b382458c8208cb74b51cad414e67a71be61e0e2 (diff) | |
| download | snap-byow-75d9adbfe26d0303f1986f3140307d2f196c73ec.tar.gz snap-byow-75d9adbfe26d0303f1986f3140307d2f196c73ec.zip | |
improve unique sprite- and costume names
Diffstat (limited to 'gui.js')
| -rw-r--r-- | gui.js | 54 |
1 files changed, 33 insertions, 21 deletions
@@ -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) { |
