From 28bbaf9415a806af03334fcb311ea3dbd32dda98 Mon Sep 17 00:00:00 2001 From: Nathan Dinsmore Date: Tue, 17 Jun 2014 11:43:31 -0400 Subject: [PATCH] Fixed orphaned comment bug Comments are now deleted and restored with the blocks to which they are attached. --- src/blocks/Block.as | 18 +++++++++++++++++- src/scratch/ScratchRuntime.as | 27 ++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/blocks/Block.as b/src/blocks/Block.as index 1986d32..6046676 100644 --- a/src/blocks/Block.as +++ b/src/blocks/Block.as @@ -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); diff --git a/src/scratch/ScratchRuntime.as b/src/scratch/ScratchRuntime.as index 47c4c23..9a8d3db 100644 --- a/src/scratch/ScratchRuntime.as +++ b/src/scratch/ScratchRuntime.as @@ -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(); + } } }