A output panels, and switches, buttons, clocks or lamps combined into one panel
- Output panel: a column of named lamps, each passing its signal on; inside a component its rows are output pins (or kept inside); "Attach output panel" docks one to a component's outputs - Select switches, buttons and clocks: Combine into an input panel (names, states, keys and wires stay; spacing kept); "All rows" makes them all switches, buttons or clocks - Select lamps or output pins: Combine into an output panel - Unnamed panel rows take the name of the pin they are wired to; output panels merge like input panels; circuits that place this one keep their wires when pins move into a panel Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7a00546e63
commit
09f945d2c5
|
|
@ -341,13 +341,25 @@ function pack() {
|
|||
<p class="hint mt-2">Or drag one wire from a selected part to a pin: the others follow onto the next pins.</p>
|
||||
</section>
|
||||
|
||||
<section v-if="only('panel') && nodes.length > 1">
|
||||
<h3>Input panels</h3>
|
||||
<section v-if="(only('panel') || only('outpanel')) && nodes.length > 1">
|
||||
<h3>Panels</h3>
|
||||
<button class="btn w-full" title="One panel with all their rows, top panel first; wires stay on their rows" @click="ed.mergeSelectedPanels()">
|
||||
<Icon name="lucide:combine" class="size-4" /> Merge into one panel
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<section v-if="(only('input', 'button', 'clock') || only('output', 'lamp')) && nodes.length > 1">
|
||||
<h3>Panel</h3>
|
||||
<button
|
||||
class="btn w-full"
|
||||
title="One panel with a row for each, top to bottom: names, settings and wires stay"
|
||||
@click="ed.combineSelected()"
|
||||
>
|
||||
<Icon :name="only('output', 'lamp') ? 'lucide:list-checks' : 'lucide:sliders-vertical'" class="size-4" />
|
||||
Combine into {{ only('output', 'lamp') ? 'an output' : 'an input' }} panel
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h3>Make a component</h3>
|
||||
<p class="hint">Wires that cross the selection border become its pins.</p>
|
||||
|
|
|
|||
|
|
@ -871,7 +871,7 @@ function onPointerUp(e: PointerEvent) {
|
|||
|
||||
function onDoubleClick(node: CircuitNode) {
|
||||
if (node.kind === 'chip' && node.chip) ed.open(node.chip)
|
||||
else if (['input', 'output', 'lamp', 'button', 'note', 'hex', 'panel', 'tag', 'ref'].includes(node.kind)) {
|
||||
else if (['input', 'output', 'lamp', 'button', 'note', 'hex', 'panel', 'outpanel', 'tag', 'ref'].includes(node.kind)) {
|
||||
ed.selection.value = [node.id]
|
||||
ed.labelFocus.value++
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ const SHORTCUTS: [string, string][] = [
|
|||
['Send signal / Use signal', 'Name a signal (CLK, RESET) once and use it anywhere in the circuit without a wire'],
|
||||
['Ctrl+F', 'Find a part, signal or component in every circuit, and where a component is used'],
|
||||
['Ctrl+P', 'Print the circuit as a sheet with a title block, or save it as PDF'],
|
||||
['Select input panels, Merge', 'One panel with all their rows; inside a component its rows are input pins'],
|
||||
['Select switches or buttons, Combine', 'One input panel (all rows can become buttons); lamps or output pins combine into an output panel'],
|
||||
['Select panels, Merge', 'One panel with all their rows; inside a component input panel rows are inputs, output panel rows outputs'],
|
||||
['Design the box (Circuit tab)', 'Move lamps, displays and the name on a component\'s box, draw lines, boxes, circles and text'],
|
||||
['Select several lamps, pins or switches', 'Green boxes show free pins in a row they fit; point along one, click to wire them all'],
|
||||
['Shift + click on canvas', 'Keep placing the same part'],
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ const HINTS: Record<string, string> = {
|
|||
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.',
|
||||
tag: 'Wire a signal into it and give it a name. Every "Use signal" with that name in this circuit carries the same signal, without a wire. Names stay inside this circuit.',
|
||||
ref: 'Carries the signal of the "Send signal" with the same name in this circuit. Select it to see all places that signal goes.',
|
||||
outpanel: 'A lamp per row, named after what it shows. Inside a component its rows are output pins, top to bottom with the other outputs. Each row passes its signal on to the right.',
|
||||
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.',
|
||||
}
|
||||
|
||||
|
|
@ -171,6 +172,8 @@ const sleeveColor = computed(() => (cableBundle.value ? ed.def.value.wires.find(
|
|||
const chipInputs = computed(() => (chipDef.value ? chipPins(chipDef.value).inputs.length : 0))
|
||||
const chipOutputs = computed(() => (chipDef.value ? chipPins(chipDef.value).outputs.length : 0))
|
||||
const hasPanel = computed(() => ed.def.value.nodes.some(x => x.kind === 'panel' && x.attach === n.value.id))
|
||||
const hasOutPanel = computed(() => ed.def.value.nodes.some(x => x.kind === 'outpanel' && x.attach === n.value.id))
|
||||
const outPanel = computed(() => n.value.kind === 'outpanel')
|
||||
|
||||
function setRowLabel(row: PanelRow, e: Event) {
|
||||
const v = (e.target as HTMLInputElement).value.trim()
|
||||
|
|
@ -335,11 +338,11 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Input panel rows -->
|
||||
<template v-if="n.kind === 'panel'">
|
||||
<!-- Panel rows: switches, buttons and clocks, or lamps -->
|
||||
<template v-if="n.kind === 'panel' || n.kind === 'outpanel'">
|
||||
<p v-if="dock" class="result pass">
|
||||
<Icon name="lucide:link" class="size-4 shrink-0" />
|
||||
<span class="grow">Attached to {{ dock }}. It moves with it and gets a row for each new input.</span>
|
||||
<span class="grow">Attached to {{ dock }}. It moves with it and gets a row for each new {{ outPanel ? 'output' : 'input' }}.</span>
|
||||
<button class="btn-sm" title="Keep the panel and its wires, but stop following the component" @click="ed.undock(n)">Detach</button>
|
||||
</p>
|
||||
<div class="field">
|
||||
|
|
@ -355,7 +358,7 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
<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)" />
|
||||
<select :value="r.type" title="Kind" @change="ed.setRowType(r, ($event.target as HTMLSelectElement).value as PanelControl)">
|
||||
<select v-if="!outPanel" :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>
|
||||
</select>
|
||||
<input
|
||||
|
|
@ -372,7 +375,7 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
/>
|
||||
<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">
|
||||
<label v-if="r.type !== 'clock' && !outPanel" 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>
|
||||
|
|
@ -384,8 +387,13 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
<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)" />
|
||||
<div v-if="r.type !== 'clock' || outPanel" class="swatches mini" title="Colour while on">
|
||||
<button
|
||||
class="swatch lamp-swatch"
|
||||
:class="[outPanel ? 'default' : 'default-on', { active: !r.color, button: r.type === 'button' && !outPanel }]"
|
||||
title="Default"
|
||||
@click="ed.setRowColor(r, undefined)"
|
||||
/>
|
||||
<button
|
||||
v-for="c in LAMP_COLORS"
|
||||
:key="c"
|
||||
|
|
@ -398,16 +406,25 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
</div>
|
||||
</li>
|
||||
</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>
|
||||
<div class="flex flex-wrap items-center gap-1">
|
||||
<button v-if="!n.attach" class="btn-sm" @click="ed.addRow(n)"><Icon name="lucide:plus" class="size-3.5" /> Add row</button>
|
||||
<template v-if="!outPanel && (n.rows?.length ?? 0) > 1">
|
||||
<span class="hint ml-auto">All rows:</span>
|
||||
<button v-for="c in CONTROLS" :key="c.value" class="btn-sm" :title="`Make every row a ${c.label.toLowerCase()}`" @click="ed.setRowsType(n, c.value)">{{ c.label }}</button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn w-full" :disabled="!n.rows?.length" @click="ed.startLinking([n.id])">
|
||||
<button class="btn w-full" :disabled="!n.rows?.length" @click="ed.startLinking([n.id], outPanel ? 'in' : 'out')">
|
||||
<Icon name="lucide:cable" class="size-4" /> Wire rows one by one
|
||||
</button>
|
||||
<label class="check mt-2" title="Used as a component, its switch and button rows are its input pins (top to bottom, with the other inputs). Clock rows stay inside.">
|
||||
<label
|
||||
class="check mt-2"
|
||||
:title="outPanel ? 'Used as a component, its rows are output pins (top to bottom, with the other outputs).' : 'Used as a component, its switch and button rows are its input pins (top to bottom, with the other inputs). Clock rows stay inside.'"
|
||||
>
|
||||
<input type="checkbox" :checked="!n.inner" @change="setInner(!($event.target as HTMLInputElement).checked)" />
|
||||
Rows are component inputs
|
||||
Rows are component {{ outPanel ? 'outputs' : 'inputs' }}
|
||||
</label>
|
||||
<p v-if="n.inner" class="hint">Kept inside: in a component the switches stay as set here, buttons read 0.</p>
|
||||
<p v-if="n.inner" class="hint">{{ outPanel ? 'Kept inside: the lamps only show in here.' : 'Kept inside: in a component the switches stay as set here, buttons read 0.' }}</p>
|
||||
</template>
|
||||
|
||||
<div v-if="cables.out.length || cables.in.length" class="field cable-choices">
|
||||
|
|
@ -559,6 +576,9 @@ function setRowHz(row: PanelRow, e: Event) {
|
|||
>
|
||||
<Icon name="lucide:sliders-vertical" class="size-4" /> Attach input panel
|
||||
</button>
|
||||
<button v-if="chipOutputs && !hasOutPanel" class="btn w-full" title="A lamp for every output, docked to the right" @click="ed.addPanel(n.id, 'out')">
|
||||
<Icon name="lucide:list-checks" class="size-4" /> Attach output panel
|
||||
</button>
|
||||
<div v-if="chipInputs || chipOutputs" class="field mb-0">
|
||||
<span>Wire one by one</span>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
|
|
|
|||
|
|
@ -357,6 +357,18 @@ const title = computed(() => {
|
|||
</g>
|
||||
</template>
|
||||
|
||||
<!-- Output panel: a lamp per row -->
|
||||
<template v-else-if="node.kind === 'outpanel'">
|
||||
<text v-if="node.label" :x="(b.w * G) / 2" :y="b.y * G - 5" text-anchor="middle" class="instance-label">{{ node.label }}</text>
|
||||
<rect :x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="3" class="body panel" :class="{ docked: !!node.attach }" />
|
||||
<g v-for="(p, i) in shape.inputs" :key="p.key" :transform="`translate(0 ${p.dy * G})`">
|
||||
<path :d="`M0 0H${0.4 * G}`" class="stub" />
|
||||
<circle :cx="0.95 * G" cy="0" :r="0.36 * G" class="lamp" :class="[rows[i]?.color ? `l-${rows[i]!.color}` : '', { on: v(p.key) === 1 }]" />
|
||||
<text :x="1.6 * G" y="3.5" class="pin-label" :class="{ named: !!rows[i]?.label || !!rows[i]?.name }">{{ p.label }}</text>
|
||||
<path :d="`M${(b.w - 0.35) * G} 0H${b.w * G}`" class="stub" />
|
||||
</g>
|
||||
</template>
|
||||
|
||||
<!-- Note -->
|
||||
<template v-else-if="node.kind === 'note'">
|
||||
<rect :x="-4" :y="b.y * G" :width="b.w * G + 8" :height="b.h * G" rx="3" class="note-bg" />
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ const BASICS: { kind: NodeKind; name: string; icon: string; key?: string; hint:
|
|||
{ 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: '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: 'outpanel', name: 'Output panel', icon: 'lucide:list-checks', hint: 'A column of lamps with names. Inside a component its rows are output pins. "Attach output panel" on a component makes one for its outputs; select lamps or output pins to combine them into one.' },
|
||||
{ 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: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.' },
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ const emit = defineEmits<{ next: [e: KeyboardEvent] }>()
|
|||
const ed = useEditor()
|
||||
|
||||
const n = computed(() => props.e.node)
|
||||
const type = computed(() => (props.e.row ? props.e.row.type : n.value.kind))
|
||||
const type = computed(() => (props.e.row ? (n.value.kind === 'outpanel' ? 'lamp' : props.e.row.type) : n.value.kind))
|
||||
const switchable = computed(() => type.value === 'input' || type.value === 'switch')
|
||||
const holdable = computed(() => type.value === 'button')
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ function pinNumber(e: IoEntry, entries: IoEntry[]) {
|
|||
}
|
||||
|
||||
function kindText(e: IoEntry, entries: IoEntry[]): string {
|
||||
if (e.row && e.node.kind === 'outpanel') return e.pin ? `pin ${pinNumber(e, entries)} · lamp` : 'lamp'
|
||||
if (e.row && e.pin) return `pin ${pinNumber(e, entries)} · ${e.row.type}`
|
||||
if (e.row) return e.row.type === 'clock' && e.row.hz ? `clock ${e.row.hz} Hz` : e.row.type
|
||||
if (e.pin) return `pin ${pinNumber(e, entries)}`
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challeng
|
|||
import { compile, INVERTIBLE, invertedPins, rowSource } from '~/lib/logic/compile'
|
||||
import { chipPins, MAX_BITS, panelOffsets, shapeOf, type Pt } from '~/lib/logic/geometry'
|
||||
import { adoptName, continueSeries, newPinLabel, uniqueLabel, usedLabels } from '~/lib/logic/labels'
|
||||
import { attachPanel, detachPanel, dockedPanels, mergePanels, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels'
|
||||
import { attachPanel, combineIntoPanel, detachPanel, dockedPanels, mergePanels, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels'
|
||||
import {
|
||||
addExamples,
|
||||
containsDef,
|
||||
|
|
@ -456,7 +456,7 @@ function create() {
|
|||
}
|
||||
// A new reference uses the signal sent last.
|
||||
if (p.kind === 'ref') node.label = def.value.nodes.filter(n => n.kind === 'tag' && n.label?.trim()).at(-1)?.label?.trim() ?? ''
|
||||
if (p.kind === 'panel') node.rows = newPanelRows()
|
||||
if (p.kind === 'panel' || p.kind === 'outpanel') node.rows = newPanelRows()
|
||||
if (p.kind === 'bundle' || p.kind === 'split') node.bits = 4
|
||||
def.value.nodes.push(node)
|
||||
const swap = (r: PinRef) => (r.node === 'ghost' ? { node: node.id, pin: r.pin } : r)
|
||||
|
|
@ -718,7 +718,7 @@ function create() {
|
|||
|
||||
/** Click on a panel row: flips a switch row. */
|
||||
function toggleRow(node: CircuitNode, row: PanelRow) {
|
||||
if (row.type !== 'switch') return
|
||||
if (row.type !== 'switch' || node.kind !== 'panel') return
|
||||
row.on = !row.on
|
||||
sim.value?.setSource(rowSource(node.id, row.id), row.on)
|
||||
play(row.on ? 'on' : 'off')
|
||||
|
|
@ -726,7 +726,7 @@ function create() {
|
|||
}
|
||||
|
||||
function pressRow(node: CircuitNode, row: PanelRow, down: boolean) {
|
||||
if (row.type !== 'button') return
|
||||
if (row.type !== 'button' || node.kind !== 'panel') return
|
||||
const key = rowSource(node.id, row.id)
|
||||
if (down && !pressed.has(key)) play('press')
|
||||
if (down) pressed.add(key)
|
||||
|
|
@ -735,34 +735,65 @@ function create() {
|
|||
}
|
||||
|
||||
// ── Panels ────────────────────────────────────────────────────────
|
||||
function addPanel(chipId: string) {
|
||||
const panel = attachPanel(project.value, def.value, chipId)
|
||||
if (!panel) return notify('Every input of this component is already connected', 'error')
|
||||
/** Docks a panel to a component: switches for its free inputs (left), or lamps for its outputs (right). */
|
||||
function addPanel(chipId: string, side: 'in' | 'out' = 'in') {
|
||||
const panel = attachPanel(project.value, def.value, chipId, side)
|
||||
if (!panel) return notify(side === 'in' ? 'Every input of this component is already connected' : 'This component has no outputs', 'error')
|
||||
selection.value = [panel.id]
|
||||
wireSel.value = []
|
||||
commit()
|
||||
notify(`Input panel with ${panel.rows!.length} switch${panel.rows!.length === 1 ? '' : 'es'} attached`)
|
||||
const n = panel.rows!.length
|
||||
notify(side === 'in' ? `Input panel with ${n} switch${n === 1 ? '' : 'es'} attached` : `Output panel with ${n} lamp${n === 1 ? '' : 's'} attached`)
|
||||
}
|
||||
|
||||
/** Joins the selected panels into one; circuits that place this one keep their wires to the rows. */
|
||||
function mergeSelectedPanels() {
|
||||
const res = mergePanels(def.value, selection.value)
|
||||
if (!res) return notify('Select at least two input panels', 'error')
|
||||
/** Pins of the open circuit got new keys: circuits that place it keep their wires on them. */
|
||||
function followPins(renamed: Map<string, string>) {
|
||||
if (!renamed.size) return
|
||||
const id = def.value.id
|
||||
for (const d of project.value.components) {
|
||||
const chips = new Set(d.nodes.filter(n => n.kind === 'chip' && n.chip === id).map(n => n.id))
|
||||
if (!chips.size) continue
|
||||
for (const w of d.wires) {
|
||||
const to = chips.has(w.to.node) ? res.renamed.get(w.to.pin) : undefined
|
||||
const to = chips.has(w.to.node) ? renamed.get(w.to.pin) : undefined
|
||||
if (to) w.to = { node: w.to.node, pin: to }
|
||||
const from = chips.has(w.from.node) ? renamed.get(w.from.pin) : undefined
|
||||
if (from) w.from = { node: w.from.node, pin: from }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Joins the selected panels into one; circuits that place this one keep their wires to the rows. */
|
||||
function mergeSelectedPanels() {
|
||||
const res = mergePanels(def.value, selection.value)
|
||||
if (!res) return notify('Select at least two panels of the same kind', 'error')
|
||||
followPins(res.renamed)
|
||||
selection.value = [res.panel.id]
|
||||
wireSel.value = []
|
||||
commit()
|
||||
notify(`One panel with ${res.panel.rows?.length ?? 0} rows`)
|
||||
}
|
||||
|
||||
/** The selected switches, buttons and clocks as one input panel, or output pins and lamps as one output panel. */
|
||||
function combineSelected() {
|
||||
const res = combineIntoPanel(def.value, selection.value, getDef.value)
|
||||
if (!res) return notify('Select switches, buttons and clocks, or output pins and lamps (two or more)', 'error')
|
||||
followPins(res.renamed)
|
||||
selection.value = [res.panel.id]
|
||||
wireSel.value = []
|
||||
commit()
|
||||
notify(`${res.panel.kind === 'outpanel' ? 'Output' : 'Input'} panel with ${res.panel.rows?.length ?? 0} rows`)
|
||||
}
|
||||
|
||||
/** Every row of an input panel the same kind: all switches, all buttons or all clocks. */
|
||||
function setRowsType(panel: CircuitNode, type: PanelControl) {
|
||||
for (const r of panel.rows ?? []) {
|
||||
r.type = type
|
||||
if (type !== 'switch') delete r.on
|
||||
if (type !== 'clock') delete r.hz
|
||||
}
|
||||
commit()
|
||||
}
|
||||
|
||||
function undock(panel: CircuitNode) {
|
||||
detachPanel(panel)
|
||||
commit()
|
||||
|
|
@ -1971,6 +2002,8 @@ function create() {
|
|||
addPanel,
|
||||
undock,
|
||||
mergeSelectedPanels,
|
||||
combineSelected,
|
||||
setRowsType,
|
||||
setRowType,
|
||||
addRow,
|
||||
dropRow,
|
||||
|
|
|
|||
|
|
@ -68,13 +68,14 @@ const ADD: { kind: NodeKind; label: string; icon: string; keys?: string }[] = [
|
|||
{ kind: 'button', label: 'Push button', icon: 'lucide:circle-dot', keys: 'B' },
|
||||
{ kind: 'clock', label: 'Clock', icon: 'lucide:audio-waveform', keys: 'C' },
|
||||
{ kind: 'panel', label: 'Input panel', icon: 'lucide:sliders-vertical', keys: 'P' },
|
||||
{ kind: 'outpanel', label: 'Output panel', icon: 'lucide:list-checks' },
|
||||
{ kind: 'hex', label: 'Hex display', icon: 'lucide:binary', keys: 'H' },
|
||||
{ kind: 'tag', label: 'Send signal', icon: 'lucide:flag' },
|
||||
{ kind: 'ref', label: 'Use signal', icon: 'lucide:tag' },
|
||||
{ kind: 'note', label: 'Note', icon: 'lucide:sticky-note', keys: 'T' },
|
||||
]
|
||||
|
||||
const NAMED = ['input', 'output', 'lamp', 'button', 'clock', 'hex', 'note', 'panel', 'chip', 'tag', 'ref']
|
||||
const NAMED = ['input', 'output', 'lamp', 'button', 'clock', 'hex', 'note', 'panel', 'outpanel', 'chip', 'tag', 'ref']
|
||||
|
||||
export function useMenu() {
|
||||
const ed = useEditor()
|
||||
|
|
@ -212,7 +213,9 @@ export function useMenu() {
|
|||
const pins = chipPins(get(n.chip)!)
|
||||
const docked = ed.def.value.nodes.some(x => x.kind === 'panel' && x.attach === n.id)
|
||||
out.push('sep')
|
||||
const dockedOut = ed.def.value.nodes.some(x => x.kind === 'outpanel' && x.attach === n.id)
|
||||
if (pins.inputs.length && !docked) out.push({ label: 'Attach input panel', icon: 'lucide:sliders-vertical', run: () => ed.addPanel(n.id) })
|
||||
if (pins.outputs.length && !dockedOut) out.push({ label: 'Attach output panel', icon: 'lucide:list-checks', run: () => ed.addPanel(n.id, 'out') })
|
||||
if (pins.inputs.length) out.push({ label: 'Wire inputs one by one', icon: 'lucide:cable', run: () => ed.startLinking([n.id], 'in') })
|
||||
if (pins.outputs.length) out.push({ label: 'Wire outputs one by one', icon: 'lucide:cable', run: () => ed.startLinking([n.id], 'out') })
|
||||
out.push(
|
||||
|
|
@ -236,11 +239,22 @@ export function useMenu() {
|
|||
children: cables.in.map(g => ({ label: g.label, note: `${g.pins.length} wires`, run: () => ed.cableIn(n.id, g.pins) })),
|
||||
})
|
||||
}
|
||||
if (n.kind === 'panel') {
|
||||
if (n.kind === 'panel' || n.kind === 'outpanel') {
|
||||
out.push('sep')
|
||||
if (!n.attach) out.push({ label: 'Add row', icon: 'lucide:plus', run: () => ed.addRow(n) })
|
||||
else out.push({ label: 'Detach from component', icon: 'lucide:link-2-off', run: () => ed.undock(n) })
|
||||
out.push({ label: 'Wire rows one by one', icon: 'lucide:cable', disabled: !n.rows?.length, run: () => ed.startLinking([n.id]) })
|
||||
out.push({ label: 'Wire rows one by one', icon: 'lucide:cable', disabled: !n.rows?.length, run: () => ed.startLinking([n.id], n.kind === 'outpanel' ? 'in' : 'out') })
|
||||
if (n.kind === 'panel' && (n.rows?.length ?? 0) > 1) {
|
||||
out.push({
|
||||
label: 'All rows',
|
||||
icon: 'lucide:rows-3',
|
||||
children: [
|
||||
{ label: 'Switches', run: () => ed.setRowsType(n, 'switch') },
|
||||
{ label: 'Buttons', run: () => ed.setRowsType(n, 'button') },
|
||||
{ label: 'Clocks', run: () => ed.setRowsType(n, 'clock') },
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
if (n.kind === 'bundle' || n.kind === 'split') out.push('sep', ...cableEnd(n))
|
||||
if (n.kind === 'tag' || n.kind === 'ref') {
|
||||
|
|
@ -291,7 +305,19 @@ export function useMenu() {
|
|||
const fits = fitItems()
|
||||
if (fits.length) out.push({ label: 'Wire them all to', icon: 'lucide:cable', children: fits })
|
||||
out.push({ label: 'Connect one by one', icon: 'lucide:list-ordered', run: () => ed.startLinking() })
|
||||
if (nodes.every(n => n.kind === 'panel')) out.push({ label: 'Merge into one panel', icon: 'lucide:combine', run: () => ed.mergeSelectedPanels() })
|
||||
if (nodes.every(n => n.kind === 'panel') || nodes.every(n => n.kind === 'outpanel')) {
|
||||
out.push({ label: 'Merge into one panel', icon: 'lucide:combine', run: () => ed.mergeSelectedPanels() })
|
||||
}
|
||||
const inKinds = ['input', 'button', 'clock']
|
||||
const outKinds = ['output', 'lamp']
|
||||
if (nodes.every(n => inKinds.includes(n.kind)) || nodes.every(n => outKinds.includes(n.kind))) {
|
||||
const outs = nodes.every(n => outKinds.includes(n.kind))
|
||||
out.push({
|
||||
label: outs ? 'Combine into an output panel' : 'Combine into an input panel',
|
||||
icon: outs ? 'lucide:list-checks' : 'lucide:sliders-vertical',
|
||||
run: () => ed.combineSelected(),
|
||||
})
|
||||
}
|
||||
if (nodes.every(n => n.kind === 'input')) {
|
||||
out.push({
|
||||
label: 'Switches',
|
||||
|
|
|
|||
|
|
@ -69,7 +69,20 @@ export function ioList(def: ComponentDef): { inputs: IoEntry[]; outputs: IoEntry
|
|||
...of(['button', 'clock']).map(node => ({ node, pin: false })),
|
||||
...inside,
|
||||
]
|
||||
const outputs: IoEntry[] = [...of(['output']).map(node => ({ node, pin: true })), ...of(['lamp', 'hex']).map(node => ({ node, pin: false }))]
|
||||
const outAt = new Map<string, IoEntry>()
|
||||
for (const node of of(['output'])) outAt.set(node.id, { node, pin: true })
|
||||
const shown: IoEntry[] = []
|
||||
for (const node of of(['outpanel'])) {
|
||||
for (const row of node.rows ?? []) {
|
||||
if (!node.inner) outAt.set(rowPin(node.id, row.id), { node, row, pin: true })
|
||||
else shown.push({ node, row, pin: false })
|
||||
}
|
||||
}
|
||||
const outputs: IoEntry[] = [
|
||||
...chipPins(def).outputs.map(p => outAt.get(p.key)!).filter(Boolean),
|
||||
...of(['lamp', 'hex']).map(node => ({ node, pin: false })),
|
||||
...shown,
|
||||
]
|
||||
return { inputs, outputs }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ export interface Issue {
|
|||
}
|
||||
|
||||
/** Parts whose open inputs are worth a warning (a cable bundle's spare wires are not). */
|
||||
export const CHECKED_INPUTS = new Set(['nand', 'chip', 'output', 'lamp', 'hex', 'pixel', 'tag'])
|
||||
export const CHECKED_INPUTS = new Set(['nand', 'chip', 'output', 'lamp', 'hex', 'pixel', 'tag', 'outpanel'])
|
||||
|
||||
function partName(n: CircuitNode, get: DefLookup): string {
|
||||
const own = !n.autoLabel && n.label?.trim()
|
||||
|
|
|
|||
|
|
@ -46,6 +46,8 @@ export function inputKeys(node: CircuitNode, pins?: ChipPins): string[] {
|
|||
return ['a']
|
||||
case 'hex':
|
||||
return ['b0', 'b1', 'b2', 'b3']
|
||||
case 'outpanel':
|
||||
return (node.rows ?? []).map(r => r.id)
|
||||
case 'bundle':
|
||||
return cableKeys(node)
|
||||
case 'chip':
|
||||
|
|
@ -73,6 +75,7 @@ export function outputKeys(node: CircuitNode, pins?: ChipPins): string[] {
|
|||
case 'hex':
|
||||
return ['q0', 'q1', 'q2', 'q3']
|
||||
case 'panel':
|
||||
case 'outpanel':
|
||||
return (node.rows ?? []).map(r => r.id)
|
||||
case 'bundle':
|
||||
return ['q']
|
||||
|
|
@ -261,6 +264,14 @@ export function compile(getDef: DefLookup, rootId: string, opts: { trace?: boole
|
|||
case 'hex':
|
||||
for (let i = 0; i < 4; i++) union(outNet.get(`${n.id}:q${i}`)!, inNet(n.id, `b${i}`))
|
||||
break
|
||||
case 'outpanel':
|
||||
// Each row shows its signal and passes it on; inside a component the rows are its outputs.
|
||||
for (const r of n.rows ?? []) {
|
||||
const net = inNet(n.id, r.id)
|
||||
union(outNet.get(`${n.id}:${r.id}`)!, net)
|
||||
if (!n.inner) result.set(rowPin(n.id, r.id), net)
|
||||
}
|
||||
break
|
||||
case 'chip': {
|
||||
const child = n.chip ? getDef(n.chip) : undefined
|
||||
const pins = pinsOf(n)
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ function wiredPins(def: ComponentDef) {
|
|||
*/
|
||||
function plugPin(n: CircuitNode, dir: 'in' | 'out', shape: Shape, wired: ReturnType<typeof wiredPins>): string | null {
|
||||
if (dir === 'in') {
|
||||
if (['note', 'panel', 'bundle', 'split', 'hex'].includes(n.kind)) return null
|
||||
if (['note', 'panel', 'outpanel', 'bundle', 'split', 'hex'].includes(n.kind)) return null
|
||||
const free = shape.inputs.filter(p => !p.bus && !wired.in.has(`${n.id}:${p.key}`))
|
||||
return free.length === 1 ? free[0]!.key : null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,10 +88,20 @@ export function chipPins(def: ComponentDef): ChipPins {
|
|||
}
|
||||
}
|
||||
inputs.sort((a, b) => a.y - b.y || a.x - b.x)
|
||||
const outputs = def.nodes.filter(n => n.kind === 'output').sort((a, b) => a.y - b.y || a.x - b.x)
|
||||
// Outputs: output pins and the rows of output panels.
|
||||
const outputs: { key: string; label: string; x: number; y: number }[] = []
|
||||
for (const n of def.nodes) {
|
||||
if (n.kind === 'output') outputs.push({ key: n.id, label: n.label?.trim() ?? '', x: n.x, y: n.y })
|
||||
else if (n.kind === 'outpanel' && !n.inner) {
|
||||
const rows = n.rows ?? []
|
||||
const at = panelOffsets(rows)
|
||||
rows.forEach((r, i) => outputs.push({ key: rowPin(n.id, r.id), label: r.label?.trim() || r.name?.trim() || '', x: n.x, y: n.y + at[i]! }))
|
||||
}
|
||||
}
|
||||
outputs.sort((a, b) => a.y - b.y || a.x - b.x)
|
||||
return {
|
||||
inputs: inputs.map((p, i) => ({ key: p.key, label: p.label || `in${i + 1}` })),
|
||||
outputs: outputs.map((n, i) => ({ key: n.id, label: n.label?.trim() || `out${i + 1}` })),
|
||||
outputs: outputs.map((p, i) => ({ key: p.key, label: p.label || `out${i + 1}` })),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -267,6 +277,20 @@ function tagWidth(node: CircuitNode): number {
|
|||
/** The name a signal tag sends, or a reference uses. */
|
||||
export const signalName = (node: CircuitNode) => node.label?.trim() ?? ''
|
||||
|
||||
/** Output panel: a lamp per row with its name, the wire in on the left and on again on the right. */
|
||||
function outPanelShape(node: CircuitNode): Shape {
|
||||
const rows = node.rows ?? []
|
||||
const dys = panelOffsets(rows)
|
||||
const labels = rows.map(rowLabel)
|
||||
const w = Math.max(3, Math.ceil(labels.reduce((m, l) => Math.max(m, l.length), 0) * LABEL_CHAR + 2.4))
|
||||
const last = dys.length ? dys[dys.length - 1]! : 0
|
||||
return {
|
||||
body: { x: 0, y: -0.5, w, h: last + 1 },
|
||||
inputs: rows.map((r, i) => ({ key: r.id, label: labels[i]!, dx: 0, dy: dys[i]! })),
|
||||
outputs: rows.map((r, i) => ({ key: r.id, label: labels[i]!, dx: w, dy: dys[i]! })),
|
||||
}
|
||||
}
|
||||
|
||||
/** Send: the wire comes in on the left into a flag carrying the name. */
|
||||
function tagShape(node: CircuitNode): Shape {
|
||||
const w = tagWidth(node)
|
||||
|
|
@ -393,6 +417,8 @@ export function shapeOf(node: CircuitNode, getDef: DefLookup): Shape {
|
|||
return cableShape(node)
|
||||
case 'pixel':
|
||||
return PIXEL_SHAPE
|
||||
case 'outpanel':
|
||||
return outPanelShape(node)
|
||||
case 'tag':
|
||||
return tagShape(node)
|
||||
case 'ref':
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ 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 === 'output' || kind === 'lamp' || kind === 'hex' || kind === 'outpanel') return 'out'
|
||||
if (kind === 'chip') return 'chip'
|
||||
if (kind === 'tag') return 'signal'
|
||||
return null
|
||||
|
|
@ -76,7 +76,7 @@ export function continueSeries(def: ComponentDef, pasted: CircuitNode[]) {
|
|||
|
||||
/** 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') {
|
||||
if (node.kind === 'panel' || node.kind === 'outpanel') {
|
||||
const row = node.rows?.find(r => r.id === pin)
|
||||
return row?.label?.trim() || row?.name?.trim() || undefined
|
||||
}
|
||||
|
|
@ -104,9 +104,10 @@ const ADOPTERS = new Set<NodeKind>(['input', 'button', 'output', 'lamp', 'tag'])
|
|||
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
|
||||
// An output panel has a pin per row; other parts are counted whole.
|
||||
const links = def.wires.filter(w => (w.from.node === node.id && w.from.pin === pin) || (w.to.node === node.id && (node.kind !== 'outpanel' || w.to.pin === pin))).length
|
||||
if (links !== 1) return false
|
||||
if (node.kind === 'panel') {
|
||||
if (node.kind === 'panel' || node.kind === 'outpanel') {
|
||||
const row = node.rows?.find(r => r.id === pin)
|
||||
if (!row || row.label?.trim() || row.name === name) return false
|
||||
row.name = name
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
// 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.
|
||||
// Panels: an input panel is a column of switches, buttons and clocks, an
|
||||
// output panel a column of lamps. A panel can be wired like any other part,
|
||||
// or docked to a component: an input panel on its left gets a row per free
|
||||
// input, an output panel on its right a row per output. A docked panel moves
|
||||
// with its component and follows it when the component gains or loses pins.
|
||||
|
||||
import { chipPins, panelOffsets, rowPin, shapeOf, type DefLookup } from './geometry'
|
||||
import { pinName } from './labels'
|
||||
import { lookup, uid } from './ops'
|
||||
import type { CircuitNode, ComponentDef, PanelRow, Project } from './types'
|
||||
|
||||
|
|
@ -14,9 +16,11 @@ export function newPanelRows(count = 4): PanelRow[] {
|
|||
return Array.from({ length: count }, () => ({ id: uid('r'), type: 'switch' as const }))
|
||||
}
|
||||
|
||||
export const isPanel = (n: CircuitNode) => n.kind === 'panel' || n.kind === 'outpanel'
|
||||
|
||||
/** 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))
|
||||
return def.nodes.filter(n => isPanel(n) && n.attach && chipIds.has(n.attach))
|
||||
}
|
||||
|
||||
interface ChipInput {
|
||||
|
|
@ -25,11 +29,23 @@ interface ChipInput {
|
|||
dy: number
|
||||
}
|
||||
|
||||
function chipInputs(chip: CircuitNode, get: DefLookup): ChipInput[] | null {
|
||||
/** The pins of a chip a panel docks to: its inputs for an input panel, its outputs for an output panel. */
|
||||
function chipInputs(chip: CircuitNode, get: DefLookup, side: 'in' | 'out' = 'in'): 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 }))
|
||||
const pins = chipPins(child)
|
||||
const labels = new Map((side === 'in' ? pins.inputs : pins.outputs).map(p => [p.key, p.label]))
|
||||
const shape = shapeOf(chip, get)
|
||||
return (side === 'in' ? shape.inputs : shape.outputs).map(p => ({ key: p.key, label: labels.get(p.key) ?? p.label, dy: p.dy }))
|
||||
}
|
||||
|
||||
const sideOf = (panel: CircuitNode) => (panel.kind === 'outpanel' ? 'out' : 'in')
|
||||
|
||||
/** A wire between a docked panel's row and its chip's pin, the right way round. */
|
||||
function dockWire(panel: CircuitNode, chipId: string, key: string) {
|
||||
return panel.kind === 'outpanel'
|
||||
? { id: uid('w'), from: { node: chipId, pin: key }, to: { node: panel.id, pin: key } }
|
||||
: { id: uid('w'), from: { node: panel.id, pin: key }, to: { node: chipId, pin: key } }
|
||||
}
|
||||
|
||||
/** Lines the rows up with the chip's pins and names them after the pins. */
|
||||
|
|
@ -48,21 +64,23 @@ function layout(panel: CircuitNode, pins: ChipInput[]) {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Docks a new panel to a placed component: an input panel on its left with
|
||||
* a switch for every input that is not wired yet, or an output panel on its
|
||||
* right with a lamp for every output. Returns the panel, or null when there
|
||||
* is nothing to show.
|
||||
*/
|
||||
export function attachPanel(project: Project, def: ComponentDef, chipId: string): CircuitNode | null {
|
||||
export function attachPanel(project: Project, def: ComponentDef, chipId: string, side: 'in' | 'out' = 'in'): CircuitNode | null {
|
||||
const get = lookup(project)
|
||||
const chip = def.nodes.find(n => n.id === chipId)
|
||||
const pins = chip && chipInputs(chip, get)
|
||||
const pins = chip && chipInputs(chip, get, side)
|
||||
if (!chip || !pins) return null
|
||||
const wired = new Set(def.wires.filter(w => w.to.node === chipId).map(w => w.to.pin))
|
||||
// Inputs take one wire; outputs can feed a lamp as well as anything else.
|
||||
const wired = side === 'in' ? new Set(def.wires.filter(w => w.to.node === chipId).map(w => w.to.pin)) : new Set<string>()
|
||||
const free = pins.filter(p => !wired.has(p.key))
|
||||
if (!free.length) return null
|
||||
const panel: CircuitNode = {
|
||||
id: uid('n'),
|
||||
kind: 'panel',
|
||||
kind: side === 'in' ? 'panel' : 'outpanel',
|
||||
x: 0,
|
||||
y: 0,
|
||||
attach: chipId,
|
||||
|
|
@ -70,10 +88,11 @@ export function attachPanel(project: Project, def: ComponentDef, chipId: string)
|
|||
}
|
||||
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
|
||||
const chipBody = shapeOf(chip, get).body
|
||||
panel.x = side === 'in' ? chip.x - DOCK_GAP - shapeOf(panel, get).body.w : chip.x + chipBody.x + chipBody.w + DOCK_GAP
|
||||
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 } })
|
||||
for (const r of panel.rows!) def.wires.push(dockWire(panel, chipId, r.id))
|
||||
return panel
|
||||
}
|
||||
|
||||
|
|
@ -86,12 +105,14 @@ export function attachPanel(project: Project, def: ComponentDef, chipId: string)
|
|||
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
|
||||
nameFreeRows(def, get)
|
||||
if (!def.nodes.some(n => isPanel(n) && 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
|
||||
if (!isPanel(panel) || !panel.attach) continue
|
||||
const side = sideOf(panel)
|
||||
const chip = nodes.get(panel.attach)
|
||||
const pins = chip && chipInputs(chip, get)
|
||||
const pins = chip && chipInputs(chip, get, side)
|
||||
if (!chip || !pins) {
|
||||
delete panel.attach
|
||||
delete panel.skip
|
||||
|
|
@ -101,13 +122,13 @@ export function syncPanels(project: Project) {
|
|||
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))
|
||||
const wired = side === 'in' ? new Set(def.wires.filter(w => w.to.node === chip.id).map(w => w.to.pin)) : new Set<string>()
|
||||
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 } })
|
||||
def.wires.push(dockWire(panel, chip.id, p.key))
|
||||
}
|
||||
}
|
||||
panel.rows = rows
|
||||
|
|
@ -120,10 +141,31 @@ export function syncPanels(project: Project) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows of free panels without a name of their own take the name of the pin
|
||||
* they are wired to: an output panel's row the one that feeds it, an input
|
||||
* panel's row the one it drives.
|
||||
*/
|
||||
function nameFreeRows(def: ComponentDef, get: DefLookup) {
|
||||
const panels = def.nodes.filter(n => isPanel(n) && !n.attach && n.rows?.some(r => !r.label?.trim()))
|
||||
if (!panels.length) return
|
||||
const nodes = new Map(def.nodes.map(n => [n.id, n]))
|
||||
for (const p of panels) {
|
||||
for (const r of p.rows!) {
|
||||
if (r.label?.trim()) continue
|
||||
const w = p.kind === 'outpanel' ? def.wires.find(x => x.to.node === p.id && x.to.pin === r.id) : def.wires.find(x => x.from.node === p.id && x.from.pin === r.id)
|
||||
const other = w && (p.kind === 'outpanel' ? w.from : w.to)
|
||||
const node = other && nodes.get(other.node)
|
||||
const name = node && (pinName(node, other.pin, get) ?? (['input', 'button', 'clock', 'output', 'lamp'].includes(node.kind) ? node.label?.trim() : undefined))
|
||||
if (name) r.name = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 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))
|
||||
def.wires = def.wires.filter(w => !(w.from.node === panel.id && w.from.pin === rowId) && !(w.to.node === panel.id && w.to.pin === rowId))
|
||||
if (panel.attach) panel.skip = [...new Set([...(panel.skip ?? []), rowId])]
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +183,10 @@ export function detachPanel(panel: CircuitNode) {
|
|||
* 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)
|
||||
const sel = def.nodes.filter(n => isPanel(n) && ids.includes(n.id))
|
||||
// Input panels join input panels, output panels output panels.
|
||||
const kind = sel[0]?.kind
|
||||
const panels = sel.filter(n => n.kind === kind).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 ?? []
|
||||
|
|
@ -157,7 +202,10 @@ export function mergePanels(def: ComponentDef, ids: string[]): { panel: CircuitN
|
|||
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 }
|
||||
for (const w of def.wires) {
|
||||
if (w.from.node === p.id && w.from.pin === r.id) w.from = { node: target.id, pin: id }
|
||||
if (w.to.node === p.id && w.to.pin === r.id) w.to = { node: target.id, pin: id }
|
||||
}
|
||||
renamed.set(rowPin(p.id, r.id), rowPin(target.id, id))
|
||||
rows.push({ ...r, id, dy: off[i]! + shift })
|
||||
})
|
||||
|
|
@ -171,3 +219,51 @@ export function mergePanels(def: ComponentDef, ids: string[]): { panel: CircuitN
|
|||
def.wires = def.wires.filter(w => !gone.has(w.from.node) && !gone.has(w.to.node))
|
||||
return { panel: target, renamed }
|
||||
}
|
||||
|
||||
const SOURCES = new Set(['input', 'button', 'clock'])
|
||||
const SINKS = new Set(['output', 'lamp'])
|
||||
|
||||
/**
|
||||
* Turns separate switches, buttons and clocks into one input panel, or
|
||||
* output pins and lamps into one output panel: a row each, top to bottom,
|
||||
* with their names, settings and wires. Rows keep their distance where the
|
||||
* parts were spread out. Returns the panel and the pin keys that changed
|
||||
* (old → new) for circuits that place this one; null when the parts are
|
||||
* not all of one side or fewer than two.
|
||||
*/
|
||||
export function combineIntoPanel(def: ComponentDef, ids: string[], get: DefLookup): { panel: CircuitNode; renamed: Map<string, string> } | null {
|
||||
const sel = def.nodes.filter(n => ids.includes(n.id)).sort((a, b) => a.y - b.y || a.x - b.x)
|
||||
const out = sel.length > 0 && sel.every(n => SINKS.has(n.kind))
|
||||
if (sel.length < 2 || !(out || sel.every(n => SOURCES.has(n.kind)))) return null
|
||||
const panel: CircuitNode = { id: uid('n'), kind: out ? 'outpanel' : 'panel', x: 0, y: sel[0]!.y, rows: [] }
|
||||
// Pins stay pins; switches, buttons, clocks and lamps that were not pins stay inside.
|
||||
const pinKind = out ? 'output' : 'input'
|
||||
if (!sel.some(n => n.kind === pinKind)) panel.inner = true
|
||||
const renamed = new Map<string, string>()
|
||||
let prev = -1
|
||||
for (const n of sel) {
|
||||
const dy = Math.max(prev + 1, n.y - sel[0]!.y)
|
||||
prev = dy
|
||||
const row: PanelRow = { id: n.id, type: n.kind === 'button' ? 'button' : n.kind === 'clock' ? 'clock' : 'switch', dy }
|
||||
const label = n.label?.trim()
|
||||
if (label && !n.autoLabel) row.label = label
|
||||
else if (label) row.name = label
|
||||
if (n.on) row.on = true
|
||||
if (n.key) row.key = n.key
|
||||
if (n.hz) row.hz = n.hz
|
||||
if (n.color) row.color = n.color
|
||||
panel.rows!.push(row)
|
||||
if (n.kind === pinKind) renamed.set(n.id, rowPin(panel.id, n.id))
|
||||
for (const w of def.wires) {
|
||||
// A lamp's or output pin's pass-through leaves the row on the right, as before.
|
||||
if (w.from.node === n.id) w.from = { node: panel.id, pin: n.id }
|
||||
if (w.to.node === n.id) w.to = { node: panel.id, pin: n.id }
|
||||
}
|
||||
}
|
||||
// Where the parts' pins were: switches' outputs on the panel's right edge, lamps' inputs on its left.
|
||||
const w = shapeOf(panel, get).body.w
|
||||
panel.x = out ? Math.min(...sel.map(n => n.x)) : Math.max(...sel.map(n => n.x + (shapeOf(n, get).outputs[0]?.dx ?? 2))) - w
|
||||
const gone = new Set(sel.map(n => n.id))
|
||||
def.nodes = [...def.nodes.filter(n => !gone.has(n.id)), panel]
|
||||
return { panel, renamed }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ export const KIND_NAMES: Partial<Record<NodeKind, string>> = {
|
|||
hex: 'Hex display',
|
||||
note: 'Note',
|
||||
panel: 'Input panel',
|
||||
outpanel: 'Output panel',
|
||||
bundle: 'Cable bundle',
|
||||
split: 'Cable split',
|
||||
pixel: 'Pixel',
|
||||
|
|
@ -53,6 +54,7 @@ export const KIND_UNLOCK: Partial<Record<NodeKind, string>> = {
|
|||
high: 'not',
|
||||
low: 'not',
|
||||
panel: 'and',
|
||||
outpanel: 'and',
|
||||
hex: 'dec2',
|
||||
pixel: 'dec2',
|
||||
bundle: 'add2',
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ export type BuiltinKind =
|
|||
| 'hex' // 4-bit hex display
|
||||
| 'note' // text annotation, no pins
|
||||
| 'panel' // column of switches, buttons and clocks, one output per row
|
||||
| 'outpanel' // column of lamps, one input per row (passed on to the right); inside a component its rows are outputs
|
||||
| 'bundle' // gathers several wires into one cable
|
||||
| 'split' // fans a cable out into its wires again
|
||||
| 'pixel' // one square of a screen; inside a component, pixels form a screen on its box
|
||||
|
|
@ -63,7 +64,7 @@ export interface CircuitNode {
|
|||
names?: string[]
|
||||
/** Input pins with a bubble: the signal comes in inverted, through one NAND (counted like any other). */
|
||||
invert?: string[]
|
||||
/** Panel whose rows stay inside a component (as fixed switches) instead of becoming its input pins. */
|
||||
/** Panel whose rows stay inside a component instead of becoming its pins (an input panel's switches stay as set). */
|
||||
inner?: boolean
|
||||
}
|
||||
|
||||
|
|
@ -71,8 +72,9 @@ export const PANEL_CONTROLS = ['switch', 'button', 'clock'] as const
|
|||
export type PanelControl = (typeof PANEL_CONTROLS)[number]
|
||||
|
||||
export interface PanelRow {
|
||||
/** Output pin key. On an attached panel: the key of the chip pin it drives. */
|
||||
/** Pin key. On an attached panel: the key of the chip pin it is wired to. */
|
||||
id: string
|
||||
/** What an input panel row is; an output panel row is always a lamp. */
|
||||
type: PanelControl
|
||||
/** Name set by the user. */
|
||||
label?: string
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
|
|||
import { compile } from '../app/lib/logic/compile'
|
||||
import { chipPins, panelOffsets } from '../app/lib/logic/geometry'
|
||||
import { createProject, lookup } from '../app/lib/logic/ops'
|
||||
import { mergePanels } from '../app/lib/logic/panels'
|
||||
import { attachPanel, combineIntoPanel, mergePanels } from '../app/lib/logic/panels'
|
||||
import { Simulator } from '../app/lib/logic/sim'
|
||||
import type { ComponentDef } from '../app/lib/logic/types'
|
||||
|
||||
|
|
@ -73,3 +73,120 @@ describe('merging input panels', () => {
|
|||
expect(sim.pin('u', 'q')).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('output panels', () => {
|
||||
it('show their signals, pass them on, and are the outputs of their component', () => {
|
||||
const p = createProject({ examples: false })
|
||||
const inner: ComponentDef = {
|
||||
id: 'inner',
|
||||
name: 'Inner',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
|
||||
{ id: 'o', kind: 'outpanel', x: 6, y: 0, rows: [{ id: 'r1', type: 'switch', label: 'Y' }, { id: 'r2', type: 'switch', label: 'Z' }] },
|
||||
{ id: 'l', kind: 'lamp', x: 14, y: 0 },
|
||||
],
|
||||
wires: [
|
||||
{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'o', pin: 'r1' } },
|
||||
{ id: 'x', from: { node: 'o', pin: 'r1' }, to: { node: 'l', pin: 'a' } },
|
||||
],
|
||||
}
|
||||
p.components.push(inner)
|
||||
expect(chipPins(inner).outputs).toEqual([
|
||||
{ key: 'o:r1', label: 'Y' },
|
||||
{ key: 'o:r2', label: 'Z' },
|
||||
])
|
||||
const root: ComponentDef = {
|
||||
id: 'root',
|
||||
name: 'Root',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
{ id: 's', kind: 'input', x: -5, y: 0 },
|
||||
{ id: 'u', kind: 'chip', chip: 'inner', x: 0, y: 0 },
|
||||
{ id: 'm', kind: 'lamp', x: 10, y: 0 },
|
||||
],
|
||||
wires: [
|
||||
{ id: 'a', from: { node: 's', pin: 'q' }, to: { node: 'u', pin: 'a' } },
|
||||
{ id: 'b', from: { node: 'u', pin: 'o:r1' }, to: { node: 'm', pin: 'a' } },
|
||||
],
|
||||
}
|
||||
p.components.push(root)
|
||||
const sim = new Simulator(compile(lookup(p), 'root'))
|
||||
sim.setSource('s', true)
|
||||
sim.run()
|
||||
expect(sim.pin('m', 'a')).toBe(1)
|
||||
// In its own circuit: the row lights and passes the signal on.
|
||||
const own = new Simulator(compile(lookup(p), 'inner'))
|
||||
own.setSource('a', true)
|
||||
own.run()
|
||||
expect([own.pin('o', 'r1'), own.pin('l', 'a')]).toEqual([1, 1])
|
||||
})
|
||||
|
||||
it('docks to the right of a component with a lamp per output, and follows new outputs', () => {
|
||||
const p = createProject({ examples: true })
|
||||
const root: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes: [{ id: 'u', kind: 'chip', chip: 'ex-add4', x: 0, y: 0 }], wires: [] }
|
||||
p.components.push(root)
|
||||
const panel = attachPanel(p, root, 'u', 'out')!
|
||||
expect(panel.kind).toBe('outpanel')
|
||||
expect(panel.rows!.map(r => r.name)).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
|
||||
expect(panel.x).toBeGreaterThan(0)
|
||||
expect(root.wires.every(w => w.from.node === 'u' && w.to.node === panel.id)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('combining parts into a panel', () => {
|
||||
it('switches and a button become one input panel; their pins, names, states and wires stay', () => {
|
||||
const p = createProject({ examples: false })
|
||||
const def: ComponentDef = {
|
||||
id: 'inner',
|
||||
name: 'Inner',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A', on: true, key: 'q' },
|
||||
{ id: 'b', kind: 'input', x: 0, y: 3, label: 'B' },
|
||||
{ id: 'go', kind: 'button', x: 0, y: 4, label: 'GO' },
|
||||
{ id: 'o', kind: 'output', x: 10, y: 0, label: 'Q' },
|
||||
],
|
||||
wires: [{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'o', pin: 'a' } }],
|
||||
}
|
||||
p.components.push(def)
|
||||
const res = combineIntoPanel(def, ['a', 'b', 'go'], lookup(p))!
|
||||
const panel = res.panel
|
||||
expect(panel.kind).toBe('panel')
|
||||
expect(panel.inner).toBeUndefined()
|
||||
expect(panel.rows!.map(r => [r.label, r.type, r.on ?? false, r.key ?? ''])).toEqual([
|
||||
['A', 'switch', true, 'q'],
|
||||
['B', 'switch', false, ''],
|
||||
['GO', 'button', false, ''],
|
||||
])
|
||||
// Spread-out parts keep their distance; B and GO were one row apart.
|
||||
expect(panelOffsets(panel.rows!)).toEqual([0, 3, 4])
|
||||
expect(def.wires[0]!.from).toEqual({ node: panel.id, pin: 'a' })
|
||||
expect(res.renamed.get("a")).toBe(`${panel.id}:a`)
|
||||
expect(def.nodes.some(n => ['a', 'b', 'go'].includes(n.id))).toBe(false)
|
||||
// Still drives Q when used: pins A, B, GO.
|
||||
expect(chipPins(def).inputs.map(x => x.label)).toEqual(['A', 'B', 'GO'])
|
||||
})
|
||||
|
||||
it('lamps become an output panel that stays inside, as lamps did', () => {
|
||||
const def: ComponentDef = {
|
||||
id: 'd',
|
||||
name: 'D',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
{ id: 's', kind: 'input', x: 0, y: 0 },
|
||||
{ id: 'l1', kind: 'lamp', x: 6, y: 0, label: 'L1', color: 'red' },
|
||||
{ id: 'l2', kind: 'lamp', x: 6, y: 1 },
|
||||
],
|
||||
wires: [{ id: 'w', from: { node: 's', pin: 'q' }, to: { node: 'l1', pin: 'a' } }],
|
||||
}
|
||||
const p = createProject({ examples: false })
|
||||
p.components.push(def)
|
||||
const res = combineIntoPanel(def, ['l1', 'l2'], lookup(p))!
|
||||
expect(res.panel.kind).toBe('outpanel')
|
||||
expect(res.panel.inner).toBe(true)
|
||||
expect(res.panel.rows![0]).toMatchObject({ label: 'L1', color: 'red' })
|
||||
expect(def.wires[0]!.to).toEqual({ node: res.panel.id, pin: 'l1' })
|
||||
expect(combineIntoPanel(def, ['s'], lookup(p))).toBeNull()
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue