Files
cocos_zhuzhan/assets/scripts/core/EmbeddedView.ts

49 lines
2.0 KiB
TypeScript

import { Camera, director, find, view, ResolutionPolicy } from 'cc';
import { DESIGN_WIDTH, DESIGN_HEIGHT } from './GridConstants';
/**
* 编辑器内嵌左栏:先 setFrameSize 铺满容器,再 FIXED_WIDTH 按宽适配(与 test.001code.com 一致)。
* 须配合 loader 在 resize 时调用 view.setFrameSize(clientW, clientH)。
*/
export function applyEmbeddedDesignResolution(): void {
view.resizeWithBrowserSize(true);
const frame = view.getFrameSize();
if (frame.width <= 0 || frame.height <= 0) {
view.setDesignResolutionSize(DESIGN_WIDTH, DESIGN_HEIGHT, ResolutionPolicy.FIXED_WIDTH);
syncEmbeddedCamerasOrtho();
return;
}
view.setDesignResolutionSize(DESIGN_WIDTH, DESIGN_HEIGHT, ResolutionPolicy.FIXED_WIDTH);
syncEmbeddedCamerasOrtho();
}
/** 内嵌画布当前可视半高(设计坐标) */
export function getEmbeddedOrthoHalf(): number {
const vis = view.getVisibleSize();
return (vis.height > 0 ? vis.height : DESIGN_HEIGHT) / 2;
}
/** 主相机 / 背景 / HUD 相机正交高度须与可视高度一致,否则关卡与 HUD 错位 */
export function syncEmbeddedCamerasOrtho(): void {
const halfH = getEmbeddedOrthoHalf();
const scene = director.getScene();
if (!scene) return;
for (const camName of ['UICamera', 'BgCamera']) {
const cam = find(camName, scene)?.getComponent(Camera);
if (cam) cam.orthoHeight = halfH;
}
const preserve = !!(globalThis as { __tfrhPreserveMapView?: boolean }).__tfrhPreserveMapView;
const mainNode = find('Main Camera', scene);
const mainCam = mainNode?.getComponent(Camera);
if (!preserve) {
if (mainCam) mainCam.orthoHeight = halfH;
if (mainNode) mainNode.setPosition(0, 0, mainNode.position.z);
}
(globalThis as { __tfrhSyncHudOrtho?: () => void }).__tfrhSyncHudOrtho = syncEmbeddedCamerasOrtho;
}
/** @deprecated 使用 syncEmbeddedCamerasOrtho */
export function syncHudCameraOrtho(): void {
syncEmbeddedCamerasOrtho();
}