/*
The MIT License

Copyright (c) 2010-2012 aaharu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package urulab.fp10.core
{
    import away3d.cameras.HoverCamera3D;
    import away3d.containers.Scene3D;
    import away3d.containers.View3D;
    import away3d.core.base.Object3D;
    import away3d.core.clip.RectangleClipping;
    import away3d.core.math.Number3D;
    import away3d.lights.DirectionalLight3D;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    /**
     * 
     * UruLab3Dクラスは「仮想漆器工房うるらぼ」用のクラスです。
     * Away3D3.5を使用しています。
     * 
     * @author aaharu
     */
    public class UruLab3D extends Sprite
    {
        // _■____■____________■__________■_______
        // _■____■____________■__________■_______
        // _■____■____________■__________■_______
        // _■____■_■_■_■___■__■_____■■■__■■■■____
        // _■____■_■■__■___■__■________■_■___■___
        // _■____■_■___■___■__■_____■■■■_■___■___
        // _■____■_■___■___■__■____■___■_■___■___
        // _■____■_■___■__■■__■____■___■_■___■___
        // __■■■■__■____■■_■__■■■■__■■■■_■■■■___■
        
        // テクスチャの大きさ
        public static const TEX_WIDTH:int = 1000;
        public static const TEX_HEIGHT:int = 1000;
        // Away3D用
        private var _scene:Scene3D = new Scene3D();
        private var _camera:HoverCamera3D;
        private var _view:View3D;
        // ライト
        private var _light:DirectionalLight3D;
        private var _lightDirection:Number3D;
        //private var _lightDirection:Vector3D;
        // カメラ移動用変数
        private var lastPanAngle:Number;
        private var lastTiltAngle:Number;
        private var lastMouseX:Number;
        private var lastMouseY:Number;
        // カメラのターゲット
        private var _target:Object3D;
        // stageのマウスダウン判定用変数
        private var isMouseDown:Boolean = false;
        // stageの大きさみたいなもの
        private var sWidth:int;
        private var sHeight:int;
        
        /**
         * sceneのgetter
         * @return Scene3D
         */
        public function get scene():Scene3D
        {
            return _scene;
        }
        
        /**
         * constructor
         * @param w 表示幅
         * @param h 表示高さ
         */
        public function UruLab3D(w:int, h:int)
        {
            super();
            sWidth = w;
            sHeight = h;
            if(stage)
                initEngine();
            else
                // stageがnullになる場合の対策
                addEventListener(Event.ADDED_TO_STAGE, initEngine);
        }
        
        /**
         * Initialize the engine
         * @param e
         */
        private function initEngine(e:Event = null):void
        {
            // EnterFrameイベント 描画はこっちで
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            
            // カメラ初期設定
            _camera = new HoverCamera3D();
            _camera.zoom = 2;
            _camera.focus = 250;
            _camera.distance = 45;
            _camera.minTiltAngle = -90;
            _camera.panAngle = -10;
            _camera.tiltAngle = 20;
            _target = new Object3D();
            _camera.target = _target;
            
            _view = new View3D({ camera: _camera, scene: _scene, stats: false, x: sWidth>>1, y: sHeight>>1 });
            _view.clipping = new RectangleClipping({ minX: -sWidth>>1, minY: -sHeight>>1, maxX: sWidth>>1, maxY: sHeight>>1 });
            addChild(_view);
            initLights();
        }
        
        /**
         * Initialise the lights
         */
        private function initLights():void
        {
            _light = new DirectionalLight3D();
            _light.color = 0xffffff;
            _light.ambient = 0.25;
            _light.diffuse = 0.75;
            _light.specular = 0.9;
            //_light.direction = _lightDirection = new Vector3D(-1, -1, -1);
            _light.direction = _lightDirection = new Number3D(-1, -1, -1);
            _scene.addLight(_light);
        }
        
        
        /**
         * render loop
         * @param e
         */
        public function onEnterFrame(e:Event):void {
            // update the camera position
            if(isMouseDown)
            {
                _camera.panAngle = 0.35 * (stage.mouseX - lastMouseX) + lastPanAngle;
                _camera.tiltAngle = 0.35 * (stage.mouseY - lastMouseY) + lastTiltAngle;
            }
            _lightDirection.x = -_camera.x;
            _lightDirection.y = -_camera.y;
            _lightDirection.z = -_camera.z;
            _light.direction = _lightDirection;
            _camera.hover();
            _view.render();
        }
        
        /**
         * カメラ移動用のマウスダウンイベントリスナー
         * @param e
         */
        public function onMouseDown(e:MouseEvent):void
        {
            lastPanAngle = _camera.panAngle;
            lastTiltAngle = _camera.tiltAngle;
            lastMouseX = stage.mouseX;
            lastMouseY = stage.mouseY;
            isMouseDown = true;
        }
        
        /**
         * カメラ移動用のマウスアップイベントリスナー
         * @param e
         */
        public function onMouseUp(e:MouseEvent):void
        {
            isMouseDown = false;
        }
        
        /**
         * ズームイン
         */
        public function zoomIn():void
        {
            _camera.zoom++;
        }
        
        /**
         * ズームアウト
         */
        public function zoomOut():void
        {
            if(_camera.zoom > 1)
                _camera.zoom--;
        }
        
        /**
         * カメラターゲット移動
         * @param value 移動幅
         */
        public function cameraUp(value:Number):void
        {
            _target.y += value;
        }
        
        /**
         * カメラターゲット移動
         * @param value 移動幅
         */
        public function cameraDown(value:Number):void
        {
            _target.y -= value;
        }
        
        /**
         * カメラターゲット移動
         * @param value 移動幅
         */
        public function cameraRight(value:Number):void
        {
            _target.x -= value;
        }
        
        /**
         * カメラターゲット移動
         * @param value 移動幅
         */
        public function cameraLeft(value:Number):void
        {
            _target.x += value;
        }
        
        /**
         * カメラターゲットリセット
         */
        public function cameraReset():void
        {
            _target.x = _target.y = 0;
            _camera.panAngle = -10;
            _camera.tiltAngle = 20;
        }
    }
}