From 83081730c0ef5b52f9f4a80021291634821fb881 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Thu, 19 Nov 2026 00:26:57 +0400 Subject: [PATCH] Move only the anchor's height when the floor height arrives - The first height that replaces the entry frame's now moves only the anchor's height. The anchor keeps its place and its eased lift, so the camera no longer jumps to a corrected fix or steps back. - A detail line is measured with the title's glyph widths. They are a little wide for its smaller type, so a long detail line is no longer clipped. - The first aircraft update is checked in the existing test, and the copy is removed; test fixtures are simpler. - Comments, the release note and CURRENT-STATE match the code. --- .../1787-cockpit-subject-camera-hud.md | 15 +-- docs/CURRENT-STATE.md | 63 ++++++----- src/cockpitProgress.js | 28 ++--- src/cockpitProgress.test.mjs | 30 ++--- src/cockpitSubjectProviders.js | 6 +- src/ui/cockpitCamera.js | 22 ++-- src/ui/cockpitController.test.mjs | 39 +------ src/ui/cockpitSignals.js | 4 +- src/ui/cockpitSubjectView.test.mjs | 107 +++++++++++++++--- 9 files changed, 176 insertions(+), 138 deletions(-) diff --git a/changelog.d/1787-cockpit-subject-camera-hud.md b/changelog.d/1787-cockpit-subject-camera-hud.md index 9231f3578b..35b3245281 100644 --- a/changelog.d/1787-cockpit-subject-camera-hud.md +++ b/changelog.d/1787-cockpit-subject-camera-hud.md @@ -1,10 +1,11 @@ - Keep an application's Cockpit subject steady. A subject whose entry frame gives a height stands there until a later frame or the floor under it gives another, so the view no longer ends while the floor loads. The camera keeps - up when the browser draws few frames: at 5 frames a second it used to fall - 700 m behind a 30 m/s subject in a minute. A provider's Live Signals rows - change where they stand, their tone included, and rows it drops leave - before new ones come in, so a changing ETA or a replaced row no longer - pushes a steady warning off the list. Progress labels give CJK, kana and - Hangul names the width they need. Labels, entry face and look keep their - last good words when a later read fails (German, @gekh, #1787). + up when the browser draws few frames: updated every frame at 5 frames a + second, it used to fall 700 m behind a 30 m/s subject in a minute. A + provider's Live Signals rows change where they stand, their tone included, + and rows it drops leave before new ones come in, so a changing ETA or a + replaced row no longer pushes a steady warning off the list. Progress labels + give CJK, kana and Hangul names the width they need. Labels, entry face and + look keep their last good words when a later read fails + (German, @gekh, #1787). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 1adff6c421..bb301fd01a 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2742,38 +2742,39 @@ priority; one first seen together with a built-in subject comes after it. The first in that order decides alone: when its gate is shut there is no entry rather than the next subject's. -A frame places the eye `eye.upM` above `heightM` (the shared floor where the -frame gives none, or, while that floor is cold, the last height the eye stood -at, a frame's or the floor's, at first the entry frame's; with neither, the camera stays where it is and the HUD reads ACQUIRING SURFACE for -at most `COCKPIT_GROUND_WAIT_TIMEOUT_MS`, after which the view ends and the -subject is released: no height stands in for a floor never read), slews toward `headingDeg` at the provider's rate, and may -turn off the floor clamps (`groundClamp: false`, for a subject inside a -structure) or keep the anchor exactly on the subject (`anchor: 'exact'`, for a -layer that already moves it smoothly; a frame with no `speedMps` is always -exact, since an inertial anchor runs on at the frame's speed); `cameraIntervalMs: 0` updates the -camera every rendered frame. Each update turns the view, eases its lift and -moves an inertial anchor by the time since the last one, at most a second past -the interval (`cockpitSubjectStepSec`); an exact anchor stands where the frame -is. The first update after entry (after any floor wait) stands its anchor on -the subject, as does the first to replace the entry frame's height with -another. A frame that is null, malformed or for another -subject ends the view (`restoreTracking: false`), as an aircraft tracked -meanwhile and the provider's layer turning off do; the exit control asks the -provider to restore its subject. The generic HUD shows the provider's kicker, -title, meta line, speed with its unit and tag, readout cards, always-visible -source rows, banner, route card and Live Signals rows (each changes in place, -its tone included, and keeps its time while it stays listed; one pushed off -comes back as the newest row; rows the view no longer lists leave before new -ones come in), a veil that darkens the +A frame places the eye `eye.upM` above `heightM`. With no `heightM` the eye +stands on the shared floor; while that floor is cold, at the last height it +stood at (at first the entry frame's). With neither, the camera stays where it +is and the HUD reads ACQUIRING SURFACE for at most +`COCKPIT_GROUND_WAIT_TIMEOUT_MS`, after which the view ends and the subject is +released: no height stands in for a floor never read. The view slews toward +`headingDeg` at the provider's rate. A frame may turn off the floor clamps +(`groundClamp: false`, for a subject inside a structure) or keep the anchor +exactly on the subject (`anchor: 'exact'`, for a layer that already moves it +smoothly; a frame with no `speedMps` is always exact, since an inertial anchor +runs on at the frame's speed); `cameraIntervalMs: 0` updates the camera every +rendered frame. Each update turns the view, eases its lift and moves an inertial +anchor by the time since the last one, at most a second past the interval +(`cockpitSubjectStepSec`); an exact anchor stands where the frame is. The first +update after entry (after any floor wait) stands its anchor on the subject; the +first height to replace the entry frame's moves the anchor to that height where +it stands. A frame that is null, malformed or for another subject ends the view +(`restoreTracking: false`), as an aircraft tracked meanwhile and the provider's +layer turning off do; the exit control asks the provider to restore its subject. +The generic HUD shows the provider's kicker, title, meta line, speed with its +unit and tag, readout cards, always-visible source rows, banner, route card and +Live Signals rows (each changes in place, its tone included, and keeps its time +while it stays listed; one pushed off comes back as the newest row; rows the +view no longer lists leave before new ones come in), a veil that darkens the picture under the HUD (with passing light streaks, which stand still under -reduced motion), arrow-key look-around when the provider asks for it, and a progress -line along the bottom edge (`src/cockpitProgress.js`: close ticks merge, -labels never overlap and are sized for CJK, kana and Hangul). Core translates none of the provider's text: a host -words it for the page's language (`gods-eye-view/i18n`: -`getLocale`, `onLocaleChange`). Its entry face, labels and look may be -functions, read again when they are shown and on a change of language, as the -HUD view is; a read that throws or is malformed keeps the last good words, at -first those checked when the provider was registered. +reduced motion), arrow-key look-around when the provider asks for it, and a +progress line along the bottom edge (`src/cockpitProgress.js`: close ticks +merge, labels never overlap and are sized for CJK, kana and Hangul). Core +translates none of the provider's text: a host words it for the page's language +(`gods-eye-view/i18n`: `getLocale`, `onLocaleChange`). Its entry face, labels +and look may be functions, read again when they are shown and on a change of +language, as the HUD view is; a read that throws or is malformed keeps the last +good words, at first those checked when the provider was registered. The Rail layer offers a read API for such a view: `getSelectedTrain()` and `getCockpitSubject()` (a snapshot of the train: where it is along its line, diff --git a/src/cockpitProgress.js b/src/cockpitProgress.js index 1761829798..abdc0eb467 100644 --- a/src/cockpitProgress.js +++ b/src/cockpitProgress.js @@ -34,36 +34,22 @@ const stronger = (a, b) => const WIDE_GLYPHS = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uA960-\uA97F\uAC00-\uD7A3\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6]/g; -/** What a full-width glyph and any other UTF-16 unit take in a title (px). */ -const TITLE_PX = Object.freeze({ wide: 10.6, narrow: 6.4 }); -/** The same in a detail line (px). */ -const DETAIL_PX = Object.freeze({ - wide: 10 / 1.2 + 0.6, - narrow: 5.8 / 1.2 + 0.6, -}); - /** * One line's width (px) in `.cockpit-progress-label` type (cockpitSubject.css: - * 10 px, 0.06 em spacing) at the glyph widths `px`: about 1 em a full-width - * glyph and 0.58 em each UTF-16 unit of any other, so an astral glyph (an - * emoji, a CJK extension) counts as two and an emoji in the BMP (☕) as one, - * narrower than it is drawn. The detail line's `` is 1/1.2 the size; - * the 0.6 px spacing it inherits is not scaled. + * 10 px, 0.06 em spacing): 10.6 px a full-width glyph and 6.4 px each UTF-16 + * unit of any other, so an astral glyph (an emoji, a CJK extension) counts as + * two and an emoji in the BMP (☕) as one, narrower than it is drawn. The + * detail line's `` draws smaller, so these widths only overestimate it. */ -function lineWidthPx(line, px) { +function lineWidthPx(line) { const text = String(line ?? ''); const wide = text.match(WIDE_GLYPHS)?.length ?? 0; - return wide * px.wide + (text.length - wide) * px.narrow; + return wide * 10.6 + (text.length - wide) * 6.4; } /** A label's width estimate (px): its wider line and 8 px of padding. */ export function estimateProgressLabelPx(label) { - return ( - Math.max( - lineWidthPx(label?.text, TITLE_PX), - lineWidthPx(label?.detail, DETAIL_PX), - ) + 8 - ); + return Math.max(lineWidthPx(label?.text), lineWidthPx(label?.detail)) + 8; } /** diff --git a/src/cockpitProgress.test.mjs b/src/cockpitProgress.test.mjs index 04920ccd68..dcf2836bce 100644 --- a/src/cockpitProgress.test.mjs +++ b/src/cockpitProgress.test.mjs @@ -163,7 +163,7 @@ test('a CJK stop name gets the width its full-width glyphs need', () => { ); }); -test('emoji and other astral glyphs in a progress label get at least 1 em each', () => { +test('astral glyphs (emoji outside the BMP, supplementary kana) in a progress label get at least 1 em each', () => { // About 1 em in 10 px type, 0.06 em apart. const emPx = 10 + 0.6; for (const [text, needPx] of [ @@ -183,21 +183,21 @@ test('emoji and other astral glyphs in a progress label get at least 1 em each', }); test('a label is as wide as its wider line, its smaller detail line included', () => { - // The detail line's is 1/1.2 of 10 px; its 0.6 px spacing is not. - const detailEmPx = 10 / 1.2 + 0.6; - const detail = '中央駅前'; // 4 full-width glyphs - const givenPx = estimateProgressLabelPx({ text: 'GATE', detail }); assert.ok( - givenPx > estimateProgressLabelPx({ text: 'GATE', detail: '' }), + estimateProgressLabelPx({ text: 'GATE', detail: '中央駅前' }) > + estimateProgressLabelPx({ text: 'GATE', detail: '' }), 'the wider detail line widens the label', ); - assert.ok( - givenPx >= detail.length * detailEmPx + 8, - `${givenPx} px given, about ${detail.length * detailEmPx} px needed`, - ); - // A line takes less room as the smaller detail than as the title. - assert.ok( - estimateProgressLabelPx({ text: 'A', detail: 'CENTRAL STATION' }) < - estimateProgressLabelPx({ text: 'CENTRAL STATION', detail: '' }), - ); + // As headless Chrome draws the detail's (8.333 px JetBrains Mono, + // 0.6 px apart): 'WWWWMMMMWWWW' at 67.2 px, '中央駅前広場東口' at 72.9 px. + for (const [detail, needPx] of [ + ['W'.repeat(64), 64 * (67.2 / 12)], + ['駅'.repeat(45), 45 * (72.9 / 8)], + ]) { + const givenPx = estimateProgressLabelPx({ text: 'A', detail }); + assert.ok( + givenPx >= needPx, + `${detail.length} × ${detail[0]}: ${givenPx} px given, ${needPx} px needed`, + ); + } }); diff --git a/src/cockpitSubjectProviders.js b/src/cockpitSubjectProviders.js index fa0b4deffb..cb528decfa 100644 --- a/src/cockpitSubjectProviders.js +++ b/src/cockpitSubjectProviders.js @@ -117,13 +117,14 @@ function lookOf(raw) { }); } -/** Promises worded members gave, each one's rejection caught once. */ +/** Promises already given a catch: a pending one is read again on every use. */ const caughtWords = new WeakSet(); /** * A worded member as `read()` gives it, normalized: called on `provider` * when it is a function (so it can follow the page's language); null when it - * is malformed (a promise too) or provider code throws. + * is malformed (a promise too) or provider code throws, its getter included + * (hence `read`). */ function worded(provider, read, normalize) { try { @@ -269,7 +270,6 @@ export function cockpitSubjectProviders( */ function keptProvider(provider, order, words) { let { face, labels, look } = words; - // Look stays on or off, at its rest pitch, as read at registration. const restPitchDeg = look?.restPitchDeg ?? 0; return Object.freeze({ kind: provider.kind, diff --git a/src/ui/cockpitCamera.js b/src/ui/cockpitCamera.js index 0aaf4bcf25..439b5fa9f3 100644 --- a/src/ui/cockpitCamera.js +++ b/src/ui/cockpitCamera.js @@ -403,11 +403,18 @@ function updateSubject(nowMs) { this.surfaceWaitStartedMs = 0; this.surfaceAcquiring = false; if (!held) { - // The first height to replace the entry frame's places the anchor - // anew: an inertial one would close on it only at the horizontal - // correction's rate. - if (this.subjectHeightFromEntry && heightM !== this.subjectHeightM) - this.cockpitAnchorValid = false; + // The first height to replace the entry frame's is the anchor's at once + // (an inertial anchor would close on it only at the horizontal + // correction's rate); where the anchor stands, it stays. + if ( + this.subjectHeightFromEntry && + this.cockpitAnchorValid && + heightM !== this.subjectHeightM + ) { + const anchor = toCartographicRadians(this.cockpitAnchor); + if (anchor) + this.cockpitAnchor = fromRadians(anchor.lon, anchor.lat, heightM); + } this.subjectHeightFromEntry = false; } this.subjectHeightM = heightM; @@ -472,9 +479,8 @@ function updateSubject(nowMs) { * Aircraft pass none and look along the heading at the profile pitch. */ function placeCamera(target, info, nowMs, dtSec, profile, look = null) { - // An update that sets the anchor stands it on the target with no step: - // the first one (the time before it, an entry or a floor wait, moved - // nothing), and a subject's whose height replaced its entry frame's. + // The update that first sets the anchor stands it on the target: the time + // before it (an entry, a floor wait) moved nothing, so it has no step. const anchoring = !this.cockpitAnchorValid; if (anchoring) { this.cockpitAnchorValid = true; diff --git a/src/ui/cockpitController.test.mjs b/src/ui/cockpitController.test.mjs index 9a08f13e77..6cea3b1bcb 100644 --- a/src/ui/cockpitController.test.mjs +++ b/src/ui/cockpitController.test.mjs @@ -444,10 +444,10 @@ test('Cockpit places the eye at its aircraft as the Cesium maths did', async () latitude: 30.19, longitude: -97.67, onGround: false, - stale: true, + stale: false, }; const { owner, views } = cockpitOver({ info, target }); - // Held on the target while the feed is stale. + // The first update stands the anchor on the target. owner.update(); let expected = cesiumCockpitEye(Cesium, { target, @@ -456,13 +456,12 @@ test('Cockpit places the eye at its aircraft as the Cesium maths did', async () dtSec: 0, liftM: 0, }); - near(views[0].destination, expected.eye, 1e-6, 'stale eye'); + near(views[0].destination, expected.eye, 1e-6, 'first eye'); near(views[0].orientation.direction, expected.forward, 1e-12, 'direction'); near(views[0].orientation.up, expected.up, 1e-12, 'up'); // Live: the anchor coasts 80 ms along the heading and is pulled toward // where the layer now shows the aircraft. - info.stale = false; const anchor = Cesium.Cartesian3.clone(expected.anchor); const moved = Cesium.Cartesian3.fromDegrees(-97.6698, 30.1901, 1210); owner.trackedFollow.position = () => moved; @@ -495,38 +494,6 @@ test('Cockpit places the eye at its aircraft as the Cesium maths did', async () } }); -test("a live aircraft's first Cockpit update stands the anchor on it", async () => { - const Cesium = await import('cesium'); - const env = environment(); - try { - const target = Cesium.Cartesian3.fromDegrees(-97.67, 30.19, 1200); - const info = { - icao24: 'abc123', - layerId: 'flights', - track: 72, - velocityMps: 200, - latitude: 30.19, - longitude: -97.67, - onGround: false, - stale: false, - }; - const { owner, views } = cockpitOver({ info, target }); - // Entry was a while ago: the step would carry the anchor 20 m ahead. - owner.update(); - const expected = cesiumCockpitEye(Cesium, { - target, - info: { ...info, stale: true }, - heading: 72, - dtSec: 0, - liftM: 0, - }); - near(views[0].destination, expected.eye, 1e-6, 'first live eye'); - owner.dispose(); - } finally { - env.restore(); - } -}); - test('a grounded Cockpit probes the mesh from the camera and keeps the eye above the floor', async () => { const Cesium = await import('cesium'); const env = environment(); diff --git a/src/ui/cockpitSignals.js b/src/ui/cockpitSignals.js index 96446163ef..6ed054a0a4 100644 --- a/src/ui/cockpitSignals.js +++ b/src/ui/cockpitSignals.js @@ -56,8 +56,8 @@ export function pushCockpitSignal( { inPlace = false } = {}, ) { if (!title || !detail) return; - // JSON keeps the fields apart: joined with `|`, a title or detail holding a - // `|` read as another pair. An in-place row also changes with its tone alone. + // JSON, not a joined string, so a separator inside a title or detail cannot + // make two rows match. An in-place row also changes with its tone alone. const signature = JSON.stringify([ title, detail, diff --git a/src/ui/cockpitSubjectView.test.mjs b/src/ui/cockpitSubjectView.test.mjs index 7d6150f046..7df612e056 100644 --- a/src/ui/cockpitSubjectView.test.mjs +++ b/src/ui/cockpitSubjectView.test.mjs @@ -219,22 +219,20 @@ function fixture( showBriefPage() {}, startBriefRotation() {}, stopBriefRotation() {}, - pushCockpitSignal(key, tone, title, detail) { - signals.push({ key, tone, title, detail }); - }, - dropCockpitSignal(key) { - dropped.push(key); - }, scheduleContextLayout() {}, updateLocalPosition() {}, maybeRefreshRegionalBrief() {}, showContextCard() {}, }); - if (realSignals) { - // The real Live Signals rows, published, not the recorder above. - delete cockpit.pushCockpitSignal; - delete cockpit.dropCockpitSignal; - } + if (!realSignals) + Object.assign(cockpit, { + pushCockpitSignal(key, tone, title, detail) { + signals.push({ key, tone, title, detail }); + }, + dropCockpitSignal(key) { + dropped.push(key); + }, + }); t.after(() => { cockpit.stop(); app.unmount(); @@ -297,7 +295,7 @@ const lastRelease = (calls) => calls.findLast(([name]) => name === 'release'); /** * Move `ride`'s subject east at `speedMps` from `startMs` (the manual clock's * start) on an inertial anchor, its frame otherwise as it was with `extra` - * on top; returns where it is now (longitude). + * on top; returns a function giving its longitude now. */ function movingEast(ride, startMs, speedMps, extra = {}) { const eastNow = () => @@ -1355,9 +1353,7 @@ test("a moving vessel's first bridge update after entry stands the anchor on its assert.equal(cockpit.enter(), true); clock.advance(1000); cockpit.update(); - const anchor = Cesium.Cartographic.fromCartesian( - Cesium.Cartesian3.clone(cockpit.cockpitAnchor), - ); + const anchor = Cesium.Cartographic.fromCartesian(cockpit.cockpitAnchor); const fix = cockpit.vesselFix; const gapM = Cesium.Cartesian3.distance( Cesium.Cartesian3.fromRadians(anchor.longitude, anchor.latitude, 0), @@ -1564,3 +1560,84 @@ test('an inertial subject placed at its entry height stands on the floor once it `1 s after the floor came the eye is at ${heightM.toFixed(2)} m, not ${floorM + 3.5} m`, ); }); + +/** How far east of LON the camera's last shot is (m). */ +const shotEastM = (shots) => + (Cesium.Math.toDegrees( + Cesium.Cartographic.fromCartesian(shots.at(-1).destination).longitude, + ) - + LON) * + M_PER_DEG_LON; + +test('the first floor after an entry height moves an inertial anchor as a cold floor does', async (t) => { + // The provider corrects its subject 100 m east as the floor arrives. + const run = (warm) => { + const clock = manualClock(t, 10_000); + const ride = subjectProvider(); + ride.subject.frame = { + ...ride.subject.frame, + heightM: FLOOR_M, + speedMps: 0, + anchor: 'inertial', + groundClamp: false, + }; + const { cockpit, shots, setFloor } = fixture(t, { providers: [ride] }); + setFloor(undefined); + cockpit.syncEntry(); + assert.equal(cockpit.enter(), true); + ride.subject.frame = { ...ride.subject.frame, heightM: null }; + clock.advance(100); + cockpit.update(); + const beforeM = shotEastM(shots); + ride.subject.frame = { ...ride.subject.frame, longitude: eastM(100) }; + if (warm) setFloor(FLOOR_M + 0.1); + clock.advance(100); + cockpit.update(); + const stepM = shotEastM(shots) - beforeM; + const heightM = shotHeight(shots); + cockpit.stop(); + return { stepM, heightM }; + }; + const cold = run(false); + const warm = run(true); + assert.ok( + Math.abs(warm.stepM - cold.stepM) < 0.02, + `east step ${warm.stepM.toFixed(3)} m with the floor arriving, ${cold.stepM.toFixed(3)} m with it cold`, + ); + assert.ok(Math.abs(warm.heightM - (FLOOR_M + 0.1 + 3.5)) < 0.02); +}); + +test('the first floor after an entry height never steps an inertial anchor back to a held fix', async (t) => { + const startMs = 10_000; + const clock = manualClock(t, startMs); + const ride = subjectProvider(); + const base = { + ...ride.subject.frame, + heightM: FLOOR_M, + speedMps: 30, + anchor: 'inertial', + groundClamp: false, + }; + ride.subject.frame = base; + const { cockpit, shots, setFloor } = fixture(t, { providers: [ride] }); + setFloor(undefined); + cockpit.syncEntry(); + assert.equal(cockpit.enter(), true); + // Telemetry that gives a new fix once a second, 30 m on each time. + ride.subject.frame = () => ({ + ...base, + heightM: null, + longitude: eastM(30 * Math.floor((performance.now() - startMs) / 1000)), + }); + for (let i = 0; i < 17; i += 1) { + clock.advance(100); + cockpit.update(); + } + const beforeM = shotEastM(shots); + // The floor arrives 1 cm off the entry height, between two fixes. + setFloor(FLOOR_M + 0.01); + clock.advance(100); + cockpit.update(); + const stepM = shotEastM(shots) - beforeM; + assert.ok(stepM > 0, `the eye stepped ${stepM.toFixed(2)} m east`); +});