, get: DefLookup): Shape {
const auto = boxShape(def.name, pins, face)
const d = def.design!
const top = auto.body.y
- const pinH = Math.max(pins.inputs.length, pins.outputs.length, 1)
- const w = Math.max(2, d.w)
- const h = Math.max(pinH === 1 ? 2 : pinH, d.h)
const hide = new Set(d.hide ?? [])
const place = d.place ?? {}
+ // Pins on their edges: where they were put, the others one row under the pin above.
+ const placePins = (list: P[]) => {
+ let prev = -Infinity
+ return list.map(p => {
+ const want = d.pinAt?.[p.key]
+ const dy = Math.max(want ?? (prev === -Infinity ? p.dy : prev + 1), prev + 1, 0)
+ prev = dy
+ return { ...p, dy }
+ })
+ }
+ const inputs = placePins(auto.inputs)
+ const lowest = Math.max(0, ...inputs.map(p => p.dy), ...auto.outputs.map(p => d.pinAt?.[p.key] ?? p.dy))
+ const w = Math.max(2, d.w)
+ const outputs = placePins(auto.outputs).map(p => ({ ...p, dx: w }))
+ const bottom = Math.max(...inputs.map(p => p.dy), ...outputs.map(p => p.dy), lowest)
+ const h = Math.max(d.h, bottom - top + 0.5, 2)
+ // Own items where auto layout put them unless placed; displays of parts inside only when shown.
+ const show = new Set(d.show ?? [])
+ const nested = nestedItems(def, get).filter(it => show.has(it.key))
+ let below = (auto.face ?? []).reduce((m, it) => Math.max(m, it.y + it.h), auto.body.y + auto.body.h - 0.6) + 0.3
+ const items = [
+ ...(auto.face ?? []),
+ ...nested.map(it => {
+ const at = { ...it, x: 0.6, y: below }
+ below += it.h + 0.3
+ return at
+ }),
+ ]
+ const shown = items
+ .filter(it => !hide.has(it.key))
+ .map(it => {
+ const at = place[it.key]
+ const scale = d.scale?.[it.key]
+ const rotate = d.rotate?.[it.key]
+ return { ...it, ...(at ? { x: at.x, y: top + at.y } : {}), ...(scale ? { scale } : {}), ...(rotate ? { rotate } : {}) }
+ })
+ // The box is never smaller than what it shows (sized and turned as shown).
+ let right = w
+ let floor = top + h
+ for (const it of shown) {
+ const k = it.scale ?? 1
+ const turned = it.rotate === 90 || it.rotate === 270
+ const iw = (turned ? it.h : it.w) * k
+ const ih = (turned ? it.w : it.h) * k
+ const cx = it.x + (it.w * k) / 2
+ const cy = it.y + (it.h * k) / 2
+ right = Math.max(right, cx + iw / 2 + 0.3)
+ floor = Math.max(floor, cy + ih / 2 + 0.3)
+ }
+ const bw = Math.ceil(right * 2) / 2
return {
...auto,
- body: { x: 0, y: top, w, h },
- outputs: auto.outputs.map(p => ({ ...p, dx: w })),
- face: (auto.face ?? [])
- .filter(it => !hide.has(it.kind === 'screen' ? 'screen' : it.id))
- .map(it => {
- const at = place[it.kind === 'screen' ? 'screen' : it.id]
- return at ? { ...it, x: at.x, y: top + at.y } : it
- }),
+ inputs,
+ outputs: bw === w ? outputs : outputs.map(p => ({ ...p, dx: bw })),
+ body: { x: 0, y: top, w: bw, h: Math.ceil((floor - top) * 2) / 2 },
+ face: shown,
nameAt: hide.has('name') ? null : place.name ? { x: place.name.x, y: top + place.name.y } : undefined,
drawing: (d.shapes ?? []).map(sh => ({ ...sh, y: top + sh.y })),
}
@@ -434,7 +588,7 @@ export function shapeOf(node: CircuitNode, getDef: DefLookup): Shape {
if (!def) return MISSING_SHAPE
const pins = chipPins(def)
const face = faceLayout(def)
- if (def.design) return designedShape(def, pins, face)
+ if (def.design) return designedShape(def, pins, face, getDef)
if (def.symbol !== 'box' && !face.items.length && pins.outputs.length === 1 && pins.inputs.length >= 1 && pins.inputs.length <= 16) {
return gateShape(def.symbol, pins.inputs, pins.outputs[0]!)
}
diff --git a/app/lib/logic/project.ts b/app/lib/logic/project.ts
index b9f8d47..f95cffe 100644
--- a/app/lib/logic/project.ts
+++ b/app/lib/logic/project.ts
@@ -40,6 +40,7 @@ function cleanNode(n: CircuitNode) {
if (n[k] !== undefined && !(Array.isArray(n[k]) && n[k]!.every(v => typeof v === 'string'))) delete n[k]
}
if (n.inner !== undefined && n.inner !== true) delete n.inner
+ if (n.screen !== undefined && typeof n.screen !== 'string') delete n.screen
}
const MAX_ROWS = 256
@@ -69,7 +70,17 @@ function cleanDesign(d: ComponentDef) {
if (!f.place || typeof f.place !== 'object' || Array.isArray(f.place)) delete f.place
else for (const [k, v] of Object.entries(f.place)) if (!v || !num(v.x, -200, 400) || !num(v.y, -200, 400)) delete f.place[k]
}
- if (f.hide !== undefined && !(Array.isArray(f.hide) && f.hide.every(v => typeof v === 'string'))) delete f.hide
+ for (const k of ['hide', 'show'] as const) if (f[k] !== undefined && !(Array.isArray(f[k]) && f[k]!.every(v => typeof v === 'string'))) delete f[k]
+ // Sizes, turns and pin rows: numbers in range only.
+ const nums = (rec: Record | undefined, ok: (v: number) => boolean) => {
+ if (!rec || typeof rec !== 'object' || Array.isArray(rec)) return undefined
+ for (const [k, v] of Object.entries(rec)) if (!(typeof v === 'number' && ok(v))) delete rec[k]
+ return rec
+ }
+ if (f.scale !== undefined) f.scale = nums(f.scale, v => v >= 0.25 && v <= 4)
+ if (f.rotate !== undefined) f.rotate = nums(f.rotate, v => [0, 90, 180, 270].includes(v))
+ if (f.pinAt !== undefined) f.pinAt = nums(f.pinAt, v => Number.isInteger(v) && v >= 0 && v <= 1000)
+ for (const k of ['scale', 'rotate', 'pinAt'] as const) if (f[k] === undefined) delete f[k]
if (f.shapes !== undefined) {
f.shapes = Array.isArray(f.shapes)
? f.shapes
@@ -87,6 +98,7 @@ function cleanDesign(d: ComponentDef) {
if (sh.text !== undefined && typeof sh.text !== 'string') delete sh.text
if (sh.size !== undefined && !num(sh.size, 0.25, 4)) delete sh.size
if (sh.fill !== undefined && sh.fill !== true) delete sh.fill
+ if (sh.rotate !== undefined && ![0, 90, 180, 270].includes(sh.rotate)) delete sh.rotate
}
}
}
diff --git a/app/lib/logic/types.ts b/app/lib/logic/types.ts
index 0c1f597..fc2d619 100644
--- a/app/lib/logic/types.ts
+++ b/app/lib/logic/types.ts
@@ -66,6 +66,8 @@ export interface CircuitNode {
invert?: string[]
/** Panel whose rows stay inside a component instead of becoming its pins (an input panel's switches stay as set). */
inner?: boolean
+ /** Pixel: the screen it belongs to on the component's box; pixels without one make the main screen. */
+ screen?: string
}
export const PANEL_CONTROLS = ['switch', 'button', 'clock'] as const
@@ -113,7 +115,7 @@ export interface Wire {
color?: WireColor
}
-export const WIRE_COLORS = ['red', 'orange', 'yellow', 'blue', 'purple', 'pink'] as const
+export const WIRE_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink'] as const
export type WireColor = (typeof WIRE_COLORS)[number]
export interface ComponentDef {
@@ -147,10 +149,22 @@ export type FaceInk = (typeof FACE_INKS)[number]
export interface FaceDesign {
w: number
h: number
- /** Top left corner of lamps and displays (by node id), the pixel screen ('screen') and the name ('name'). */
+ /**
+ * Top left corner of things on the box, by key: a lamp or display (its
+ * node id), a pixel screen ('screen', or 'screen:'), the name
+ * ('name'), or a display of a component inside ('/').
+ */
place?: Record
- /** Left off the box: lamp and display node ids, 'screen', 'name'. */
+ /** Left off the box (keys as in place). */
hide?: string[]
+ /** Displays of components inside, shown on this box (they are left off unless listed). */
+ show?: string[]
+ /** Size of a thing on the box, 1 as it comes. */
+ scale?: Record
+ /** Turn of a thing on the box in degrees: 0, 90, 180 or 270. */
+ rotate?: Record
+ /** Row of a pin on its edge (by pin key); pins without one follow the pin above. */
+ pinAt?: Record
shapes?: FaceShape[]
}
@@ -168,6 +182,8 @@ export interface FaceShape {
fill?: boolean
/** Text size: 1 is normal. */
size?: number
+ /** Turn in degrees around its middle. */
+ rotate?: number
}
export interface Settings {
diff --git a/app/lib/match.ts b/app/lib/match.ts
new file mode 100644
index 0000000..af48527
--- /dev/null
+++ b/app/lib/match.ts
@@ -0,0 +1,17 @@
+// How well a name matches what was typed, for finding parts by keyboard.
+
+/**
+ * The whole name, its start, a word start, anywhere in it, or its letters
+ * in order (adr4 finds Adder 4); 0 for no match. Empty input matches all.
+ */
+export function matchScore(text: string, q: string): number {
+ const t = text.toLowerCase()
+ if (!q) return 1
+ if (t === q) return 100
+ if (t.startsWith(q)) return 80
+ if (t.split(/[\s\-_/()]+/).some(w => w.startsWith(q))) return 60
+ if (t.includes(q)) return 40
+ let i = 0
+ for (const c of t) if (c === q[i]) i++
+ return i === q.length ? 15 : 0
+}
diff --git a/app/lib/recent.ts b/app/lib/recent.ts
new file mode 100644
index 0000000..69d79b8
--- /dev/null
+++ b/app/lib/recent.ts
@@ -0,0 +1,26 @@
+// Parts placed lately, newest first ("nand", "chip:"), kept in this
+// browser: the parts list and the quick-add box offer them first.
+
+import { ref } from 'vue'
+import type { NodeKind } from './logic/types'
+
+const KEY = 'logigate:recent-parts'
+
+function load(): string[] {
+ try {
+ const v = JSON.parse(localStorage.getItem(KEY) ?? '[]')
+ return Array.isArray(v) ? v.filter(x => typeof x === 'string').slice(0, 8) : []
+ } catch {
+ return []
+ }
+}
+
+export const recentParts = ref(typeof localStorage === 'undefined' ? [] : load())
+
+export function rememberPart(kind: NodeKind, chip?: string) {
+ const key = chip ? `chip:${chip}` : kind
+ recentParts.value = [key, ...recentParts.value.filter(k => k !== key)].slice(0, 8)
+ try {
+ localStorage.setItem(KEY, JSON.stringify(recentParts.value))
+ } catch {}
+}
diff --git a/tests/face.test.ts b/tests/face.test.ts
index 5cf8850..94fe08b 100644
--- a/tests/face.test.ts
+++ b/tests/face.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { compile } from '../app/lib/logic/compile'
-import { shapeOf } from '../app/lib/logic/geometry'
+import { faceCandidates, screenLayouts, shapeOf } from '../app/lib/logic/geometry'
+import { matchScore } from '../app/lib/match'
import { createProject, lookup } from '../app/lib/logic/ops'
import { toProject } from '../app/lib/logic/project'
import { Simulator } from '../app/lib/logic/sim'
@@ -86,3 +87,75 @@ describe('a box drawn by hand', () => {
expect(toProject(JSON.parse(JSON.stringify(p))).components.find(d => d.id === 'inner')!.design).toBeUndefined()
})
})
+
+describe('the box, finer', () => {
+ it('pixels make one screen per screen name', () => {
+ const def: ComponentDef = {
+ id: 'd',
+ name: 'D',
+ symbol: 'box',
+ nodes: [
+ { id: 'p1', kind: 'pixel', x: 0, y: 0 },
+ { id: 'p2', kind: 'pixel', x: 1, y: 0 },
+ { id: 'q1', kind: 'pixel', x: 0, y: 5, screen: 'Status' },
+ ],
+ wires: [],
+ }
+ expect(screenLayouts(def).map(s => [s.key, s.pixels!.length])).toEqual([
+ ['screen', 2],
+ ['screen:Status', 1],
+ ])
+ })
+
+ it('shows displays of components inside when asked, with their values, and spaces pins', () => {
+ const { p, inner } = shown()
+ const outer: ComponentDef = {
+ id: 'outer',
+ name: 'Outer',
+ symbol: 'box',
+ nodes: [
+ { id: 'x', kind: 'input', x: 0, y: 0, label: 'X' },
+ { id: 'y', kind: 'input', x: 0, y: 1, label: 'Y' },
+ { id: 'u', kind: 'chip', chip: 'inner', x: 5, y: 0 },
+ ],
+ wires: [{ id: 'w', from: { node: 'x', pin: 'q' }, to: { node: 'u', pin: 'a' } }],
+ }
+ const root: ComponentDef = {
+ id: 'root',
+ name: 'Root',
+ symbol: 'box',
+ nodes: [
+ { id: 's', kind: 'input', x: -4, y: 0 },
+ { id: 'o', kind: 'chip', chip: 'outer', x: 0, y: 0 },
+ ],
+ wires: [{ id: 'v', from: { node: 's', pin: 'q' }, to: { node: 'o', pin: 'x' } }],
+ }
+ p.components.push(outer, root)
+ const get = lookup(p)
+ const cands = faceCandidates(outer, get)
+ expect(cands.filter(c => c.nested).map(c => c.key)).toEqual(['u/l', 'u/h'])
+ const chip = { id: 'o', kind: 'chip' as const, chip: 'outer', x: 0, y: 0 }
+ expect(shapeOf(chip, get).face ?? []).toEqual([])
+ outer.design = { w: 8, h: 6, show: ['u/l'], scale: { 'u/l': 2 }, rotate: { 'u/l': 90 }, pinAt: { y: 3 } }
+ const s = shapeOf(chip, get)
+ expect(s.face).toEqual([expect.objectContaining({ id: 'u/l', key: 'u/l', scale: 2, rotate: 90 })])
+ // Y was put three rows down, under X: a gap.
+ expect(s.inputs.map(i => i.dy)).toEqual([0, 3])
+ // The lamp deep inside lights on the outer box when its signal is on.
+ const sim = new Simulator(compile(get, 'root'))
+ sim.setSource('s', true)
+ sim.run()
+ expect(sim.face('o', 'u/l', 'a')).toBe(1)
+ void inner
+ })
+})
+
+describe('finding parts', () => {
+ it('matches names by start, word, inside, and letters in order', () => {
+ expect(matchScore('Adder 4', 'adder 4')).toBeGreaterThan(matchScore('Adder 4', 'add'))
+ expect(matchScore('Adder 4', 'add')).toBeGreaterThan(matchScore('Full Adder', 'add'))
+ expect(matchScore('Full Adder', 'add')).toBeGreaterThan(matchScore('Ladder', 'add'))
+ expect(matchScore('Adder 4', 'adr4')).toBeGreaterThan(0)
+ expect(matchScore('Lamp', 'xyz')).toBe(0)
+ })
+})