Fixed scripts running after they were deleted and added delete item to procedure hat context menu

This commit is contained in:
Nathan Dinsmore 2014-06-16 13:02:19 -04:00
parent 5132c96eb2
commit 4f89b18d5a
3 changed files with 22 additions and 30 deletions

View file

@ -748,21 +748,32 @@ public class Block extends Sprite {
Scratch.app.gh.grabOnMouseUp(newStack);
}
public function deleteStack():void {
if (isProcDef() || isEmbeddedInProcHat()) return; // don't delete procedure definition this way for now
if (parent == null) return;
public function deleteStack():Boolean {
if (op == 'proc_declaration') {
return (parent as Block).deleteStack();
}
var app:Scratch = Scratch.app;
var top:Block = topBlock();
if (op == Specs.PROCEDURE_DEF && app.runtime.allCallsOf(spec, app.viewedObj()).length) {
DialogBox.notify('Cannot Delete', 'To delete a block definition, first remove all uses of the block.', stage);
return false;
}
if (top == this && app.interp.isRunning(top, app.viewedObj())) {
app.interp.toggleThread(top, app.viewedObj());
}
// TODO: Remove any waiting reporter data in the Scratch.app.extensionManager
if (parent is Block) Block(parent).removeBlock(this);
else parent.removeChild(this);
else if (parent) parent.removeChild(this);
this.cacheAsBitmap = false;
// set position for undelete
x = top.x;
y = top.y;
if (top != this) x += top.width + 5;
Scratch.app.runtime.recordForUndelete(this, x, y, 0, Scratch.app.viewedObj());
Scratch.app.scriptsPane.saveScripts();
Scratch.app.runtime.checkForGraphicEffects();
app.runtime.recordForUndelete(this, x, y, 0, app.viewedObj());
app.scriptsPane.saveScripts();
app.runtime.checkForGraphicEffects();
app.updatePalette();
return true;
}
public function addComment():void {

View file

@ -399,7 +399,7 @@ public class BlockMenus implements DragClient {
m.addItem('record...', recordSound);
showMenu(m);
}
private function recordSound():void {
app.setTab('sounds');
app.soundsPart.recordSound();
@ -514,9 +514,9 @@ public class BlockMenus implements DragClient {
if (!isInPalette(block)) {
if (!block.isProcDef()) {
m.addItem('duplicate', duplicateStack);
m.addItem('delete', block.deleteStack);
m.addLine();
}
m.addItem('delete', block.deleteStack);
m.addLine();
m.addItem('add comment', block.addComment);
}
m.addItem('help', block.showHelp);

View file

@ -62,31 +62,12 @@ public class BlockPalette extends ScrollFrameContents {
}
var b:Block = obj as Block;
if (b) {
if ((b.op == Specs.PROCEDURE_DEF) && hasCallers(b, app)) {
DialogBox.notify('Cannot Delete', 'To delete a block definition, first remove all uses of the block.', stage);
return false;
}
if (b.parent) b.parent.removeChild(b);
b.restoreOriginalPosition(); // restore position in case block is undeleted
Scratch.app.runtime.recordForUndelete(b, b.x, b.y, 0, Scratch.app.viewedObj());
app.scriptsPane.saveScripts();
app.updatePalette();
return true;
return b.deleteStack();
}
return false;
}
private function hasCallers(def:Block, app:Scratch):Boolean {
var callCount:int;
for each (var stack:Block in app.viewedObj().scripts) {
// for each block in stack
stack.allBlocksDo(function (b:Block):void {
if ((b.op == Specs.CALL) && (b.spec == def.spec)) callCount++;
});
}
return callCount > 0;
}
public static function strings():Array {
return ['Cannot Delete', 'To delete a block definition, first remove all uses of the block.'];
}