Fixed orphaned comment bug

Comments are now deleted and restored with the blocks to which they are attached.
This commit is contained in:
Nathan Dinsmore 2014-06-17 11:43:31 -04:00
parent 5132c96eb2
commit 28bbaf9415
2 changed files with 39 additions and 6 deletions

View file

@ -43,7 +43,7 @@ import flash.display.*;
import translation.Translator;
import util.*;
import uiwidgets.*;
import scratch.ScratchStage;
import scratch.*;
public class Block extends Sprite {
@ -765,6 +765,22 @@ public class Block extends Sprite {
Scratch.app.runtime.checkForGraphicEffects();
}
public function attachedCommentsIn(scriptsPane:ScriptsPane):Array {
var allBlocks:Array = [];
allBlocksDo(function (b:Block):void {
allBlocks.push(b);
});
var result:Array = []
if (!scriptsPane) return result;
for (var i:int = 0; i < scriptsPane.numChildren; i++) {
var c:ScratchComment = scriptsPane.getChildAt(i) as ScratchComment;
if (c && c.blockRef && allBlocks.indexOf(c.blockRef) != -1) {
result.push(c);
}
}
return result;
}
public function addComment():void {
var scriptsPane:ScriptsPane = topBlock().parent as ScriptsPane;
if (scriptsPane) scriptsPane.addComment(this);

View file

@ -941,6 +941,16 @@ public class ScratchRuntime {
public function clearLastDelete():void { lastDelete = null }
public function recordForUndelete(obj:*, x:int, y:int, index:int, owner:* = null):void {
if (obj is Block) {
var comments:Array = (obj as Block).attachedCommentsIn(app.scriptsPane);
if (comments.length) {
for each (var c:ScratchComment in comments) {
c.parent.removeChild(c);
}
app.scriptsPane.fixCommentLayout();
obj = [obj, comments];
}
}
lastDelete = [obj, x, y, index, owner];
}
@ -966,13 +976,20 @@ public class ScratchRuntime {
app.addNewSprite(obj);
obj.setScratchXY(x, y);
app.selectSprite(obj);
} else if ((obj is Block) || (obj is ScratchComment)) {
} else if ((obj is Array) || (obj is Block) || (obj is ScratchComment)) {
app.selectSprite(prevOwner);
app.setTab('scripts');
obj.x = x;
obj.y = y;
if (obj is Block) obj.cacheAsBitmap = true;
app.scriptsPane.addChild(obj);
var b:DisplayObject = obj is Array ? obj[0] : obj;
b.x = x;
b.y = y;
if (b is Block) b.cacheAsBitmap = true;
app.scriptsPane.addChild(b);
if (obj is Array) {
for each (var c:ScratchComment in obj[1]) {
app.scriptsPane.addChild(c);
}
app.scriptsPane.fixCommentLayout();
}
}
}