
const { ccclass, property } = cc._decorator;

@ccclass
export default class Adapt extends cc.Component {
    @property(cc.Widget)
    widget: cc.Widget = null

    private _dw = 750
    private _dh = 1334

    /**
     * 专门针对  竖屏  h5 适配
     */

    protected onLoad(): void {
        if(cc.sys.platform === cc.sys.DESKTOP_BROWSER || cc.sys.platform === cc.sys.MOBILE_BROWSER){
            this._adapt()
        }
    }

    private _adapt() {
        // 设计尺寸比例
        let drate = this._dw / this._dh
        // 实际尺寸比例
        let preate = cc.winSize.width / cc.winSize.height

        if (preate <= drate) {
            cc.view.setDesignResolutionSize(this._dw, cc.winSize.height, cc.ResolutionPolicy.FIXED_WIDTH);
            this.widget.left = 0
            this.widget.right = 0
        } else {
            let r = cc.winSize.height / this._dh
            cc.view.setDesignResolutionSize(this._dw / r, this._dh / r, cc.ResolutionPolicy.FIXED_WIDTH);
            let gap = cc.winSize.width - this._dw
            this.widget.left = gap / 2
            this.widget.right = gap / 2
        }
        this.widget.updateAlignment();
    }
}
