const { ccclass, property } = cc._decorator;

//定义
export enum Direction {
    UP = 1,
    Down = 2,
    Left = 3,
    Right = 4,
};

@ccclass
export default class PopUp extends cc.Component {

    @property({ type: cc.Enum(Direction) })
    direction = Direction.Down;

    @property
    duration = 0.5;

    @property
    delay = 0;

    protected orgX = 0;
    protected orgY = 0;
    onEnable() {
        let widget = this.node.getComponent(cc.Widget);
        if (widget) {
        widget.updateAlignment();
            widget.enabled = false;
        }
        this.popIn();
    }

    public popIn() {
        this.orgX = this.node.x;
        this.orgY = this.node.y;
        if (this.direction == Direction.UP) {
            this.node.y = cc.winSize.height;
        } else if (this.direction == Direction.Down) {
            this.node.y = -cc.winSize.height;
        } else if (this.direction == Direction.Left) {
            this.node.x = -cc.winSize.width;
        } else {
            this.node.x = cc.winSize.width;
        }
        
        cc.tween(this.node)
            .delay(this.delay)
            .to(this.duration, { x: this.orgX, y: this.orgY }, { easing: "backOut" })
            .call(() => {
                let widget = this.node.getComponent(cc.Widget);
                if (widget) {
                    widget.enabled = true;
                    widget.updateAlignment();
                }
            })
            .start();
    }

}