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.
This commit is contained in:
Sameh Khamis committed 2026-11-04 08:48:51 -08:00
1 parent 83ec9473b6
commit 02d492a079
97 files changed
+2506 -1307

No files matched your search

+14 -18
View File
@@ -42,6 +42,7 @@ import {
releaseContinuousRender,
uninstallRenderGovernor,
} from '../../renderGovernor.js';
import { fakeInput } from '../../input/testDoubles.mjs';
const BOSTON = { lat: 42.3601, lon: -71.0589 };
@@ -319,9 +320,11 @@ function harness(
else globalThis.window = originalWindow;
});
const input = fakeInput(viewer);
return {
layer,
viewer,
input,
primitives,
sprites,
pickOwners,
@@ -3169,11 +3172,7 @@ test('click handler selects a migrated scripted billboard and prepares a valid d
const entry = app.vehicles()[0];
assert.equal(entry.markerCollection, app.state()._animatedMarkers);
app.viewer.scene.pick = () => ({ primitive: entry.marker, id: entry.key });
app
.state()
._clickHandler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)({
position: new Cesium.Cartesian2(500, 400),
});
app.input.click(new Cesium.Cartesian2(500, 400));
assert.equal(app.state()._selectedKey, entry.key);
const body = app.layer._transitPartsForTest().trails.diagnostics().body;
assert.ok(body.geometryInstances.length > 0);
@@ -3401,11 +3400,7 @@ test('preset clicks preserve both pick id paths through moving and stationary co
assert.equal(entry.marker.id, entry.key);
for (const pick of [{ id: entry.key }, { primitive: entry.marker }]) {
app.viewer.scene.pick = () => pick;
app
.state()
._clickHandler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)(
{ position: new Cesium.Cartesian2(500, 400) },
);
app.input.click(new Cesium.Cartesian2(500, 400));
assert.equal(app.state()._selectedKey, entry.key);
parts.selection.clearSelection();
}
@@ -3449,19 +3444,20 @@ test('sensor clicks map displayed pixels into the pre-shader pick buffer', async
clientHeight: 1080,
};
const uniforms = { intensity: 1, pixelation: 1 };
let preset = 'surveillance';
scene.postProcessStages = {
getStageByName: () => ({ enabled: true, uniforms }),
getStageByName: (name) =>
name === `godsEyeView_${preset}`
? { enabled: true, uniforms }
: undefined,
};
const click = app
.state()
._clickHandler.getInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
app.layer._transitPartsForTest().rendering.setStylePreset('surveillance');
click({ position: new Cesium.Cartesian2(1440, 540) });
const click = (position) => app.input.click(position);
click(new Cesium.Cartesian2(1440, 540));
assert.ok(Math.abs(actual.x - 1472) < 1e-6);
assert.equal(actual.y, 540);
app.layer._transitPartsForTest().rendering.setStylePreset('thermal');
preset = 'thermal';
uniforms.pixelation = 1.5;
click({ position: new Cesium.Cartesian2(100.9, 200.1) });
click(new Cesium.Cartesian2(100.9, 200.1));
assert.ok(Math.abs(actual.x - 100.5) < 1e-6);
assert.ok(Math.abs(actual.y - 201) < 1e-6);
assert.equal(app.state()._selectedKey, entry.key);
+10 -49
View File
@@ -7,7 +7,7 @@ import {
createTransitSelectedOverlayEntry,
} from './policy.js';
import { getRegisteredTransitFeed } from '../../data/transitFeeds.js';
import { isPointerFree } from '../../data/inputOwnership.js';
import { inputFor } from '../../input/dispatcher.js';
import { t } from '../../i18n/index.js';
/**
@@ -115,49 +115,12 @@ export function createSelection({ state, services, parts }) {
}
function installClickHandler(viewer) {
if (state._clickHandler) return;
state._clickHandler = new Cesium.ScreenSpaceEventHandler(
viewer.scene.canvas,
);
state._clickHandler.setInputAction((click) => {
// Ambient selection never competes for a pointer a tool has claimed:
// placing a draw vertex on top of a bus must not also select the bus.
if (!isPointerFree()) return;
const scene = viewer.scene;
// Pick buffers precede post-processing. Map a sensor pixel back through
// the active shader's barrel distortion and quantization before picking.
const position = Cesium.Cartesian2.clone(click.position);
const style = state._stylePreset;
const stage = scene.postProcessStages?.getStageByName?.(
`godsEyeView_${style}`,
);
if (stage?.enabled && (style === 'thermal' || style === 'surveillance')) {
const intensity = stage.uniforms.intensity;
const canvas = scene.canvas,
w = canvas.clientWidth,
h = canvas.clientHeight;
let u = position.x / w,
v = 1 - position.y / h;
if (style === 'surveillance') {
const x = u * 2 - 1,
y = v * 2 - 1,
r2 = x * x + y * y;
const d = 1 + r2 * intensity * 0.25 + r2 * r2 * intensity * 0.075;
u = (x * d + 1) / 2;
v = (y * d + 1) / 2;
}
const grid = 1 + (stage.uniforms.pixelation - 1) * intensity;
u +=
((Math.floor((u * canvas.width) / grid) * grid) / canvas.width - u) *
intensity;
v +=
((Math.floor((v * canvas.height) / grid) * grid) / canvas.height -
v) *
intensity;
position.x = u * w;
position.y = (1 - v) * h;
}
const picked = scene.pick(position);
if (state._clickSubscription) return;
// The dispatcher maps a sensor-style click back into the pick buffer
// (src/input/sensorPick.js) and skips it while a tool holds the pointer.
state._clickSubscription = inputFor(viewer).onClick((event) => {
const position = event.position;
const picked = event.pick;
state._lastPickForTest = {
id: typeof picked?.id === 'string' ? picked.id : null,
primitiveId:
@@ -188,15 +151,13 @@ export function createSelection({ state, services, parts }) {
}
}
if (state._selectedKey) clearSelection();
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
});
document.addEventListener('keydown', onKeyDown);
}
function removeClickHandler() {
if (state._clickHandler) {
state._clickHandler.destroy();
state._clickHandler = null;
}
state._clickSubscription?.();
state._clickSubscription = null;
document.removeEventListener('keydown', onKeyDown);
}
+2 -2
View File
@@ -47,8 +47,8 @@ export function createState({ services }) {
/** @type {(() => void)|null} */
state._preRenderRemove = null;
state._renderHeld = false;
/** @type {Cesium.ScreenSpaceEventHandler|null} */
state._clickHandler = null;
/** @type {(() => void)|null} Unsubscribes the vehicle-selection click. */
state._clickSubscription = null;
/**
* The vehicles currently mid-glide. The per-frame pass walks THIS, not the
* whole fleet, and the render hold is derived from it together with the