From fa849d7a0585669fd6cb7f3696c07535b6e6494d Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 27 Oct 2026 13:34:16 +0100 Subject: [PATCH] A pick pins for a cable, buttons on pointing, right-click on pins; docked panels move freely - Pointing at a part shows its round buttons, as if it were selected; they stay while the pointer goes to them. - A box around some pins of one part (not over its middle), or Shift + click on pins, picks exactly those pins: a cable plug on them, wire them one by one, invert or unwire them, from buttons beside them or a right-click. - Right-click on a pin offers what can be done right there first: split a component's cable socket into wires (or join), pull a cable out of a cable end, invert an input. - A docked panel moves both ways now: moved up or down off its component's pins it lets go and stays a panel of its own (rows and wires kept); moved sideways it stays docked. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/assets/css/main.css | 8 + app/components/editor/Canvas.vue | 261 ++++++++++++++++++++++--------- app/components/editor/Help.vue | 4 + app/composables/useEditor.ts | 102 +++++++++++- app/lib/logic/move.ts | 8 +- app/lib/logic/panels.ts | 14 ++ 6 files changed, 312 insertions(+), 85 deletions(-) diff --git a/app/assets/css/main.css b/app/assets/css/main.css index b2a34a0..28f80cf 100644 --- a/app/assets/css/main.css +++ b/app/assets/css/main.css @@ -5665,3 +5665,11 @@ button.tile:hover { stroke: #10151c; stroke-width: 1; } + +/* A picked pin: ready for a cable or wires on exactly the picked ones. */ +.picked-pin { + fill: rgb(76 194 255 / 0.18); + stroke: var(--accent); + stroke-width: 2; + pointer-events: none; +} diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index d163ed6..e2eb288 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -445,74 +445,123 @@ function cableOfEnd(n: CircuitNode): CircuitNode | undefined { } /** - * Buttons beside the selection. A component (or panel, or hex display): a - * cable onto its outputs and its inputs, and each connector port split into - * its wires or joined again. A cable end: plug in or out, take the cable - * apart. A cable: take it apart. Several wires: bundle them into a cable. + * Buttons beside a part: a cable onto its outputs and its inputs, and each + * connector port split into its wires or joined again. A cable end: plug in + * or out, take the cable apart. + */ +function nodeButtons(n: CircuitNode): ActionButton[] { + const out: ActionButton[] = [] + const s = shapes.value.get(n.id) + if (!s) return out + const key = (k: string) => `${n.id}|${k}` + // Connector ports: beside the socket, out of the box. + for (const p of [...s.inputs, ...s.outputs]) { + if (!p.conn || n.kind !== 'chip') continue + const split = !!n.splitPorts?.includes(p.key) + out.push({ + key: key(`port:${p.key}`), + x: n.x + p.dx + (p.dx === 0 ? -1.3 : 1.3), + y: n.y + p.dy, + title: split ? `Join the ${p.label} wires back into its socket` : `Split ${p.label} into wires`, + icon: split ? 'lucide:merge' : 'lucide:split', + run: () => ed.toggleSplit(n, p.key), + }) + } + const choices = ed.cableChoices(n) + const mid = (ps: { dy: number }[]) => (Math.min(...ps.map(p => p.dy)) + Math.max(...ps.map(p => p.dy))) / 2 + const outs = s.outputs.filter(p => !p.bus && p.face !== -1) + const ins = s.inputs.filter(p => !p.bus && p.face !== 1) + // Clear of a port's button on the same edge. + const clear = (x: number, y: number) => { + while (out.some(b => b.x === x && Math.abs(b.y - y) < 1.2)) y += 0.5 + return y + } + if (choices.out.length && outs.length) { + const x = n.x + Math.max(...outs.map(p => p.dx)) + 1.3 + out.push({ key: key('cable:out'), x, y: clear(x, n.y + mid(outs)), title: 'A cable plug on these outputs', run: e => onCableButton(n, 'out', e) }) + } + if (choices.in.length && ins.length) { + const x = n.x + Math.min(...ins.map(p => p.dx)) - 1.3 + out.push({ key: key('cable:in'), x, y: clear(x, n.y + mid(ins)), title: 'A cable plug on these inputs', flip: true, run: e => onCableButton(n, 'in', e) }) + } + if (n.kind === 'bundle' || n.kind === 'split') { + // On the cable's side of the end, clear of its cable pin: pull a cable out, plug in or out, take the cable apart. + const x = n.kind === 'bundle' ? n.x + s.body.x + s.body.w + 1 : n.x + s.body.x - 1 + const busY = n.y + (n.kind === 'bundle' ? s.outputs : s.inputs).find(p => p.bus)!.dy + // From its top row down, stepping over the cable pin's row. + let y = n.y + s.body.y + 0.5 + for (const b of endActions(n)) { + if (Math.abs(y - busY) < 0.9) y = busY + 1 + out.push({ ...b, key: key(b.key), x, y }) + y += 1.2 + } + } + return out +} + +/** What can be done at a cable end: pull a cable out, plug in or out, take the cable apart. */ +function endActions(n: CircuitNode): Omit[] { + const ends: Omit[] = [] + const bus = n.kind === 'bundle' ? { pin: 'q', dir: 'out' as const } : { pin: 'a', dir: 'in' as const } + if (pullable({ node: n.id, ...bus }) && !linked.value[bus.dir].has(`${n.id}:${bus.pin}`)) { + ends.push({ + key: 'pull', + title: 'Pull the cable out: click where its other end goes (or drag it out of its pin)', + icon: 'lucide:cable', + run: () => (ed.linking.value = { queue: [{ node: n.id, ...bus, label: '' }], at: 0 }), + }) + } + if (n.plug) ends.push({ key: 'unplug', title: 'Unplug: it steps aside and its wires show again', icon: 'lucide:unplug', run: () => ed.unplugEnd(n) }) + else if (ed.plugTarget(n)) ends.push({ key: 'plug', title: 'Plug straight onto the pins its wires run to', icon: 'lucide:plug', run: () => ed.plugEnd(n) }) + const cable = cableOfEnd(n) + if (cable) ends.push({ key: 'apart', title: 'Split the cable into single wires', icon: 'lucide:split', run: () => ed.removeCable(cable.id) }) + return ends +} + +/** Buttons beside picked pins: a cable plug on exactly them, or wire them one by one. */ +function pickedButtons(): ActionButton[] { + const sel = ed.pinSel.value + const n = sel && nodeMap.value.get(sel.node) + const s = n && shapes.value.get(n.id) + if (!sel || !n || !s) return [] + const pins = (sel.dir === 'out' ? s.outputs : s.inputs).filter(p => sel.pins.includes(p.key)) + if (!pins.length) return [] + const side = sel.dir === 'out' ? 1 : -1 + const x = n.x + (side > 0 ? Math.max(...pins.map(p => p.dx)) : Math.min(...pins.map(p => p.dx))) + side * 1.3 + const mid = n.y + (Math.min(...pins.map(p => p.dy)) + Math.max(...pins.map(p => p.dy))) / 2 + const count = `${pins.length} ${sel.dir === 'out' ? 'output' : 'input'}${pins.length === 1 ? '' : 's'}` + const out: ActionButton[] = [] + if (pins.length >= 2) out.push({ key: 'picked|cable', x, y: mid - 0.6, title: `A cable plug on these ${count}`, flip: side < 0, run: () => ed.cablePicked() }) + out.push({ key: 'picked|link', x, y: out.length ? mid + 0.6 : mid, title: `Wire these ${count} one by one: click where each goes`, icon: 'lucide:cable', run: () => ed.linkPicked() }) + return out +} + +// ── A part pointed at shows its buttons too, as if it were selected ── +const hoverNode = ref(null) +let hoverTimer: ReturnType | undefined +/** Pointing at a part (after a moment), or at one of its buttons, keeps them; leaving lets them go a moment later. */ +function trackHover(target: Element | null) { + if (target?.closest('[data-action]')) return clearTimeout(hoverTimer) + const id = target?.closest('[data-node]')?.getAttribute('data-node') ?? null + if (id === hoverNode.value) return clearTimeout(hoverTimer) + clearTimeout(hoverTimer) + hoverTimer = setTimeout(() => (hoverNode.value = id), id ? 150 : 600) +} + +/** + * Buttons beside the selection, and beside a part pointed at. Picked pins: + * a cable on them, or wire them one by one. A cable: take it apart. Several + * wires: bundle them into a cable. */ const actionButtons = computed(() => { if (drag.value || ed.linking.value || ed.placing.value || ed.cabling.value) return [] - const out: ActionButton[] = [] + const out: ActionButton[] = [...pickedButtons()] const sel = ed.selection.value - const n = sel.length === 1 && !ed.wireSel.value.length ? nodeMap.value.get(sel[0]!) : undefined - if (n) { - const s = shapes.value.get(n.id)! - // Connector ports: beside the socket, out of the box. - for (const p of [...s.inputs, ...s.outputs]) { - if (!p.conn || n.kind !== 'chip') continue - const split = !!n.splitPorts?.includes(p.key) - out.push({ - key: `port:${p.key}`, - x: n.x + p.dx + (p.dx === 0 ? -1.3 : 1.3), - y: n.y + p.dy, - title: split ? `Join the ${p.label} wires back into its socket` : `Split ${p.label} into wires`, - icon: split ? 'lucide:merge' : 'lucide:split', - run: () => ed.toggleSplit(n, p.key), - }) - } - const choices = ed.cableChoices(n) - const mid = (ps: { dy: number }[]) => (Math.min(...ps.map(p => p.dy)) + Math.max(...ps.map(p => p.dy))) / 2 - const outs = s.outputs.filter(p => !p.bus && p.face !== -1) - const ins = s.inputs.filter(p => !p.bus && p.face !== 1) - // Clear of a port's button on the same edge. - const clear = (x: number, y: number) => { - while (out.some(b => b.x === x && Math.abs(b.y - y) < 1.2)) y += 0.5 - return y - } - if (choices.out.length && outs.length) { - const x = n.x + Math.max(...outs.map(p => p.dx)) + 1.3 - out.push({ key: 'cable:out', x, y: clear(x, n.y + mid(outs)), title: 'A cable plug on these outputs', run: e => onCableButton('out', e) }) - } - if (choices.in.length && ins.length) { - const x = n.x + Math.min(...ins.map(p => p.dx)) - 1.3 - out.push({ key: 'cable:in', x, y: clear(x, n.y + mid(ins)), title: 'A cable plug on these inputs', flip: true, run: e => onCableButton('in', e) }) - } - if (n.kind === 'bundle' || n.kind === 'split') { - // On the cable's side of the end, clear of its cable pin: pull a cable out, plug in or out, take the cable apart. - const ends: Omit[] = [] - const bus = n.kind === 'bundle' ? { pin: 'q', dir: 'out' as const } : { pin: 'a', dir: 'in' as const } - if (pullable({ node: n.id, ...bus }) && !linked.value[bus.dir].has(`${n.id}:${bus.pin}`)) { - ends.push({ - key: 'pull', - title: 'Pull the cable out: click where its other end goes (or drag it out of its pin)', - icon: 'lucide:cable', - run: () => (ed.linking.value = { queue: [{ node: n.id, ...bus, label: '' }], at: 0 }), - }) - } - if (n.plug) ends.push({ key: 'unplug', title: 'Unplug: it steps aside and its wires show again', icon: 'lucide:unplug', run: () => ed.unplugEnd(n) }) - else if (ed.plugTarget(n)) ends.push({ key: 'plug', title: 'Plug straight onto the pins its wires run to', icon: 'lucide:plug', run: () => ed.plugEnd(n) }) - const cable = cableOfEnd(n) - if (cable) ends.push({ key: 'apart', title: 'Split the cable into single wires', icon: 'lucide:split', run: () => ed.removeCable(cable.id) }) - const x = n.kind === 'bundle' ? n.x + s.body.x + s.body.w + 1 : n.x + s.body.x - 1 - const busY = n.y + (n.kind === 'bundle' ? s.outputs : s.inputs).find(p => p.bus)!.dy - // From its top row down, stepping over the cable pin's row. - let y = n.y + s.body.y + 0.5 - for (const b of ends) { - if (Math.abs(y - busY) < 0.9) y = busY + 1 - out.push({ ...b, x, y }) - y += 1.2 - } - } + const picked = sel.length === 1 && !ed.wireSel.value.length ? sel[0] : undefined + for (const id of new Set([picked, hoverNode.value])) { + const n = id ? nodeMap.value.get(id) : undefined + // With pins of it picked, the buttons for those pins stand in for its own. + if (n && n.id !== ed.pinSel.value?.node) out.push(...nodeButtons(n)) } // Wires: a cable can be taken apart, several single wires bundled. const wires = sel.length ? [] : routes.value.filter(r => wireSelected.value.has(r.wire.id)) @@ -532,9 +581,7 @@ const actionButtons = computed(() => { }) /** A cable button: with one way to do it, it is done; with several, they are offered. */ -function onCableButton(side: 'out' | 'in', e: PointerEvent) { - const n = nodeMap.value.get(ed.selection.value[0]!) - if (!n) return +function onCableButton(n: CircuitNode, side: 'out' | 'in', e: PointerEvent) { const groups = ed.cableChoices(n)[side] const run = (pins: string[]) => (side === 'out' ? ed.cableOut(n.id, pins) : ed.cableIn(n.id, pins)) if (groups.length === 1) return run(groups[0]!.pins) @@ -699,6 +746,11 @@ function onPointerDown(e: PointerEvent) { const n = nodeMap.value.get(pinEl.getAttribute('data-node')!) if (n && ed.canInvert(n, pinEl.getAttribute('data-pin')!)) return ed.toggleInvert(n, pinEl.getAttribute('data-pin')!) } + if (pinEl && e.shiftKey) { + // Shift + click on a pin: in or out of the picked pins. + ed.pickPin(pinEl.getAttribute('data-node')!, pinEl.getAttribute('data-dir') as 'in' | 'out', pinEl.getAttribute('data-pin')!) + return + } if (pinEl) { drag.value = { kind: 'wire', @@ -811,7 +863,7 @@ function onPointerMove(e: PointerEvent) { cursor.value = w altDown.value = e.altKey const d = drag.value - if (!d) return + if (!d) return trackHover(e.target as Element) switch (d.kind) { case 'pan': ed.views[def.value.id] = { ...view.value, x: d.vx + e.clientX - d.sx, y: d.vy + e.clientY - d.sy } @@ -829,7 +881,7 @@ function onPointerMove(e: PointerEvent) { } if (d.still && d.from) { // Dropped onto pins it can join, it connects there (Alt: just place it). - const at = d.from.map(p => ({ ...p, x: p.x + dx, y: d.orig.lockY.has(p.node) ? p.y : p.y + dy })) + const at = d.from.map(p => ({ ...p, x: p.x + dx, y: p.y + dy })) d.snap = e.altKey ? null : dropLinks(at, d.still, d.grabbed === 1 ? 1 : 0) } if (dx !== d.at[0] || dy !== d.at[1]) { @@ -858,6 +910,20 @@ function onPointerMove(e: PointerEvent) { // Dragged right to left, or across wires without touching any part: every wire the box // crosses. Otherwise only wires lying fully inside, so a box over some parts doesn't // catch (and on Delete remove) unrelated wires passing through. + // Around pins of one part, all inputs or all outputs, and not over its middle: those pins. + const pinsIn = allPins.value.filter(p => !p.bus && p.x > x0 && p.x < x1 && p.y > y0 && p.y < y1) + const owner = pinsIn[0]?.node + const host = owner ? nodeMap.value.get(owner) : undefined + if (host && pinsIn.every(p => p.node === owner && p.dir === pinsIn[0]!.dir) && nodes.every(id => id === owner)) { + const b = shapes.value.get(owner!)!.body + const cx = host.x + b.x + b.w / 2 + const cy = host.y + b.y + b.h / 2 + if (!(cx > x0 && cx < x1 && cy > y0 && cy < y1)) { + ed.pickPins(owner!, pinsIn[0]!.dir, pinsIn.map(p => p.pin)) + break + } + } + if (ed.pinSel.value) ed.pinSel.value = null const box = { x0, y0, x1, y1 } const crossing = w[0] < d.start[0] || !nodes.length const wires = routes.value @@ -1165,12 +1231,12 @@ const ARROWS: Record = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], Arr function nudge(dx: number, dy: number) { const ids = ed.withFollowers(ed.selection.value) if (!ids.size && !ed.wireSel.value.length) return - ed.unplugMoved(ids) // A wire moved on its own keeps the shape it has. for (const r of routes.value) { if (wireSelected.value.has(r.wire.id) && !r.wire.pts && !ids.has(r.wire.from.node) && !ids.has(r.wire.to.node)) r.wire.pts = r.pts.slice(1, -1) } moveTo(moveStart(def.value, ids, ed.wireSel.value), dx, dy) + ed.unplugMoved(ids) ed.commit(false) } @@ -1252,7 +1318,7 @@ function onKey(e: KeyboardEvent) { } if (ed.linking.value) return ed.stopLinking() // Nothing to cancel: Esc pauses, like in a game. - if (!ed.placing.value && !ed.selection.value.length && !ed.wireSel.value.length) return ed.pause() + if (!ed.placing.value && !ed.selection.value.length && !ed.wireSel.value.length && !ed.pinSel.value) return ed.pause() ed.placing.value = null ed.clearSelection() } else if (ARROWS[e.key]) { @@ -1534,8 +1600,10 @@ function openMenuAt(target: Element, at: Pt, x: number, y: number) { const nodeId = target.closest('[data-node]')?.getAttribute('data-node') const wireId = target.closest('[data-wire]')?.getAttribute('data-wire') const junction = target.closest('[data-junction]')?.getAttribute('data-junction') + // On a picked pin the picked pins stay picked (selecting the part would put them down). + const onPicked = !!ed.pinSel.value && ed.pinSel.value.node === nodeId && ed.pinSel.value.pins.includes(target.closest('[data-pin]')?.getAttribute('data-pin') ?? '') if (nodeId && nodeMap.value.has(nodeId)) { - if (!selected.value.has(nodeId)) { + if (!selected.value.has(nodeId) && !onPicked) { ed.selection.value = [nodeId] ed.wireSel.value = [] } @@ -1551,12 +1619,42 @@ function openMenuAt(target: Element, at: Pt, x: number, y: number) { const onThing = !!(nodeId || wireId || junction) const entries = build(onThing ? { kind: 'selection' } : { kind: 'canvas', at }, canvasOps) // On an input pin: its bubble first (the same as Alt + click, which some desktops take for themselves). - const pin = target.closest('[data-pin][data-dir="in"]')?.getAttribute('data-pin') + const pinEl = target.closest('[data-pin]') + const pin = pinEl?.getAttribute('data-pin') + const dir = pinEl?.getAttribute('data-dir') as 'in' | 'out' | undefined const n = nodeId ? nodeMap.value.get(nodeId) : undefined - if (pin && n && ed.canInvert(n, pin)) { - const on = n.invert?.includes(pin) - entries.unshift({ label: on ? 'Take the bubble off this input' : 'Invert this input', icon: 'lucide:circle-dot', keys: 'Alt+click', run: () => ed.toggleInvert(n, pin) }, 'sep') + const here: MenuEntry[] = [] + if (pin && dir && n) { + const shape = shapes.value.get(n.id)! + const p = (dir === 'in' ? shape.inputs : shape.outputs).find(x => x.key === pin) + // A cable socket on a component: split it into its wires right here, or join them. + if (p?.conn && n.kind === 'chip') { + const split = !!n.splitPorts?.includes(pin) + here.push({ label: split ? `Join the ${p.label} wires` : `Split ${p.label} into wires here`, icon: split ? 'lucide:merge' : 'lucide:split', run: () => ed.toggleSplit(n, pin) }) + } + // A cable end's cable pin: pull the cable out right here (plugging and taking apart are in the end's own menu below). + if (p?.bus && (n.kind === 'bundle' || n.kind === 'split')) { + const pull = endActions(n).find(x => x.key === 'pull') + if (pull) here.push({ label: 'Pull the cable out', icon: pull.icon, run: () => pull.run(new PointerEvent('pointerdown')) }) + } + if (dir === 'in' && ed.canInvert(n, pin)) { + const on = n.invert?.includes(pin) + here.push({ label: on ? 'Take the bubble off this input' : 'Invert this input', icon: 'lucide:circle-dot', keys: 'Alt+click', run: () => ed.toggleInvert(n, pin) }) + } } + // Picked pins (right-clicked on one of them): what can be done with all of them. + const sel = ed.pinSel.value + if (sel && n && sel.node === n.id && (!pin || sel.pins.includes(pin))) { + const count = `${sel.pins.length} ${sel.dir === 'out' ? 'output' : 'input'}${sel.pins.length === 1 ? '' : 's'}` + here.unshift( + { head: `${count} picked` }, + ...(sel.pins.length >= 2 ? [{ label: 'A cable plug on them', icon: sel.dir === 'out' ? 'lucide:merge' : 'lucide:split', run: () => ed.cablePicked() }] : []), + { label: 'Wire them one by one', icon: 'lucide:cable', run: () => ed.linkPicked() }, + ...(sel.dir === 'in' ? [{ label: 'Invert them', icon: 'lucide:circle-dot', run: () => ed.invertPicked() }] : []), + { label: 'Remove their wires', icon: 'lucide:unlink', danger: true, run: () => ed.unwirePicked() }, + ) + } + if (here.length) entries.unshift(...here, 'sep') menu.value = entries.length ? { x: x + 2, y: y + 2, entries } : null } @@ -1613,6 +1711,14 @@ const latchMarks = computed(() => }), ) +/** The picked pins, where they are. */ +const pickedPins = computed(() => { + const sel = ed.pinSel.value + if (!sel) return [] + const keep = new Set(sel.pins) + return allPins.value.filter(p => p.node === sel.node && p.dir === sel.dir && keep.has(p.pin)) +}) + /** Pins a dragged part will connect to when dropped, shown as green rings. */ const dropRings = computed(() => { const g = ghostLanding.value @@ -1681,7 +1787,7 @@ defineExpose({ fit, zoomAt }) @pointermove="onPointerMove" @pointerup="onPointerUp" @pointercancel="onPointerUp" - @pointerleave="cursor = null" + @pointerleave="(cursor = null), trackHover(null)" @wheel.prevent="onWheel" @contextmenu.prevent @dragover.prevent="onDragOver" @@ -1847,6 +1953,7 @@ defineExpose({ fit, zoomAt }) + (null) + // Picking parts or wires puts picked pins down, and so does another circuit. + watch([() => selection.value.length, () => wireSel.value.length], ([a, b]) => { + if (a || b) pinSel.value = null + }) + watch(() => def.value.id, () => (pinSel.value = null)) + + /** Pins of a part in their order on it, top to bottom. */ + function pinOrder(node: string, dir: 'in' | 'out', pins: string[]): string[] { + const n = def.value.nodes.find(x => x.id === node) + if (!n) return [] + const all = (dir === 'in' ? shapeOf(n, getDef.value).inputs : shapeOf(n, getDef.value).outputs).map(p => p.key) + return all.filter(k => pins.includes(k)) + } + + function pickPins(node: string, dir: 'in' | 'out', pins: string[]) { + selection.value = [] + wireSel.value = [] + const list = pinOrder(node, dir, pins) + pinSel.value = list.length ? { node, dir, pins: list } : null + } + + /** Shift + click on a pin: in or out of the picked pins (pins of another part or side start a new pick). */ + function pickPin(node: string, dir: 'in' | 'out', pin: string) { + const s = pinSel.value + const same = s && s.node === node && s.dir === dir + pickPins(node, dir, same ? (s.pins.includes(pin) ? s.pins.filter(p => p !== pin) : [...s.pins, pin]) : [pin]) + } + + /** A cable plug on exactly the picked pins. */ + function cablePicked() { + const s = pinSel.value + if (!s) return + pinSel.value = null + if (s.dir === 'out') cableOut(s.node, s.pins) + else cableIn(s.node, s.pins) + } + + /** Wire the picked pins one after another: click where each one goes. */ + function linkPicked() { + const s = pinSel.value + const n = s && def.value.nodes.find(x => x.id === s.node) + if (!s || !n) return + const shape = shapeOf(n, getDef.value) + const label = (k: string) => (s.dir === 'in' ? shape.inputs : shape.outputs).find(p => p.key === k)?.label ?? '' + pinSel.value = null + placing.value = null + linking.value = { queue: s.pins.map(pin => ({ node: s.node, pin, dir: s.dir, label: label(pin) })), at: 0 } + } + + /** Bubbles on every picked input (or off, when they all have one). */ + function invertPicked() { + const s = pinSel.value + const n = s && s.dir === 'in' ? def.value.nodes.find(x => x.id === s.node) : undefined + if (!s || !n) return + const pins = s.pins.filter(p => canInvert(n, p)) + if (!pins.length) return notify('These inputs can not take a bubble', 'error') + const all = pins.every(p => n.invert?.includes(p)) + const set = new Set(n.invert ?? []) + for (const p of pins) { + if (all) set.delete(p) + else set.add(p) + } + if (set.size) n.invert = [...set] + else delete n.invert + commit() + } + + /** Removes the wires on the picked pins. */ + function unwirePicked() { + const s = pinSel.value + if (!s) return + const pins = new Set(s.pins) + const before = def.value.wires.length + def.value.wires = def.value.wires.filter(w => !(s.dir === 'out' ? w.from.node === s.node && pins.has(w.from.pin) : w.to.node === s.node && pins.has(w.to.pin))) + const n = before - def.value.wires.length + commit() + notify(n ? `Removed ${n} wire${n === 1 ? '' : 's'}` : 'No wires on these pins') } // ── Saving ──────────────────────────────────────────────────────── @@ -1229,11 +1315,16 @@ function create() { commit() } - /** Ends dragged away from the part they are plugged into come loose. */ + /** + * Parts moved away from the part they hang on come loose: cable ends + * unplug, latched parts let go, and a docked panel moved up or down off its + * component's pins becomes a panel of its own (rows and wires kept). + */ function unplugMoved(ids: Set) { for (const n of def.value.nodes) { if (n.plug && ids.has(n.id) && !ids.has(n.plug)) delete n.plug if (n.latch && ids.has(n.id) && !ids.has(n.latch)) delete n.latch + if (n.attach && ids.has(n.id) && !ids.has(n.attach) && offItsPins(def.value, n, getDef.value)) detachPanel(n) } } @@ -2620,6 +2711,13 @@ function create() { wireSel, selectedWires, clearSelection, + pinSel, + pickPins, + pickPin, + cablePicked, + linkPicked, + invertPicked, + unwirePicked, placing, nav, views, diff --git a/app/lib/logic/move.ts b/app/lib/logic/move.ts index 118495c..3b62b05 100644 --- a/app/lib/logic/move.ts +++ b/app/lib/logic/move.ts @@ -12,8 +12,6 @@ import type { ComponentDef } from './types' export interface MoveStart { /** Parts that move (followers included), where they were. */ nodes: Map - /** Panels docked to a part that stays: they only slide sideways. */ - lockY: Set /** Corners of every wire that has hand-placed ones. */ pts: Map /** Wires picked along with the parts. */ @@ -22,15 +20,13 @@ export interface MoveStart { export function moveStart(def: ComponentDef, ids: Set, wires: Iterable = []): MoveStart { const nodes = new Map() - const lockY = new Set() for (const n of def.nodes) { if (!ids.has(n.id)) continue nodes.set(n.id, [n.x, n.y]) - if (n.attach && !ids.has(n.attach)) lockY.add(n.id) } const pts = new Map() for (const w of def.wires) if (w.pts) pts.set(w.id, w.pts.map(p => [p[0], p[1]] as Pt)) - return { nodes, lockY, pts, wires: new Set(wires) } + return { nodes, pts, wires: new Set(wires) } } /** @@ -45,7 +41,7 @@ export function moveParts(def: ComponentDef, start: MoveStart, dx: number, dy: n for (const n of def.nodes) { const at = start.nodes.get(n.id) if (!at) continue - const step: Pt = [dx, start.lockY.has(n.id) ? 0 : dy] + const step: Pt = [dx, dy] n.x = at[0] + step[0] n.y = at[1] + step[1] if (step[0] || step[1]) by.set(n.id, step) diff --git a/app/lib/logic/panels.ts b/app/lib/logic/panels.ts index 7050b4a..03210ab 100644 --- a/app/lib/logic/panels.ts +++ b/app/lib/logic/panels.ts @@ -67,6 +67,20 @@ function layout(panel: CircuitNode, pins: ChipInput[]) { return top } +/** + * A docked panel moved up or down off its component's pins: its first row no + * longer sits level with that row's pin. Moved only sideways, it still does. + */ +export function offItsPins(def: ComponentDef, panel: CircuitNode, get: DefLookup): boolean { + const chip = panel.attach ? def.nodes.find(n => n.id === panel.attach) : undefined + const pins = chip && chipInputs(chip, get, sideOf(panel)) + if (!chip || !pins) return false + const order = new Map(pins.map((p, i) => [p.key, i])) + const first = (panel.rows ?? []).filter(r => order.has(r.id)).sort((a, b) => order.get(a.id)! - order.get(b.id)!)[0] + const pin = first && pins.find(p => p.key === first.id) + return !!pin && panel.y !== chip.y + pin.dy +} + /** * 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