Files
gods-eye-view/src/globe/navara/input.js
T
German Khokhlov 4e195dac6b fix(globe): the Navara follow orbits on drag and zooms to the floor
Review of Task 5: while following on Navara a left drag panned the globe
about the ground point under the cursor (range 2,154 m → 54,136 m) and the
wheel zoomed toward that point (462 m after 40 notches, Cesium stops at
the 150 m floor). While a follow runs, Navara's spin, zoom and tilt are
now suspended (input.suspendCameraInputs, which gives the app's own
setCameraInputs choice back after the release) and followGestures.js turns
a drag into heading and pitch of the ENU offset (a canvas width is one
turn, its height half a turn; elevation kept within 2°-89°) and a wheel
notch into 20 % of its range, never inside the minimum range, as Cesium's
EntityView orbits the target. The offset is no longer read back from a
camera the follow alone moves.

Also (review item 7): a follow requests a render only when the target, the
offset or a gesture changed, so a still target lets Navara idle.

The contract follow check drags (range within ±5 %, target within 3 px)
and turns the wheel 40 notches (never below 149 m, ends within 5 % of
150 m) on both engines with real input (qa-globe-contract's pointer gains
drag and wheel); after stop it waits for Cesium's gesture inertia to end
before checking that the camera is free. Contract: Cesium 73/73, Navara
79/79.
2026-10-16 20:57:10 +04:00

183 lines
5.8 KiB
JavaScript

// Screen input for the Navara engine, from DOM pointer events on Navara's
// canvas, with Cesium's ScreenSpaceEventHandler semantics: left button only,
// a click is a down and up within 5 px, positions are CSS px from the
// canvas's top-left corner, and a listener with a modifier fires only while
// that key is held (shift over ctrl over alt), one without only while none is.
//
// Camera inputs map to Navara's controller flags (view.camera.options, S4).
// Navara 0.2.1 lets a flyTo run on under user input, where Cesium cancels it;
// `onCameraInput` reports user camera input so the engine can do the same.
import { INPUT_TYPES, normalizeInputOptions } from '../contract.js';
import { createPickDelivery } from '../pickDelivery.js';
const CLICK_PIXEL_TOLERANCE = 5; // Cesium's _clickPixelTolerance
const SCREEN_EVENTS = ['pointerdown', 'pointerup', 'pointermove', 'dblclick'];
const CAMERA_EVENTS = ['pointerdown', 'wheel', 'touchstart'];
function modifierOf(event) {
if (event.shiftKey) return 'shift';
if (event.ctrlKey) return 'ctrl';
if (event.altKey) return 'alt';
return undefined;
}
/**
* @param {{ canvas: HTMLCanvasElement, camera: { options: object } }} view
* @param {{ onCameraInput?: (type: string) => void,
* picker?: { clickPick: Function, pick: Function, drillPick: Function },
* scheduleFrame?: (run: () => void) => void }} [options] `picker`
* answers listeners with { pick, drill } (picking.js): clicks from the
* GPU pick of the gesture, moves from the CPU pick once a frame.
* @returns {{ input: object, destroy: () => void }}
*/
export function createNavaraInput(
view,
{
onCameraInput = () => {},
picker = null,
scheduleFrame = (run) => requestAnimationFrame(run),
} = {},
) {
const { canvas } = view;
const listeners = new Map(INPUT_TYPES.map((type) => [type, new Set()]));
let attached = false;
let destroyed = false;
let cameraInputs = true;
let cameraOptions = {};
let downAt = null;
let last = null;
const delivery = createPickDelivery({
pick: (position, type) =>
type === 'move' ? picker?.pick(position) : picker?.clickPick(position),
drill: (position, limit) => picker?.drillPick(position, limit) ?? [],
schedule: (run) => scheduleFrame(() => !destroyed && run()),
});
const positionOf = (event) => {
const rect = canvas.getBoundingClientRect();
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
};
const emit = (type, event, payload) => {
const modifier = modifierOf(event);
const group = listeners.get(type);
delivery.deliver(
type,
payload,
[...group].filter((entry) => entry.modifier === modifier),
{ isLive: (entry) => group.has(entry) },
);
};
const onScreenEvent = (event) => {
const position = positionOf(event);
switch (event.type) {
case 'pointerdown':
if (event.button !== 0) break;
downAt = position;
emit('down', event, { position });
break;
case 'pointerup': {
if (event.button !== 0) break;
emit('up', event, { position });
const start = downAt;
downAt = null;
if (
start &&
Math.hypot(position.x - start.x, position.y - start.y) <
CLICK_PIXEL_TOLERANCE
)
emit('click', event, { position });
break;
}
case 'pointermove':
emit('move', event, {
startPosition: last ?? position,
endPosition: position,
});
break;
case 'dblclick':
emit('doubleClick', event, { position });
break;
}
last = position;
};
const onCameraEvent = (event) => {
if (cameraInputs) onCameraInput(event.type);
};
for (const type of CAMERA_EVENTS)
canvas.addEventListener(type, onCameraEvent, {
capture: true,
passive: true,
});
let suspended = 0;
const sendCameraInputs = () => {
const enabled = cameraInputs && suspended === 0;
input.setCameraOptions({
enableSpin: enabled,
enableZoom: enabled,
enableTilt: enabled,
});
};
const input = {
on(type, listener, options = {}) {
const { modifier, pick, drill } = normalizeInputOptions(type, options);
if (destroyed) return () => {};
if (!attached) {
attached = true;
for (const name of SCREEN_EVENTS)
canvas.addEventListener(name, onScreenEvent);
}
const entry = { listener, modifier, pick, drill };
listeners.get(type).add(entry);
return () => {
listeners.get(type).delete(entry);
};
},
setCameraInputs(enabled) {
cameraInputs = Boolean(enabled);
sendCameraInputs();
},
/**
* Navara-only: Navara's spin, zoom and tilt off until the returned
* release (the follow camera takes drag and wheel itself); the app's
* setCameraInputs choice comes back after the last release.
*/
suspendCameraInputs() {
suspended++;
sendCameraInputs();
let released = false;
return () => {
if (released) return;
released = true;
suspended--;
sendCameraInputs();
};
},
/**
* Navara-only extension: camera controller options (zoom distances,
* speeds, ...). Navara has no getter, so the engine keeps every option
* set through it and always sends the merged set.
*/
setCameraOptions(options) {
cameraOptions = { ...cameraOptions, ...options };
view.camera.options = { ...cameraOptions };
},
};
return {
input,
destroy() {
if (destroyed) return;
destroyed = true;
for (const type of CAMERA_EVENTS)
canvas.removeEventListener(type, onCameraEvent, { capture: true });
if (attached)
for (const name of SCREEN_EVENTS)
canvas.removeEventListener(name, onScreenEvent);
attached = false;
for (const set of listeners.values()) set.clear();
},
};
}