simplify unique stageshot names

and get rid of regex
pull/3/merge
jmoenig 2014-07-17 14:39:37 +02:00
rodzic 7472261ca6
commit f38fb73cd3
1 zmienionych plików z 11 dodań i 30 usunięć

Wyświetl plik

@ -4086,39 +4086,20 @@ SpriteMorph.prototype.reactToDropOf = function (morph, hand) {
// SpriteMorph screenshots // SpriteMorph screenshots
SpriteMorph.prototype.re = /\((\d+)\)$/; // RegExp to match costume names
SpriteMorph.prototype.newCostumeName = function (name) { SpriteMorph.prototype.newCostumeName = function (name) {
var foundSameName = false,
foundIndex = false,
lastIndex = 0,
i = 1,
p = null,
costume = null;
for (i = 1; i <= this.costumes.length(); i += 1) { function stemOf(aName) {
costume = this.costumes.at(i); var ix = aName.indexOf('(');
if (costume !== null) { if (ix < 0) {return aName; }
if (costume.name === name) { return aName.substring(0, ix);
foundSameName = true;
}
if (foundSameName) {
p = this.re.exec(costume.name);
if (p) {
lastIndex = Number(p[1]);
foundIndex = true;
}
}
}
} }
if (foundSameName) {
if (foundIndex) { var stem = stemOf(name),
lastIndex += 1; similar = this.costumes.asArray().filter(function (eachCostume) {
return name + '(' + lastIndex + ')'; // New index with a +1 return stemOf(eachCostume.name) === stem;
} }).length;
return name + '(2)'; // start off indexing with (1)
} return stem + (similar ? '(' + (similar + 1) + ')' : '');
return name;
}; };
SpriteMorph.prototype.doScreenshot = function (imgSource, data) { SpriteMorph.prototype.doScreenshot = function (imgSource, data) {