Merge pull request #279 from nathan/rename-costume-update-refs

Fixed renaming costume breaking references to that costume
This commit is contained in:
Shane M. Clements 2014-06-18 12:57:29 +02:00
commit 111dbeac8e
2 changed files with 40 additions and 7 deletions

View file

@ -665,6 +665,19 @@ public class ScratchRuntime {
// Script utilities
//------------------------------
public function renameCostume(newName:String):void {
var obj:ScratchObj = app.viewedObj();
var costume:ScratchCostume = obj.currentCostume();
var oldName:String = costume.costumeName;
if (obj.isCostumeNameUsed(newName)) return;
costume.costumeName = newName;
var uses:Array = obj.isStage ? allUsesOfBackdrop(oldName) : allUsesOfCostume(oldName);
for each (var a:BlockArg in uses) {
a.setArgValue(newName);
}
app.setSaveNeeded();
}
public function clearRunFeedback():void {
if(app.editMode) {
for each (var stack:Block in allStacks()) {
@ -721,6 +734,30 @@ public class ScratchRuntime {
return false;
}
public function allUsesOfBackdrop(backdropName:String):Array {
var result:Array = [];
allStacksAndOwnersDo(function (stack:Block, target:ScratchObj):void {
stack.allBlocksDo(function (b:Block):void {
for each (var a:BlockArg in b.args) {
if (a.menuName == 'backdrop' && a.argValue == backdropName) result.push(a);
}
});
});
return result;
}
public function allUsesOfCostume(costumeName:String):Array {
var result:Array = [];
for each (var stack:Block in app.viewedObj().scripts) {
stack.allBlocksDo(function (b:Block):void {
for each (var a:BlockArg in b.args) {
if (a.menuName == 'costume' && a.argValue == costumeName) result.push(a);
}
});
}
return result;
}
public function allUsesOfVariable(varName:String, owner:ScratchObj):Array {
var variableBlocks:Array = [Specs.SET_VAR, Specs.CHANGE_VAR, "showVariable:", "hideVariable:"];
var result:Array = [];

View file

@ -200,14 +200,10 @@ public class ImagesPart extends UIPart {
}
private function nameChanged():void {
var obj:ScratchObj = app.viewedObj();
if(!obj.isCostumeNameUsed(nameField.contents())) {
obj.currentCostume().costumeName = nameField.contents();
app.runtime.renameCostume(nameField.contents());
nameField.setContents(app.viewedObj().currentCostume().costumeName);
(listFrame.contents as MediaPane).refresh();
}
else
nameField.setContents(obj.currentCostume().costumeName);
}
private function addNewCostumeButtons():void {
var left:int = 8;