diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 0dabb63f42..5c8f1daf49 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -3374,6 +3374,11 @@ catalogued systems). The chip stays ON. A loading or failed Bike Share city shows its own text. Leaving coverage clears the last-update time. The voice analyst gets the same English notes; both translate them through `translateStatusMessage` (`src/ui/statusText.js`). +Above its gate Bike Share also marks every covered city on the globe: a +green, white-ringed dot per registry system, 5 km up so terrain never hides +it, shown only while the layer is on and stations are not drawn. Clicking one +flies straight down into the city from 20 km, an explicit camera action like +the other camera controls (`attachShellServices({ runNavigation })`). ## Places and CCTV request bounds diff --git a/src/layers/bikeshare/coverage.test.mjs b/src/layers/bikeshare/coverage.test.mjs new file mode 100644 index 0000000000..781253e669 --- /dev/null +++ b/src/layers/bikeshare/coverage.test.mjs @@ -0,0 +1,139 @@ +// Bike Share's covered cities on the globe (HAL-921): above the height +// stations draw at, every system in the registry has a marker, so the globe +// says where bike share is, and a click on one flies down into its city. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { GBFS_CITY_REGISTRY } from './registry.js'; +import { createState } from './state.js'; +import { createIngestion } from './ingestion.js'; +import { createModel } from './model.js'; +import { createViewport } from './viewport.js'; +import { createRendering } from './rendering.js'; +import { createSelection } from './selection.js'; +import { createLifecycle } from './lifecycle.js'; +import { COVERAGE_FLY_HEIGHT_M } from './policy.js'; +import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; +import { fakeInput } from '../../input/testDoubles.mjs'; + +globalThis.document ??= { + addEventListener() {}, + removeEventListener() {}, +}; + +function makeLayer() { + const owners = new Map(); + const services = { + render: { governorRequestRender() {} }, + overlays: { + setOverlayEntries() {}, + setOverlaySourceVisible() {}, + clearOverlaySource() {}, + }, + sprites: { registerSpriteCollection() {}, restoreSpriteOrder() {} }, + picking: { + registerPickOwner: (id, owns) => owners.set(id, owns), + unregisterPickOwner: (id) => owners.delete(id), + }, + }; + const source = { getStations: async () => ({ data: { stations: [] } }) }; + const state = createState({ services }); + const parts = {}; + const context = { state, services, parts, source }; + parts.ingestion = createIngestion(context); + parts.model = createModel(context); + parts.viewport = createViewport(context); + parts.rendering = createRendering(context); + parts.selection = createSelection(context); + parts.queries = { collectDetectableStations: () => [] }; + parts.lifecycle = createLifecycle(context); + + const camera = { lat: 20, lon: 0, height: 18_000_000 }; + const flights = []; + const viewer = { scene: { pick: () => undefined, canvas: {} } }; + const { draw } = bindFakeDraw(viewer, { + camera: { + pose: () => ({ ...camera }), + viewRectangle: () => null, + on: () => () => {}, + flyTo: (destination, options) => flights.push({ destination, options }), + }, + }); + // The layer's click subscription, called with a hit as the dispatcher gives it. + const fake = fakeInput(viewer); + const clicks = []; + const subscribe = fake.input.onClick; + fake.input.onClick = (fn) => { + clicks.push(fn); + return subscribe.call(fake.input, () => {}); + }; + parts.lifecycle.methods.init(viewer); + const coverage = () => + draw.find('bikeshare:') && + [...draw.sets.values()].find( + (set) => !set.disposed && set.spec.id.endsWith(':coverage'), + ); + return { + state, + parts, + owners, + flights, + coverage, + click: (hit) => clicks.at(-1)({ drawHit: hit }), + async moveTo(height) { + camera.height = height; + await parts.viewport.runProximityCheck(); + }, + enable: () => parts.lifecycle.methods.enable(viewer), + disable: () => parts.lifecycle.methods.disable(), + }; +} + +test('every covered city has a marker, shown only while the layer is on and too high for stations', async () => { + const layer = makeLayer(); + const set = layer.coverage(); + assert.equal(set.items.size, GBFS_CITY_REGISTRY.length); + assert.equal(set.show, false, 'hidden while the layer is off'); + layer.enable(); + await layer.moveTo(18_000_000); + assert.equal(set.show, true, 'the globe shows where bike share is'); + await layer.moveTo(20_000); + assert.equal( + set.show, + false, + 'stations take over below the activation height', + ); + await layer.moveTo(18_000_000); + assert.equal(set.show, true); + layer.disable(); + assert.equal(set.show, false, 'gone with the layer'); +}); + +test('a marker belongs to the layer and its click flies straight down into the city', async () => { + const layer = makeLayer(); + layer.enable(); + await layer.moveTo(18_000_000); + const city = GBFS_CITY_REGISTRY.find( + (each) => each.id === 'boston-bluebikes', + ); + const id = `city:${city.id}`; + assert.equal(layer.owners.get('bikeshare')(id), true); + assert.equal(layer.owners.get('bikeshare')('city:nowhere'), false); + const navigated = []; + layer.parts.lifecycle.methods.attachShellServices({ + runNavigation: (navigate) => { + navigated.push('explicit'); + return navigate(); + }, + }); + layer.click({ set: layer.coverage().spec.id, id }); + assert.deepEqual(navigated, ['explicit'], 'an explicit camera action'); + assert.equal(layer.flights.length, 1); + const { destination, options } = layer.flights[0]; + assert.deepEqual(destination, { + lon: city.centerLon, + lat: city.centerLat, + height: COVERAGE_FLY_HEIGHT_M, + }); + assert.equal(options.orientation.pitch, -Math.PI / 2); + assert.ok(COVERAGE_FLY_HEIGHT_M < 48_000, 'below the activation height'); +}); diff --git a/src/layers/bikeshare/lifecycle.js b/src/layers/bikeshare/lifecycle.js index e7c4b04896..4079b0c80e 100644 --- a/src/layers/bikeshare/lifecycle.js +++ b/src/layers/bikeshare/lifecycle.js @@ -20,6 +20,18 @@ export function createLifecycle({ } const methods = { + /** + * The shell's services: `runNavigation(navigate)` runs a covered city's + * flight as an explicit camera action, as the other camera controls are. + * @param {{ runNavigation?: Function }|null} shellServices + */ + attachShellServices(shellServices) { + layerState._runNavigation = + typeof shellServices?.runNavigation === 'function' + ? shellServices.runNavigation + : null; + }, + /** * Initialize the bikeshare layer. Makes the station draw sets on the * viewer's renderer (`drawOf`), resets all internal state, and installs @@ -81,8 +93,11 @@ export function createLifecycle({ ); parts.selection._installClickHandler(viewer); // Pick-ownership (H2): station point ids are string render-map keys. - registerPickOwner('bikeshare', (pickedId) => - layerState._stationRenderMap.has(pickedId), + registerPickOwner( + 'bikeshare', + (pickedId) => + layerState._stationRenderMap.has(pickedId) || + parts.selection.coverageCityOf(pickedId) !== null, ); if (!layerState._cameraChangedOff) { @@ -124,6 +139,7 @@ export function createLifecycle({ parts.viewport.deactivateAllCities(); layerState._cityRuntime.clear(); if (layerState._stationSet) layerState._stationSet.show = false; + parts.viewport.showCoverage(false); layerState._count = 0; layerState._loading = false; layerState._loadingOps = 0; diff --git a/src/layers/bikeshare/policy.js b/src/layers/bikeshare/policy.js index cf928a9dae..7686ca116c 100644 --- a/src/layers/bikeshare/policy.js +++ b/src/layers/bikeshare/policy.js @@ -15,6 +15,22 @@ const ACTIVATION_ALTITUDE_M = 50000; export const ACTIVATION_ENTER_ALTITUDE_M = ACTIVATION_ALTITUDE_M - 2000; +/** + * Where a covered city's marker flies to: straight down from this height, + * well inside the activation band, with the city's stations in view. + */ +export const COVERAGE_FLY_HEIGHT_M = 20000; + +/** A covered city's marker: the stations' green, larger, ringed in white. */ +export const COVERAGE_PIXEL_SIZE = 11; +export const COVERAGE_OUTLINE_WIDTH = 2; + +/** + * A marker's height: above the highest covered city's ground, so terrain + * never hides it, while the globe itself hides the markers behind it. + */ +export const COVERAGE_HEIGHT_M = 5000; + /** Hysteresis exit threshold — layer deactivates when camera rises above this. */ export const ACTIVATION_EXIT_ALTITUDE_M = ACTIVATION_ALTITUDE_M + 2000; diff --git a/src/layers/bikeshare/rendering.js b/src/layers/bikeshare/rendering.js index e0f29fb75e..1111da9e17 100644 --- a/src/layers/bikeshare/rendering.js +++ b/src/layers/bikeshare/rendering.js @@ -13,7 +13,11 @@ import { POINT_SCALE_BY_DISTANCE, POINT_FADE_BY_DISTANCE, POINT_DISABLE_DEPTH_TEST_M, + COVERAGE_PIXEL_SIZE, + COVERAGE_OUTLINE_WIDTH, + COVERAGE_HEIGHT_M, } from './policy.js'; +import { GBFS_CITY_REGISTRY } from './registry.js'; import { heightsOf } from '../../queries/heights.js'; import { fromDegrees } from '../../geo/ellipsoid.js'; import { drawOf } from '../../render/rendererOf.js'; @@ -24,6 +28,9 @@ import { drawOf } from '../../render/rendererOf.js'; * share a renderer. Picks name them as `Hit.set`. */ export const BIKESHARE_SET_ID = 'bikeshare'; + +/** A covered city's marker id: its registry id under this prefix. */ +export const COVERAGE_ID_PREFIX = 'city:'; let instances = 0; export function createRendering({ @@ -51,6 +58,23 @@ export function createRendering({ drawOrder: services.sprites?.spriteDrawOrder?.('bikeshare') ?? 0, }); layerState._selectedSet = draw.points({ id: `${prefix}:selected` }); + // One marker per covered city, drawn while the camera is too high for + // stations: the globe shows where bike share is. + layerState._coverageSet = draw.points({ + id: `${prefix}:coverage`, + show: false, + drawOrder: services.sprites?.spriteDrawOrder?.('bikeshare') ?? 0, + }); + layerState._coverageSet.set( + GBFS_CITY_REGISTRY.map((city) => ({ + id: `${COVERAGE_ID_PREFIX}${city.id}`, + pos: fromDegrees(city.centerLon, city.centerLat, COVERAGE_HEIGHT_M), + pixelSize: COVERAGE_PIXEL_SIZE, + color: COLOR_GREEN, + outlineColor: [1, 1, 1, 0.9], + outlineWidth: COVERAGE_OUTLINE_WIDTH, + })), + ); // Drawn from the start, empty, so both take their place in the scene // now, as the layer's collections did; the stations' place among the // other sprite layers is their draw order (`spriteDrawOrder`). @@ -62,8 +86,10 @@ export function createRendering({ function detachSets() { layerState._stationSet?.dispose(); layerState._selectedSet?.dispose(); + layerState._coverageSet?.dispose(); layerState._stationSet = null; layerState._selectedSet = null; + layerState._coverageSet = null; } /** diff --git a/src/layers/bikeshare/selection.js b/src/layers/bikeshare/selection.js index bbf962d8d3..cb3a5bf315 100644 --- a/src/layers/bikeshare/selection.js +++ b/src/layers/bikeshare/selection.js @@ -4,7 +4,11 @@ import { BIKESHARE_SELECTED_OVERLAY_SOURCE_ID, BIKESHARE_SELECTED_OVERLAY_SOURCE_OPTIONS, SELECTED_POINT_STYLE, + COVERAGE_FLY_HEIGHT_M, } from './policy.js'; +import { GBFS_CITY_REGISTRY } from './registry.js'; +import { COVERAGE_ID_PREFIX } from './rendering.js'; +import { cameraOf } from '../../render/rendererOf.js'; export function createSelection({ state: layerState, @@ -139,6 +143,38 @@ export function createSelection({ * @param {object} viewer - The viewer the layer draws on. */ + /** + * The covered city a marker id names, or null. + * @param {unknown} id + */ + function coverageCityOf(id) { + if (typeof id !== 'string' || !id.startsWith(COVERAGE_ID_PREFIX)) + return null; + const cityId = id.slice(COVERAGE_ID_PREFIX.length); + return GBFS_CITY_REGISTRY.find((city) => city.id === cityId) ?? null; + } + + /** + * Straight down over a covered city, low enough for its stations, as the + * shell's camera action when it gave one. + * @param {{ centerLat: number, centerLon: number }} city + */ + function flyIntoCity(city) { + const viewer = layerState._viewer; + if (!viewer) return; + const fly = () => + cameraOf(viewer).flyTo( + { + lon: city.centerLon, + lat: city.centerLat, + height: COVERAGE_FLY_HEIGHT_M, + }, + { orientation: { heading: 0, pitch: -Math.PI / 2, roll: 0 } }, + ); + if (layerState._runNavigation) layerState._runNavigation(fly); + else fly(); + } + function _installClickHandler(viewer) { if (layerState._clickSubscription) return; @@ -149,6 +185,15 @@ export function createSelection({ // Clicking the selected highlight itself — ignore (don't deselect). if (hit.set === layerState._selectedSet?.id) return; + // A covered city's marker: fly down into it. + if (hit.set === layerState._coverageSet?.id) { + const city = coverageCityOf(hit.id); + if (city) { + flyIntoCity(city); + return; + } + } + // A station dot of this layer's own set if ( hit.set === layerState._stationSet?.id && @@ -183,5 +228,6 @@ export function createSelection({ _selectStation, _installClickHandler, _onKeyDown, + coverageCityOf, }; } diff --git a/src/layers/bikeshare/state.js b/src/layers/bikeshare/state.js index 075f67a43a..ab5c5aac67 100644 --- a/src/layers/bikeshare/state.js +++ b/src/layers/bikeshare/state.js @@ -27,6 +27,17 @@ export function createState({ services }) { layerState._stationSet = null; + /** + * Draw set of the covered cities' markers, shown above the activation + * height so the globe says where bike share is, or null. + */ + + layerState._coverageSet = null; + + /** The shell's camera action for a marker's flight (`attachShellServices`), or null. */ + + layerState._runNavigation = null; + /** Draw set of the selected station's highlight dot, or null. */ layerState._selectedSet = null; diff --git a/src/layers/bikeshare/viewport.js b/src/layers/bikeshare/viewport.js index 89acab7c6c..a7a843250d 100644 --- a/src/layers/bikeshare/viewport.js +++ b/src/layers/bikeshare/viewport.js @@ -115,6 +115,18 @@ export function createViewport({ state: layerState, services, parts, source }) { parts.rendering.removeCityPoints(cityId); } + /** + * Show the covered cities' markers, or hide them: shown while the layer is + * on and the camera is too high for stations. + * @param {boolean} shown + */ + function showCoverage(shown) { + const set = layerState._coverageSet; + if (!set || set.show === shown) return; + set.show = shown; + governorRequestRender('bikeshare-coverage'); + } + /** Repaint the row now: it follows the camera, the manager tick is 60 s. */ function refreshRow() { layerState._dataManager?.refreshLayerStats?.('bikeshare'); @@ -212,11 +224,13 @@ export function createViewport({ state: layerState, services, parts, source }) { const wasGateEnabled = layerState._altitudeGateEnabled; // Altitude gate: disable all cities when camera is too high if (!shouldActivateAtAltitude(altitude)) { + showCoverage(true); deactivateAllCities(); if (wasGateEnabled) refreshRow(); return; } + showCoverage(false); const center = getCameraCenterLatLon(layerState._viewer); if (!center) return; @@ -288,6 +302,7 @@ export function createViewport({ state: layerState, services, parts, source }) { activateCity, runProximityCheck, scheduleProximityCheck, + showCoverage, onCameraChanged, }; } diff --git a/src/ui/layerBindings.js b/src/ui/layerBindings.js index 14bda13bdb..edad0c7bc1 100644 --- a/src/ui/layerBindings.js +++ b/src/ui/layerBindings.js @@ -158,6 +158,15 @@ export class LayerBindings { }); this._weatherShellModules.push(news); } + // Bike share flies into a covered city from its marker the same way. + const bikeshare = this._dataManager?.layers?.get('bikeshare')?.module; + if (typeof bikeshare?.attachShellServices === 'function') { + bikeshare.attachShellServices({ + runNavigation: (navigate) => + this.runImmediateNavigation('bike share', navigate), + }); + this._weatherShellModules.push(bikeshare); + } } _persistAwarenessSelection(event, cleared = false) {