Files
planet/frontend/public/earth/js/object-callout-layout.js
linkong 1d153c2667
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / backend (pull_request) Has been cancelled
ci / frontend (pull_request) Has been cancelled
ci / delivery (push) Has been cancelled
ci / delivery (pull_request) Has been cancelled
release: bump version to 0.76.20
2026-11-10 17:52:24 +08:00

144 lines
5.7 KiB
JavaScript

const MARGIN = 14;
const clamp = (value, low, high) => Math.max(low, Math.min(Math.max(low, high), value));
export function placeObjectCallout(source, size, viewport, obstacles = []) {
const isMobile = viewport.width <= 820 || (typeof document !== "undefined" && document.body?.classList?.contains?.("layout-mode-mobile"));
const margin = isMobile ? 14 : MARGIN;
const width = Math.min(size.width, viewport.width - margin * 2);
const height = Math.min(size.height, viewport.height - margin * 2);
const globe = viewport.globe || { x: viewport.width / 2, y: viewport.height / 2, radius: 360 };
const globeX = Number.isFinite(globe.x) ? globe.x : viewport.width / 2;
const globeY = Number.isFinite(globe.y) ? globe.y : viewport.height / 2;
const globeR = Number.isFinite(globe.radius) ? globe.radius : Math.min(viewport.width, viewport.height) * 0.40;
const previousPosition = viewport.previousPosition || null;
const arbitratedSlot = viewport.arbitratedSlot || null;
if (isMobile) {
const topHudEdge = 76;
const bottomHudEdge = viewport.height - 110;
const preferAbove = source.y > viewport.height * 0.52;
const targetX = clamp(source.x - width / 2, margin, viewport.width - width - margin);
const targetY = preferAbove
? clamp(source.y - height - 36, topHudEdge, bottomHudEdge - height)
: clamp(source.y + 36, topHudEdge, bottomHudEdge - height);
return {
x: Math.round(targetX),
y: Math.round(targetY),
width,
height,
side: preferAbove ? "bottom" : "top",
};
}
// Safe HUD vertical bounds:
const topToastObs = obstacles.filter(o => o.y < 80 && o.height <= 80);
const topToastEdge = topToastObs.reduce((max, o) => Math.max(max, o.y + o.height), 0);
const topHudEdge = topToastEdge > 0 ? (topToastEdge + 12) : 64;
const bottomObs = obstacles.filter(o => o.y > viewport.height - 110);
const bottomHudEdge = bottomObs.length > 0
? bottomObs.reduce((min, o) => Math.min(min, o.y - 12), viewport.height - margin)
: viewport.height - margin;
// Safe HUD horizontal bounds:
const leftHudObs = obstacles.filter(o => o.x < 140 && o.y > 60 && o.height > 100);
const leftHudEdge = leftHudObs.reduce((max, o) => Math.max(max, o.x + o.width), 0);
const minX = Math.max(margin, leftHudEdge > 0 ? (leftHudEdge + 12) : margin);
const maxX = viewport.width - width - margin;
// 1. Flank determination (obeys arbitratedSlot when provided by shared stage arbitrator):
const prevX = previousPosition?.x;
const prevSide = Number.isFinite(prevX)
? (prevX + width * 0.5 >= globeX ? "right" : "left")
: null;
const FLANK_DEADBAND = 12;
let isRight;
if (arbitratedSlot?.side) {
isRight = arbitratedSlot.side === "right";
} else if (prevSide === "right") {
isRight = source.x >= globeX - FLANK_DEADBAND;
} else if (prevSide === "left") {
isRight = source.x > globeX + FLANK_DEADBAND;
} else {
isRight = source.x >= globeX;
}
// 2. Continuous vertical Y follow within assigned corridor:
const corridorMinY = Number.isFinite(arbitratedSlot?.corridor?.[0])
? Math.max(topHudEdge, arbitratedSlot.corridor[0])
: topHudEdge;
const corridorMaxY = Number.isFinite(arbitratedSlot?.corridor?.[1])
? Math.min(bottomHudEdge, arbitratedSlot.corridor[1])
: bottomHudEdge;
const minAllowedY = corridorMinY;
const maxAllowedY = Math.max(minAllowedY, corridorMaxY - height);
const dyAnchor = source.y - globeY;
const uy = clamp(dyAnchor / Math.max(1, globeR), -1, 1);
const continuousY = source.y - height * ((1 - uy) * 0.5);
let targetY = clamp(Math.round(continuousY), minAllowedY, maxAllowedY);
if (prevSide === (isRight ? "right" : "left") && Number.isFinite(previousPosition?.y)) {
const prevY = clamp(Math.round(previousPosition.y), minAllowedY, maxAllowedY);
if (Math.abs(targetY - prevY) <= 2) {
targetY = prevY;
}
}
// 3. Compact radial tether & corner overlap (连线不开远,靠近地球边缘,角覆盖地球 30px):
const midY = targetY + height * 0.5;
const dyMid = midY - globeY;
const rAtY = Math.abs(dyMid) < globeR ? Math.sqrt(globeR * globeR - dyMid * dyMid) : 0;
const overlapDepth = 30;
let targetX;
if (isRight) {
const rimX = globeX + rAtY;
const idealLeft = Math.max(rimX - overlapDepth, source.x + 24);
targetX = clamp(Math.round(idealLeft), minX, maxX);
} else {
const rimX = globeX - rAtY;
const idealRight = Math.min(rimX + overlapDepth, source.x - 24);
targetX = clamp(Math.round(idealRight - width), minX, maxX);
}
// 4. Static HUD obstacle avoidance when no arbitrated multi-card corridor is active:
if (!arbitratedSlot) {
const flankObstacles = obstacles.filter(obs =>
Math.max(targetX, obs.x) < Math.min(targetX + width, obs.x + obs.width)
);
const rect = { x: targetX, y: targetY, width, height };
const collidingObs = flankObstacles.find(obs =>
Math.max(rect.x, obs.x) < Math.min(rect.x + rect.width, obs.x + obs.width) &&
Math.max(rect.y, obs.y) < Math.min(rect.y + rect.height, obs.y + obs.height)
);
if (collidingObs) {
const aboveY = Math.round(collidingObs.y - height - 12);
const belowY = Math.round(collidingObs.y + collidingObs.height + 12);
const canAbove = aboveY >= topHudEdge;
const canBelow = belowY + height <= bottomHudEdge;
if (canAbove && canBelow) {
targetY = (Math.abs(aboveY - targetY) <= Math.abs(belowY - targetY)) ? aboveY : belowY;
} else if (canAbove) {
targetY = aboveY;
} else if (canBelow) {
targetY = belowY;
}
}
}
return {
x: targetX,
y: clamp(targetY, minAllowedY, maxAllowedY),
width,
height,
side: isRight ? "left" : "right",
stageSide: isRight ? "right" : "left",
corridor: [corridorMinY, corridorMaxY],
};
}