import EventMgr from "../Module/EventMgr/EventMgr";
import { Singleton } from "../Utils/Singleton";

const WIDTH = 1280
const HIGHT = 720

export class View extends Singleton {
	Init() {
		//适配一次
		this.initSize();
		//注册事件 当尺寸变化时重新适配
		EventMgr.reg("adjustUI", () => {
			this.initSize();
		}, 1);
	}

	initSize() {
		//根据屏幕大小决定适配策略
		//想明白原理，请阅读本文 https://blog.csdn.net/qq_36720848/article/details/89742451
		var s = cc.view.getFrameSize();
		var rw = s.width;
		var rh = s.height;
		var finalW = rw;
		var finalH = rh;

		if ((rw / rh) > (WIDTH / HIGHT)) {
			//!#zh: 是否优先将设计分辨率高度撑满视图高度。 */
			//cvs.fitHeight = true;

			//如果更长，则用定高
			finalH = HIGHT;
			finalW = finalH * rw / rh;
		}
		else {
			/*!#zh: 是否优先将设计分辨率宽度撑满视图宽度。 */
			//cvs.fitWidth = true;
			//如果更短，则用定宽
			finalW = WIDTH;
			finalH = rh / rw * finalW;
		}


		cc.view.setDesignResolutionSize(finalW, finalH, cc.ResolutionPolicy.UNKNOWN);
		let cvs = cc.find('Canvas')!.getComponent(cc.Canvas);
		cvs.designResolution = cc.size(finalW, finalH);
		cvs.node.width = finalW;
		cvs.node.height = finalH;
		cvs.node.emit("resize");
	}
}



