fix(trails): a double-click zooms to a trail line again

A trail was polyline entities, which the default renderer's double-click
zoomed to. As draw-port lines they took no part in it. Every trail on a
viewer shares one double-click handler, let go with the last trail.
This commit is contained in:
Sameh Khamis committed 2026-11-14 01:28:28 -08:00
1 parent 322e4a91d3
commit a9de7b3510
2 files changed
+120 -10

No files matched your search

+65 -10
View File
@@ -27,8 +27,9 @@
* changing settle into static geometry.
*/
import { cssColor } from '../render/color.js';
import { findDraw } from '../render/rendererOf.js';
import { findDraw, findInput } from '../render/rendererOf.js';
import { registerPickOwner } from './pickRegistry.js';
import { drawnItemTarget, followOnDoubleClick } from './stockFollow.js';
// Trail lines are pickable (a click on one must not fall through to the
// ground under it). A trail hugs its aircraft, so an unclaimed pick would
@@ -43,6 +44,48 @@ registerPickOwner('trails', (pickedId) =>
/** @type {number} Uniquifier for trail set and line ids. */
let _trailSeq = 0;
/**
* Per viewer, its trails' drawn lines by set id and the one double-click
* handler they share: a double-click on a trail line zooms to it, as the
* default renderer zoomed to the polyline entity a trail was drawn as.
* @type {WeakMap<object, {lines: Map<string, (id: string) => object|null>,
* follow: object|null}>}
*/
const doubleClicks = new WeakMap();
/**
* Take part in `viewer`'s trail double-click with the set `setId`, whose
* line by id `lineFor` gives. Returns how to leave it.
*/
function joinDoubleClick(viewer, setId, lineFor) {
let shared = doubleClicks.get(viewer);
if (!shared) {
const lines = new Map();
// A scene that takes no input has no double-click.
const input = findInput(viewer);
shared = {
lines,
follow: input
? followOnDoubleClick({
viewer,
inputFor: () => input,
name: 'trails',
enabled: () => true,
targetFor: (hit) =>
drawnItemTarget(hit.set, lines.get(hit.set)?.(hit.id)),
})
: null,
};
doubleClicks.set(viewer, shared);
}
shared.lines.set(setId, lineFor);
return () => {
if (!shared.lines.delete(setId) || shared.lines.size) return;
shared.follow?.destroy();
if (doubleClicks.get(viewer) === shared) doubleClicks.delete(viewer);
};
}
/** @constant {number} Alpha where the trail passes the depth test. */
const TRAIL_ALPHA = 0.85;
/** @constant {number} Alpha where the trail is behind/below scene geometry —
@@ -133,11 +176,21 @@ export function createTrail(viewer, { color, width = 2.5 }) {
* the position objects it was drawn from, which never change. */
let chunks = [];
let chunkSeq = 0;
/** Leaves the viewer's trail double-click, once the set is drawn. */
let leaveDoubleClick = null;
/** The line drawn under `id`, for a double-click to zoom to. */
function lineFor(id) {
const shown = drawn.get(id)?.item;
if (shown) return shown.show === false ? null : shown;
return chunks.find((chunk) => chunk.id === id)?.item ?? null;
}
function ensureSet() {
if (set || destroyed || !viewer || viewer.isDestroyed?.()) return set;
set =
findDraw(viewer)?.lines({ id: `trail:${seq}`, show: visible }) ?? null;
if (set) leaveDoubleClick = joinDoubleClick(viewer, set.id, lineFor);
return set;
}
@@ -223,16 +276,16 @@ export function createTrail(viewer, { color, width = 2.5 }) {
}
}
const positions = seg.slice(i, end + 1);
const chunk = { id: `${bodyId}:${++chunkSeq}`, positions };
next.push(chunk);
added.push(
lineItem(
chunk.id,
positions.map(plain),
TRAIL_ALPHA,
TRAIL_OCCLUDED_ALPHA,
),
const id = `${bodyId}:${++chunkSeq}`;
const item = lineItem(
id,
positions.map(plain),
TRAIL_ALPHA,
TRAIL_OCCLUDED_ALPHA,
);
const chunk = { id, positions, item };
next.push(chunk);
added.push(item);
i = end;
}
});
@@ -318,6 +371,8 @@ export function createTrail(viewer, { color, width = 2.5 }) {
/** Remove the trail permanently (layer disable/teardown). */
destroy() {
destroyed = true;
leaveDoubleClick?.();
leaveDoubleClick = null;
try {
set?.dispose();
} catch {
+55
View File
@@ -3,6 +3,8 @@ import assert from 'node:assert/strict';
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
import { CHUNK_POINTS, createTrail } from './trailRenderer.js';
import { findPickOwner } from './pickRegistry.js';
import { bindFakeGroundRenderer } from '../testSupport/fakeGroundRenderer.mjs';
import { fromDegrees } from '../geo/ellipsoid.js';
function viewerWithDraw(t) {
const viewer = { isDestroyed: () => false };
@@ -229,3 +231,56 @@ test('clearing a segmented trail lets every line go', (t) => {
trail.setSegments([points(4)]);
assert.equal(historyLines(set).length, 1, 'drawn again after a clear');
});
test('a double-click on a trail line zooms to that line, as to the polyline entity it was', (t) => {
const viewer = { isDestroyed: () => false };
const fake = bindFakeGroundRenderer(viewer);
t.after(fake.unbind);
let dblclick = null;
const stock = { dblclickFollow: true };
fake.renderer.input = {
onPointer(type, handler) {
assert.equal(type, 'dblclick');
dblclick = handler;
return () => {
dblclick = null;
};
},
stockEnabled: (name) => stock[name] === true,
};
const trail = createTrail(viewer, { color: '#00d4ff' });
const a = fromDegrees(0, 0, 1000);
const b = fromDegrees(0.1, 0, 1000);
trail.setPositions([a, b]);
const set = fake.liveSets().find((s) => s.spec.id.startsWith('trail:'));
const [line] = set.items.values();
fake.setPick(() => ({ set: set.spec.id, id: line.id }));
const at = { position: { x: 1, y: 2 } };
dblclick(at);
assert.equal(fake.flights.length, 1, 'zoomed at once');
const { sphere, options } = fake.flights[0];
assert.equal(options.durationS, 0);
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, z: (a.z + b.z) / 2 };
assert.ok(
Math.hypot(
sphere.center.x - mid.x,
sphere.center.y - mid.y,
sphere.center.z - mid.z,
) < 1,
);
assert.equal(fake.follows.length, 0, 'nothing followed');
// Held off (a tool holds the stock follow), a hidden trail line, or
// another layer's hit: nothing.
stock.dblclickFollow = false;
dblclick(at);
stock.dblclickFollow = true;
fake.setPick(() => ({ set: 'other', id: line.id }));
dblclick(at);
assert.equal(fake.flights.length, 1);
// The last trail gone, the viewer's double-click is let go.
trail.destroy();
assert.equal(dblclick, null);
});