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

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

    [SWF(backgroundColor=0x000000)]

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

        private var planeObj  : DisplayObject3D;
        private var planeSize : int = 300;      //Planeオブジェクト1辺の長さ
        private var segment   : int = 1;        //面の分割数
        private var material  : BitmapMaterial;
        private var bmpData   : BitmapData;

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

        static public const
            TEXTURE_W:int = 240,    ///< テクスチャの幅
            TEXTURE_H:int = 240;    ///< テクスチャの高さ

        // _______________________________________________________________________
        //                                                             Constructor
        public function Pv3dKey():void
        {
            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);
            /// stageに対して、keyDownHandlerを追加
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);

            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 Scene3D( container );

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

            //rootNode生成
            rootNode = scene.addChild( new DisplayObject3D( "rootNode" ) );

            //BitmapDataの作成。Bitmapに角丸矩形を描く
            bmpData = new BitmapData(TEXTURE_W,TEXTURE_H,true,0x00000000);
            var rrect:Shape = new Shape();
            rrect.graphics.lineStyle(0,0x00000000);
            rrect.graphics.beginFill(0xF7005F);
            rrect.graphics.drawRoundRect(0,0,TEXTURE_W,TEXTURE_H,50);
            rrect.graphics.endFill();
            bmpData.draw(rrect);

            //マテリアル設定
            material = new BitmapMaterial(bmpData);
            material.oneSide = false;

            //Planeオブジェクト生成
            planeObj = rootNode.addChild( new Plane( material, planeSize, planeSize, segment, segment) );
        }

        private function keyDownHandler(evt:KeyboardEvent):void {
            var a:int = 2;
            if(evt.keyCode == Keyboard.UP){
                valy -= a;
            }
            if(evt.keyCode == Keyboard.DOWN){
                valy += a;
            }
            if(evt.keyCode == Keyboard.LEFT){
                valx += a;
            }
            if(evt.keyCode == Keyboard.RIGHT){
                valx -= a;
            }
        }

        // _______________________________________________________________________
        //                                                                    Loop
        private function loop3D( event:Event ):void
        {
            //マウス座標でオブジェクトを回転
            planeObj.rotationY += valx;
            planeObj.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;
        }

    }
}