JAVAC.JP
HOSHI TETSUYA ―“S–ξ
2005/07/25
hossy@javac.jp
http://www.javac.jp
Google
WWW ‚πŒŸυ WWW.JAVAC.JP ‚πŒŸυ

A-5 ‰Ϋ‘θ5 - ‰π“š

‰Ϋ‘θ(5)
main.as
---
var graph:GraphControl=new GraphControl(this);
stop();

ExXML.as
---
class ExXML extends XML{
    private var ignoreWhite=true
    var addEventListener:Function;
    var removeEventListener:Function;
    private var dispatchEvent:Function;
}

OvalClass.as
---
import mx.events.EventDispatcher;

class OvalClass extends MovieClip{
    private var index;
    private var SCREEN_WIDTH:Number=640;
    private var SCREEN_HEIGHT:Number=480;
    private var CENTER_X:Number=320;
    private var CENTER_Y:Number=240;
    private var graphControl:GraphControl;
    private var mouseListener:MovieClip;
    private var MOUSE_LISTENER_DEPTH:Number=999;
    private var mcLoader:MovieClipLoader;
    private var seed:MovieClip;
    private var mcListener:Object;

    function OvalClass(){
    }

    public function setIndex(index:Number):Void{
        this.index=index;
    }

    public function setGraphControl(graphControl:GraphControl):Void{
        this.graphControl=graphControl;
    }

    private function onPress():Void{
        this.startDrag(false,-CENTER_X+_width/2,-CENTER_Y+_height/2
        ,SCREEN_WIDTH-CENTER_X-_width/2,SCREEN_HEIGHT-CENTER_Y-_height/2);
        addMouseListener();
    }

    private function onRelease():Void{
        this.stopDrag();
        removeMouseListener();
    }

    private function onReleaseOutside():Void{
        onRelease();
    }

    private function addMouseListener():Void{
        this.createEmptyMovieClip("mouseListener",MOUSE_LISTENER_DEPTH);
        EventDispatcher.initialize(mouseListener);
        mouseListener.onMouseMove=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onMouseMoveHandler";
            this.dispatchEvent(lo);
        }
        mouseListener.addEventListener("onMouseMoveHandler",this);
    }

    private function removeMouseListener():Void{
        mouseListener.removeEventListener("onMouseMoveHandler",this);
        mouseListener.removeMovieClip();
    }

    private function onMouseMoveHandler():Void{
        var x:Number=_x;
        var y:Number=_y;
        graphControl.setPosition(index,x,y);
        graphControl.refreshLines(index);
    }

    public function loadImage(url:String):Void{
        mcLoader=new MovieClipLoader();
        trace("loadStart:"+url);
        this.mcListener=new Object();
        var lo:Object=this.mcListener;
        EventDispatcher.initialize(lo);
        lo.onLoadInit=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="centering";
            this.dispatchEvent(lo);
        }
        lo.onLoadError=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="printOut";
            lo.str="Load Error.";
            this.dispatchEvent(lo);
        }
        lo.onLoadProgress=function(target_mc:MovieClip
                    ,loadedBytes:Number,totalBytes:Number){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="printOut";
            lo.str="Now Loading."+":"+String(loadedBytes)+"/"+String(totalBytes);
            this.dispatchEvent(lo);
        }
        lo.addEventListener("centering",this);
        lo.addEventListener("printOut",this);
        mcLoader.addListener(lo);
        this.createEmptyMovieClip("seed",1);
        mcLoader.loadClip(url,this.seed);
    }

    private function printOut(eventObj):Void{
        var str:String=String(eventObj.str);
        trace(str);
    }

    private function centering():Void{
        trace("loadEnd");
        this.seed._width=50;
        this.seed._height=50;
        this.seed._x=-this.seed._width/2;
        this.seed._y=-this.seed._height/2;
    }
}

LineMcDataStorage.as
---
class LineMcDataStorage{
    private var lineMcList:Array;

    function LineMcDataStorage(){
        lineMcList=new Array();
    }

    public function getLineMcLength():Number{
        return lineMcList.length;
    }

    public function addLineMcList(index:Number,mc:MovieClip):Void{
        lineMcList.splice(index,0,Object(mc));
    }

    public function removeLineMcList(index:Number):Void{
        var mc:MovieClip=MovieClip(lineMcList[index]);
        mc.removeMovieClip();
        lineMcList.splice(index,1);
    }

    public function getLineMc(index:Number):MovieClip{
        return MovieClip(lineMcList[index]);
    }
}

GraphDataStorage.as
---
class GraphDataStorage{
    private var pointList:Array;
    private var lineList:Array;
    private var imageList:Array;

    function GraphDataStorage(){
        reset();
    }

    public function reset():Void{
        pointList=new Array();
        lineList=new Array();
        imageList=new Array();
    }

    public function getPointListLength():Number{
        return pointList.length;
    }

    public function getLineListLength():Number{
        return lineList.length;
    }

    public function getImageListLength():Number{
        return imageList.length;
    }

