import { EventMgr } from "../../Script/Core/Base/EventMgr";
import { Touch_Start, Touch_Move, Touch_End } from "./eventDef";

const {ccclass, property} = cc._decorator;

@ccclass
export default class touch extends cc.Component {

    /*===========================自动绑定组件开始===========================*/
	/*===========================自动绑定组件结束===========================*/

    /*===========================自动生成按钮事件开始==========================*/

	/*===========================自动生成按钮事件结束==========================*/


    touchId: number = -1;

    protected onEnable(): void {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onTouchStart(event: cc.Event.EventTouch) {
        if(this.touchId != -1) return;

        this.touchId = event.getID();
        EventMgr.inst.emit(Touch_Start);
    }
    onTouchMove(event: cc.Event.EventTouch) {
        if(this.touchId != event.getID()) return;

        let offset = event.getLocation().sub(event.getStartLocation());
        EventMgr.inst.emit(Touch_Move, offset);
    }
    onTouchEnd(event: cc.Event.EventTouch) {
        if(this.touchId != event.getID()) return;

        this.touchId = -1;
        EventMgr.inst.emit(Touch_End);
    }
}