diff --git a/src/scenes/dataPacks/presentation.js b/src/scenes/dataPacks/presentation.js index 89052a9b0..17e588493 100644 --- a/src/scenes/dataPacks/presentation.js +++ b/src/scenes/dataPacks/presentation.js @@ -1,6 +1,10 @@ import { packItemId, renderPackGeometry } from './geometry.js'; import { fromDegrees } from '../../geo/ellipsoid.js'; -import { drawOf, findFrame } from '../../render/rendererOf.js'; +import { drawOf, findFrame, findInput } from '../../render/rendererOf.js'; +import { + drawnItemTarget, + followOnDoubleClick, +} from '../../data/stockFollow.js'; import { useDataPacksStore } from '../../ui/stores/dataPacks.js'; import { applicationPinia } from '../../ui/vueRuntime.js'; @@ -66,6 +70,34 @@ export function createPackPresentations( cards = cards.filter((card) => card.key !== key); present(); } + // Each drawn point set's items by id: a double-click follows a pack's + // point, as the default renderer followed the point entity it was (its + // lines, polygons and images were primitives, which it never followed). + const pointSets = new Map(); + let stockFollow = null; + function followPoints(setId, items) { + pointSets.set(setId, new Map(items.map((item) => [item.id, item]))); + if (stockFollow) return; + // A scene that takes no input has no double-click. + const input = findInput(viewer); + if (!input) return; + stockFollow = followOnDoubleClick({ + viewer, + inputFor: () => input, + name: 'data-packs', + enabled: () => true, + targetFor: (hit) => + drawnItemTarget(hit.set, pointSets.get(hit.set)?.get(hit.id)), + }); + } + function unfollowPoints(setId) { + if (!pointSets.delete(setId)) return; + // Removing a followed entity ended the follow. + stockFollow?.retain((key) => !key.startsWith(`${setId}:`)); + if (pointSets.size) return; + stockFollow?.destroy(); + stockFollow = null; + } function resource(pack) { const sets = [], urls = [], @@ -89,6 +121,7 @@ export function createPackPresentations( const set = drawOf(viewer)[kind]({ id: `data-pack:${++drawSets}` }); sets.push(set); set.set(items); + if (kind === 'points') followPoints(set.id, items); requestRender(); return set.id; }, @@ -126,7 +159,10 @@ export function createPackPresentations( for (const key of featureKeys.splice(0)) targets.delete(key); // The view stops a removed card's player as it goes. if (card) removeCard(card); - for (const set of sets.splice(0)) set.dispose(); + for (const set of sets.splice(0)) { + unfollowPoints(set.id); + set.dispose(); + } if (labelSource) { labels.length = 0; overlayHost.clearSource(labelSource); diff --git a/src/scenes/dataPacks/presentation.test.mjs b/src/scenes/dataPacks/presentation.test.mjs index 4c3574eaf..35dad7401 100644 --- a/src/scenes/dataPacks/presentation.test.mjs +++ b/src/scenes/dataPacks/presentation.test.mjs @@ -3,6 +3,8 @@ import assert from 'node:assert/strict'; import { createPackPresentations } from './presentation.js'; import { packItemId } from './geometry.js'; import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; +import { bindFakeGroundRenderer } from '../../testSupport/fakeGroundRenderer.mjs'; +import { fromDegrees } from '../../geo/ellipsoid.js'; test('abort releases an in-flight image URL before the decoder settles or the viewer is destroyed', async () => { let settle; @@ -302,3 +304,80 @@ test('an image pack draws its decoded pixels as a rectangle at its height', asyn else globalThis.document = priorDocument; } }); + +test('a double-click follows a pack point, as the point entity it was, and never a line or area', async (t) => { + const viewer = { container: {} }; + const fake = bindFakeGroundRenderer(viewer); + t.after(fake.unbind); + let dblclick = null; + fake.renderer.input = { + onPointer(type, handler) { + assert.equal(type, 'dblclick'); + dblclick = handler; + return () => { + dblclick = null; + }; + }, + stockEnabled: (name) => name === 'dblclickFollow', + }; + const ring = [ + [2, 1], + [2.1, 1], + [2.1, 1.1], + [2, 1], + ]; + const bytes = new TextEncoder().encode( + JSON.stringify({ + type: 'FeatureCollection', + features: [ + { + type: 'Feature', + id: 'point', + geometry: { type: 'Point', coordinates: [2, 1, 10] }, + }, + { + type: 'Feature', + id: 'area', + geometry: { type: 'Polygon', coordinates: [ring] }, + }, + ], + }), + ); + const handle = await createPackPresentations(viewer, new Map(), { + store: { publish() {} }, + }).geojson({ + pack: { id: 'data', attribution: { text: 'Example', license: 'CC0' } }, + asset: { bytes, mimeType: 'application/geo+json' }, + signal: new AbortController().signal, + }); + const points = fake.liveSets().find((record) => record.kind === 'points'); + const shapes = fake.liveSets().find((record) => record.kind === 'shapes'); + const at = { position: { x: 1, y: 2 } }; + + fake.setPick(() => ({ set: shapes.spec.id, id: packItemId('data', 'area') })); + dblclick(at); + assert.equal( + fake.follows.length + fake.flights.length, + 0, + 'an area: nothing', + ); + + fake.setPick(() => ({ + set: points.spec.id, + id: packItemId('data', 'point'), + })); + dblclick(at); + assert.equal(fake.follows.length, 1); + const followed = fake.follows[0]; + const expected = fromDegrees(2, 1, 10); + const got = followed.position(); + assert.ok( + Math.hypot(got.x - expected.x, got.y - expected.y, got.z - expected.z) < + 1e-6, + ); + + // Removing the pack ends the follow and lets the double-click go. + handle.dispose(); + assert.equal(fake.renderer.camera.followed, null); + assert.equal(dblclick, null); +});