logigate/app/components/editor/CircuitPane.vue

176 lines
8.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
// "This": what the circuit you are in is. Its name and health on top, four
// numbers (the transistors open their breakdown), its truth table and how it
// looks as a part. Things to do with it are in the menu on its name in the
// top bar; unused components are with the components in the parts bar.
import { TRANSISTORS_PER_NAND } from '~/lib/logic/analyze'
import { transistorBreakdown } from '~/lib/logic/bulk'
import { chipPins } from '~/lib/logic/geometry'
import { usages } from '~/lib/logic/ops'
import type { SymbolKind } from '~/lib/logic/types'
const ed = useEditor()
const def = ed.def
const SYMBOLS: { value: SymbolKind; label: string }[] = [
{ value: 'box', label: 'Box with name' },
{ value: 'and', label: 'AND gate' },
{ value: 'or', label: 'OR gate' },
{ value: 'xor', label: 'XOR gate' },
{ value: 'not', label: 'NOT gate' },
{ value: 'buf', label: 'Buffer' },
{ value: 'nand', label: 'NAND gate' },
{ value: 'nor', label: 'NOR gate' },
{ value: 'xnor', label: 'XNOR gate' },
]
const nameInput = ref<HTMLInputElement>()
watch(
() => ed.labelFocus.value,
() =>
nextTick(() => {
if (ed.selection.value.length) return
nameInput.value?.focus()
nameInput.value?.select()
}),
)
const pins = computed(() => chipPins(def.value))
const users = computed(() => usages(ed.project.value, def.value.id))
const groupNames = computed(() => [...new Set(ed.project.value.components.map(d => d.group).filter(Boolean))])
const hasFace = computed(() => def.value.nodes.some(n => n.kind === 'lamp' || n.kind === 'hex' || n.kind === 'pixel'))
const total = computed(() => ed.transistors(def.value.id))
const breakdown = computed(() => transistorBreakdown(def.value, ed.nands.value, ed.getDef.value))
function toggleFace(e: Event) {
def.value.face = (e.target as HTMLInputElement).checked
ed.commit(false)
}
// ── Problems ────────────────────────────────────────────────────────
const LEVEL_ICON = { error: 'lucide:circle-x', warn: 'lucide:triangle-alert', info: 'lucide:info' } as const
const showInfo = ref(false)
const problems = computed(() => ed.issues.value.filter(i => i.level !== 'info'))
const worst = computed<'error' | 'warn' | 'ok'>(() => (problems.value.some(i => i.level === 'error') ? 'error' : problems.value.length ? 'warn' : 'ok'))
const shownIssues = computed(() => ed.issues.value.filter(i => showInfo.value || i.level !== 'info'))
const infoCount = computed(() => ed.issues.value.filter(i => i.level === 'info').length)
/** The transistors tile shows where they are. */
const showCost = ref(false)
</script>
<template>
<section class="this-head">
<input
ref="nameInput"
v-model="def.name"
class="title-input"
title="Rename this circuit"
spellcheck="false"
@change="ed.commit(false)"
@keydown.enter="($event.target as HTMLInputElement).blur()"
/>
<p class="this-sub">
<template v-if="users.length">
Used in
<template v-for="(u, i) in users" :key="u.id">
<a href="#" @click.prevent="ed.open(u.id)">{{ u.name }}</a><template v-if="i < users.length - 1">, </template>
</template>
</template>
<template v-else>{{ def.id === ed.mainId.value ? 'The main circuit' : 'Not used in another circuit' }}</template>
</p>
<div class="health" :class="worst">
<Icon :name="worst === 'ok' ? 'lucide:circle-check' : LEVEL_ICON[worst]" class="size-4 shrink-0" />
<span class="grow">{{ worst === 'ok' ? 'All good' : `${problems.length} ${problems.length === 1 ? 'problem' : 'problems'}` }}</span>
<label v-if="infoCount" class="health-notes" title="Show notes too: things that work but may not be meant">
<input v-model="showInfo" type="checkbox" /> {{ infoCount }} {{ infoCount === 1 ? 'note' : 'notes' }}
</label>
</div>
<ul v-if="shownIssues.length" class="issue-list">
<li v-for="(i, k) in shownIssues.slice(0, 40)" :key="k">
<button class="issue" :class="i.level" :disabled="!i.nodes.length" @click="ed.showIssue(i.nodes)">
<Icon :name="LEVEL_ICON[i.level]" class="issue-icon size-4 shrink-0" />
<span>{{ i.text }}</span>
</button>
</li>
</ul>
<p v-if="shownIssues.length > 40" class="hint">and {{ shownIssues.length - 40 }} more</p>
<p v-for="err in ed.errors.value" :key="err" class="result fail">
<Icon name="lucide:triangle-alert" class="size-4 shrink-0" /> {{ err }}
</p>
<div class="tiles">
<button class="tile" title="List and rename them" @click="ed.panelTab.value = 'pins'">
<span class="tile-label">Inputs</span>
<span class="tile-value">{{ pins.inputs.length }}</span>
</button>
<button class="tile" title="List and rename them" @click="ed.panelTab.value = 'pins'">
<span class="tile-label">Outputs</span>
<span class="tile-value">{{ pins.outputs.length }}</span>
</button>
<button class="tile" :class="{ active: showCost }" :aria-expanded="showCost" title="CMOS: 4 per NAND gate, at every depth. Click for where they are" @click="showCost = !showCost">
<span class="tile-label">Transistors</span>
<span class="tile-value">{{ total.toLocaleString() }}</span>
<span class="tile-note">{{ ed.circuitStats.value.nands.toLocaleString() }} NAND</span>
</button>
<div class="tile" :title="ed.circuitStats.value.sequential ? 'It has feedback loops, so it can hold state' : 'Longest path from an input to an output'">
<span class="tile-label">{{ ed.circuitStats.value.sequential ? 'Memory' : 'Delay' }}</span>
<span class="tile-value">{{ ed.circuitStats.value.sequential ? 'Yes' : ed.circuitStats.value.delay }}</span>
<span class="tile-note">{{ ed.circuitStats.value.sequential ? 'feedback loops' : 'gates deep' }}</span>
</div>
</div>
<template v-if="showCost">
<p v-if="!breakdown.length" class="hint mt-2">No gates here yet. Switches, lamps, displays and cables need no transistors.</p>
<ul v-else class="cost-list mt-2">
<li v-for="l in breakdown" :key="l.key" title="Select these parts" @click="ed.selectOnly(l.ids)">
<span class="cost-name">
<span class="truncate">{{ l.name }}</span>
<span class="cost-each">{{ l.count }} × {{ l.each.toLocaleString() }}</span>
</span>
<span class="cost-total">{{ l.total.toLocaleString() }}</span>
</li>
<li class="cost-sum">
<span class="cost-name">
<span>Total</span>
<span class="cost-each">{{ (total / TRANSISTORS_PER_NAND).toLocaleString() }} NAND × 4</span>
</span>
<span class="cost-total">{{ total.toLocaleString() }}</span>
</li>
</ul>
</template>
</section>
<section class="folds">
<EditorFold id="circuit-table" title="Truth table">
<EditorTestPane part="table" />
</EditorFold>
<EditorFold id="circuit-component" title="As a component" :note="def.group || ''">
<label class="field">
<span>Description</span>
<textarea v-model="def.description" rows="2" placeholder="What does it do?" @change="ed.commit(false)" />
</label>
<label class="field">
<span>Drawn as</span>
<select v-model="def.symbol" @change="ed.commit(false)">
<option v-for="s in SYMBOLS" :key="s.value" :value="s.value">{{ s.label }}</option>
</select>
</label>
<label class="field">
<span>Group in the parts list</span>
<input v-model="def.group" list="groups" placeholder="My components" @change="ed.commit(false)" />
<datalist id="groups">
<option v-for="g in groupNames" :key="g" :value="g" />
</datalist>
</label>
<label v-if="hasFace" class="check mb-3">
<input type="checkbox" :checked="def.face !== false" @change="toggleFace" />
Show its lamps and displays on the box
</label>
<button class="btn w-full" :title="`How it looks when placed: ${def.design ? 'drawn by hand' : 'laid out automatically'}`" @click="ed.designFace(def.id)">
<Icon name="lucide:pen-tool" class="size-4" /> Design the box
</button>
</EditorFold>
</section>
</template>