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

C-2 リファクタリングを行う

現在のソースでは、ViewerControlクラスに処理が集中しているので、処理を分散させたい。(ViewerControlクラスは、単独の処理を行うためのクラスではなく、『処理をまとめる』ためのクラスであることが、望ましい。)

まず、ムービークリップへ、コメントのハンドラーを付ける部分をCaptionControlに移動します。
CaptionControl.as
---
import mx.events.EventDispatcher;

class CaptionControl{
    private var path:MovieClip;
    private var captionDepth:Number;

    function CaptionControl(path:MovieClip){
        this.path=path;
        captionDepth=0;
    }

    public function addCaption(eventObj:Object):Void{
        var mc:MovieClip=MovieClip(eventObj.mc);
        var comment:String=String(eventObj.comment);
        var depth:Number=captionDepth;
        captionDepth++;
        var bounds:Object=mc.getBounds(path);
        var x:Number=Number(bounds.xMin+bounds.xMax)/2;
        var y:Number=Number(bounds.yMin+bounds.yMax)/2;
        path.createTextField("chaption",depth,x+10,y+10,1,1);
        var tf=path.chaption;
        tf.type="dynamic";
        tf.textColor=0x000000;
        tf.html=true;
        tf.htmlText=comment;
        tf.selectable=false;
        tf.background=true;
        tf.backgroundColor=0xFFFFEE;
        tf.border=true;
        tf.borderColor=0x000000;
        tf.multiline=true;
        tf.autoSize=true;
    }

    public function removeCaption():Void{
        path.chaption.removeTextField();
    }

    public function setCaption(mc:MovieClip):Void{
        EventDispatcher.initialize(mc);
        mc.onRollOver=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="addCaption";
            lo.mc=this;
            lo.comment=this.comment;
            this.dispatchEvent(lo);
        };
        mc.onRollOut=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="removeCaption";
            this.dispatchEvent(lo);
        };
        mc.onPress=function(){
            var lo:Object=new Object();
            lo.target=this;
            lo.type="removeCaption";
            this.dispatchEvent(lo);
        };
        mc.addEventListener("addCaption",this);
        mc.addEventListener("removeCaption",this);
    }
}
また、ロードしたXMLのDataStorageへの格納を行う箇所を、丸々、専用クラスに移します。
ConfigSpliterクラスとModelSpliterクラスを作成します。
ConfigSpliter.as
---
class ConfigSpliter{
    private var dataStorage:ModelDataStorage;
    private var modelAddress:String;

    function ConfigSpliter(dataStorage:ModelDataStorage){
        this.dataStorage=dataStorage;
    }

    public function getAddress():String{
        return modelAddress;
    }

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

ModelSpliter.as
---
class ModelSpliter{
    private var dataStorage:ModelDataStorage;
    private var paramControl:ParamControl;

    function ModelSpliter(){
    }

    public function setModelDataStorage(dataStorage:ModelDataStorage):Void{
        this.dataStorage=dataStorage;
    }

    public function setParamControl(paramControl:ParamControl):Void{
        this.paramControl=paramControl;
    }

    public function split(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);
            }
        }
    }
}
これらの処置を行うことで、ある程度、ViewerControlの可読性を上昇させることができたのではないでしょうか。
以下に、変更後のViewerControlを記載します。
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 depthManager:DepthManager;
    private var captionControl:CaptionControl;
    private var oldX:Number;
    private var oldY:Number;
    private var VIEW_MC_DEPTH:Number=100;
    private var CAPTION_MC_DEPTH:Number=150;
    private var MOUSE_MC_DEPTH:Number=200;
    private var CENTER_X:Number=0;
    private var CENTER_Y:Number=0;

    function ViewerControl(path:MovieClip){
        this.path=path;
        IOService=new XMLIOService();
        dataStorage=new ModelDataStorage();
        paramControl=new ParamControl();
        depthManager=new DepthManager();
        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);
        path.createEmptyMovieClip("caption_mc",CAPTION_MC_DEPTH);
        path.caption_mc._x=CENTER_X;
        path.caption_mc._y=CENTER_Y;
        captionControl=new CaptionControl(path.caption_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 spliter:ConfigSpliter=new ConfigSpliter(dataStorage);
        spliter.split(ex);
        var address:String=spliter.getAddress();
        if(address.length>0){
            load(address);
        }
    }

    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 spliter:ModelSpliter=new ModelSpliter();
        spliter.setModelDataStorage(dataStorage);
        spliter.setParamControl(paramControl);
        spliter.split(ex);
        dataStorage.addPointIndexToLineLists();
        dataStorage.centering();
        drawView();
        addMouseListener();
    }

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

    public function drawView():Void{
        depthManager.reset();
        drawOvals();
        drawLines();
        depthManager.sort();
        depthManager.updateDepths();
    }

    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 comment:String=dataStorage.getPointComment(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);
            var mc:MovieClip=drawingControl.drawOval(sx,sy,ssize,colR,colG,colB,colA);
            mc.comment=comment;
            depthManager.addItem(z,mc);
            if(comment.length>0){
                captionControl.setCaption(mc);
            }
        }
    }

    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 comment:String=dataStorage.getLineComment(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);
            var mc:MovieClip=drawingControl.drawLine(sx1,sy1,sx2,sy2,size,colR,colG,colB,colA);
            mc.comment=comment;
            if(comment.length>0){
                captionControl.setCaption(mc);
            }
        }
    }

    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;
    }
}
もちろん、描画部分やマウスイベントの部分も抜き出すことで、もっとViewerControlはシンプルな構造を取ることができるようになると思います。

全ソースを以下に置きます。
003_refactory.zip



BACKTOPNEXT




All Contents Copyright (C) 2005 HOSHI Tetsuya
Home