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

3-3 note属性に従い分子ごとの着色・サイズ変更

note属性に従って、サイズ変更と着色を行います。
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
    ,colR:Number,colG:Number,colB:Number,colA: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;
        var col:Color=new Color(mc);
        var transformObj:Object=new Object();
        transformObj.ra=Number(colR);
        transformObj.ga=Number(colG);
        transformObj.ba=Number(colB);
        transformObj.aa=Number(colA);
        transformObj.rb=0;
        transformObj.gb=0;
        transformObj.bb=0;
        transformObj.ab=0;
        col.setTransform(transformObj);
    }

    public function drawLine(x1:Number,y1:Number,x2:Number,y2:Number,size:Number
    ,colR:Number,colG:Number,colB:Number,colA:Number):Void{
        var depth:Number=lineDepth;
        lineDepth++;
        var mcname:String="line"+depth;
        lineBase.createEmptyMovieClip(mcname,depth);
        var mc:MovieClip=lineBase[mcname];
        mc.lineStyle(size,0xFFFFFF);
        mc.moveTo(x1,y1);
        mc.lineTo(x2,y2);
        var col:Color=new Color(mc);
        var transformObj:Object=new Object();
        transformObj.ra=Number(colR);
        transformObj.ga=Number(colG);
        transformObj.ba=Number(colB);
        transformObj.aa=Number(colA);
        transformObj.rb=0;
        transformObj.gb=0;
        transformObj.bb=0;
        transformObj.ab=0;
        col.setTransform(transformObj);
    }

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

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

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

class ViewerControl{
    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;
    private var modelAddress:String;

    function ViewerControl(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);
        configLoad("config.xml");
    }

    private function configLoad(address:String):Void{
        trace("loading:"+address);
        IOService.reset();
        IOService.addLoadComplete("configLoaded",this);
        IOService.load(address,null,false);
    }

    public function configLoaded(ex:ExXML):Void{
        var nodes:Array=ex.firstChild.childNodes;
        var pointNoteList:Array;
        var lineNoteList:Array;
        for(var i:Number=0;i<nodes.length;i++){
            var n:XMLNode=XMLNode(nodes[i]);
            if(n.nodeName=="model"){
                var dirname:String=String(n.attributes.dirname);
                var filename:String=String(n.attributes.filename);
                modelAddress=dirname+filename;
            }else if(n.nodeName=="list"){
            }else if(n.nodeName=="pointnotelist"){
                pointNoteList=n.childNodes;
            }else if(n.nodeName=="linenotelist"){
                lineNoteList=n.childNodes;
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        for(var i:Number=0;i<pointNoteList.length;i++){
            var n:XMLNode=XMLNode(pointNoteList[i]);
            if(n.nodeName=="pointnote"){
                var id:String=String(n.attributes.note);
                var size:Number=Number(n.attributes.size);
                var colR:Number=Number(n.attributes.col_r);
                var colG:Number=Number(n.attributes.col_g);
                var colB:Number=Number(n.attributes.col_b);
                var colA:Number=Number(n.attributes.col_a);
                dataStorage.setPointNote(id,size,colR,colG,colB,colA);
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        for(var i:Number=0;i<lineNoteList.length;i++){
            var n:XMLNode=XMLNode(lineNoteList[i]);
            if(n.nodeName=="linenote"){
                var id:String=String(n.attributes.note);
                var size:Number=Number(n.attributes.size);
                var colR:Number=Number(n.attributes.col_r);
                var colG:Number=Number(n.attributes.col_g);
                var colB:Number=Number(n.attributes.col_b);
                var colA:Number=Number(n.attributes.col_a);
                dataStorage.setLineNote(id,size,colR,colG,colB,colA);
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        if(modelAddress.length>0){
            load(modelAddress);
        }
    }

    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 configNode:XMLNode;
        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 if(n.nodeName=="config"){
                configNode=n;
            }else{
                trace("invalid node. :"+n.nodeName);
            }
        }
        if(configNode!=undefined){
            var scale:Number=Number(configNode.attributes.scale);
            var cx:Number=Number(configNode.attributes.cx);
            var cy:Number=Number(configNode.attributes.cy);
            var cz:Number=Number(configNode.attributes.cz);
            var length:Number=Number(configNode.attributes.length);
            paramControl.setScale(scale);
            paramControl.setScreenLength(length);
            paramControl.setCenterPosition(cx,cy,cz);
        }
        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);
                var note:String=String(n.attributes.note);
                var comment:String=String(n.attributes.comment);
                dataStorage.addPoint(id,x,y,z,note,comment);
            }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);
                var note:String=String(n.attributes.note);
                var comment:String=String(n.attributes.comment);
                dataStorage.addLine(id,point1,point2,note,comment);
            }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 id:String=dataStorage.getPointNote(i);
            var size:Number=dataStorage.getPointNoteSize(id);
            var colR:Number=dataStorage.getPointNoteColR(id);
            var colG:Number=dataStorage.getPointNoteColG(id);
            var colB:Number=dataStorage.getPointNoteColB(id);
            var colA:Number=dataStorage.getPointNoteColA(id);
            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,colR,colG,colB,colA);
        }
    }

    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 id:String=dataStorage.getLineNote(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);
            var size:Number=dataStorage.getLineNoteSize(id);
            var colR:Number=dataStorage.getLineNoteColR(id);
            var colG:Number=dataStorage.getLineNoteColG(id);
            var colB:Number=dataStorage.getLineNoteColB(id);
            var colA:Number=dataStorage.getLineNoteColA(id);
            drawingControl.drawLine(sx1,sy1,sx2,sy2,size,colR,colG,colB,colA);
        }
    }

    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;
    }
}
これにより、ノード・エッジが管理され、以下のようになります。
example



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home