Share the camera events and the stub's drawn positions with other cameras

The geo export now has geodesicAt and horizonFrom. The camera events module
is a public export (./render/camera/events) and holds move detection and the
command hooks as well as the per-listener changed measure; the stub camera
uses them. Each onCommand registration is now its own, so removing one twin
leaves the other. The stub's testing.drawnPosition says where it draws an
item, so a camera that is not the stub's can follow drawn items.
This commit is contained in:
Sameh Khamis committed 2026-11-08 19:22:42 -08:00
1 parent afd8dba67b
commit ef616b7905
9 files changed
+320 -70

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createCameraEvents, MOVE_END_WAIT_MS } from './events.js';
import { geodesicAt, horizonFrom } from '../../geo/index.js';
const view = (x) => ({
position: { x, y: 0, z: 0 },
direction: { x: -1, y: 0, z: 0 },
up: { x: 0, y: 0, z: 1 },
});
test('moveStart on a change, moveEnd once nothing moved for 500 ms and nothing is followed', () => {
let eye = view(7e6);
let following = false;
const seen = [];
const events = createCameraEvents({
pose: () => eye,
world: () => eye,
following: () => following,
report: (error) => assert.fail(error),
});
events.on('moveStart', () => seen.push('start'));
events.on('moveEnd', () => seen.push('end'));
events.stepMoveDetection(0);
eye = view(7e6 + 1);
events.stepMoveDetection(16);
assert.deepEqual(seen, ['start']);
events.stepMoveDetection(16 + MOVE_END_WAIT_MS - 1);
assert.deepEqual(seen, ['start']);
// A later change starts the wait again, and moveStart is not repeated.
eye = view(7e6 + 2);
events.stepMoveDetection(400);
events.stepMoveDetection(16 + MOVE_END_WAIT_MS);
assert.deepEqual(seen, ['start']);
events.stepMoveDetection(400 + MOVE_END_WAIT_MS - 1);
assert.deepEqual(seen, ['start']);
following = true;
events.stepMoveDetection(400 + MOVE_END_WAIT_MS);
assert.deepEqual(seen, ['start'], 'never while following');
following = false;
events.stepMoveDetection(401 + MOVE_END_WAIT_MS);
assert.deepEqual(seen, ['start', 'end']);
});
test('command hooks run before each command, each registration on its own', () => {
const events = createCameraEvents({ pose: () => view(1), report() {} });
const seen = [];
const hook = (c) => seen.push(c.kind);
const offFirst = events.onCommand(hook);
events.onCommand(hook);
events.command('setView');
assert.deepEqual(seen, ['setView', 'setView']);
offFirst();
offFirst();
events.command('flyTo');
assert.deepEqual(seen, ['setView', 'setView', 'flyTo'], 'one twin is left');
// A hook removed by an earlier one in the same delivery does not run.
const order = [];
const removing = createCameraEvents({ pose: () => view(1), report() {} });
let offSecond = null;
removing.onCommand(() => {
order.push('first');
offSecond();
});
offSecond = removing.onCommand(() => order.push('second'));
removing.command('setView');
removing.command('setView');
assert.deepEqual(order, ['first', 'first']);
const reported = [];
const throwing = createCameraEvents({
pose: () => view(1),
report: (error) => reported.push(error.message),
});
throwing.onCommand(() => {
throw new Error('boom');
});
throwing.onCommand(() => seen.push('after'));
throwing.command('flyTo');
assert.deepEqual(reported, ['boom']);
assert.equal(seen.at(-1), 'after');
});
test('the geo export has the geodesic point and the horizon test', () => {
const half = geodesicAt(0, 0, Math.PI / 2, 0, 0.5);
assert.ok(Math.abs(half.lon - Math.PI / 4) < 1e-12);
assert.ok(Math.abs(half.lat) < 1e-12);
const horizon = horizonFrom({ x: 7e6, y: 0, z: 0 });
assert.equal(horizon.isPointVisible({ x: 6378137, y: 0, z: 0 }), true);
assert.equal(horizon.isPointVisible({ x: -6378137, y: 0, z: 0 }), false);
});