mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-21 02:32:18 +00:00
The picker preselected the active camera, and a native select fires no
change for the option it already shows. So while CCTV was still following
the view, choosing the camera the picker showed did nothing, and the next
pan switched to another camera.
The layer now reports whether the active camera is the opening one
(activeCameraProvisional). While it is, the picker shows that camera from
a hidden, disabled first option, so choosing it from the list is a change
and ends automatic selection; closing the list keeps it. A same-camera
pick that ends the opening pick notifies listeners so the picker drops the
hidden option, and reopenNearestCamera passes { opening: true } so its own
notification already carries the opening state.
docs/CURRENT-STATE.md now says when automatic selection ends. Tests cover
the hidden option, confirming and dismissing the picker before a pan, the
notification, and focusNearestToPoint, which is now on main.
203 lines
6.3 KiB
JavaScript
203 lines
6.3 KiB
JavaScript
import { isFollowing } from '../../data/followed.js';
|
|
import { CCTV_FOCUS_RESULT } from './policy.js';
|
|
import { cameraOf } from '../../render/rendererOf.js';
|
|
|
|
export function createNavigation({
|
|
state: layerState,
|
|
services,
|
|
parts,
|
|
source,
|
|
}) {
|
|
/**
|
|
* Nearest catalog camera to a WGS84 point.
|
|
* @param {number} lat Latitude in degrees.
|
|
* @param {number} lon Longitude in degrees.
|
|
* @returns {string|null} Camera ID, or null (invalid point, empty catalog).
|
|
*/
|
|
function nearestCameraIdToPoint(lat, lon) {
|
|
if (
|
|
!Number.isFinite(lat) ||
|
|
!Number.isFinite(lon) ||
|
|
Math.abs(lat) > 90 ||
|
|
Math.abs(lon) > 180 ||
|
|
!layerState._records.length
|
|
)
|
|
return null;
|
|
|
|
let best = null;
|
|
for (const record of layerState._records) {
|
|
const distKm = parts.model.haversineKm(
|
|
lat,
|
|
lon,
|
|
record.camera.lat,
|
|
record.camera.lon,
|
|
);
|
|
if (!best || distKm < best.distKm) {
|
|
best = { id: record.camera.id, distKm };
|
|
}
|
|
}
|
|
return best?.id || null;
|
|
}
|
|
|
|
/**
|
|
* Finds the camera closest to the viewer's current position.
|
|
* @returns {string|null} Camera ID of the nearest camera, or null.
|
|
*/
|
|
function nearestCameraIdToViewer() {
|
|
const pose = parts.model.eyePose();
|
|
if (!pose) return null;
|
|
return nearestCameraIdToPoint(pose.lat, pose.lon);
|
|
}
|
|
|
|
/**
|
|
* The camera the layer opens on when none is selected: the one nearest the
|
|
* viewer, or the first record when the viewer has no position yet.
|
|
* @returns {string|null} Camera ID, or null when the catalog is empty.
|
|
*/
|
|
|
|
function openingCameraId() {
|
|
return (
|
|
nearestCameraIdToViewer() || layerState._records[0]?.camera?.id || null
|
|
);
|
|
}
|
|
|
|
/**
|
|
* After a settled move, moves the opening camera to the one nearest the
|
|
* view, until someone chooses a camera. A shared view's flight lands after
|
|
* enable() picked from the startup view.
|
|
* @returns {boolean} True when the active camera changed.
|
|
*/
|
|
|
|
function reopenNearestCamera() {
|
|
if (!layerState._openingCameraProvisional || !layerState._enabled)
|
|
return false;
|
|
const nearest = nearestCameraIdToViewer();
|
|
if (!nearest || nearest === layerState._activeCameraId) return false;
|
|
// Still the opening pick: its notification tells the picker so.
|
|
parts.selection.setActiveCamera(nearest, { opening: true });
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Flies the viewer's camera to frame the specified CCTV camera,
|
|
* looking along its heading from above.
|
|
* @param {object|null} viewer The viewer that owns the camera.
|
|
* @param {Object|null} record CCTV camera runtime record.
|
|
* @param {number} [duration=2.2] - Flight duration in seconds.
|
|
* @returns {'focused'|'no-active-camera'|'tracking-holds-view'|'cockpit-active'} Focus result.
|
|
*/
|
|
|
|
function focusCctvRecord(viewer, record, duration = 2.2) {
|
|
if (!viewer || !record) return CCTV_FOCUS_RESULT.NO_ACTIVE_CAMERA;
|
|
if (
|
|
typeof document !== 'undefined' &&
|
|
document.body?.classList.contains('cockpit-mode')
|
|
) {
|
|
console.debug('[Data:CCTV] focus ignored while cockpit owns the camera');
|
|
return CCTV_FOCUS_RESULT.COCKPIT_ACTIVE;
|
|
}
|
|
if (isFollowing(viewer)) {
|
|
console.debug(
|
|
'[Data:CCTV] focus ignored while a tracked entity owns the camera',
|
|
);
|
|
return CCTV_FOCUS_RESULT.TRACKING_HOLDS_VIEW;
|
|
}
|
|
const { camera } = record;
|
|
const range = Math.max(280, camera.rangeM * 1.18);
|
|
cameraOf(viewer).flyToBounds(
|
|
{ center: record.position, radius: Math.max(40, camera.rangeM * 0.36) },
|
|
{
|
|
offset: {
|
|
heading: parts.model.toRad(camera.headingDeg),
|
|
pitch: parts.model.toRad(-22),
|
|
range,
|
|
},
|
|
durationS: Math.max(0.2, duration || 0),
|
|
easing: 'cubicInOut',
|
|
},
|
|
);
|
|
return CCTV_FOCUS_RESULT.FOCUSED;
|
|
}
|
|
|
|
function focusCamera(cameraId, duration = 2.2) {
|
|
return focusCctvRecord(
|
|
layerState._viewer,
|
|
layerState._recordById.get(cameraId),
|
|
duration,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Advances to the next camera if auto-hop is enabled and the hop interval
|
|
* has elapsed. If the viewer has panned to a new region since the last hop,
|
|
* snaps to the nearest camera instead of cycling sequentially.
|
|
* @param {number} nowMs - Current timestamp in milliseconds.
|
|
*/
|
|
|
|
function maybeAutoHop(nowMs) {
|
|
if (
|
|
!layerState._autoHop ||
|
|
layerState._autoHopSuspended ||
|
|
!layerState._enabled ||
|
|
layerState._records.length < 2
|
|
)
|
|
return;
|
|
if (nowMs - layerState._lastHopAt < layerState._autoHopSec * 1000) return;
|
|
|
|
const viewKey = parts.model.currentViewContext();
|
|
const viewChanged = viewKey !== layerState._lastViewContext;
|
|
layerState._lastViewContext = viewKey;
|
|
|
|
if (viewChanged) {
|
|
const nearest = nearestCameraIdToViewer();
|
|
if (nearest && nearest !== layerState._activeCameraId) {
|
|
// Use setActiveCamera so the full activation path runs (obstruction
|
|
// probe, projection runtime, geometry rewrite) — previously bypassed
|
|
// with a bare assignment
|
|
parts.selection.setActiveCamera(nearest);
|
|
layerState._lastHopAt = nowMs;
|
|
return;
|
|
}
|
|
}
|
|
|
|
const nextIdx = cctvCycleIndex(
|
|
layerState._records.findIndex(
|
|
(record) => record.camera.id === layerState._activeCameraId,
|
|
),
|
|
1,
|
|
layerState._records.length,
|
|
);
|
|
parts.selection.setActiveCamera(layerState._records[nextIdx].camera.id);
|
|
layerState._lastHopAt = nowMs;
|
|
}
|
|
|
|
/**
|
|
* Resolves a catalog cycle target, including the explicit no-selection state.
|
|
* NEXT from null selects the first record; PREV selects the last.
|
|
* @param {number} currentIdx
|
|
* @param {number} step
|
|
* @param {number} count
|
|
* @returns {number}
|
|
*/
|
|
|
|
function cctvCycleIndex(currentIdx, step, count) {
|
|
const total = Number.isFinite(count) ? Math.floor(count) : 0;
|
|
if (total <= 0) return -1;
|
|
const delta = Number.isFinite(step) ? Math.trunc(step) : 1;
|
|
if (!Number.isFinite(currentIdx) || currentIdx < 0) {
|
|
return delta < 0 ? total - 1 : 0;
|
|
}
|
|
return (((Math.floor(currentIdx) + delta) % total) + total) % total;
|
|
}
|
|
return {
|
|
nearestCameraIdToPoint,
|
|
nearestCameraIdToViewer,
|
|
openingCameraId,
|
|
reopenNearestCamera,
|
|
focusCctvRecord,
|
|
focusCamera,
|
|
maybeAutoHop,
|
|
cctvCycleIndex,
|
|
};
|
|
}
|