mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Draw the Draw tool's live preview through the draw port and take the tool off the Cesium list
The preview of a shape being drawn is now two sets on the renderer's draw port instead of a Cesium data source: one dashed rubber band along the globe (the chosen colour at 0.9, 3 px, dash 16, primary at 0.35 behind the surface) and a dot per vertex (8 px, black outline at 0.6, drawn over depth). The band is written on each pointer move and vertex change rather than re-read every frame. A pointer move now carries `pickPosition`, its point in the pick buffer corrected for sensor styles as a click's position is, which the rubber band follows; the tool reads its canvas from the renderer and asks frames through the frame port. The diagnostics count the preview sets live on the viewer (`previewSets`, `previewItems`) in place of preview data sources. One difference: where the band runs behind the surface it is drawn in a solid colour, as the draw contract's depth-fail colour is a colour, not a dashed material.
This commit is contained in:
1 parent
c49191a87b
commit
3cce1aaa3e
7 files changed
+198
-167
No files matched your search
+100
-101
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Manual whiteboard drawing: the Cesium + DOM half.
|
||||
* Manual whiteboard drawing: the scene + DOM half.
|
||||
*
|
||||
* UTILITIES ▸ Draw turns the globe into a whiteboard you draw on by hand: pick a
|
||||
* shape (area, line or pin) and a colour, click the vertices on the real world,
|
||||
@@ -20,7 +20,7 @@
|
||||
* pointer) and double click (track it) on the viewer's handler, outside every
|
||||
* layer and therefore outside the shared claim. Both are suppressed for the
|
||||
* session and given back on the way out, and `destroy()` gives back every
|
||||
* listener, the pointer subscriptions, the preview data source and the window
|
||||
* listener, the pointer subscriptions, the preview draw sets and the window
|
||||
* handle, so the tool can be turned off or the shell disposed without leaving
|
||||
* anything behind.
|
||||
*
|
||||
@@ -30,7 +30,6 @@
|
||||
* drapes finished areas and routes onto the surface, so the height is dropped
|
||||
* at finish, deliberately and in one place — see `finishSpec` in drawMode.js.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { pickWorldFromScreen } from './annotationResolver.js';
|
||||
import {
|
||||
claimPointer,
|
||||
@@ -38,7 +37,6 @@ import {
|
||||
releasePointer,
|
||||
} from '../data/inputOwnership.js';
|
||||
import { inputFor } from '../input/dispatcher.js';
|
||||
import { sensorPickPosition } from '../input/sensorPick.js';
|
||||
import { pointerOwnerName } from '../data/pointerOwnerNames.js';
|
||||
import {
|
||||
DRAW_SHAPES,
|
||||
@@ -55,9 +53,20 @@ import {
|
||||
import { DRAW_COLORS, DRAW_COLOR_CSS, presentDrawMarks } from './drawMarks.js';
|
||||
|
||||
import { onLocaleChange, t, translateKnown } from '../i18n/index.js';
|
||||
import { fromDegrees } from '../geo/index.js';
|
||||
import { cssColor } from '../render/color.js';
|
||||
import { drawOf, findFrame, rendererOf } from '../render/rendererOf.js';
|
||||
/** The id this tool claims the pointer under. */
|
||||
const DRAW_POINTER_OWNER = 'draw';
|
||||
const PREVIEW_DATA_SOURCE_NAME = 'gev-draw-preview';
|
||||
/** Each tool's preview sets get their own ids. */
|
||||
let previewSerial = 0;
|
||||
/**
|
||||
* Preview sets drawn on each viewer by its live tools, so a tool left
|
||||
* undestroyed beside its replacement shows up in `diagnostics()`.
|
||||
*/
|
||||
const livePreviewSets = new WeakMap();
|
||||
const countPreviewSets = (viewer, delta) =>
|
||||
livePreviewSets.set(viewer, (livePreviewSets.get(viewer) ?? 0) + delta);
|
||||
|
||||
const PREVIEW = DRAW_COLOR_CSS;
|
||||
|
||||
@@ -69,7 +78,7 @@ const PREVIEW = DRAW_COLOR_CSS;
|
||||
* through the actions it gives `connect`, which returns their disconnect.
|
||||
* A row's fly-to is an immediate navigation through `navigation`, like
|
||||
* Import's, so Cockpit can refuse it and a running orbit or follow lets go.
|
||||
* @param {{viewer: Cesium.Viewer, annotations: {annotate: Function, clear: Function}, navigation?: object, publish?: Function, connect?: Function}} deps
|
||||
* @param {{viewer: object, annotations: {annotate: Function, clear: Function}, navigation?: object, publish?: Function, connect?: Function}} deps
|
||||
* @returns {{destroy: Function}|null}
|
||||
*/
|
||||
export function initDrawTool({
|
||||
@@ -97,7 +106,6 @@ export function initDrawTool({
|
||||
// the board, leaving draw mode, teardown. An `annotate()` that resolves after
|
||||
// one of those must not write its outcome over the newer state.
|
||||
let generation = 0;
|
||||
const previewEntities = [];
|
||||
// Every DOM listener this tool adds (the session's keydown), so teardown is
|
||||
// one loop rather than a list that can fall out of step with the bindings.
|
||||
const domListeners = [];
|
||||
@@ -106,14 +114,26 @@ export function initDrawTool({
|
||||
domListeners.push([target, type, listener, options]);
|
||||
};
|
||||
|
||||
const dataSource = new Cesium.CustomDataSource(PREVIEW_DATA_SOURCE_NAME);
|
||||
// `add()` resolves on the NEXT tick, so an init-then-immediate-destroy would
|
||||
// remove a source that had not been attached yet and leave the attachment to
|
||||
// land afterwards — a preview data source nobody owns. Chain the removal onto
|
||||
// the attachment instead of racing it.
|
||||
let attaching = Promise.resolve(viewer.dataSources.add(dataSource)).catch(
|
||||
() => null,
|
||||
);
|
||||
// The preview: one dashed rubber band and a dot per vertex, drawn through
|
||||
// the renderer's draw port. Both sets live as long as the tool.
|
||||
const draw = drawOf(viewer);
|
||||
const previewId = `draw-preview:${++previewSerial}`;
|
||||
const previewLines = draw.lines({ id: `${previewId}:line` });
|
||||
const previewPoints = draw.points({ id: `${previewId}:vertices` });
|
||||
countPreviewSets(viewer, 2);
|
||||
let previewReleased = false;
|
||||
const releasePreview = () => {
|
||||
if (previewReleased) return;
|
||||
previewReleased = true;
|
||||
countPreviewSets(viewer, -2);
|
||||
previewLines.dispose();
|
||||
previewPoints.dispose();
|
||||
};
|
||||
/** The canvas the renderer draws in (the viewer's own when none is bound). */
|
||||
const canvasOf = () => rendererOf(viewer)?.canvas ?? viewer.canvas;
|
||||
const requestRender = () => findFrame(viewer)?.requestRender('draw-preview');
|
||||
// Teardown finishes synchronously; `whenSettled()` keeps its promise.
|
||||
const attaching = Promise.resolve();
|
||||
|
||||
const present = () =>
|
||||
publish({
|
||||
@@ -137,70 +157,66 @@ export function initDrawTool({
|
||||
|
||||
// ---- preview ---------------------------------------------------------
|
||||
const vertexPositions = () =>
|
||||
session.vertices.map((v) =>
|
||||
Cesium.Cartesian3.fromDegrees(v.lon, v.lat, v.height || 0),
|
||||
);
|
||||
const previewLine = dataSource.entities.add({
|
||||
show: false,
|
||||
polyline: {
|
||||
positions: new Cesium.CallbackProperty(() => {
|
||||
if (!session) return [];
|
||||
const pts = vertexPositions();
|
||||
if (cursor && session.shape !== 'pin') pts.push(cursor);
|
||||
if (session.shape === 'area' && pts.length >= 3) pts.push(pts[0]);
|
||||
return pts;
|
||||
}, false),
|
||||
width: 3,
|
||||
material: new Cesium.PolylineDashMaterialProperty({
|
||||
color: Cesium.Color.fromCssColorString(PREVIEW.primary).withAlpha(0.9),
|
||||
dashLength: 16,
|
||||
}),
|
||||
depthFailMaterial: new Cesium.PolylineDashMaterialProperty({
|
||||
color: Cesium.Color.fromCssColorString(PREVIEW.primary).withAlpha(0.35),
|
||||
dashLength: 16,
|
||||
}),
|
||||
clampToGround: false,
|
||||
},
|
||||
});
|
||||
const syncPreview = () => {
|
||||
session.vertices.map((v) => fromDegrees(v.lon, v.lat, v.height || 0));
|
||||
/** The rubber band, from the vertices to the pointer (and closed for an area). */
|
||||
const syncPreviewLine = () => {
|
||||
if (destroyed) return;
|
||||
previewEntities.forEach((e) => dataSource.entities.remove(e));
|
||||
previewEntities.length = 0;
|
||||
if (!session) {
|
||||
previewLine.show = false;
|
||||
setHint(() => drawHint(null));
|
||||
viewer.scene.requestRender();
|
||||
if (!session || session.shape === 'pin') {
|
||||
previewLines.clear();
|
||||
return;
|
||||
}
|
||||
const stroke = Cesium.Color.fromCssColorString(
|
||||
PREVIEW[color] || PREVIEW.primary,
|
||||
);
|
||||
previewLine.polyline.material = new Cesium.PolylineDashMaterialProperty({
|
||||
color: stroke.withAlpha(0.9),
|
||||
dashLength: 16,
|
||||
});
|
||||
previewLine.show = session.shape !== 'pin';
|
||||
for (const v of session.vertices) {
|
||||
previewEntities.push(
|
||||
dataSource.entities.add({
|
||||
position: Cesium.Cartesian3.fromDegrees(v.lon, v.lat, v.height || 0),
|
||||
point: {
|
||||
pixelSize: 8,
|
||||
color: stroke,
|
||||
outlineColor: Cesium.Color.BLACK.withAlpha(0.6),
|
||||
outlineWidth: 2,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const pts = vertexPositions();
|
||||
if (cursor) pts.push(cursor);
|
||||
if (session.shape === 'area' && pts.length >= 3) pts.push(pts[0]);
|
||||
if (pts.length < 2) {
|
||||
previewLines.clear();
|
||||
return;
|
||||
}
|
||||
previewLines.upsert([
|
||||
{
|
||||
id: 'band',
|
||||
positions: pts,
|
||||
widthPx: 3,
|
||||
// Along the ellipsoid between vertices, as the entity polyline was.
|
||||
mode: 'geodesic',
|
||||
style: {
|
||||
kind: 'dash',
|
||||
color: withAlpha(cssColor(PREVIEW[color] || PREVIEW.primary), 0.9),
|
||||
length: 16,
|
||||
},
|
||||
depthFailColor: withAlpha(cssColor(PREVIEW.primary), 0.35),
|
||||
},
|
||||
]);
|
||||
};
|
||||
const syncPreview = () => {
|
||||
if (destroyed) return;
|
||||
if (!session) {
|
||||
previewPoints.clear();
|
||||
previewLines.clear();
|
||||
setHint(() => drawHint(null));
|
||||
requestRender();
|
||||
return;
|
||||
}
|
||||
const stroke = cssColor(PREVIEW[color] || PREVIEW.primary);
|
||||
previewPoints.set(
|
||||
session.vertices.map((v, index) => ({
|
||||
id: String(index),
|
||||
pos: { lon: v.lon, lat: v.lat, height: v.height || 0 },
|
||||
pixelSize: 8,
|
||||
color: stroke,
|
||||
outlineColor: [0, 0, 0, 0.6],
|
||||
outlineWidth: 2,
|
||||
disableDepthTest: Number.POSITIVE_INFINITY,
|
||||
})),
|
||||
);
|
||||
syncPreviewLine();
|
||||
setHint(() => drawHint(session));
|
||||
viewer.scene.requestRender();
|
||||
requestRender();
|
||||
};
|
||||
|
||||
// ---- vertices from clicks --------------------------------------------
|
||||
const worldAt = (position) => {
|
||||
const canvas = viewer.scene.canvas;
|
||||
const canvas = canvasOf();
|
||||
const w = canvas.clientWidth || canvas.width || 1;
|
||||
const h = canvas.clientHeight || canvas.height || 1;
|
||||
return pickWorldFromScreen(viewer, position.x / w, position.y / h);
|
||||
@@ -230,11 +246,10 @@ export function initDrawTool({
|
||||
if (!session || session.shape === 'pin' || destroyed) return;
|
||||
// The click's own position is sensor-corrected; the rubber band must
|
||||
// follow the same point the click would place.
|
||||
const p = worldAt(sensorPickPosition(viewer.scene, event.endPosition));
|
||||
cursor = p
|
||||
? Cesium.Cartesian3.fromDegrees(p.lon, p.lat, p.height || 0)
|
||||
: null;
|
||||
viewer.scene.requestRender();
|
||||
const p = worldAt(event.pickPosition ?? event.endPosition);
|
||||
cursor = p ? fromDegrees(p.lon, p.lat, p.height || 0) : null;
|
||||
syncPreviewLine();
|
||||
requestRender();
|
||||
};
|
||||
|
||||
// ---- finish / cancel -------------------------------------------------
|
||||
@@ -314,9 +329,7 @@ export function initDrawTool({
|
||||
t.closest('button, a, select, [role="button"], [role="radio"]')
|
||||
)
|
||||
return false;
|
||||
return (
|
||||
t === document.body || t === viewer.scene.canvas || t.tagName === 'CANVAS'
|
||||
);
|
||||
return t === document.body || t === canvasOf() || t.tagName === 'CANVAS';
|
||||
};
|
||||
const onKey = (event) => {
|
||||
if (!active || destroyed || typingElsewhere(event)) return;
|
||||
@@ -506,8 +519,11 @@ export function initDrawTool({
|
||||
destroyed,
|
||||
sceneHandler: Boolean(unbindScene),
|
||||
domListeners: domListeners.length,
|
||||
previewDataSources: countPreviewDataSources(viewer),
|
||||
previewEntities: destroyed ? 0 : dataSource.entities.values.length,
|
||||
// Every live tool's on this viewer: 2 while one tool lives.
|
||||
previewSets: livePreviewSets.get(viewer) ?? 0,
|
||||
previewItems: previewReleased
|
||||
? 0
|
||||
: previewLines.size() + previewPoints.size(),
|
||||
pointerOwner: pointerOwner(),
|
||||
// The viewer's own select and follow, which the input port runs: off
|
||||
// while a session is open, back on after it closes.
|
||||
@@ -534,18 +550,7 @@ export function initDrawTool({
|
||||
for (const [target, type, listener, options] of domListeners.splice(0)) {
|
||||
target.removeEventListener(type, listener, options);
|
||||
}
|
||||
previewEntities.length = 0;
|
||||
dataSource.entities.removeAll();
|
||||
// Wait for the pending add() before removing: a destroy() in the same
|
||||
// tick as init would otherwise remove nothing and let the attachment land
|
||||
// behind it.
|
||||
attaching = attaching.then(() => {
|
||||
try {
|
||||
viewer.dataSources.remove(dataSource, true);
|
||||
} catch {
|
||||
/* viewer already disposed — nothing to detach from */
|
||||
}
|
||||
});
|
||||
releasePreview();
|
||||
document.body.classList.remove('gev-drawing');
|
||||
hint = '';
|
||||
present();
|
||||
@@ -557,13 +562,7 @@ export function initDrawTool({
|
||||
return api;
|
||||
}
|
||||
|
||||
/** How many draw previews are attached — more than one means a leaked tool. */
|
||||
function countPreviewDataSources(viewer) {
|
||||
const sources = viewer?.dataSources;
|
||||
if (!sources) return 0;
|
||||
let found = 0;
|
||||
for (let index = 0; index < sources.length; index += 1) {
|
||||
if (sources.get(index)?.name === PREVIEW_DATA_SOURCE_NAME) found += 1;
|
||||
}
|
||||
return found;
|
||||
/** `color` at `alpha`. */
|
||||
function withAlpha(color, alpha) {
|
||||
return [color[0], color[1], color[2], alpha];
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
resetPointerOwnership,
|
||||
} from '../data/inputOwnership.js';
|
||||
import { fakeInput } from '../input/testDoubles.mjs';
|
||||
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
|
||||
|
||||
/* ── DOM doubles ───────────────────────────────────────────────────────── */
|
||||
|
||||
@@ -144,33 +145,9 @@ function fakeViewer() {
|
||||
setInputAction: (action, type) => actions.set(type, action),
|
||||
removeInputAction: (type) => actions.delete(type),
|
||||
};
|
||||
const attached = [];
|
||||
const pendingAdds = [];
|
||||
const dataSources = {
|
||||
get length() {
|
||||
return attached.length;
|
||||
},
|
||||
get: (index) => attached[index],
|
||||
add(source) {
|
||||
// Cesium resolves this on a later tick — the whole point of finding 7.
|
||||
const promise = Promise.resolve().then(() => {
|
||||
attached.push(source);
|
||||
return source;
|
||||
});
|
||||
pendingAdds.push(promise);
|
||||
return promise;
|
||||
},
|
||||
remove(source) {
|
||||
const at = attached.indexOf(source);
|
||||
if (at >= 0) attached.splice(at, 1);
|
||||
return at >= 0;
|
||||
},
|
||||
};
|
||||
return {
|
||||
scene: { canvas, requestRender() {} },
|
||||
screenSpaceEventHandler: stockHandler,
|
||||
dataSources,
|
||||
_attached: attached,
|
||||
_actions: actions,
|
||||
};
|
||||
}
|
||||
@@ -231,6 +208,10 @@ function harness({ navigation = null, beforeInput = () => {} } = {}) {
|
||||
// The viewer's own actions are bound before its input port takes them.
|
||||
beforeInput(viewer);
|
||||
const input = fakeInput(viewer);
|
||||
// The renderer the tool draws its preview with, drawing in the viewer's canvas.
|
||||
const { draw, unbind: unbindDraw } = bindFakeDraw(viewer, {
|
||||
canvas: viewer.scene.canvas,
|
||||
});
|
||||
const annotations = fakeAnnotations();
|
||||
const previousDocument = globalThis.document;
|
||||
const previousWindow = globalThis.window;
|
||||
@@ -253,6 +234,7 @@ function harness({ navigation = null, beforeInput = () => {} } = {}) {
|
||||
tool,
|
||||
shown,
|
||||
viewer,
|
||||
draw,
|
||||
annotations,
|
||||
byId,
|
||||
documentDouble,
|
||||
@@ -263,6 +245,7 @@ function harness({ navigation = null, beforeInput = () => {} } = {}) {
|
||||
input,
|
||||
restore() {
|
||||
input.destroy();
|
||||
unbindDraw();
|
||||
resetPointerOwnership();
|
||||
if (previousDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = previousDocument;
|
||||
@@ -416,15 +399,16 @@ test('the live preview hangs at the height the click landed on', async () => {
|
||||
h.tool.addVertex(-97.75, 30.26, 187.5);
|
||||
h.tool.addVertex(-97.74, 30.26, 212.25);
|
||||
|
||||
await h.tool.whenSettled();
|
||||
const source = h.viewer._attached[0] ?? null;
|
||||
assert.ok(source, 'the preview data source is attached');
|
||||
const points = source.entities.values.filter((entity) => entity.point);
|
||||
assert.equal(points.length, 2, 'one preview dot per vertex');
|
||||
const heights = points.map((entity) => {
|
||||
const position = entity.position.getValue(Cesium.JulianDate.now());
|
||||
return Cesium.Cartographic.fromCartesian(position).height;
|
||||
});
|
||||
const dots = [...h.draw.sets.values()].find(
|
||||
(set) => !set.disposed && set.kind === 'points',
|
||||
);
|
||||
assert.equal(dots.items.size, 2, 'one preview dot per vertex');
|
||||
const heights = [...dots.items.values()].map((item) => item.pos.height);
|
||||
const band = [...h.draw.sets.values()]
|
||||
.find((set) => !set.disposed && set.kind === 'lines')
|
||||
.items.get('band');
|
||||
assert.equal(band.positions.length, 2, 'the rubber band joins the vertices');
|
||||
assert.equal(band.mode, 'geodesic', 'it follows the globe, not draped');
|
||||
assert.ok(Math.abs(heights[0] - 187.5) < 0.5, `got ${heights[0]}`);
|
||||
assert.ok(Math.abs(heights[1] - 212.25) < 0.5, `got ${heights[1]}`);
|
||||
|
||||
@@ -443,18 +427,46 @@ test('the live preview hangs at the height the click landed on', async () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('destroy in the same tick as init still removes the preview data source', async () => {
|
||||
test('the rubber band follows the pointer, closes an area and takes the chosen colour', async () => {
|
||||
const h = harness();
|
||||
try {
|
||||
// Nothing has been attached yet — dataSources.add() resolves later.
|
||||
assert.equal(h.viewer._attached.length, 0, 'the add is still pending');
|
||||
await h.tool.destroy();
|
||||
await Promise.resolve();
|
||||
assert.equal(
|
||||
h.viewer._attached.length,
|
||||
0,
|
||||
'the preview must not be left attached behind the teardown',
|
||||
const sets = () => [...h.draw.sets.values()].filter((set) => !set.disposed);
|
||||
const band = () => sets().find((set) => set.kind === 'lines').items.get('band');
|
||||
h.act('toggleDraw');
|
||||
h.tool.setShape('area');
|
||||
h.tool.addVertex(-97.75, 30.26);
|
||||
assert.equal(band(), undefined, 'one vertex and no pointer draw no band');
|
||||
h.tool.addVertex(-97.74, 30.26);
|
||||
h.tool.addVertex(-97.74, 30.27);
|
||||
assert.equal(band().positions.length, 4, 'an area of three closes back to its first vertex');
|
||||
assert.deepEqual(band().positions[3], band().positions[0]);
|
||||
|
||||
h.tool.setColor('red');
|
||||
assert.deepEqual(band().style.color, [1, 107 / 255, 107 / 255, 0.9], 'the band takes the colour');
|
||||
assert.deepEqual(
|
||||
band().depthFailColor,
|
||||
[139 / 255, 233 / 255, 1, 0.35],
|
||||
'behind the surface it stays the primary colour, as before',
|
||||
);
|
||||
assert.equal(band().style.kind, 'dash');
|
||||
assert.equal(band().style.length, 16);
|
||||
assert.equal(band().widthPx, 3);
|
||||
|
||||
h.tool.setShape('pin');
|
||||
assert.equal(band(), undefined, 'a pin has no rubber band');
|
||||
} finally {
|
||||
h.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('destroy in the same tick as init disposes the preview sets', async () => {
|
||||
const h = harness();
|
||||
try {
|
||||
const live = () => [...h.draw.sets.values()].filter((set) => !set.disposed);
|
||||
assert.equal(live().length, 2, 'the rubber band and the vertex dots');
|
||||
await h.tool.destroy();
|
||||
assert.equal(live().length, 0, 'nothing is left drawn behind the teardown');
|
||||
assert.equal(h.tool.diagnostics().previewSets, 0, 'no live tool draws a preview');
|
||||
} finally {
|
||||
h.restore();
|
||||
}
|
||||
@@ -486,7 +498,7 @@ test('destroy releases the pointer, the handler, the listeners and the stock act
|
||||
stockClick,
|
||||
'the viewer got its selecting click back',
|
||||
);
|
||||
assert.equal(h.viewer._attached.length, 0, 'the preview data source is detached');
|
||||
assert.equal(after.previewSets, 0, 'the preview sets are disposed');
|
||||
assert.equal(h.annotations.listeners.size, 0, 'and it no longer follows the board');
|
||||
|
||||
// Idempotent: a second destroy is harmless.
|
||||
|
||||
Reference in new issue
Block a user