diff --git a/src/ui/cockpitDoubleClick.test.mjs b/src/ui/cockpitDoubleClick.test.mjs index d65af9c40..661ee434f 100644 --- a/src/ui/cockpitDoubleClick.test.mjs +++ b/src/ui/cockpitDoubleClick.test.mjs @@ -13,7 +13,11 @@ import { import { bindRenderer } from '../render/rendererOf.js'; import { entityFollowTarget } from '../data/followed.js'; import { createCesiumInput } from '../render/cesium/input.js'; -import { routeSceneDoubleClick, onSceneDoubleClick } from './cockpitInput.js'; +import { + routeSceneDoubleClick, + onSceneDoubleClick, + onScenePointerDown, +} from './cockpitInput.js'; import { inputFor } from '../input/dispatcher.js'; function harness(t, { stock = null } = {}) { @@ -24,8 +28,9 @@ function harness(t, { stock = null } = {}) { }, clock: { currentTime: 0 }, }; - // The followed aircraft is drawn at (100, 100). - const project = () => ({ x: 100, y: 100 }); + // The followed aircraft is drawn at (100, 100), unless a test moves it. + let drawnAt = { x: 100, y: 100 }; + const project = () => drawnAt; // With `stock`, the renderer's own input port takes the viewer's stock // double-click follow and runs it as the app's does; else the fake input. // The camera port follows the aircraft, as the flights layer's follow does. @@ -73,7 +78,14 @@ function harness(t, { stock = null } = {}) { stop(); input.destroy(); }); - return { input, controller, stop }; + return { + input, + controller, + stop, + moveTo: (at) => { + drawnAt = at; + }, + }; } /** The Cesium renderer's input port over a fake handler, with `follow` as @@ -249,3 +261,34 @@ test('an active Cockpit leaves presses alone, and stopping lets a hold go', (t) stop(); assert.equal(controller.holds, 0); }); + +test('a ship moved by the first click still enters, with the stock follow held where it was', async (t) => { + const follows = []; + const { input, controller, moveTo } = harness(t, { + stock: (event) => follows.push(event.position), + }); + controller.resolveEntryKind = () => 'vessel'; + controller.readVesselInfo = () => ({ + id: 'ship-1', + latitude: 37.8, + longitude: -122.4, + }); + const was = { x: 100, y: 100 }; + // The first press notes where the ship is drawn; its click re-centres the + // camera, and the ship is drawn elsewhere by the second press. + onScenePointerDown.call(controller); + input.tap(was); + moveTo({ x: 300, y: 100 }); + input.down(was); + assert.equal( + input.stockEnabled('dblclickFollow'), + false, + 'held at the press where the ship was', + ); + input.up(was); + input.dblclick(was); + assert.equal(controller.entered, 1); + assert.equal(follows.length, 0, 'the stock follow never ran'); + await Promise.resolve(); + assert.equal(input.stockEnabled('dblclickFollow'), true); +}); diff --git a/src/ui/cockpitInput.js b/src/ui/cockpitInput.js index 0bfbaf808..0ac353dc5 100644 --- a/src/ui/cockpitInput.js +++ b/src/ui/cockpitInput.js @@ -96,12 +96,26 @@ export function onScenePointerDown(nowMs = performance.now()) { * refused by the gate: the caller does nothing else with it. */ export function onSceneDoubleClick(position, nowMs = performance.now()) { - const start = this.doubleClickStart; + const hit = hitsFollowedTrack.call(this, position, nowMs); this.doubleClickStart = null; if (this.active || this.destroyed || !isPointerFree()) return false; if (!Number.isFinite(position?.x) || !Number.isFinite(position?.y)) return false; - const hit = + if (!hit) return false; + this.enter(); + return true; +} + +/** + * Whether `position` is on the followed track: where it is drawn now, or + * where a ship was drawn as this gesture's first press began (its click + * may have moved it). + */ +function hitsFollowedTrack(position, nowMs) { + if (!Number.isFinite(position?.x) || !Number.isFinite(position?.y)) + return false; + const start = this.doubleClickStart; + return ( doubleClickHitsSubject(position, subjectCanvasPosition.call(this)) || (Boolean(start) && nowMs - start.atMs <= DOUBLE_CLICK_MEMORY_MS && @@ -109,10 +123,8 @@ export function onSceneDoubleClick(position, nowMs = performance.now()) { // aircraft, another ship) is hit only where it is drawn. this.resolveEntryKind() === 'vessel' && start.vesselId === (this.readVesselInfo()?.id ?? null) && - doubleClickHitsSubject(position, start)); - if (!hit) return false; - this.enter(); - return true; + doubleClickHitsSubject(position, start)) + ); } /** Hold the renderer's own double-click follow off; returns the release. */ @@ -178,7 +190,9 @@ export function routeSceneDoubleClick() { isPointerFree() && (this.active ? Boolean(this.orbitalSiteUnder?.(position)) - : doubleClickHitsSubject(position, subjectCanvasPosition.call(this))); + : // Where the ship was as the gesture began counts too: the first + // click may have moved it, and the double-click still enters. + hitsFollowedTrack.call(this, position, performance.now())); endsDouble = !!last?.onSubject && at - last.at <= SUBJECT_PRESS_HOLD_MS &&