
export class JoystickModule extends Laya.EventDispatcher {
    private _InitX: number;
    private _InitY: number;
    private _startStageX: number;
    private _startStageY: number;
    private _lastStageX: number;
    private _lastStageY: number;
    private _button:Laya.Point;
    private touchId: number;
    private _curPos: Laya.Point;
    // private _center:fairygui.GImage;
    
    public static JoystickMoving: string = "JoystickMoving";
    public static JoystickUp: string = "JoystickUp";

    public radius: number;
    private _clickpanel:fairygui.GLoader;
    public dis:number = 0; 
    public degree:number = 0;
    public angle:number = 0;

    public offx:number = 0;


    public constructor(clickpanel:fairygui.GLoader) {
        super();
        this._clickpanel = clickpanel;
        this._button = new Laya.Point();
        this.touchId = -1;
        this.radius = 400;
        this._curPos = new Laya.Point();
        this._clickpanel.on(Laya.Event.MOUSE_DOWN,this, this.onTouchDown);
    }

    private _iscanCBack:boolean = false;
    private onTouchDown(evt: Laya.Event) {
        if(this.touchId == -1) {//First touch
            this.touchId = evt.touchId;
            this.offx = 0;
            this._iscanCBack = false;
            fairygui.GRoot.inst.globalToLocal(Laya.stage.mouseX, Laya.stage.mouseY,this._curPos);
            var bx: number = this._curPos.x;
            var by: number = this._curPos.y;
            this._lastStageX = bx;
            this._lastStageY = by;
            this._startStageX = bx;
            this._startStageY = by;
            this._button.x = bx;
            this._button.y = by;
            if( this._callobj && this._downCallback )
            {
                this._downCallback.call( this._callobj );
            }
            this._clickpanel.on(Laya.Event.MOUSE_MOVE,this,this.OnTouchMove);
            this._clickpanel.on(Laya.Event.MOUSE_UP,this,this.OnTouchUp);
            this._clickpanel.on( Laya.Event.MOUSE_OUT,this,this.OnTouchUp );
        }
    }


    private _callobj:any;
    private _movecallback:Function;
    private _updatecallback:Function;
    private _downCallback:Function;
    public setEventCallback( callobj:any,downCallback,movecallback:Function,upcallback:Function ):void
    {
        this._callobj = callobj;
        this._downCallback = downCallback;
        this._movecallback = movecallback;
        this._updatecallback = upcallback;
    }

    public Trigger(evt: Laya.Event): void {
        this.onTouchDown(evt);
    }

    /************************************************* */
    private tiledMap:Laya.TiledMap;
		private mLastMouseX: number = 0;
		private mLastMouseY: number = 0;
		private mX: number = 0;
		private mY: number = 0;
    	//创建地图
		private createMap() {
			//创建地图对象
			this.tiledMap = new Laya.TiledMap();

			this.mX = this.mY = 0;
			//创建地图，适当的时候调用destory销毁地图
			this.tiledMap.createMap("res/tiledMap/desert.json", new Laya.Rectangle(0, 0, Laya.Browser.width, Laya.Browser.height), new Laya.Handler(this, this.completeHandler));
		}
		/**
		 * 地图加载完成的回调
		 */
		private completeHandler(): void {
			console.log("地图创建完成");
			console.log("ClientW:" + Laya.Browser.clientWidth + " ClientH:" + Laya.Browser.clientHeight);
        }
          /************************************************* */



   

    private OnTouchUp(evt:Laya.Event=null): void {
        if(this.touchId != -1 && evt.touchId == this.touchId) {
            this.touchup();
            if( this._callobj && this._updatecallback )
            {
                this._updatecallback.call( this._callobj );
            }
        }
    }

    private _iacanCallback:boolean = false;
    public touchup():void
    {
        this._iscanCBack = false;
        this.touchId = -1;
        this._clickpanel.off(Laya.Event.MOUSE_MOVE,this,this.OnTouchMove);
        this._clickpanel.off(Laya.Event.MOUSE_UP,this,this.OnTouchUp);
        this._clickpanel.off( Laya.Event.MOUSE_OUT,this,this.OnTouchUp );
        this._iacanCallback = false;
        this.degree = 0;
    }

    private OnTouchMove(evt: Laya.Event): void {
        if(this.touchId != -1 && evt.touchId == this.touchId) {
            var bx: number = Laya.stage.mouseX;
            var by: number = Laya.stage.mouseY;
            var moveX: number = bx - this._lastStageX;
            var moveY: number = by - this._lastStageY;
            this._lastStageX = bx;
            this._lastStageY = by;
            var buttonX: number = this._button.x + moveX;
            var buttonY: number = this._button.y + moveY;

            var offsetX: number = buttonX - this._startStageX;
            var offsetY: number = buttonY - this._startStageY;
            var disX:number = 0;
            if( moveX >= 2 || moveX <= -2 )
            {
                this._iscanCBack = true;
            }
            else
            {
                this._iscanCBack = false;
            }
            this.offx = moveX;
            if( offsetX == 0 && offsetY == 0 )
            {
                return;
            }
            if( offsetX > 0 )
            {
                if( offsetX > this.radius )
                {
                    disX = this.radius;
                    this.angle = 90;
                }
                else
                {
                    disX = this.radius - offsetX;
                    this.angle = 90 - disX/this.radius * 90;
                }
            }
            else if( offsetX == 0 )
            {
                this.angle = 0;
            }
            else
            {
                if( -offsetX > this.radius )
                {
                    disX = this.radius;
                    this.angle = -90;
                }
                else
                {
                    disX = this.radius + offsetX;
                    this.angle = -(90-disX/this.radius * 90);
                }
            }
            var rad: number = Math.atan2(offsetY,offsetX);
            this.degree = rad;
            var maxX: number = this.radius * Math.cos(rad);
            var maxY: number = this.radius * Math.sin(rad);
            if(Math.abs(offsetX) > Math.abs(maxX))
                offsetX = maxX;
            if(Math.abs(offsetY) > Math.abs(maxY))
                offsetY = maxY;
            buttonX = this._startStageX + offsetX;
            buttonY = this._startStageY + offsetY;
            if(buttonX < 0)
                buttonX = 0;
            if(buttonY > fairygui.GRoot.inst.height)
                buttonY = fairygui.GRoot.inst.height;
            this._button.x = buttonX;
            this._button.y = buttonY;
            this.dis = Math.sqrt( offsetX*offsetX + offsetY*offsetY );
            this._iacanCallback = true;
            if( this._iscanCBack && this._callobj && this._movecallback && this._iacanCallback )
            {
                this._movecallback.call( this._callobj,this.offx );
            }
        }
    }

    public dispose():void
    {
        this._clickpanel.off(Laya.Event.MOUSE_DOWN,this, this.onTouchDown);
    }
}
