A one-click cable bundling with smarter placement, BF unlink keeps the cable
- "Bundle into cable" (Ctrl+G) places both ends itself again: right after the sources on their middle row, right before the targets on theirs, so the wires keep their flow; backwards wires get a backwards cable, and an end steps aside when a part is in the way. "Place the ends yourself…" (Ctrl+Shift+G) keeps the two-click placement. - "Unlink all" on a bundle or split removes only the wires going in or out; the cable between them stays. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
cf24578248
commit
4fddb8cd34
|
|
@ -13,7 +13,7 @@ Nuxt 4 (client-only editor, `ssr: false`), Tailwind CSS v4, Lucide icons via `@n
|
|||
- Puzzles: "Publish as puzzle" (This circuit) turns a circuit into a link `/puzzle/<id>`. Only its behaviour is published, as a truth table, or for circuits with memory as a run of input changes from power-on; the circuit stays private. Whoever opens the link gets the puzzle in their project, sees the target and the author's transistor count, and after a pass their solution goes to the server, which re-runs the checks and counts the transistors itself (with a fixed work budget) and keeps each player's best. The board shows everyone's transistor counts and your place. The puzzle of the week is generated from the ISO week, the same for everyone: three or four inputs, one or two outputs, each depending on every input. Players are anonymous ids per browser.
|
||||
- Sound (speaker in the top bar; volume and clock ticks in Settings, the gear in the top bar): clicks for placing, wiring, switches and buttons, a tick per clock cycle up to 8 Hz (off by default), and jingles for a solve, a failed check, unlocks and new records. All synthesised with Web Audio, no sound files ([app/lib/audio.ts](app/lib/audio.ts)).
|
||||
- Transistors: every component shows how many it takes, counted as a CMOS NAND (4 transistors) per gate at every depth: in the palette, its settings and the circuit stats. Each challenge keeps your record, the fewest transistors of any passing solution, and says so when you beat it.
|
||||
- Cables: a bundle gathers 2 to 32 wires into one cable, a split fans it out again in the same order; one bundle can feed several splits. Select the wires (or parts and wires with a box) and press "Bundle into cable…" or Ctrl+G, then click where the cable starts and where it ends (a preview follows the mouse; Esc cancels); "place it for me" puts the ends next to the sources and targets. Drag the bundle and split later to move the ends. Up to 16 wires, the cable is drawn as a ribbon: each wire is its own thin line inside, in its own colour, lit while it carries a 1. The sleeve starts neutral and gets its own colour (cable or bundle/split settings). "Remove cable" turns it back into single wires. A cable end only joins another cable end, and the cable shows its width.
|
||||
- Cables: a bundle gathers 2 to 32 wires into one cable, a split fans it out again in the same order; one bundle can feed several splits. Select the wires (a box across them, or Shift + click) and press "Bundle into cable" or Ctrl+G: the cable starts right after the sources on their middle row and ends right before the targets on theirs, so the wires keep running as they did (backwards too); an end steps aside when another part is in the way. "Place the ends yourself…" (Ctrl+Shift+G) lets you click where it starts and ends, with a preview; Esc cancels. "Unlink all" on a bundle or split removes only the wires going in or out; the cable stays. Drag the bundle and split later to move the ends. Up to 16 wires, the cable is drawn as a ribbon: each wire is its own thin line inside, in its own colour, lit while it carries a 1. The sleeve starts neutral and gets its own colour (cable or bundle/split settings). "Remove cable" turns it back into single wires. A cable end only joins another cable end, and the cable shows its width.
|
||||
- Pixels and screens: a pixel is a one-grid square that lights at 1, in five colours. Inside a component all pixels show on its box as one screen without gaps (every column and row they sit on becomes one of the screen), so you build your own LCD; see "Screen 4×4" and "Demo: Screen".
|
||||
- Input panel: one part with a column of controls; each row is a switch, button or clock. "Attach input panel" on a placed component docks one to its left with a row per free input, lined up with the pins and named after them. A docked panel moves with its component, gets a row when the component gains an input and loses it when the pin goes; inputs that are wired elsewhere, and rows you remove, are left alone. Inside a component a switch row keeps its position and a button row is 0.
|
||||
- Automatic names: copies continue a series (D0 copied gives D1, D2; A gives B), and so does placing a new input or output after a numbered one. A switch, button, lamp or output pin without a name of its own (or with an automatic one) takes the name of the component pin it gets wired to, made unique (Cin, Cin2); a name typed by hand is kept. On the canvas, a switch, button, clock, lamp or output pin without a name typed by hand shows the name of the component pin it is wired to, dimmed and in italics (or its automatic name, also dimmed); the name follows the wiring as it changes.
|
||||
|
|
|
|||
|
|
@ -217,8 +217,11 @@ function pack() {
|
|||
|
||||
<section v-if="wires.length >= 2">
|
||||
<h3>Cable</h3>
|
||||
<button class="btn w-full" title="Then click where the cable starts and where it ends (Ctrl+G)" @click="ed.startCable(wires.map(w => w.id))">
|
||||
<Icon name="lucide:merge" class="size-4" /> Bundle the {{ wires.length }} wires into a cable…
|
||||
<button class="btn w-full" title="Starts right after the sources, ends right before the targets (Ctrl+G)" @click="ed.bundleSelected(wires.map(w => w.id))">
|
||||
<Icon name="lucide:merge" class="size-4" /> Bundle the {{ wires.length }} wires into a cable
|
||||
</button>
|
||||
<button class="btn btn-ghost mt-1 w-full" title="Click where the cable starts, then where it ends (Ctrl+Shift+G)" @click="ed.startCable(wires.map(w => w.id))">
|
||||
Place the ends yourself…
|
||||
</button>
|
||||
</section>
|
||||
|
||||
|
|
|
|||
|
|
@ -721,7 +721,8 @@ function onKey(e: KeyboardEvent) {
|
|||
ed.duplicate()
|
||||
} else if (mod && key === 'g') {
|
||||
e.preventDefault()
|
||||
ed.startCable()
|
||||
if (e.shiftKey) ed.startCable()
|
||||
else ed.bundleSelected()
|
||||
} else if (mod && key === 'a') {
|
||||
e.preventDefault()
|
||||
ed.selectAll()
|
||||
|
|
|
|||
|
|
@ -36,11 +36,11 @@ const isCable = computed(() => wires.value.length === 1 && cable.value?.id === w
|
|||
<button class="btn" :disabled="!wires.some(w => w.pts)" @click="ed.autoRoute()"><Icon name="lucide:wand-sparkles" class="size-4" /> Auto route</button>
|
||||
</div>
|
||||
<template v-if="wires.length >= 2">
|
||||
<button class="btn btn-primary mt-2 w-full" title="Then click where the cable starts and where it ends (Ctrl+G)" @click="ed.startCable()">
|
||||
<Icon name="lucide:merge" class="size-4" /> Bundle into cable…
|
||||
<button class="btn btn-primary mt-2 w-full" title="Starts right after the sources, ends right before the targets (Ctrl+G)" @click="ed.bundleSelected()">
|
||||
<Icon name="lucide:merge" class="size-4" /> Bundle into cable
|
||||
</button>
|
||||
<button class="btn btn-ghost mt-1 w-full" title="Start just after the sources, end just before the targets" @click="ed.bundleSelected()">
|
||||
Bundle, place it for me
|
||||
<button class="btn btn-ghost mt-1 w-full" title="Click where the cable starts, then where it ends (Ctrl+Shift+G)" @click="ed.startCable()">
|
||||
Place the ends yourself…
|
||||
</button>
|
||||
</template>
|
||||
<button v-if="cable" class="btn mt-2 w-full" title="Take the cable apart again into single wires" @click="ed.removeCable(cable.id)">
|
||||
|
|
|
|||
|
|
@ -54,14 +54,24 @@ export function bundleWires(
|
|||
const mean = (v: number[]) => Math.round(v.reduce((s, x) => s + x, 0) / v.length)
|
||||
const right = Math.max(...ends.map(e => e.a.x))
|
||||
const left = Math.min(...ends.map(e => e.b.x))
|
||||
const bx = right + 2
|
||||
const sx = Math.max(bx + 4, left - 3)
|
||||
// Automatic: the start just after the sources on their middle row, the end
|
||||
// just before the targets on theirs, so the wires keep running the way they
|
||||
// did. Wires that run backwards get a cable that runs backwards. Each end
|
||||
// steps aside when that spot is taken by another part.
|
||||
const taken = def.nodes.map(nd => {
|
||||
const b = shapeOf(nd, get).body
|
||||
return { x: nd.x + b.x, y: nd.y + b.y, w: b.w, h: b.h }
|
||||
})
|
||||
const start = freeSpot(taken, [right + 2, mean(ends.map(e => e.a.y)) - mid], n, 1)
|
||||
taken.push({ x: start[0], y: start[1] - 0.5, w: 1, h: n })
|
||||
const end = freeSpot(taken, [left - 3, mean(ends.map(e => e.b.y)) - mid], n, -1)
|
||||
const [bx, sx] = [start[0], end[0]]
|
||||
const bundle: CircuitNode = at
|
||||
? { id: uid('n'), kind: 'bundle', bits: n, x: Math.round(at.start[0]), y: Math.round(at.start[1]) - mid }
|
||||
: { id: uid('n'), kind: 'bundle', bits: n, x: bx, y: mean(ends.map(e => e.a.y)) - mid }
|
||||
: { id: uid('n'), kind: 'bundle', bits: n, x: bx, y: start[1] }
|
||||
const split: CircuitNode = at
|
||||
? { id: uid('n'), kind: 'split', bits: n, x: Math.round(at.end[0]), y: Math.round(at.end[1]) - mid }
|
||||
: { id: uid('n'), kind: 'split', bits: n, x: sx, y: mean(ends.map(e => e.b.y)) - mid }
|
||||
: { id: uid('n'), kind: 'split', bits: n, x: sx, y: end[1] }
|
||||
def.nodes.push(bundle, split)
|
||||
def.wires = def.wires.filter(w => !ids.has(w.id))
|
||||
ends.forEach((e, i) => {
|
||||
|
|
@ -74,6 +84,23 @@ export function bundleWires(
|
|||
return { ok: true, bundle, split }
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
* -1 away from the targets) or up and down, keeping half a step clear.
|
||||
*/
|
||||
function freeSpot(taken: { x: number; y: number; w: number; h: number }[], at: Pt, n: number, dir: 1 | -1): Pt {
|
||||
const free = (x: number, y: number) =>
|
||||
!taken.some(b => x - 0.5 < b.x + b.w && x + 1.5 > b.x && y - 1 < b.y + b.h && y + n > b.y)
|
||||
for (let step = 0; step <= 4; step++) {
|
||||
for (const dy of [0, 1, -1, 2, -2, 3, -3, 4, -4, 6, -6]) {
|
||||
const x = at[0] + dir * step
|
||||
if (free(x, at[1] + dy)) return [x, at[1] + dy]
|
||||
}
|
||||
}
|
||||
return at
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a cable apart again: its bundle and splits go, and each source is
|
||||
* wired straight to the targets its wire reached. Returns how many wires
|
||||
|
|
|
|||
|
|
@ -412,10 +412,16 @@ export function swapNode(project: Project, def: ComponentDef, nodeId: string, ta
|
|||
return removed
|
||||
}
|
||||
|
||||
/** Removes every wire attached to the given parts. Returns how many. */
|
||||
/**
|
||||
* Removes every wire attached to the given parts. Returns how many. A cable
|
||||
* (bundle to split) is part of the cable, not a connection: it stays, only
|
||||
* the wires into the bundle and out of the split go.
|
||||
*/
|
||||
export function unlinkWires(def: ComponentDef, ids: Set<string>): number {
|
||||
const kind = new Map(def.nodes.map(n => [n.id, n.kind]))
|
||||
const cable = (w: Wire) => kind.get(w.from.node) === 'bundle' && kind.get(w.to.node) === 'split'
|
||||
const before = def.wires.length
|
||||
def.wires = def.wires.filter(w => !ids.has(w.from.node) && !ids.has(w.to.node))
|
||||
def.wires = def.wires.filter(w => cable(w) || (!ids.has(w.from.node) && !ids.has(w.to.node)))
|
||||
return before - def.wires.length
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,3 +53,67 @@ describe('placing a cable', () => {
|
|||
expect([0, 1, 2].map(i => sim.pin(`l${i}`, 'a'))).toEqual([1, 0, 1])
|
||||
})
|
||||
})
|
||||
|
||||
describe('automatic cable placement', () => {
|
||||
const setup = (srcX: number, dstX: number, extra: ComponentDef['nodes'] = []): ComponentDef => ({
|
||||
id: 'c',
|
||||
name: 'C',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
...[0, 1, 2].map(i => ({ id: `s${i}`, kind: 'input' as const, x: srcX, y: i * 2 })),
|
||||
...[0, 1, 2].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: dstX, y: i * 2 })),
|
||||
...extra,
|
||||
],
|
||||
wires: [0, 1, 2].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' } })),
|
||||
})
|
||||
const get = lookup(createProject({ examples: false }))
|
||||
|
||||
it('starts right after the sources and ends right before the targets', () => {
|
||||
const d = setup(0, 30)
|
||||
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
|
||||
if (!res.ok) throw new Error(res.error)
|
||||
expect([res.bundle.x, res.split.x]).toEqual([4, 27])
|
||||
// Centred on the rows the wires come from and go to.
|
||||
expect([res.bundle.y, res.split.y]).toEqual([1, 1])
|
||||
})
|
||||
|
||||
it('runs backwards when the wires do, the end still just before the targets', () => {
|
||||
const d = setup(30, 0)
|
||||
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
|
||||
if (!res.ok) throw new Error(res.error)
|
||||
expect(res.split.x).toBe(-3)
|
||||
expect(res.bundle.x).toBe(34)
|
||||
})
|
||||
|
||||
it('steps aside when another part sits where an end would go', () => {
|
||||
const d = setup(0, 30, [{ id: 'block', kind: 'nand', x: 3, y: 0 }])
|
||||
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
|
||||
if (!res.ok) throw new Error(res.error)
|
||||
const nand = { x0: 3, x1: 7, y0: -0.5, y1: 2.5 }
|
||||
const body = { x0: res.bundle.x, x1: res.bundle.x + 1, y0: res.bundle.y - 0.5, y1: res.bundle.y + 2.5 }
|
||||
const overlap = body.x0 < nand.x1 && body.x1 > nand.x0 && body.y0 < nand.y1 && body.y1 > nand.y0
|
||||
expect(overlap).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('unlinking cable ends', () => {
|
||||
it('keeps the cable and removes only the wires into the bundle or out of the split', async () => {
|
||||
const { unlinkWires } = await import('../app/lib/logic/ops')
|
||||
const d: ComponentDef = {
|
||||
id: 'c',
|
||||
name: 'C',
|
||||
symbol: 'box',
|
||||
nodes: [
|
||||
...[0, 1].map(i => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 2 })),
|
||||
...[0, 1].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: 30, y: i * 2 })),
|
||||
],
|
||||
wires: [0, 1].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' } })),
|
||||
}
|
||||
const res = bundleWires(d, ['w0', 'w1'], lookup(createProject({ examples: false })))
|
||||
if (!res.ok) throw new Error(res.error)
|
||||
expect(unlinkWires(d, new Set([res.bundle.id]))).toBe(2)
|
||||
expect(d.wires.some(w => w.from.node === res.bundle.id && w.to.node === res.split.id)).toBe(true)
|
||||
expect(unlinkWires(d, new Set([res.bundle.id, res.split.id]))).toBe(2)
|
||||
expect(d.wires.map(w => `${w.from.node}>${w.to.node}`)).toEqual([`${res.bundle.id}>${res.split.id}`])
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue