feat(ui-migration): 坐标换算与水平锚点三区推断

左上原点/左上锚点 → Cocos 中心原点/中心锚点(0.5,0.5)。
fitHeight 下垂直永不溢出, 故只推断水平锚点。
背景四边拉伸; 越界对象只标记不裁剪。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-28 00:41:35 +08:00
co-authored by Claude Opus 5
parent 8901221e09
commit f2e766a89a
2 changed files with 119 additions and 0 deletions
@@ -0,0 +1,46 @@
/**
* 坐标换算与锚点推断。见 spec §2。
* 旧引擎:原点左上、Y 向下、精灵锚点左上。
* Cocos:原点在父节点中心、Y 向上、节点锚点 (0.5, 0.5)。
*/
export const DESIGN_WIDTH = 1280;
export const DESIGN_HEIGHT = 720;
const HALF_W = DESIGN_WIDTH / 2;
const HALF_H = DESIGN_HEIGHT / 2;
const LEFT_ZONE = DESIGN_WIDTH / 4; // 320
const RIGHT_ZONE = (DESIGN_WIDTH * 3) / 4; // 960
/** 左上坐标 → Cocos 中心锚点坐标。spec §2.2。 */
export function toCocosPosition(left, top, width, height) {
return {
x: left + width / 2 - HALF_W,
y: HALF_H - top - height / 2,
};
}
/**
* 水平锚点三区推断。spec §2.3。
* fitHeight 下垂直方向永远精确撑满,故不推断垂直锚点。
*/
export function inferWidget(left, width) {
const cx = left + width / 2;
if (cx < LEFT_ZONE) return { horizontal: 'left', left };
if (cx > RIGHT_ZONE) return { horizontal: 'right', right: DESIGN_WIDTH - (left + width) };
return { horizontal: 'center', horizontalCenter: cx - HALF_W };
}
/** 背景与遮罩:四边拉伸。spec §2.4。 */
export function stretchWidget() {
return { horizontal: 'stretch', vertical: 'stretch', left: 0, right: 0, top: 0, bottom: 0 };
}
export function isFullScreen(width, height) {
return width >= DESIGN_WIDTH && height >= DESIGN_HEIGHT;
}
/** 坐标超出画布。spec §2.5:不裁剪,只标记。 */
export function isOffscreen(left, top, width, height) {
return left < 0 || top < 0 || left + width > DESIGN_WIDTH || top + height > DESIGN_HEIGHT;
}