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

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

    import com.google.maps.Map;
    import com.google.maps.MapEvent;
    import com.google.maps.MapType;
    import com.google.maps.LatLng;
    import flash.geom.Point;
    import flash.geom.Matrix;

    [SWF(width=450, height=350, backgroundColor=0xB9CCD5)]

    public class Pv3dGoogleMap extends Sprite
    {
        // _______________________________________________________________________
        //                                                                  vars3D
        private var container : Sprite;
        private var scene     : MovieScene3D;
        private var camera    : Camera3D;
        private var cubeObj   : Cube;
        private var planeSize : int = 300;

        private var valx      : Number = 0;
        private var valy      : Number = 0;
        private var map : Map;
        private var mapContainer:Sprite;

        static public const
            TEXTURE_W:int = 360,    ///< テクスチャの幅
            TEXTURE_H:int = 360,    ///< テクスチャの高さ
            SCREEN_W:int = 450,     ///< SWFの幅
            SCREEN_H:int = 350;     ///< SWFの高さ

        // _______________________________________________________________________
        //                                                             Constructor
        public function Pv3dGoogleMap():void
        {
            super();

            map = new Map();
            // Google Map API のKEYは各人取得のこと
            map.key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

            map.setSize(new Point(TEXTURE_W, TEXTURE_H));
            map.addEventListener(MapEvent.MAP_READY, function(event:Event):void{
                map.setCenter(new LatLng(35.698684,139.774214), 17, MapType.NORMAL_MAP_TYPE);
                map.disableDragging();
            });
            mapContainer = new Sprite();
            mapContainer.addChild(map);
            // Google MapのクラスはSWFの元になるクラスにaddChildしなければ読み込まれない様子
            addChild(mapContainer);
            // addChildしたので消しておく
            mapContainer.visible = false;

            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
            this.addEventListener(Event.ENTER_FRAME, loop3D);
            this.stage.addEventListener(Event.RESIZE, onStageResize);

            init3D();
        }

        // _______________________________________________________________________
        //                                                                  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 MovieScene3D( container );

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

            var bmpData:BitmapData = new BitmapData(TEXTURE_W, TEXTURE_H);

            var ml:MaterialsList = new MaterialsList(
            {
                all: new BitmapMaterial(bmpData),
                front: new BitmapMaterial(bmpData),
                back: new BitmapMaterial(bmpData),
                right: new BitmapMaterial(bmpData),
                left: new BitmapMaterial(bmpData),
                top: new BitmapMaterial(bmpData),
                bottom: new BitmapMaterial(bmpData)
            });

            // オブジェクト生成            
            cubeObj = new Cube( ml, planeSize, planeSize, planeSize, 1, 1, 1 );
            cubeObj.material.lineColor = 0x00FFFF;
            cubeObj.material.lineAlpha = 1;

            scene.addChild(cubeObj);
            cubeObj.rotationX = 45;
            cubeObj.rotationY = 45;

            var filter:Array = new Array;
            filter.push( new DropShadowFilter() );

            cubeObj.container.filters = filter;
        }

        // _______________________________________________________________________
        //                                                                    Loop
        private function loop3D( event:Event ):void
        {
            //マウス座標でオブジェクトを回転
            valx += this.container.mouseX / 50;
            valy += this.container.mouseY / 50;

            cubeObj.rotationY = valx;
            cubeObj.rotationX = valy;

            // Google Mapの画像をテクスチャとして描画
            cubeObj.material.bitmap.draw(mapContainer);

            //レンダリング
            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;
        }

    }
}