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
+103
-16
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { Download, RefreshCw, Save, Upload } from 'lucide-react';
|
||||
import { Download, RefreshCw, Upload } from 'lucide-react';
|
||||
import { api, isVersionConflict } from './lib/api';
|
||||
import { MapCanvas } from './components/MapCanvas';
|
||||
import { ParameterPanel, type ParameterPanelHandle } from './components/ParameterPanel';
|
||||
@@ -19,6 +19,7 @@ import type { ElementOutline } from './edit/handle-layer';
|
||||
import {
|
||||
azimuthBetween,
|
||||
coordinateAtStation,
|
||||
haversineMeters,
|
||||
metersAlongPolyline,
|
||||
polylineLengthMeters,
|
||||
toLonLat,
|
||||
@@ -116,6 +117,7 @@ function App() {
|
||||
const signalRedoRef = useRef<GeoFeature[][]>([]);
|
||||
const signalSavedRef = useRef<GeoFeature[] | null>(null);
|
||||
const signalMoveActiveRef = useRef(false);
|
||||
const signalRotateActiveRef = useRef(false);
|
||||
const [layers, setLayers] = useState(defaultLayers);
|
||||
const [scene, setScene] = useState(false);
|
||||
const [status, setStatus] = useState('正在加载工作台...');
|
||||
@@ -739,6 +741,20 @@ function App() {
|
||||
signalRedoRef.current = [];
|
||||
signalMoveActiveRef.current = true;
|
||||
}
|
||||
const currentFeature = current.trafficSignals.assemblies.features.find(
|
||||
(feature) => feature.properties?.signal_uid === uid,
|
||||
);
|
||||
const stop: Coordinate | null = currentFeature
|
||||
? [Number(currentFeature.properties?.stop_lon), Number(currentFeature.properties?.stop_lat)]
|
||||
: null;
|
||||
let nextCoordinate = coordinate;
|
||||
if (stop && stop.every(Number.isFinite)) {
|
||||
const distance = haversineMeters(stop, coordinate);
|
||||
if (distance > 40) {
|
||||
const ratio = 40 / distance;
|
||||
nextCoordinate = [stop[0] + (coordinate[0] - stop[0]) * ratio, stop[1] + (coordinate[1] - stop[1]) * ratio];
|
||||
}
|
||||
}
|
||||
return {
|
||||
...current,
|
||||
trafficSignals: {
|
||||
@@ -747,7 +763,7 @@ function App() {
|
||||
...current.trafficSignals.assemblies,
|
||||
features: current.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||
: feature,
|
||||
),
|
||||
},
|
||||
@@ -759,15 +775,80 @@ function App() {
|
||||
const finishSignalMove = (uid: string, coordinate: Coordinate) => {
|
||||
const current = state;
|
||||
if (!current) return;
|
||||
const source = current.trafficSignals.assemblies.features.find((feature) => feature.properties?.signal_uid === uid);
|
||||
const stop: Coordinate | null = source
|
||||
? [Number(source.properties?.stop_lon), Number(source.properties?.stop_lat)]
|
||||
: null;
|
||||
let nextCoordinate = coordinate;
|
||||
if (stop && stop.every(Number.isFinite)) {
|
||||
const distance = haversineMeters(stop, coordinate);
|
||||
if (distance > 40) {
|
||||
const ratio = 40 / distance;
|
||||
nextCoordinate = [stop[0] + (coordinate[0] - stop[0]) * ratio, stop[1] + (coordinate[1] - stop[1]) * ratio];
|
||||
}
|
||||
}
|
||||
const features = current.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: coordinate } }
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||
: feature,
|
||||
);
|
||||
signalDraftRef.current = features;
|
||||
signalMoveActiveRef.current = false;
|
||||
setSignalDirty(true);
|
||||
};
|
||||
const rotateSignal = (uid: string, mastHeading: number) => {
|
||||
const heading = ((mastHeading % 360) + 360) % 360;
|
||||
setState((current) => {
|
||||
if (!current) return current;
|
||||
if (!signalRotateActiveRef.current) {
|
||||
signalUndoRef.current.push(current.trafficSignals.assemblies.features);
|
||||
signalRedoRef.current = [];
|
||||
signalRotateActiveRef.current = true;
|
||||
}
|
||||
return {
|
||||
...current,
|
||||
trafficSignals: {
|
||||
...current.trafficSignals,
|
||||
assemblies: {
|
||||
...current.trafficSignals.assemblies,
|
||||
features: current.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? {
|
||||
...feature,
|
||||
properties: {
|
||||
...feature.properties,
|
||||
mast_heading_deg: heading,
|
||||
face_heading_deg: (((heading - 90) % 360) + 360) % 360,
|
||||
},
|
||||
}
|
||||
: feature,
|
||||
),
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
setSignalDirty(true);
|
||||
};
|
||||
const finishSignalRotate = (uid: string, mastHeading: number) => {
|
||||
const current = state;
|
||||
if (!current) return;
|
||||
const heading = ((mastHeading % 360) + 360) % 360;
|
||||
const features = current.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? {
|
||||
...feature,
|
||||
properties: {
|
||||
...feature.properties,
|
||||
mast_heading_deg: heading,
|
||||
face_heading_deg: (((heading - 90) % 360) + 360) % 360,
|
||||
},
|
||||
}
|
||||
: feature,
|
||||
);
|
||||
signalDraftRef.current = features;
|
||||
signalRotateActiveRef.current = false;
|
||||
setSignalDirty(true);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<header>
|
||||
@@ -809,17 +890,11 @@ function App() {
|
||||
<a className="button" href="/api/export.zip" download>
|
||||
<Download size={15} /> 导出道路包
|
||||
</a>
|
||||
<Button onClick={() => void save()} disabled={!staged.length}>
|
||||
<Save size={15} /> 保存修改
|
||||
</Button>
|
||||
<Button onClick={() => void compile()}>
|
||||
<RefreshCw size={15} /> 保存并重新生成
|
||||
</Button>
|
||||
{editMode ? (
|
||||
<>
|
||||
<Button onClick={() => void saveDirectEdits()} disabled={!session?.dirty}>
|
||||
<Save size={15} /> 保存直接编辑
|
||||
</Button>
|
||||
<Button onClick={undoEdit} disabled={!session?.canUndo && !signalUndoRef.current.length}>
|
||||
撤销
|
||||
</Button>
|
||||
@@ -898,9 +973,15 @@ function App() {
|
||||
scene={scene}
|
||||
onSelectRoad={setSelected}
|
||||
onFeature={handleFeature}
|
||||
onBackground={() => setElement(null)}
|
||||
signalSelectionUid={typeof marking?.signal_uid === 'string' ? marking.signal_uid : null}
|
||||
onBackground={() => {
|
||||
setElement(null);
|
||||
setMarking(null);
|
||||
}}
|
||||
onSignalDrag={moveSignal}
|
||||
onSignalDragEnd={finishSignalMove}
|
||||
onSignalRotate={rotateSignal}
|
||||
onSignalRotateEnd={finishSignalRotate}
|
||||
handles={editHandles}
|
||||
ranges={editRanges}
|
||||
element={elementOutline}
|
||||
@@ -1435,9 +1516,19 @@ function SignalEditor({
|
||||
: null;
|
||||
const saveField = (key: string, value: unknown) => {
|
||||
if (!feature) return;
|
||||
const mastHeading = key === 'mast_heading_deg' ? ((Number(value) % 360) + 360) % 360 : null;
|
||||
void update(
|
||||
state.trafficSignals.assemblies.features.map((item) =>
|
||||
item === feature ? { ...item, properties: { ...item.properties, [key]: value } } : item,
|
||||
item === feature
|
||||
? {
|
||||
...item,
|
||||
properties: {
|
||||
...item.properties,
|
||||
[key]: mastHeading ?? value,
|
||||
...(mastHeading === null ? {} : { face_heading_deg: (((mastHeading - 90) % 360) + 360) % 360 }),
|
||||
},
|
||||
}
|
||||
: item,
|
||||
),
|
||||
);
|
||||
};
|
||||
@@ -1505,11 +1596,7 @@ function SignalEditor({
|
||||
</label>
|
||||
<label>
|
||||
灯面朝向(度)
|
||||
<input
|
||||
type="number"
|
||||
value={Number(feature.properties?.face_heading_deg || 0)}
|
||||
onChange={(event) => saveField('face_heading_deg', Number(event.target.value))}
|
||||
/>
|
||||
<input type="number" value={Number(feature.properties?.face_heading_deg || 0)} readOnly />
|
||||
</label>
|
||||
<label>
|
||||
相位组
|
||||
|
||||
Reference in new issue
Block a user