mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
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.
203 lines
6.9 KiB
JavaScript
203 lines
6.9 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createNavaraInput } from './input.js';
|
|
|
|
function fakeCanvas() {
|
|
const canvas = new EventTarget();
|
|
canvas.getBoundingClientRect = () => ({ left: 100, top: 50 });
|
|
canvas.listenerCount = 0;
|
|
const add = canvas.addEventListener.bind(canvas);
|
|
const remove = canvas.removeEventListener.bind(canvas);
|
|
canvas.addEventListener = (...args) => {
|
|
canvas.listenerCount++;
|
|
add(...args);
|
|
};
|
|
canvas.removeEventListener = (...args) => {
|
|
canvas.listenerCount--;
|
|
remove(...args);
|
|
};
|
|
return canvas;
|
|
}
|
|
|
|
const pointer = (type, x, y, extra = {}) =>
|
|
Object.assign(new Event(type), {
|
|
clientX: x,
|
|
clientY: y,
|
|
button: 0,
|
|
shiftKey: false,
|
|
ctrlKey: false,
|
|
altKey: false,
|
|
...extra,
|
|
});
|
|
|
|
function setup() {
|
|
const canvas = fakeCanvas();
|
|
const cameraOptions = [];
|
|
const cameraInputs = [];
|
|
const view = {
|
|
canvas,
|
|
camera: {
|
|
set options(value) {
|
|
cameraOptions.push(value);
|
|
},
|
|
},
|
|
};
|
|
const { input, destroy } = createNavaraInput(view, {
|
|
onCameraInput: (type) => cameraInputs.push(type),
|
|
});
|
|
return { canvas, input, destroy, cameraOptions, cameraInputs };
|
|
}
|
|
|
|
test('down, up and click carry canvas-relative positions', () => {
|
|
const { canvas, input } = setup();
|
|
const seen = [];
|
|
for (const type of ['down', 'up', 'click'])
|
|
input.on(type, (event) => seen.push([type, event.position]));
|
|
canvas.dispatchEvent(pointer('pointerdown', 110, 60));
|
|
canvas.dispatchEvent(pointer('pointerup', 113, 61));
|
|
assert.deepEqual(seen, [
|
|
['down', { x: 10, y: 10 }],
|
|
['up', { x: 13, y: 11 }],
|
|
['click', { x: 13, y: 11 }],
|
|
]);
|
|
});
|
|
|
|
test('a drag beyond the 5 px click tolerance is not a click; other buttons are ignored', () => {
|
|
const { canvas, input } = setup();
|
|
let clicks = 0;
|
|
input.on('click', () => clicks++);
|
|
canvas.dispatchEvent(pointer('pointerdown', 110, 60));
|
|
canvas.dispatchEvent(pointer('pointerup', 120, 60));
|
|
canvas.dispatchEvent(pointer('pointerdown', 110, 60, { button: 2 }));
|
|
canvas.dispatchEvent(pointer('pointerup', 110, 60, { button: 2 }));
|
|
assert.equal(clicks, 0);
|
|
});
|
|
|
|
test('move reports start and end positions; doubleClick a position', () => {
|
|
const { canvas, input } = setup();
|
|
const seen = [];
|
|
input.on('move', (event) => seen.push(event));
|
|
input.on('doubleClick', (event) => seen.push(event));
|
|
canvas.dispatchEvent(pointer('pointermove', 110, 60));
|
|
canvas.dispatchEvent(pointer('pointermove', 115, 70));
|
|
canvas.dispatchEvent(pointer('dblclick', 115, 70));
|
|
assert.deepEqual(seen, [
|
|
{ startPosition: { x: 10, y: 10 }, endPosition: { x: 10, y: 10 } },
|
|
{ startPosition: { x: 10, y: 10 }, endPosition: { x: 15, y: 20 } },
|
|
{ position: { x: 15, y: 20 } },
|
|
]);
|
|
});
|
|
|
|
test('modifiers: a listener fires only for its own key, an unmodified one only without keys', () => {
|
|
const { canvas, input } = setup();
|
|
const seen = [];
|
|
input.on('down', () => seen.push('none'));
|
|
input.on('down', () => seen.push('shift'), { modifier: 'shift' });
|
|
input.on('down', () => seen.push('ctrl'), { modifier: 'ctrl' });
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1));
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1, { shiftKey: true }));
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1, { ctrlKey: true }));
|
|
// Cesium takes shift over ctrl over alt.
|
|
canvas.dispatchEvent(
|
|
pointer('pointerdown', 1, 1, { ctrlKey: true, shiftKey: true }),
|
|
);
|
|
assert.deepEqual(seen, ['none', 'shift', 'ctrl', 'shift']);
|
|
});
|
|
|
|
test('on validates its arguments and returns an idempotent remover', () => {
|
|
const { canvas, input } = setup();
|
|
assert.throws(() => input.on('wheel', () => {}), TypeError);
|
|
assert.throws(
|
|
() => input.on('click', () => {}, { modifier: 'meta' }),
|
|
TypeError,
|
|
);
|
|
let count = 0;
|
|
const off = input.on('down', () => count++);
|
|
off();
|
|
off();
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1));
|
|
assert.equal(count, 0);
|
|
});
|
|
|
|
test('setCameraInputs toggles Navara camera control and user input cancels flights only while enabled', () => {
|
|
const { canvas, input, cameraOptions, cameraInputs } = setup();
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1));
|
|
canvas.dispatchEvent(new Event('wheel'));
|
|
assert.deepEqual(cameraInputs, ['pointerdown', 'wheel']);
|
|
input.setCameraInputs(false);
|
|
canvas.dispatchEvent(pointer('pointerdown', 1, 1));
|
|
assert.deepEqual(cameraInputs, ['pointerdown', 'wheel']);
|
|
input.setCameraInputs(true);
|
|
assert.deepEqual(cameraOptions, [
|
|
{ enableSpin: false, enableZoom: false, enableTilt: false },
|
|
{ enableSpin: true, enableZoom: true, enableTilt: true },
|
|
]);
|
|
});
|
|
|
|
test('destroy removes every DOM listener and later subscriptions are inert', () => {
|
|
const { canvas, input, destroy } = setup();
|
|
input.on('click', () => {});
|
|
input.on('move', () => {});
|
|
destroy();
|
|
assert.equal(canvas.listenerCount, 0);
|
|
const off = input.on('click', () => {});
|
|
assert.equal(typeof off, 'function');
|
|
assert.equal(canvas.listenerCount, 0);
|
|
});
|
|
|
|
test('setCameraInputs merges into the camera options set through the engine', () => {
|
|
const { input, cameraOptions } = setup();
|
|
input.setCameraOptions({ minimumZoomDistance: 50, zoomSpeed: 2 });
|
|
input.setCameraInputs(false);
|
|
assert.deepEqual(cameraOptions.at(-1), {
|
|
minimumZoomDistance: 50,
|
|
zoomSpeed: 2,
|
|
enableSpin: false,
|
|
enableZoom: false,
|
|
enableTilt: false,
|
|
});
|
|
input.setCameraOptions({ zoomSpeed: 3 });
|
|
assert.deepEqual(cameraOptions.at(-1), {
|
|
minimumZoomDistance: 50,
|
|
zoomSpeed: 3,
|
|
enableSpin: false,
|
|
enableZoom: false,
|
|
enableTilt: false,
|
|
});
|
|
});
|
|
|
|
test('clicks with pick wait for the click pick; moves with pick use the CPU pick once a frame', async () => {
|
|
const canvas = fakeCanvas();
|
|
const frames = [];
|
|
const calls = [];
|
|
const view = { canvas, camera: { set options(value) {} } };
|
|
const { input } = createNavaraInput(view, {
|
|
picker: {
|
|
clickPick: async (at) => (calls.push(['click', at]), { id: 'gpu' }),
|
|
pick: (at) => (calls.push(['cpu', at]), { id: 'cpu' }),
|
|
drillPick: (at, limit) => (
|
|
calls.push(['drill', limit]),
|
|
[{ id: 'a' }, { id: 'b' }]
|
|
),
|
|
},
|
|
scheduleFrame: (run) => frames.push(run),
|
|
});
|
|
const seen = [];
|
|
input.on('click', (event) => seen.push(['click', event]), {
|
|
pick: true,
|
|
drill: 1,
|
|
});
|
|
input.on('move', (event) => seen.push(['move', event]), { pick: true });
|
|
canvas.dispatchEvent(pointer('pointerdown', 110, 60));
|
|
canvas.dispatchEvent(pointer('pointerup', 110, 60));
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
assert.deepEqual(seen[0][1].picked, { id: 'gpu' });
|
|
assert.deepEqual(seen[0][1].drilled, [{ id: 'a' }]);
|
|
canvas.dispatchEvent(pointer('pointermove', 120, 70));
|
|
canvas.dispatchEvent(pointer('pointermove', 121, 71));
|
|
assert.equal(frames.length, 1);
|
|
frames[0]();
|
|
assert.deepEqual(seen.at(-1)[1].picked, { id: 'cpu' });
|
|
assert.deepEqual(calls.at(-1), ['cpu', { x: 21, y: 21 }]);
|
|
});
|