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

5-3 ノードをドラッグ可能にする

前述のGraphにおいて、ノードをドラッグ可能にします。
方法論としては、リンケージovalにOvalClassをレジスタークラスします。
以下がそのOvalClassクラスです。
class OvalClass extends MovieClip{
    private var SCREEN_WIDTH:Number=640;
    private var SCREEN_HEIGHT:Number=480;
    private var CENTER_X:Number=320;
    private var CENTER_Y:Number=240;

    function OvalClass(){
    }

    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);
    }

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

    private function onReleaseOutside():Void{
        onRelease();
    }
}
実行すると、以下のようになります。
example
確かにノードのドラッグには成功していますが、エッジが追従してくれていません。
以下のように書き直すと追従します。
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;

    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();
    }
}

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;
        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();
    }

    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 size:Number=20;
            var mc:MovieClip=drawing.drawOval(x,y,size);
            mc.setIndex(i);
            mc.setGraphControl(this);
        }
    }

    private function drawLines():Void{
        var len:Number=dataStorage.getLineListLength();
        for(var i:Number=0;i<len;i++){
            var index1:Number=dataStorage.getLineIndex1(i);
            var index2:Number=dataStorage.getLineIndex2(i);
            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);
        }
    }

    public function refreshLines():Void{
        drawing.removeLines();
        drawLines();
    }

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

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;
        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;
    }

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

    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);
    }

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

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

    function GraphDataStorage(){
        reset();
    }

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

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

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

    public function addPoint(id:String,x:Number,y:Number):Number{
        var o:Object=new Object();
        o.id=id;
        o.x=x;
        o.y=y;
        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;
    }

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

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

    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 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;
    }
}
以下のように追従して動きます。
example



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home