A input panels, automatic names, one-by-one wiring, tidy routing, truth table export
- Input panel part: rows of switches, buttons and clocks. "Attach input
panel" docks one to a component with a row per free input; it moves
with the component and follows its pins.
- Copies and new pins continue name series (D0 -> D1, A -> B); unnamed
switches, lamps and pins take the name of the component pin they are
wired to.
- Connecting: target pins are ringed, dropping on a part uses its next
free pin, click-then-click works, and "Connect one by one" (W) wires a
selection's pins in turn.
- Tidy wiring option and "Tidy all wires": sorted, evenly spaced columns
per gap, one trunk per signal, fewer crossings.
- Truth table full screen, CSV download, copy as Markdown, current row
highlighted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:17:25 +00:00
|
|
|
// Input panels: one part with a column of switches, buttons and clocks.
|
|
|
|
|
// A panel can be wired like any other part, or docked to a component, where
|
|
|
|
|
// it gets one row per free input pin. A docked panel moves with its
|
|
|
|
|
// component and follows it when the component gains or loses pins.
|
|
|
|
|
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
import { chipPins, panelOffsets, rowPin, shapeOf, type DefLookup } from './geometry'
|
A input panels, automatic names, one-by-one wiring, tidy routing, truth table export
- Input panel part: rows of switches, buttons and clocks. "Attach input
panel" docks one to a component with a row per free input; it moves
with the component and follows its pins.
- Copies and new pins continue name series (D0 -> D1, A -> B); unnamed
switches, lamps and pins take the name of the component pin they are
wired to.
- Connecting: target pins are ringed, dropping on a part uses its next
free pin, click-then-click works, and "Connect one by one" (W) wires a
selection's pins in turn.
- Tidy wiring option and "Tidy all wires": sorted, evenly spaced columns
per gap, one trunk per signal, fewer crossings.
- Truth table full screen, CSV download, copy as Markdown, current row
highlighted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:17:25 +00:00
|
|
|
import { lookup, uid } from './ops'
|
|
|
|
|
import type { CircuitNode, ComponentDef, PanelRow, Project } from './types'
|
|
|
|
|
|
|
|
|
|
/** Free space between a docked panel and its component, in grid units. */
|
|
|
|
|
const DOCK_GAP = 3
|
|
|
|
|
|
|
|
|
|
export function newPanelRows(count = 4): PanelRow[] {
|
|
|
|
|
return Array.from({ length: count }, () => ({ id: uid('r'), type: 'switch' as const }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Panels docked to one of the given chip nodes. */
|
|
|
|
|
export function dockedPanels(def: ComponentDef, chipIds: Set<string>): CircuitNode[] {
|
|
|
|
|
return def.nodes.filter(n => n.kind === 'panel' && n.attach && chipIds.has(n.attach))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ChipInput {
|
|
|
|
|
key: string
|
|
|
|
|
label: string
|
|
|
|
|
dy: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function chipInputs(chip: CircuitNode, get: DefLookup): ChipInput[] | null {
|
|
|
|
|
const child = chip.kind === 'chip' && chip.chip ? get(chip.chip) : undefined
|
|
|
|
|
if (!child) return null
|
|
|
|
|
const labels = new Map(chipPins(child).inputs.map(p => [p.key, p.label]))
|
|
|
|
|
return shapeOf(chip, get).inputs.map(p => ({ key: p.key, label: labels.get(p.key) ?? p.label, dy: p.dy }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Lines the rows up with the chip's pins and names them after the pins. */
|
|
|
|
|
function layout(panel: CircuitNode, pins: ChipInput[]) {
|
|
|
|
|
const byKey = new Map(pins.map(p => [p.key, p]))
|
|
|
|
|
const order = new Map(pins.map((p, i) => [p.key, i]))
|
|
|
|
|
const rows = (panel.rows ?? []).filter(r => byKey.has(r.id)).sort((a, b) => order.get(a.id)! - order.get(b.id)!)
|
|
|
|
|
const top = rows.length ? byKey.get(rows[0]!.id)!.dy : 0
|
|
|
|
|
for (const r of rows) {
|
|
|
|
|
const p = byKey.get(r.id)!
|
|
|
|
|
r.dy = p.dy - top
|
|
|
|
|
r.name = p.label
|
|
|
|
|
}
|
|
|
|
|
panel.rows = rows
|
|
|
|
|
return top
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Docks a new panel to the left of a placed component, with a switch for
|
|
|
|
|
* every input that is not wired yet. Returns the panel, or null when every
|
|
|
|
|
* input is already in use.
|
|
|
|
|
*/
|
|
|
|
|
export function attachPanel(project: Project, def: ComponentDef, chipId: string): CircuitNode | null {
|
|
|
|
|
const get = lookup(project)
|
|
|
|
|
const chip = def.nodes.find(n => n.id === chipId)
|
|
|
|
|
const pins = chip && chipInputs(chip, get)
|
|
|
|
|
if (!chip || !pins) return null
|
|
|
|
|
const wired = new Set(def.wires.filter(w => w.to.node === chipId).map(w => w.to.pin))
|
|
|
|
|
const free = pins.filter(p => !wired.has(p.key))
|
|
|
|
|
if (!free.length) return null
|
|
|
|
|
const panel: CircuitNode = {
|
|
|
|
|
id: uid('n'),
|
|
|
|
|
kind: 'panel',
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
attach: chipId,
|
|
|
|
|
rows: free.map(p => ({ id: p.key, type: 'switch' })),
|
|
|
|
|
}
|
|
|
|
|
if (free.length < pins.length) panel.skip = pins.filter(p => wired.has(p.key)).map(p => p.key)
|
|
|
|
|
const top = layout(panel, pins)
|
|
|
|
|
panel.x = chip.x - DOCK_GAP - shapeOf(panel, get).body.w
|
|
|
|
|
panel.y = chip.y + top
|
|
|
|
|
def.nodes.push(panel)
|
|
|
|
|
for (const r of panel.rows!) def.wires.push({ id: uid('w'), from: { node: panel.id, pin: r.id }, to: { node: chipId, pin: r.id } })
|
|
|
|
|
return panel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Keeps docked panels in step with their components: rows for pins that are
|
|
|
|
|
* gone are dropped, new free pins get a switch, and rows stay lined up with
|
|
|
|
|
* the pins. A panel whose component was removed is undocked. Runs on every
|
|
|
|
|
* structural change; wires to dropped rows are cleaned up afterwards.
|
|
|
|
|
*/
|
|
|
|
|
export function syncPanels(project: Project) {
|
|
|
|
|
const get = lookup(project)
|
|
|
|
|
for (const def of project.components) {
|
|
|
|
|
if (!def.nodes.some(n => n.kind === 'panel' && n.attach)) continue
|
|
|
|
|
const nodes = new Map(def.nodes.map(n => [n.id, n]))
|
|
|
|
|
for (const panel of def.nodes) {
|
|
|
|
|
if (panel.kind !== 'panel' || !panel.attach) continue
|
|
|
|
|
const chip = nodes.get(panel.attach)
|
|
|
|
|
const pins = chip && chipInputs(chip, get)
|
|
|
|
|
if (!chip || !pins) {
|
|
|
|
|
delete panel.attach
|
|
|
|
|
delete panel.skip
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
const keys = new Set(pins.map(p => p.key))
|
|
|
|
|
const rows = panel.rows ?? []
|
|
|
|
|
const skip = (panel.skip ?? []).filter(k => keys.has(k))
|
|
|
|
|
const known = new Set([...rows.map(r => r.id), ...skip])
|
|
|
|
|
const wired = new Set(def.wires.filter(w => w.to.node === chip.id).map(w => w.to.pin))
|
|
|
|
|
for (const p of pins) {
|
|
|
|
|
if (known.has(p.key)) continue
|
|
|
|
|
if (wired.has(p.key)) skip.push(p.key)
|
|
|
|
|
else {
|
|
|
|
|
rows.push({ id: p.key, type: 'switch' })
|
|
|
|
|
def.wires.push({ id: uid('w'), from: { node: panel.id, pin: p.key }, to: { node: chip.id, pin: p.key } })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
panel.rows = rows
|
|
|
|
|
if (skip.length) panel.skip = skip
|
|
|
|
|
else delete panel.skip
|
|
|
|
|
const top = layout(panel, pins)
|
|
|
|
|
// Keep the first row on its pin, wherever the panel was moved sideways.
|
|
|
|
|
if (panel.rows.length) panel.y = chip.y + top
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Removes one row; on a docked panel that pin is left alone from then on. */
|
|
|
|
|
export function removeRow(def: ComponentDef, panel: CircuitNode, rowId: string) {
|
|
|
|
|
panel.rows = (panel.rows ?? []).filter(r => r.id !== rowId)
|
|
|
|
|
def.wires = def.wires.filter(w => !(w.from.node === panel.id && w.from.pin === rowId))
|
|
|
|
|
if (panel.attach) panel.skip = [...new Set([...(panel.skip ?? []), rowId])]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A docked panel's pins drive the component; undocking keeps rows, wires and position. */
|
|
|
|
|
export function detachPanel(panel: CircuitNode) {
|
|
|
|
|
delete panel.attach
|
|
|
|
|
delete panel.skip
|
|
|
|
|
}
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Joins panels into one: the top one keeps its rows, the others' rows follow
|
|
|
|
|
* below in order, keeping their spacing, with their wires. The joined panel
|
|
|
|
|
* is free (no longer docked). Returns it with the pin keys that changed
|
|
|
|
|
* (old → new), so circuits that place this one can keep their wires; null
|
|
|
|
|
* for fewer than two panels.
|
|
|
|
|
*/
|
|
|
|
|
export function mergePanels(def: ComponentDef, ids: string[]): { panel: CircuitNode; renamed: Map<string, string> } | null {
|
|
|
|
|
const panels = def.nodes.filter(n => n.kind === 'panel' && ids.includes(n.id)).sort((a, b) => a.y - b.y || a.x - b.x)
|
|
|
|
|
if (panels.length < 2) return null
|
|
|
|
|
const [target, ...rest] = panels as [CircuitNode, ...CircuitNode[]]
|
|
|
|
|
const rows = target.rows ?? []
|
|
|
|
|
const at = panelOffsets(rows)
|
|
|
|
|
rows.forEach((r, i) => (r.dy = at[i]!))
|
|
|
|
|
let bottom = at.length ? at[at.length - 1]! : -1
|
|
|
|
|
const used = new Set(rows.map(r => r.id))
|
|
|
|
|
const renamed = new Map<string, string>()
|
|
|
|
|
for (const p of rest) {
|
|
|
|
|
const theirs = p.rows ?? []
|
|
|
|
|
const off = panelOffsets(theirs)
|
|
|
|
|
const shift = bottom + 1 - (off[0] ?? 0)
|
|
|
|
|
theirs.forEach((r, i) => {
|
|
|
|
|
const id = used.has(r.id) ? uid('r') : r.id
|
|
|
|
|
used.add(id)
|
|
|
|
|
for (const w of def.wires) if (w.from.node === p.id && w.from.pin === r.id) w.from = { node: target.id, pin: id }
|
|
|
|
|
renamed.set(rowPin(p.id, r.id), rowPin(target.id, id))
|
|
|
|
|
rows.push({ ...r, id, dy: off[i]! + shift })
|
|
|
|
|
})
|
|
|
|
|
if (theirs.length) bottom = rows[rows.length - 1]!.dy!
|
|
|
|
|
}
|
|
|
|
|
target.rows = rows
|
|
|
|
|
delete target.attach
|
|
|
|
|
delete target.skip
|
|
|
|
|
const gone = new Set(rest.map(p => p.id))
|
|
|
|
|
def.nodes = def.nodes.filter(n => !gone.has(n.id))
|
|
|
|
|
def.wires = def.wires.filter(w => !gone.has(w.from.node) && !gone.has(w.to.node))
|
|
|
|
|
return { panel: target, renamed }
|
|
|
|
|
}
|