diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index 8d10b11..6d67eac 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -4,7 +4,7 @@ import { CHECKED_INPUTS } from '~/lib/logic/check' import { cableBits, GRID as G, pinKindOf, pinPos, routeWire, type Pt, type Shape } from '~/lib/logic/geometry' import type { CircuitNode, PanelRow, PinRef, Wire } from '~/lib/logic/types' import { branchCorners, branchTo, endGrip, moveCorner, moveSegment, nearEnd, routeTouchesBox, segmentAt, segmentDrag, splitDrag } from '~/lib/logic/wires' -import { circuitRoutes, findCrossings } from '~/lib/logic/routing' +import { circuitRoutes, planHops } from '~/lib/logic/routing' import { seriesLinks, type Link } from '~/lib/logic/bulk' import { dropLinks, dropPins, fitStart, type DropPin } from '~/lib/logic/fit' import type { CanvasOps, MenuEntry } from '~/composables/useMenu' @@ -47,6 +47,8 @@ interface Route { kind: string /** A cable between connector ports: its type's name and colour. */ port?: { name: string; color?: string } + /** It jumps over other lines somewhere. */ + hops?: boolean } /** The middle of a route's longest straight run, where a cable's label goes. */ @@ -67,25 +69,32 @@ function midOfLongest(draw: Pt[]): Pt | undefined { const toPath = (pts: Pt[]) => pts.map(([x, y], i) => `${i ? 'L' : 'M'}${x * G} ${y * G}`).join('') -const routes = computed(() => { - const routed = circuitRoutes(def.value, shapes.value, { tidy: !!ed.project.value.settings.tidy }) - const hops = new Map>() - for (const c of findCrossings(routed)) { - const segs = hops.get(c.wire) ?? hops.set(c.wire, new Map()).get(c.wire)! - segs.set(c.seg, [...(segs.get(c.seg) ?? []), c.x]) +/** How wide a wire is drawn, in pixels, when it is a cable: its sleeve with the outline. */ +function cablePx(w: Wire | undefined): number | undefined { + const from = w && nodeMap.value.get(w.from.node) + if (!from) return + if (from.kind === 'bundle') { + const bits = cableBits(from) + return bits <= RIBBON_MAX ? bits * LANE * G + 6 : 8 } - const style = ed.project.value.settings.crossing ?? 'bow' + const conn = shapes.value.get(from.id)?.outputs.find(p => p.key === w.from.pin)?.conn + if (conn) return Math.min(8, Math.max(2, ed.getDef.value.conn?.(conn)?.lines.length ?? 2)) * LANE * G + 6 +} + +const routes = computed(() => { const byId = new Map(def.value.wires.map(w => [w.id, w])) + const routed = circuitRoutes(def.value, shapes.value, { tidy: !!ed.project.value.settings.tidy, widthOf: id => cablePx(byId.get(id)) }) + const hops = planHops(routed) + const style = ed.project.value.settings.crossing ?? 'bow' return routed.map((r, k) => { const wire = byId.get(r.id)! const from = nodeMap.value.get(wire.from.node) const pin = from && shapes.value.get(from.id)?.outputs.find(p => p.key === wire.from.pin) - const route: Route = { wire, pts: r.grid, draw: r.draw, d: wirePath(r.draw, hops.get(k), style), src: r.net, kind: pin ? pinKindOf(pin) : '' } + const route: Route = { wire, pts: r.grid, draw: r.draw, d: wirePath(r.draw, hops.get(k), style), src: r.net, kind: pin ? pinKindOf(pin) : '', hops: !!hops.get(k)?.size } if (pin?.conn) { // A cable between connector ports: a sleeve in its type's colour, named. const type = ed.getDef.value.conn?.(pin.conn) route.port = { name: type?.name ?? '?', color: type?.color } - route.d = toPath(r.draw) route.sleeve = Math.min(8, Math.max(2, type?.lines.length ?? 2)) * LANE * G + 3 route.mark = midOfLongest(r.draw) } @@ -94,8 +103,7 @@ const routes = computed(() => { if (route.bits <= RIBBON_MAX) { // Each wire keeps the colour of the wire that runs into the bundle. const feed = new Map(def.value.wires.filter(w => w.to.node === from.id).map(w => [w.to.pin, w.color])) - route.d = toPath(r.draw) - route.lanes = ribbon(r.draw, route.bits).map((lane, i) => ({ i, d: toPath(lane), color: feed.get(`b${i}`) })) + route.lanes = ribbon(r.draw, route.bits).map((lane, i) => ({ i, d: wirePath(lane, hops.get(k), style), color: feed.get(`b${i}`) })) route.sleeve = route.bits * LANE * G + 3 } // The width mark sits on the middle of the longest straight run. @@ -107,6 +115,34 @@ const routes = computed(() => { const routeOf = (wire: Wire) => routes.value.find(r => r.wire === wire) +/** Classes of a wire's group: its colour, its signal's value, and how it is being used. */ +function wireClass(r: Route) { + return [ + r.wire.color ? `c-${r.wire.color}` : r.port?.color ? `c-${r.port.color}` : '', + { + on: srcVal(r.src) === 1, + 'port-cable': !!r.port, + selected: wireSelected.value.has(r.wire.id), + hover: hoverSrc.value === r.src, + cable: !!r.bits, + 'drop-target': dropWire.value === r.wire.id, + moving: movingWire.value === r.wire.id, + }, + ] +} + +/** + * Sleeved cables, grouped by signal: each group is drawn outline first, then + * core, then lines, so cables of one signal that share a trunk merge into + * one cable that splits. Groups that hop over others come later, on top. + */ +const cableGroups = computed(() => { + const groups = new Map() + for (const r of routes.value) if (r.lanes || r.port) groups.set(r.src, [...(groups.get(r.src) ?? []), r]) + const hopping = (g: Route[]) => (g.some(r => r.hops) ? 1 : 0) + return [...groups.values()].sort((a, b) => hopping(a) - hopping(b)) +}) + /** The number a cable carries right now: wire 0 is the lowest bit. */ function cableValue(r: Route): { dec: string; tip: string } | null { if (!r.bits) return null @@ -1431,42 +1467,28 @@ defineExpose({ fit, zoomAt }) - - - -