From 75d9adbfe26d0303f1986f3140307d2f196c73ec Mon Sep 17 00:00:00 2001 From: jmoenig Date: Thu, 24 Jul 2014 11:40:36 +0200 Subject: improve unique sprite- and costume names --- objects.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index d378b75..2b04eb4 100644 --- a/objects.js +++ b/objects.js @@ -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) { -- cgit v1.3.1