import EventMgr from "../Module/EventMgr/EventMgr";
import { generateUUID } from "../Utils/util";

/**
 * 背景适配组建
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class BGView extends cc.Component {
	protected onLoad(): void {
		this.setBgScale();
		//注册事件 当尺寸变化时重新适配
		EventMgr.reg("adjustUI", () => {
			this.setBgScale();
		}, generateUUID());
	}

	//背景适配
	setBgScale() {
		// 1. 先找到 SHOW_ALL 模式适配之后，本节点的实际宽高以及初始缩放值
		let scaleForShowAll = Math.min(
			cc.view.getCanvasSize().width / this.node.width,
			cc.view.getCanvasSize().height / this.node.height
		);
		let realWidth = this.node.width * scaleForShowAll;
		let realHeight = this.node.height * scaleForShowAll;

		// 2. 基于第一步的数据，再做缩放适配
		this.node.scale = Math.max(
			cc.view.getCanvasSize().width / realWidth,
			cc.view.getCanvasSize().height / realHeight
		);
	}
}
