diff --git a/src/layers/news/index.js b/src/layers/news/index.js index 4ad5826c2..82666554c 100644 --- a/src/layers/news/index.js +++ b/src/layers/news/index.js @@ -1,5 +1,9 @@ import { isPointerFree } from '../../data/inputOwnership.js'; import { inputFor as defaultInputFor } from '../../input/dispatcher.js'; +import { + drawnItemTarget, + followOnDoubleClick, +} from '../../data/stockFollow.js'; import { fromDegrees } from '../../geo/ellipsoid.js'; import { cssColor } from '../../render/color.js'; import { @@ -218,10 +222,15 @@ export function createNewsLayer({ if (drawPictureCards !== null && typeof drawPictureCards !== 'function') throw new TypeError('News picture cards must be a function'); + const outlineItems = new Map(); let viewer = null, // The layer's draw sets (`sets.icons` and the rest), made on init. sets = null, clickOff = null, + // The follow a double-click started, and what each set draws now (the + // markers by set, the outlines by id), for it to follow or zoom to. + stockFollow = null, + drawnMarkers = new Map(), listener = null, runNavigation = null, request = null, @@ -881,6 +890,19 @@ export function createNewsLayer({ horizon.update(cullTargets, { quiet: true }); for (const kind of ['halos', 'points', 'icons', 'boxes']) sets[kind].set(list[kind]); + // Every marker was drawn anew as an entity: that ended a follow of one. + stockFollow?.retain( + (key) => + !['halos', 'points', 'icons', 'boxes'].some((kind) => + key.startsWith(`${sets[kind].id}:`), + ), + ); + drawnMarkers = new Map( + ['halos', 'points', 'icons', 'boxes'].map((kind) => [ + sets[kind].id, + new Map(list[kind].map((item) => [item.id, item])), + ]), + ); updatePulse(); if (drawPictureCards) // At every height: zoomed out, a hotspot's reports give way to its @@ -1026,6 +1048,8 @@ export function createNewsLayer({ for (const [index, area] of disputed.entries()) for (const [k, points] of area.rings.entries()) line(`${pickId}~disputed${index}.${k}`, closed(points), 2, dotted); + for (const item of [...parts.fills, ...parts.borders]) + outlineItems.set(item.id, item); if (parts.fills.length) sets.outlineFills.upsert(parts.fills); if (parts.borders.length) sets.outlineLines.upsert(parts.borders); outlineDisputed.set(key, disputed); @@ -1410,11 +1434,37 @@ export function createNewsLayer({ if (id && isOwnedByOtherLayer(NEWS_LAYER_ID, id)) return; if (selectedId && !picked?.id) layer.setParams({ clear: true }); }); + // A double-click follows a marker and zooms to an outline, as the + // default renderer did for their entities. + stockFollow = followOnDoubleClick({ + viewer, + inputFor, + name: NEWS_LAYER_ID, + enabled: () => enabled && !destroyed, + targetFor: followTargetFor, + }); } function removeSelection() { clickOff?.(); clickOff = null; + stockFollow?.destroy(); + stockFollow = null; + } + + /** + * What a double-click on a hit follows or zooms to, as the default + * renderer did for the entity it was: a marker is followed, an outline or + * hotspot box zoomed to; an item not shown takes no part. + */ + function followTargetFor(hit) { + if (!sets || hit.surface) return null; + const item = + drawnMarkers.get(hit.set)?.get(hit.id) ?? + (hit.set === sets.outlineFills.id || hit.set === sets.outlineLines.id + ? outlineItems.get(hit.id) + : null); + return item && item.show !== false ? drawnItemTarget(hit.set, item) : null; } function feedView() { @@ -1760,6 +1810,7 @@ export function createNewsLayer({ if (sets) for (const set of Object.values(sets)) set.dispose(); sets = null; outlineParts.clear(); + outlineItems.clear(); outlineDisputed.clear(); viewer = null; listener = null; diff --git a/src/layers/news/layer.test.mjs b/src/layers/news/layer.test.mjs index 9804011d7..bdcff6658 100644 --- a/src/layers/news/layer.test.mjs +++ b/src/layers/news/layer.test.mjs @@ -103,6 +103,10 @@ function fixture( }); cameraPort.port.flyTo = (destination, options = {}) => flights.push({ destination, ...options }); + // What the camera follows, and where a double-click zooms. + cameraPort.port.followed = null; + cameraPort.port.follow = (target) => + (cameraPort.port.followed = target ?? null); const camera = { setHeight: (next) => cameraPort.setPose({ @@ -112,8 +116,10 @@ function fixture( moveEnd: () => cameraPort.emit('moveEnd'), }; const viewer = {}; + let picked = null; const { draw, unbind } = bindFakeDraw(viewer, { camera: cameraPort.port, + query: { pick: () => picked }, frame: { requestRender() {}, on: () => () => {} }, ...(quality ? { quality } : {}), }); @@ -127,11 +133,23 @@ function fixture( ]; // The scene's click dispatcher: `click(hit)` clicks over a drawn item. const clickHandlers = new Set(); + const doubleHandlers = new Set(); const input = { onClick(fn) { clickHandlers.add(fn); return () => clickHandlers.delete(fn); }, + onPointer(type, fn) { + if (type !== 'dblclick') return () => {}; + doubleHandlers.add(fn); + return () => doubleHandlers.delete(fn); + }, + stockEnabled: (name) => name === 'dblclickFollow', + }; + // `dblclick(hit)` double-clicks over a drawn item. + const dblclick = (hit) => { + picked = hit; + for (const fn of [...doubleHandlers]) fn({ position: { x: 10, y: 10 } }); }; const click = (topHit) => { for (const fn of [...clickHandlers]) @@ -172,6 +190,13 @@ function fixture( items, click, clickHandlers, + dblclick, + cameraPort: cameraPort.port, + /** The id of the layer's live draw set named `part`. */ + setId: (part) => + [...draw.sets.values()].find( + (set) => !set.disposed && set.spec.id.endsWith(`:${part}`), + )?.spec.id, }; } @@ -1301,3 +1326,51 @@ test('zoomed out, a hotspot is one beacon; off, nothing draws; destroyed, the se layer.destroy(); assert.deepEqual(items('points'), []); }); + +test('a double-click follows a marker until the markers are drawn anew, and zooms to a hotspot outline', async (t) => { + const { layer, items, dblclick, cameraPort, setId } = fixture(t, { + height: 500_000, + source: { + getOutline: async () => ({ + polygons: [ + { + outer: [ + [30, 45], + [40, 45], + [40, 52], + [30, 52], + ], + holes: [], + }, + ], + borders: [], + disputed: [], + }), + }, + }); + const zooms = []; + cameraPort.flyToBounds = (sphere) => zooms.push(sphere); + layer.enable(); + await layer.update(); + await flush(); + const marker = [...items('points'), ...items('icons')].find((item) => + String(item.id).startsWith('news:report:major~'), + ); + const set = setId(items('points').includes(marker) ? 'points' : 'icons'); + dblclick({ set, id: marker.id, surface: false }); + assert.ok(cameraPort.followed, 'the marker is followed'); + assert.deepEqual(cameraPort.followed.position(), marker.pos); + // A render draws every marker anew, as the entities were. + await layer.update(); + await flush(); + assert.equal(cameraPort.followed, null, 'drawn anew: no longer followed'); + // Each line a hotspot box or outline draws is zoomed to. + const line = [...items('boxes'), ...items('outline-lines')].find( + (item) => item.show !== false, + ); + const part = items('boxes').includes(line) ? 'boxes' : 'outline-lines'; + assert.ok(line, 'a box or outline is drawn'); + dblclick({ set: setId(part), id: line.id, surface: false }); + assert.ok(zooms.at(-1)?.radius > 0, 'it is zoomed to'); + assert.equal(cameraPort.followed, null); +});