A cable ends plug straight into parts and show their wire names, custom input panels with keys, colours and spacing
- A bundle or split whose wires all meet one part sits on its pins with a latch, moves with the part, and comes loose when dragged away; Ctrl+G plugs both ends in when it can - Cable ends take the spacing of the pins they plug into, or a spacing you set - Each wire of a cable carries a name (S0..S3, Cout), shown on free cable ends and as dimmed names on what they feed - Bundles get "Attach input panel"; panel rows get a keyboard key, an on colour, space above and up/down order Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
4fddb8cd34
commit
1741631ab0
|
|
@ -800,6 +800,28 @@ kbd {
|
||||||
stroke-width: 1.2;
|
stroke-width: 1.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.node .body.cable-end.plugged {
|
||||||
|
fill: #253447;
|
||||||
|
stroke: #6fa3cf;
|
||||||
|
}
|
||||||
|
|
||||||
|
.node .latch rect {
|
||||||
|
fill: #6fa3cf;
|
||||||
|
stroke: #1b2633;
|
||||||
|
stroke-width: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.node .latch .contact {
|
||||||
|
stroke: #9fd0f5;
|
||||||
|
stroke-width: 2.5;
|
||||||
|
stroke-linecap: round;
|
||||||
|
}
|
||||||
|
|
||||||
|
.node .cable-name {
|
||||||
|
font-size: 9px;
|
||||||
|
fill: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
/* Pixel and pixel screens. */
|
/* Pixel and pixel screens. */
|
||||||
.node .pixel {
|
.node .pixel {
|
||||||
fill: #141a22;
|
fill: #141a22;
|
||||||
|
|
@ -1070,6 +1092,16 @@ kbd {
|
||||||
stroke: #ffb3b3;
|
stroke: #ffb3b3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Panel rows in a colour of their own */
|
||||||
|
.switch.on.l-yellow { fill: rgb(255 211 77 / 0.25); stroke: #ffd34d; }
|
||||||
|
.switch.on.l-red { fill: rgb(255 92 92 / 0.25); stroke: #ff5c5c; }
|
||||||
|
.switch.on.l-blue { fill: rgb(77 171 255 / 0.25); stroke: #4dabff; }
|
||||||
|
.switch.on.l-white { fill: rgb(244 246 250 / 0.22); stroke: #f4f6fa; }
|
||||||
|
.button.on.l-yellow { fill: #ffd34d; stroke: #fff0c2; }
|
||||||
|
.button.on.l-green { fill: #3ddc84; stroke: #b5f5d0; }
|
||||||
|
.button.on.l-blue { fill: #4dabff; stroke: #bfe0ff; }
|
||||||
|
.button.on.l-white { fill: #f4f6fa; stroke: #ffffff; }
|
||||||
|
|
||||||
.display {
|
.display {
|
||||||
fill: #0a0d11;
|
fill: #0a0d11;
|
||||||
}
|
}
|
||||||
|
|
@ -1711,6 +1743,55 @@ kbd {
|
||||||
flex: 0 0 56px;
|
flex: 0 0 56px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.panel-rows li {
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-rows li.open {
|
||||||
|
padding-bottom: 4px;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-rows .icon-btn.active {
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-rows .row-more {
|
||||||
|
display: flex;
|
||||||
|
flex: 1 0 100%;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px 8px;
|
||||||
|
padding-left: 26px;
|
||||||
|
font-size: 11px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-rows .row-more label {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-rows .row-more input {
|
||||||
|
flex: 0 0 40px;
|
||||||
|
width: 40px;
|
||||||
|
padding: 2px 4px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.swatches.mini {
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.swatches.mini .swatch {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lamp-swatch.default-on { background: var(--on); }
|
||||||
|
.lamp-swatch.default-on.button { background: var(--danger); }
|
||||||
|
|
||||||
.challenge .score,
|
.challenge .score,
|
||||||
.inspector .score {
|
.inspector .score {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
||||||
|
|
@ -384,7 +384,7 @@ function onPointerDown(e: PointerEvent) {
|
||||||
|
|
||||||
if (node.kind === 'button') ed.press(node, true)
|
if (node.kind === 'button') ed.press(node, true)
|
||||||
if (row && !multi) ed.pressRow(node, row, true)
|
if (row && !multi) ed.pressRow(node, row, true)
|
||||||
const ids = ed.withDocked(selected.value.has(id) ? ed.selection.value : [id])
|
const ids = ed.withFollowers(selected.value.has(id) ? ed.selection.value : [id])
|
||||||
const orig = new Map<string, Pt>()
|
const orig = new Map<string, Pt>()
|
||||||
const lockY = new Set<string>()
|
const lockY = new Set<string>()
|
||||||
for (const sid of ids) {
|
for (const sid of ids) {
|
||||||
|
|
@ -607,8 +607,10 @@ function onPointerUp() {
|
||||||
case 'move':
|
case 'move':
|
||||||
if (d.node.kind === 'button') ed.press(d.node, false)
|
if (d.node.kind === 'button') ed.press(d.node, false)
|
||||||
if (d.row && !d.moved) ed.pressRow(d.node, d.row, false)
|
if (d.row && !d.moved) ed.pressRow(d.node, d.row, false)
|
||||||
if (d.moved) ed.commit(false)
|
if (d.moved) {
|
||||||
else if (d.toggle && d.row) ed.toggleRow(d.node, d.row)
|
ed.unplugMoved(new Set(d.orig.keys()))
|
||||||
|
ed.commit(false)
|
||||||
|
} else if (d.toggle && d.row) ed.toggleRow(d.node, d.row)
|
||||||
else if (d.toggle) ed.toggleInput(d.node)
|
else if (d.toggle) ed.toggleInput(d.node)
|
||||||
break
|
break
|
||||||
case 'wire': {
|
case 'wire': {
|
||||||
|
|
@ -667,8 +669,9 @@ const ARROWS: Record<string, Pt> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], Arr
|
||||||
|
|
||||||
/** Arrow keys: move the selected parts and wires one grid step (four with Shift). */
|
/** Arrow keys: move the selected parts and wires one grid step (four with Shift). */
|
||||||
function nudge(dx: number, dy: number) {
|
function nudge(dx: number, dy: number) {
|
||||||
const ids = ed.withDocked(ed.selection.value)
|
const ids = ed.withFollowers(ed.selection.value)
|
||||||
if (!ids.size && !ed.wireSel.value.length) return
|
if (!ids.size && !ed.wireSel.value.length) return
|
||||||
|
ed.unplugMoved(ids)
|
||||||
for (const n of def.value.nodes) {
|
for (const n of def.value.nodes) {
|
||||||
if (!ids.has(n.id)) continue
|
if (!ids.has(n.id)) continue
|
||||||
n.x += dx
|
n.x += dx
|
||||||
|
|
@ -698,12 +701,15 @@ function onKey(e: KeyboardEvent) {
|
||||||
}
|
}
|
||||||
const mod = e.ctrlKey || e.metaKey
|
const mod = e.ctrlKey || e.metaKey
|
||||||
const key = e.key.toLowerCase()
|
const key = e.key.toLowerCase()
|
||||||
// Keys bound to switches and buttons win over the shortcuts below.
|
// Keys bound to switches, buttons and panel rows win over the shortcuts below.
|
||||||
if (!mod && !e.altKey && e.key.length === 1) {
|
if (!mod && !e.altKey && e.key.length === 1) {
|
||||||
const bound = def.value.nodes.filter(n => n.key === key && (n.kind === 'input' || n.kind === 'button'))
|
const bound = def.value.nodes.filter(n => n.key === key && (n.kind === 'input' || n.kind === 'button'))
|
||||||
if (bound.length) {
|
const rows = boundRows(key)
|
||||||
|
if (bound.length || rows.length) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
if (!e.repeat) for (const n of bound) n.kind === 'input' ? ed.toggleInput(n) : ed.press(n, true)
|
if (e.repeat) return
|
||||||
|
for (const n of bound) n.kind === 'input' ? ed.toggleInput(n) : ed.press(n, true)
|
||||||
|
for (const [n, r] of rows) r.type === 'button' ? ed.pressRow(n, r, true) : ed.toggleRow(n, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -760,6 +766,14 @@ function onKeyUp(e: KeyboardEvent) {
|
||||||
if (e.key === ' ') spaceDown.value = false
|
if (e.key === ' ') spaceDown.value = false
|
||||||
const key = e.key.toLowerCase()
|
const key = e.key.toLowerCase()
|
||||||
for (const n of def.value.nodes) if (n.kind === 'button' && n.key === key) ed.press(n, false)
|
for (const n of def.value.nodes) if (n.kind === 'button' && n.key === key) ed.press(n, false)
|
||||||
|
for (const [n, r] of boundRows(key)) if (r.type === 'button') ed.pressRow(n, r, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Panel rows bound to a key. */
|
||||||
|
function boundRows(key: string): [CircuitNode, PanelRow][] {
|
||||||
|
const out: [CircuitNode, PanelRow][] = []
|
||||||
|
for (const n of def.value.nodes) if (n.kind === 'panel') for (const r of n.rows ?? []) if (r.key === key) out.push([n, r])
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Overlays ────────────────────────────────────────────────────────
|
// ── Overlays ────────────────────────────────────────────────────────
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,8 @@ const SHORTCUTS: [string, string][] = [
|
||||||
['Right or middle drag, Space + drag', 'Move the view'],
|
['Right or middle drag, Space + drag', 'Move the view'],
|
||||||
['Mouse wheel', 'Zoom'],
|
['Mouse wheel', 'Zoom'],
|
||||||
['N, I, O, L, B, C, H, T, P, X', 'Place NAND, input, output, lamp, button, clock, hex display, note, input panel, pixel'],
|
['N, I, O, L, B, C, H, T, P, X', 'Place NAND, input, output, lamp, button, clock, hex display, note, input panel, pixel'],
|
||||||
['Select wires, Bundle into cable', 'Run several wires as one thick cable; Remove cable undoes it'],
|
['Select wires, Ctrl+G', 'Bundle them into one cable; an end whose wires all meet one part plugs straight into it'],
|
||||||
|
['Drag a plugged cable end away', 'Pull it out; drop its pins on a part\'s pins to plug it in again'],
|
||||||
['Shift + click on canvas', 'Keep placing the same part'],
|
['Shift + click on canvas', 'Keep placing the same part'],
|
||||||
['Ctrl+C, Ctrl+V, Ctrl+D', 'Copy, paste, duplicate; names continue their series (D0 → D1)'],
|
['Ctrl+C, Ctrl+V, Ctrl+D', 'Copy, paste, duplicate; names continue their series (D0 → D1)'],
|
||||||
['Delete', 'Remove the selection'],
|
['Delete', 'Remove the selection'],
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
import { stats } from '~/lib/logic/analyze'
|
import { stats } from '~/lib/logic/analyze'
|
||||||
import { compile } from '~/lib/logic/compile'
|
import { compile } from '~/lib/logic/compile'
|
||||||
import { cableOf } from '~/lib/logic/cables'
|
import { cableOf } from '~/lib/logic/cables'
|
||||||
import { cableBits, chipPins, MAX_BITS, rowLabel } from '~/lib/logic/geometry'
|
import { cableBits, cableOffsets, chipPins, MAX_BITS, panelOffsets, rowLabel } from '~/lib/logic/geometry'
|
||||||
import { SINK_KINDS, SOURCE_KINDS } from '~/lib/logic/ops'
|
import { SINK_KINDS, SOURCE_KINDS } from '~/lib/logic/ops'
|
||||||
import { KIND_NAMES } from '~/lib/logic/progress'
|
import { KIND_NAMES } from '~/lib/logic/progress'
|
||||||
import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wide'
|
import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wide'
|
||||||
|
|
@ -26,10 +26,10 @@ const HINTS: Record<string, string> = {
|
||||||
button: 'Gives 1 while held down with the mouse or its key.',
|
button: 'Gives 1 while held down with the mouse or its key.',
|
||||||
lamp: 'Lights up at 1. Inside a component it also shows on the component box.',
|
lamp: 'Lights up at 1. Inside a component it also shows on the component box.',
|
||||||
hex: 'Shows inputs 1, 2, 4, 8 as one hex digit. Inside a component it also shows on the component box.',
|
hex: 'Shows inputs 1, 2, 4, 8 as one hex digit. Inside a component it also shows on the component box.',
|
||||||
bundle: 'Wire 0 (top) to the last on the left go into the cable on the right. Run the cable to a split, which gives them back in the same order.',
|
bundle: 'Wire 0 (top) to the last on the left go into the cable on the right. Run the cable to a split, which gives them back in the same order. The wires are named after what drives them, or else what they drive.',
|
||||||
split: 'Takes a cable from a bundle and gives its wires back in the same order, top to bottom.',
|
split: 'Takes a cable from a bundle and gives its wires back in the same order, top to bottom, with their names.',
|
||||||
pixel: 'Lights at 1. Inside a component, all its pixels show on the component box as one screen, without gaps: space them out inside to wire them, every column and row they sit on becomes a column and row of the screen.',
|
pixel: 'Lights at 1. Inside a component, all its pixels show on the component box as one screen, without gaps: space them out inside to wire them, every column and row they sit on becomes a column and row of the screen.',
|
||||||
panel: 'Click a row on the canvas to flip its switch or hold its button. Rows without a name take the name of the pin they are wired to.',
|
panel: 'Click a row on the canvas to flip its switch or hold its button, or press its key. Rows without a name take the name of the pin they are wired to. Open a row (the arrow) for its key, colour, spacing and order.',
|
||||||
}
|
}
|
||||||
|
|
||||||
const n = computed(() => props.node)
|
const n = computed(() => props.node)
|
||||||
|
|
@ -159,6 +159,49 @@ function setRowLabel(row: PanelRow, e: Event) {
|
||||||
ed.commit()
|
ed.commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Rows showing their extra settings (key, colour, spacing, order). */
|
||||||
|
const openRows = ref(new Set<string>())
|
||||||
|
function toggleRow(id: string) {
|
||||||
|
const s = new Set(openRows.value)
|
||||||
|
if (!s.delete(id)) s.add(id)
|
||||||
|
openRows.value = s
|
||||||
|
}
|
||||||
|
const rowGaps = computed(() => {
|
||||||
|
const at = panelOffsets(n.value.rows ?? [])
|
||||||
|
return at.map((y, i) => (i ? y - at[i - 1]! - 1 : 0))
|
||||||
|
})
|
||||||
|
|
||||||
|
function setRowKey(row: PanelRow, e: KeyboardEvent) {
|
||||||
|
if (e.key === 'Tab') return
|
||||||
|
e.preventDefault()
|
||||||
|
if (e.key === 'Backspace' || e.key === 'Delete' || e.key === 'Escape') ed.setRowKey(row, undefined)
|
||||||
|
else if (e.key.length === 1 && e.key !== ' ') ed.setRowKey(row, e.key.toLowerCase())
|
||||||
|
else return
|
||||||
|
;(e.target as HTMLInputElement).blur()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What a part is called in messages: its label, its component's name, or its kind. */
|
||||||
|
function partName(id: string | undefined): string {
|
||||||
|
const p = id ? ed.def.value.nodes.find(x => x.id === id) : undefined
|
||||||
|
if (!p) return 'part'
|
||||||
|
if (p.label?.trim() && !p.autoLabel) return p.label.trim()
|
||||||
|
if (p.kind === 'chip') return (p.chip ? ed.getDef.value(p.chip)?.name : undefined) ?? 'component'
|
||||||
|
if (p.kind === 'panel') return 'the input panel'
|
||||||
|
return (KIND_NAMES[p.kind] ?? p.kind).toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
const isCableEnd = computed(() => n.value.kind === 'bundle' || n.value.kind === 'split')
|
||||||
|
const plugPart = computed(() => (isCableEnd.value && !n.value.plug ? ed.plugTarget(n.value) : undefined))
|
||||||
|
const pitch = computed(() => {
|
||||||
|
const o = cableOffsets(n.value)
|
||||||
|
return o.every((v, i) => v === i * (o[1] ?? 1)) ? (o[1] ?? 1) : null
|
||||||
|
})
|
||||||
|
const undriven = computed(() => {
|
||||||
|
if (n.value.kind !== 'bundle') return 0
|
||||||
|
const fed = new Set(ed.def.value.wires.filter(w => w.to.node === n.value.id).map(w => w.to.pin))
|
||||||
|
return cableBits(n.value) - fed.size
|
||||||
|
})
|
||||||
|
|
||||||
function setRowHz(row: PanelRow, e: Event) {
|
function setRowHz(row: PanelRow, e: Event) {
|
||||||
const v = Number((e.target as HTMLInputElement).value)
|
const v = Number((e.target as HTMLInputElement).value)
|
||||||
if (v > 0 && v <= 1000) row.hz = v
|
if (v > 0 && v <= 1000) row.hz = v
|
||||||
|
|
@ -233,7 +276,15 @@ function setRowHz(row: PanelRow, e: Event) {
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<span>Rows</span>
|
<span>Rows</span>
|
||||||
<ul class="panel-rows">
|
<ul class="panel-rows">
|
||||||
<li v-for="(r, i) in n.rows ?? []" :key="r.id">
|
<li v-for="(r, i) in n.rows ?? []" :key="r.id" :class="{ open: openRows.has(r.id) }">
|
||||||
|
<button
|
||||||
|
class="icon-btn"
|
||||||
|
:class="{ active: openRows.has(r.id) }"
|
||||||
|
:title="openRows.has(r.id) ? 'Fewer settings' : 'Key, colour, spacing and order'"
|
||||||
|
@click="toggleRow(r.id)"
|
||||||
|
>
|
||||||
|
<Icon :name="openRows.has(r.id) ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-3.5" />
|
||||||
|
</button>
|
||||||
<input :value="r.label ?? ''" :placeholder="rowLabel({ ...r, label: undefined }, i)" title="Name" @change="setRowLabel(r, $event)" />
|
<input :value="r.label ?? ''" :placeholder="rowLabel({ ...r, label: undefined }, i)" title="Name" @change="setRowLabel(r, $event)" />
|
||||||
<select :value="r.type" title="Kind" @change="ed.setRowType(r, ($event.target as HTMLSelectElement).value as PanelControl)">
|
<select :value="r.type" title="Kind" @change="ed.setRowType(r, ($event.target as HTMLSelectElement).value as PanelControl)">
|
||||||
<option v-for="c in CONTROLS" :key="c.value" :value="c.value">{{ c.label }}</option>
|
<option v-for="c in CONTROLS" :key="c.value" :value="c.value">{{ c.label }}</option>
|
||||||
|
|
@ -251,6 +302,31 @@ function setRowHz(row: PanelRow, e: Event) {
|
||||||
@change="setRowHz(r, $event)"
|
@change="setRowHz(r, $event)"
|
||||||
/>
|
/>
|
||||||
<button class="icon-btn" title="Remove this row" @click="ed.dropRow(n, r.id)"><Icon name="lucide:x" class="size-3.5" /></button>
|
<button class="icon-btn" title="Remove this row" @click="ed.dropRow(n, r.id)"><Icon name="lucide:x" class="size-3.5" /></button>
|
||||||
|
<div v-if="openRows.has(r.id)" class="row-more">
|
||||||
|
<label v-if="r.type !== 'clock'" title="Keyboard key that flips the switch or holds the button">
|
||||||
|
<span>Key</span>
|
||||||
|
<input :value="r.key?.toUpperCase() ?? ''" readonly placeholder="–" class="key-input" @keydown="setRowKey(r, $event)" />
|
||||||
|
</label>
|
||||||
|
<label v-if="!n.attach && i > 0" title="Empty rows above this one">
|
||||||
|
<span>Space</span>
|
||||||
|
<input type="number" min="0" max="20" :value="rowGaps[i]" @change="ed.setRowGap(n, i, Number(($event.target as HTMLInputElement).value))" />
|
||||||
|
</label>
|
||||||
|
<span v-if="!n.attach" class="flex gap-0.5">
|
||||||
|
<button class="icon-btn" title="Move up" :disabled="i === 0" @click="ed.moveRow(n, i, -1)"><Icon name="lucide:arrow-up" class="size-3.5" /></button>
|
||||||
|
<button class="icon-btn" title="Move down" :disabled="i === (n.rows?.length ?? 0) - 1" @click="ed.moveRow(n, i, 1)"><Icon name="lucide:arrow-down" class="size-3.5" /></button>
|
||||||
|
</span>
|
||||||
|
<div v-if="r.type !== 'clock'" class="swatches mini" title="Colour while on">
|
||||||
|
<button class="swatch lamp-swatch default-on" :class="{ active: !r.color, button: r.type === 'button' }" title="Default" @click="ed.setRowColor(r, undefined)" />
|
||||||
|
<button
|
||||||
|
v-for="c in LAMP_COLORS"
|
||||||
|
:key="c"
|
||||||
|
class="swatch lamp-swatch"
|
||||||
|
:class="[`l-${c}`, { active: r.color === c }]"
|
||||||
|
:title="c"
|
||||||
|
@click="ed.setRowColor(r, c)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<button v-if="!n.attach" class="btn-sm self-start" @click="ed.addRow(n)"><Icon name="lucide:plus" class="size-3.5" /> Add row</button>
|
<button v-if="!n.attach" class="btn-sm self-start" @click="ed.addRow(n)"><Icon name="lucide:plus" class="size-3.5" /> Add row</button>
|
||||||
|
|
@ -315,10 +391,47 @@ function setRowHz(row: PanelRow, e: Event) {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<template v-if="n.kind === 'bundle' || n.kind === 'split'">
|
<template v-if="n.kind === 'bundle' || n.kind === 'split'">
|
||||||
|
<p v-if="n.plug" class="result pass">
|
||||||
|
<Icon name="lucide:plug-zap" class="size-4 shrink-0" />
|
||||||
|
<span class="grow">Plugged into {{ partName(n.plug) }}. It moves with it; drag it away to pull it out.</span>
|
||||||
|
<button class="btn-sm" title="Pull it out: it steps aside and its wires show again" @click="ed.unplugEnd(n)">Unplug</button>
|
||||||
|
</p>
|
||||||
|
<button
|
||||||
|
v-else-if="plugPart"
|
||||||
|
class="btn mb-2 w-full"
|
||||||
|
:title="`Sit straight on the pins of ${partName(plugPart.id)}, without wires in between`"
|
||||||
|
@click="ed.plugEnd(n)"
|
||||||
|
>
|
||||||
|
<Icon name="lucide:plug" class="size-4" /> Plug into {{ partName(plugPart.id) }}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
v-if="n.kind === 'bundle' && undriven && !n.plug"
|
||||||
|
class="btn mb-2 w-full"
|
||||||
|
title="A switch for every wire nothing drives yet; the cable plugs into it. Make each row a button or clock, give it a key or colour."
|
||||||
|
@click="ed.addCablePanel(n.id)"
|
||||||
|
>
|
||||||
|
<Icon name="lucide:sliders-vertical" class="size-4" /> Attach input panel
|
||||||
|
</button>
|
||||||
|
<div class="grid grid-cols-2 gap-2">
|
||||||
<label class="field">
|
<label class="field">
|
||||||
<span>Wires in the cable</span>
|
<span>Wires</span>
|
||||||
<input type="number" min="2" :max="MAX_BITS" step="1" :value="cableBits(n)" @change="ed.setBits(n, Number(($event.target as HTMLInputElement).value))" />
|
<input type="number" min="2" :max="MAX_BITS" step="1" :value="cableBits(n)" @change="ed.setBits(n, Number(($event.target as HTMLInputElement).value))" />
|
||||||
</label>
|
</label>
|
||||||
|
<label class="field" :title="n.plug ? 'Set by the pins it is plugged into' : 'Rows from one wire to the next, to line up with pins further apart'">
|
||||||
|
<span>Spacing</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
max="8"
|
||||||
|
step="1"
|
||||||
|
:value="pitch ?? ''"
|
||||||
|
:placeholder="pitch ? '' : 'as plugged'"
|
||||||
|
:disabled="!!n.plug"
|
||||||
|
@change="ed.setPitch(n, Number(($event.target as HTMLInputElement).value))"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<p v-if="n.names?.some(Boolean)" class="hint -mt-1">Wires: {{ n.names.map((x, i) => x || i).join(', ') }}</p>
|
||||||
<div v-if="cableBundle" class="field">
|
<div v-if="cableBundle" class="field">
|
||||||
<span>Sleeve colour (the wires inside keep theirs)</span>
|
<span>Sleeve colour (the wires inside keep theirs)</span>
|
||||||
<div class="swatches">
|
<div class="swatches">
|
||||||
|
|
@ -333,7 +446,7 @@ function setRowHz(row: PanelRow, e: Event) {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p class="hint">Drag the bundle and the split to decide where the cable starts and ends.</p>
|
<p class="hint">Drag the bundle and the split to decide where the cable starts and ends. Drop an end with its pins on a part's pins to plug it in.</p>
|
||||||
<button v-if="cableBundle" class="btn mb-2 w-full" title="Take the cable apart again into single wires" @click="ed.removeCable(cableBundle.id)">
|
<button v-if="cableBundle" class="btn mb-2 w-full" title="Take the cable apart again into single wires" @click="ed.removeCable(cableBundle.id)">
|
||||||
<Icon name="lucide:split" class="size-4" /> Remove cable
|
<Icon name="lucide:split" class="size-4" /> Remove cable
|
||||||
</button>
|
</button>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { gateDrawing, sevenSegment } from '~/lib/draw'
|
import { gateDrawing, sevenSegment } from '~/lib/draw'
|
||||||
import { GRID as G, noteLines, noteScale, PANEL_CONTROL_W, type Shape } from '~/lib/logic/geometry'
|
import { cableNames, GRID as G, noteLines, noteScale, PANEL_CONTROL_W, type Shape } from '~/lib/logic/geometry'
|
||||||
import type { CircuitNode } from '~/lib/logic/types'
|
import type { CircuitNode } from '~/lib/logic/types'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
|
|
@ -27,16 +27,25 @@ const own = computed(() => (props.node.autoLabel ? '' : (props.node.label?.trim(
|
||||||
const ioText = computed(() => own.value || props.hint || props.node.label?.trim() || '')
|
const ioText = computed(() => own.value || props.hint || props.node.label?.trim() || '')
|
||||||
const ghosted = computed(() => !own.value && !!ioText.value)
|
const ghosted = computed(() => !own.value && !!ioText.value)
|
||||||
|
|
||||||
/** Lines inside a bundle or split from each single wire to the cable pin. */
|
/** 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) : []))
|
||||||
|
|
||||||
|
/** Lines inside a bundle or split from each single wire (or its name) to the cable pin. */
|
||||||
const cableFan = computed(() => {
|
const cableFan = computed(() => {
|
||||||
const s = props.shape
|
const s = props.shape
|
||||||
const cable = [...s.inputs, ...s.outputs].find(p => p.bus)
|
const cable = [...s.inputs, ...s.outputs].find(p => p.bus)
|
||||||
const singles = [...s.inputs, ...s.outputs].filter(p => !p.bus)
|
const singles = [...s.inputs, ...s.outputs].filter(p => !p.bus)
|
||||||
if (!cable) return ''
|
if (!cable) return ''
|
||||||
const cx = props.node.kind === 'bundle' ? 0.75 * G : 0.25 * G
|
const bundle = props.node.kind === 'bundle'
|
||||||
return singles.map(p => `M${p.dx * G} ${p.dy * G}L${cx} ${cable.dy * G}`).join('') + `M${cx} ${cable.dy * G}L${cable.dx * G} ${cable.dy * G}`
|
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}`
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/** The edge a plugged cable end touches its part with. */
|
||||||
|
const seam = computed(() => (props.node.kind === 'bundle' ? 0 : G))
|
||||||
|
|
||||||
const v = (pin: string) => (props.ghost ? 0 : ed.val(props.node.id, pin))
|
const v = (pin: string) => (props.ghost ? 0 : ed.val(props.node.id, pin))
|
||||||
/** Value inside this chip, for its lamps and displays. */
|
/** Value inside this chip, for its lamps and displays. */
|
||||||
const fv = (inner: string, pin: string) => (props.ghost ? 0 : ed.faceVal(props.node.id, inner, pin))
|
const fv = (inner: string, pin: string) => (props.ghost ? 0 : ed.faceVal(props.node.id, inner, pin))
|
||||||
|
|
@ -168,8 +177,24 @@ const title = computed(() => {
|
||||||
|
|
||||||
<!-- Cable ends: a bundle gathers wires into one cable, a split fans it out -->
|
<!-- Cable ends: a bundle gathers wires into one cable, a split fans it out -->
|
||||||
<template v-else-if="node.kind === 'bundle' || node.kind === 'split'">
|
<template v-else-if="node.kind === 'bundle' || node.kind === 'split'">
|
||||||
<rect :x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="2" class="body cable-end" />
|
<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" />
|
<path :d="cableFan" class="cable-fan" />
|
||||||
|
<template v-if="names.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'"
|
||||||
|
class="pin-label cable-name"
|
||||||
|
>{{ names[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">
|
||||||
|
<path :d="`M${seam} ${b.y * G + 2}V${(b.y + b.h) * G - 2}`" class="contact" />
|
||||||
|
<rect :x="seam - 0.3 * G" :y="b.y * G - 3" :width="0.6 * G" :height="5" rx="1.5" />
|
||||||
|
<rect :x="seam - 0.3 * G" :y="(b.y + b.h) * G - 2" :width="0.6 * G" :height="5" rx="1.5" />
|
||||||
|
</g>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<!-- Pixel: one square of a screen -->
|
<!-- Pixel: one square of a screen -->
|
||||||
|
|
@ -185,9 +210,10 @@ const title = computed(() => {
|
||||||
<rect :x="0" :y="-0.5 * G" :width="b.w * G" :height="G" class="row-hit" />
|
<rect :x="0" :y="-0.5 * G" :width="b.w * G" :height="G" class="row-hit" />
|
||||||
<text :x="5" y="3.5" class="pin-label" :class="{ named: !!rows[i]?.label || !!rows[i]?.name }">{{ p.label }}</text>
|
<text :x="5" y="3.5" class="pin-label" :class="{ named: !!rows[i]?.label || !!rows[i]?.name }">{{ p.label }}</text>
|
||||||
<g :transform="`translate(${(b.w - PANEL_CONTROL_W) * G} 0)`">
|
<g :transform="`translate(${(b.w - PANEL_CONTROL_W) * G} 0)`">
|
||||||
|
<text v-if="rows[i]?.key" x="-3" y="3" text-anchor="end" class="key-badge">{{ rows[i]!.key!.toUpperCase() }}</text>
|
||||||
<template v-if="rows[i]?.type === 'button'">
|
<template v-if="rows[i]?.type === 'button'">
|
||||||
<rect x="2" :y="-0.4 * G" :width="1.3 * G" :height="0.8 * G" rx="2" class="body" />
|
<rect x="2" :y="-0.4 * G" :width="1.3 * G" :height="0.8 * G" rx="2" class="body" />
|
||||||
<circle :cx="2 + 0.65 * G" cy="0" :r="0.27 * G" class="button" :class="{ on: v(p.key) === 1 }" />
|
<circle :cx="2 + 0.65 * G" cy="0" :r="0.27 * G" class="button" :class="[rows[i]?.color ? `l-${rows[i]!.color}` : '', { on: v(p.key) === 1 }]" />
|
||||||
</template>
|
</template>
|
||||||
<path
|
<path
|
||||||
v-else-if="rows[i]?.type === 'clock'"
|
v-else-if="rows[i]?.type === 'clock'"
|
||||||
|
|
@ -196,7 +222,7 @@ const title = computed(() => {
|
||||||
:class="{ on: v(p.key) === 1 }"
|
:class="{ on: v(p.key) === 1 }"
|
||||||
/>
|
/>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<rect x="2" :y="-0.38 * G" :width="1.3 * G" :height="0.76 * G" :rx="0.38 * G" class="switch" :class="{ on: v(p.key) === 1 }" />
|
<rect x="2" :y="-0.38 * G" :width="1.3 * G" :height="0.76 * G" :rx="0.38 * G" class="switch" :class="[rows[i]?.color ? `l-${rows[i]!.color}` : '', { on: v(p.key) === 1 }]" />
|
||||||
<circle :cx="v(p.key) === 1 ? 2 + 0.92 * G : 2 + 0.38 * G" cy="0" :r="0.27 * G" class="knob" />
|
<circle :cx="v(p.key) === 1 ? 2 + 0.92 * G : 2 + 0.38 * G" cy="0" :r="0.27 * G" class="knob" />
|
||||||
</template>
|
</template>
|
||||||
<path :d="`M${1.55 * G} 0H${PANEL_CONTROL_W * G}`" class="stub" />
|
<path :d="`M${1.55 * G} 0H${PANEL_CONTROL_W * G}`" class="stub" />
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ const BASICS: { kind: NodeKind; name: string; icon: string; key?: string; hint:
|
||||||
{ kind: 'output', name: 'Output', icon: 'lucide:log-out', key: 'O', hint: 'Shows a value. Inside a component it becomes an output pin.' },
|
{ kind: 'output', name: 'Output', icon: 'lucide:log-out', key: 'O', hint: 'Shows a value. Inside a component it becomes an output pin.' },
|
||||||
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.' },
|
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.' },
|
||||||
{ kind: 'button', name: 'Push button', icon: 'lucide:circle-dot', key: 'B', hint: '1 while held down.' },
|
{ kind: 'button', name: 'Push button', icon: 'lucide:circle-dot', key: 'B', hint: '1 while held down.' },
|
||||||
{ kind: 'panel', name: 'Input panel', icon: 'lucide:sliders-vertical', key: 'P', hint: 'A column of switches, buttons and clocks. On a component, "Attach input panel" makes one for its inputs.' },
|
{ kind: 'panel', name: 'Input panel', icon: 'lucide:sliders-vertical', key: 'P', hint: 'A column of switches, buttons and clocks, each with its own name, key and colour. "Attach input panel" on a component or a cable bundle makes one for its free inputs.' },
|
||||||
{ kind: 'clock', name: 'Clock', icon: 'lucide:audio-waveform', key: 'C', hint: 'Square wave. Speed and pause are in the top bar.' },
|
{ kind: 'clock', name: 'Clock', icon: 'lucide:audio-waveform', key: 'C', hint: 'Square wave. Speed and pause are in the top bar.' },
|
||||||
{ kind: 'high', name: 'Constant 1', icon: 'lucide:plus', hint: 'Always 1, like the supply rail.' },
|
{ kind: 'high', name: 'Constant 1', icon: 'lucide:plus', hint: 'Always 1, like the supply rail.' },
|
||||||
{ kind: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.' },
|
{ kind: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.' },
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@
|
||||||
// One shared instance for the whole page.
|
// One shared instance for the whole page.
|
||||||
|
|
||||||
import { nandCounts, runSteps, stats, tableCsv, TRANSISTORS_PER_NAND, truthTable, type Stats, type TestResult, type TruthTable } from '~/lib/logic/analyze'
|
import { nandCounts, runSteps, stats, tableCsv, TRANSISTORS_PER_NAND, truthTable, type Stats, type TestResult, type TruthTable } from '~/lib/logic/analyze'
|
||||||
import { bundleWires, dissolveCable } from '~/lib/logic/cables'
|
import { bundleWires, cablePanel, dissolveCable, pluggedInto, plugInto, syncCables } from '~/lib/logic/cables'
|
||||||
import { loadSoundSettings, saveSoundSettings, sfx, type Sound, type SoundSettings } from '~/lib/audio'
|
import { loadSoundSettings, saveSoundSettings, sfx, type Sound, type SoundSettings } from '~/lib/audio'
|
||||||
import { challengeName, challengeOpen, defUsable, isCampaign, KIND_NAMES, kindUnlocked, NEEDS, unlockedBy } from '~/lib/logic/progress'
|
import { challengeName, challengeOpen, defUsable, isCampaign, KIND_NAMES, kindUnlocked, NEEDS, unlockedBy } from '~/lib/logic/progress'
|
||||||
import { apiId, makePuzzle, scoreSolution } from '~/lib/logic/puzzles'
|
import { apiId, makePuzzle, scoreSolution } from '~/lib/logic/puzzles'
|
||||||
import { baseOf, MAX_GATE_INPUTS, NAND_BASE, wideGate } from '~/lib/logic/wide'
|
import { baseOf, MAX_GATE_INPUTS, NAND_BASE, wideGate } from '~/lib/logic/wide'
|
||||||
import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challenges'
|
import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challenges'
|
||||||
import { compile, rowSource } from '~/lib/logic/compile'
|
import { compile, rowSource } from '~/lib/logic/compile'
|
||||||
import { MAX_BITS, shapeOf, type Pt } from '~/lib/logic/geometry'
|
import { MAX_BITS, panelOffsets, shapeOf, type Pt } from '~/lib/logic/geometry'
|
||||||
import { adoptName, continueSeries, newPinLabel } from '~/lib/logic/labels'
|
import { adoptName, continueSeries, newPinLabel } from '~/lib/logic/labels'
|
||||||
import { attachPanel, detachPanel, dockedPanels, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels'
|
import { attachPanel, detachPanel, dockedPanels, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels'
|
||||||
import {
|
import {
|
||||||
|
|
@ -57,7 +57,7 @@ import {
|
||||||
type AccountUser,
|
type AccountUser,
|
||||||
type ProjectMeta,
|
type ProjectMeta,
|
||||||
} from '~/lib/logic/storage'
|
} from '~/lib/logic/storage'
|
||||||
import type { CircuitNode, NodeKind, PanelControl, PanelRow, PinRef, Project, PuzzleSpec, WireColor } from '~/lib/logic/types'
|
import type { CircuitNode, LampColor, NodeKind, PanelControl, PanelRow, PinRef, Project, PuzzleSpec, WireColor } from '~/lib/logic/types'
|
||||||
|
|
||||||
/** A challenge check plus how many transistors the solution uses. */
|
/** A challenge check plus how many transistors the solution uses. */
|
||||||
/** How everyone did on a puzzle, from the server. */
|
/** How everyone did on a puzzle, from the server. */
|
||||||
|
|
@ -206,6 +206,7 @@ function create() {
|
||||||
const canRedo = ref(false)
|
const canRedo = ref(false)
|
||||||
|
|
||||||
function resetHistory() {
|
function resetHistory() {
|
||||||
|
syncCables(project.value)
|
||||||
undoStack.length = 0
|
undoStack.length = 0
|
||||||
redoStack.length = 0
|
redoStack.length = 0
|
||||||
last = snap()
|
last = snap()
|
||||||
|
|
@ -213,12 +214,16 @@ function create() {
|
||||||
canRedo.value = false
|
canRedo.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Record a change. Structural changes (parts, wires) rebuild the simulation. */
|
/**
|
||||||
|
* Record a change. Structural changes (parts, wires) rebuild the simulation.
|
||||||
|
* Cables follow every change: a new name or a moved part shows on them.
|
||||||
|
*/
|
||||||
function commit(structural = true) {
|
function commit(structural = true) {
|
||||||
if (structural) {
|
if (structural) {
|
||||||
syncPanels(project.value)
|
syncPanels(project.value)
|
||||||
pruneWires(project.value)
|
pruneWires(project.value)
|
||||||
}
|
}
|
||||||
|
syncCables(project.value)
|
||||||
const s = snap()
|
const s = snap()
|
||||||
if (s === last) return
|
if (s === last) return
|
||||||
undoStack.push(last)
|
undoStack.push(last)
|
||||||
|
|
@ -523,6 +528,16 @@ function create() {
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** What moves along when parts move: docked panels and plugged-in cable ends, and what is plugged into those. */
|
||||||
|
function withFollowers(ids: Iterable<string>): Set<string> {
|
||||||
|
const set = new Set(ids)
|
||||||
|
for (let size = -1; size !== set.size; ) {
|
||||||
|
size = set.size
|
||||||
|
for (const p of [...dockedPanels(def.value, set), ...pluggedInto(def.value, set)]) set.add(p.id)
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
function deleteSelected() {
|
function deleteSelected() {
|
||||||
const d = def.value
|
const d = def.value
|
||||||
if (!selection.value.length && !wireSel.value.length) return
|
if (!selection.value.length && !wireSel.value.length) return
|
||||||
|
|
@ -653,6 +668,88 @@ function create() {
|
||||||
commit()
|
commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Swaps a row of a free panel with its neighbour; the rows keep their places, so the switches trade them. */
|
||||||
|
function moveRow(panel: CircuitNode, index: number, by: -1 | 1) {
|
||||||
|
const rows = panel.rows ?? []
|
||||||
|
const j = index + by
|
||||||
|
if (panel.attach || j < 0 || j >= rows.length) return
|
||||||
|
const at = panelOffsets(rows)
|
||||||
|
;[rows[index], rows[j]] = [rows[j]!, rows[index]!]
|
||||||
|
rows.forEach((r, i) => (r.dy = at[i]!))
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Empty rows above a row of a free panel; the rows below move along. */
|
||||||
|
function setRowGap(panel: CircuitNode, index: number, gap: number) {
|
||||||
|
const rows = panel.rows ?? []
|
||||||
|
if (panel.attach || index < 1 || index >= rows.length) return
|
||||||
|
const at = panelOffsets(rows)
|
||||||
|
const shift = at[index - 1]! + 1 + Math.max(0, Math.min(20, Math.round(gap) || 0)) - at[index]!
|
||||||
|
rows.forEach((r, i) => (r.dy = i < index ? at[i]! : at[i]! + shift))
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A keyboard key for a panel row (empty to clear it). */
|
||||||
|
function setRowKey(row: PanelRow, key: string | undefined) {
|
||||||
|
if (key) row.key = key
|
||||||
|
else delete row.key
|
||||||
|
commit(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
function setRowColor(row: PanelRow, color: LampColor | undefined) {
|
||||||
|
if (color) row.color = color
|
||||||
|
else delete row.color
|
||||||
|
commit(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Cable ends ────────────────────────────────────────────────────
|
||||||
|
/** The part a cable end's single wires all run to (for a bundle: come from), if just one. */
|
||||||
|
function plugTarget(end: CircuitNode): CircuitNode | undefined {
|
||||||
|
const wires = end.kind === 'bundle' ? def.value.wires.filter(w => w.to.node === end.id) : def.value.wires.filter(w => w.from.node === end.id)
|
||||||
|
const ids = new Set(wires.map(w => (end.kind === 'bundle' ? w.from.node : w.to.node)))
|
||||||
|
const part = ids.size === 1 ? def.value.nodes.find(n => n.id === [...ids][0]) : undefined
|
||||||
|
return part && part.kind !== 'bundle' && part.kind !== 'split' ? part : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function plugEnd(end: CircuitNode) {
|
||||||
|
const part = plugTarget(end)
|
||||||
|
if (!part || !plugInto(def.value, end, part.id, getDef.value)) {
|
||||||
|
return notify('Its wires must all run to the pins of one part, in the same order as the cable', 'error')
|
||||||
|
}
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pulls a cable end out of its part: it steps two squares away and its wires show again. */
|
||||||
|
function unplugEnd(end: CircuitNode) {
|
||||||
|
if (!end.plug) return
|
||||||
|
delete end.plug
|
||||||
|
end.x += end.kind === 'bundle' ? 2 : -2
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ends dragged away from the part they are plugged into come loose. */
|
||||||
|
function unplugMoved(ids: Set<string>) {
|
||||||
|
for (const n of def.value.nodes) if (n.plug && ids.has(n.id) && !ids.has(n.plug)) delete n.plug
|
||||||
|
}
|
||||||
|
|
||||||
|
function setPitch(end: CircuitNode, pitch: number) {
|
||||||
|
if (end.plug) return
|
||||||
|
const p = Math.max(1, Math.min(8, Math.round(pitch) || 1))
|
||||||
|
if (p === 1) delete end.pitch
|
||||||
|
else end.pitch = p
|
||||||
|
delete end.offsets
|
||||||
|
commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
function addCablePanel(bundleId: string) {
|
||||||
|
const panel = cablePanel(def.value, bundleId, getDef.value)
|
||||||
|
if (!panel) return notify('Every wire of this cable is already driven', 'error')
|
||||||
|
selection.value = [panel.id]
|
||||||
|
wireSel.value = []
|
||||||
|
commit()
|
||||||
|
notify(`Input panel with ${panel.rows!.length} switches plugged into the cable`)
|
||||||
|
}
|
||||||
|
|
||||||
function addRow(panel: CircuitNode) {
|
function addRow(panel: CircuitNode) {
|
||||||
panel.rows = [...(panel.rows ?? []), ...newPanelRows(1)]
|
panel.rows = [...(panel.rows ?? []), ...newPanelRows(1)]
|
||||||
commit()
|
commit()
|
||||||
|
|
@ -1682,6 +1779,17 @@ function create() {
|
||||||
addRow,
|
addRow,
|
||||||
dropRow,
|
dropRow,
|
||||||
withDocked,
|
withDocked,
|
||||||
|
withFollowers,
|
||||||
|
unplugMoved,
|
||||||
|
moveRow,
|
||||||
|
setRowGap,
|
||||||
|
setRowKey,
|
||||||
|
setRowColor,
|
||||||
|
plugTarget,
|
||||||
|
plugEnd,
|
||||||
|
unplugEnd,
|
||||||
|
setPitch,
|
||||||
|
addCablePanel,
|
||||||
linking,
|
linking,
|
||||||
startLinking,
|
startLinking,
|
||||||
linkTo,
|
linkTo,
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,18 @@
|
||||||
// Cables: several wires bundled into one. A bundle part gathers the wires,
|
// Cables: several wires bundled into one. A bundle part gathers the wires,
|
||||||
// one cable runs to a split part (or several), which fans them out again.
|
// one cable runs to a split part (or several), which fans them out again.
|
||||||
// Wire i of the cable is input i of the bundle and output i of the split.
|
// Wire i of the cable is input i of the bundle and output i of the split.
|
||||||
|
//
|
||||||
|
// A cable end can plug straight into a part: its wires sit on the part's
|
||||||
|
// pins (no wire to draw), and it moves with the part. Each wire of a cable
|
||||||
|
// carries a name, from what drives it or else from what it drives, which a
|
||||||
|
// free cable end shows next to its pins.
|
||||||
|
|
||||||
import { cableBits, MAX_BITS, pinPos, shapeOf, type DefLookup, type Pt } from './geometry'
|
import { cableBits, cableOffsets, MAX_BITS, pinPos, shapeOf, type DefLookup, type Pt } from './geometry'
|
||||||
import { uid } from './ops'
|
import { pinName } from './labels'
|
||||||
import type { CircuitNode, ComponentDef, Wire } from './types'
|
import { lookup, uid } from './ops'
|
||||||
|
import type { CircuitNode, ComponentDef, Project, Wire } from './types'
|
||||||
|
|
||||||
|
const CABLE_ENDS = new Set(['bundle', 'split'])
|
||||||
|
|
||||||
const isCable = (def: ComponentDef, w: Wire) => def.nodes.find(n => n.id === w.from.node)?.kind === 'bundle'
|
const isCable = (def: ComponentDef, w: Wire) => def.nodes.find(n => n.id === w.from.node)?.kind === 'bundle'
|
||||||
|
|
||||||
|
|
@ -81,9 +89,215 @@ export function bundleWires(
|
||||||
})
|
})
|
||||||
// The sleeve starts neutral: the wires inside show their own colours.
|
// The sleeve starts neutral: the wires inside show their own colours.
|
||||||
def.wires.push({ id: uid('w'), from: { node: bundle.id, pin: 'q' }, to: { node: split.id, pin: 'a' } })
|
def.wires.push({ id: uid('w'), from: { node: bundle.id, pin: 'q' }, to: { node: split.id, pin: 'a' } })
|
||||||
|
if (!at) {
|
||||||
|
// All the wires leave one part (or reach one part) in cable order: that end plugs straight in,
|
||||||
|
// unless another part sits where it would go.
|
||||||
|
for (const [end, side] of [
|
||||||
|
[bundle, new Set(ends.map(e => e.w.from.node))],
|
||||||
|
[split, new Set(ends.map(e => e.w.to.node))],
|
||||||
|
] as const) {
|
||||||
|
const part = side.size === 1 ? nodes.get([...side][0]!) : undefined
|
||||||
|
const lay = part && !CABLE_ENDS.has(part.kind) ? plugLayout(def, end, part, get) : null
|
||||||
|
if (!part || !lay) continue
|
||||||
|
const box = { x: lay.x, y: lay.y - 0.5, w: 1, h: lay.offsets[n - 1]! + 1 }
|
||||||
|
const clash = def.nodes.some(nd => {
|
||||||
|
if (nd === part || nd === bundle || nd === split || nd.kind === 'note') return false
|
||||||
|
const b = shapeOf(nd, get).body
|
||||||
|
return box.x < nd.x + b.x + b.w && box.x + box.w > nd.x + b.x && box.y < nd.y + b.y + b.h && box.y + box.h > nd.y + b.y
|
||||||
|
})
|
||||||
|
if (!clash) plugInto(def, end, part.id, get)
|
||||||
|
}
|
||||||
|
}
|
||||||
return { ok: true, bundle, split }
|
return { ok: true, bundle, split }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The wires on the single-wire side of a cable end: into a bundle, out of a split. */
|
||||||
|
function singleWires(def: ComponentDef, end: CircuitNode): Wire[] {
|
||||||
|
return end.kind === 'bundle' ? def.wires.filter(w => w.to.node === end.id) : def.wires.filter(w => w.from.node === end.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
const farEnd = (end: CircuitNode, w: Wire) => (end.kind === 'bundle' ? w.from : w.to)
|
||||||
|
const bitOf = (end: CircuitNode, w: Wire) => Number((end.kind === 'bundle' ? w.to.pin : w.from.pin).slice(1))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where a cable end sits when plugged into `part`: each of its wires on the
|
||||||
|
* part pin it is wired to, wires not wired to the part one row on from their
|
||||||
|
* neighbour. Null when those pins are not on one edge in the cable's order.
|
||||||
|
*/
|
||||||
|
export function plugLayout(def: ComponentDef, end: CircuitNode, part: CircuitNode, get: DefLookup): { x: number; y: number; offsets: number[] } | null {
|
||||||
|
const n = cableBits(end)
|
||||||
|
const shape = shapeOf(part, get)
|
||||||
|
const pins = end.kind === 'bundle' ? shape.outputs : shape.inputs
|
||||||
|
const ys: (number | undefined)[] = Array.from({ length: n }, () => undefined)
|
||||||
|
let x: number | undefined
|
||||||
|
for (const w of singleWires(def, end)) {
|
||||||
|
const other = farEnd(end, w)
|
||||||
|
const i = bitOf(end, w)
|
||||||
|
const p = other.node === part.id ? pins.find(k => k.key === other.pin) : undefined
|
||||||
|
if (!p || p.bus || !(i >= 0 && i < n) || ys[i] !== undefined) continue
|
||||||
|
if (x !== undefined && part.x + p.dx !== x) return null
|
||||||
|
x = part.x + p.dx
|
||||||
|
ys[i] = part.y + p.dy
|
||||||
|
}
|
||||||
|
const first = ys.findIndex(y => y !== undefined)
|
||||||
|
if (x === undefined || first < 0) return null
|
||||||
|
for (let i = first - 1; i >= 0; i--) ys[i] = ys[i + 1]! - 1
|
||||||
|
for (let i = first + 1; i < n; i++) ys[i] ??= ys[i - 1]! + 1
|
||||||
|
for (let i = 1; i < n; i++) if (!(ys[i]! > ys[i - 1]!)) return null
|
||||||
|
const top = ys[0]!
|
||||||
|
return { x: end.kind === 'split' ? x - 1 : x, y: top, offsets: ys.map(y => y! - top) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Plugs a cable end into a part. False, and the end left unplugged, when the pins don't line up. */
|
||||||
|
export function plugInto(def: ComponentDef, end: CircuitNode, partId: string, get: DefLookup): boolean {
|
||||||
|
const part = def.nodes.find(n => n.id === partId)
|
||||||
|
const lay = part && part.id !== end.id && CABLE_ENDS.has(end.kind) && !CABLE_ENDS.has(part.kind) ? plugLayout(def, end, part, get) : null
|
||||||
|
if (!part || !lay) {
|
||||||
|
delete end.plug
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
end.plug = part.id
|
||||||
|
end.x = lay.x
|
||||||
|
end.y = lay.y
|
||||||
|
const k = lay.offsets[1]!
|
||||||
|
if (k <= 8 && lay.offsets.every((v, i) => v === i * k)) {
|
||||||
|
if (k === 1) delete end.pitch
|
||||||
|
else end.pitch = k
|
||||||
|
delete end.offsets
|
||||||
|
} else end.offsets = lay.offsets
|
||||||
|
for (const w of singleWires(def, end)) if (farEnd(end, w).node === part.id) delete w.pts
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The part a free cable end touches with every wire of its single-wire side, so it counts as plugged in. */
|
||||||
|
function touching(def: ComponentDef, end: CircuitNode, nodes: Map<string, CircuitNode>, get: DefLookup): string | null {
|
||||||
|
const wires = singleWires(def, end)
|
||||||
|
const ids = new Set(wires.map(w => farEnd(end, w).node))
|
||||||
|
const part = ids.size === 1 ? nodes.get([...ids][0]!) : undefined
|
||||||
|
if (!part || CABLE_ENDS.has(part.kind)) return null
|
||||||
|
const mine = shapeOf(end, get)
|
||||||
|
const theirs = shapeOf(part, get)
|
||||||
|
const inward = end.kind === 'bundle'
|
||||||
|
for (const w of wires) {
|
||||||
|
const a = pinPos(end, mine, inward ? w.to.pin : w.from.pin, inward ? 'in' : 'out')
|
||||||
|
const b = pinPos(part, theirs, inward ? w.from.pin : w.to.pin, inward ? 'out' : 'in')
|
||||||
|
if (!a || !b || a.x !== b.x || a.y !== b.y) return null
|
||||||
|
}
|
||||||
|
return part.id
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What a wire of a cable is called at its source: the name of the switch, row or component pin driving it. */
|
||||||
|
function sourceName(node: CircuitNode, pin: string, get: DefLookup): string {
|
||||||
|
if (node.kind === 'panel') return node.rows?.find(r => r.id === pin)?.label?.trim() ?? ''
|
||||||
|
if (node.kind === 'input' || node.kind === 'button' || node.kind === 'clock') return node.label?.trim() ?? ''
|
||||||
|
return pinName(node, pin, get) ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What a wire of a cable is called at its target: the lamp, output or component pin it drives. */
|
||||||
|
function targetName(node: CircuitNode, pin: string, get: DefLookup): string {
|
||||||
|
if (node.kind === 'output' || node.kind === 'lamp' || node.kind === 'hex') return node.label?.trim() ?? ''
|
||||||
|
return pinName(node, pin, get) ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
|
function setNames(node: CircuitNode, names: string[]) {
|
||||||
|
if (!names.some(Boolean)) delete node.names
|
||||||
|
else if (node.names?.join('\n') !== names.join('\n') || node.names.length !== names.length) node.names = names
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Names every cable's wires, on the bundle and its splits. */
|
||||||
|
function syncNames(def: ComponentDef, nodes: Map<string, CircuitNode>, get: DefLookup) {
|
||||||
|
const named = new Set<string>()
|
||||||
|
const bundles = def.nodes.filter(n => n.kind === 'bundle')
|
||||||
|
// Twice, so a cable fed by another cable's split picks up its names.
|
||||||
|
for (let round = 0; round < 2; round++) {
|
||||||
|
for (const b of bundles) {
|
||||||
|
const splits = new Set(def.wires.filter(w => w.from.node === b.id && nodes.get(w.to.node)?.kind === 'split').map(w => w.to.node))
|
||||||
|
const names = Array.from({ length: cableBits(b) }, (_, i) => {
|
||||||
|
const pin = `b${i}`
|
||||||
|
const feed = def.wires.find(w => w.to.node === b.id && w.to.pin === pin)
|
||||||
|
const src = feed && nodes.get(feed.from.node)
|
||||||
|
const own = src ? sourceName(src, feed.from.pin, get) : ''
|
||||||
|
if (own) return own
|
||||||
|
for (const w of def.wires) {
|
||||||
|
if (w.from.pin !== pin || !splits.has(w.from.node)) continue
|
||||||
|
const t = nodes.get(w.to.node)
|
||||||
|
const name = t ? targetName(t, w.to.pin, get) : ''
|
||||||
|
if (name) return name
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
setNames(b, names)
|
||||||
|
named.add(b.id)
|
||||||
|
for (const id of splits) {
|
||||||
|
setNames(nodes.get(id)!, names)
|
||||||
|
named.add(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const n of def.nodes) if (n.kind === 'split' && !named.has(n.id)) delete n.names
|
||||||
|
// Rows of a free panel that feed a cable show the wire's name until they get their own.
|
||||||
|
for (const w of def.wires) {
|
||||||
|
const b = nodes.get(w.to.node)
|
||||||
|
const p = nodes.get(w.from.node)
|
||||||
|
if (b?.kind !== 'bundle' || p?.kind !== 'panel' || p.attach) continue
|
||||||
|
const row = p.rows?.find(r => r.id === w.from.pin)
|
||||||
|
const name = b.names?.[Number(w.to.pin.slice(1))]
|
||||||
|
if (row && !row.label?.trim() && name) row.name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keeps cables in step after a change: names follow what the wires connect,
|
||||||
|
* plugged ends stay on their part's pins (and come loose when they no longer
|
||||||
|
* line up), and a free end whose wires all touch one part's pins plugs in.
|
||||||
|
*/
|
||||||
|
export function syncCables(project: Project) {
|
||||||
|
const get = lookup(project)
|
||||||
|
for (const def of project.components) {
|
||||||
|
if (!def.nodes.some(n => CABLE_ENDS.has(n.kind))) continue
|
||||||
|
const nodes = new Map(def.nodes.map(n => [n.id, n]))
|
||||||
|
syncNames(def, nodes, get)
|
||||||
|
for (const end of def.nodes) {
|
||||||
|
if (!CABLE_ENDS.has(end.kind)) continue
|
||||||
|
const id = end.plug ?? touching(def, end, nodes, get)
|
||||||
|
if (id) plugInto(def, end, id, get)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A new input panel for a bundle: a switch for each wire that has nothing
|
||||||
|
* driving it yet, lined up with the bundle's pins. When it drives every
|
||||||
|
* wire, the bundle plugs into it. Null when every wire is driven already.
|
||||||
|
*/
|
||||||
|
export function cablePanel(def: ComponentDef, bundleId: string, get: DefLookup): CircuitNode | null {
|
||||||
|
const bundle = def.nodes.find(n => n.id === bundleId && n.kind === 'bundle')
|
||||||
|
if (!bundle) return null
|
||||||
|
const fed = new Set(def.wires.filter(w => w.to.node === bundle.id).map(w => w.to.pin))
|
||||||
|
const free = cableOffsets(bundle)
|
||||||
|
.map((dy, i) => ({ pin: `b${i}`, dy, name: bundle.names?.[i] }))
|
||||||
|
.filter(p => !fed.has(p.pin))
|
||||||
|
if (!free.length) return null
|
||||||
|
const top = free[0]!.dy
|
||||||
|
const panel: CircuitNode = {
|
||||||
|
id: uid('n'),
|
||||||
|
kind: 'panel',
|
||||||
|
x: 0,
|
||||||
|
y: bundle.y + top,
|
||||||
|
rows: free.map(p => ({ id: uid('r'), type: 'switch' as const, dy: p.dy - top, ...(p.name ? { name: p.name } : {}) })),
|
||||||
|
}
|
||||||
|
panel.x = bundle.x - shapeOf(panel, get).body.w
|
||||||
|
def.nodes.push(panel)
|
||||||
|
free.forEach((p, i) => def.wires.push({ id: uid('w'), from: { node: panel.id, pin: panel.rows![i]!.id }, to: { node: bundle.id, pin: p.pin } }))
|
||||||
|
if (!fed.size) plugInto(def, bundle, panel.id, get)
|
||||||
|
return panel
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The nearest free place for a cable end of `n` wires (top pin at `at`):
|
* The nearest free place for a cable end of `n` wires (top pin at `at`):
|
||||||
* as given, else a little further along (`dir` +1 away from the sources,
|
* as given, else a little further along (`dir` +1 away from the sources,
|
||||||
|
|
|
||||||
|
|
@ -229,16 +229,45 @@ export function cableBits(node: CircuitNode): number {
|
||||||
return Math.min(MAX_BITS, Math.max(2, Math.round(n)))
|
return Math.min(MAX_BITS, Math.max(2, Math.round(n)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Bundle: one input per wire on the left, the cable leaves on the right. Split: the mirror image. */
|
/** Row of each wire of a cable end below the first: evenly `pitch` apart, or as plugged. */
|
||||||
function cableShape(node: CircuitNode): Shape {
|
export function cableOffsets(node: CircuitNode): number[] {
|
||||||
const n = cableBits(node)
|
const n = cableBits(node)
|
||||||
const wires = Array.from({ length: n }, (_, i) => ({ key: `b${i}`, label: String(i), dy: i }))
|
const o = node.offsets
|
||||||
const mid = Math.floor((n - 1) / 2)
|
if (o?.length === n && o[0] === 0 && o.every((v, i) => i === 0 || v > o[i - 1]!)) return o
|
||||||
const body = { x: 0, y: -0.5, w: 1, h: n }
|
const pitch = Math.max(1, Math.min(8, Math.round(node.pitch ?? 1)))
|
||||||
if (node.kind === 'bundle') {
|
return Array.from({ length: n }, (_, i) => i * pitch)
|
||||||
return { body, inputs: wires.map(p => ({ ...p, dx: 0 })), outputs: [{ key: 'q', label: '', dx: 1, dy: mid, bus: true }] }
|
}
|
||||||
|
|
||||||
|
/** 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.
|
||||||
|
*/
|
||||||
|
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 last = dys[dys.length - 1]!
|
||||||
|
const mid = Math.floor(last / 2)
|
||||||
|
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: [{ key: 'a', label: '', dx: 0, dy: mid, bus: true }], outputs: wires.map(p => ({ ...p, dx: 1 })) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const PIXEL_SHAPE: Shape = {
|
const PIXEL_SHAPE: Shape = {
|
||||||
|
|
@ -269,7 +298,8 @@ function panelShape(node: CircuitNode): Shape {
|
||||||
const rows = node.rows ?? []
|
const rows = node.rows ?? []
|
||||||
const dys = panelOffsets(rows)
|
const dys = panelOffsets(rows)
|
||||||
const labels = rows.map(rowLabel)
|
const labels = rows.map(rowLabel)
|
||||||
const w = Math.max(4, Math.ceil(labels.reduce((m, l) => Math.max(m, l.length), 0) * LABEL_CHAR + PANEL_CONTROL_W + 1.1))
|
const keys = rows.some(r => r.key) ? 0.7 : 0
|
||||||
|
const w = Math.max(4, Math.ceil(labels.reduce((m, l) => Math.max(m, l.length), 0) * LABEL_CHAR + PANEL_CONTROL_W + 1.1 + keys))
|
||||||
const last = dys.length ? dys[dys.length - 1]! : 0
|
const last = dys.length ? dys[dys.length - 1]! : 0
|
||||||
return {
|
return {
|
||||||
body: { x: 0, y: -0.5, w, h: last + 1 },
|
body: { x: 0, y: -0.5, w, h: last + 1 },
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,8 @@ export function pinName(node: CircuitNode, pin: string, get: DefLookup): string
|
||||||
const row = node.rows?.find(r => r.id === pin)
|
const row = node.rows?.find(r => r.id === pin)
|
||||||
return row?.label?.trim() || row?.name?.trim() || undefined
|
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
|
if (node.kind !== 'chip' || !node.chip) return undefined
|
||||||
const def = get(node.chip)
|
const def = get(node.chip)
|
||||||
return def?.nodes.find(n => n.id === pin)?.label?.trim() || undefined
|
return def?.nodes.find(n => n.id === pin)?.label?.trim() || undefined
|
||||||
|
|
|
||||||
|
|
@ -236,9 +236,17 @@ export function pasteClip(def: ComponentDef, clip: Clip, dx: number, dy: number)
|
||||||
return [...ids.values()]
|
return [...ids.values()]
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copied panels dock to the copy of their component, or to nothing when it was not copied along. */
|
/**
|
||||||
|
* Copied panels dock to the copy of their component, and copied cable ends
|
||||||
|
* plug into the copy of their part, or to nothing when it was not copied along.
|
||||||
|
*/
|
||||||
function redock(nodes: CircuitNode[], ids: Map<string, string>) {
|
function redock(nodes: CircuitNode[], ids: Map<string, string>) {
|
||||||
for (const n of nodes) {
|
for (const n of nodes) {
|
||||||
|
if (n.plug) {
|
||||||
|
const to = ids.get(n.plug)
|
||||||
|
if (to) n.plug = to
|
||||||
|
else delete n.plug
|
||||||
|
}
|
||||||
if (!n.attach) continue
|
if (!n.attach) continue
|
||||||
const to = ids.get(n.attach)
|
const to = ids.get(n.attach)
|
||||||
if (to) n.attach = to
|
if (to) n.attach = to
|
||||||
|
|
|
||||||
|
|
@ -495,9 +495,11 @@ export function routeAll(inputs: RouteInput[], bodies: Box[], pins: Pt[], opts:
|
||||||
const pinSet = new Set(pins.map(([x, y]) => pinKey(x, y)))
|
const pinSet = new Set(pins.map(([x, y]) => pinKey(x, y)))
|
||||||
const bodyIndex = new BodyIndex(bodies)
|
const bodyIndex = new BodyIndex(bodies)
|
||||||
const grid = new Map<string, Pt[]>()
|
const grid = new Map<string, Pt[]>()
|
||||||
// Hand-routed wires first: they are fixed, the others route around them.
|
// Plugged in: the two pins touch, there is nothing to route.
|
||||||
|
for (const w of inputs) if (w.a[0] === w.b[0] && w.a[1] === w.b[1]) grid.set(w.id, [w.a, w.b])
|
||||||
|
// Hand-routed wires next: they are fixed, the others route around them.
|
||||||
for (const w of inputs) {
|
for (const w of inputs) {
|
||||||
if (!w.pts?.length) continue
|
if (!w.pts?.length || grid.has(w.id)) continue
|
||||||
const r = routeWire(w.a, w.b, w.pts)
|
const r = routeWire(w.a, w.b, w.pts)
|
||||||
grid.set(w.id, r)
|
grid.set(w.id, r)
|
||||||
occ.add(r, w.net)
|
occ.add(r, w.net)
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,14 @@ export interface CircuitNode {
|
||||||
attach?: string
|
attach?: string
|
||||||
/** Chip pins an attached panel leaves alone (wired elsewhere, or a row the user removed). */
|
/** Chip pins an attached panel leaves alone (wired elsewhere, or a row the user removed). */
|
||||||
skip?: string[]
|
skip?: string[]
|
||||||
|
/** Cable end plugged straight into a part: the id of that part. It sits on the part's pins and moves with it. */
|
||||||
|
plug?: string
|
||||||
|
/** Grid rows from one wire of a cable end to the next. Empty: 1. */
|
||||||
|
pitch?: number
|
||||||
|
/** Cable end plugged into pins that are not evenly spaced: each wire's row below the first. */
|
||||||
|
offsets?: number[]
|
||||||
|
/** Names of the wires in a cable, top to bottom, from what drives them or else what they drive. */
|
||||||
|
names?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PANEL_CONTROLS = ['switch', 'button', 'clock'] as const
|
export const PANEL_CONTROLS = ['switch', 'button', 'clock'] as const
|
||||||
|
|
@ -70,6 +78,10 @@ export interface PanelRow {
|
||||||
hz?: number
|
hz?: number
|
||||||
/** Offset below the first row in grid units; default one below the row before. */
|
/** Offset below the first row in grid units; default one below the row before. */
|
||||||
dy?: number
|
dy?: number
|
||||||
|
/** Keyboard key that flips the switch or holds the button down. */
|
||||||
|
key?: string
|
||||||
|
/** Colour of the switch or button while it is on. */
|
||||||
|
color?: LampColor
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LAMP_COLORS = ['yellow', 'red', 'green', 'blue', 'white'] as const
|
export const LAMP_COLORS = ['yellow', 'red', 'green', 'blue', 'white'] as const
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,230 @@
|
||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { bundleWires, cablePanel, 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'
|
||||||
|
import { circuitRoutes } from '../app/lib/logic/routing'
|
||||||
|
import { Simulator } from '../app/lib/logic/sim'
|
||||||
|
import type { CircuitNode, ComponentDef, Project } from '../app/lib/logic/types'
|
||||||
|
|
||||||
|
/** Switches A0..A3 and B0..B3 wired to an Adder 4, its outputs to lamps. */
|
||||||
|
function adder(): { p: Project; def: ComponentDef; ins: string[]; outs: string[] } {
|
||||||
|
const p = createProject({ examples: true })
|
||||||
|
const pins = chipPins(lookup(p)('ex-add4')!)
|
||||||
|
const ins = pins.inputs.map(x => x.key)
|
||||||
|
const outs = pins.outputs.map(x => x.key)
|
||||||
|
const def: ComponentDef = {
|
||||||
|
id: 'root',
|
||||||
|
name: 'Root',
|
||||||
|
symbol: 'box',
|
||||||
|
nodes: [
|
||||||
|
{ id: 'add', kind: 'chip', chip: 'ex-add4', x: 20, y: 0 },
|
||||||
|
...pins.inputs.map((x, i) => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 3, label: x.label })),
|
||||||
|
...pins.outputs.map((_, i) => ({ id: `l${i}`, kind: 'lamp' as const, x: 50, y: 20 + i * 2 })),
|
||||||
|
],
|
||||||
|
wires: [
|
||||||
|
...ins.map((k, i) => ({ id: `wi${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'add', pin: k } })),
|
||||||
|
...outs.map((k, i) => ({ id: `wo${i}`, from: { node: 'add', pin: k }, to: { node: `l${i}`, pin: 'a' } })),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
p.components.push(def)
|
||||||
|
return { p, def, ins, outs }
|
||||||
|
}
|
||||||
|
|
||||||
|
function sums(p: Project, def: ComponentDef): string[] {
|
||||||
|
const out: string[] = []
|
||||||
|
for (const [a, b] of [[3, 5], [15, 1], [9, 9], [0, 0]] as const) {
|
||||||
|
const s = new Simulator(compile(lookup(p), def.id))
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
s.setSource(`s${i}`, ((a >> i) & 1) === 1)
|
||||||
|
s.setSource(`s${i + 4}`, ((b >> i) & 1) === 1)
|
||||||
|
}
|
||||||
|
s.run()
|
||||||
|
out.push([0, 1, 2, 3, 4].map(i => s.pin(`l${i}`, 'a')).join(''))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
const node = (def: ComponentDef, id: string) => def.nodes.find(n => n.id === id)!
|
||||||
|
|
||||||
|
/** Where each wire on the single-wire side of a cable end meets the part it is plugged into. */
|
||||||
|
function touches(p: Project, def: ComponentDef, end: CircuitNode): boolean {
|
||||||
|
const get = lookup(p)
|
||||||
|
const wires = end.kind === 'bundle' ? def.wires.filter(w => w.to.node === end.id) : def.wires.filter(w => w.from.node === end.id)
|
||||||
|
return wires.every(w => {
|
||||||
|
const a = pinPos(node(def, w.from.node), shapeOf(node(def, w.from.node), get), w.from.pin, 'out')!
|
||||||
|
const b = pinPos(node(def, w.to.node), shapeOf(node(def, w.to.node), get), w.to.pin, 'in')!
|
||||||
|
return a.x === b.x && a.y === b.y
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('plugged cable ends', () => {
|
||||||
|
it('bundling wires into one part plugs the split straight onto its pins', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const before = sums(p, def)
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
expect(res.split.plug).toBe('add')
|
||||||
|
expect(res.bundle.plug).toBeUndefined()
|
||||||
|
expect(touches(p, def, res.split)).toBe(true)
|
||||||
|
expect(res.split.x).toBe(19)
|
||||||
|
expect(sums(p, def)).toEqual(before)
|
||||||
|
// The plugged wires have no length, so there is nothing to draw.
|
||||||
|
const routes = circuitRoutes(def, new Map(def.nodes.map(n => [n.id, shapeOf(n, lookup(p))])))
|
||||||
|
const plugged = new Set(def.wires.filter(w => w.from.node === res.split.id).map(w => w.id))
|
||||||
|
for (const r of routes.filter(r => plugged.has(r.id))) expect(r.grid).toHaveLength(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a bundle plugs onto the outputs, and the free split names its wires after them', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
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.
|
||||||
|
const s = shapeOf(res.split, lookup(p))
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('names come from what drives the wires, else from what they drive', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
for (const i of [0, 1, 2, 3]) delete node(def, `s${i}`).label
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.bundle.names).toEqual(['A0', 'A1', 'A2', 'A3'])
|
||||||
|
node(def, 's1').label = 'X'
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.bundle.names).toEqual(['A0', 'X', 'A2', 'A3'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('follows its part, and comes loose when the pins no longer line up', () => {
|
||||||
|
const { p, def, ins } = adder()
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
const chip = node(def, 'add')
|
||||||
|
chip.x += 7
|
||||||
|
chip.y += 3
|
||||||
|
syncCables(p)
|
||||||
|
expect(touches(p, def, res.split)).toBe(true)
|
||||||
|
// Rewire one wire to a pin further down: no longer in the cable's order on one edge.
|
||||||
|
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b0')!.to.pin = ins[8]!
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.split.plug).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a free end dropped with its pins on a part plugs in', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
const { x, y } = res.bundle
|
||||||
|
delete res.bundle.plug
|
||||||
|
res.bundle.x += 3
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.bundle.plug).toBeUndefined()
|
||||||
|
res.bundle.x = x
|
||||||
|
res.bundle.y = y
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.bundle.plug).toBe('add')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('takes the spacing of pins further apart', () => {
|
||||||
|
const p = createProject({ examples: true })
|
||||||
|
const get = lookup(p)
|
||||||
|
const def: ComponentDef = {
|
||||||
|
id: 'root',
|
||||||
|
name: 'Root',
|
||||||
|
symbol: 'box',
|
||||||
|
nodes: [
|
||||||
|
{ id: 'g', kind: 'nand', x: 10, y: 0 },
|
||||||
|
{ id: 'b', kind: 'split', bits: 2, x: 0, y: 0 },
|
||||||
|
],
|
||||||
|
wires: [
|
||||||
|
{ id: 'w0', from: { node: 'b', pin: 'b0' }, to: { node: 'g', pin: 'a' } },
|
||||||
|
{ id: 'w1', from: { node: 'b', pin: 'b1' }, to: { node: 'g', pin: 'b' } },
|
||||||
|
],
|
||||||
|
}
|
||||||
|
p.components.push(def)
|
||||||
|
const split = node(def, 'b')
|
||||||
|
expect(plugInto(def, split, 'g', get)).toBe(true)
|
||||||
|
const gate = shapeOf(node(def, 'g'), get).inputs
|
||||||
|
expect(split.pitch).toBe(gate[1]!.dy - gate[0]!.dy)
|
||||||
|
expect(touches(p, def, split)).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('input panels for cables', () => {
|
||||||
|
it('a bundle gets a panel with a switch per wire, plugged in and named', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
// Take the switches away: the bundle has nothing driving it.
|
||||||
|
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
|
||||||
|
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
|
||||||
|
syncCables(p)
|
||||||
|
const panel = cablePanel(def, res.bundle.id, lookup(p))!
|
||||||
|
syncCables(p)
|
||||||
|
expect(panel.rows!.map(r => r.name)).toEqual(['A0', 'A1', 'A2', 'A3'])
|
||||||
|
expect(res.bundle.plug).toBe(panel.id)
|
||||||
|
expect(touches(p, def, res.bundle)).toBe(true)
|
||||||
|
// Flipping row 2 sets A2.
|
||||||
|
const s = new Simulator(compile(lookup(p), def.id))
|
||||||
|
s.setSource(rowSource(panel.id, panel.rows![2]!.id), true)
|
||||||
|
s.run()
|
||||||
|
expect([0, 1, 2, 3].map(i => s.pin(`l${i}`, 'a')).join('')).toBe('0010')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a bundle plugged into a panel takes the spacing of its rows', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
|
||||||
|
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
|
||||||
|
const panel = cablePanel(def, res.bundle.id, lookup(p))!
|
||||||
|
// A gap of two rows above the third row.
|
||||||
|
const at = panelOffsets(panel.rows!)
|
||||||
|
panel.rows!.forEach((r, i) => (r.dy = at[i]! + (i >= 2 ? 2 : 0)))
|
||||||
|
syncCables(p)
|
||||||
|
expect(res.bundle.offsets).toEqual([0, 1, 4, 5])
|
||||||
|
expect(touches(p, def, res.bundle)).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('copying plugged ends', () => {
|
||||||
|
it('a copied end plugs into the copy of its part, or into nothing', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
||||||
|
if (!res.ok) throw new Error(res.error)
|
||||||
|
const both = pasteClip(def, copySelection(def, new Set(['add', res.split.id])), 0, 30)
|
||||||
|
const copy = both.map(id => node(def, id)).find(n => n.kind === 'split')!
|
||||||
|
const chip = both.map(id => node(def, id)).find(n => n.kind === 'chip')!
|
||||||
|
expect(copy.plug).toBe(chip.id)
|
||||||
|
const alone = pasteClip(def, copySelection(def, new Set([res.split.id])), 0, 60)
|
||||||
|
expect(node(def, alone[0]!).plug).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('cables saved before plugs existed', () => {
|
||||||
|
it('stay where they are and compute the same; only names are added', () => {
|
||||||
|
const { p, def } = adder()
|
||||||
|
const before = sums(p, def)
|
||||||
|
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 }, { id: 'sp', kind: 'split', bits: 4, x: 16, y: 2 })
|
||||||
|
def.wires = def.wires.filter(w => !['wi0', 'wi1', 'wi2', 'wi3'].includes(w.id))
|
||||||
|
const ins = chipPins(lookup(p)('ex-add4')!).inputs
|
||||||
|
for (const i of [0, 1, 2, 3]) {
|
||||||
|
def.wires.push({ id: `f${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
|
||||||
|
def.wires.push({ id: `t${i}`, from: { node: 'sp', pin: `b${i}` }, to: { node: 'add', pin: ins[i]!.key } })
|
||||||
|
}
|
||||||
|
def.wires.push({ id: 'c', from: { node: 'b', pin: 'q' }, to: { node: 'sp', pin: 'a' } })
|
||||||
|
syncCables(p)
|
||||||
|
expect([node(def, 'b').x, node(def, 'b').y, node(def, 'sp').x, node(def, 'sp').y]).toEqual([8, 2, 16, 2])
|
||||||
|
expect(node(def, 'b').plug ?? node(def, 'sp').plug).toBeUndefined()
|
||||||
|
expect(node(def, 'sp').names).toEqual(['A0', 'A1', 'A2', 'A3'])
|
||||||
|
expect(sums(p, def)).toEqual(before)
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue