Read the followed target, not trackedEntity, in the follow readers

The tracked card, screen annotations, north-up, share links and Cockpit
ask data/followed.js what the camera follows, so they work the same when
a layer follows through the camera port. followed.js gains
entityFollowTarget (a FollowTarget whose presentation reads the drawn
entity's record), resumeFollowing and followedPosition.
This commit is contained in:
Sameh Khamis committed 2026-11-06 15:31:36 -08:00
1 parent 7eec2f828c
commit 99f9a701d9
16 files changed
+215 -84

No files matched your search

+60 -1
View File
@@ -7,7 +7,14 @@
* Until every layer does, a layer may still track an entity of its own
* through Cesium's `trackedEntity`; that entity is seen here through the
* same fields, read from its entity record. Readers ask this module, never
* `trackedEntity`, so they work either way.
* `trackedEntity`, so they work either way. A layer that still draws its
* target as an entity follows through the port with `entityFollowTarget`,
* whose presentation fields read that same record.
*
* Its readers (the tracked card, Cockpit, the share link's thing view,
* north-up) still read the drawn entity and its `displayPosition`: a port
* target without them is followed by the camera but not shown by them,
* until those readers move with the drawing to the renderer.
*/
import { findCamera } from '../render/rendererOf.js';
import { readEntityRecord } from './entityRecords.js';
@@ -125,3 +132,55 @@ export function stopFollowing(viewer) {
if (camera?.followed) camera.follow(null);
else if (viewer.trackedEntity) viewer.trackedEntity = undefined;
}
/**
* A FollowTarget for a layer that draws its target as an entity of its own:
* the camera fields given (`position`, `viewFrom`, `frame`, `minRange`, ...)
* plus what the app shows about it, read live from the entity's record, so the
* layer keeps writing that record as before. `entity` is the drawn entity.
* @param {object} entity
* @param {object} camera The FollowTarget's camera fields.
* @returns {Followed & object}
*/
export function entityFollowTarget(entity, camera) {
const target = Object.create(entityView(entity));
// Defined, not assigned: the record's fields are getters, and a camera
// field of the same name (`key`, say) takes their place.
for (const [name, value] of Object.entries(camera))
Object.defineProperty(target, name, {
value,
enumerable: true,
writable: true,
configurable: true,
});
return target;
}
/**
* Follow `followed` again after `stopFollowing`: a port target through the
* camera port, a layer's own tracked entity through `trackedEntity`.
* @param {object} viewer
* @param {Followed|null} followed What `followedOf` gave.
*/
export function resumeFollowing(viewer, followed) {
if (!viewer || !followed) return;
if (entityViews.get(followed.entity) === followed)
viewer.trackedEntity = followed.entity;
else findCamera(viewer)?.follow(followed);
}
/**
* Where `followed` is shown this frame: the layer's display cache, else its
* entity's position at the viewer's time; null when neither answers.
* @param {object} viewer
* @param {Followed|null} followed
*/
export function followedPosition(viewer, followed) {
const display = followed?.displayPosition;
const shown = typeof display === 'function' ? display() : null;
return (
shown ||
followed?.entity?.position?.getValue?.(viewer?.clock?.currentTime) ||
null
);
}
+52
View File
@@ -3,9 +3,12 @@ import test from 'node:test';
import * as Cesium from 'cesium';
import { entityRecord } from './entityRecords.js';
import {
entityFollowTarget,
followedOf,
followedPosition,
isFollowing,
onFollowChanged,
resumeFollowing,
stopFollowing,
} from './followed.js';
import { bindRenderer } from '../render/rendererOf.js';
@@ -121,3 +124,52 @@ test('a renderer bound after subscribing is still heard', (t) => {
camera.follow(null);
assert.deepEqual(seen, ['k', null]);
});
test('an entity follow target reads its entity record live', () => {
const entity = new Cesium.Entity({ id: 'drawn', name: 'N2' });
const position = () => ({ x: 1, y: 2, z: 3 });
const target = entityFollowTarget(entity, { position, minRange: 150 });
assert.equal(target.key, 'entity:drawn');
entityRecord(entity).trackedId = 'flights:abc';
entityRecord(entity).labelModel = { title: 'N2' };
assert.equal(target.key, 'flights:abc');
assert.equal(target.labelModel.title, 'N2');
assert.equal(target.entity, entity);
assert.equal(target.position, position);
assert.equal(target.minRange, 150);
const keyed = entityFollowTarget(entity, { key: 'flights:given' });
assert.equal(keyed.key, 'flights:given', 'a camera field takes the place');
assert.equal(keyed.labelModel.title, 'N2');
});
test('resumeFollowing follows a port target again, or re-tracks an entity', (t) => {
const viewer = trackingViewer();
const entity = new Cesium.Entity({ id: 'legacy' });
viewer.trackedEntity = entity;
const legacy = followedOf(viewer);
stopFollowing(viewer);
resumeFollowing(viewer, legacy);
assert.equal(viewer.trackedEntity, entity);
assert.equal(followedOf(viewer), legacy);
viewer.trackedEntity = undefined;
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
const target = entityFollowTarget(new Cesium.Entity({ id: 'p' }), {});
camera.follow(target);
stopFollowing(viewer);
resumeFollowing(viewer, target);
assert.equal(camera.followed, target);
resumeFollowing(viewer, null);
assert.equal(camera.followed, target);
});
test('followedPosition prefers the display cache over the entity position', () => {
const at = new Cesium.Cartesian3(4, 5, 6);
const viewer = { clock: { currentTime: Cesium.JulianDate.now() } };
const entity = new Cesium.Entity({ id: 'e', position: at });
const view = entityFollowTarget(entity, {});
assert.deepEqual(followedPosition(viewer, view), at);
entityRecord(entity).displayPosition = () => ({ x: 7, y: 8, z: 9 });
assert.deepEqual(followedPosition(viewer, view), { x: 7, y: 8, z: 9 });
assert.equal(followedPosition(viewer, null), null);
});
+7 -7
View File
@@ -1,3 +1,4 @@
import { followedOf, isFollowing, onFollowChanged } from './followed.js';
import {
clearOverlaySource,
setOverlayEntries,
@@ -126,7 +127,7 @@ function entryIdFor(entity) {
}
function activeEntity() {
return _viewer?.trackedEntity || _selectedContext?.entity || null;
return followedOf(_viewer)?.entity || _selectedContext?.entity || null;
}
/**
@@ -235,11 +236,10 @@ export function initTrackedReadout(viewer) {
if (_viewer) destroyTrackedReadout();
_viewer = viewer;
_overlayHost.setVisible(TRACKED_OVERLAY_SOURCE_ID, true);
_trackedEntityChangedRemove =
viewer.trackedEntityChanged?.addEventListener?.(() => {
if (viewer.trackedEntity) _selectedContext = null;
syncActiveEntity();
}) || null;
_trackedEntityChangedRemove = onFollowChanged(viewer, (followed) => {
if (followed) _selectedContext = null;
syncActiveEntity();
});
_contextSelectedHandler = (event) => {
const record = event.detail;
if (READOUT_CONTEXT_LAYERS.has(record?.layerId)) {
@@ -263,7 +263,7 @@ export function initTrackedReadout(viewer) {
};
_aircraftSelectedHandler = () => {
_selectedContext = null;
if (!_viewer?.trackedEntity) clearTrackedSource();
if (!isFollowing(_viewer)) clearTrackedSource();
};
window.addEventListener('gev:entity-selected', _contextSelectedHandler);
window.addEventListener(