Move the imagery box preview into the recent imagery renderer

The box selection tool no longer builds Cesium entities itself: its fill
and dashed outline live in createImageryBoxPreview in the recent imagery
rendering module, and the tool takes the preview as a dependency. The
cockpit contact card reads the nearest contact's position with the geo
helpers. Both files leave the allow-list.

Tests now inject the preview with the Cesium double, check that a second
destroy returns the same promise, and pin the nose-relative bearing to a
known contact.
This commit is contained in:
Sameh Khamis committed 2026-11-07 03:11:51 -08:00
1 parent 6ee3bb2f7d
commit d928c30337
7 files changed
+153 -87

No files matched your search

+3 -1
View File
@@ -13,6 +13,7 @@ import {
SPLIT_PUBLISH_MS,
createRecentImageryLayer,
} from './index.js';
import { createImageryBoxPreview } from './rendering.js';
import {
BOX,
CANDIDATES,
@@ -1399,7 +1400,8 @@ test('with SELECT BOX armed, Escape clears the preview first and the next Escape
const cancels = [];
const tool = initImageryBoxTool({
viewer: fakes.viewer,
cesium: fakes.cesium,
createPreview: (viewer, dragBox) =>
createImageryBoxPreview(viewer, dragBox, { cesium: fakes.cesium }),
pickWorld: fakes.pickWorld,
documentRef: fakes.documentRef,
onBox: (box) => f.layer.setBox(box),
+95
View File
@@ -11,6 +11,7 @@
// renderer is its Cesium side, so it brings the factories a viewer with no
// renderer bound gets those ports from, and marks a tileset host as a base
// the globe port drapes on.
import * as Cesium from 'cesium';
import '../../render/cesium/camera.js';
import { markBaseTileset } from '../../render/cesium/globe.js';
import { PRODUCTS, gibsTemplate } from './model.js';
@@ -235,3 +236,97 @@ export function createRecentImageryRenderer({
},
};
}
/**
* The box tool's preview: the box being dragged, a translucent fill and a
* dashed outline on the ground (the draw port draws no dashed draped
* lines), read from `dragBox()` each frame it draws.
* @param {object} viewer
* @param {() => ({west: number, south: number, east: number, north: number}|null)} dragBox
* Degrees, or null while no drag is live.
* @param {{cesium?: object}} [options] The Cesium namespace (tests pass a double).
* @returns {{setVisible: (visible: boolean) => void, changed: () => void, destroy: () => Promise<void>}}
*/
export function createImageryBoxPreview(
viewer,
dragBox,
{ cesium = Cesium } = {},
) {
const dataSource = new cesium.CustomDataSource(
'gev-recent-imagery-box-preview',
);
let attaching = Promise.resolve(viewer.dataSources.add(dataSource)).catch(
() => null,
);
let destroyed = false;
const stroke = cesium.Color.fromCssColorString('#8be9ff');
const previewRectangle = () => {
const box = dragBox();
return box
? cesium.Rectangle.fromDegrees(box.west, box.south, box.east, box.north)
: undefined;
};
const previewOutline = () => {
const box = dragBox();
if (!box) return [];
return cesium.Cartesian3.fromDegreesArray([
box.west,
box.south,
box.east,
box.south,
box.east,
box.north,
box.west,
box.north,
box.west,
box.south,
]);
};
const fill = dataSource.entities.add({
show: false,
rectangle: {
coordinates: new cesium.CallbackProperty(previewRectangle, false),
material: stroke.withAlpha(0.12),
classificationType: cesium.ClassificationType.BOTH,
},
});
// A rectangle graphic cannot draw a dashed edge on the ground, so the
// outline is its own draped polyline traced round the same corners.
const outline = dataSource.entities.add({
show: false,
polyline: {
positions: new cesium.CallbackProperty(previewOutline, false),
width: 2,
clampToGround: true,
classificationType: cesium.ClassificationType.BOTH,
material: new cesium.PolylineDashMaterialProperty({
color: stroke.withAlpha(0.95),
dashLength: 12,
}),
},
});
const requestRender = () => viewer.scene.requestRender?.();
return {
setVisible(visible) {
fill.show = visible;
outline.show = visible;
requestRender();
},
/** The box moved: draw it again. */
changed: requestRender,
/** Detach the preview; resolves once it is gone. */
destroy() {
if (destroyed) return attaching;
destroyed = true;
dataSource.entities.removeAll();
attaching = attaching.then(() => {
try {
viewer.dataSources.remove(dataSource, true);
} catch {
/* viewer already disposed */
}
});
return attaching;
},
};
}