// Dragging a handle, on native OpenLayers. // // Two things the ol-ext probe settled are baked in here: // // 1. The handle position is derived from the clamped constraint value, never from // the raw pointer delta. `Transform` translated its proxy by the delta, so a // drag reading -24.1 m left the handle 24 m out while the constraint clamped at // -5.4 m. `EditGhostLayer.show()` takes the value, not the cursor. // 2. Hit-testing happens once, on pointerdown. ol-ext ran `forEachFeatureAtPixel` // from `handleMoveEvent_`, i.e. a canvas readback every mousemove. A drag only // needs to know what it grabbed at the start. // // Two kinds of handle share the gesture: a value handle moves the constraint's // number, a range end moves the interval its anchor covers. They are branched here // rather than in two interactions so only one of them can ever own a pointer. // // Baseline layers are never touched: this reads the handle layer and writes only // to the ghost source. import PointerInteraction from 'ol/interaction/Pointer'; import type MapBrowserEvent from 'ol/MapBrowserEvent'; import type VectorLayer from 'ol/layer/Vector'; import type VectorSource from 'ol/source/Vector'; import type { EditGhostLayer } from './ghost-layer'; import { HANDLE_ID } from './handle-layer'; import type { Coordinate } from './meters'; import { projectHandleValue, projectIntervalEnd } from './projection'; import type { IntervalRangeHandle } from './selection'; import type { EditHandle, RoadIntervalAnchor } from './types'; /** * OpenLayers hands every pointer handler the same widened event: the union covers * keyboard and wheel because `PointerInteraction` shares one dispatch path. The * fields used here — `map`, `pixel`, `coordinate` — exist on all of them. */ type MapPointerEvent = MapBrowserEvent; type Active = | { kind: 'value'; handle: EditHandle; origin: Coordinate } | { kind: 'range'; range: IntervalRangeHandle; origin: Coordinate }; export interface HandleDragOptions { /** The `editHandles` layer, the only layer hit-tested. */ layer: VectorLayer; ghost: EditGhostLayer; /** Manifest lookup by handle id; handles carry nothing but their id. */ handleAt: (handleId: string) => EditHandle | undefined; /** Range-end lookup, for the anchor-moving handles. */ rangeAt: (handleId: string) => IntervalRangeHandle | undefined; /** Live value during a value drag. Callers debounce the preview request. */ onDrag: (handle: EditHandle, value: number) => void; /** Final value on pointerup. Callers send this one without debouncing. */ onEnd: (handle: EditHandle, value: number) => void; /** Live interval during a range drag. */ onRangeDrag: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void; onRangeEnd: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void; /** A reserve handle refused the drag, with the manifest's reason. */ onBlocked?: (handle: EditHandle, reason: string) => void; /** * True when the last solve came back `degraded`. design.md requires the ghost to * stay with a pending state rather than clearing and letting the geometry * flicker while a slow answer is still on the way. */ holdGhost?: () => boolean; } export function createHandleDragInteraction(options: HandleDragOptions): PointerInteraction { let active: Active | null = null; const intervalAt = (state: Extract, to: Coordinate) => projectIntervalEnd( state.range.anchor, state.range.end, state.origin, to, state.range.tangentAzimuth, state.range.roadLengthMeters, state.range.window, ); return new PointerInteraction({ handleDownEvent: (event: MapPointerEvent) => { const feature = event.map.forEachFeatureAtPixel(event.pixel, (candidate) => candidate, { layerFilter: (layer) => layer === options.layer, hitTolerance: 10, }); if (!feature) return false; const id = String(feature.get(HANDLE_ID)); const origin = event.coordinate as Coordinate; const range = options.rangeAt(id); if (range) { active = { kind: 'range', range, origin }; options.ghost.showInterval(range, range.anchor); return true; } const handle = options.handleAt(id); if (!handle) return false; if (!handle.editable) { // Explain, then decline the gesture so the map still pans. Swallowing it // would make a reserve handle feel broken rather than owned elsewhere. options.onBlocked?.(handle, handle.disabledReason || '该手柄不可拖动。'); return false; } active = { kind: 'value', handle, origin }; options.ghost.show(handle, handle.value.current); return true; }, handleDragEvent: (event: MapPointerEvent) => { const state = active; if (!state) return; const to = event.coordinate as Coordinate; if (state.kind === 'range') { const anchor = intervalAt(state, to); options.ghost.showInterval(state.range, anchor); options.onRangeDrag(state.range, anchor); return; } const value = projectHandleValue(state.handle, state.origin, to); options.ghost.show(state.handle, value); options.onDrag(state.handle, value); }, handleUpEvent: (event: MapPointerEvent) => { const state = active; active = null; if (!state) return false; const to = event.coordinate as Coordinate; // The final request is not debounced: whatever the pointer settled on is the // value the user meant, and it must not be dropped by a pending timer. if (state.kind === 'range') options.onRangeEnd(state.range, intervalAt(state, to)); else options.onEnd(state.handle, projectHandleValue(state.handle, state.origin, to)); if (options.holdGhost?.()) options.ghost.markPending(); else options.ghost.clear(); return false; }, }); }