From 29c32026d71de4bfd5b6c906e07562cf05959821 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 21 Oct 2026 10:39:54 +0200 Subject: [PATCH] A cables you place yourself, drawn as coloured ribbons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Select wires (or a box of parts and wires), "Bundle into cable…" or Ctrl+G, then click where the cable starts and where it ends, with a preview; "place it for me" keeps the automatic placement. - Up to 16 wires, a cable is a ribbon: one thin line per wire inside the sleeve, in that wire's colour, lit on its own. - The sleeve starts neutral (no longer takes the first wire's colour) and has its own colour in the cable and bundle/split settings. Wide cables are clickable across their width; the width mark sits above the ribbon. Co-Authored-By: Claude Opus 5.5 (1M context) --- README.md | 4 +- app/assets/css/main.css | 40 +++++++++++++++ app/components/editor/App.vue | 6 ++- app/components/editor/BulkSettings.vue | 7 +++ app/components/editor/Canvas.vue | 70 +++++++++++++++++++++++--- app/components/editor/NodeSettings.vue | 19 ++++++- app/components/editor/WireSettings.vue | 23 +++++---- app/composables/useEditor.ts | 39 +++++++++++++- app/lib/logic/cables.ts | 56 +++++++++++++++++---- tests/cables-ribbon.test.ts | 55 ++++++++++++++++++++ tests/cables.test.ts | 5 +- 11 files changed, 291 insertions(+), 33 deletions(-) create mode 100644 tests/cables-ribbon.test.ts diff --git a/README.md b/README.md index 42bdef8..da54856 100644 --- a/README.md +++ b/README.md @@ -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/`. 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 thick cable, a split fans it out again in the same order; one bundle can feed several splits. Select some wires and press "Bundle into cable" to have both ends placed for you (the cable takes the wires' colour); "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 (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. - 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. @@ -49,7 +49,7 @@ All in [app/lib/logic/](app/lib/logic/), plain TypeScript with no Vue dependency - `examples.ts` builds the example library with a small builder; `ops.ts` holds project edits (package selection, copy/paste, prune dangling wires, recursion check). - `routing.ts` lays out all wires of a circuit (free lanes, detours, separation, crossings, and the tidy layout). - `progress.ts` holds the campaign rules; `puzzles.ts` makes, checks and scores puzzles and generates the weekly one (shared with the server, [server/api/puzzles/](server/api/puzzles/), storage in `data/puzzles`). -- `cables.ts` bundles wires into cables and takes them apart again. +- `cables.ts` bundles wires into cables (ends where you click, or next to sources and targets), takes them apart again, and lays out the ribbon lines (`offsetPath`, `ribbon`). - `panels.ts` docks input panels to components and keeps them in step; `labels.ts` does series names and names taken from pins. - `wires.ts` edits wire routes (segment and corner drags, splits, branches); `project.ts` validates project data for both the browser and the server; `storage.ts` is the browser's project library. diff --git a/app/assets/css/main.css b/app/assets/css/main.css index 2bf47d4..a139c64 100644 --- a/app/assets/css/main.css +++ b/app/assets/css/main.css @@ -2212,3 +2212,43 @@ kbd { padding: 0; border-bottom: 2px solid transparent; } + +/* Ribbon cable: sleeve, dark core, one thin line per wire in its own colour. */ +.cable-sleeve { + fill: none; + stroke: var(--w-off); + stroke-linejoin: round; + stroke-linecap: butt; +} + +.wire.on .cable-sleeve { + stroke: color-mix(in srgb, var(--w-on) 55%, var(--w-off)); +} + +.cable-core { + fill: none; + stroke: #10151c; + stroke-linejoin: round; + stroke-linecap: butt; +} + +.lane { + --w-on: var(--on); + --w-off: #3b4554; + fill: none; + stroke: var(--w-off); + stroke-width: 1.3; + stroke-linejoin: round; + pointer-events: none; +} + +.lane.on { + stroke: var(--w-on); +} + +.lane.c-red { --w-on: #ff5c5c; --w-off: #6b3337; } +.lane.c-orange { --w-on: #ff9f43; --w-off: #6b4a2b; } +.lane.c-yellow { --w-on: #ffd93d; --w-off: #625a2e; } +.lane.c-blue { --w-on: #4dabff; --w-off: #2b4a6b; } +.lane.c-purple { --w-on: #b388ff; --w-off: #463768; } +.lane.c-pink { --w-on: #ff7ac6; --w-off: #673657; } diff --git a/app/components/editor/App.vue b/app/components/editor/App.vue index 7cf411b..82bcdc8 100644 --- a/app/components/editor/App.vue +++ b/app/components/editor/App.vue @@ -32,7 +32,11 @@ onMounted(() => {
{{ ed.notice.value.text }}
-
+
+ Cable with {{ ed.cabling.value.wires.length }} wires: + {{ ed.cabling.value.start ? 'click where it ends (its split).' : 'click where it starts (its bundle).' }} Esc cancels. +
+
Click on the canvas to place. Shift places several, Esc stops.
diff --git a/app/components/editor/BulkSettings.vue b/app/components/editor/BulkSettings.vue index c6303d4..ed19be5 100644 --- a/app/components/editor/BulkSettings.vue +++ b/app/components/editor/BulkSettings.vue @@ -215,6 +215,13 @@ function pack() {
+
+

Cable

+ +
+

Arrange

diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index 44264b7..386b380 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -5,6 +5,7 @@ import type { CircuitNode, PanelRow, PinRef, Wire } from '~/lib/logic/types' import { branchCorners, branchTo, moveCorner, moveSegment, segmentDrag, splitDrag } from '~/lib/logic/wires' import { circuitRoutes, findCrossings } from '~/lib/logic/routing' import { seriesLinks } from '~/lib/logic/bulk' +import { LANE, ribbon } from '~/lib/logic/cables' import { wirePath } from '~/lib/draw' import { pinName } from '~/lib/logic/labels' @@ -33,8 +34,15 @@ interface Route { bits?: number /** Where the cable's width mark goes. */ mark?: Pt + /** A cable drawn as a ribbon: one line per wire, coloured like that wire. */ + lanes?: { i: number; d: string; color?: string }[] + /** Width of the cable's sleeve in pixels. */ + sleeve?: number } +/** Cables with up to this many wires show each wire as its own line. */ +const RIBBON_MAX = 16 + const toPath = (pts: Pt[]) => pts.map(([x, y], i) => `${i ? 'L' : 'M'}${x * G} ${y * G}`).join('') const routes = computed(() => { @@ -52,6 +60,13 @@ const routes = computed(() => { const route: Route = { wire, pts: r.grid, draw: r.draw, d: wirePath(r.draw, hops.get(k), style), src: r.net } if (from?.kind === 'bundle') { route.bits = cableBits(from) + if (route.bits <= RIBBON_MAX) { + // Each wire keeps the colour of the wire that runs into the bundle. + const feed = new Map(def.value.wires.filter(w => w.to.node === from.id).map(w => [w.to.pin, w.color])) + route.d = toPath(r.draw) + route.lanes = ribbon(r.draw, route.bits).map((lane, i) => ({ i, d: toPath(lane), color: feed.get(`b${i}`) })) + route.sleeve = route.bits * LANE * G + 3 + } // The width mark sits on the middle of the longest straight run. let best = 0 for (let i = 0; i < r.draw.length - 1; i++) { @@ -306,6 +321,11 @@ function onPointerDown(e: PointerEvent) { return ed.stopLinking() } + if (ed.cabling.value) { + ed.placeCable([Math.round(w[0]), Math.round(w[1])]) + return + } + const placing = ed.placing.value if (placing) { if (placing.chip && !ed.canPlace(placing.chip)) { @@ -692,6 +712,9 @@ function onKey(e: KeyboardEvent) { else if (mod && key === 'd') { e.preventDefault() ed.duplicate() + } else if (mod && key === 'g') { + e.preventDefault() + ed.startCable() } else if (mod && key === 'a') { e.preventDefault() ed.selectAll() @@ -699,6 +722,10 @@ function onKey(e: KeyboardEvent) { e.preventDefault() ed.deleteSelected() } else if (e.key === 'Escape') { + if (ed.cabling.value) { + ed.cabling.value = null + return + } if (ed.linking.value) return ed.stopLinking() ed.placing.value = null ed.clearSelection() @@ -810,11 +837,23 @@ const handles = computed(() => const gridSize = computed(() => G * view.value.zoom) const flow = computed(() => ed.project.value.settings.flow) +/** Preview while placing a cable: its start (where you clicked or the mouse) and its end. */ +const cableGhost = computed(() => { + const c = ed.cabling.value + if (!c || !cursor.value) return null + const at: Pt = [Math.round(cursor.value[0]), Math.round(cursor.value[1])] + const bits = c.wires.length + const mid = Math.floor((bits - 1) / 2) + const end = (kind: 'bundle' | 'split', p: Pt): CircuitNode => ({ id: `ghost-${kind}`, kind, bits, x: p[0], y: p[1] - mid }) + if (!c.start) return { nodes: [end('bundle', at)], line: '' } + return { nodes: [end('bundle', c.start), end('split', at)], line: toPath(routeWire(c.start, at)) } +}) + const cursorStyle = computed(() => { if (drag.value?.kind === 'pan') return 'grabbing' if (spaceDown.value) return 'grab' if (ed.placing.value) return 'copy' - if (ed.linking.value) return 'crosshair' + if (ed.linking.value || ed.cabling.value) return 'crosshair' return 'default' }) @@ -859,23 +898,38 @@ defineExpose({ fit, zoomAt }) { on: srcVal(r.src) === 1, selected: wireSelected.has(r.wire.id), hover: hoverSrc === r.src, cable: !!r.bits }, ]" > - - - + + + - - {{ r.bits }} + + {{ r.bits }} @@ -916,6 +970,10 @@ defineExpose({ fit, zoomAt }) {{ b.n }} + diff --git a/app/components/editor/NodeSettings.vue b/app/components/editor/NodeSettings.vue index 4de9ba9..7f641ed 100644 --- a/app/components/editor/NodeSettings.vue +++ b/app/components/editor/NodeSettings.vue @@ -6,7 +6,7 @@ import { cableBits, chipPins, MAX_BITS, rowLabel } from '~/lib/logic/geometry' import { SINK_KINDS, SOURCE_KINDS } from '~/lib/logic/ops' import { KIND_NAMES } from '~/lib/logic/progress' import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wide' -import { LAMP_COLORS, type CircuitNode, type NodeKind, type PanelControl, type PanelRow } from '~/lib/logic/types' +import { LAMP_COLORS, WIRE_COLORS, type CircuitNode, type NodeKind, type PanelControl, type PanelRow } from '~/lib/logic/types' const props = defineProps<{ node: CircuitNode }>() const ed = useEditor() @@ -146,6 +146,8 @@ const cableBundle = computed(() => { return feed ? cableOf(ed.def.value, feed) : undefined }) +const sleeveColor = computed(() => (cableBundle.value ? ed.def.value.wires.find(w => w.from.node === cableBundle.value!.id)?.color : undefined)) + 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)) @@ -317,6 +319,21 @@ function setRowHz(row: PanelRow, e: Event) { Wires in the cable +
+ Sleeve colour (the wires inside keep theirs) +
+
+
+

Drag the bundle and the split to decide where the cable starts and ends.

diff --git a/app/components/editor/WireSettings.vue b/app/components/editor/WireSettings.vue index a0daf92..113ec7c 100644 --- a/app/components/editor/WireSettings.vue +++ b/app/components/editor/WireSettings.vue @@ -9,13 +9,16 @@ const wireColor = computed(() => { return colors.size === 1 ? [...colors][0] : null }) const cable = computed(() => (wires.value.length === 1 ? cableOf(ed.def.value, wires.value[0]!) : undefined)) +/** The selected wire is the cable itself (it leaves a bundle). */ +const isCable = computed(() => wires.value.length === 1 && cable.value?.id === wires.value[0]!.from.node)