diff --git a/app/components/editor/FaceDialog.vue b/app/components/editor/FaceDialog.vue index 2fe152b..b701983 100644 --- a/app/components/editor/FaceDialog.vue +++ b/app/components/editor/FaceDialog.vue @@ -82,10 +82,13 @@ const nameBox = computed(() => { return { x: c.x - w / 2, y: c.y - 0.55, w, h: 1.1 } }) -/** Pins on both edges, to drag up and down. */ +/** Pins on both edges, cable sockets too, to drag up and down. */ const pins = computed(() => [ - ...shape.value.inputs.filter(p => !p.bus).map(p => ({ ...p, side: 'in' as const })), - ...shape.value.outputs.filter(p => !p.bus).map(p => ({ ...p, side: 'out' as const })), + ...shape.value.inputs.filter(p => p.dx === 0).map(p => ({ ...p, side: 'in' as const })), + ...shape.value.outputs.filter(p => p.dx > 0).map(p => ({ ...p, side: 'out' as const })), + // Pins on the other edge than usual (lines of a socket that run the other way) stay with their edge. + ...shape.value.inputs.filter(p => p.dx > 0).map(p => ({ ...p, side: 'out' as const })), + ...shape.value.outputs.filter(p => p.dx === 0).map(p => ({ ...p, side: 'in' as const })), ]) const shapes = computed(() => def.value?.design?.shapes ?? []) @@ -108,7 +111,7 @@ type Drag = | { type: 'resize'; id: string; handle: 'a' | 'b'; orig: FaceShape } | { type: 'scale'; key: string } | { type: 'box'; start: { x: number; y: number }; cur: { x: number; y: number }; add: boolean } - | { type: 'pin'; key: string; side: 'in' | 'out'; start: number; rows: { key: string; dy: number; set: boolean }[]; index: number } + | { type: 'pin'; key: string; side: 'in' | 'out'; start: number; rows: { key: string; dy: number; set: boolean; port?: string }[]; index: number } const drag = ref(null) function onDown(e: PointerEvent) { @@ -157,9 +160,9 @@ function onDown(e: PointerEvent) { if (id.startsWith('pin:')) { const key = id.slice(4) const pin = pins.value.find(x => x.key === key)! - const side = pins.value.filter(x => x.side === pin.side) + const side = pins.value.filter(x => x.side === pin.side).sort((a, b) => a.dy - b.dy) const pinAt = def.value.design?.pinAt ?? {} - drag.value = { type: 'pin', key, side: pin.side, start: p.y, rows: side.map(x => ({ key: x.key, dy: x.dy, set: pinAt[x.key] !== undefined })), index: side.findIndex(x => x.key === key) } + drag.value = { type: 'pin', key, side: pin.side, start: p.y, rows: side.map(x => ({ key: x.key, dy: x.dy, set: pinAt[x.key] !== undefined, ...(x.port ? { port: x.port } : {}) })), index: side.findIndex(x => x.key === key) } return } const handle = el!.getAttribute('data-handle') as 'a' | 'b' | null @@ -236,28 +239,31 @@ function onMove(e: PointerEvent) { } if (d.type === 'pin') { const row = d.rows[d.index]! + // A cable socket takes the lines split out of it along: they move as one block. + const block = d.rows.filter(r => r === row || (r.port && r.port === row.key)) + const others = d.rows.filter(r => !block.includes(r)) + const span = Math.max(...block.map(r => r.dy)) - row.dy const want = Math.max(0, Math.round(row.dy + p.y - d.start)) - const next = d.rows[d.index + 1] - const prevRow = d.rows[d.index - 1] + const above = others.filter(r => r.dy < row.dy).at(-1) + const below = others.find(r => r.dy > row.dy + span) const pinAt = (design().pinAt ??= {}) - if ((next && want >= next.dy) || (prevRow && want <= prevRow.dy)) { + const putBlock = (top: number) => { + for (const r of block) pinAt[r.key] = top + (r.dy - row.dy) + } + if ((below && want + span >= below.dy) || (above && want <= above.dy)) { // Onto or past a neighbour: the pins change order. The others stay where they are, this one goes where it is dragged. - const others = d.rows.filter((_, i) => i !== d.index) const at = others.filter(r => r.dy < want || (r.dy === want && want > row.dy)).length - const order = [...others.slice(0, at), row, ...others.slice(at)].map(r => r.key) + const order = [...others.slice(0, at), ...block, ...others.slice(at)].map(r => r.key) const keep = (design().pinOrder ?? []).filter(k => !d.rows.some(r => r.key === k)) design().pinOrder = [...keep, ...order] for (const r of others) pinAt[r.key] = r.dy - pinAt[row.key] = want + putBlock(want) return } - // Dragging a pin moves it and the pins below it on that edge: a gap opens above it. - const prev = d.index > 0 ? d.rows[d.index - 1]!.dy : -1 - const delta = Math.max(Math.round(p.y - d.start), prev + 1 - row.dy) - d.rows.forEach((r, i) => { - if (i === d.index) pinAt[r.key] = Math.max(0, r.dy + delta) - else if (i > d.index && r.set) pinAt[r.key] = Math.max(0, r.dy + delta) - }) + // Moved along its own gap: it and the pins below it on that edge go, so a gap opens above it. + const delta = Math.max(Math.round(p.y - d.start), (above ? above.dy + 1 : 0) - row.dy) + putBlock(row.dy + delta) + for (const r of others) if (r.dy > row.dy && r.set) pinAt[r.key] = Math.max(0, r.dy + delta) return } if (d.type === 'move') { diff --git a/app/lib/logic/geometry.ts b/app/lib/logic/geometry.ts index f7a82a7..5ae2cc4 100644 --- a/app/lib/logic/geometry.ts +++ b/app/lib/logic/geometry.ts @@ -763,9 +763,9 @@ export function portLines(node: CircuitNode, get: DefLookup): { name: string; en */ function portShape(node: CircuitNode, get: DefLookup): Shape { const lines = portLines(node, get) - // Wide enough for the longest line name, and for the title: " in · ". + // Wide enough for the longest line name, and for the title " in · " (9px letters, about a third of a square each). const title = `${(node.conn && get.conn?.(node.conn)?.name) || 'Port'} out${node.label?.trim() ? ` · ${node.label.trim()}` : ''}` - const w = Math.max(4, Math.ceil(lines.reduce((m, l) => Math.max(m, l.name.length), 0) * LABEL_CHAR + 2.6), Math.ceil(title.length * 0.42 + 1.2)) + const w = Math.max(4, Math.ceil(lines.reduce((m, l) => Math.max(m, l.name.length), 0) * LABEL_CHAR + 2.6), Math.ceil(title.length * 0.34 + 0.8)) const inner = node.kind === 'portin' ? w : 0 const face: 1 | -1 = node.kind === 'portin' ? 1 : -1 const bus: PinInfo = { key: 'bus', label: '', dx: inner, dy: 0, bus: true, conn: node.conn }