package{ import flash.display.Sprite; import flash.display.Shape; import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextLineMetrics; import flash.text.TextFieldAutoSize; import fl.containers.ScrollPane; // don't forget to add ScrollPane component to the library public class DOM2Sprite extends Sprite{ // formating information var fmt:TextFormat = new TextFormat("Arial",10); var boldFmt:TextFormat = new TextFormat(null,null,null,true); var italicFmt:TextFormat = new TextFormat(null,null,null,null,true); var blueFmt:TextFormat = new TextFormat(null,null,0x0000FF); function DOM2Sprite(node:XML,posX:int=0,posY:int=0) { //trace("DOM2Sprite("+node.toXMLString()+","+posX+","+posY+")"); var type:String = node.nodeKind(); x=posX; y=posY; addEventListener(MouseEvent.MOUSE_DOWN,zoomIn); addEventListener(MouseEvent.MOUSE_UP,zoomOut); // create a text Node for the content var outT:TextField = new TextField(); with (outT){ defaultTextFormat = fmt; autoSize=TextFieldAutoSize.LEFT; background=true; } // made the text node a child of the Sprite addChild(outT); // add content to the text node switch (type) { case "element": // add node name outT.appendText(node.localName()+":"); outT.setTextFormat(boldFmt,0,outT.length-1); // keep width for indenting the children var nameWidth:int=outT.width; // add text for attributes var attributes:XMLList = node.@*; for (var i:int=0;i0?outT.height:0; var nbChild:int=attributes.length(); var childHeights=0; // create sprites for children for each (var child:XML in node.children()){ var s:Sprite = new DOM2Sprite(child,nameWidth,txtHeight+childHeights); childHeights+=s.height; addChild(s); } break; case "text": outT.appendText(node[0].toString().replace(/ *\n */g,' ')); outT.setTextFormat(blueFmt); } // wrap the whole Sprite with a filled white rectangle as background // that will hide parts underneath during the zoom var shp:Shape = new Shape(); shp.graphics.beginFill(0xFFFFFF); shp.graphics.drawRect(0,0,width-1,height-1); shp.graphics.endFill(); // add the filled rectangle as background addChildAt(shp,0); } function zoomIn(event:MouseEvent){ //trace(name+".zoomIn"+event.target+","+event.currentTarget); // put target and its ancestors in the front of the display list at each level var dispObj:DisplayObject = DisplayObject(event.currentTarget); var container:DisplayObjectContainer = dispObj.parent; while(container is Sprite){ container.addChild(dispObj); dispObj=container; container=container.parent; //trace("container="+container); } // scale up the current target progressively incrementalZoom(Sprite(event.currentTarget),2.0,12); event.stopPropagation(); } function zoomOut(event:MouseEvent){ // scale down faster incrementalZoom(Sprite(event.currentTarget),1.0,4); event.stopPropagation(); } // change scaleX and scaleY of sprite s upto "to" in "steps" steps function incrementalZoom(s:Sprite,to:Number,steps:int){ var ds:Number=(to-s.scaleX)/steps; function scale (event:Event){ // internal function called at each step //trace("scale:"+s.name+":"+s.scaleX+":"+ds+":"+to); if(ds<0?(s.scaleX+ds<=to):(s.scaleX+ds>=to)){ s.removeEventListener(Event.ENTER_FRAME,scale); // remove itself s.scaleX=s.scaleY=to; } else s.scaleX=s.scaleY+=ds; } s.addEventListener(Event.ENTER_FRAME,scale); } } }