logigate/app/composables/useParts.ts

156 lines
7.5 KiB
TypeScript
Raw Normal View History

// Everything that can be placed, grouped for the parts list and searchable
// by keyboard (the list's search box, and the quick-add box at the mouse):
// the built-in parts by what they are for, then the components by group.
import { defUsable } from '~/lib/logic/progress'
import { matchScore } from '~/lib/match'
import { recentParts } from '~/lib/recent'
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
export interface BasicPart {
kind: NodeKind
name: string
icon: string
/** Key that places it straight away. */
key?: string
hint: string
/** Other words people search for it by. */
aka?: string
}
export const BASIC_GROUPS: { name: string; parts: BasicPart[] }[] = [
{
name: 'Logic',
parts: [{ kind: 'nand', name: 'NAND', icon: 'lucide:square-function', key: 'N', hint: 'The one logic gate. Output is 0 only when both inputs are 1.', aka: 'gate' }],
},
{
name: 'Inputs',
parts: [
{ kind: 'input', name: 'Input switch', icon: 'lucide:toggle-left', key: 'I', hint: 'A switch. Inside a component it becomes an input pin.', aka: 'pin in toggle' },
{ kind: 'button', name: 'Push button', icon: 'lucide:circle-dot', key: 'B', hint: '1 while held down.', aka: 'momentary' },
{ kind: 'clock', name: 'Clock', icon: 'lucide:audio-waveform', key: 'C', hint: 'Square wave. Speed and pause are in the top bar.', aka: 'oscillator tick' },
{ kind: 'panel', name: 'Input panel', icon: 'lucide:sliders-vertical', key: 'P', hint: 'A column of switches, buttons and clocks, each with its own name, key and colour.', aka: 'switches buttons' },
{ kind: 'high', name: 'Constant 1', icon: 'lucide:plus', hint: 'Always 1, like the supply rail.', aka: 'vcc high one supply' },
{ kind: 'keyboard', name: 'Keyboard', icon: 'lucide:keyboard', hint: 'Click it, then type: the first key waiting is on D0–D7 (its code), RDY is 1 while one waits, a rising NEXT takes it.', aka: 'text input ascii typing keys' },
{ kind: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.', aka: 'gnd low zero' },
],
},
{
name: 'Outputs',
parts: [
{ kind: 'output', name: 'Output pin', icon: 'lucide:log-out', key: 'O', hint: 'Shows a value. Inside a component it becomes an output pin.', aka: 'pin out' },
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.', aka: 'led light' },
{ kind: 'outpanel', name: 'Output panel', icon: 'lucide:list-checks', hint: 'A column of named lamps. Inside a component its rows are output pins.', aka: 'lamps leds' },
{ kind: 'hex', name: 'Hex display', icon: 'lucide:binary', key: 'H', hint: 'Shows 4 bits as 0–F, and passes them on.', aka: 'seven segment 7seg digit' },
{ kind: 'terminal', name: 'Terminal', icon: 'lucide:square-terminal', hint: 'A text screen: a rising WR writes the character whose code is on D0–D7 (10 starts a new line, 8 goes back); a rising CLR clears it.', aka: 'text output ascii console screen print' },
{ kind: 'pixel', name: 'Pixel', icon: 'lucide:square', key: 'X', hint: 'One square of a screen, lit at 1, or in colour with red, green and blue inputs. Pixels inside a component show on its box as screens.', aka: 'screen lcd rgb colour color monitor' },
],
},
{
name: 'Wiring',
parts: [
{ kind: 'bundle', name: 'Cable bundle', icon: 'lucide:merge', hint: 'Gathers several wires into one cable.', aka: 'bus' },
{ kind: 'split', name: 'Cable split', icon: 'lucide:split', hint: 'Fans a cable out into its wires again.', aka: 'bus' },
{ kind: 'tag', name: 'Send signal', icon: 'lucide:flag', hint: 'Names the signal wired into it, like CLK or RESET.', aka: 'label net tag' },
{ kind: 'ref', name: 'Use signal', icon: 'lucide:tag', hint: 'The signal of the "Send signal" with the same name, anywhere in this circuit.', aka: 'label net reference' },
],
},
{
name: 'Testing',
parts: [
{
kind: 'bench',
name: 'Test bench',
icon: 'lucide:flask-conical',
hint: 'Drop it on a component: a table of tests for it. Every combination, clock pulses, or ticks until the outputs are right.',
aka: 'test truth table check verify unit',
},
],
},
{
name: 'Notes',
parts: [{ kind: 'note', name: 'Note', icon: 'lucide:sticky-note', key: 'T', hint: 'Text label for your drawing.', aka: 'text comment' }],
},
]
export const BASIC_PARTS = BASIC_GROUPS.flatMap(g => g.parts)
/** Library groups in this order; your own components come first. */
export const GROUP_ORDER = ['', 'Puzzles', 'Gates', 'Routing', 'Arithmetic', 'Memory', 'Displays', 'Demos']
export interface PartHit {
kind: NodeKind
chip?: string
name: string
icon: string
/** Where it lives in the list. */
group: string
key?: string
score: number
}
export function useParts() {
const ed = useEditor()
const recent = recentParts
/** Components grouped like the list shows them. */
const componentGroups = computed(() => {
const map = new Map<string, ComponentDef[]>()
for (const d of ed.project.value.components) {
const g = d.group?.trim() ?? ''
if (!map.has(g)) map.set(g, [])
map.get(g)!.push(d)
}
const rank = (g: string) => (GROUP_ORDER.includes(g) ? GROUP_ORDER.indexOf(g) : GROUP_ORDER.length)
return [...map.entries()]
.sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b))
.map(([name, defs]) => ({ name: name || 'My components', defs }))
})
const placeable = (d: ComponentDef) => d.id !== ed.def.value.id && ed.canPlace(d.id) && defUsable(ed.project.value, d)
/** Everything that matches, best first: built-in parts and components that can go in the open circuit. */
function search(query: string, limit = 12): PartHit[] {
const q = query.trim().toLowerCase()
const hits: PartHit[] = []
for (const g of BASIC_GROUPS) {
for (const p of g.parts) {
if (!ed.canPlaceKind(p.kind)) continue
const score = Math.max(matchScore(p.name, q), matchScore(p.aka ?? '', q) * 0.8, p.key && q === p.key.toLowerCase() ? 90 : 0)
if (score) hits.push({ kind: p.kind, name: p.name, icon: p.icon, group: g.name, key: p.key, score: score + 1 })
}
}
for (const g of componentGroups.value) {
for (const d of g.defs) {
if (!placeable(d)) continue
const score = matchScore(d.name, q)
if (score) hits.push({ kind: 'chip', chip: d.id, name: d.name, icon: 'lucide:box', group: g.name, score })
}
}
if (!q) {
// Nothing typed yet: what was used lately, then the basics.
const order = new Map(recent.value.map((k, i) => [k, i]))
const at = (h: PartHit) => order.get(h.chip ? `chip:${h.chip}` : h.kind) ?? Infinity
return hits.sort((a, b) => at(a) - at(b)).slice(0, limit)
}
return hits.sort((a, b) => b.score - a.score || a.name.length - b.name.length).slice(0, limit)
}
/** The recently placed parts that still exist and can be placed here. */
const recentList = computed(() =>
recent.value
.map(k => {
if (!k.startsWith('chip:')) {
const p = BASIC_PARTS.find(b => b.kind === k)
return p && ed.canPlaceKind(p.kind) ? { kind: p.kind, name: p.name, icon: p.icon } : null
}
const d = ed.getDef.value(k.slice(5))
return d && placeable(d) ? { kind: 'chip' as NodeKind, chip: d.id, name: d.name, icon: 'lucide:box' } : null
})
.filter((x): x is { kind: NodeKind; chip?: string; name: string; icon: string } => !!x)
.slice(0, 6),
)
return { componentGroups, search, recentParts: recentList }
}