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

2-6 ドラッグして回転させるイベントの付加

このままではイベント発生がなく、3Dを体験することはできません。
ドラッグで立方体を自由回転させられるようにします。
onMouseDown, onMouseUp, onMouseMoveのイベントを付加しますが、この際、EventDispatcherを用います。
import mx.events.EventDispatcher;

class CubeControl{
    private var path:MovieClip;
    private var IOService:XMLIOService;
    private var dataStorage:ModelDataStorage;
    private var paramControl:ParamControl;
    private var drawingControl:DrawingControl;
    private var oldX:Number;
    private var oldY:Number;
    private var VIEW_MC_DEPTH:Number=100;
    private var MOUSE_MC_DEPTH:Number=200;
    private var CENTER_X:Number=0;
    private var CENTER_Y:Number=0;

    function CubeControl(path:MovieClip){
        this.path=path;
        IOService=new XMLIOService();
        dataStorage=new ModelDataStorage();
        paramControl=new ParamControl();
        path.createEmptyMovieClip("view_mc",VIEW_MC_DEPTH);
        path.view_mc._x=CENTER_X;
        path.view_mc._y=CENTER_Y;
        drawingControl=new DrawingControl(path.view_mc);
        load("xml/001.xml");
    }

    public function load(address:String):Void{
        trace("loading:"+address);
        removeMouseListener();
        dataStorage.reset();
        IOService.reset();
        IOService.addLoadComplete("loaded",this);
        IOService.load(address,null,false);
    }

    public function loaded(ex:ExXML):Void{
        var nodes:Array=ex.firstChild.childNodes;
        var pointList:Array;
        var lineList:Array;
        for(var i:Number=0;i<nodes.length;i++){
            var n:XMLNode=XMLNode(nodes[i]);
            if(n.nodeName=="pointlist"){
                pointList=n.childNodes;
            }else if(n.nodeName=="linelist"){
                lineList=n.childNodes;
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        for(var i:Number=0;i<pointList.length;i++){
            var n:XMLNode=XMLNode(pointList[i]);
            if(n.nodeName=="point"){
                var id:String=String(n.attributes.id);
                var x:Number=Number(n.attributes.x);
                var y:Number=Number(n.attributes.y);
                var z:Number=Number(n.attributes.z);
                dataStorage.addPoint(id,x,y,z);
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        for(var i:Number=0;i<lineList.length;i++){
            var n:XMLNode=XMLNode(lineList[i]);
            if(n.nodeName=="line"){
                var id:String=String(n.attributes.id);
                var point1:String=String(n.attributes.point1);
                var point2:String=String(n.attributes.point2);
                dataStorage.addLine(id,point1,point2);
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        dataStorage.addPointIndexToLineLists();
        drawView();
        addMouseListener();
    }

    public function resetView():Void{
        drawingControl.removeAllOvals();
        drawingControl.removeAllLines();
    }

    public function drawView():Void{
        drawOvals();
        drawLines();
    }

    private function drawOvals():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 z:Number=dataStorage.getPointPosZ(i);
            var size:Number=50;
            var sx:Number=paramControl.getScreenX(x,y,z,size);
            var sy:Number=paramControl.getScreenY(x,y,z,size);
            var ssize:Number=paramControl.getScreenSize(x,y,z,size);
            drawingControl.drawOval(sx,sy,ssize);
        }
    }

    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 z1:Number=dataStorage.getPointPosZ(index1);
            var size1:Number=1;
            var x2:Number=dataStorage.getPointPosX(index2);
            var y2:Number=dataStorage.getPointPosY(index2);
            var z2:Number=dataStorage.getPointPosZ(index2);
            var size2:Number=1;
            var sx1:Number=paramControl.getScreenX(x1,y1,z1,size1);
            var sy1:Number=paramControl.getScreenY(x1,y1,z1,size1);
            var sx2:Number=paramControl.getScreenX(x2,y2,z2,size2);
            var sy2:Number=paramControl.getScreenY(x2,y2,z2,size2);
            drawingControl.drawLine(sx1,sy1,sx2,sy2);
        }
    }

    private function addMouseListener():Void{
        path.createEmptyMovieClip("mouse_mc",MOUSE_MC_DEPTH);
        var mc:MovieClip=path.mouse_mc;
        mc._x=CENTER_X;
        mc._y=CENTER_Y;
        EventDispatcher.initialize(mc);
        mc.onMouseUp=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onMouseUpHandler";
            lo.x=this._xmouse;
            lo.y=this._ymouse;
            this.dispatchEvent(lo);
        };
        mc.onMouseDown=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onMouseDownHandler";
            lo.x=this._xmouse;
            lo.y=this._ymouse;
            this.dispatchEvent(lo);
        };
        mc.addEventListener("onMouseUpHandler",this);
        mc.addEventListener("onMouseDownHandler",this);
    }

    private function removeMouseListener():Void{
        var mc:MovieClip=path.mouse_mc;
        mc.removeEventListener("onMouseUpHandler",this);
        mc.removeEventListener("onMouseDownHandler",this);
        mc.removeMovieClip();
    }

    private function addMouseMoveListener():Void{
        var mc:MovieClip=path.mouse_mc;
        mc.onMouseMove=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="onMouseMoveHandler";
            lo.x=this._xmouse;
            lo.y=this._ymouse;
            this.dispatchEvent(lo);
        };
        mc.addEventListener("onMouseMoveHandler",this);
    }

    private function removeMouseMoveListener():Void{
        var mc:MovieClip=path.mouse_mc;
        mc.removeEventListener("onMouseMoveHandler",this);
    }

    private function onMouseUpHandler(eventObj:Object):Void{
        var x:Number=Number(eventObj.x);
        var y:Number=Number(eventObj.y);
        removeMouseMoveListener();
    }

    private function onMouseDownHandler(eventObj:Object):Void{
        var x:Number=Number(eventObj.x);
        var y:Number=Number(eventObj.y);
        oldX=x;
        oldY=y;
        addMouseMoveListener();
    }

    private function onMouseMoveHandler(eventObj:Object):Void{
        var x:Number=Number(eventObj.x);
        var y:Number=Number(eventObj.y);
        var rotX:Number=-Math.PI*(y-oldY)/200;
        var rotY:Number=Math.PI*(x-oldX)/200;
        dataStorage.rotateX(rotX);
        dataStorage.rotateY(rotY);
        resetView();
        drawView();
        oldX=x;
        oldY=y;
    }
}



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home