Files
road-compiler/workbench/client/src/edit/ghost-layer.ts
T

149 lines
6.0 KiB
TypeScript

// The `editGhost` source: client-side immediate feedback while a drag is live.
//
// Written to directly, never through React state. The ol-ext probe drove its
// readout from a setState per `translating` event and produced ~1600 renders in
// one session; React is the wrong owner of per-frame feedback. The ghost is also
// not authoritative — it shows where the handle now sits and how far it moved,
// while `editPreview` carries the geometry the server actually solved.
import Feature from 'ol/Feature';
import LineString from 'ol/geom/LineString';
import Point from 'ol/geom/Point';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';
import { coordinateAtStation, fromLonLat, type Coordinate } from './meters';
import { handlePositionFor } from './projection';
import type { IntervalRangeHandle } from './selection';
import type { EditHandle, RoadIntervalAnchor } from './types';
/** The stretch of road an interval edit applies to. */
const BAND = new Style({
stroke: new Stroke({ color: '#29695699', width: 14 }),
});
/**
* Clamped turns the knob amber so "the road cannot go further" reads differently
* from "the drag is still following". Pending dims it while a solve is over
* budget, so a slow answer looks slow rather than broken.
*/
function knob(label: string, clamped: boolean, pending: boolean): Style {
const colour = clamped ? '#d49318' : '#00a5cf';
return new Style({
image: new CircleStyle({
radius: 8,
fill: new Fill({ color: pending ? `${colour}66` : colour }),
stroke: new Stroke({ color: pending ? '#ffffff88' : '#fff', width: 2 }),
}),
text: new Text({
text: pending ? `${label} 求解中…` : label,
offsetY: -18,
font: '600 12px system-ui, sans-serif',
fill: new Fill({ color: pending ? '#59696c' : '#1b2426' }),
stroke: new Stroke({ color: '#ffffffcc', width: 3 }),
}),
});
}
export class EditGhostLayer {
readonly layer: VectorLayer<VectorSource>;
private readonly source = new VectorSource();
private pending = false;
constructor() {
this.layer = new VectorLayer({
source: this.source,
// Above the handles so the live knob is never occluded by the static one.
zIndex: 110,
style: (feature) => {
if (feature.get('band')) return BAND;
return knob(String(feature.get('label') ?? ''), Boolean(feature.get('clamped')), this.pending);
},
});
}
/**
* Shows the handle at the position implied by `value`. `value` is already
* clamped by `projectHandleValue()`, so a ghost pinned at the bound is the
* correct picture: the road cannot go further.
*
* There is deliberately no translucent outline of the resulting road, which
* `research/joint-solver.md` originally asked for. Drawing it accurately means
* recomputing the road surface in the browser — the second geometry algorithm
* the design forbids — and drawing it crudely would be wrong exactly where the
* solver does something non-trivial: transitions, junction boundaries, clamps.
* A preview that lies is worse than none. Since the solve dropped to ~142 ms the
* authoritative geometry arrives fast enough to show the shape itself, so the
* ghost only says what the preview cannot: how far you asked to move, and
* whether you have hit the limit.
*/
show(handle: EditHandle, value: number): void {
// Control markings are dragged from their selected outline, like lane
// maneuvers. The generic scalar knob/readout would reintroduce a confusing
// blue point and "+N 米" label, so let the preview outline be the only feedback.
if (handle.kind === 'junction-crosswalk-inset' || handle.kind === 'junction-stop-line-offset') {
this.pending = false;
this.source.clear();
return;
}
const clamped = value >= handle.value.max - 1e-6 || value <= handle.value.min + 1e-6;
const delta = value - handle.value.current;
this.pending = false;
this.source.clear();
this.source.addFeature(
new Feature({
geometry: new Point(fromLonLat(handlePositionFor(handle, value))),
label: `${delta >= 0 ? '+' : ''}${delta.toFixed(2)} 米`,
clamped,
}),
);
}
/**
* Holds the ghost while a solve is over budget. design.md requires the client to
* keep the ghost and a pending state rather than let the geometry flicker, so a
* degraded response dims the knob in place instead of clearing it.
*/
markPending(): void {
if (this.source.getFeatures().length === 0) return;
this.pending = true;
this.layer.changed();
}
/**
* Shows the interval a range drag is resizing: a band along the road plus its
* length. A pair of dots cannot convey *where* the edit applies, which is the
* whole point of the range handles.
*/
showInterval(range: IntervalRangeHandle, anchor: RoadIntervalAnchor): void {
const span = Math.max(0, anchor.endStation - anchor.startStation);
const steps = 24;
const points: Coordinate[] = [];
for (let index = 0; index <= steps; index += 1) {
const point = coordinateAtStation(range.centerline, anchor.startStation + (span * index) / steps);
if (point) points.push(point);
}
if (points.length < 2) return;
// Pinned when the dragged end has run into the reserve or the other end.
const station = range.end === 'start' ? anchor.startStation : anchor.endStation;
const clamped = station <= range.window.minStation + 1e-6 || station >= range.window.maxStation - 1e-6;
this.source.clear();
this.source.addFeatures([
new Feature({ geometry: new LineString(points.map((point) => fromLonLat(point))), band: true }),
new Feature({
geometry: new Point(fromLonLat(points[range.end === 'start' ? 0 : points.length - 1])),
label: `${(span * range.roadLengthMeters).toFixed(1)} 米`,
clamped,
}),
]);
}
clear(): void {
this.source.clear();
}
}