From 26e8e26ba84d6699ee0c6b5f0aaf245350e215f0 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 14 Nov 2026 01:15:29 -0800 Subject: [PATCH] fix(ghostmaps): a double-click follows a pin or zooms to a line or area again Each edition was drawn as entities, which the default renderer's double-click followed (a pin) or zoomed to (a line or polygon); an expired report or a replaced edition took the follow with it. drawnItemTarget reads that rule off a draw item, for every layer that drew entities before. --- src/data/stockFollow.js | 55 ++++++++++++++++++++++ src/data/stockFollow.test.mjs | 49 ++++++++++++++++++++ src/layers/ghostMaps/index.js | 61 +++++++++++++++++++++++-- src/layers/ghostMaps/lifecycle.test.mjs | 43 +++++++++++++++-- 4 files changed, 202 insertions(+), 6 deletions(-) diff --git a/src/data/stockFollow.js b/src/data/stockFollow.js index 30f48551a..42e1c1df8 100644 --- a/src/data/stockFollow.js +++ b/src/data/stockFollow.js @@ -10,6 +10,61 @@ import { followOffsetFitting } from '../geo/frames.js'; import { findCamera, findQuery } from '../render/rendererOf.js'; import { entityFollowTarget } from './followed.js'; +import { positionToEcef } from '../geo/ellipsoid.js'; +import { sphereFromPoints } from '../geo/sphere.js'; + +/** A line's or shape's positions as ECEF points. */ +function pointsOf(positions) { + if (!positions) return []; + if (ArrayBuffer.isView(positions)) { + const out = []; + for (let i = 0; i + 2 < positions.length; i += 3) + out.push({ x: positions[i], y: positions[i + 1], z: positions[i + 2] }); + return out; + } + return Array.from(positions, positionToEcef); +} + +/** + * What a double-click on a drawn item does, as the default renderer did + * for the entity a layer drew it as before: a point, an icon or a disc + * (an entity with a position) is followed, framed to fit the disc; a line + * or another shape (an entity without one) is zoomed to. For a layer's + * `targetFor`, given the hit's set id and the item drawn there. + * @param {string} set The hit's set id. + * @param {object|null|undefined} item The draw item (draw.d.ts) the hit names. + */ +export function drawnItemTarget(set, item) { + if (!item) return null; + const key = `${set}:${item.id}`; + if (item.pos) { + const at = positionToEcef(item.pos); + return { key, position: () => ({ ...at }) }; + } + const geometry = item.geometry; + if (geometry?.kind === 'ellipse') { + const at = positionToEcef(geometry.center); + const radius = Math.max(geometry.semiMajor, geometry.semiMinor); + return { + key, + position: () => ({ ...at }), + sphere: { center: at, radius: Number.isFinite(radius) ? radius : 0 }, + }; + } + let points = pointsOf(item.positions); + if (geometry?.kind === 'polygon') points = pointsOf(geometry.rings?.[0]); + else if (geometry?.kind === 'wall') points = pointsOf(geometry.positions); + else if (geometry?.kind === 'rectangle') { + const { west, south, east, north } = geometry.rect; + points = pointsOf([ + { lon: west, lat: south }, + { lon: east, lat: south }, + { lon: east, lat: north }, + { lon: west, lat: north }, + ]); + } + return points.length ? { zoomTo: sphereFromPoints(points) } : null; +} /** * Where the camera frames a part it starts following: `followOffsetFitting` diff --git a/src/data/stockFollow.test.mjs b/src/data/stockFollow.test.mjs index ccc1f986f..fcfdf272a 100644 --- a/src/data/stockFollow.test.mjs +++ b/src/data/stockFollow.test.mjs @@ -117,3 +117,52 @@ test('the frame view fits a sphere and steepens far from the ground', () => { assert.ok(Math.abs(elevation - (Math.PI / 4) * 1.5) < 1e-9); assert.ok(far.y < 0); }); + +test('a drawn item is followed as an entity with a position was, or zoomed to as one without', async () => { + const { drawnItemTarget } = await import('./stockFollow.js'); + const near = (a, b) => Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z) < 1e-6; + const { fromDegrees } = await import('../geo/ellipsoid.js'); + const point = drawnItemTarget('s', { id: 'p', pos: { lon: 10, lat: 20 } }); + assert.equal(point.key, 's:p'); + assert.ok(near(point.position(), fromDegrees(10, 20, 0))); + const disc = drawnItemTarget('s', { + id: 'd', + geometry: { + kind: 'ellipse', + center: { lon: 10, lat: 20 }, + semiMajor: 4000, + semiMinor: 3000, + }, + }); + assert.equal(disc.sphere.radius, 4000); + assert.ok(near(disc.position(), fromDegrees(10, 20, 0))); + const flat = new Float64Array([ + ...Object.values(fromDegrees(0, 0, 0)), + ...Object.values(fromDegrees(2, 0, 0)), + ]); + const line = drawnItemTarget('s', { id: 'l', positions: flat }); + assert.ok(line.zoomTo.radius > 100_000 && !line.position); + const polygon = drawnItemTarget('s', { + id: 'g', + geometry: { + kind: 'polygon', + rings: [ + [ + { lon: 0, lat: 0 }, + { lon: 1, lat: 0 }, + { lon: 1, lat: 1 }, + ], + ], + }, + }); + assert.ok(polygon.zoomTo.radius > 50_000); + const rect = drawnItemTarget('s', { + id: 'r', + geometry: { + kind: 'rectangle', + rect: { west: 0, south: 0, east: 1, north: 1 }, + }, + }); + assert.ok(rect.zoomTo.radius > 50_000); + assert.equal(drawnItemTarget('s', null), null); +}); diff --git a/src/layers/ghostMaps/index.js b/src/layers/ghostMaps/index.js index 933262241..fc45c3370 100644 --- a/src/layers/ghostMaps/index.js +++ b/src/layers/ghostMaps/index.js @@ -16,7 +16,11 @@ import { import { createHorizonCull } from '../streetLevel/view.js'; import { fromDegrees } from '../../geo/ellipsoid.js'; import { hexColor } from '../../render/color.js'; -import { drawOf, findFrame } from '../../render/rendererOf.js'; +import { drawOf, findFrame, findInput } from '../../render/rendererOf.js'; +import { + drawnItemTarget, + followOnDoubleClick, +} from '../../data/stockFollow.js'; import { readGhostMapsKml } from './kml.js'; import { fieldLabel, @@ -197,6 +201,8 @@ export function createGhostMapsLayer({ let _stream = 'closed'; let _queue = Promise.resolve(); let _clickSubscription = null; + /** The follow a double-click on a placemark started, while installed. */ + let _stockFollow = null; let _rowListener = null; let _selectedKey = null; let _selectedCardId = null; @@ -265,7 +271,10 @@ export function createGhostMapsLayer({ for (const [index, feature] of item.features) { if (!isGhostMapsEventExpired(feature.dateMs, at, _ageDays)) continue; for (const [kind, ids] of Object.entries(feature.itemIds)) - if (ids.length) item.sets?.[kind]?.remove(ids); + if (ids.length) { + dropFollowOf(item.sets?.[kind], ids); + item.sets?.[kind]?.remove(ids); + } item.features.delete(index); if (_selectedKey === `${productId}:${index}`) clearSelection(); changed = true; @@ -278,9 +287,22 @@ export function createGhostMapsLayer({ } } + /** + * End a double-click's follow of an item that goes (any of `ids`, or the + * whole set), as removing its entity ended the default renderer's. + */ + function dropFollowOf(set, ids = null) { + const key = _stockFollow?.following?.key; + if (!set || !key?.startsWith(`${set.id}:`)) return; + if (!ids || ids.includes(key.slice(set.id.length + 1))) _stockFollow.stop(); + } + function disposeSets(sets) { if (!sets) return; - for (const set of Object.values(sets)) set.dispose(); + for (const set of Object.values(sets)) { + dropFollowOf(set); + set.dispose(); + } } /** @@ -307,6 +329,13 @@ export function createGhostMapsLayer({ } for (const pin of next.pins) pin.set = sets.icons; next.sets = sets; + // Each set's items by id, for a double-click to follow or zoom to. + next.drawn = new Map( + Object.entries(sets).map(([kind, set]) => [ + set.id, + new Map(next.items[kind].map((item) => [item.id, item])), + ]), + ); } /** The placemark a hit on one of this layer's items is, or null. */ @@ -736,7 +765,32 @@ export function createGhostMapsLayer({ }); } + /** + * A double-click follows a pin and zooms to a line or an area, as the + * default renderer did for their entities (a scene that takes no input + * has none). + */ + function installFollow() { + if (_stockFollow || !_viewer || !inputFor || !findInput(_viewer)) return; + _stockFollow = followOnDoubleClick({ + viewer: _viewer, + inputFor, + name: GHOSTMAPS_LAYER_ID, + enabled: () => _enabled, + targetFor: (hit) => { + const key = hitKey(hit); + const loaded = key && _loaded.get(key.split(':')[0]); + return drawnItemTarget( + hit.set, + loaded?.drawn?.get(hit.set)?.get(hit.id), + ); + }, + }); + } + function removeClickHandler() { + _stockFollow?.destroy(); + _stockFollow = null; _clickSubscription?.(); _clickSubscription = null; picking?.unregisterPickOwner?.(GHOSTMAPS_LAYER_ID); @@ -770,6 +824,7 @@ export function createGhostMapsLayer({ for (const item of _loaded.values()) for (const set of Object.values(item.sets ?? {})) set.show = true; installClickHandler(); + installFollow(); if (!_unsubscribe && typeof source.subscribe === 'function') _unsubscribe = source.subscribe( (catalog) => { diff --git a/src/layers/ghostMaps/lifecycle.test.mjs b/src/layers/ghostMaps/lifecycle.test.mjs index 7bba31645..8acb2ff14 100644 --- a/src/layers/ghostMaps/lifecycle.test.mjs +++ b/src/layers/ghostMaps/lifecycle.test.mjs @@ -2,6 +2,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { createGhostMapsLayer } from './index.js'; import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; +import { fakeCameraPort } from '../../testSupport/fakeCameraPort.mjs'; import { GHOSTMAPS_PRODUCTS, GHOSTMAPS_LATEST_DROP } from './catalog.js'; function element(localName, children = [], value = '', attributes = {}) { @@ -121,13 +122,26 @@ function fixture( const viewer = {}; // An input port and a query port: a click picks what `click(id)` names. const listeners = new Set(); + const doubleListeners = new Set(); let picked = null; + const camera = fakeCameraPort(); + // What the camera follows, as the port's `follow` sets it. + camera.port.followed = null; + camera.port.follow = (target) => (camera.port.followed = target ?? null); const { draw, unbind } = bindFakeDraw(viewer, { + camera: camera.port, input: { + behaviours: { dblclickFollow: { enabled: true } }, on(type, cb) { - if (type !== 'click') return () => {}; - listeners.add(cb); - return () => listeners.delete(cb); + const into = + type === 'click' + ? listeners + : type === 'dblclick' + ? doubleListeners + : null; + if (!into) return () => {}; + into.add(cb); + return () => into.delete(cb); }, }, query: { pick: () => picked }, @@ -151,6 +165,17 @@ function fixture( modifiers: { shift: false, ctrl: false, alt: false }, }); }; + const dblclick = (id) => { + const set = items().get(id); + picked = set ? { set, id, surface: false } : null; + for (const cb of [...doubleListeners]) + cb({ + type: 'dblclick', + position: { x: 0, y: 0 }, + button: 0, + modifiers: { shift: false, ctrl: false, alt: false }, + }); + }; // The editions drawn: each draws in sets of its own. const mounted = { get size() { @@ -191,6 +216,8 @@ function fixture( items, liveSets, click, + dblclick, + camera: camera.port, }; } @@ -642,3 +669,13 @@ test('narrowing to a window the loaded data covers clears a failed wider reload' assert.equal(layer.getStats().error, null); assert.equal(layer.getStats().count, 1); }); + +test('a double-click follows a pin until its edition goes, as its entity was', async (t) => { + const { layer, viewer, dblclick, camera } = fixture(t); + await layer.update(viewer); + dblclick('ghostmaps:cip:0:0'); + assert.ok(camera.followed, 'the pin is followed'); + assert.match(camera.followed.key, /:icons:ghostmaps:cip:0:0$/); + layer.disable(); + assert.equal(camera.followed, null, 'its edition gone, so is the follow'); +});