diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index 07026a55a..c907c87f2 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -2972,6 +2972,7 @@ "src/voice/narration.js", "src/voice/earcon.js", "src/voice/pointerContext.js", + "src/render/rendererOf.js", "src/voice/pointerReticle.js", "src/i18n/index.js", "src/i18n/locale.js", diff --git a/src/input/dispatcher.js b/src/input/dispatcher.js index 7aa2beb28..45c8bb609 100644 --- a/src/input/dispatcher.js +++ b/src/input/dispatcher.js @@ -137,6 +137,29 @@ const dispatchers = new WeakMap(); * @property {() => void} destroy */ +/** + * What is under canvas CSS pixel `x, y` now, as a click would see it (a hit + * on the follow's helper is a hit on its target), for a reader that is not + * handed a click (point-and-ask). Null when nothing is there or the pick + * fails. + * @param {object} viewer + * @param {number} x + * @param {number} y + * @returns {object|null} + */ +export function pickAt(viewer, x, y) { + try { + if (!viewer || viewer.isDestroyed?.()) return null; + const scene = viewer.scene; + if (typeof scene?.pick !== 'function') return null; + return ( + pickThroughFollowHelper(scene.pick(new Cesium.Cartesian2(x, y))) ?? null + ); + } catch { + return null; + } +} + /** * The scene's dispatcher, made on first use. * @param {object} viewer A Cesium viewer (or a test double with `scene`). diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index d406cf17d..192191966 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -119,9 +119,6 @@ "src/ui/cockpitController.js", "src/ui/flyToDataSource.js", "src/ui/imageryBoxTool.js", - "src/ui/locationNavigation.js", - "src/ui/navigationController.js", "src/ui/shareFollow.js", - "src/voice/gevActions.js", - "src/voice/pointerPick.js" + "src/voice/gevActions.js" ] diff --git a/src/render/unboundCamera.test.mjs b/src/render/unboundCamera.test.mjs index 6767477fe..de91569b9 100644 --- a/src/render/unboundCamera.test.mjs +++ b/src/render/unboundCamera.test.mjs @@ -86,6 +86,19 @@ const DRAW_FROM_CALLER = new Set([ './layers/air-quality', ]); +/** + * Cesium-free entries that reach the camera port but draw nothing: given a + * viewer with no renderer bound, they find no camera + * (`findCamera` is null) and leave it alone until the caller loads + * `./render/cesium/unbound`. + */ +const CAMERA_FROM_CALLER = new Set([ + './ui/navigation', + './voice/commands', + './voice/controller', + './voice/realtime-session', +]); + const pkg = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')); /** The browser module an export resolves to, or null for Node-only ones. */ @@ -196,6 +209,10 @@ test('every public entry that reaches the renderer bridge loads the unbound came const carrier = INJECTED[key]; const loads = (file) => reachable(file, { eager: true }).has(FACTORY); if (DRAW_FROM_CALLER.has(key)) continue; + if (CAMERA_FROM_CALLER.has(key)) { + assert.ok(!loads(entry), `${key} loads the factory: unlist it`); + continue; + } if (!loads(entry) && !(carrier && loads(carrier))) missing.push(key); } assert.ok(bridged > 0, 'no entry reaches the bridge: the walk is broken'); @@ -231,7 +248,11 @@ function checkLookupFactory( const entry = path.normalize(target.slice(2)); if (![...reachable(entry)].some(calls)) continue; reaching += 1; - if (fromCaller && DRAW_FROM_CALLER.has(key)) continue; + if ( + fromCaller && + (DRAW_FROM_CALLER.has(key) || CAMERA_FROM_CALLER.has(key)) + ) + continue; const carrier = INJECTED[key]; const loads = (file) => reachable(file, { eager: true }).has(factory); if (!loads(entry) && !(carrier && loads(carrier))) missing.push(key); diff --git a/src/ui/cameraOrientationControls.test.mjs b/src/ui/cameraOrientationControls.test.mjs index 6a5820a8f..f7db379ef 100644 --- a/src/ui/cameraOrientationControls.test.mjs +++ b/src/ui/cameraOrientationControls.test.mjs @@ -12,6 +12,7 @@ import { toggleCameraTilt, } from './cameraOrientationControls.js'; import { NavigationController } from './navigationController.js'; +import { bindRenderer } from '../render/rendererOf.js'; import { entityRecord } from '../data/entityRecords.js'; function createViewer({ cameraPosition, target, pickPosition = null } = {}) { @@ -600,10 +601,10 @@ test('orientation animation cancels on replacement and explicit cancellation', ( test('orientation navigation preserves follow and selection; new navigation cancels animation', () => { const calls = []; const entity = {}; - const viewer = { - trackedEntity: entity, + const viewer = { trackedEntity: entity }; + bindRenderer(viewer, { camera: { cancelFlight: () => calls.push('cancel-flight') }, - }; + }); let cockpit = false; const navigation = new NavigationController({ viewer, diff --git a/src/ui/locationNavigation.js b/src/ui/locationNavigation.js index 713c3b817..e11447f03 100644 --- a/src/ui/locationNavigation.js +++ b/src/ui/locationNavigation.js @@ -1,6 +1,5 @@ -import { findCamera } from '../render/cesium/camera.js'; +import { findCamera } from '../render/rendererOf.js'; import { stopFollowing } from '../data/followed.js'; -import * as Cesium from 'cesium'; import { t } from '../i18n/index.js'; import { createStateChannel } from '../app/stateChannel.js'; import { LocationControls } from './location.js'; @@ -387,15 +386,19 @@ export class LocationNavigation { settled = true; clearTimeout(timer); this._endWorldJumpTransition(); - const carto = this.viewer.camera.positionCartographic; + const pose = findCamera(this.viewer)?.pose(); const result = { ok: !cancelled, action: 'zoom_to_globe', cancelled, heightKm: Math.round(GLOBE_VIEW.heightM / 1000), centeredOn: { - latitude: Number(Cesium.Math.toDegrees(carto.latitude).toFixed(2)), - longitude: Number(Cesium.Math.toDegrees(carto.longitude).toFixed(2)), + latitude: Number.isFinite(pose?.lat) + ? Number(pose.lat.toFixed(2)) + : null, + longitude: Number.isFinite(pose?.lon) + ? Number(pose.lon.toFixed(2)) + : null, }, }; this._publishGlobeResetting?.(false); @@ -405,7 +408,7 @@ export class LocationNavigation { }; this._cancelGlobeReset = () => finish(true); timer = window.setTimeout(() => { - const height = this.viewer.camera.positionCartographic?.height; + const height = findCamera(this.viewer)?.pose().height; finish( !Number.isFinite(height) || Math.abs(height - GLOBE_VIEW.heightM) > 1000, diff --git a/src/ui/navigationController.js b/src/ui/navigationController.js index 7db0a42f2..44be4e02c 100644 --- a/src/ui/navigationController.js +++ b/src/ui/navigationController.js @@ -1,4 +1,4 @@ -import { findCamera } from '../render/cesium/camera.js'; +import { findCamera } from '../render/rendererOf.js'; import { stopFollowing } from '../data/followed.js'; import { cancelCameraArrival } from '../data/cameraArrival.js'; import { createStateChannel } from '../app/stateChannel.js'; @@ -217,7 +217,7 @@ export class NavigationController { release: () => { this.interruptCameraMotion('camera-orientation'); this.stopOrbit(); - this.viewer.camera.cancelFlight(); + findCamera(this.viewer)?.cancelFlight(); }, navigate, }); diff --git a/src/voice/pointerContext.js b/src/voice/pointerContext.js index 590e33a52..a750d5525 100644 --- a/src/voice/pointerContext.js +++ b/src/voice/pointerContext.js @@ -18,6 +18,7 @@ * * Provider-neutral and Cesium-free: the pick is injected. */ +import { findCamera } from '../render/rendererOf.js'; /** Movement below this many CSS pixels is still the same dwell. */ const POINTER_DWELL_PX = 12; @@ -55,23 +56,28 @@ const finite = finiteOrNull; /** * Camera pose identity: position to 1 m, orientation to about 0.06 degrees. - * A pixel read under one key names nothing under another. Reads plain - * numbers only, so it needs no Cesium import. - * @param {object} viewer Cesium viewer. + * A pixel read under one key names nothing under another. Reads the + * camera port's pose. + * @param {object} viewer The scene object. * @returns {string|null} */ export function cameraPoseKey(viewer) { - const camera = viewer?.camera; - const p = camera?.positionWC; + let pose = null; + try { + pose = findCamera(viewer)?.pose() ?? null; + } catch { + return null; + } + const p = pose?.position; if (!p || !Number.isFinite(p.x)) return null; const angle = (value) => Math.round((Number(value) || 0) * 1000); return [ Math.round(p.x), Math.round(p.y), Math.round(p.z), - angle(camera.heading), - angle(camera.pitch), - angle(camera.roll), + angle(pose.heading), + angle(pose.pitch), + angle(pose.roll), ].join(','); } diff --git a/src/voice/pointerPick.js b/src/voice/pointerPick.js index 82679d78b..76eb59817 100644 --- a/src/voice/pointerPick.js +++ b/src/voice/pointerPick.js @@ -1,5 +1,5 @@ -import { pickThroughFollowHelper } from '../render/cesium/follow.js'; -import * as Cesium from 'cesium'; +import { pickAt as defaultPickAt } from '../input/dispatcher.js'; +import { findCamera } from '../render/rendererOf.js'; import { resolvePickedContext } from '../data/pickedContext.js'; import { cameraPoseKey } from './pointerContext.js'; import * as defaultAnnotationResolver from '../annotations/annotationResolver.js'; @@ -11,20 +11,30 @@ function pointerRadiusKm(cameraHeightM) { return Math.round(Math.min(400, Math.max(5, heightKm * 0.75))); } +function cameraHeightM(viewer) { + try { + return findCamera(viewer)?.pose().height; + } catch { + return undefined; + } +} + /** - * The Cesium side of point-and-ask: one entity pick and one depth-aware world + * The scene side of point-and-ask: one entity pick and one depth-aware world * pick at canvas CSS pixels. Called lazily by the pointer tracker (dwell and * snapshot only), never per mouse move. * @param {object} options - * @param {object} options.viewer Cesium viewer. + * @param {object} options.viewer The scene object. * @param {object} [options.dataManager] Layer lifecycle. * @param {Function} [options.pickWorldFromScreen] Normalized-pixel world pick. + * @param {Function} [options.pickAt] Entity pick at canvas CSS pixels. * @returns {(x: number, y: number) => {entity: object|null, ground: object|null, radiusKm: number}|null} */ export function createPointerPicker({ viewer, dataManager = null, pickWorldFromScreen = defaultAnnotationResolver.pickWorldFromScreen, + pickAt = defaultPickAt, }) { return function pickPointer(x, y) { const scene = viewer?.scene; @@ -33,12 +43,7 @@ export function createPointerPicker({ const width = canvas.clientWidth || canvas.width || 0; const height = canvas.clientHeight || canvas.height || 0; if (!width || !height) return null; - let picked = null; - try { - picked = pickThroughFollowHelper(scene.pick(new Cesium.Cartesian2(x, y))); - } catch { - picked = null; - } + const picked = pickAt(viewer, x, y); const entity = resolvePickedContext(picked, { dataManager }); let ground = null; try { @@ -55,7 +60,7 @@ export function createPointerPicker({ return { entity, ground, - radiusKm: pointerRadiusKm(viewer.camera?.positionCartographic?.height), + radiusKm: pointerRadiusKm(cameraHeightM(viewer)), cameraKey: cameraPoseKey(viewer), }; };