package
{
    import flash.display.*;
    import flash.events.*;

    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;

    public class ColladaView extends Sprite
    {
        // _______________________________________________________________________
        //                                                                  vars3D
        private var container : Sprite;
        private var scene     : Scene3D;
        private var camera    : Camera3D;

        private var valx      : Number = 0;
        private var valy      : Number = 0;
        private var col:Collada;

        // _______________________________________________________________________
        //                                                             Constructor
        public function ColladaView():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;

            init3D();

            this.addEventListener(Event.ENTER_FRAME, loop3D);
            this.stage.addEventListener(Event.RESIZE, onStageResize);
            this.stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelHandler);
        }

        private function onMouseWheelHandler(evt:MouseEvent):void
        {
            // カメラの前後移動
            var add:int = -10*evt.delta;

            if( add < 0 ){
                if( camera.z < 400 ){
                    return;
                }
            }else{
                if( camera.z > 1500 ){
                    return;
                }
            }

            camera.z += add;
        }
        // _______________________________________________________________________
        //                                                                  Init3D
        private function init3D():void
        {
            //コンテナ生成
            this.container = new Sprite();
            addChild(this.container);
            this.container.x = this.stage.stageWidth  / 2;
            this.container.y = this.stage.stageHeight / 2;

            //シーン生成
            scene = new Scene3D( container );

            //カメラ設定
            camera = new Camera3D();
            camera.z = 1000;
            camera.focus = 500;
            camera.zoom = 1;

            //Colladaオブジェクトの読み込み
            col = new Collada("gun.dae");
            scene.addChild( col );

        }

        // _______________________________________________________________________
        //                                                                    Loop
        private function loop3D( event:Event ):void
        {
            valx += this.container.mouseX / 50;
            valy += this.container.mouseY / 50;
            col.rotationY = valx;
            col.rotationX = valy;

            //レンダリング
            this.scene.renderCamera( camera );
        }

        // _______________________________________________________________________
        //                                                           onStageResize
        private function onStageResize(event:Event):void
        {
            this.container.x = this.stage.stageWidth  / 2;
            this.container.y = this.stage.stageHeight / 2;
        }

    }
}