import { Node, ResolutionPolicy, screen, UITransform, Vec3, view } from 'cc'; declare const wx: any; export const DESIGN_W = 750; export const DESIGN_H = 1334; export interface ScreenMetrics { visibleW: number; visibleH: number; /** 顶部安全区高度(含刘海/灵动岛,设计坐标) */ topInset: number; /** 微信右上角胶囊下沿距屏幕顶部的安全距离(设计坐标) */ capsuleBottom: number; /** 底部安全区高度(含 iPhone Home 手势条,设计坐标) */ bottomInset: number; /** 安全区实际可用高度(safeRoot 局部坐标系中 >= 1334,全面屏自动舒展消除上下大空白) */ safeHeight: number; /** 安全区半高:safeRoot 内顶安全边 = +safeHalfH,底安全边 = -safeHalfH */ safeHalfH: number; /** 超出 1334 基准的额外垂直空间(用于均匀拉宽中间板块间距) */ extraH: number; /** 主内容区整体缩放系数(仅超短屏 + 胶囊时自动收拢防裁切) */ contentScale: number; /** safeRoot 中心在屏幕中的 Y 偏移 */ contentOffsetY: number; /** 背景图全屏铺满(cover)所需缩放系数 */ bgCoverScale: number; isWeChat: boolean; /** 浏览器预览调试参数 ?wxsim=iphone15 / ?wxsim=iphonese 时的模拟胶囊信息 */ mockCapsule?: { top: number; height: number; width: number; right: number }; } function getWeChatSimMode(): 'iphone15' | 'iphonese' | null { try { if (typeof window === 'undefined' || !window.location?.search) return null; const params = new URLSearchParams(window.location.search); const val = (params.get('wxsim') || '').toLowerCase(); if (val === 'iphone15' || val === '15') return 'iphone15'; if (val === 'iphonese' || val === 'se') return 'iphonese'; } catch { // Ignore non-browser environments. } return null; } /** * 自适应分辨率策略: * - 竖屏手机(比例 <= 9:16,如 16:9 / 19.5:9 / 20:9)使用 FIXED_WIDTH,消除全面屏上下黑边; * - 平板 / 折叠屏展开 / 桌面横屏预览(比例 > 9:16)使用 FIXED_HEIGHT,确保上下完整不裁切。 */ export function applyResponsivePolicy(): void { const frame = screen.windowSize; if (!frame || frame.width <= 0 || frame.height <= 0) return; const aspect = frame.width / frame.height; const designAspect = DESIGN_W / DESIGN_H; const policy = aspect <= designAspect + 0.005 ? ResolutionPolicy.FIXED_WIDTH : ResolutionPolicy.FIXED_HEIGHT; view.setDesignResolutionSize(DESIGN_W, DESIGN_H, policy); } /** * 统一读取微信小游戏胶囊按钮(getMenuButtonBoundingClientRect)与安全区(getWindowInfo / getSystemInfoSync), * 并换算为 Cocos 设计分辨率坐标系。 */ export function computeScreenMetrics(): ScreenMetrics { const vis = view.getVisibleSize(); const visibleW = Math.max(DESIGN_W, vis.width || DESIGN_W); const visibleH = Math.max(DESIGN_H, vis.height || DESIGN_H); let topInset = 0; let capsuleBottom = 0; let bottomInset = 0; let isWeChat = false; let mockCapsule: ScreenMetrics['mockCapsule']; if (typeof wx !== 'undefined') { isWeChat = true; try { const winInfo = typeof wx.getWindowInfo === 'function' ? wx.getWindowInfo() : typeof wx.getSystemInfoSync === 'function' ? wx.getSystemInfoSync() : null; const winH = Number(winInfo?.windowHeight) || Number(winInfo?.screenHeight) || 0; const scaleY = winH > 0 ? visibleH / winH : visibleW / (Number(winInfo?.windowWidth) || 375); const safe = winInfo?.safeArea; if (safe && winH > 0) { topInset = Math.max(0, Number(safe.top) || 0) * scaleY; const rawBottom = winH - (Number(safe.bottom) || winH); bottomInset = Math.max(0, rawBottom) * scaleY; } else if (winInfo?.statusBarHeight) { topInset = Math.max(0, Number(winInfo.statusBarHeight) || 0) * scaleY; } if (typeof wx.getMenuButtonBoundingClientRect === 'function') { const menu = wx.getMenuButtonBoundingClientRect(); if (menu && Number(menu.bottom) > 0) { // 紧贴胶囊下沿留 10px 轻呼吸距,避免顶部出现大块空白 capsuleBottom = Number(menu.bottom) * scaleY + 10; } } if (capsuleBottom <= 0) { capsuleBottom = Math.max(topInset + 76, 108); } } catch { capsuleBottom = 112; bottomInset = 32; } } else { const sim = getWeChatSimMode(); if (sim === 'iphone15') { isWeChat = true; topInset = 98; capsuleBottom = 172; bottomInset = 52; mockCapsule = { top: 98, height: 62, width: 168, right: 20 }; } else if (sim === 'iphonese') { isWeChat = true; topInset = 38; capsuleBottom = 112; bottomInset = 0; mockCapsule = { top: 38, height: 64, width: 174, right: 18 }; } } const effectiveTop = Math.max(topInset, capsuleBottom); // 底部手势条只保留必要安全高度(上限 54px),避免底部产生大块空木板 const effectiveBottom = Math.min(54, Math.max(0, bottomInset)); const availableH = Math.max(640, visibleH - effectiveTop - effectiveBottom); const contentScale = availableH < DESIGN_H ? Math.max(0.78, availableH / DESIGN_H) : 1; const safeHeight = availableH >= DESIGN_H ? Math.min(1560, availableH) : DESIGN_H; const safeHalfH = safeHeight / 2; const extraH = Math.max(0, safeHeight - DESIGN_H); const safeTopY = visibleH / 2 - effectiveTop; const safeBottomY = -visibleH / 2 + effectiveBottom; const contentOffsetY = (safeTopY + safeBottomY) / 2; const bgCoverScale = Math.max(visibleW / DESIGN_W, visibleH / DESIGN_H); return { visibleW, visibleH, topInset, capsuleBottom, bottomInset, safeHeight, safeHalfH, extraH, contentScale, contentOffsetY, bgCoverScale, isWeChat, mockCapsule, }; } /** 将主内容容器精准居中并缩放到安全区(避开微信胶囊 + 刘海 + 底部手势条) */ export function applySafeContentTransform(contentRoot: Node, m: ScreenMetrics): void { const ui = contentRoot.getComponent(UITransform); if (ui) ui.setContentSize(DESIGN_W, m.safeHeight); contentRoot.setPosition(0, m.contentOffsetY, 0); contentRoot.setScale(new Vec3(m.contentScale, m.contentScale, 1)); } /** 将背景精灵按屏幕比例等比铺满(cover),消除全面屏上下黑边 */ export function applyCoverBackground(bgNode: Node, m: ScreenMetrics): void { bgNode.setPosition(0, 0, 0); bgNode.setScale(new Vec3(m.bgCoverScale, m.bgCoverScale, 1)); } /** 将遮罩节点铺满全屏(兼容任意长宽比) */ export function applyFullScrimSize(scrimNode: Node, m: ScreenMetrics): void { const ui = scrimNode.getComponent(UITransform); if (ui) ui.setContentSize(Math.max(2400, m.visibleW + 40), Math.max(2400, m.visibleH + 40)); }