    public function addPoint(id:String,x:Number,y:Number,image:String):Number{
        var o:Object=new Object();
        o.id=id;
        o.x=x;
        o.y=y;
        o.image=image;
        o.connecter=new Array();
        pointList.push(o);
        return getPointListLength()-1;
    }

    public function addLine(id:String,point1:String,point2:String):Number{
        var o:Object=new Object();
        o.id=id;
        o.point1=point1;
        o.point2=point2;
        lineList.push(o);
        return getLineListLength()-1;
    }

    public function addImage(id:String,url:String):Void{
        var o:Object=new Object();
        o.id=id;
        o.url=url;
        imageList.push(o);
    }

    private function addPointIndexToLine(index:Number):Void{
        var point1:String=String(lineList[index].point1);
        var point2:String=String(lineList[index].point2);
        var pointIndex1:Number=getPointIndexFromId(point1);
        var pointIndex2:Number=getPointIndexFromId(point2);
        lineList[index].index1=pointIndex1;
        lineList[index].index2=pointIndex2;
        addConnecter(pointIndex1,index);
        addConnecter(pointIndex2,index);
    }

    public function addPointIndexToLineLists():Void{
        var len:Number=getLineListLength();
        for(var i:Number=0;i<len;i++){
            addPointIndexToLine(i);
        }
    }

    private function addConnecter(pointIndex:Number,lineIndex:Number):Void{
        pointList[pointIndex].connecter.push(lineIndex);
    }

    private function getPointIndexFromId(id:String):Number{
        var ret:Number;
        for(var i:Number=0;i<pointList.length;i++){
            var targetId:String=String(pointList[i].id);
            if(id==targetId){
                ret=i;
                break;
            }
        }
        return ret;
    }

    public function getPointId(index:Number):String{
        return String(pointList[index].id);
    }

    public function getPointPosX(index:Number):Number{
        return Number(pointList[index].x);
    }

    public function getPointPosY(index:Number):Number{
        return Number(pointList[index].y);
    }

    public function getPointImage(index:Number):String{
        return String(pointList[index].image);
    }

    public function getConnecterLength(index:Number):Number{
        return Number(pointList[index].connecter.length);
    }

    public function getConnecter(pointIndex:Number,index:Number):Number{
        return Number(pointList[pointIndex].connecter[index]);
    }

    public function getLineId(index:Number):String{
        return String(lineList[index].id);
    }

    public function getLineIndex1(index:Number):Number{
        return Number(lineList[index].index1);
    }

    public function getLineIndex2(index:Number):Number{
        return Number(lineList[index].index2);
    }

    public function setPointPosition(index:Number,x:Number,y:Number):Void{
        pointList[index].x=x;
        pointList[index].y=y;
    }

    private function getImageId(index:Number):String{
        return String(imageList[index].id);
    }

    private function getImageUrl(index:Number):String{
        return String(imageList[index].url);
    }

    public function getImageFromId(id:String):String{
        var ret:String;
        for(var i:Number=0;i<getImageListLength();i++){
            trace(getImageId(i)+":"+id);
            if(getImageId(i)==id){
                ret=getImageUrl(i);
            }
        }
        return ret;
    }
}

DrawingControl.as
---
class DrawingControl{
    private var path:MovieClip;
    private var ovalBase:MovieClip;
    private var lineBase:MovieClip;
    private var ovalDepth:Number;
    private var lineDepth:Number;
    private var OVAL_BASE_DEPTH:Number=500;
    private var LINE_BASE_DEPTH:Number=300;
    private var OVAL_START_DEPTH:Number=1;
    private var LINE_START_DEPTH:Number=1;
    private var lineMcList:LineMcDataStorage;

    function DrawingControl(path:MovieClip){
        this.path=path;
        path.createEmptyMovieClip("oval_base",OVAL_BASE_DEPTH);
        ovalBase=path.oval_base;
        path.createEmptyMovieClip("line_base",LINE_BASE_DEPTH);
        lineBase=path.line_base;
        ovalDepth=OVAL_START_DEPTH;
        lineDepth=LINE_START_DEPTH;
        lineMcList=new LineMcDataStorage();
    }

    public function drawOval(x:Number,y:Number):MovieClip{
        var depth:Number=ovalDepth;
        ovalDepth++;
        var linkage:String="oval";
        var mcname:String="oval"+String(depth);
        ovalBase.attachMovie(linkage,mcname,depth);
        var mc:MovieClip=ovalBase[mcname];
        mc._x=x;
        mc._y=y;
        return mc;
    }

    public function drawLine(x1:Number,y1:Number,x2:Number,y2:Number
                        ,size:Number,color16:Number,index:Number):Void{
        var depth:Number=lineDepth;
        lineDepth++;
        var mcname:String="line"+depth;
        lineBase.createEmptyMovieClip(mcname,depth);
        var mc:MovieClip=lineBase[mcname];
        mc.lineStyle(size,color16);
        mc.moveTo(x1,y1);
        mc.lineTo(x2,y2);
        lineMcList.addLineMcList(index,mc);
    }

