mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
The pan used Cesium's QUADRATIC_OUT; through the camera port it took cubicOut, the contract's nearest ease. The contract gains quadraticOut, which Cesium maps to QUADRATIC_OUT and the stub camera computes.
263 lines
8.3 KiB
JavaScript
263 lines
8.3 KiB
JavaScript
import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js';
|
|
import { horizonFrom } from '../geo/horizon.js';
|
|
import { add, sub } from '../geo/vector.js';
|
|
import { canvasOf, findCamera, findQuery } from '../render/rendererOf.js';
|
|
import { WORLD_OVERLAY_OCCLUDER_SELECTORS } from '../overlays/worldOverlayChrome.js';
|
|
|
|
// DOM cards also avoid the HUD edges and responsive sheet containers.
|
|
export const WILDLIFE_CARD_CHROME_SELECTORS = Object.freeze([
|
|
...WORLD_OVERLAY_OCCLUDER_SELECTORS,
|
|
'.hud-left-edge',
|
|
'.hud-right-edge',
|
|
'#right-panel-stack',
|
|
'.gev-panel-rail',
|
|
'#location-bar',
|
|
'#control-panel',
|
|
'#param-slider-panel',
|
|
'#cockpit-hud .cockpit-topline',
|
|
'[data-mobile-sheet]',
|
|
]);
|
|
const GAP = 12;
|
|
const EDGE = 6;
|
|
export const WILDLIFE_SELECTED_CARD_WIDTH = 258;
|
|
|
|
const overlaps = (a, b) =>
|
|
a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;
|
|
const contains = (r, p) =>
|
|
p.x >= r.left && p.x <= r.right && p.y >= r.top && p.y <= r.bottom;
|
|
const clamp = (value, low, high) => Math.max(low, Math.min(high, value));
|
|
const rectAt = (left, top, width, height) => ({
|
|
left,
|
|
top,
|
|
right: left + width,
|
|
bottom: top + height,
|
|
width,
|
|
height,
|
|
});
|
|
|
|
/** Read visible chrome rectangles inside the viewer's usable screen area. */
|
|
export function readWildlifeCardSafeArea({
|
|
documentRef,
|
|
viewer,
|
|
windowRef = globalThis.window,
|
|
}) {
|
|
const canvasRect = canvasOf(viewer)?.getBoundingClientRect?.();
|
|
const width =
|
|
windowRef?.innerWidth || documentRef.documentElement.clientWidth;
|
|
const height =
|
|
windowRef?.innerHeight || documentRef.documentElement.clientHeight;
|
|
const left = Math.max(0, canvasRect?.left || 0) + EDGE;
|
|
const top = Math.max(0, canvasRect?.top || 0) + EDGE;
|
|
const right =
|
|
Math.min(width, canvasRect?.width > 0 ? canvasRect.right : width) - EDGE;
|
|
const bottom =
|
|
Math.min(height, canvasRect?.height > 0 ? canvasRect.bottom : height) -
|
|
EDGE;
|
|
const bounds = rectAt(
|
|
left,
|
|
top,
|
|
Math.max(0, right - left),
|
|
Math.max(0, bottom - top),
|
|
);
|
|
const obstacles = [];
|
|
const elements = [
|
|
...documentRef.querySelectorAll(WILDLIFE_CARD_CHROME_SELECTORS.join(',')),
|
|
];
|
|
for (const element of elements) {
|
|
let hidden = false;
|
|
for (
|
|
let node = element;
|
|
node && node !== documentRef.body;
|
|
node = node.parentElement
|
|
) {
|
|
const style = windowRef?.getComputedStyle?.(node);
|
|
if (
|
|
node.hidden ||
|
|
style?.display === 'none' ||
|
|
style?.visibility === 'hidden' ||
|
|
style?.opacity === '0'
|
|
) {
|
|
hidden = true;
|
|
break;
|
|
}
|
|
}
|
|
if (hidden) continue;
|
|
const rect = element.getBoundingClientRect();
|
|
if (!(rect.width > 0 && rect.height > 0) || !overlaps(rect, bounds))
|
|
continue;
|
|
obstacles.push(
|
|
rectAt(
|
|
rect.left - EDGE,
|
|
rect.top - EDGE,
|
|
rect.width + EDGE * 2,
|
|
rect.height + EDGE * 2,
|
|
),
|
|
);
|
|
}
|
|
return { bounds, obstacles, elements };
|
|
}
|
|
|
|
function nearestSafeAnchor(point, { bounds, obstacles }) {
|
|
const xs = [clamp(point.x, bounds.left + GAP, bounds.right - GAP)];
|
|
const ys = [clamp(point.y, bounds.top + GAP, bounds.bottom - GAP)];
|
|
for (const rect of obstacles) {
|
|
xs.push(
|
|
clamp(rect.left - GAP, bounds.left + GAP, bounds.right - GAP),
|
|
clamp(rect.right + GAP, bounds.left + GAP, bounds.right - GAP),
|
|
);
|
|
ys.push(
|
|
clamp(rect.top - GAP, bounds.top + GAP, bounds.bottom - GAP),
|
|
clamp(rect.bottom + GAP, bounds.top + GAP, bounds.bottom - GAP),
|
|
);
|
|
}
|
|
return (
|
|
xs
|
|
.flatMap((x) => ys.map((y) => ({ x, y })))
|
|
.filter(
|
|
(p) => contains(bounds, p) && !obstacles.some((r) => contains(r, p)),
|
|
)
|
|
.sort(
|
|
(a, b) =>
|
|
Math.hypot(a.x - point.x, a.y - point.y) -
|
|
Math.hypot(b.x - point.x, b.y - point.y),
|
|
)[0] || null
|
|
);
|
|
}
|
|
|
|
/** Four anchored placements, clamped along their free axis. Chrome is a set of
|
|
* actual DOM rectangles, so corner HUDs do not erase otherwise clear lanes. */
|
|
export function placeWildlifeSightingCard(point, size, safeArea) {
|
|
const { bounds, obstacles } = safeArea;
|
|
const anchor =
|
|
contains(bounds, point) && !obstacles.some((r) => contains(r, point))
|
|
? point
|
|
: nearestSafeAnchor(point, safeArea);
|
|
if (!anchor) return null;
|
|
const width = Math.min(size.width, bounds.width);
|
|
const height = Math.min(size.height, bounds.height);
|
|
const centeredX = clamp(
|
|
anchor.x - width / 2,
|
|
bounds.left,
|
|
bounds.right - width,
|
|
);
|
|
const centeredY = clamp(
|
|
anchor.y - height / 2,
|
|
bounds.top,
|
|
bounds.bottom - height,
|
|
);
|
|
const candidates = [
|
|
{
|
|
placement: 'above',
|
|
...rectAt(centeredX, anchor.y - GAP - height, width, height),
|
|
},
|
|
{ placement: 'below', ...rectAt(centeredX, anchor.y + GAP, width, height) },
|
|
{ placement: 'right', ...rectAt(anchor.x + GAP, centeredY, width, height) },
|
|
{
|
|
placement: 'left',
|
|
...rectAt(anchor.x - GAP - width, centeredY, width, height),
|
|
},
|
|
];
|
|
const fits = (rect) =>
|
|
rect.left >= bounds.left &&
|
|
rect.right <= bounds.right &&
|
|
rect.top >= bounds.top &&
|
|
rect.bottom <= bounds.bottom &&
|
|
!obstacles.some((r) => overlaps(rect, r));
|
|
let chosen = candidates.find(fits);
|
|
if (!chosen) {
|
|
// Crowded chrome may require sliding a side placement farther from its
|
|
// anchor. Search only obstacle/bounds edges; no pixels or idle frame loop.
|
|
const xs = [bounds.left, bounds.right - width, centeredX];
|
|
const ys = [bounds.top, bounds.bottom - height, centeredY];
|
|
for (const r of obstacles) {
|
|
xs.push(r.left - width, r.right);
|
|
ys.push(r.top - height, r.bottom);
|
|
}
|
|
const alternatives = xs
|
|
.flatMap((x) =>
|
|
ys.map((y) => ({
|
|
placement:
|
|
y + height <= anchor.y
|
|
? 'above'
|
|
: y >= anchor.y
|
|
? 'below'
|
|
: x >= anchor.x
|
|
? 'right'
|
|
: 'left',
|
|
...rectAt(x, y, width, height),
|
|
})),
|
|
)
|
|
.filter((r) => fits(r) && !contains(r, anchor));
|
|
alternatives.sort(
|
|
(a, b) =>
|
|
Math.hypot(
|
|
clamp(anchor.x, a.left, a.right) - anchor.x,
|
|
clamp(anchor.y, a.top, a.bottom) - anchor.y,
|
|
) -
|
|
Math.hypot(
|
|
clamp(anchor.x, b.left, b.right) - anchor.x,
|
|
clamp(anchor.y, b.top, b.bottom) - anchor.y,
|
|
),
|
|
);
|
|
chosen = alternatives[0];
|
|
}
|
|
return chosen
|
|
? {
|
|
...chosen,
|
|
anchor,
|
|
pan: { x: anchor.x - point.x, y: anchor.y - point.y },
|
|
}
|
|
: null;
|
|
}
|
|
|
|
/** Project a front-side sighting into viewport coordinates. */
|
|
export function projectWildlifeSighting(viewer, row, layer) {
|
|
const camera = viewer ? findCamera(viewer) : null;
|
|
const query = viewer ? findQuery(viewer) : null;
|
|
if (
|
|
!camera ||
|
|
!query ||
|
|
!Number.isFinite(row?.lat) ||
|
|
!Number.isFinite(row?.lon)
|
|
)
|
|
return null;
|
|
const position =
|
|
layer?.getSightingPosition?.(row.stableId) || fromDegrees(row.lon, row.lat);
|
|
if (horizonFrom(camera.pose().position).isPointVisible(position) !== true)
|
|
return null;
|
|
const point = query.project(position);
|
|
if (!point) return null;
|
|
const rect = canvasOf(viewer)?.getBoundingClientRect?.();
|
|
return { x: point.x + (rect?.left || 0), y: point.y + (rect?.top || 0) };
|
|
}
|
|
|
|
/** Translate the camera by the smallest screen correction and preserve its
|
|
* altitude/orientation. A card must never zoom merely to reveal its anchor. */
|
|
export function panWildlifeSighting(viewer, from, to, reducedMotion = false) {
|
|
const camera = viewer ? findCamera(viewer) : null;
|
|
const query = viewer ? findQuery(viewer) : null;
|
|
const canvas = canvasOf(viewer);
|
|
if (!camera || !query || !canvas) return false;
|
|
const rect = canvas.getBoundingClientRect();
|
|
const pick = (p) => query.pickEllipsoid(p.x - rect.left, p.y - rect.top);
|
|
const start = pick(from);
|
|
const finish = pick(to);
|
|
if (!start || !finish) return false;
|
|
const pose = camera.pose();
|
|
const destination = toLonLatH(add(pose.position, sub(start, finish)));
|
|
if (!destination) return false;
|
|
camera.flyTo(
|
|
{ lon: destination.lon, lat: destination.lat, height: pose.height },
|
|
{
|
|
orientation: {
|
|
heading: pose.heading,
|
|
pitch: pose.pitch,
|
|
roll: pose.roll,
|
|
},
|
|
durationS: reducedMotion ? 0 : 0.22,
|
|
easing: 'quadraticOut',
|
|
},
|
|
);
|
|
return true;
|
|
}
|