feat: drag lane turn arrows along their lanes
This commit is contained in:
1 parent
5457f960ff
commit
9cb715a592
12 files changed
+335
-24
No files matched your search
@@ -15,7 +15,7 @@ import {
|
||||
} from './edit/element';
|
||||
import { directEditEnabled, intervalEditingSupported } from './edit/flag';
|
||||
import type { ElementOutline } from './edit/handle-layer';
|
||||
import type { Coordinate } from './edit/meters';
|
||||
import { azimuthBetween, signedMetersAlongAxis, type Coordinate } from './edit/meters';
|
||||
import { rendersSame } from './edit/parameter';
|
||||
import { PreviewRequester, type PreviewDraft } from './edit/preview-request';
|
||||
import { anchorSnapshotFor, constraintValueFor, draftConstraint, operationFor } from './edit/projection';
|
||||
@@ -212,6 +212,23 @@ function App() {
|
||||
}) ?? [],
|
||||
);
|
||||
}, [element, state]);
|
||||
const laneSelectionFeatures = useMemo(() => {
|
||||
if (!element || element.kind !== 'lane-maneuver' || !state) return [];
|
||||
const ids = new Set(element.featureIds);
|
||||
return (
|
||||
state.layers.turnArrows?.features.filter((feature) => {
|
||||
const nativeId = feature.properties?.native_id;
|
||||
return typeof nativeId === 'string' && ids.has(nativeId);
|
||||
}) ?? []
|
||||
);
|
||||
}, [element, state]);
|
||||
const laneSelectionAxisAzimuth = useMemo(() => {
|
||||
if (!element || element.kind !== 'lane-maneuver' || !state) return null;
|
||||
const road = state.compiled.model.roads.find((item) => item.id === element.roadId);
|
||||
const previous = road?.centerline.at(-2);
|
||||
const end = road?.centerline.at(-1);
|
||||
return previous && end ? azimuthBetween(previous, end) : null;
|
||||
}, [element, state]);
|
||||
const sessionRef = useRef<EditSession | null>(null);
|
||||
if (directEditEnabled && !sessionRef.current) sessionRef.current = new EditSession();
|
||||
// Per-frame drag feedback reaches the panel through this rather than through
|
||||
@@ -635,6 +652,27 @@ function App() {
|
||||
ranges={editRanges}
|
||||
element={elementOutline}
|
||||
laneSelectionBounds={laneSelectionBounds}
|
||||
laneSelectionFeatures={laneSelectionFeatures}
|
||||
laneSelectionAxisAzimuth={laneSelectionAxisAzimuth}
|
||||
onLaneSelectionDragEnd={(from, to) => {
|
||||
if (!element || element.kind !== 'lane-maneuver' || element.placementDistanceMeters === undefined) return;
|
||||
const road = state.compiled.model.roads.find((item) => item.id === element.roadId);
|
||||
const previous = road?.centerline.at(-2);
|
||||
const end = road?.centerline.at(-1);
|
||||
if (!previous || !end) return;
|
||||
const deltaTowardJunction = signedMetersAlongAxis(from, to, azimuthBetween(previous, end));
|
||||
const existing = [...state.overrides.overrides, ...staged]
|
||||
.reverse()
|
||||
.find((item) => item.kind === 'lane-maneuver' && item.laneId === element.laneId);
|
||||
stage({
|
||||
id: existing?.id || `车道转向:${element.laneId}`,
|
||||
kind: 'lane-maneuver',
|
||||
laneId: element.laneId,
|
||||
maneuver: typeof existing?.maneuver === 'string' ? existing.maneuver : element.maneuver,
|
||||
placementDistanceMeters: Math.max(0, element.placementDistanceMeters - deltaTowardJunction),
|
||||
});
|
||||
setStatus('箭头位置已暂存;保存并重新生成后应用。');
|
||||
}}
|
||||
preview={preview}
|
||||
ghostPending={session?.degraded ?? false}
|
||||
onHandleDrag={(handle, value) => {
|
||||
@@ -967,7 +1005,15 @@ function LaneManeuverEditor({
|
||||
<select
|
||||
value={maneuver}
|
||||
onChange={(event) =>
|
||||
stage({ id, kind: 'lane-maneuver', laneId: element.laneId, maneuver: event.target.value })
|
||||
stage({
|
||||
id,
|
||||
kind: 'lane-maneuver',
|
||||
laneId: element.laneId,
|
||||
maneuver: event.target.value,
|
||||
...(typeof override?.placementDistanceMeters === 'number'
|
||||
? { placementDistanceMeters: override.placementDistanceMeters }
|
||||
: {}),
|
||||
})
|
||||
}
|
||||
>
|
||||
{MANEUVER_OPTIONS.map(([value, label]) => (
|
||||
|
||||
@@ -4,6 +4,7 @@ import Map from 'ol/Map';
|
||||
import Polygon from 'ol/geom/Polygon';
|
||||
import View from 'ol/View';
|
||||
import Select from 'ol/interaction/Select';
|
||||
import PointerInteraction from 'ol/interaction/Pointer';
|
||||
import VectorLayer from 'ol/layer/Vector';
|
||||
import VectorSource from 'ol/source/Vector';
|
||||
import Fill from 'ol/style/Fill';
|
||||
@@ -17,7 +18,7 @@ import { createHandleDragInteraction } from '../edit/drag-interaction';
|
||||
import { EditGhostLayer } from '../edit/ghost-layer';
|
||||
import { EditHandleLayer, type ElementOutline } from '../edit/handle-layer';
|
||||
import { EditPreviewLayer } from '../edit/preview-layer';
|
||||
import { fromLonLat, type Coordinate } from '../edit/meters';
|
||||
import { fromLonLat, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
|
||||
import type { IntervalRangeHandle } from '../edit/selection';
|
||||
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
||||
|
||||
@@ -38,6 +39,10 @@ interface Props {
|
||||
element?: ElementOutline | null;
|
||||
/** A display-only outline for the selected v1 turn arrow. */
|
||||
laneSelectionBounds?: Coordinate[] | null;
|
||||
/** Polygon parts copied to the drag feedback layer; baseline parts are hidden by id. */
|
||||
laneSelectionFeatures?: GeoJson['features'];
|
||||
laneSelectionAxisAzimuth?: number | null;
|
||||
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
||||
/** Authoritative preview geometry; null returns the map to the baseline. */
|
||||
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
||||
/** Live value during a drag — the caller debounces the preview request. */
|
||||
@@ -63,6 +68,9 @@ export function MapCanvas({
|
||||
ranges,
|
||||
element,
|
||||
laneSelectionBounds,
|
||||
laneSelectionFeatures,
|
||||
laneSelectionAxisAzimuth,
|
||||
onLaneSelectionDragEnd,
|
||||
preview,
|
||||
onHandleDrag,
|
||||
onHandleDragEnd,
|
||||
@@ -78,6 +86,8 @@ export function MapCanvas({
|
||||
const ghostRef = useRef<EditGhostLayer | null>(null);
|
||||
const previewRef = useRef<EditPreviewLayer | null>(null);
|
||||
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
|
||||
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
|
||||
const hiddenMarkingIdsRef = useRef<ReadonlySet<string>>(new Set());
|
||||
const visibleRef = useRef(visible);
|
||||
visibleRef.current = visible;
|
||||
// Callbacks live in refs so the map is built once and never rebuilt when a
|
||||
@@ -85,8 +95,26 @@ export function MapCanvas({
|
||||
// the listener is registered once, so without a ref it would keep calling the
|
||||
// first render's closure — harmless while those only called stable setters,
|
||||
// but not once one of them reads current state.
|
||||
const dragRef = useRef({ onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd, ghostPending });
|
||||
dragRef.current = { onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd, ghostPending };
|
||||
const dragRef = useRef({
|
||||
onHandleDrag,
|
||||
onHandleDragEnd,
|
||||
onHandleBlocked,
|
||||
onRangeDrag,
|
||||
onRangeEnd,
|
||||
onLaneSelectionDragEnd,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
});
|
||||
dragRef.current = {
|
||||
onHandleDrag,
|
||||
onHandleDragEnd,
|
||||
onHandleBlocked,
|
||||
onRangeDrag,
|
||||
onRangeEnd,
|
||||
onLaneSelectionDragEnd,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
};
|
||||
const selectRef = useRef({ onSelectRoad, onFeature, onBackground });
|
||||
selectRef.current = { onSelectRoad, onFeature, onBackground };
|
||||
const selectedRef = useRef<Road | null>(selected);
|
||||
@@ -95,11 +123,17 @@ export function MapCanvas({
|
||||
selectedRef.current = selected;
|
||||
stateRef.current = state;
|
||||
sceneRef.current = scene;
|
||||
hiddenMarkingIdsRef.current = new Set(
|
||||
(laneSelectionFeatures ?? [])
|
||||
.map((feature) => feature.properties?.native_id)
|
||||
.filter((id): id is string => typeof id === 'string'),
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!target.current) return;
|
||||
const layers = createLayers(
|
||||
() => selectedRef.current,
|
||||
() => sceneRef.current,
|
||||
() => hiddenMarkingIdsRef.current,
|
||||
);
|
||||
layersRef.current = layers;
|
||||
const laneSelectionSource = new VectorSource();
|
||||
@@ -108,7 +142,7 @@ export function MapCanvas({
|
||||
zIndex: 90,
|
||||
style: new Style({
|
||||
stroke: new Stroke({ color: '#d49318', width: 2, lineDash: [6, 4] }),
|
||||
fill: new Fill({ color: 'rgba(212, 147, 24, 0.12)' }),
|
||||
fill: new Fill({ color: '#f5f6ee' }),
|
||||
}),
|
||||
});
|
||||
laneSelectionSourceRef.current = laneSelectionSource;
|
||||
@@ -146,6 +180,47 @@ export function MapCanvas({
|
||||
style: null,
|
||||
});
|
||||
map.addInteraction(select);
|
||||
let laneDragOrigin: Coordinate | null = null;
|
||||
let laneDragFeatures: Feature[] = [];
|
||||
map.addInteraction(
|
||||
new PointerInteraction({
|
||||
handleDownEvent: (event) => {
|
||||
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
||||
layerFilter: (layer) => layer === laneSelectionLayer,
|
||||
hitTolerance: 10,
|
||||
});
|
||||
if (!hit) return false;
|
||||
laneDragOrigin = event.coordinate as Coordinate;
|
||||
laneDragFeatures = laneSelectionFeaturesRef.current.map((feature) => feature.clone());
|
||||
return true;
|
||||
},
|
||||
handleDragEvent: (event) => {
|
||||
const axis = dragRef.current.laneSelectionAxisAzimuth;
|
||||
if (!laneDragOrigin || axis === null || axis === undefined || !laneDragFeatures.length) return;
|
||||
const origin = fromLonLat(toLonLat(laneDragOrigin));
|
||||
const moved = fromLonLat(
|
||||
offsetCoordinate(
|
||||
toLonLat(laneDragOrigin),
|
||||
axis,
|
||||
signedMetersAlongAxis(laneDragOrigin, event.coordinate as Coordinate, axis),
|
||||
),
|
||||
);
|
||||
const dx = moved[0] - origin[0];
|
||||
const dy = moved[1] - origin[1];
|
||||
laneSelectionFeaturesRef.current.forEach((feature, index) => {
|
||||
const geometry = laneDragFeatures[index]?.getGeometry()?.clone();
|
||||
geometry?.translate(dx, dy);
|
||||
feature.setGeometry(geometry);
|
||||
});
|
||||
},
|
||||
handleUpEvent: (event) => {
|
||||
if (laneDragOrigin) dragRef.current.onLaneSelectionDragEnd?.(laneDragOrigin, event.coordinate as Coordinate);
|
||||
laneDragOrigin = null;
|
||||
laneDragFeatures = [];
|
||||
return false;
|
||||
},
|
||||
}),
|
||||
);
|
||||
if (handleLayer && ghost) {
|
||||
map.addInteraction(
|
||||
createHandleDragInteraction({
|
||||
@@ -211,13 +286,24 @@ export function MapCanvas({
|
||||
const source = laneSelectionSourceRef.current;
|
||||
if (!source) return;
|
||||
source.clear();
|
||||
if (laneSelectionBounds && laneSelectionBounds.length >= 3)
|
||||
const parts = (laneSelectionFeatures ?? []).flatMap((feature) => {
|
||||
const coordinates = feature.geometry?.coordinates;
|
||||
if (feature.geometry?.type !== 'Polygon' || !Array.isArray(coordinates) || !Array.isArray(coordinates[0]))
|
||||
return [];
|
||||
const ring = coordinates[0] as Coordinate[];
|
||||
return [new Feature({ geometry: new Polygon([ring.map(fromLonLat)]) })];
|
||||
});
|
||||
if (parts.length) source.addFeatures(parts);
|
||||
else if (laneSelectionBounds && laneSelectionBounds.length >= 3)
|
||||
source.addFeature(
|
||||
new Feature({
|
||||
geometry: new Polygon([[...laneSelectionBounds, laneSelectionBounds[0]].map(fromLonLat)]),
|
||||
}),
|
||||
new Feature({ geometry: new Polygon([[...laneSelectionBounds, laneSelectionBounds[0]].map(fromLonLat)]) }),
|
||||
);
|
||||
}, [laneSelectionBounds]);
|
||||
laneSelectionFeaturesRef.current = parts;
|
||||
}, [laneSelectionBounds, laneSelectionFeatures]);
|
||||
useEffect(() => {
|
||||
// Selection changes only affect presentation; compiler GeoJSON stays untouched.
|
||||
layersRef.current?.markings.changed();
|
||||
}, [laneSelectionFeatures]);
|
||||
useEffect(() => {
|
||||
// A non-degraded answer means the pending ghost has been superseded by real
|
||||
// geometry. Clearing an empty ghost is a no-op, so this is safe mid-drag.
|
||||
|
||||
@@ -118,6 +118,7 @@ export interface LaneManeuverSelection {
|
||||
laneId: string;
|
||||
/** The current rendered maneuver; empty when the lane has no turn arrow. */
|
||||
maneuver: string;
|
||||
placementDistanceMeters?: number;
|
||||
roadId: string;
|
||||
featureIds: string[];
|
||||
}
|
||||
@@ -203,6 +204,8 @@ export function elementSelectionFor(
|
||||
kind: 'lane-maneuver',
|
||||
laneId,
|
||||
maneuver,
|
||||
placementDistanceMeters:
|
||||
typeof first.placement_distance_meters === 'number' ? first.placement_distance_meters : undefined,
|
||||
roadId,
|
||||
featureIds: members
|
||||
.map((member) => text(member.properties?.native_id))
|
||||
|
||||
@@ -28,7 +28,11 @@ export type LayerName =
|
||||
| 'connectors'
|
||||
| 'diagnostics'
|
||||
| 'selectedRoad';
|
||||
export function createLayers(onRoad: () => Road | null, scene: () => boolean) {
|
||||
export function createLayers(
|
||||
onRoad: () => Road | null,
|
||||
scene: () => boolean,
|
||||
hiddenMarkingIds: () => ReadonlySet<string> = () => new Set(),
|
||||
) {
|
||||
const simple = (color: string, width = 1) =>
|
||||
new Style({ fill: new Fill({ color: `${color}55` }), stroke: new Stroke({ color, width }) });
|
||||
const layers: Record<LayerName, VectorLayer<VectorSource>> = {
|
||||
@@ -66,7 +70,10 @@ export function createLayers(onRoad: () => Road | null, scene: () => boolean) {
|
||||
}),
|
||||
markings: new VectorLayer({
|
||||
source: source(),
|
||||
style: new Style({ fill: new Fill({ color: '#f5f6ee' }), stroke: new Stroke({ color: '#d9dacf', width: 1 }) }),
|
||||
style: (feature) =>
|
||||
hiddenMarkingIds().has(String(feature.get('native_id')))
|
||||
? undefined
|
||||
: new Style({ fill: new Fill({ color: '#f5f6ee' }), stroke: new Stroke({ color: '#d9dacf', width: 1 }) }),
|
||||
}),
|
||||
centerLines: new VectorLayer({
|
||||
source: source(),
|
||||
|
||||
Reference in new issue
Block a user