feat: edit lane turn arrows in workbench
This commit is contained in:
1 parent
8b84166e6f
commit
e48eb15827
9 files changed
+330
-28
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
elementSelectionFor,
|
||||
groupMembers,
|
||||
handleForElement,
|
||||
isControlElement,
|
||||
orientedBoundsOf,
|
||||
specFor,
|
||||
specOfKind,
|
||||
@@ -187,7 +188,7 @@ function App() {
|
||||
* dot — no second drag path, no second constraint.
|
||||
*/
|
||||
const elementOutline = useMemo<ElementOutline | null>(() => {
|
||||
if (!directEditEnabled || !element || !state) return null;
|
||||
if (!directEditEnabled || !element || !isControlElement(element) || !state) return null;
|
||||
const handle = handleForElement(element, state.compiled.model.roads, editHandles);
|
||||
if (!handle?.editable) return null;
|
||||
// Read the preview when there is one. `state.layers` is the last *compiled*
|
||||
@@ -504,10 +505,15 @@ function App() {
|
||||
// zebra with the stop line protecting it. Clicking anything else drops the
|
||||
// selection, because a box left around what the user navigated away from
|
||||
// reads as a stuck UI.
|
||||
if (directEditEnabled) {
|
||||
const spec = specFor(properties);
|
||||
setElement(spec ? elementSelectionFor(properties, state.layers[spec.layer]?.features ?? []) : null);
|
||||
}
|
||||
const spec = specFor(properties);
|
||||
const nextElement = elementSelectionFor(
|
||||
properties,
|
||||
spec ? (state.layers[spec.layer]?.features ?? []) : (state.layers.laneCenterlines?.features ?? []),
|
||||
);
|
||||
// lane-maneuver is a regular v1 override, not a direct-edit constraint, so
|
||||
// it remains available in the shipped workbench. The flag still contains
|
||||
// control-marking outlines and all v2 handles behind the experimental path.
|
||||
setElement(directEditEnabled || nextElement?.kind === 'lane-maneuver' ? nextElement : null);
|
||||
const uid = properties.signal_uid;
|
||||
if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`);
|
||||
else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式');
|
||||
@@ -525,7 +531,9 @@ function App() {
|
||||
<span className={editDiagnostics.some((item) => item.severity === 'error') ? 'dirty' : ''}>
|
||||
{editDiagnostics.find((item) => item.severity === 'error')?.message ||
|
||||
(element
|
||||
? `已选中${specOfKind(element.kind).label} ${element.crossingNodeId}(${element.featureIds.length} 条)`
|
||||
? element.kind === 'lane-maneuver'
|
||||
? `已选中第 ${laneIndex(element.laneId)} 车道(${element.featureIds.length} 个箭头 part)`
|
||||
: `已选中${specOfKind(element.kind).label} ${element.crossingNodeId}(${element.featureIds.length} 条)`
|
||||
: editHandles.length
|
||||
? `手柄 ${editHandles.length} 个`
|
||||
: selected
|
||||
@@ -658,6 +666,7 @@ function App() {
|
||||
state={state}
|
||||
staged={staged}
|
||||
marking={marking}
|
||||
element={element}
|
||||
stage={stage}
|
||||
status={setStatus}
|
||||
setState={setState}
|
||||
@@ -706,6 +715,7 @@ function Inspector({
|
||||
state,
|
||||
staged,
|
||||
marking,
|
||||
element,
|
||||
stage,
|
||||
status,
|
||||
setState,
|
||||
@@ -714,6 +724,7 @@ function Inspector({
|
||||
state: WorkbenchState;
|
||||
staged: Override[];
|
||||
marking: Record<string, unknown> | null;
|
||||
element: ElementSelection | null;
|
||||
stage: (item: Override) => void;
|
||||
status: (value: string) => void;
|
||||
setState: (next: WorkbenchState) => void;
|
||||
@@ -892,6 +903,7 @@ function Inspector({
|
||||
) : null;
|
||||
})}
|
||||
<MarkingEditor marking={marking} stage={stage} />
|
||||
<LaneManeuverEditor element={element} overrides={[...state.overrides.overrides, ...staged]} stage={stage} />
|
||||
<SignalEditor state={state} signal={signal} status={status} update={updateSignal} />
|
||||
<Candidates state={state} setState={setState} status={status} />
|
||||
<details>
|
||||
@@ -907,6 +919,55 @@ function Inspector({
|
||||
</>
|
||||
);
|
||||
}
|
||||
const MANEUVER_OPTIONS: Array<[string, string]> = [
|
||||
['', '无箭头'],
|
||||
['through', '直行'],
|
||||
['left', '左转'],
|
||||
['right', '右转'],
|
||||
['through;left', '直行或左转'],
|
||||
['through;right', '直行或右转'],
|
||||
['through;left;right', '直行、左转或右转'],
|
||||
];
|
||||
function LaneManeuverEditor({
|
||||
element,
|
||||
overrides,
|
||||
stage,
|
||||
}: {
|
||||
element: ElementSelection | null;
|
||||
overrides: Override[];
|
||||
stage: (item: Override) => void;
|
||||
}) {
|
||||
if (!element || element.kind !== 'lane-maneuver') return null;
|
||||
// Old override files may use arbitrary ids. Reuse the latest matching entry
|
||||
// so staging replaces it instead of leaving two conflicting maneuvers on one lane.
|
||||
const override = [...overrides]
|
||||
.reverse()
|
||||
.find((item) => item.kind === 'lane-maneuver' && item.laneId === element.laneId);
|
||||
const id = override?.id || `车道转向:${element.laneId}`;
|
||||
const maneuver = typeof override?.maneuver === 'string' ? override.maneuver : element.maneuver;
|
||||
return (
|
||||
<form>
|
||||
<hr />
|
||||
<h2>车道转向箭头</h2>
|
||||
<output>第 {laneIndex(element.laneId)} 车道</output>
|
||||
<label>
|
||||
转向
|
||||
<select
|
||||
value={maneuver}
|
||||
onChange={(event) =>
|
||||
stage({ id, kind: 'lane-maneuver', laneId: element.laneId, maneuver: event.target.value })
|
||||
}
|
||||
>
|
||||
{MANEUVER_OPTIONS.map(([value, label]) => (
|
||||
<option value={value} key={value}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
function MarkingEditor({
|
||||
marking,
|
||||
stage,
|
||||
|
||||
@@ -2,7 +2,9 @@ import { describe, expect, it } from 'vitest';
|
||||
import { azimuthBetween, haversineMeters, offsetCoordinate } from './meters';
|
||||
import {
|
||||
CROSSWALK_PROVENANCE,
|
||||
LANE_CENTERLINE_SOURCE,
|
||||
STOP_LINE_PROVENANCE,
|
||||
TURN_ARROW_PROVENANCE,
|
||||
elementIdOf,
|
||||
handleForElement,
|
||||
elementSelectionFor,
|
||||
@@ -26,6 +28,18 @@ function stripe(crossingNodeId: string | number, part: number, roadId = 'road:wa
|
||||
};
|
||||
}
|
||||
|
||||
function turnArrow(laneId: string, maneuver: string, part: number, roadId = 'road:way/-3246:forward'): StripeFeature {
|
||||
return {
|
||||
properties: {
|
||||
native_id: `turn-arrow:${laneId}:${maneuver}:${part}`,
|
||||
lane_id: laneId,
|
||||
maneuver,
|
||||
road_id: roadId,
|
||||
provenance: TURN_ARROW_PROVENANCE,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Two groups of six, the arrangement a real area produces. */
|
||||
const features: StripeFeature[] = [
|
||||
...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26452, part)),
|
||||
@@ -63,8 +77,16 @@ describe('elementIdOf', () => {
|
||||
expect(elementIdOf(stopLine.properties)).not.toBe(elementIdOf(stripe(-26452, 1).properties));
|
||||
});
|
||||
|
||||
it('rejects a marking that has no spec, such as a lane arrow', () => {
|
||||
expect(elementIdOf({ provenance: 'native-road-turn-arrow/v1', lane_id: 'lane:x' })).toBeNull();
|
||||
it('groups a turn arrow by both lane and maneuver', () => {
|
||||
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties)).toBe(
|
||||
'turn-arrow:lane:road:way/-3246:forward:1\u001fthrough;left',
|
||||
);
|
||||
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 2).properties)).toBe(
|
||||
elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties),
|
||||
);
|
||||
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'left', 1).properties)).not.toBe(
|
||||
elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties),
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects anything else the user may click', () => {
|
||||
@@ -120,6 +142,49 @@ describe('elementSelectionFor', () => {
|
||||
// selecting a group with no members on screen.
|
||||
expect(elementSelectionFor(stripe(-26452, 1).properties, [])).toBeNull();
|
||||
});
|
||||
|
||||
it('selects all parts of one turn arrow without merging another maneuver on the lane', () => {
|
||||
const laneId = 'lane:road:way/-3246:forward:1';
|
||||
const arrows = [
|
||||
...[0, 1, 2].map((part) => turnArrow(laneId, 'through;left', part)),
|
||||
...[0, 1, 2].map((part) => turnArrow(laneId, 'left', part)),
|
||||
];
|
||||
expect(elementSelectionFor(arrows[1].properties, arrows)).toEqual({
|
||||
kind: 'lane-maneuver',
|
||||
laneId,
|
||||
maneuver: 'through;left',
|
||||
roadId: 'road:way/-3246:forward',
|
||||
featureIds: [0, 1, 2].map((part) => `turn-arrow:${laneId}:through;left:${part}`),
|
||||
});
|
||||
});
|
||||
|
||||
it('selects a centerline as a lane with no current turn arrow', () => {
|
||||
expect(
|
||||
elementSelectionFor(
|
||||
{
|
||||
native_id: 'lane:road:way/-3246:forward:2',
|
||||
road_id: 'road:way/-3246:forward',
|
||||
source: LANE_CENTERLINE_SOURCE,
|
||||
},
|
||||
[],
|
||||
),
|
||||
).toEqual({
|
||||
kind: 'lane-maneuver',
|
||||
laneId: 'lane:road:way/-3246:forward:2',
|
||||
maneuver: '',
|
||||
roadId: 'road:way/-3246:forward',
|
||||
featureIds: ['lane:road:way/-3246:forward:2'],
|
||||
});
|
||||
});
|
||||
|
||||
it('does not offer the repeated direction arrows as lane-maneuver edits', () => {
|
||||
expect(
|
||||
elementSelectionFor(
|
||||
{ provenance: 'native-road-direction-arrow/v1', lane_id: 'lane:road:way/-3246:forward:1' },
|
||||
[],
|
||||
),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleForElement', () => {
|
||||
|
||||
@@ -25,8 +25,12 @@ import { azimuthBetween, eastNorthMeters, haversineMeters, offsetCoordinate, typ
|
||||
export const CROSSWALK_PROVENANCE = 'native-road-crosswalk/v1';
|
||||
/** Stop lines share `controlFeature()`; kept so the two are never confused. */
|
||||
export const STOP_LINE_PROVENANCE = 'native-road-stop-line/v1';
|
||||
/** Turn arrows are grouped by both the lane and the rendered maneuver. */
|
||||
export const TURN_ARROW_PROVENANCE = 'native-road-turn-arrow/v1';
|
||||
/** Lane centerlines are the entry point for lanes which do not yet draw an arrow. */
|
||||
export const LANE_CENTERLINE_SOURCE = 'native-road-lane-centerline/v3-control-clipped';
|
||||
|
||||
export type ElementKind = 'crosswalk' | 'stop-line';
|
||||
export type ElementKind = 'crosswalk' | 'stop-line' | 'turn-arrow';
|
||||
|
||||
/**
|
||||
* What makes one kind of marking an editable element.
|
||||
@@ -44,12 +48,12 @@ export interface ElementSpec {
|
||||
/** Prefix of the group id, matching the compiler's `native_id`. */
|
||||
idPrefix: string;
|
||||
/** Layer the members live in, in both `state.layers` and a preview. */
|
||||
layer: 'crosswalks' | 'vehicleStopLines';
|
||||
/** The constraint that moves this element. */
|
||||
constraintKind: 'junction-crosswalk-inset' | 'junction-stop-line-offset';
|
||||
layer: 'crosswalks' | 'vehicleStopLines' | 'turnArrows';
|
||||
/** The constraint that moves this control element. */
|
||||
constraintKind?: 'junction-crosswalk-inset' | 'junction-stop-line-offset';
|
||||
label: string;
|
||||
/** The property whose value groups members together. */
|
||||
groupProperty: string;
|
||||
/** Properties whose ordered values group rendered parts together. */
|
||||
groupProperties: readonly string[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,20 +66,28 @@ export const ELEMENT_SPECS: readonly ElementSpec[] = [
|
||||
{
|
||||
kind: 'crosswalk',
|
||||
provenance: CROSSWALK_PROVENANCE,
|
||||
idPrefix: 'crosswalk',
|
||||
idPrefix: 'crosswalk:node/',
|
||||
layer: 'crosswalks',
|
||||
constraintKind: 'junction-crosswalk-inset',
|
||||
label: '斑马线',
|
||||
groupProperty: 'crossing_node_id',
|
||||
groupProperties: ['crossing_node_id'],
|
||||
},
|
||||
{
|
||||
kind: 'stop-line',
|
||||
provenance: STOP_LINE_PROVENANCE,
|
||||
idPrefix: 'stop-line',
|
||||
idPrefix: 'stop-line:node/',
|
||||
layer: 'vehicleStopLines',
|
||||
constraintKind: 'junction-stop-line-offset',
|
||||
label: '停止线',
|
||||
groupProperty: 'crossing_node_id',
|
||||
groupProperties: ['crossing_node_id'],
|
||||
},
|
||||
{
|
||||
kind: 'turn-arrow',
|
||||
provenance: TURN_ARROW_PROVENANCE,
|
||||
idPrefix: 'turn-arrow:',
|
||||
layer: 'turnArrows',
|
||||
label: '转向箭头',
|
||||
groupProperties: ['lane_id', 'maneuver'],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -89,8 +101,8 @@ export function specOfKind(kind: ElementKind): ElementSpec {
|
||||
return ELEMENT_SPECS.find((spec) => spec.kind === kind)!;
|
||||
}
|
||||
|
||||
export interface ElementSelection {
|
||||
kind: ElementKind;
|
||||
export interface ControlElementSelection {
|
||||
kind: 'crosswalk' | 'stop-line';
|
||||
/** `<prefix>:node/<crossingId>` — the group, not one of its features. */
|
||||
elementId: string;
|
||||
crossingNodeId: string;
|
||||
@@ -100,6 +112,18 @@ export interface ElementSelection {
|
||||
featureIds: string[];
|
||||
}
|
||||
|
||||
/** A discrete v1 override target, selected from an arrow or a lane centerline. */
|
||||
export interface LaneManeuverSelection {
|
||||
kind: 'lane-maneuver';
|
||||
laneId: string;
|
||||
/** The current rendered maneuver; empty when the lane has no turn arrow. */
|
||||
maneuver: string;
|
||||
roadId: string;
|
||||
featureIds: string[];
|
||||
}
|
||||
|
||||
export type ElementSelection = ControlElementSelection | LaneManeuverSelection;
|
||||
|
||||
/** The shape this module needs off a GeoJSON feature; anything else is ignored. */
|
||||
export interface StripeFeature {
|
||||
properties?: Record<string, unknown> | null;
|
||||
@@ -121,6 +145,13 @@ function text(value: unknown): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
function groupKeyOf(properties: Record<string, unknown>, spec: ElementSpec): string | null {
|
||||
const parts = spec.groupProperties.map((property) => text(properties[property]));
|
||||
// U+001F is an ASCII control separator, so lane ids and maneuvers cannot
|
||||
// accidentally produce the same key through ordinary punctuation.
|
||||
return parts.every((part): part is string => part !== null) ? parts.join('\u001f') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The group id for a clicked feature, or `null` when it is not an editable
|
||||
* element — a road surface, a lane arrow, or anything else the user may hit.
|
||||
@@ -128,8 +159,8 @@ function text(value: unknown): string | null {
|
||||
export function elementIdOf(properties: Record<string, unknown> | null | undefined): string | null {
|
||||
const spec = specFor(properties);
|
||||
if (!spec) return null;
|
||||
const key = text(properties![spec.groupProperty]);
|
||||
return key ? `${spec.idPrefix}:node/${key}` : null;
|
||||
const key = groupKeyOf(properties!, spec);
|
||||
return key ? `${spec.idPrefix}${key}` : null;
|
||||
}
|
||||
|
||||
/** Every feature belonging to one group, in the order the compiler emitted them. */
|
||||
@@ -149,6 +180,11 @@ export function elementSelectionFor(
|
||||
features: readonly StripeFeature[],
|
||||
): ElementSelection | null {
|
||||
const spec = specFor(properties);
|
||||
if (!spec && properties?.source === LANE_CENTERLINE_SOURCE) {
|
||||
const laneId = text(properties.native_id);
|
||||
const roadId = text(properties.road_id);
|
||||
return laneId && roadId ? { kind: 'lane-maneuver', laneId, maneuver: '', roadId, featureIds: [laneId] } : null;
|
||||
}
|
||||
const elementId = elementIdOf(properties);
|
||||
if (!spec || !elementId) return null;
|
||||
const members = groupMembers(features, elementId);
|
||||
@@ -158,8 +194,24 @@ export function elementSelectionFor(
|
||||
if (!members.length) return null;
|
||||
const first = members[0].properties ?? {};
|
||||
const roadId = text(first.road_id);
|
||||
if (!roadId) return null;
|
||||
if (spec.kind === 'turn-arrow') {
|
||||
const laneId = text(first.lane_id);
|
||||
const maneuver = text(first.maneuver);
|
||||
return laneId && maneuver
|
||||
? {
|
||||
kind: 'lane-maneuver',
|
||||
laneId,
|
||||
maneuver,
|
||||
roadId,
|
||||
featureIds: members
|
||||
.map((member) => text(member.properties?.native_id))
|
||||
.filter((id): id is string => Boolean(id)),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
const crossingNodeId = text(first.crossing_node_id);
|
||||
if (!roadId || !crossingNodeId) return null;
|
||||
if (!crossingNodeId) return null;
|
||||
return {
|
||||
kind: spec.kind,
|
||||
elementId,
|
||||
@@ -171,7 +223,14 @@ export function elementSelectionFor(
|
||||
|
||||
/** True when the click should replace the current selection rather than keep it. */
|
||||
export function isSameElement(current: ElementSelection | null, next: ElementSelection | null): boolean {
|
||||
return current?.elementId === next?.elementId;
|
||||
if (!current || !next) return current === next;
|
||||
return current.kind === 'lane-maneuver' && next.kind === 'lane-maneuver'
|
||||
? current.laneId === next.laneId
|
||||
: current.kind !== 'lane-maneuver' && next.kind !== 'lane-maneuver' && current.elementId === next.elementId;
|
||||
}
|
||||
|
||||
export function isControlElement(element: ElementSelection): element is ControlElementSelection {
|
||||
return element.kind !== 'lane-maneuver';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,7 +249,7 @@ export function isSameElement(current: ElementSelection | null, next: ElementSel
|
||||
* it computes that handle's ceiling; see `direct-edit-api.md` §11.
|
||||
*/
|
||||
export function handleForElement<T extends { kind: string; anchor: Record<string, unknown> }>(
|
||||
element: ElementSelection,
|
||||
element: ControlElementSelection,
|
||||
roads: readonly RoadRef[],
|
||||
handles: readonly T[],
|
||||
): T | null {
|
||||
|
||||
Reference in new issue
Block a user