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

3-2 config.xmlのロード

config.xmlのロード・configタグ・note属性・コメントの追加により、プログラムはより汎用的になりますが、一方で、様々な部分に変更が加わります。
ModelDataStorage.as
---
class ModelDataStorage extends RotateTemplate{
    private var pointList:Array;
    private var lineList:Array;
    private var pointNoteList:Object;
    private var lineNoteList:Object;

    function ModelDataStorage(){
        pointNoteList=new Object();
        lineNoteList=new Object();
        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,z:Number
    ,note:String,comment:String):Void{
        var o:Object=new Object();
        o.id=id;
        o.x=x;
        o.y=y;
        o.z=z;
        o.note=note;
        o.comment=comment;
        pointList.push(o);
    }

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

    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 getPointPosZ(index:Number):Number{
        return Number(pointList[index].z);
    }

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

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

    public function addLine(id:String,point1:String,point2:String
    ,note:String,comment:String):Void{
        var o:Object=new Object();
        o.id=id;
        o.point1=point1;
        o.point2=point2;
        o.note=note;
        o.comment=comment;
        lineList.push(o);
    }

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

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

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

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

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

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

    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 setPointNote(id:String,size:Number
    ,colR:Number,colG:Number,colB:Number,colA:Number):Void{
        var o:Object=new Object();
        o.id=id;
        o.size=size;
        o.colR=colR;
        o.colG=colG;
        o.colB=colB;
        o.colA=colA;
        pointNoteList[id]=o;
    }

    public function getPointNoteSize(id:String):Number{
        return Number(pointNoteList[id].size);
    }

    public function getPointNoteColR(id:String):Number{
        return Number(pointNoteList[id].colR);
    }

    public function getPointNoteColG(id:String):Number{
        return Number(pointNoteList[id].colG);
    }

    public function getPointNoteColB(id:String):Number{
        return Number(pointNoteList[id].colB);
    }

    public function getPointNoteColA(id:String):Number{
        return Number(pointNoteList[id].colA);
    }

    public function setLineNote(id:String,size:Number
    ,colR:Number,colG:Number,colB:Number,colA:Number):Void{
        var o:Object=new Object();
        o.id=id;
        o.size=size;
        o.colR=colR;
        o.colG=colG;
        o.colB=colB;
        o.colA=colA;
        lineNoteList[id]=o;
    }

    public function getLineNoteSize(id:String):Number{
        return Number(lineNoteList[id].size);
    }

    public function getLineNoteColR(id:String):Number{
        return Number(lineNoteList[id].colR);
    }

    public function getLineNoteColG(id:String):Number{
        return Number(lineNoteList[id].colG);
    }

    public function getLineNoteColB(id:String):Number{
        return Number(lineNoteList[id].colB);
    }

    public function getLineNoteColA(id:String):Number{
        return Number(lineNoteList[id].colA);
    }
}
---

ParamControl.as
---
class ParamControl{
    private var cx:Number;
    private var cy:Number;
    private var cz:Number;
    private var screenLength:Number;
    private var scale:Number;

    function ParamControl(){
    }

    function getScreenX(x:Number,y:Number,z:Number,size:Number):Number{
        return scale*(x-cx)*screenLength/(z-cz);
    }

    function getScreenY(x:Number,y:Number,z:Number,size:Number):Number{
        return scale*(y-cy)*screenLength/(z-cz);
    }

    function getScreenSize(x:Number,y:Number,z:Number,size:Number):Number{
        return Math.abs(scale*size*screenLength/(z-cz));
    }

    public function setCenterPosition(x:Number,y:Number,z:Number):Void{
        cx=x;
        cy=y;
        cz=z;
    }

    public function setScale(scale:Number):Void{
        this.scale=scale;
    }

    public function setScreenLength(length:Number):Void{
        screenLength=length;
    }
}
---

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=="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 size:Number=0.5;
            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