mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The street-level provider credits, the map source credit, the shared inline OpenStreetMap and OpenMapTiles credits and the ALPR provider credit go through the renderer's credits port instead of Cesium's credit display. Their markup has one link each and is read as the port's plain data; the ALPR credit is built as plain data, which the port escapes. src/maps/credits.js and src/layers/streetLevel/credits.js no longer reach Cesium and leave the allow-list. findCredits finds the port as findCamera finds the camera: a Cesium viewer of a caller's own, with no renderer bound, gets a credits port over its own credit display, so the public entries keep their attribution. A port built that way is released, not disposed, when a renderer binds later, and a credit let go after its viewer is destroyed leaves the display alone. The per-layer data credits and the transit and event credits stay on Cesium's display: their markup carries several links each, which the contract's credit cannot hold.
172 lines
5.5 KiB
JavaScript
172 lines
5.5 KiB
JavaScript
/**
|
|
* A camera port for tests of code that reads the camera: a settable pose,
|
|
* a settable view rectangle, and events the test raises itself.
|
|
*/
|
|
import { createCameraEvents } from '../render/camera/events.js';
|
|
import { toDegrees } from '../geo/index.js';
|
|
import { bindRenderer, rendererOf } from '../render/rendererOf.js';
|
|
import { createCesiumCamera } from '../render/cesium/camera.js';
|
|
import { fakeCesiumCredits } from './fakeCreditsPort.mjs';
|
|
|
|
const DEFAULT_POSE = Object.freeze({
|
|
lon: 0,
|
|
lat: 0,
|
|
height: 1000,
|
|
heading: 0,
|
|
pitch: -Math.PI / 2,
|
|
roll: 0,
|
|
position: { x: 0, y: 0, z: 0 },
|
|
direction: { x: 0, y: 0, z: -1 },
|
|
up: { x: 0, y: 1, z: 0 },
|
|
right: { x: 1, y: 0, z: 0 },
|
|
fovy: Math.PI / 3,
|
|
aspectRatio: 1,
|
|
near: 1,
|
|
far: 5e8,
|
|
frameIsIdentity: true,
|
|
});
|
|
|
|
/**
|
|
* @param {{pose?: object, viewRectangle?: object|null}} [initial]
|
|
*/
|
|
export function fakeCameraPort({ pose = {}, viewRectangle = null } = {}) {
|
|
let current = { ...DEFAULT_POSE, ...pose };
|
|
let rect = viewRectangle;
|
|
const events = createCameraEvents({
|
|
pose: () => ({ ...current }),
|
|
report: (error) => {
|
|
throw error;
|
|
},
|
|
});
|
|
const counts = {};
|
|
const port = {
|
|
pose: () => ({ ...current }),
|
|
viewRectangle: () => (rect ? { ...rect } : null),
|
|
on(event, cb, options) {
|
|
const off = events.on(event, cb, options);
|
|
counts[event] = (counts[event] ?? 0) + 1;
|
|
let live = true;
|
|
return () => {
|
|
if (live) counts[event] -= 1;
|
|
live = false;
|
|
off();
|
|
};
|
|
},
|
|
};
|
|
return {
|
|
port,
|
|
/** Change the pose the port reports. */
|
|
setPose: (fields) => (current = { ...current, ...fields }),
|
|
setViewRectangle: (next) => (rect = next),
|
|
emit: (event) => events.emit(event),
|
|
/** Run the contract's `changed` measure, as a frame would. */
|
|
stepChanged: () => events.stepChanged(),
|
|
listenerCount: (event) => counts[event] ?? 0,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Bind to a fake viewer the Cesium renderer's own camera port over its
|
|
* Cesium-shaped fake camera, for tests whose code is half on the port: the
|
|
* port's commands reach the fake camera as the renderer maps them
|
|
* (`flyTo({destination: Cartesian3, ...})`), while poses are read leniently
|
|
* from what the fake has (`positionCartographic`, `heading`, `pitch`,
|
|
* `roll`) and events come from the fake's own `changed`, `moveStart` and
|
|
* `moveEnd`. A fake with `trackedEntityChanged` and `entities` follows
|
|
* through the port, as the renderer does. Its credits are the renderer's
|
|
* credits port over the fake's `creditDisplay`, if it has one. Binding a
|
|
* viewer twice keeps the first. Returns the viewer.
|
|
* @template {{camera: object}} V
|
|
* @param {V} viewer
|
|
* @returns {V}
|
|
*/
|
|
export function bindFakeCesiumCamera(viewer) {
|
|
if (rendererOf(viewer)) return viewer;
|
|
const fake = viewer.camera;
|
|
const noEvent = { addEventListener: () => () => {} };
|
|
// A fake that does not keep Cesium's current flight gets one kept for it,
|
|
// as Cesium does: from a flight's start until it completes or cancels.
|
|
let current = null;
|
|
const flown = (fly) =>
|
|
function (...args) {
|
|
const options = args.at(-1) ?? {};
|
|
const flight = {};
|
|
current = flight;
|
|
const settle = (cb) => () => {
|
|
if (current === flight) current = null;
|
|
cb?.();
|
|
};
|
|
args[args.length - 1] = {
|
|
...options,
|
|
complete: settle(options.complete),
|
|
cancel: settle(options.cancel),
|
|
};
|
|
return fly.apply(this, args);
|
|
};
|
|
const keepsFlight = '_currentFlight' in fake;
|
|
const camera = new Proxy(fake, {
|
|
get(target, key, receiver) {
|
|
if ((key === 'moveStart' || key === 'moveEnd') && !target[key])
|
|
return noEvent;
|
|
if (!keepsFlight) {
|
|
if (key === '_currentFlight') return current ?? undefined;
|
|
if ((key === 'flyTo' || key === 'flyToBoundingSphere') && target[key])
|
|
return flown(target[key]);
|
|
if (key === 'cancelFlight' && target[key])
|
|
return function (...args) {
|
|
current = null;
|
|
return target[key].apply(this, args);
|
|
};
|
|
}
|
|
return Reflect.get(target, key, receiver);
|
|
},
|
|
});
|
|
const scene = {
|
|
postUpdate: noEvent,
|
|
preUpdate: viewer.scene?.preUpdate ?? noEvent,
|
|
get screenSpaceCameraController() {
|
|
return viewer.scene?.screenSpaceCameraController ?? undefined;
|
|
},
|
|
requestRender: () => viewer.scene?.requestRender?.(),
|
|
};
|
|
// A fake with Cesium's entity tracking follows as the renderer does.
|
|
const tracks = Boolean(viewer.trackedEntityChanged && viewer.entities);
|
|
const real = createCesiumCamera({
|
|
scene,
|
|
camera,
|
|
...(tracks ? { widget: viewer } : {}),
|
|
report: (error) => {
|
|
throw error;
|
|
},
|
|
}).port;
|
|
const port = Object.create(real, {
|
|
pose: {
|
|
value() {
|
|
const c = fake.positionCartographic;
|
|
const p = fake.positionWC;
|
|
return {
|
|
...DEFAULT_POSE,
|
|
...(p ? { position: { x: p.x, y: p.y, z: p.z } } : {}),
|
|
lon: c ? toDegrees(c.longitude) : NaN,
|
|
lat: c ? toDegrees(c.latitude) : NaN,
|
|
height: c ? c.height : NaN,
|
|
heading: fake.heading ?? 0,
|
|
pitch: fake.pitch ?? -Math.PI / 2,
|
|
roll: fake.roll ?? 0,
|
|
};
|
|
},
|
|
},
|
|
viewRectangle: { value: () => null },
|
|
on: {
|
|
value(event, cb) {
|
|
const source = fake[event];
|
|
const off = source?.addEventListener?.(cb);
|
|
if (typeof off === 'function') return off;
|
|
return () => source?.removeEventListener?.(cb);
|
|
},
|
|
},
|
|
});
|
|
bindRenderer(viewer, { camera: port, credits: fakeCesiumCredits(viewer) });
|
|
return viewer;
|
|
}
|