R slim cable ends: free ends are just their wires running into the ribbon, names only where nothing else shows them
- A free bundle or split has no box any more: each wire bends straight into its own lane of the cable, lit and coloured like the wire; a dashed outline shows on hover to grab it - A wire's name shows next to a free end only when the part at the other end doesn't show it already (a switch or lamp of that name, a component pin) - Names no longer widen a cable end, so its pins never move Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1741631ab0
commit
81a33b9b4b
|
|
@ -794,12 +794,36 @@ kbd {
|
|||
stroke: #7d8aa0;
|
||||
}
|
||||
|
||||
.node .cable-fan {
|
||||
fill: none;
|
||||
stroke: #7d8aa0;
|
||||
stroke-width: 1.2;
|
||||
/* A free cable end is only its wires running into the cable; this is where to grab it. */
|
||||
.node .cable-hit {
|
||||
fill: transparent;
|
||||
stroke: transparent;
|
||||
}
|
||||
|
||||
.node:hover .cable-hit,
|
||||
.node.ghost .cable-hit {
|
||||
stroke: rgb(125 138 160 / 0.5);
|
||||
stroke-dasharray: 3 3;
|
||||
}
|
||||
|
||||
.harness {
|
||||
--w-on: var(--on);
|
||||
--w-off: var(--off);
|
||||
fill: none;
|
||||
stroke: var(--w-off);
|
||||
stroke-width: 1.6;
|
||||
stroke-linejoin: round;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.harness.on { stroke: var(--w-on); }
|
||||
.harness.c-red { --w-on: #ff5c5c; --w-off: #6b3337; }
|
||||
.harness.c-orange { --w-on: #ff9f43; --w-off: #6b4a2b; }
|
||||
.harness.c-yellow { --w-on: #ffd93d; --w-off: #625a2e; }
|
||||
.harness.c-blue { --w-on: #4dabff; --w-off: #2b4a6b; }
|
||||
.harness.c-purple { --w-on: #b388ff; --w-off: #463768; }
|
||||
.harness.c-pink { --w-on: #ff7ac6; --w-off: #673657; }
|
||||
|
||||
.node .body.cable-end.plugged {
|
||||
fill: #253447;
|
||||
stroke: #6fa3cf;
|
||||
|
|
@ -819,7 +843,7 @@ kbd {
|
|||
|
||||
.node .cable-name {
|
||||
font-size: 9px;
|
||||
fill: var(--text);
|
||||
fill: var(--muted);
|
||||
}
|
||||
|
||||
/* Pixel and pixel screens. */
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import type { CircuitNode, PanelRow, PinRef, Wire } from '~/lib/logic/types'
|
|||
import { branchCorners, branchTo, moveCorner, moveSegment, routeTouchesBox, segmentDrag, splitDrag } from '~/lib/logic/wires'
|
||||
import { circuitRoutes, findCrossings } from '~/lib/logic/routing'
|
||||
import { seriesLinks } from '~/lib/logic/bulk'
|
||||
import { LANE, ribbon } from '~/lib/logic/cables'
|
||||
import { LANE, ribbon, RIBBON_MAX } from '~/lib/logic/cables'
|
||||
import { wirePath } from '~/lib/draw'
|
||||
import { pinName } from '~/lib/logic/labels'
|
||||
|
||||
|
|
@ -40,9 +40,6 @@ interface Route {
|
|||
sleeve?: number
|
||||
}
|
||||
|
||||
/** Cables with up to this many wires show each wire as its own line. */
|
||||
const RIBBON_MAX = 16
|
||||
|
||||
const toPath = (pts: Pt[]) => pts.map(([x, y], i) => `${i ? 'L' : 'M'}${x * G} ${y * G}`).join('')
|
||||
|
||||
const routes = computed<Route[]>(() => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<script setup lang="ts">
|
||||
import { gateDrawing, sevenSegment } from '~/lib/draw'
|
||||
import { cableNames, GRID as G, noteLines, noteScale, PANEL_CONTROL_W, type Shape } from '~/lib/logic/geometry'
|
||||
import { LANE, nameTags, RIBBON_MAX } from '~/lib/logic/cables'
|
||||
import { GRID as G, noteLines, noteScale, PANEL_CONTROL_W, type Shape } from '~/lib/logic/geometry'
|
||||
import type { CircuitNode } from '~/lib/logic/types'
|
||||
|
||||
const props = defineProps<{
|
||||
|
|
@ -27,22 +28,55 @@ const own = computed(() => (props.node.autoLabel ? '' : (props.node.label?.trim(
|
|||
const ioText = computed(() => own.value || props.hint || props.node.label?.trim() || '')
|
||||
const ghosted = computed(() => !own.value && !!ioText.value)
|
||||
|
||||
/** Width of the names column of a cable end, on its cable side. */
|
||||
const nameCol = computed(() => b.value.w - 1)
|
||||
const names = computed(() => (props.node.kind === 'bundle' || props.node.kind === 'split' ? cableNames(props.node) : []))
|
||||
const cableEnd = computed(() => props.node.kind === 'bundle' || props.node.kind === 'split')
|
||||
|
||||
/** Lines inside a bundle or split from each single wire (or its name) to the cable pin. */
|
||||
const cableFan = computed(() => {
|
||||
const s = props.shape
|
||||
const cable = [...s.inputs, ...s.outputs].find(p => p.bus)
|
||||
const singles = [...s.inputs, ...s.outputs].filter(p => !p.bus)
|
||||
if (!cable) return ''
|
||||
const bundle = props.node.kind === 'bundle'
|
||||
const edge = (bundle ? nameCol.value : 1 - nameCol.value) * G
|
||||
const cx = (bundle ? nameCol.value + 0.75 : 0.25 - nameCol.value) * G
|
||||
return singles.map(p => `M${edge} ${p.dy * G}L${cx} ${cable.dy * G}`).join('') + `M${cx} ${cable.dy * G}L${cable.dx * G} ${cable.dy * G}`
|
||||
/** What is wired to a cable end: which single wires, in which colour, whether the cable is, and the names to show. */
|
||||
const cableLinks = computed(() => {
|
||||
if (!cableEnd.value || props.ghost) return null
|
||||
const d = ed.def.value
|
||||
const id = props.node.id
|
||||
const inward = props.node.kind === 'bundle'
|
||||
const linked = new Set<string>()
|
||||
const colors = new Map<string, string>()
|
||||
let bus = false
|
||||
for (const w of d.wires) {
|
||||
const single = inward ? w.to.node === id && w.to.pin : w.from.node === id && w.from.pin
|
||||
if (single) {
|
||||
linked.add(single)
|
||||
if (w.color && !colors.has(single)) colors.set(single, w.color)
|
||||
}
|
||||
if (inward ? w.from.node === id : w.to.node === id) bus = true
|
||||
}
|
||||
return { linked, colors, bus, tags: nameTags(d, props.node, ed.getDef.value) }
|
||||
})
|
||||
|
||||
/**
|
||||
* Each single wire of a cable end runs on into its own line of the ribbon:
|
||||
* a short straight piece at the pin, then over to its lane at the cable pin.
|
||||
*/
|
||||
const harness = computed(() => {
|
||||
const s = props.shape
|
||||
const bus = [...s.inputs, ...s.outputs].find(p => p.bus)
|
||||
const singles = props.node.kind === 'bundle' ? s.inputs : s.outputs
|
||||
if (!bus) return []
|
||||
const n = singles.length
|
||||
const lane = n <= RIBBON_MAX ? LANE : 0
|
||||
const dir = props.node.kind === 'bundle' ? 1 : -1
|
||||
return singles.map((p, i) => {
|
||||
const x = p.dx * G
|
||||
const bx = bus.dx * G
|
||||
const y = (bus.dy + (i - (n - 1) / 2) * lane) * G
|
||||
return { key: p.key, d: `M${x} ${p.dy * G}H${x + dir * 0.2 * G}L${bx - dir * 0.2 * G} ${y}H${bx}` }
|
||||
})
|
||||
})
|
||||
|
||||
/** Free cable ends hide the dots where wires join them, so the wires run straight on. */
|
||||
function dot(key: string): boolean {
|
||||
const c = cableLinks.value
|
||||
if (!c || props.node.plug) return true
|
||||
return key === 'q' || key === 'a' ? !c.bus : !c.linked.has(key)
|
||||
}
|
||||
|
||||
/** The edge a plugged cable end touches its part with. */
|
||||
const seam = computed(() => (props.node.kind === 'bundle' ? 0 : G))
|
||||
|
||||
|
|
@ -176,18 +210,25 @@ const title = computed(() => {
|
|||
</template>
|
||||
|
||||
<!-- Cable ends: a bundle gathers wires into one cable, a split fans it out -->
|
||||
<template v-else-if="node.kind === 'bundle' || node.kind === 'split'">
|
||||
<rect :x="b.x * G" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="2" class="body cable-end" :class="{ plugged: !!node.plug }" />
|
||||
<path :d="cableFan" class="cable-fan" />
|
||||
<template v-if="names.length">
|
||||
<template v-else-if="cableEnd">
|
||||
<rect v-if="node.plug" :x="b.x * G" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="2" class="body cable-end plugged" />
|
||||
<rect v-else :x="b.x * G - 2" :y="b.y * G" :width="b.w * G + 4" :height="b.h * G" rx="3" class="cable-hit" />
|
||||
<path
|
||||
v-for="h in harness"
|
||||
:key="h.key"
|
||||
:d="h.d"
|
||||
class="harness"
|
||||
:class="[cableLinks?.colors.get(h.key) ? `c-${cableLinks.colors.get(h.key)}` : '', { on: v(h.key) === 1 }]"
|
||||
/>
|
||||
<template v-if="cableLinks?.tags.length">
|
||||
<text
|
||||
v-for="(p, i) in node.kind === 'bundle' ? shape.inputs : shape.outputs"
|
||||
:key="p.key"
|
||||
:x="node.kind === 'bundle' ? 4 : G - 4"
|
||||
:y="p.dy * G + 3.5"
|
||||
:text-anchor="node.kind === 'bundle' ? 'start' : 'end'"
|
||||
:x="node.kind === 'bundle' ? -4 : G + 4"
|
||||
:y="p.dy * G - 3"
|
||||
:text-anchor="node.kind === 'bundle' ? 'end' : 'start'"
|
||||
class="pin-label cable-name"
|
||||
>{{ names[i] }}</text>
|
||||
>{{ cableLinks.tags[i] }}</text>
|
||||
</template>
|
||||
<!-- Plugged in: a contact strip along the part's edge, held by a latch at each end -->
|
||||
<g v-if="node.plug" class="latch">
|
||||
|
|
@ -256,11 +297,11 @@ const title = computed(() => {
|
|||
<template v-if="!ghost">
|
||||
<g v-for="p in shape.inputs" :key="`i-${p.key}`">
|
||||
<circle :cx="p.dx * G" :cy="p.dy * G" r="7" class="pin-hit" :data-node="node.id" :data-pin="p.key" data-dir="in" />
|
||||
<circle :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
|
||||
<circle v-if="dot(p.key)" :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
|
||||
</g>
|
||||
<g v-for="p in shape.outputs" :key="`o-${p.key}`">
|
||||
<circle :cx="p.dx * G" :cy="p.dy * G" r="7" class="pin-hit" :data-node="node.id" :data-pin="p.key" data-dir="out" />
|
||||
<circle :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
|
||||
<circle v-if="dot(p.key)" :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
|
||||
</g>
|
||||
</template>
|
||||
</g>
|
||||
|
|
|
|||
|
|
@ -265,6 +265,51 @@ export function syncCables(project: Project) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a part already shows `name` at the pin a cable wire meets: a
|
||||
* switch, lamp or pin called that (or named after the wire, dimmed), a
|
||||
* panel row or a component pin of that name.
|
||||
*/
|
||||
function showsName(node: CircuitNode, pin: string, name: string, get: DefLookup): boolean {
|
||||
switch (node.kind) {
|
||||
case 'input':
|
||||
case 'button':
|
||||
case 'clock':
|
||||
case 'output':
|
||||
case 'lamp':
|
||||
return !!node.autoLabel || !node.label?.trim() || node.label.trim() === name
|
||||
case 'panel': {
|
||||
const row = node.rows?.find(r => r.id === pin)
|
||||
return (row?.label?.trim() || row?.name?.trim()) === name
|
||||
}
|
||||
case 'chip':
|
||||
return pinName(node, pin, get) === name
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The names a free cable end shows next to its wires, '' where the part at
|
||||
* the other end of a wire shows that name already. None on a plugged end:
|
||||
* the part it sits on shows them.
|
||||
*/
|
||||
export function nameTags(def: ComponentDef, end: CircuitNode, get: DefLookup): string[] {
|
||||
if (end.plug || !end.names?.some(Boolean)) return []
|
||||
const nodes = new Map(def.nodes.map(n => [n.id, n]))
|
||||
const wires = singleWires(def, end)
|
||||
return Array.from({ length: cableBits(end) }, (_, i) => {
|
||||
const name = end.names![i]?.trim() ?? ''
|
||||
const links = wires.filter(w => bitOf(end, w) === i)
|
||||
const known = links.length > 0 && links.every(w => {
|
||||
const far = farEnd(end, w)
|
||||
const other = nodes.get(far.node)
|
||||
return !!other && showsName(other, far.pin, name, get)
|
||||
})
|
||||
return known ? '' : name
|
||||
})
|
||||
}
|
||||
|
||||
/** Cable ends plugged into one of the given parts: they move along with them. */
|
||||
export function pluggedInto(def: ComponentDef, ids: Set<string>): CircuitNode[] {
|
||||
return def.nodes.filter(n => n.plug && ids.has(n.plug))
|
||||
|
|
@ -364,6 +409,9 @@ export function offsetPath(pts: Pt[], d: number): Pt[] {
|
|||
/** Space between the lines of a ribbon cable, in grid units. */
|
||||
export const LANE = 0.14
|
||||
|
||||
/** Cables with up to this many wires show each wire as its own line. */
|
||||
export const RIBBON_MAX = 16
|
||||
|
||||
/** One line per wire of a cable, wire 0 on top (on the left of the direction of travel). */
|
||||
export function ribbon(pts: Pt[], bits: number): Pt[][] {
|
||||
return Array.from({ length: bits }, (_, i) => offsetPath(pts, (i - (bits - 1) / 2) * LANE))
|
||||
|
|
|
|||
|
|
@ -238,36 +238,17 @@ export function cableOffsets(node: CircuitNode): number[] {
|
|||
return Array.from({ length: n }, (_, i) => i * pitch)
|
||||
}
|
||||
|
||||
/** Names a cable end shows next to its wires: none while plugged in, the part shows them. */
|
||||
export function cableNames(node: CircuitNode): string[] {
|
||||
if (node.plug || !node.names?.some(Boolean)) return []
|
||||
return Array.from({ length: cableBits(node) }, (_, i) => node.names![i] ?? '')
|
||||
}
|
||||
|
||||
/**
|
||||
* Bundle: one input per wire on the left, the cable leaves on the right.
|
||||
* Split: the mirror image. Names take a column on the cable side, so the
|
||||
* single wires keep their place.
|
||||
*/
|
||||
/** Bundle: one input per wire on the left, the cable leaves on the right. Split: the mirror image. */
|
||||
function cableShape(node: CircuitNode): Shape {
|
||||
const dys = cableOffsets(node)
|
||||
const names = cableNames(node)
|
||||
const side = names.length ? Math.ceil(Math.max(...names.map(l => l.length)) * LABEL_CHAR + 0.5) : 0
|
||||
const wires = dys.map((dy, i) => ({ key: `b${i}`, label: names[i] || String(i), dy }))
|
||||
const wires = dys.map((dy, i) => ({ key: `b${i}`, label: node.names?.[i] || String(i), dy }))
|
||||
const last = dys[dys.length - 1]!
|
||||
const mid = Math.floor(last / 2)
|
||||
const body = { x: 0, y: -0.5, w: 1, h: last + 1 }
|
||||
if (node.kind === 'bundle') {
|
||||
return {
|
||||
body: { x: 0, y: -0.5, w: 1 + side, h: last + 1 },
|
||||
inputs: wires.map(p => ({ ...p, dx: 0 })),
|
||||
outputs: [{ key: 'q', label: '', dx: 1 + side, dy: mid, bus: true }],
|
||||
}
|
||||
}
|
||||
return {
|
||||
body: { x: -side, y: -0.5, w: 1 + side, h: last + 1 },
|
||||
inputs: [{ key: 'a', label: '', dx: -side, dy: mid, bus: true }],
|
||||
outputs: wires.map(p => ({ ...p, dx: 1 })),
|
||||
return { body, inputs: wires.map(p => ({ ...p, dx: 0 })), outputs: [{ key: 'q', label: '', dx: 1, dy: mid, bus: true }] }
|
||||
}
|
||||
return { body, inputs: [{ key: 'a', label: '', dx: 0, dy: mid, bus: true }], outputs: wires.map(p => ({ ...p, dx: 1 })) }
|
||||
}
|
||||
|
||||
const PIXEL_SHAPE: Shape = {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, expect, it } from 'vitest'
|
||||
import { bundleWires, cablePanel, plugInto, syncCables } from '../app/lib/logic/cables'
|
||||
import { bundleWires, cablePanel, nameTags, plugInto, syncCables } from '../app/lib/logic/cables'
|
||||
import { compile, rowSource } from '../app/lib/logic/compile'
|
||||
import { chipPins, panelOffsets, pinPos, shapeOf } from '../app/lib/logic/geometry'
|
||||
import { copySelection, createProject, lookup, pasteClip } from '../app/lib/logic/ops'
|
||||
|
|
@ -82,13 +82,39 @@ describe('plugged cable ends', () => {
|
|||
expect(res.bundle.plug).toBe('add')
|
||||
syncCables(p)
|
||||
expect(res.split.names).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
|
||||
// The names take a column on the cable side: the wires keep their place.
|
||||
// Names never change the shape: the pins stay where they were.
|
||||
const s = shapeOf(res.split, lookup(p))
|
||||
expect(s.body).toEqual({ x: 0, y: -0.5, w: 1, h: 5 })
|
||||
expect(s.outputs.every(o => o.dx === 1)).toBe(true)
|
||||
expect(s.inputs[0]!.dx).toBeLessThan(0)
|
||||
expect(s.body.x).toBe(s.inputs[0]!.dx)
|
||||
// Plugged in, the bundle leaves the names to the component.
|
||||
expect(shapeOf(res.bundle, lookup(p)).body.w).toBe(1)
|
||||
expect(nameTags(def, res.bundle, lookup(p))).toEqual([])
|
||||
})
|
||||
|
||||
it('a free end shows only the names the part at the other end does not show already', () => {
|
||||
const { p, def } = adder()
|
||||
const get = lookup(p)
|
||||
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], get)
|
||||
if (!res.ok) throw new Error(res.error)
|
||||
syncCables(p)
|
||||
// Lamps without a name show the wire's name, dimmed.
|
||||
expect(nameTags(def, res.split, get)).toEqual(['', '', '', '', ''])
|
||||
// A lamp with another name, and a NAND gate, don't.
|
||||
node(def, 'l1').label = 'Bit 1'
|
||||
def.nodes.push({ id: 'g', kind: 'nand', x: 60, y: 30 })
|
||||
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b4')!.to = { node: 'g', pin: 'a' }
|
||||
expect(nameTags(def, res.split, get)).toEqual(['', 'S1', '', '', 'Cout'])
|
||||
})
|
||||
|
||||
it('a bundle fed by switches of the same names shows none', () => {
|
||||
const { p, def } = adder()
|
||||
const get = lookup(p)
|
||||
const ins = chipPins(get('ex-add4')!).inputs
|
||||
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 })
|
||||
def.wires = def.wires.filter(w => !['wi4', 'wi5', 'wi6', 'wi7'].includes(w.id))
|
||||
for (const i of [0, 1, 2, 3]) def.wires.push({ id: `f${i}`, from: { node: `s${i + 4}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
|
||||
syncCables(p)
|
||||
expect(node(def, 'b').names).toEqual(ins.slice(4, 8).map(x => x.label))
|
||||
expect(nameTags(def, node(def, 'b'), get)).toEqual(['', '', '', ''])
|
||||
})
|
||||
|
||||
it('names come from what drives the wires, else from what they drive', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue