Files
gods-eye-view/src/globe/pickDelivery.js
T
German Khokhlov b0cb8ce881 feat(globe): picks on input payloads
input.on('click'|'doubleClick'|'move', fn, { pick, drill }) delivers
{ …event, picked, drilled }: PickResult { id, owner, instance, primitive
(Cesium) } names the mover set (and instance) or line hit. One pick (and
one drill, as deep as the deepest listener) per event, shared by its
listeners; moves with picks are coalesced to one delivery per frame with
the latest position (src/globe/pickDelivery.js). Option checks in the
contract (normalizeInputOptions).

- Cesium: scene.pick / drillPick, and the picker names the owner: movers
  map billboards, points and models to their set and instance, lines
  their primitives to the line.
- Navara: a click with pick takes Navara's GPU pick of the same gesture
  (featureClick; its pick helper answers on the same pointerup, usually
  before the engine's click, so a fresh answer waits for it; otherwise
  the click waits up to 250 ms, then the CPU pick). Navara 0.2.1 answers
  clicks in quick succession with the first click's pick: a GPU answer
  whose instance is not drawn near the click is stale and the CPU pick
  answers. Hover, drill and the synchronous picker.pick/drillPick are CPU
  picks (src/globe/cpuPicks.js) of pickable mover sets and pickable lines
  (within half the width + 3 px), behind the globe excluded, nearest
  first; lines are picked on the CPU after an empty GPU answer.
  picker.clickStats() (Navara only) counts the answers and latencies.
- The fake engine picks its own movers and lines on the CPU unless
  scripted.
- The contract suite clicks with real pointer events (puppeteer) on both
  engines: a sprite is picked with its owner and instance, the map picks
  nothing, two sprites in one place drill both, a line is picked, a
  hover picks the sprite under it. Navara, 20 clicks: payload p50 2.8 ms,
  p95 4.1 ms after the click (puppeteer dispatch included), 20/20 GPU
  answers. The Cesium harness keeps the first-run card closed.
2026-10-16 18:25:12 +04:00

68 lines
2.5 KiB
JavaScript

// Delivers input events to listeners that asked for picks (input.on with
// { pick, drill }), for every engine: one pick (and one drill, as deep as
// the deepest listener asked) per event, shared by its listeners; moves are
// coalesced to one delivery per frame with the latest position. Picks may
// be asynchronous (Navara answers clicks from its GPU pick of the gesture):
// listeners still registered when the answer arrives get it.
const settle = (value, then) =>
value && typeof value.then === 'function' ? value.then(then) : then(value);
/**
* @param {{ pick: (position: object, type: string) => any, drill: (position: object, limit: number) => any,
* schedule: (run: () => void) => void }} ports `schedule` runs a callback
* once on the next frame (requestAnimationFrame in a browser).
*/
export function createPickDelivery({ pick, drill, schedule }) {
let pendingMove = null;
function run(type, event, entries, isLive) {
const at = event.position ?? event.endPosition;
const depth = entries.reduce(
(most, entry) => Math.max(most, entry.drill),
0,
);
return settle(pick(at, type), (picked) =>
settle(depth ? drill(at, depth) : undefined, (drilled) => {
for (const entry of entries) {
if (!isLive(entry)) continue;
entry.listener({
...event,
picked,
...(entry.drill
? { drilled: (drilled ?? []).slice(0, entry.drill) }
: {}),
});
}
}),
);
}
return {
/**
* @param {string} type
* @param {object} event the contract event, without picks
* @param {{ listener: Function, pick: boolean, drill: number }[]} entries
* @param {{ isLive?: (entry: object) => boolean }} [options]
* @returns {void | Promise<void>} settled once the pick listeners ran
*/
deliver(type, event, entries, { isLive = () => true } = {}) {
const pickers = [];
for (const entry of entries)
if (entry.pick) pickers.push(entry);
else if (isLive(entry)) entry.listener(event);
if (!pickers.length) return undefined;
if (type !== 'move') return run(type, event, pickers, isLive);
const scheduled = pendingMove !== null;
pendingMove = { event, pickers, isLive };
if (!scheduled)
schedule(() => {
const latest = pendingMove;
pendingMove = null;
run('move', latest.event, latest.pickers, latest.isLive);
});
return undefined;
},
};
}