import { ui } from "../ui/layaMaxUI";
import { EventManager } from "../game_module/EventManager";
import { RoleController } from "../game_logic/RoleController";

export class game_ui extends ui.game_uiUI implements UI_Interface {



    constructor() {
        super();
        this.resizeHander();
        EventManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    resizeHander() {
        this.height = Laya.stage.height;
    }
    OnShow(data?: any) {
        this.UiEventHandler();
    }

    UiEventHandler() {
        this.on(Laya.Event.MOUSE_DOWN, this, this.OnClickDown);
        this.on(Laya.Event.MOUSE_UP, this, this.OnClickUp);
        this.on(Laya.Event.MOUSE_OUT, this, this.OnClickUp);
    }

    private isdown=false;
    OnClickDown(e: Laya.Event) {
        if (e.target instanceof Laya.UIComponent) return;
        this.isdown=true;
        this.on(Laya.Event.MOUSE_MOVE, this, this.OnSlider);
        this.curMousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);
    }
    OnClickUp() {
        if(!this.isdown)return;
        this.isdown=false;
        this.off(Laya.Event.MOUSE_MOVE, this, this.OnSlider);
    }

    curMousePos: Laya.Vector2;
    OnSlider() {
        var MousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);
        var offestX = MousePos.x - this.curMousePos.x;
        if (offestX > 0) offestX = offestX < 5 ? offestX : 5;
        if (offestX < 0) offestX = offestX > -5 ? offestX : -5;
        var rotateDir = new Laya.Vector3(0, -offestX * 0.01, 0);
        RoleController.Instance.RoleRotation(rotateDir);
        this.curMousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);;
    }
    OnClose() {
        EventManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        this.off(Laya.Event.MOUSE_DOWN, this, this.OnClickDown);
        this.off(Laya.Event.MOUSE_UP, this, this.OnClickUp);
        this.off(Laya.Event.MOUSE_OUT, this, this.OnClickUp);
    }
}