JAVAC.JP
HOSHI TETSUYA 星鉄矢
2005/07/25
hossy@javac.jp
http://www.javac.jp
Google
WWW を検索 WWW.JAVAC.JP を検索

6-3 大きなグラフの遊泳 - 実装例

exampleの実装にあたって、その他のソースを掲載します。

main.asはタイムラインからincludeされるファイルです。
今までの例同様、メインとなるクラスを、タイムラインを引数に生成します。
main.as
---
var graph:GraphControl=new GraphControl(this);
stop();
ExXMLクラスも前述のものと同様です。
ExXML.as
---
class ExXML extends XML{
    private var ignoreWhite=true
    var addEventListener:Function;
    var removeEventListener:Function;
    private var dispatchEvent:Function;
}
GraphControlクラスがこの例のメインのクラスです。
グラフ描画としての基本機能の他に、マウスクリックのイベントを受け取るルーチンを含みます。
drawOvals,drawLinesメソッドにおいて、ParamControlで座標変換を行っています。
ノード・エッジ共に全体の再描画を行っています。
GraphControl.as
---
import mx.events.EventDispatcher;

class GraphControl{
    private var path:MovieClip;
    private var stageMc:MovieClip;
    private var mouseListener:MovieClip;
    private var dataStorage:GraphDataStorage;
    private var drawing:DrawingControl;
    private var paramControl:ParamControl;
    private var ex:ExXML;
    private var STAGE_MC_DEPTH:Number=100;
    private var MOUSE_MC_DEPTH:Number=200;
    private var XMIN:Number=-320;
    private var YMIN:Number=-240;
    private var XMAX:Number=320;
    private var YMAX:Number=240;

    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);
        paramControl=new ParamControl();
        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;
        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{
                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 index:Number=dataStorage.addPoint(id,x,y);
            }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.");
            }
        }
        dataStorage.addPointIndexToLineLists();
        drawPoints();
        drawLines();
        addMouseHandler();
    }

    private function drawPoints():Void{
        drawing.resetOvals();
        var x1:Number=paramControl.getDataX(XMIN);
        var y1:Number=paramControl.getDataY(YMIN);
        var x2:Number=paramControl.getDataX(XMAX);
        var y2:Number=paramControl.getDataY(YMAX);
        var arr:Array=dataStorage.getAreaPoint(x1,y1,x2,y2);
        for(var i:Number=0;i<arr.length;i++){
            var n:Number=Number(arr[i]);
            var x:Number=paramControl.getScreenX(dataStorage.getPointPosX(n));
            var y:Number=paramControl.getScreenY(dataStorage.getPointPosY(n));
            var size:Number=20;
            drawing.drawOval(x,y,size);
        }
    }

    private function drawLines():Void{
        drawing.resetLines();
        var x1:Number=paramControl.getDataX(XMIN);
        var y1:Number=paramControl.getDataY(YMIN);
        var x2:Number=paramControl.getDataX(XMAX);
        var y2:Number=paramControl.getDataY(YMAX);
        var arr:Array=dataStorage.getAreaLine(x1,y1,x2,y2);
        for(var i:Number=0;i<arr.length;i++){
            var n:Number=Number(arr[i]);
            var index1:Number=dataStorage.getLineIndex1(n);
            var index2:Number=dataStorage.getLineIndex2(n);
            var x1:Number=paramControl.getScreenX(dataStorage.getPointPosX(index1));
            var y1:Number=paramControl.getScreenY(dataStorage.getPointPosY(index1));
            var x2:Number=paramControl.getScreenX(dataStorage.getPointPosX(index2));
            var y2:Number=paramControl.getScreenY(dataStorage.getPointPosY(index2));
            var size:Number=0;
            var color16:Number=0x000000;
            drawing.drawLine(x1,y1,x2,y2,size,color16);
        }
    }

    private function addMouseHandler():Void{
        path.createEmptyMovieClip("mouse_listener",MOUSE_MC_DEPTH);
        mouseListener=path.mouse_listener;
        mouseListener._x=320;
        mouseListener._y=240;
        EventDispatcher.initialize(mouseListener);
        mouseListener.onMouseDown=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onMouseDownHandler";
            lo.x=this._xmouse;
            lo.y=this._ymouse;
            this.dispatchEvent(lo);
        };
        mouseListener.addEventListener("onMouseDownHandler",this);
    }

    private function removeMouseHandler():Void{
        mouseListener.removeEventListener("onMouseDownHandler",this);
    }

    private function onMouseDownHandler(eventObj:Object):Void{
        var x:Number=Number(eventObj.x);
        var y:Number=Number(eventObj.y);
        var tx:Number=paramControl.getDataX(x);
        var ty:Number=paramControl.getDataY(y);
        if(Key.isDown(Key.CONTROL)){
            if(Key.isDown(Key.SHIFT)){
                paramControl.changePosScaleDown(tx,ty);
            }else{
                paramControl.changePosScaleUp(tx,ty);
            }
        }else{
            paramControl.changePos(tx,ty);
        }
        drawPoints();
        drawLines();
    }
}
DrawingControlクラスは、前述のDrawingControlクラスに、resetOvalsメソッド、resetLinesメソッドを加えたものです。
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;

    function DrawingControl(path:MovieClip){
        this.path=path;
        ovalDepth=OVAL_START_DEPTH;
        lineDepth=LINE_START_DEPTH;
    }

    public function resetOvals():Void{
        ovalBase.removeMovieClip();
        path.createEmptyMovieClip("oval_base",OVAL_BASE_DEPTH);
        ovalBase=path.oval_base;
    }

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

    public function drawOval(x:Number,y:Number,size:Number):Void{
        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;
        mc._xscale=size;
        mc._yscale=size;
    }

    public function drawLine(x1:Number,y1:Number,x2:Number,y2:Number
                                    ,size:Number,color16: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);
    }
}
これらのソースの実装で、exampleの実現が可能です。



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home