    public function removeLine(index:Number):Void{
        lineMcList.removeLineMcList(index);
    }

    public function removeAllLines():Void{
        lineBase.removeMovieClip();
        path.createEmptyMovieClip("line_base",LINE_BASE_DEPTH);
        lineBase=path.line_base;
    }
}

GraphControl.as
---
import mx.events.EventDispatcher;

class GraphControl{
    private var path:MovieClip;
    private var stageMc:MovieClip;
    private var dataStorage:GraphDataStorage;
    private var drawing:DrawingControl;
    private var ex:ExXML;
    private var STAGE_MC_DEPTH:Number=100;

    function GraphControl(path:MovieClip){
        this.path=path;
        path.createEmptyMovieClip("stage_mc",STAGE_MC_DEPTH);
        stageMc=path.stage_mc;
        stageMc._x=320;
        stageMc._y=240;
        dataStorage=new GraphDataStorage();
        drawing=new DrawingControl(stageMc);
        loadGraphData("GraphData.xml");
    }

    private function loadGraphData(address:String):Void{
        ex=new ExXML();
        EventDispatcher.initialize(ex);
        ex.onLoad=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onLoadXML";
            this.dispatchEvent(lo);
        };
        ex.addEventListener("onLoadXML",this);
        ex.load(address);
    }

    public function onLoadXML():Void{
        var arr:Array=ex.firstChild.childNodes;
        var pointNodes:Array;
        var lineNodes:Array;
        var imageNodes:Array;
        for(var i:Number=0;i<arr.length;i++){
            var node:XMLNode=XMLNode(arr[i]);
            if(node.nodeName=="pointlist"){
                pointNodes=node.childNodes;
            }else if(node.nodeName=="linelist"){
                lineNodes=node.childNodes;
            }else if(node.nodeName=="imagelist"){
                imageNodes=node.childNodes;
            }else{
                trace("XMLNode is invalid form.");
            }
        }
        for(var i:Number=0;i<pointNodes.length;i++){
            var node:XMLNode=XMLNode(pointNodes[i]);
            if(node.nodeName=="point"){
                var o:Object=node.attributes;
                var id:String=String(o.id);
                var x:Number=Number(o.x);
                var y:Number=Number(o.y);
                var image:String=String(o.image);
                var index:Number=dataStorage.addPoint(id,x,y,image);
            }else{
                trace("XMLNode is invalid form.");
            }
        }
        for(var i:Number=0;i<lineNodes.length;i++){
            var node:XMLNode=XMLNode(lineNodes[i]);
            if(node.nodeName=="line"){
                var o:Object=node.attributes;
                var id:String=String(o.id);
                var point1:String=String(o.point1);
                var point2:String=String(o.point2);
                var index:Number=dataStorage.addLine(id,point1,point2);
            }else{
                trace("XMLNode is invalid form.");
            }
        }
        for(var i:Number=0;i<imageNodes.length;i++){
            var node:XMLNode=XMLNode(imageNodes[i]);
            if(node.nodeName=="image"){
                var o:Object=node.attributes;
                var id:String=String(o.id);
                var url:String=String(o.url);
                dataStorage.addImage(id,url);
            }else{
                trace("XMLNode is invalid form.");
            }
        }
        dataStorage.addPointIndexToLineLists();
        drawPoints();
        drawAllLines();
    }

    private function drawPoints():Void{
        var len:Number=dataStorage.getPointListLength();
        for(var i:Number=0;i<len;i++){
            var x:Number=dataStorage.getPointPosX(i);
            var y:Number=dataStorage.getPointPosY(i);
            var mc:MovieClip=drawing.drawOval(x,y);
            mc.setIndex(i);
            mc.setGraphControl(this);
            var imageId:String=dataStorage.getPointImage(i);
            var url:String=dataStorage.getImageFromId(imageId);
            mc.loadImage(url);
        }
    }

    private function drawAllLines():Void{
        var len:Number=dataStorage.getLineListLength();
        for(var i:Number=0;i<len;i++){
            drawLine(i);
        }
    }

    private function drawLine(index:Number):Void{
        var index1:Number=dataStorage.getLineIndex1(index);
        var index2:Number=dataStorage.getLineIndex2(index);
        var x1:Number=dataStorage.getPointPosX(index1);
        var y1:Number=dataStorage.getPointPosY(index1);
        var x2:Number=dataStorage.getPointPosX(index2);
        var y2:Number=dataStorage.getPointPosY(index2);
        var size:Number=0;
        var color16:Number=0x000000;
        drawing.drawLine(x1,y1,x2,y2,size,color16,index);
    }

    public function refreshLines(index:Number):Void{
        var len:Number=dataStorage.getConnecterLength(index);
        for(var i:Number=0;i<len;i++){
            var lineIndex:Number=dataStorage.getConnecter(index,i);
            drawing.removeLine(lineIndex);
            drawLine(lineIndex);
        }
    }

    public function setPosition(index:Number,x:Number,y:Number):Void{
        dataStorage.setPointPosition(index,x,y);
    }
}



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home