logigate/app/components/editor/CircuitPane.vue

157 lines
6.4 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">
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))
const showBreakdown = ref(true)
function toggleFace(e: Event) {
def.value.face = (e.target as HTMLInputElement).checked
ed.commit(false)
}
const confirmDelete = ref(false)
watch(() => def.value.id, () => (confirmDelete.value = false))
</script>
<template>
<section>
<h3>This circuit</h3>
<label class="field">
<span>Name</span>
<input ref="nameInput" v-model="def.name" @change="ed.commit(false)" @keydown.enter="($event.target as HTMLInputElement).blur()" />
</label>
<label class="field">
<span>Description</span>
<textarea v-model="def.description" rows="2" placeholder="What does it do?" @change="ed.commit(false)" />
</label>
<div class="grid grid-cols-2 gap-2">
<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</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>
</div>
<label v-if="hasFace" class="check">
<input type="checkbox" :checked="def.face !== false" @change="toggleFace" />
Show its lamps and displays on the component box
</label>
<dl class="stats">
<dt>Pins</dt>
<dd>
<a href="#" title="List and rename them" @click.prevent="ed.rightTab.value = 'pins'">{{ pins.inputs.length }} in, {{ pins.outputs.length }} out</a>
</dd>
<dt>NAND gates</dt>
<dd>{{ ed.circuitStats.value.nands.toLocaleString() }}</dd>
<dt>Transistors</dt>
<dd>{{ total.toLocaleString() }}</dd>
<dt>{{ ed.circuitStats.value.sequential ? 'Memory' : 'Delay' }}</dt>
<dd>{{ ed.circuitStats.value.sequential ? 'yes (feedback loops)' : `${ed.circuitStats.value.delay} gates` }}</dd>
</dl>
<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>
<p v-if="users.length" class="hint">
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>
</p>
</section>
<section>
<button class="section-toggle" @click="showBreakdown = !showBreakdown">
<Icon :name="showBreakdown ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
Where the transistors are
</button>
<template v-if="showBreakdown">
<p v-if="!breakdown.length" class="hint mt-2">No gates here yet. Switches, lamps, displays and cables need no transistors.</p>
<table v-else class="cost">
<tbody>
<tr v-for="l in breakdown" :key="l.key" title="Select these parts" @click="ed.selectOnly(l.ids)">
<td class="what">{{ l.count }} × {{ l.name }}</td>
<td class="num">{{ l.each.toLocaleString() }} each</td>
<td class="num strong">{{ l.total.toLocaleString() }}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td class="num">{{ (total / TRANSISTORS_PER_NAND).toLocaleString() }} NAND</td>
<td class="num strong">{{ total.toLocaleString() }}</td>
</tr>
</tfoot>
</table>
<p class="hint mt-2">
Everything here is built from NAND gates, and a CMOS NAND gate has 4 transistors: 2 in parallel to the supply, 2 in series to
ground. So the count is 4 per NAND inside, at every depth: a NOT costs 4, an AND 8, a full adder 60. (A NOT made directly in
CMOS would need only 2; here it is built from a NAND.)
</p>
</template>
</section>
<section>
<div class="grid gap-2">
<button class="btn w-full" title="Remove hand-placed bends in this circuit and lay out every wire tidily (undo brings them back)" @click="ed.tidyAll()">
<Icon name="lucide:wand-sparkles" class="size-4" /> Tidy all wires
</button>
<button
v-if="!ed.task.value"
class="btn w-full"
title="Turn this circuit into a puzzle link: others build the same behaviour, your circuit stays private"
@click="ed.dialog.value = 'publish'"
>
<Icon name="lucide:puzzle" class="size-4" /> Publish as puzzle
</button>
</div>
</section>
<section class="actions">
<button class="btn btn-ghost" @click="ed.copyComponent(def.id)"><Icon name="lucide:copy" class="size-4" /> Duplicate</button>
<button v-if="!confirmDelete" class="btn btn-ghost" @click="confirmDelete = true"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
<button v-else class="btn btn-danger" @click="ed.removeComponent(def.id); confirmDelete = false">Really delete?</button>
</section>
</template>