diff --git a/changelog.d/1767-space-stations.md b/changelog.d/1767-space-stations.md index dc441eadb7..3a06aec2f7 100644 --- a/changelog.d/1767-space-stations.md +++ b/changelog.d/1767-space-stations.md @@ -37,5 +37,6 @@ #1767). - Let a click on a mapped installation's label box select the site, as a click on its 7 px dot does: from orbit the label is what a click lands on. - As with every card on top, the label takes the click from an aircraft or - vessel drawn under it (Denis Groshev, @ZloyS, #1767). + The label takes a click only where nothing else is drawn under it, so an + aircraft or vessel under a base's label stays selectable (Denis Groshev, + @ZloyS, #1767). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 9f07a38be5..f3af8557f2 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -3025,10 +3025,13 @@ with the arrows' easing on a slower clock (250 ms time constant, about `prefers-reduced-motion`. A single click on a site's marker, or on the label box the overlay paints for it, focuses it too: the click is resolved through the layer's own lookup (`findAt`: `scene.pick` for a named marker's -`{ installationId }` or a site entity, else the overlay's hit rect for the -label, which the installation entries now register as interactive; from -orbit the dot is 7 px, so the label is what a click lands on). The map's own -click selection reads the label the same way. A click anywhere else +`{ installationId }` or a site entity, else the box the overlay painted for +the label; from orbit the dot is 7 px, so the label is what a click lands +on). The map's own click selection reads the label the same way, through a +click claim (`registerClickClaim`) rather than as an interactive card: the +label takes a click only where nothing another layer draws is under it, so +an aircraft or vessel drawn under a base's label stays selectable, and +another layer's card painted over the label keeps its click. A click anywhere else (a site beyond the reach included), the focused site's row, NADIR, Home or `0` ends the focus and the view eases back to nadir on the same clock; an arrow ends it (or the ease back) and diff --git a/src/app/layers/overlayHost.js b/src/app/layers/overlayHost.js index 0e384f7472..cf3cfee68e 100644 --- a/src/app/layers/overlayHost.js +++ b/src/app/layers/overlayHost.js @@ -1,5 +1,6 @@ import { clearOverlaySource, + getOverlayPaintRect, getWorldOverlayDiagnostics, hitTestWorldOverlay, setOverlayEntries, @@ -9,6 +10,7 @@ export const overlayHost = Object.freeze({ clearSource: clearOverlaySource, getDiagnostics: getWorldOverlayDiagnostics, hitTest: hitTestWorldOverlay, + paintRect: getOverlayPaintRect, setEntries: setOverlayEntries, setVisible: setOverlaySourceVisible, }); diff --git a/src/layers/installations/controls.js b/src/layers/installations/controls.js index 15e30fc6dd..3bb7830d86 100644 --- a/src/layers/installations/controls.js +++ b/src/layers/installations/controls.js @@ -178,10 +178,10 @@ export function createControls({ }, /** - * The mapped installation whose label box the shared overlay paints on - * top at a canvas position: what a click lands on from far out, where the - * dot is 7 px. Another layer's label painted over it there wins, so this - * answers null. The dot, outline and fill themselves are query port + * The mapped installation whose label box the shared overlay paints at a + * canvas position: what a click lands on from far out, where the dot is + * 7 px. Another layer's card or label that takes clicks there wins, so + * this answers null. The dot, outline and fill themselves are query port * hits, read with {@link findByPick}. Null for anything else. * @param {{x: number, y: number}|null|undefined} position Canvas CSS px. * @returns {object|null} As {@link getById}. @@ -189,13 +189,15 @@ export function createControls({ findAt(position) { if (!Number.isFinite(position?.x) || !Number.isFinite(position?.y)) return null; - let hit = null; + let top = null; try { - hit = overlayHost?.hitTest?.(position.x, position.y) ?? null; + top = overlayHost?.hitTest?.(position.x, position.y) ?? null; } catch { - hit = null; + top = null; } - return hit?.sourceId === LAYER_ID ? methods.getById(hit.entryId) : null; + if (top) return null; + const id = parts.namedMarkers?.labelAt?.(position) ?? null; + return id ? methods.getById(id) : null; }, /** diff --git a/src/layers/installations/labelBoxClick.test.mjs b/src/layers/installations/labelBoxClick.test.mjs index d720453aa6..79e9153c35 100644 --- a/src/layers/installations/labelBoxClick.test.mjs +++ b/src/layers/installations/labelBoxClick.test.mjs @@ -1,10 +1,10 @@ // src/layers/installations/labelBoxClick.test.mjs — a click on a mapped -// installation's label box finds the site only where that label is the one -// painted on top: another layer's label over it keeps the click. +// installation's label box finds the site, read from the box the overlay +// painted for it, unless another layer's card or label that takes clicks is +// painted there. import assert from 'node:assert/strict'; import test from 'node:test'; import { createControls } from './controls.js'; -import { LAYER_ID } from './policy.js'; const RECORD = Object.freeze({ id: 'base', @@ -15,27 +15,28 @@ const RECORD = Object.freeze({ longitude: 20, }); -/** The installations controls over a fake overlay whose hit rects are given. */ -function controlsWith(rects) { +const inside = (box, { x, y }) => + x >= box.x && x <= box.x + box.w && y >= box.y && y <= box.y + box.h; + +/** + * The installations controls over a fake overlay: `cards` are the hit rects + * of entries that take clicks (other layers'), `labels` the boxes painted + * for this layer's labels, which are not cards. + */ +function controlsWith({ cards = [], labels = {} }) { const overlayHost = { - // As the world overlay does: the last painted rect under the point wins, - // filtered to a source when one is asked for. - hitTest(x, y, options = {}) { - for (let i = rects.length - 1; i >= 0; i--) { - const rect = rects[i]; - if (options.sourceId && rect.sourceId !== options.sourceId) continue; - if ( - x < rect.x || - x > rect.x + rect.w || - y < rect.y || - y > rect.y + rect.h - ) - continue; - return { sourceId: rect.sourceId, entryId: rect.entryId }; - } + // As the world overlay does: the last painted card under the point wins. + hitTest(x, y) { + for (let i = cards.length - 1; i >= 0; i--) + if (inside(cards[i], { x, y })) + return { sourceId: cards[i].sourceId, entryId: cards[i].entryId }; return null; }, }; + const namedMarkers = { + labelAt: (position) => + Object.keys(labels).find((id) => inside(labels[id], position)) ?? null, + }; const controls = createControls({ state: { enabled: true, @@ -43,42 +44,40 @@ function controlsWith(rects) { recordById: new Map([[RECORD.id, RECORD]]), }, services: {}, - parts: { rendering: { installationSurfaceHeightM: () => 0 } }, + parts: { + rendering: { installationSurfaceHeightM: () => 0 }, + namedMarkers, + }, source: {}, overlayHost, }); return controls.methods; } -const OWN = { - sourceId: LAYER_ID, - entryId: 'base', - x: 100, - y: 100, - w: 80, - h: 20, -}; +const BASE_LABEL = { x: 100, y: 100, w: 80, h: 20 }; test("a click on the site's own label box finds the site", () => { - const methods = controlsWith([OWN]); + const methods = controlsWith({ labels: { base: BASE_LABEL } }); assert.equal(methods.findAt({ x: 120, y: 110 })?.id, 'base'); assert.equal(methods.findAt({ x: 10, y: 10 }), null, 'outside every box'); assert.equal(methods.findAt(null), null); }); -test("another layer's label painted over the site's keeps the click", () => { - const methods = controlsWith([ - OWN, - { sourceId: 'flights', entryId: 'abc123', x: 110, y: 105, w: 40, h: 12 }, - ]); +test("another layer's card painted over the site's label keeps the click", () => { + const methods = controlsWith({ + labels: { base: BASE_LABEL }, + cards: [ + { sourceId: 'flights', entryId: 'abc123', x: 110, y: 105, w: 40, h: 12 }, + ], + }); assert.equal( methods.findAt({ x: 120, y: 110 }), null, - 'the flight is on top', + 'the flight card is on top', ); assert.equal( methods.findAt({ x: 170, y: 110 })?.id, 'base', - 'where the site is on top, it is found', + 'past the card, the site is found', ); }); diff --git a/src/layers/installations/namedMarkers.js b/src/layers/installations/namedMarkers.js index 0c7d848c89..e826ecf288 100644 --- a/src/layers/installations/namedMarkers.js +++ b/src/layers/installations/namedMarkers.js @@ -221,21 +221,19 @@ export function createNamedMarkers({ state, services, parts, overlayHost }) { // What `visit` hands out: the marker's id, position and whether // it is on this side of the horizon. point: { id: { installationId: record.id }, position, show: false }, - entry: Object.assign( - createInfrastructureOverlayEntry({ - id: record.id, - source: LAYER_ID, - position, - title: installationTitle(record.name), - priority: record.areaM2 || 0, - accent: COLOR_BY_CLASS[record.class] || '#9ca6b0', - }), - // The overlay keeps a hit rect for an interactive entry, so a - // click on the label box finds the site (the map click through the - // pick registry's card on top, the Cockpit through - // controls.findAt), not only one on the dot. - { interactive: true }, - ), + // Not an interactive overlay entry: those are cards that take every + // click from what is drawn under them, and an aircraft or vessel + // under a base's label must stay selectable. The label box still + // finds the site through its painted box (labelAt), which the + // layer's click claim reads where nothing else is drawn. + entry: createInfrastructureOverlayEntry({ + id: record.id, + source: LAYER_ID, + position, + title: installationTitle(record.name), + priority: record.areaM2 || 0, + accent: COLOR_BY_CLASS[record.class] || '#9ca6b0', + }), }; markers.set(record.id, marker); } else { @@ -281,6 +279,32 @@ export function createNamedMarkers({ state, services, parts, overlayHost }) { visitor(marker.recordId, marker.point); } }, + /** + * The site whose label box the shared overlay painted at a canvas + * position in its last frame, or null: what a click lands on from far + * out, where the dot is 7 px. + * @param {{x: number, y: number}|null|undefined} position Canvas CSS px. + * @returns {string|null} The installation id. + */ + labelAt(position) { + if (!state.enabled) return null; + const x = position?.x; + const y = position?.y; + if (!Number.isFinite(x) || !Number.isFinite(y)) return null; + for (let i = ordered.length - 1; i >= 0; i--) { + const { recordId } = ordered[i]; + const box = overlayHost.paintRect?.(LAYER_ID, recordId); + if ( + box && + x >= box.x && + x <= box.x + box.w && + y >= box.y && + y <= box.y + box.h + ) + return recordId; + } + return null; + }, refresh, enable, hide, diff --git a/src/layers/installations/namedMarkers.test.mjs b/src/layers/installations/namedMarkers.test.mjs index cdac82dae0..1d2361830b 100644 --- a/src/layers/installations/namedMarkers.test.mjs +++ b/src/layers/installations/namedMarkers.test.mjs @@ -26,7 +26,13 @@ function setup(t) { setVisible(_id, value) { visible = value; }, + // The boxes the overlay painted last frame, by entry id. + paintRect(id, entryId) { + assert.equal(id, 'military-installations'); + return boxes.get(entryId) ?? null; + }, }; + const boxes = new Map(); let x = -100; t.mock.method(C.SceneTransforms, 'worldToWindowCoordinates', () => new C.Cartesian2(x, 300)); const camera = { changed: new C.Event(), moveEnd: new C.Event(), positionWC: C.Cartesian3.fromDegrees(0, 0, 1e6), directionWC: new C.Cartesian3(0, 0, -1) }; @@ -42,7 +48,7 @@ function setup(t) { lifecycle.init(state.viewer); lifecycle.enable(); const records = [1, 2].map((n) => ({ id: `osm:military:w${n}`, name: `Site ${n}`, namedArea: true, areaM2: 100 / n, latitude: 0, longitude: 0, pointOnly: true })); const points = () => [...draw.sets.values()].find((set) => set.kind === 'points' && !set.disposed); - return { state, camera, draw, points, get labels() { return labels; }, get options() { return options; }, get visible() { return visible; }, records, markers: parts.namedMarkers, lifecycle, setX: (value) => { x = value; }, publications: () => publications, loads: () => loads }; + return { state, camera, draw, points, get labels() { return labels; }, get options() { return options; }, get visible() { return visible; }, records, markers: parts.namedMarkers, lifecycle, boxes, setX: (value) => { x = value; }, publications: () => publications, loads: () => loads }; } test('Contacts camera motion reprojects and declutters existing labels without source loads or primitive replacement', (t) => { @@ -170,7 +176,7 @@ test('the bounded host cohort ranks wide points and polygon titles by area', (t) !entry.stateless && entry.horizonCull && entry.collisionGroup === 'ambient-card' && - entry.interactive === true, + entry.interactive === false, ), ); const first = h.labels[0]; @@ -200,17 +206,36 @@ test('wide and polygon titles use the Data Centers entry contract and persistent priority: entry.priority, accent: entry.accent, }); - // Interactive: the overlay then keeps a hit rect for the label box, so - // a click on it finds the site (controls.findAt). - assert.deepEqual(entry, { - ...reference, - interactive: true, - selected: false, - }); + // Not interactive: an interactive entry is a card that takes the click + // from an aircraft or vessel drawn under it. A click on the label box + // finds the site through its painted box (labelAt). + assert.deepEqual(entry, { ...reference, selected: false }); } h.lifecycle.destroy(h.state.viewer); }); +test("labelAt finds the site whose label box was painted at a point, in CSS px", (t) => { + const h = setup(t); + h.markers.sync(h.records); + const [first, second] = h.records.map((record) => record.id); + h.boxes.set(first, { x: 100, y: 100, w: 80, h: 20 }); + h.boxes.set(second, { x: 300, y: 100, w: 60, h: 20 }); + assert.equal(h.markers.labelAt({ x: 120, y: 110 }), first); + assert.equal(h.markers.labelAt({ x: 360, y: 120 }), second, 'edges count'); + assert.equal(h.markers.labelAt({ x: 250, y: 110 }), null, 'between boxes'); + assert.equal(h.markers.labelAt(null), null); + assert.equal(h.markers.labelAt({ x: Number.NaN, y: 110 }), null); + // A site no longer marked has no label to find, even with a stale box. + h.markers.sync([h.records[1]]); + assert.equal(h.markers.labelAt({ x: 120, y: 110 }), null); + assert.equal(h.markers.labelAt({ x: 320, y: 110 }), second); + // Nor does a layer that is off. + h.state.enabled = false; + assert.equal(h.markers.labelAt({ x: 320, y: 110 }), null); + h.state.enabled = true; + h.lifecycle.destroy(h.state.viewer); +}); + test('selection promotes exactly one entry and deselection restores that same label', (t) => { const h = setup(t); h.markers.sync(h.records); diff --git a/src/layers/installations/selection.js b/src/layers/installations/selection.js index a6534f8654..e8d8ad0144 100644 --- a/src/layers/installations/selection.js +++ b/src/layers/installations/selection.js @@ -1,5 +1,10 @@ import { inputFor } from '../../input/dispatcher.js'; -import { clickCard, isUnderOtherCard } from '../../data/pickRegistry.js'; +import { + clickCard, + isUnderOtherCard, + registerClickClaim, + unregisterClickClaim, +} from '../../data/pickRegistry.js'; import { LAYER_ID } from './policy.js'; export function createSelection({ @@ -37,8 +42,39 @@ export function createSelection({ return layerState.selectedId === id; } + /** Whether a draw hit is one of this layer's: a dot, outline, fill or named marker. */ + function ownsHit(hit) { + return Boolean( + hit && + (hit.set === parts.namedMarkers?.setId || + parts.rendering.ownsSet(hit.set)), + ); + } + + /** + * A site's label box as the card on top of a click, for every layer's + * clickCard (the pick registry's click claim). Only where nothing another + * layer draws is under the click: an aircraft or vessel drawn under a + * base's label keeps its own click, while a label over the map, or over + * this layer's own dots and fills, takes it. Runs only where no card or + * label that takes clicks is painted on top. + * @param {object} event The shared dispatcher click. + * @returns {{sourceId: string, entryId: string}|null} + */ + function labelClickCard(event) { + if (!layerState.enabled || !event) return null; + if (event.topHit && !ownsHit(event.drawHit)) return null; + const id = + parts.namedMarkers?.labelAt?.(event.screenPosition ?? event.position) ?? + null; + return id && layerState.recordById.has(id) + ? { sourceId: LAYER_ID, entryId: id } + : null; + } + function installInteraction(viewer) { if (layerState.clickSubscription) return; + registerClickClaim(LAYER_ID, labelClickCard); const events = [ 'gev:entity-selected', 'gev:entity-selection-cleared', @@ -57,18 +93,16 @@ export function createSelection({ // under it does not act, and the click is not empty space either. A // site's own label box is this layer's. if (isUnderOtherCard(event, [LAYER_ID])) return; - // The site whose label box is painted on top at the click takes it, - // whatever is drawn under it: from far out the click lands on the label, - // not the 7 px dot. Else a site's dot, outline or fill, or a named - // marker: each draw item names its installation in its `meta`. + // The site whose label box is at the click takes it where nothing + // another layer draws is under it (labelClickCard): from far out the + // click lands on the label, not the 7 px dot. Else a site's dot, + // outline or fill, or a named marker: each draw item names its + // installation in its `meta`. const card = clickCard(event); const hit = event.drawHit; const id = (card?.sourceId === LAYER_ID ? card.entryId : null) ?? - (hit?.set === parts.namedMarkers?.setId || - parts.rendering.ownsSet(hit?.set) - ? hit.meta?.installationId - : null) ?? + (ownsHit(hit) ? hit.meta?.installationId : null) ?? null; if (id && layerState.recordById.has(id) && id !== layerState.selectedId) { selectRecord(id); @@ -88,6 +122,7 @@ export function createSelection({ destroy() { removeContextListeners?.(); removeContextListeners = null; + unregisterClickClaim(LAYER_ID); }, }; } diff --git a/src/layers/installations/selection.test.mjs b/src/layers/installations/selection.test.mjs index 9354b143e0..a1636d2c28 100644 --- a/src/layers/installations/selection.test.mjs +++ b/src/layers/installations/selection.test.mjs @@ -7,7 +7,7 @@ import { createRendering } from './rendering.js'; import { createNamedMarkers, namedPointId } from './namedMarkers.js'; import { bindFakeCesiumScene } from '../../testSupport/fakeCesiumScene.mjs'; import { fakeInput } from '../../input/testDoubles.mjs'; -import { setClickCardHitTest } from '../../data/pickRegistry.js'; +import { clickCard, setClickCardHitTest } from '../../data/pickRegistry.js'; import { LAYER_ID } from './policy.js'; function harness(t) { @@ -61,6 +61,8 @@ function harness(t) { () => new Cesium.Cartesian2(640, 400), ); let labels = []; + // The boxes the overlay painted last frame for this layer's labels. + const boxes = new Map(); const services = { context, anchors: {}, @@ -83,6 +85,9 @@ function harness(t) { }, clearSource() {}, setVisible() {}, + paintRect(_id, entryId) { + return boxes.get(entryId) ?? null; + }, }, }; parts.rendering = createRendering(ctx); @@ -117,6 +122,7 @@ function harness(t) { draw, input, parts, + boxes, /** Click on what `pick` names, as the scene would pick it. */ click(value) { picked = value; @@ -260,33 +266,63 @@ test('a click on a site dot or its outline selects the site; another set does no assert.equal(selectedNow(), false); }); -test("a click on a site's label box selects it, whatever is drawn under it", (t) => { +test("a click on a site's label box selects it where nothing else is drawn under it", (t) => { const h = harness(t); h.state.selectedId = null; context.clearSelectedEntityContextForLayer('military-installations'); h.parts.rendering.renderRecords(); - let top = null; - const asked = []; - setClickCardHitTest((x, y) => { - asked.push({ x, y }); - return top; - }); - t.after(() => setClickCardHitTest(null)); + // The card every layer reads for each click, as Context's bridge reads it. + const cards = []; + const offCards = h.input.input.onClick((event) => + cards.push(clickCard(event)), + ); + t.after(offCards); // No label at the click and nothing picked: nothing is selected. h.click(undefined); - assert.deepEqual( - asked.at(-1), - { x: 640, y: 400 }, - 'in CSS px, where clicked', - ); h.assertSelected(false); - // The site's own label box on top selects it; again releases it. - top = { sourceId: LAYER_ID, entryId: 'base' }; + assert.equal(cards.at(-1), null); + // The site's label box at the click (CSS px) selects it; again releases it. + h.boxes.set('base', { x: 600, y: 390, w: 80, h: 20 }); h.click(undefined); h.assertSelected(true); + assert.deepEqual(cards.at(-1), { sourceId: LAYER_ID, entryId: 'base' }); h.click(undefined); h.assertSelected(false); - // The label painted on top takes the click from an aircraft drawn under it. + // Over the site's own named marker, the label still takes the click. + const setId = h.parts.namedMarkers.setId; + const item = h.draw.set(setId).items.get(namedPointId('base')); + h.click({ id: { set: setId, id: item.id, meta: item.meta } }); + h.assertSelected(true); + assert.deepEqual(cards.at(-1), { sourceId: LAYER_ID, entryId: 'base' }); + // An aircraft drawn under the label keeps its click: the label is no card + // there, so its own layer and Context can select it, and the site lets go. h.click({ id: 'flight:abc123' }); - h.assertSelected(true); + h.assertSelected(false); + assert.equal(cards.at(-1), null); + // Another layer's card painted over the label takes the click. + setClickCardHitTest(() => ({ sourceId: 'earthquake-card', entryId: 'ak1' })); + t.after(() => setClickCardHitTest(null)); + h.click(undefined); + h.assertSelected(false); + setClickCardHitTest(null); + // A layer that is off claims nothing. + h.state.enabled = false; + h.click(undefined); + assert.equal(cards.at(-1), null); + h.state.enabled = true; +}); + +test('destroy withdraws the label claim', (t) => { + const h = harness(t); + h.boxes.set('base', { x: 600, y: 390, w: 80, h: 20 }); + const cards = []; + const offCards = h.input.input.onClick((event) => + cards.push(clickCard(event)), + ); + t.after(offCards); + h.click(undefined); + assert.deepEqual(cards.at(-1), { sourceId: LAYER_ID, entryId: 'base' }); + h.selection.destroy(); + h.click(undefined); + assert.equal(cards.at(-1), null); });