Files
gods-eye-view/src/layers/radio/interaction.js
T
Sameh Khamis 02d492a079 Take pointer input through one dispatcher per scene
Every layer and tool bound its own ScreenSpaceEventHandler and picked the
same click again: with the usual layers on, one click ran about 26
scene.pick and 2 drillPick passes (120-215 ms in NYC with 18 layers). Pointer
input now comes in through src/input/dispatcher.js, one handler per scene.
A click is picked lazily and at most once per kind of query, and every
subscriber reads the same ClickEvent (pick, hit, id, ownerId, gesture,
drill, ground). The same click now costs 3 picks and 2 drills, 17-70 ms.

- Ambient onClick subscribers run only while no tool holds the pointer,
  checked once per event; tools subscribe with their lease. Moves and
  releases always reach ambient subscribers, so hover and drags end cleanly.
- Camera input and the viewer's stock select/track clicks are ref-counted
  holds (suspendCamera, suppressStockClicks). The dispatcher owns the
  controller's enableInputs, so a Cesium flight that ends no longer turns
  input back on under a drag or the cockpit.
- The draw tool, imagery box, CCTV gizmo, cockpit, directions and every
  selecting layer use it; bindTrackingClickGesture is gone (the gesture is
  on the event). A source scan forbids new handlers, enableInputs writes and
  stock-click removal outside the dispatcher.
- The sensor-style pick correction transit had now applies to every layer.
- Radio's 8-pick near-miss ring is one 17x17 windowed drill.
- A missed pick passed to pickContact is no longer picked again, and a
  touch tap's contact search shares the click's drill.
- A gizmo drag whose release Cesium never reported ends on the next press
  instead of leaving the camera held.

The dispatcher is exported as ./input with the pointer-lease functions, and
documented in docs/APPLICATION.md.
2026-11-04 08:48:51 -08:00

153 lines
5.0 KiB
JavaScript

import { inputFor } from '../../input/dispatcher.js';
import {
RADIO_PREFIX,
RADIO_PICK_WINDOW_PX,
RADIO_OVERLAY_SOURCE_ID,
HORIZON_TICK_MS,
} from './policy.js';
export function createInteraction({
state: layerState,
services,
parts,
source,
}) {
const {
resolvePickId,
isOwnedByOtherLayer,
registerPickOwner,
unregisterPickOwner,
} = services.picking;
const { setOverlaySourceVisible } = services.overlays;
/** Resolve a station id from ordinary, selected, or Cesium cluster pick shapes. */
function radioStationIdFromPick(picked) {
const pending = [picked?.id, picked?.primitive?.id];
const seen = new Set();
while (pending.length) {
const value = pending.shift();
if (typeof value === 'string' || typeof value === 'number') {
const id = String(value);
if (id.startsWith(`${RADIO_PREFIX}selected:`))
return id.slice(`${RADIO_PREFIX}selected:`.length);
if (id.startsWith(RADIO_PREFIX)) return id.slice(RADIO_PREFIX.length);
continue;
}
if (!value || typeof value !== 'object' || seen.has(value)) continue;
seen.add(value);
if (Array.isArray(value)) pending.push(...value);
else {
pending.push(value.id);
if (value.primitive) pending.push(value.primitive.id);
}
}
return null;
}
/**
* The station under a click: the pick itself, then one drill through a
* window around it, which also catches a near miss. Both passes are the
* click's own, so other layers reading the same click share them.
* @param {import('../../input/dispatcher.js').ClickEvent} event
* @returns {string|null}
*/
function pickedRadioStationAt(event) {
if (!event) return null;
const stationFromPick = (picked) => {
const stationId = radioStationIdFromPick(picked);
return stationId && layerState._stationById.has(stationId)
? stationId
: null;
};
const primaryPick = event.pick;
const primaryStationId = stationFromPick(primaryPick);
if (primaryStationId) return primaryStationId;
const primaryId = resolvePickId(primaryPick);
if (primaryId && isOwnedByOtherLayer('radio', primaryId)) return null;
for (const picked of event.drill(
16,
RADIO_PICK_WINDOW_PX,
RADIO_PICK_WINDOW_PX,
)) {
const stationId = stationFromPick(picked);
if (stationId) return stationId;
}
return null;
}
function radioPresentationAllowed() {
if (!layerState._managerLifecyclePresentation) return layerState._enabled;
return (
layerState._enabled &&
layerState._managerLifecyclePresentation.lifecycleState === 'enabled' &&
layerState._managerLifecyclePresentation.enabled &&
!layerState._managerLifecyclePresentation.uncertain
);
}
function syncRadioLifecyclePresentation() {
const visible = radioPresentationAllowed();
if (layerState._dataSource) layerState._dataSource.show = visible;
setOverlaySourceVisible(RADIO_OVERLAY_SOURCE_ID, visible);
if (visible) {
installInteraction();
parts.rendering.updateSelectionEntity();
parts.rendering.updateRenderVisibility();
parts.rendering.scheduleRadioOverlayPublish();
} else {
removeInteraction();
if (layerState._selectedEntity && layerState._viewer)
layerState._viewer.entities.remove(layerState._selectedEntity);
layerState._selectedEntity = null;
}
}
function installInteraction() {
if (!layerState._viewer || layerState._clickSubscription) return;
registerPickOwner('radio', (id) => id.startsWith(RADIO_PREFIX));
const viewer = layerState._viewer;
layerState._clickSubscription = inputFor(viewer).onClick((event) => {
if (!radioPresentationAllowed()) return;
const stationId = pickedRadioStationAt(event);
if (!stationId) return;
layerState._playFallbackId = null;
layerState._playFallbackFocus = null;
parts.selection.selectRadioStation(stationId, {
autoplay: true,
origin: 'user',
});
if (typeof document !== 'undefined') {
document.dispatchEvent(
new CustomEvent('gev:radio-selected', { detail: { stationId } }),
);
}
});
// Polling remains bounded during flights, but a stationary camera no longer
// rewrites every station's visibility four times per second while voice and
// audio processing share the main thread.
layerState._horizonTimer = setInterval(
() => parts.rendering.updateRenderVisibility({ force: false }),
HORIZON_TICK_MS,
);
}
function removeInteraction() {
unregisterPickOwner('radio');
layerState._clickSubscription?.();
layerState._clickSubscription = null;
if (layerState._horizonTimer) clearInterval(layerState._horizonTimer);
layerState._horizonTimer = null;
}
return {
radioStationIdFromPick,
pickedRadioStationAt,
radioPresentationAllowed,
syncRadioLifecyclePresentation,
installInteraction,
removeInteraction,
};
}