mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Merge origin/main into the renderer seam
Brings in main up to f24abfc88: cockpit subject providers, the render
failure notice, aircraft drawn in their own mesh colours, airport ground
vehicles, and an aircraft with no ground track facing its true heading.
Under the seam:
- The render failure notice lives in the Cesium adapter
(src/render/cesium/renderFailure.js) and is the application hook
renderFailureNotice({allowReload, store}); its private Cesium fields
(Event's raise queue, the widget's render loop flag) are read through
internals.js. The inline panel's timer waits while the frame port reports
the loop stopped, and goes on after a Retry.
- Ordinary aircraft models draw white with the 'highlight' colour blend
through the draw port; tracked and emergency colours tint the mesh.
- Held trains and transit vehicles hide their route set and markers through
the draw port; the fleet model test fakes answer loads through the fake
draw port instead of Cesium's glTF loader.
- The Cockpit's subject view follows and places the camera through the
camera port.
This commit is contained in:
commit
866a9816f2
142 files changed
+9067
-308
No files matched your search
@@ -0,0 +1,237 @@
|
||||
import { toCartographicRadians } from '../../geo/ellipsoid.js';
|
||||
import {
|
||||
getRegisteredTransitFeed,
|
||||
transitRouteLabel,
|
||||
} from '../../data/transitFeeds.js';
|
||||
import { displayMotion } from './movement.js';
|
||||
|
||||
/**
|
||||
* `previous` when it holds each of `next`'s values, else a frozen copy of
|
||||
* `next`: a part of a snapshot that did not change is the one read before.
|
||||
* `next` is a draft, filled again on every read.
|
||||
*/
|
||||
function reuse(previous, next) {
|
||||
if (!previous) return Object.freeze({ ...next });
|
||||
for (const name in next)
|
||||
if (!Object.is(previous[name], next[name]))
|
||||
return Object.freeze({ ...next });
|
||||
return previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read access to one vehicle, and a hold on it for a first-person view.
|
||||
*
|
||||
* `getSelectedVehicle()` and `getCockpitSubject()` describe the selected
|
||||
* vehicle and the held one as a frozen snapshot, read again on each call:
|
||||
* where it is drawn, how fast it moves and how that is known. A call while
|
||||
* nothing in it changed returns the snapshot it returned before, and a part
|
||||
* that did not change (its feed, its stop) is the same object in the next
|
||||
* one, so a view can read it on every frame. Transit has no route shapes or
|
||||
* stops, so a snapshot names only the vehicle's last reported stop.
|
||||
* `setCockpitSubject(key)` holds a vehicle: its sprite, 3D model, card,
|
||||
* trail and detection box are hidden, it keeps moving when it is out of the camera's
|
||||
* view, its feed stays polled, and clicks and Escape no longer change the
|
||||
* selection. The hold ends with `setCockpitSubject(null)`, and on
|
||||
* its own when the vehicle is removed or the layer is turned off.
|
||||
* `focusVehicle(key)` selects a vehicle, as a click does. A snapshot's
|
||||
* `selectedAtMs` is when the user last selected the selected vehicle (a
|
||||
* click, or `focusVehicle`), even again. It is null for any other vehicle,
|
||||
* after a deselection, and for a vehicle selected only by a hold: a hold is
|
||||
* not a user's selection.
|
||||
*
|
||||
* @param {object} context The layer's state, parts and services.
|
||||
* @returns {{methods: object}}
|
||||
*/
|
||||
export function createTransitCockpitSubject({ state, parts, services }) {
|
||||
const liftM = Number.isFinite(services.ground?.GROUND_FLOOR_LIFT_M)
|
||||
? services.ground.GROUND_FLOOR_LIFT_M
|
||||
: 0;
|
||||
const finiteOrNull = (value) =>
|
||||
typeof value === 'number' && Number.isFinite(value) ? value : null;
|
||||
/** Each vehicle's last snapshot, returned again while nothing changed. */
|
||||
const views = new WeakMap();
|
||||
/** The drafts vehicleView fills on every read (reuse). */
|
||||
const draft = {
|
||||
view: {},
|
||||
feed: {},
|
||||
position: {},
|
||||
motion: {},
|
||||
stop: {},
|
||||
provenance: {},
|
||||
};
|
||||
|
||||
/** The feed in use, else the registry's entry for it. */
|
||||
function feedEntry(feedId) {
|
||||
return state._activeFeeds.get(feedId) || getRegisteredTransitFeed(feedId);
|
||||
}
|
||||
|
||||
function feedOf(feed, previous) {
|
||||
if (!feed) return null;
|
||||
const next = draft.feed;
|
||||
next.id = feed.id;
|
||||
next.name = feed.name ?? null;
|
||||
next.operator = feed.operator ?? null;
|
||||
next.region = feed.region ?? null;
|
||||
next.attribution = feed.attribution ?? null;
|
||||
next.license = feed.license ?? null;
|
||||
next.licenseUrl = feed.licenseUrl ?? null;
|
||||
return reuse(previous, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* The floor under the vehicle where playback draws it, without the shared
|
||||
* lift: the drawn marker's height while the frame pass places it (in view,
|
||||
* or held), which near the ground follows the terrain between reports;
|
||||
* else its segment's two reports' floors at the playback fraction. Null
|
||||
* while either floor is unknown, as on cold terrain, so a view waits for
|
||||
* the surface. Not `entry.heightM`, the floor at the newest report, which
|
||||
* playback has not reached yet.
|
||||
*/
|
||||
function drawnFloorOf(entry) {
|
||||
const segment = entry.segment;
|
||||
if (!segment || entry.heightPending) return null;
|
||||
const placed =
|
||||
entry.surfaceReady === true &&
|
||||
entry.hasRendered &&
|
||||
entry.marker?.position &&
|
||||
(state._visible.has(entry) || entry.key === state._cockpitSubjectKey);
|
||||
if (placed) {
|
||||
const drawn = toCartographicRadians(entry.marker.position);
|
||||
if (drawn) return drawn.height - liftM;
|
||||
}
|
||||
const from = segment.from?.h;
|
||||
const to = segment.to?.h;
|
||||
if (!Number.isFinite(from) || !Number.isFinite(to)) return null;
|
||||
const f = Number.isFinite(segment.fraction)
|
||||
? Math.max(0, Math.min(1, segment.fraction))
|
||||
: 1;
|
||||
return from + (to - from) * f - liftM;
|
||||
}
|
||||
|
||||
/** The vehicle as a frozen snapshot (see the module comment), or null. */
|
||||
function vehicleView(entry) {
|
||||
if (!entry?.record) return null;
|
||||
const previous = views.get(entry);
|
||||
const record = entry.record;
|
||||
const sample = entry.sample ?? null;
|
||||
const feed = feedEntry(entry.feedId);
|
||||
const reported = finiteOrNull(record.speedMps);
|
||||
const derived = finiteOrNull(sample?.segmentSpeedMps);
|
||||
const status = state._feedStatus.get(entry.feedId);
|
||||
|
||||
const position = draft.position;
|
||||
position.latitude = finiteOrNull(sample?.lat) ?? record.lat;
|
||||
position.longitude = finiteOrNull(sample?.lon) ?? record.lon;
|
||||
position.groundHeightM = drawnFloorOf(entry);
|
||||
position.atMs = finiteOrNull(sample?.displayT);
|
||||
|
||||
const motion = draft.motion;
|
||||
motion.reportedSpeedMps = reported;
|
||||
motion.derivedSpeedMps = derived;
|
||||
motion.speedSource =
|
||||
reported !== null ? 'reported' : derived !== null ? 'derived' : null;
|
||||
motion.courseDeg = finiteOrNull(entry.courseDeg);
|
||||
motion.reportedBearingDeg = finiteOrNull(record.bearing);
|
||||
motion.segmentCourseDeg = finiteOrNull(sample?.segmentCourseDeg);
|
||||
motion.phase = sample?.phase ?? null;
|
||||
motion.word = displayMotion(entry, Date.now()).word;
|
||||
motion.reportAgeMs = finiteOrNull(sample?.latestReportAgeMs);
|
||||
motion.shownBehindMs = finiteOrNull(sample?.actualDelayMs);
|
||||
motion.feedStale = status?.stale === true;
|
||||
|
||||
const stop = draft.stop;
|
||||
stop.stopId = record.stopId || null;
|
||||
stop.status = record.status || null;
|
||||
|
||||
const provenance = draft.provenance;
|
||||
provenance.positionSource = 'gps';
|
||||
provenance.timestampSource = record.timestampSource ?? null;
|
||||
|
||||
const view = draft.view;
|
||||
view.key = entry.key;
|
||||
view.feedId = entry.feedId;
|
||||
view.selectedAtMs =
|
||||
state._selectedKey === entry.key ? state._selectedAtMs : null;
|
||||
view.id = String(record.id ?? '');
|
||||
view.routeId = record.routeId || null;
|
||||
view.routeLabel =
|
||||
(feed && record.routeId
|
||||
? transitRouteLabel(feed, record.routeId)
|
||||
: null) || null;
|
||||
view.tripId = record.tripId || null;
|
||||
view.directionId = record.directionId ?? null;
|
||||
view.label = record.label || null;
|
||||
view.mode = entry.mode ?? null;
|
||||
view.modeInferred = entry.modeInferred === true;
|
||||
view.feed = feedOf(feed, previous?.feed);
|
||||
view.position = reuse(previous?.position, position);
|
||||
view.motion = reuse(previous?.motion, motion);
|
||||
view.stop = reuse(previous?.stop, stop);
|
||||
view.provenance = reuse(previous?.provenance, provenance);
|
||||
const snapshot = reuse(previous, view);
|
||||
views.set(entry, snapshot);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/** Show or hide a vehicle's sprite and trail for the hold. */
|
||||
function repaint(entry) {
|
||||
if (!entry?.marker) return;
|
||||
if (entry.key === state._cockpitSubjectKey) entry.marker.show = false;
|
||||
else parts.rendering.placeSample(entry);
|
||||
parts.rendering.schedulePlayback(entry);
|
||||
parts.trails.update();
|
||||
state._detectRevision += 1;
|
||||
}
|
||||
|
||||
const methods = {
|
||||
/** The selected vehicle (see createTransitCockpitSubject), or null. */
|
||||
getSelectedVehicle() {
|
||||
return vehicleView(state._vehicles.get(state._selectedKey));
|
||||
},
|
||||
/** The held vehicle, or null when none is held. */
|
||||
getCockpitSubject() {
|
||||
return vehicleView(state._vehicles.get(state._cockpitSubjectKey));
|
||||
},
|
||||
/**
|
||||
* Hold the vehicle `key` for a first-person view, or release the hold
|
||||
* with null. A vehicle it holds is selected, if it was not already.
|
||||
* @param {string|null} key
|
||||
* @returns {boolean} False for a vehicle the layer does not show.
|
||||
*/
|
||||
setCockpitSubject(key) {
|
||||
const previous = state._vehicles.get(state._cockpitSubjectKey);
|
||||
if (key === null || key === undefined) {
|
||||
state._cockpitSubjectKey = null;
|
||||
// The hold kept it in play out of view: it leaves until the next
|
||||
// visibility sweep finds it in view again.
|
||||
if (previous) {
|
||||
state._visible.delete(previous);
|
||||
parts.rendering.requestVisibility();
|
||||
}
|
||||
repaint(previous);
|
||||
parts.selection.refreshSelectedCard(true);
|
||||
return true;
|
||||
}
|
||||
const entry = state._vehicles.get(key);
|
||||
if (!entry?.marker) return false;
|
||||
if (state._selectedKey !== key) parts.selection.selectVehicle(key);
|
||||
state._cockpitSubjectKey = key;
|
||||
state._visible.add(entry);
|
||||
if (previous && previous !== entry) repaint(previous);
|
||||
repaint(entry);
|
||||
parts.selection.refreshSelectedCard(true);
|
||||
return true;
|
||||
},
|
||||
/**
|
||||
* Select the vehicle `key`, as a click on it does.
|
||||
* @param {string} key
|
||||
* @returns {boolean} False for a vehicle the layer does not show.
|
||||
*/
|
||||
focusVehicle(key) {
|
||||
if (!state._vehicles.get(key)?.marker) return false;
|
||||
parts.selection.selectVehicle(key, { byUser: true });
|
||||
return true;
|
||||
},
|
||||
};
|
||||
return { methods, vehicleView };
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { createQueries } from './queries.js';
|
||||
import { createControls } from './controls.js';
|
||||
import { createLifecycle } from './lifecycle.js';
|
||||
import { createTesting } from './testing.js';
|
||||
import { createTransitCockpitSubject } from './cockpitSubject.js';
|
||||
import { TRANSIT_POLL_MS } from './policy.js';
|
||||
|
||||
/**
|
||||
@@ -54,12 +55,14 @@ export function createTransitLayer({
|
||||
parts.controls = createControls(context);
|
||||
parts.lifecycle = createLifecycle(context);
|
||||
parts.testing = createTesting(context);
|
||||
parts.cockpit = createTransitCockpitSubject(context);
|
||||
|
||||
return Object.assign(
|
||||
{ updateInterval: TRANSIT_POLL_MS },
|
||||
parts.lifecycle.methods,
|
||||
parts.queries.methods,
|
||||
parts.controls.methods,
|
||||
parts.cockpit.methods,
|
||||
parts.testing,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,6 +71,8 @@ export function createIngestion({ state, services, parts, source }) {
|
||||
function removeVehicle(key) {
|
||||
const entry = state._vehicles.get(key);
|
||||
if (!entry) return;
|
||||
// A first-person view on it loses its subject (cockpitSubject.js).
|
||||
if (state._cockpitSubjectKey === key) state._cockpitSubjectKey = null;
|
||||
if (state._selectedKey === key) parts.selection.clearSelection();
|
||||
parts.rendering.removeMarker(entry);
|
||||
entry.marker = null;
|
||||
|
||||
@@ -102,6 +102,7 @@ export function createLifecycle({ state, services, parts }) {
|
||||
state._vehicles.clear();
|
||||
parts.height.clear();
|
||||
state._selectedKey = null;
|
||||
state._cockpitSubjectKey = null;
|
||||
state._lastUpdate = null;
|
||||
state._error = null;
|
||||
state._limitWarned = false;
|
||||
|
||||
@@ -8,9 +8,11 @@ import {
|
||||
FEED_STALE_AFTER_MS,
|
||||
MISSED_POLLS_TO_DROP,
|
||||
TRANSIT_POLL_MS,
|
||||
TRANSIT_SELECTED_OVERLAY_SOURCE_ID,
|
||||
VEHICLE_MAX_FIX_AGE_MS,
|
||||
} from './policy.js';
|
||||
import { FLOOR_WARM_PER_POLL } from './policy.js';
|
||||
import { fleetModelFakes } from '../../testSupport/fleetModelFakes.mjs';
|
||||
import {
|
||||
FUTURE_STAMP_TOLERANCE_MS,
|
||||
ORDER_REJECTS_BEFORE_RESET,
|
||||
@@ -224,6 +226,9 @@ function harness(
|
||||
east: at.lon + 0.1,
|
||||
north: at.lat + 0.1,
|
||||
},
|
||||
// Fleet-model services (testSupport/fleetModelFakes.mjs), with a camera
|
||||
// looking straight down for them; without them vehicles stay icons.
|
||||
fleet = null,
|
||||
} = {},
|
||||
) {
|
||||
const viewRect = { ...view };
|
||||
@@ -290,6 +295,7 @@ function harness(
|
||||
viewRect.north,
|
||||
);
|
||||
},
|
||||
...(fleet ? { view: viewFrom(at.lon, at.lat, altitude) } : {}),
|
||||
},
|
||||
scene: {
|
||||
canvas: { addEventListener() {}, removeEventListener() {} },
|
||||
@@ -339,6 +345,7 @@ function harness(
|
||||
const layer = createTransitLayer({
|
||||
source,
|
||||
services: {
|
||||
...(fleet ? { fleet } : {}),
|
||||
ground,
|
||||
mesh: {
|
||||
sampleMeshFloorCells(scene, points, options) {
|
||||
@@ -944,6 +951,27 @@ test('disable aborts in-flight work and a response that lands late is ignored',
|
||||
assert.equal(app.vehicles().length, 1);
|
||||
});
|
||||
|
||||
test('a held vehicle with no floor yet tells its view no height', async (t) => {
|
||||
// Not the ellipsoid: a first-person view waits for the surface instead.
|
||||
const app = harness(t, { floorAt: () => undefined });
|
||||
app.serve('mbta', () => ({
|
||||
status: 200,
|
||||
body: snapshot(
|
||||
'mbta',
|
||||
'MBTA',
|
||||
[vehicle('bus-1', 42.36, -71.06, Date.now() / 1000)],
|
||||
{ fetchedAt: Date.now() },
|
||||
),
|
||||
}));
|
||||
app.layer.enable(app.viewer);
|
||||
await app.layer.update();
|
||||
app.settle();
|
||||
const entry = app.vehicles()[0];
|
||||
assert.equal(entry.heightPending, true, 'it has nothing to stand on');
|
||||
assert.ok(app.layer.setCockpitSubject(entry.key) !== false);
|
||||
assert.equal(app.layer.getCockpitSubject().position.groundHeightM, null);
|
||||
});
|
||||
|
||||
test('a vehicle waits for its floor rather than standing on the ellipsoid', async (t) => {
|
||||
// The shared ground-floor system answers from the rendered mesh cell or the
|
||||
// real DEM cell; until one of them is warm there is no honest height to draw
|
||||
@@ -1966,11 +1994,19 @@ test('a segment climbs from the ground it left to the ground it reaches', async
|
||||
assert.ok(Math.abs(entry.heightM - 101.5) < 1, 'the destination is uphill');
|
||||
const height = () =>
|
||||
Cesium.Cartographic.fromCartesian(entry.marker.position).height;
|
||||
// A first-person view reads the floor where the vehicle is drawn, not the
|
||||
// newest report's floor uphill.
|
||||
app.layer.setCockpitSubject(entry.key);
|
||||
const floor = () => app.layer.getCockpitSubject().position.groundHeightM;
|
||||
app.run(400);
|
||||
assert.ok(
|
||||
height() < 30,
|
||||
`still on the ground it is drawn on, got ${height().toFixed(1)} m`,
|
||||
);
|
||||
assert.ok(
|
||||
Math.abs(floor() + 1.5 - height()) < 1,
|
||||
`its snapshot stands where it is drawn, got ${floor().toFixed(1)} m`,
|
||||
);
|
||||
|
||||
// Halfway along the segment it is halfway up; at the end, on the high ground.
|
||||
const firstFixAt = start - AGE_MS;
|
||||
@@ -1984,6 +2020,7 @@ test('a segment climbs from the ground it left to the ground it reaches', async
|
||||
Math.abs(height() - 101.5) < 1.5,
|
||||
`and arrives on the high ground, got ${height().toFixed(1)} m`,
|
||||
);
|
||||
assert.ok(Math.abs(floor() - 100) < 1.5, 'and so does its snapshot');
|
||||
});
|
||||
test('a lone impossible fix leaves the displayed vehicle and health untouched', async (t) => {
|
||||
const app = harness(t, { floorAt: () => 5 });
|
||||
@@ -4452,3 +4489,211 @@ test('the prompt names the altitude the gate opens at, not 3,000 km', async (t)
|
||||
'Fly below 2,850 km to a covered region',
|
||||
);
|
||||
});
|
||||
|
||||
/** A bus running north a step a poll, polled `polls` times; its key. */
|
||||
async function busRunningNorth(app, polls = 4, extra = {}) {
|
||||
let step = 0;
|
||||
app.serve('mbta', () => ({
|
||||
status: 200,
|
||||
body: snapshot(
|
||||
'mbta',
|
||||
'MBTA',
|
||||
[
|
||||
vehicle('bus-1', 42.36 + step * 0.001, -71.06, reported(), {
|
||||
stopId: 'place-1',
|
||||
status: 'IN_TRANSIT_TO',
|
||||
...extra,
|
||||
}),
|
||||
],
|
||||
{ fetchedAt: Date.now() },
|
||||
),
|
||||
}));
|
||||
app.layer.enable(app.viewer);
|
||||
await app.layer.update();
|
||||
app.settle();
|
||||
for (let i = 0; i < polls; i += 1) {
|
||||
step += 1;
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
await app.layer.update();
|
||||
app.settle();
|
||||
}
|
||||
return 'mbta:bus-1';
|
||||
}
|
||||
|
||||
test('the selected vehicle reads as a snapshot of its place, motion and source', async (t) => {
|
||||
const app = harness(t);
|
||||
const key = await busRunningNorth(app, 4, { speedMps: 9 });
|
||||
assert.equal(app.layer.getSelectedVehicle(), null);
|
||||
app.layer._transitPartsForTest().selection.selectVehicle(key);
|
||||
const view = app.layer.getSelectedVehicle();
|
||||
assert.ok(Object.isFrozen(view));
|
||||
assert.equal(view.key, key);
|
||||
assert.equal(view.feedId, 'mbta');
|
||||
assert.equal(view.id, 'bus-1');
|
||||
assert.equal(view.routeId, '1');
|
||||
assert.equal(typeof view.mode, 'string');
|
||||
assert.ok(view.feed.attribution, 'the feed names its credit');
|
||||
assert.equal(view.motion.reportedSpeedMps, 9);
|
||||
assert.equal(view.motion.speedSource, 'reported');
|
||||
assert.equal(view.provenance.positionSource, 'gps');
|
||||
assert.deepEqual(view.stop, { stopId: 'place-1', status: 'IN_TRANSIT_TO' });
|
||||
const entry = app.vehicles()[0];
|
||||
// Where it is drawn, without the shared lift (read back from the marker).
|
||||
assert.ok(
|
||||
Math.abs(view.position.groundHeightM - (entry.heightM - 1.5)) < 1e-3,
|
||||
);
|
||||
assert.ok(Number.isFinite(view.position.latitude));
|
||||
});
|
||||
|
||||
test('a vehicle read again while nothing changed is the snapshot read before', async (t) => {
|
||||
const app = harness(t);
|
||||
const key = await busRunningNorth(app);
|
||||
app.layer._transitPartsForTest().selection.selectVehicle(key);
|
||||
const first = app.layer.getSelectedVehicle();
|
||||
assert.equal(app.layer.getSelectedVehicle(), first, 'the same snapshot');
|
||||
// It moves on: a new snapshot at its new place, what did not change kept.
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
await app.layer.update();
|
||||
app.settle();
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
const moved = app.layer.getSelectedVehicle();
|
||||
assert.notEqual(moved, first, 'a new snapshot');
|
||||
assert.ok(Object.isFrozen(moved) && Object.isFrozen(moved.position));
|
||||
assert.ok(moved.position.latitude > first.position.latitude, 'moved');
|
||||
assert.equal(moved.feed, first.feed);
|
||||
assert.equal(moved.stop, first.stop);
|
||||
assert.equal(moved.provenance, first.provenance);
|
||||
assert.equal(app.layer.getSelectedVehicle(), moved);
|
||||
});
|
||||
|
||||
test('a held vehicle draws no 3D model either; let go, its model draws again', async (t) => {
|
||||
const models = await fleetModelFakes();
|
||||
const app = harness(t, { fleet: models.fleet, altitude: 1_000 });
|
||||
models.watch(app.draw);
|
||||
const key = await busRunningNorth(app);
|
||||
const [entry] = app.vehicles();
|
||||
assert.notEqual(entry.mode, 'unknown', 'a mode its model is chosen by');
|
||||
// The throttled sweep runs; a model's load answers off the frame and the
|
||||
// next frames place it.
|
||||
const frames = async () => {
|
||||
app.advance(250);
|
||||
app.settle();
|
||||
models.load();
|
||||
for (let i = 0; i < 8; i += 1) await Promise.resolve();
|
||||
app.settle();
|
||||
};
|
||||
await frames();
|
||||
assert.equal(models.drawn(key).length, 1, 'near the camera: its model');
|
||||
assert.equal(entry.modelOwnsVisual, true);
|
||||
assert.equal(entry.marker.show, false, 'its icon hands over');
|
||||
|
||||
assert.equal(app.layer.setCockpitSubject(key), true);
|
||||
app.frame();
|
||||
// The view's eye is inside the vehicle.
|
||||
assert.equal(models.drawn(key).length, 0, 'no model');
|
||||
assert.equal(entry.modelOwnsVisual, false);
|
||||
assert.equal(entry.marker.show, false, 'and no icon');
|
||||
await frames();
|
||||
assert.equal(models.drawn(key).length, 0, 'not on a later frame either');
|
||||
assert.equal(entry.marker.show, false);
|
||||
|
||||
assert.equal(app.layer.setCockpitSubject(null), true);
|
||||
await frames();
|
||||
assert.equal(models.drawn(key).length, 1, 'let go: its model again');
|
||||
assert.equal(entry.modelOwnsVisual, true);
|
||||
assert.equal(entry.marker.show, false);
|
||||
});
|
||||
|
||||
test('selectedAtMs is the user’s selection time; a hold and a deselection leave none', async (t) => {
|
||||
const app = harness(t);
|
||||
const key = await busRunningNorth(app);
|
||||
// A vehicle only a hold selected reports none.
|
||||
assert.equal(app.layer.setCockpitSubject(key), true);
|
||||
assert.equal(app.layer.getCockpitSubject().selectedAtMs, null);
|
||||
app.layer.setCockpitSubject(null);
|
||||
assert.equal(app.layer.setCockpitSubject(null), true, 'twice is harmless');
|
||||
assert.equal(app.state()._selectedKey, key);
|
||||
// The user's selection stamps it, again on a re-selection.
|
||||
assert.equal(app.layer.focusVehicle(key), true);
|
||||
const first = app.layer.getSelectedVehicle().selectedAtMs;
|
||||
assert.ok(Number.isFinite(first));
|
||||
app.run(1000);
|
||||
app.layer.focusVehicle(key);
|
||||
assert.ok(app.layer.getSelectedVehicle().selectedAtMs > first);
|
||||
// Deselected, then held: none.
|
||||
globalThis.document.dispatchEvent(
|
||||
Object.assign(new Event('keydown'), { key: 'Escape' }),
|
||||
);
|
||||
assert.equal(app.state()._selectedKey, null);
|
||||
assert.equal(app.state()._selectedAtMs, null);
|
||||
app.layer.setCockpitSubject(key);
|
||||
assert.equal(app.layer.getCockpitSubject().selectedAtMs, null);
|
||||
});
|
||||
|
||||
test('a held vehicle hides, keeps moving out of view, and lets go when it goes', async (t) => {
|
||||
const app = harness(t);
|
||||
const key = await busRunningNorth(app);
|
||||
assert.equal(app.layer.setCockpitSubject('mbta:nope'), false);
|
||||
assert.equal(app.layer.setCockpitSubject(key), true);
|
||||
assert.equal(app.state()._selectedKey, key, 'holding selects it');
|
||||
assert.equal(app.layer.getCockpitSubject().key, key);
|
||||
assert.equal(
|
||||
app.overlaySources.has(TRANSIT_SELECTED_OVERLAY_SOURCE_ID),
|
||||
false,
|
||||
'no card',
|
||||
);
|
||||
const [entry] = app.vehicles();
|
||||
app.frame();
|
||||
assert.equal(entry.marker.show, false);
|
||||
// The camera's view leaves it, as a view from inside it does: it plays on.
|
||||
app.lookAt({ west: 10, south: 10, east: 11, north: 11 });
|
||||
app.settle();
|
||||
assert.equal(
|
||||
app.layer._transitPartsForTest().rendering.vehicleInView(entry),
|
||||
true,
|
||||
);
|
||||
const before = app.layer.getCockpitSubject().position.latitude;
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
await app.layer.update();
|
||||
app.settle();
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
assert.ok(
|
||||
app.layer.getCockpitSubject().position.latitude > before,
|
||||
'still moving',
|
||||
);
|
||||
// Escape and clicks are the view's while it holds the vehicle.
|
||||
globalThis.document.dispatchEvent(
|
||||
Object.assign(new Event('keydown'), { key: 'Escape' }),
|
||||
);
|
||||
assert.equal(app.state()._selectedKey, key);
|
||||
|
||||
assert.equal(app.layer.setCockpitSubject(null), true);
|
||||
assert.equal(app.layer.getCockpitSubject(), null);
|
||||
// Let go, it is out of play until a sweep finds it in view.
|
||||
assert.equal(app.state()._visible.has(entry), false, 'out of play');
|
||||
app.lookAt({
|
||||
west: -71.2,
|
||||
south: 42.2,
|
||||
east: -70.9,
|
||||
north: 42.5,
|
||||
});
|
||||
app.advance(250);
|
||||
app.settle();
|
||||
assert.equal(entry.marker.show, true, 'shown again');
|
||||
assert.equal(app.layer.focusVehicle('mbta:nope'), false);
|
||||
assert.equal(app.layer.focusVehicle(key), true);
|
||||
|
||||
app.layer.setCockpitSubject(key);
|
||||
app.silence('mbta');
|
||||
app.serve('mbta', () => ({
|
||||
status: 200,
|
||||
body: snapshot('mbta', 'MBTA', [], { fetchedAt: Date.now() }),
|
||||
}));
|
||||
for (let i = 0; i < MISSED_POLLS_TO_DROP + 1; i += 1) {
|
||||
app.run(TRANSIT_POLL_MS);
|
||||
await app.layer.update();
|
||||
}
|
||||
assert.equal(app.vehicles().length, 0, 'the vehicle is gone');
|
||||
assert.equal(app.state()._cockpitSubjectKey, null);
|
||||
assert.equal(app.layer.getCockpitSubject(), null);
|
||||
});
|
||||
@@ -484,7 +484,14 @@ export function createRendering({ state, services, parts }) {
|
||||
);
|
||||
entry.surfaceReady = !!known;
|
||||
entry.heightPending = !known;
|
||||
setShown(entry, !!known && vehicleInView(entry) && !entry.modelOwnsVisual);
|
||||
// A vehicle a first-person view rides keeps moving, unseen.
|
||||
setShown(
|
||||
entry,
|
||||
!!known &&
|
||||
vehicleInView(entry) &&
|
||||
!entry.modelOwnsVisual &&
|
||||
entry.key !== state._cockpitSubjectKey,
|
||||
);
|
||||
if (known) {
|
||||
const p = entry.marker.position;
|
||||
const s = state._scratchCartesian;
|
||||
@@ -566,7 +573,7 @@ export function createRendering({ state, services, parts }) {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function vehicleInView(entry) {
|
||||
return state._visible.has(entry);
|
||||
return state._visible.has(entry) || entry.key === state._cockpitSubjectKey;
|
||||
}
|
||||
|
||||
/** A preset tuple as a colour (0..1 channels), cached per profile+mode. */
|
||||
@@ -892,7 +899,7 @@ export function createRendering({ state, services, parts }) {
|
||||
});
|
||||
}
|
||||
|
||||
/** Hand an entry's visual back to its icon. */
|
||||
/** Hand an entry's visual back to its icon, unless a view rides it. */
|
||||
function handBack(entry) {
|
||||
if (!entry.modelOwnsVisual) return;
|
||||
entry.modelOwnsVisual = false;
|
||||
@@ -901,7 +908,8 @@ export function createRendering({ state, services, parts }) {
|
||||
entry,
|
||||
vehicleInView(entry) &&
|
||||
!entry.heightPending &&
|
||||
entry.surfaceReady !== false,
|
||||
entry.surfaceReady !== false &&
|
||||
entry.key !== state._cockpitSubjectKey,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -939,7 +947,12 @@ export function createRendering({ state, services, parts }) {
|
||||
}
|
||||
item.position = entry.marker.position;
|
||||
item.headingDeg = Number.isFinite(entry.courseDeg) ? entry.courseDeg : 0;
|
||||
item.show = !entry.heightPending && entry.surfaceReady !== false;
|
||||
// A vehicle a first-person view rides draws no model: the camera is
|
||||
// inside it (cockpitSubject.js).
|
||||
item.show =
|
||||
!entry.heightPending &&
|
||||
entry.surfaceReady !== false &&
|
||||
entry.key !== state._cockpitSubjectKey;
|
||||
next.set(entry.key, item);
|
||||
}
|
||||
for (const [key, item] of modelItems)
|
||||
@@ -1055,7 +1068,10 @@ export function createRendering({ state, services, parts }) {
|
||||
}
|
||||
}
|
||||
}
|
||||
const visible = (planes || bounds) && horizon && inFrustum;
|
||||
// A held vehicle (cockpitSubject.js) is the camera's own: it stays in
|
||||
// play wherever the view looks.
|
||||
const held = entry.key === state._cockpitSubjectKey;
|
||||
const visible = held || ((planes || bounds) && horizon && inFrustum);
|
||||
const visibility = (entry.visibility ||= {});
|
||||
visibility.bounds = bounds;
|
||||
visibility.boundsApplied = !planes;
|
||||
@@ -1085,6 +1101,7 @@ export function createRendering({ state, services, parts }) {
|
||||
setShown(
|
||||
entry,
|
||||
Boolean(
|
||||
!held &&
|
||||
visible &&
|
||||
!entry.heightPending &&
|
||||
entry.surfaceReady !== false &&
|
||||
|
||||
@@ -47,6 +47,12 @@ export function createSelection({ state, services, parts }) {
|
||||
? state._vehicles.get(state._selectedKey)
|
||||
: null;
|
||||
if (!entry) return;
|
||||
// A vehicle a first-person view rides shows no card.
|
||||
if (entry.key === state._cockpitSubjectKey) {
|
||||
state._selectedCardText = null;
|
||||
state._overlayHost.clearSource(TRANSIT_SELECTED_OVERLAY_SOURCE_ID);
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
if (!force && now - state._selectedCardAt < SELECTED_CARD_REFRESH_MS)
|
||||
return;
|
||||
@@ -92,6 +98,7 @@ export function createSelection({ state, services, parts }) {
|
||||
: null;
|
||||
parts.trails.clear();
|
||||
state._selectedKey = null;
|
||||
state._selectedAtMs = null;
|
||||
state._selectedCardText = null;
|
||||
state._detectRevision += 1;
|
||||
// Cleared first: the styling path reads the selection from state.
|
||||
@@ -99,11 +106,18 @@ export function createSelection({ state, services, parts }) {
|
||||
state._overlayHost.clearSource(TRANSIT_SELECTED_OVERLAY_SOURCE_ID);
|
||||
}
|
||||
|
||||
function selectVehicle(key) {
|
||||
/**
|
||||
* Select a vehicle. `byUser` marks a selection the user made (a click, or
|
||||
* focusVehicle), even of the selected vehicle again: its time is the
|
||||
* snapshot's `selectedAtMs` (cockpitSubject.js). A first-person view's hold
|
||||
* selects without it.
|
||||
*/
|
||||
function selectVehicle(key, { byUser = false } = {}) {
|
||||
clearSelection();
|
||||
const entry = state._vehicles.get(key);
|
||||
if (!entry?.marker) return;
|
||||
state._selectedKey = key;
|
||||
if (byUser) state._selectedAtMs = Date.now();
|
||||
state._detectRevision += 1;
|
||||
parts.rendering.paintSelected(entry, true);
|
||||
parts.trails.select(entry);
|
||||
@@ -112,6 +126,8 @@ export function createSelection({ state, services, parts }) {
|
||||
}
|
||||
|
||||
function onKeyDown(event) {
|
||||
// While a first-person view rides the vehicle, Escape is the view's.
|
||||
if (state._cockpitSubjectKey) return;
|
||||
if (event.key === 'Escape' && state._selectedKey) clearSelection();
|
||||
}
|
||||
|
||||
@@ -120,6 +136,8 @@ export function createSelection({ state, services, parts }) {
|
||||
// The dispatcher maps a sensor-style click back into the pick buffer
|
||||
// (src/input/sensorPick.js) and skips it while a tool holds the pointer.
|
||||
state._clickSubscription = inputFor(viewer).onClick((event) => {
|
||||
// A held vehicle keeps the selection until its view lets it go.
|
||||
if (state._cockpitSubjectKey) return;
|
||||
const position = event.position;
|
||||
// A marker is an item of one of this layer's two sets, by its key.
|
||||
const hit = event.drawHit;
|
||||
@@ -134,7 +152,7 @@ export function createSelection({ state, services, parts }) {
|
||||
set === 'moving' ? 'animated' : set === 'still' ? 'stationary' : null,
|
||||
};
|
||||
if (key) {
|
||||
selectVehicle(key);
|
||||
selectVehicle(key, { byUser: true });
|
||||
return;
|
||||
}
|
||||
if (state._selectedKey) clearSelection();
|
||||
|
||||
@@ -96,6 +96,15 @@ export function createState({ services }) {
|
||||
|
||||
/** @type {string|null} */
|
||||
state._selectedKey = null;
|
||||
/** When the user last selected a vehicle, even the selected one (ms). */
|
||||
state._selectedAtMs = null;
|
||||
/**
|
||||
* The vehicle a first-person view holds (cockpitSubject.js), or null: its
|
||||
* sprite, 3D model, card, trail and detection box are hidden, and it keeps
|
||||
* moving out of the camera's view.
|
||||
* @type {string|null}
|
||||
*/
|
||||
state._cockpitSubjectKey = null;
|
||||
state._selectedCardAt = 0;
|
||||
/** The card text last published, so an unchanged card is not re-sent. */
|
||||
state._selectedCardText = null;
|
||||
|
||||
@@ -235,8 +235,11 @@ export function createTrails({ state, services, parts, source }) {
|
||||
}
|
||||
function update() {
|
||||
if (!selected || !renderer) return;
|
||||
// A first-person view on the vehicle rides where its trail would start.
|
||||
renderer.setVisible(
|
||||
state._enabled && state._vehicles.get(selected.key) === selected,
|
||||
state._enabled &&
|
||||
state._vehicles.get(selected.key) === selected &&
|
||||
selected.key !== state._cockpitSubjectKey,
|
||||
);
|
||||
renderer.setDisplaySample(selected.sample, selected.marker?.position);
|
||||
}
|
||||
|
||||
@@ -91,6 +91,10 @@ export function createViewport({ state, services, parts }) {
|
||||
if (state._activeFeeds.has(feed.id)) desired.set(feed.id, feed);
|
||||
}
|
||||
}
|
||||
// A vehicle a first-person view rides keeps its feed polled.
|
||||
const held = state._vehicles.get(state._cockpitSubjectKey);
|
||||
if (held && state._activeFeeds.has(held.feedId))
|
||||
desired.set(held.feedId, state._activeFeeds.get(held.feedId));
|
||||
for (const feedId of [...state._activeFeeds.keys()]) {
|
||||
if (!desired.has(feedId)) {
|
||||
state._inFlight.get(feedId)?.controller.abort();
|
||||
|
||||
Reference in new issue
Block a user