feat(workbench): add traffic signal rotation handle
This commit is contained in:
1 parent
9b7c3d59bd
commit
caa3cb843f
4 files changed
+234
-23
No files matched your search
@@ -1,12 +1,15 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import Feature from 'ol/Feature';
|
||||
import LineString from 'ol/geom/LineString';
|
||||
import Map from 'ol/Map';
|
||||
import Point from 'ol/geom/Point';
|
||||
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 CircleStyle from 'ol/style/Circle';
|
||||
import Fill from 'ol/style/Fill';
|
||||
import Stroke from 'ol/style/Stroke';
|
||||
import Style from 'ol/style/Style';
|
||||
@@ -18,6 +21,7 @@ import { EditGhostLayer } from '../edit/ghost-layer';
|
||||
import { EditHandleLayer, type ElementOutline } from '../edit/handle-layer';
|
||||
import { EditPreviewLayer } from '../edit/preview-layer';
|
||||
import { fromLonLat, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
|
||||
import { azimuthBetween } from '../edit/meters';
|
||||
import type { IntervalRangeHandle } from '../edit/selection';
|
||||
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
||||
|
||||
@@ -49,6 +53,10 @@ interface Props {
|
||||
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
||||
onSignalDrag?: (signalUid: string, coordinate: Coordinate) => void;
|
||||
onSignalDragEnd?: (signalUid: string, coordinate: Coordinate) => void;
|
||||
/** The selected signal gets a temporary pivot marker and rotate handle. */
|
||||
signalSelectionUid?: string | null;
|
||||
onSignalRotate?: (signalUid: string, mastHeading: number) => void;
|
||||
onSignalRotateEnd?: (signalUid: string, mastHeading: number) => 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. */
|
||||
@@ -83,6 +91,9 @@ export function MapCanvas({
|
||||
onLaneSelectionDragEnd,
|
||||
onSignalDrag,
|
||||
onSignalDragEnd,
|
||||
signalSelectionUid,
|
||||
onSignalRotate,
|
||||
onSignalRotateEnd,
|
||||
preview,
|
||||
onHandleDrag,
|
||||
onHandleDragEnd,
|
||||
@@ -102,6 +113,8 @@ export function MapCanvas({
|
||||
const fittedAreaIdRef = useRef<string | null>(null);
|
||||
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
|
||||
const laneSelectionLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
||||
const signalTransformSourceRef = useRef<VectorSource | null>(null);
|
||||
const signalTransformLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
||||
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
|
||||
const laneSelectionAnchorRef = useRef<Coordinate | null>(null);
|
||||
const hiddenMarkingIdsRef = useRef<ReadonlySet<string>>(new Set());
|
||||
@@ -122,6 +135,8 @@ export function MapCanvas({
|
||||
onLaneSelectionDrag,
|
||||
onSignalDrag,
|
||||
onSignalDragEnd,
|
||||
onSignalRotate,
|
||||
onSignalRotateEnd,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
});
|
||||
@@ -135,6 +150,8 @@ export function MapCanvas({
|
||||
onLaneSelectionDrag,
|
||||
onSignalDrag,
|
||||
onSignalDragEnd,
|
||||
onSignalRotate,
|
||||
onSignalRotateEnd,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
};
|
||||
@@ -177,6 +194,26 @@ export function MapCanvas({
|
||||
});
|
||||
laneSelectionSourceRef.current = laneSelectionSource;
|
||||
laneSelectionLayerRef.current = laneSelectionLayer;
|
||||
const signalTransformSource = new VectorSource();
|
||||
const signalTransformLayer = new VectorLayer({
|
||||
source: signalTransformSource,
|
||||
zIndex: 95,
|
||||
style: (feature) => {
|
||||
const component = feature.get('signal_transform');
|
||||
if (component === 'crosshair') return new Style({ stroke: new Stroke({ color: '#0e7490', width: 2 }) });
|
||||
if (component === 'rotate-leader')
|
||||
return new Style({ stroke: new Stroke({ color: '#2563eb', width: 1.5, lineDash: [4, 3] }) });
|
||||
return new Style({
|
||||
image: new CircleStyle({
|
||||
radius: 8,
|
||||
fill: new Fill({ color: '#2563eb' }),
|
||||
stroke: new Stroke({ color: '#ffffff', width: 2 }),
|
||||
}),
|
||||
});
|
||||
},
|
||||
});
|
||||
signalTransformSourceRef.current = signalTransformSource;
|
||||
signalTransformLayerRef.current = signalTransformLayer;
|
||||
// Direct-edit sources are always installed; visibility and hit testing follow
|
||||
// the in-page browser/edit switch.
|
||||
const handleLayer = new EditHandleLayer();
|
||||
@@ -194,6 +231,7 @@ export function MapCanvas({
|
||||
...Object.values(layers),
|
||||
...(previewLayer ? previewLayer.all() : []),
|
||||
laneSelectionLayer,
|
||||
signalTransformLayer,
|
||||
...(handleLayer ? [handleLayer.layer] : []),
|
||||
...(ghost ? [ghost.layer] : []),
|
||||
],
|
||||
@@ -204,31 +242,62 @@ export function MapCanvas({
|
||||
condition: click,
|
||||
// Handles are not selectable features: clicking one must not re-target the
|
||||
// road inspector, and dragging is owned by the edit interaction.
|
||||
layers: (layer) => layer !== layers.selectedRoad && layer !== handleLayer?.layer && layer !== laneSelectionLayer,
|
||||
layers: (layer) =>
|
||||
layer !== layers.selectedRoad &&
|
||||
layer !== handleLayer?.layer &&
|
||||
layer !== laneSelectionLayer &&
|
||||
layer !== signalTransformLayer,
|
||||
hitTolerance: 12,
|
||||
style: null,
|
||||
});
|
||||
map.addInteraction(select);
|
||||
let signalUid: string | null = null;
|
||||
let rotatePivot: Coordinate | null = null;
|
||||
map.addInteraction(
|
||||
new PointerInteraction({
|
||||
handleDownEvent: (event) => {
|
||||
if (!editModeRef.current) return false;
|
||||
const rotateHit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
||||
layerFilter: (layer) => layer === signalTransformLayer,
|
||||
hitTolerance: 12,
|
||||
});
|
||||
if (rotateHit?.get('signal_transform') === 'rotate-handle') {
|
||||
const uid = rotateHit.get('signal_uid');
|
||||
const pivot = rotateHit.get('signal_pivot');
|
||||
if (typeof uid === 'string' && Array.isArray(pivot) && pivot.length === 2) {
|
||||
signalUid = uid;
|
||||
rotatePivot = pivot as Coordinate;
|
||||
event.map.getViewport().style.cursor = 'grabbing';
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
||||
layerFilter: (layer) => layer === layers.signals,
|
||||
hitTolerance: 10,
|
||||
});
|
||||
const uid = hit?.get('signal_uid');
|
||||
if (typeof uid !== 'string' || hit?.get('signal_component')) return false;
|
||||
if (!hit || typeof uid !== 'string' || hit.get('signal_component')) return false;
|
||||
signalUid = uid;
|
||||
selectRef.current.onFeature(hit.getProperties());
|
||||
event.map.getViewport().style.cursor = 'grabbing';
|
||||
return true;
|
||||
},
|
||||
handleDragEvent: (event) => {
|
||||
if (signalUid) dragRef.current.onSignalDrag?.(signalUid, toLonLat(event.coordinate as Coordinate));
|
||||
if (signalUid) {
|
||||
const target = toLonLat(event.coordinate as Coordinate);
|
||||
if (rotatePivot) dragRef.current.onSignalRotate?.(signalUid, azimuthBetween(rotatePivot, target));
|
||||
else dragRef.current.onSignalDrag?.(signalUid, target);
|
||||
}
|
||||
},
|
||||
handleUpEvent: (event) => {
|
||||
if (signalUid) dragRef.current.onSignalDragEnd?.(signalUid, toLonLat(event.coordinate as Coordinate));
|
||||
if (signalUid) {
|
||||
const target = toLonLat(event.coordinate as Coordinate);
|
||||
if (rotatePivot) dragRef.current.onSignalRotateEnd?.(signalUid, azimuthBetween(rotatePivot, target));
|
||||
else dragRef.current.onSignalDragEnd?.(signalUid, target);
|
||||
}
|
||||
signalUid = null;
|
||||
rotatePivot = null;
|
||||
event.map.getViewport().style.cursor = '';
|
||||
return false;
|
||||
},
|
||||
}),
|
||||
@@ -372,6 +441,53 @@ export function MapCanvas({
|
||||
);
|
||||
laneSelectionFeaturesRef.current = parts;
|
||||
}, [laneSelectionBounds, laneSelectionFeatures]);
|
||||
useEffect(() => {
|
||||
const source = signalTransformSourceRef.current;
|
||||
const layer = signalTransformLayerRef.current;
|
||||
if (!source || !layer) return;
|
||||
source.clear();
|
||||
layer.setVisible(Boolean(editMode && signalSelectionUid));
|
||||
if (!editMode || !signalSelectionUid) return;
|
||||
const signal = state.trafficSignals.assemblies.features.find(
|
||||
(feature) => feature.properties?.signal_uid === signalSelectionUid,
|
||||
);
|
||||
const pivot =
|
||||
signal?.geometry?.type === 'Point' && Array.isArray(signal.geometry.coordinates)
|
||||
? (signal.geometry.coordinates as Coordinate)
|
||||
: null;
|
||||
const mastHeading = Number(signal?.properties?.mast_heading_deg);
|
||||
const reach = Number(signal?.properties?.mast_reach_m);
|
||||
if (!pivot || !Number.isFinite(mastHeading) || !Number.isFinite(reach)) return;
|
||||
const mastEnd = offsetCoordinate(pivot, mastHeading, reach);
|
||||
const rotateHandle = offsetCoordinate(pivot, mastHeading, reach + 1.2);
|
||||
const crosshairRadius = 0.45;
|
||||
source.addFeatures([
|
||||
new Feature({
|
||||
geometry: new LineString([
|
||||
fromLonLat(offsetCoordinate(pivot, 270, crosshairRadius)),
|
||||
fromLonLat(offsetCoordinate(pivot, 90, crosshairRadius)),
|
||||
]),
|
||||
signal_transform: 'crosshair',
|
||||
}),
|
||||
new Feature({
|
||||
geometry: new LineString([
|
||||
fromLonLat(offsetCoordinate(pivot, 180, crosshairRadius)),
|
||||
fromLonLat(offsetCoordinate(pivot, 0, crosshairRadius)),
|
||||
]),
|
||||
signal_transform: 'crosshair',
|
||||
}),
|
||||
new Feature({
|
||||
geometry: new LineString([fromLonLat(mastEnd), fromLonLat(rotateHandle)]),
|
||||
signal_transform: 'rotate-leader',
|
||||
}),
|
||||
new Feature({
|
||||
geometry: new Point(fromLonLat(rotateHandle)),
|
||||
signal_transform: 'rotate-handle',
|
||||
signal_uid: signalSelectionUid,
|
||||
signal_pivot: pivot,
|
||||
}),
|
||||
]);
|
||||
}, [editMode, signalSelectionUid, state.trafficSignals.assemblies.features]);
|
||||
useEffect(() => {
|
||||
// Selection changes only affect presentation; compiler GeoJSON stays untouched.
|
||||
layersRef.current?.markings.changed();
|
||||
|
||||
Reference in new issue
Block a user