135 lines
5.7 KiB
TypeScript
135 lines
5.7 KiB
TypeScript
// Automatic names. Copies and new parts continue a series (D0, D1, D2;
|
|
// A, B, C), and a switch, lamp or pin without a name of its own takes the
|
|
// name of the component pin it is wired to.
|
|
|
|
import type { DefLookup } from './geometry'
|
|
import type { CircuitNode, ComponentDef, NodeKind } from './types'
|
|
|
|
const SERIES = /^(.*?)(\d+)$/
|
|
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
/** Kinds whose labels share one set of names, so D0 the input and D0 the output can both exist. */
|
|
export function labelSpace(kind: NodeKind): string | null {
|
|
if (kind === 'input' || kind === 'button' || kind === 'clock' || kind === 'panel') return 'in'
|
|
if (kind === 'output' || kind === 'lamp' || kind === 'hex') return 'out'
|
|
if (kind === 'chip') return 'chip'
|
|
if (kind === 'tag') return 'signal'
|
|
return null
|
|
}
|
|
|
|
export function usedLabels(def: ComponentDef, space: string, except?: Set<string>): Set<string> {
|
|
const used = new Set<string>()
|
|
for (const n of def.nodes) {
|
|
if (except?.has(n.id) || labelSpace(n.kind) !== space) continue
|
|
const l = n.label?.trim()
|
|
if (l) used.add(l)
|
|
}
|
|
return used
|
|
}
|
|
|
|
/**
|
|
* The next free name after `label` in its series: D0 → D1 (D07 → D08), and a
|
|
* single capital letter → the next free letter. Null for names without a series.
|
|
*/
|
|
export function nextInSeries(label: string, used: Set<string>): string | null {
|
|
const m = SERIES.exec(label)
|
|
if (m) {
|
|
const [, stem, digits] = m
|
|
for (let i = Number(digits) + 1; ; i++) {
|
|
const next = `${stem}${String(i).padStart(digits!.length, '0')}`
|
|
if (!used.has(next)) return next
|
|
}
|
|
}
|
|
const at = label.length === 1 ? LETTERS.indexOf(label) : -1
|
|
if (at >= 0) {
|
|
for (const c of LETTERS.slice(at + 1)) if (!used.has(c)) return c
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** `base` itself when free, else a numbered variant (Cin → Cin2, D0 → D0_2). */
|
|
export function uniqueLabel(base: string, used: Set<string>): string {
|
|
if (!used.has(base)) return base
|
|
const sep = /\d$/.test(base) ? '_' : ''
|
|
for (let i = 2; ; i++) if (!used.has(`${base}${sep}${i}`)) return `${base}${sep}${i}`
|
|
}
|
|
|
|
/**
|
|
* Renames pasted parts so their series continue: copying D0..D3 next to
|
|
* themselves gives D4..D7. Names without a series stay as they are.
|
|
*/
|
|
export function continueSeries(def: ComponentDef, pasted: CircuitNode[]) {
|
|
const ids = new Set(pasted.map(n => n.id))
|
|
const order = (n: CircuitNode) => Number(SERIES.exec(n.label?.trim() ?? '')?.[2] ?? Infinity)
|
|
const list = pasted.filter(n => labelSpace(n.kind) && n.label?.trim()).sort((a, b) => order(a) - order(b) || a.y - b.y || a.x - b.x)
|
|
const used = new Map<string, Set<string>>()
|
|
for (const n of list) {
|
|
const space = labelSpace(n.kind)!
|
|
if (!used.has(space)) used.set(space, usedLabels(def, space, ids))
|
|
const taken = used.get(space)!
|
|
const label = n.label!.trim()
|
|
const next = taken.has(label) ? nextInSeries(label, taken) : label
|
|
if (next) n.label = next
|
|
taken.add(n.label!)
|
|
}
|
|
}
|
|
|
|
/** Name of a component pin as its inside calls it, without the in1/out1 fallback. */
|
|
export function pinName(node: CircuitNode, pin: string, get: DefLookup): string | undefined {
|
|
if (node.kind === 'panel') {
|
|
const row = node.rows?.find(r => r.id === pin)
|
|
return row?.label?.trim() || row?.name?.trim() || undefined
|
|
}
|
|
// A wire of a cable carries the name of its signal (the cable pin itself has none).
|
|
if (node.kind === 'bundle' || node.kind === 'split') return /^b\d+$/.test(pin) ? node.names?.[Number(pin.slice(1))]?.trim() || undefined : undefined
|
|
if (node.kind !== 'chip' || !node.chip) return undefined
|
|
const def = get(node.chip)
|
|
// A pin made from a panel row: panel:row.
|
|
const at = pin.indexOf(':')
|
|
if (at > 0) {
|
|
const row = def?.nodes.find(n => n.id === pin.slice(0, at))?.rows?.find(r => r.id === pin.slice(at + 1))
|
|
return row?.label?.trim() || row?.name?.trim() || undefined
|
|
}
|
|
return def?.nodes.find(n => n.id === pin)?.label?.trim() || undefined
|
|
}
|
|
|
|
const ADOPTERS = new Set<NodeKind>(['input', 'button', 'output', 'lamp', 'tag'])
|
|
|
|
/**
|
|
* After a new wire: a switch, button, output pin or lamp that has no name
|
|
* of its own (or only an automatic one) takes the name of the component pin
|
|
* at the other end, as long as this is its only connection. A panel row
|
|
* without a name does the same. Returns true if a name changed.
|
|
*/
|
|
export function adoptName(def: ComponentDef, node: CircuitNode, pin: string, other: CircuitNode, otherPin: string, get: DefLookup): boolean {
|
|
const name = pinName(other, otherPin, get)
|
|
if (!name) return false
|
|
const links = def.wires.filter(w => (w.from.node === node.id && w.from.pin === pin) || w.to.node === node.id).length
|
|
if (links !== 1) return false
|
|
if (node.kind === 'panel') {
|
|
const row = node.rows?.find(r => r.id === pin)
|
|
if (!row || row.label?.trim() || row.name === name) return false
|
|
row.name = name
|
|
return true
|
|
}
|
|
if (!ADOPTERS.has(node.kind) || (node.label?.trim() && !node.autoLabel)) return false
|
|
const label = uniqueLabel(name, usedLabels(def, labelSpace(node.kind)!, new Set([node.id])))
|
|
if (node.label === label) return false
|
|
node.label = label
|
|
node.autoLabel = true
|
|
return true
|
|
}
|
|
|
|
/** Label for a new input or output pin: continues the series of the last one placed, else the next free letter. */
|
|
export function newPinLabel(def: ComponentDef, kind: 'input' | 'output'): string {
|
|
const used = usedLabels(def, labelSpace(kind)!)
|
|
const last = def.nodes.filter(n => n.kind === kind && n.label?.trim()).at(-1)?.label?.trim()
|
|
if (last && SERIES.test(last)) return nextInSeries(last, used)!
|
|
if (kind === 'input') {
|
|
for (const c of 'ABCDEFGHIJKLMNOPRSTUVWXYZ') if (!used.has(c)) return c
|
|
return uniqueLabel('I1', used)
|
|
}
|
|
if (!used.has('Q')) return 'Q'
|
|
for (let i = 1; ; i++) if (!used.has(`Q${i}`)) return `Q${i}`
|
|
}
|