Extract the dialogue canvas's routing upgrades into routeEdge() in the shared lib: perpendicular handle stubs, candidate scoring (Z-step vs straight for forward edges, top/bottom bus corridors for back edges), per-edge detour lane spreading, and multi-pass obstacle clearing. The NPC dialogue RoutedFlowEdge becomes a thin renderer over it, and the tech tree RoutedEdge now inherits the same logic while keeping its own features intact: manual waypoints route verbatim (and define the add-a-bend base), per-source stepPosition lanes feed the Z-step, and its fixed right/left handles map to the new direction params. New pure spec covers the Z-step shape, blocked-forward detouring, back-edge bus routing, per-id lane spread, and waypoint pass-through.
188 lines
6.5 KiB
TypeScript
188 lines
6.5 KiB
TypeScript
"use client";
|
|
|
|
import { BaseEdge, EdgeLabelRenderer, useReactFlow, type EdgeProps } from "@xyflow/react";
|
|
import React from "react";
|
|
|
|
import { buildRoutePath, type RouteWaypoint } from "./edgeRoutes";
|
|
import { routeEdge, type RoutePoint, type RouteRect } from "./edgeRouting";
|
|
|
|
export type RoutedEdgeData = {
|
|
/** User-placed waypoints for this edge (empty = automatic routing). */
|
|
waypoints: RouteWaypoint[];
|
|
/** "source:target" key used to persist the route. */
|
|
routeId: string;
|
|
/** Technology ids at each end, so the edge may connect to them. */
|
|
sourceId: number;
|
|
targetId: number;
|
|
/** Node rectangles the edge should route around. */
|
|
obstacles: RouteRect[];
|
|
/** Where the automatic vertical run sits (0..1). */
|
|
stepPosition: number;
|
|
editable: boolean;
|
|
onRouteChange: (routeId: string, waypoints: RouteWaypoint[]) => void;
|
|
};
|
|
|
|
/**
|
|
* A prerequisite edge. It draws a polyline from the source handle through any
|
|
* user waypoints to the target handle, bending around technologies it is not
|
|
* connected to, with rounded corners at each bend.
|
|
*
|
|
* Interaction (only when `editable`):
|
|
* - Select the edge to reveal "+" handles at each segment's midpoint; click one
|
|
* to add a bend there.
|
|
* - Drag a waypoint dot to move it.
|
|
* - Double-click a waypoint dot to remove it.
|
|
*/
|
|
export function RoutedEdge({
|
|
id,
|
|
sourceX,
|
|
sourceY,
|
|
targetX,
|
|
targetY,
|
|
markerEnd,
|
|
style,
|
|
data,
|
|
selected,
|
|
}: EdgeProps) {
|
|
const route = data as unknown as RoutedEdgeData | undefined;
|
|
const waypoints = route?.waypoints ?? [];
|
|
const editable = route?.editable ?? false;
|
|
const { screenToFlowPosition } = useReactFlow();
|
|
const dragIndexRef = React.useRef<number | null>(null);
|
|
|
|
// Shared shape builder (same logic as the dialogue canvas): forward edges
|
|
// take a stubbed Z-step at the source's lane (stepPosition), back edges try
|
|
// bus corridors around the cards, and every candidate is detoured around
|
|
// unrelated technologies with the cleanest result winning. User waypoints,
|
|
// when present, are routed verbatim.
|
|
const start: RoutePoint = { x: sourceX, y: sourceY };
|
|
const end: RoutePoint = { x: targetX, y: targetY };
|
|
const { points: routed, base } = routeEdge({
|
|
id: route?.routeId,
|
|
start,
|
|
end,
|
|
sourceDirection: "right",
|
|
targetDirection: "left",
|
|
nodes: route?.obstacles ?? [],
|
|
sourceId: route?.sourceId,
|
|
targetId: route?.targetId,
|
|
waypoints: waypoints.length > 0 ? waypoints : undefined,
|
|
stepPosition: route?.stepPosition,
|
|
});
|
|
const path = routed.length >= 2 ? buildRoutePath(routed) : "";
|
|
|
|
const commit = (next: RouteWaypoint[]) => route?.onRouteChange(route.routeId, next);
|
|
|
|
const onHandlePointerDown =
|
|
(index: number) => (event: React.PointerEvent<HTMLDivElement>) => {
|
|
if (!editable) return;
|
|
event.stopPropagation();
|
|
event.preventDefault();
|
|
dragIndexRef.current = index;
|
|
event.currentTarget.setPointerCapture(event.pointerId);
|
|
};
|
|
|
|
const onHandlePointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
const index = dragIndexRef.current;
|
|
if (index === null) return;
|
|
const point = screenToFlowPosition({ x: event.clientX, y: event.clientY });
|
|
commit(
|
|
waypoints.map((wp, i) =>
|
|
i === index ? { x: Math.round(point.x), y: Math.round(point.y) } : wp,
|
|
),
|
|
);
|
|
};
|
|
|
|
const onHandlePointerUp = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
if (dragIndexRef.current === null) return;
|
|
dragIndexRef.current = null;
|
|
try {
|
|
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
} catch {
|
|
// capture may already be released
|
|
}
|
|
};
|
|
|
|
const onHandleDoubleClick = (index: number) => (event: React.MouseEvent) => {
|
|
if (!editable) return;
|
|
event.stopPropagation();
|
|
commit(waypoints.filter((_, i) => i !== index));
|
|
};
|
|
|
|
// Midpoints of the UNROUTED segments, for the "add a bend" handles (a routed
|
|
// detour is not a place the user can insert a waypoint at).
|
|
const midpoints: { x: number; y: number; insertAt: number }[] = [];
|
|
for (let i = 0; i < base.length - 1; i += 1) {
|
|
midpoints.push({
|
|
x: (base[i].x + base[i + 1].x) / 2,
|
|
y: (base[i].y + base[i + 1].y) / 2,
|
|
insertAt: i,
|
|
});
|
|
}
|
|
|
|
const addAt = (index: number, point: RouteWaypoint) => {
|
|
const next = [...waypoints];
|
|
next.splice(index, 0, point);
|
|
commit(next);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<BaseEdge id={id} path={path} markerEnd={markerEnd} style={style} />
|
|
<EdgeLabelRenderer>
|
|
{selected && editable
|
|
? midpoints.map((mid, i) => (
|
|
<div
|
|
key={`${route?.routeId}-mid-${i}`}
|
|
className="nodrag nopan"
|
|
style={{
|
|
position: "absolute",
|
|
transform: `translate(-50%, -50%) translate(${mid.x}px, ${mid.y}px)`,
|
|
pointerEvents: "all",
|
|
}}
|
|
>
|
|
<button
|
|
type="button"
|
|
aria-label="Add route bend"
|
|
title="Add a bend here"
|
|
data-testid="tech-edge-add-bend"
|
|
onPointerDown={(e) => e.stopPropagation()}
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
addAt(mid.insertAt, { x: Math.round(mid.x), y: Math.round(mid.y) });
|
|
}}
|
|
className="flex h-4 w-4 items-center justify-center rounded-full border border-[#a5f3fc]/70 bg-black/80 font-mono text-[10px] leading-none text-[#a5f3fc] hover:border-[#a5f3fc] hover:bg-[#22d3ee]/25"
|
|
>
|
|
+
|
|
</button>
|
|
</div>
|
|
))
|
|
: null}
|
|
{waypoints.map((wp, index) => (
|
|
<div
|
|
key={`${route?.routeId}-wp-${index}`}
|
|
className="nodrag nopan"
|
|
style={{
|
|
position: "absolute",
|
|
transform: `translate(-50%, -50%) translate(${wp.x}px, ${wp.y}px)`,
|
|
pointerEvents: "all",
|
|
}}
|
|
>
|
|
<div
|
|
role="button"
|
|
aria-label="Route waypoint"
|
|
title="Drag to move, double-click to remove"
|
|
data-testid="tech-edge-waypoint"
|
|
onPointerDown={onHandlePointerDown(index)}
|
|
onPointerMove={onHandlePointerMove}
|
|
onPointerUp={onHandlePointerUp}
|
|
onDoubleClick={onHandleDoubleClick(index)}
|
|
className="h-3 w-3 rounded-full border border-[#a5f3fc]/80 bg-[#22d3ee]/90 shadow-[0_0_6px_rgba(34,211,238,0.7)]"
|
|
style={{ cursor: "grab" }}
|
|
/>
|
|
</div>
|
|
))}
|
|
</EdgeLabelRenderer>
|
|
</>
|
|
);
|
|
}
|