278 lines
12 KiB
Vue
278 lines
12 KiB
Vue
<script setup lang="ts">
|
|
import type { TestResult, TruthTable } from '~/lib/logic/analyze'
|
|
import { CHALLENGES } from '~/lib/logic/challenges'
|
|
import { chipPins } from '~/lib/logic/geometry'
|
|
import { usages } from '~/lib/logic/ops'
|
|
import { WIRE_COLORS, type CrossingStyle, 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 single = computed(() =>
|
|
ed.selection.value.length === 1 && !ed.wireSel.value.length ? def.value.nodes.find(n => n.id === ed.selection.value[0]) : undefined,
|
|
)
|
|
const wires = ed.selectedWires
|
|
const wireColor = computed(() => {
|
|
const colors = new Set(wires.value.map(w => w.color ?? ''))
|
|
return colors.size === 1 ? [...colors][0] : null
|
|
})
|
|
const hasFace = computed(() => def.value.nodes.some(n => n.kind === 'lamp' || n.kind === 'hex'))
|
|
const nameInput = ref<HTMLInputElement>()
|
|
watch(
|
|
() => ed.labelFocus.value,
|
|
() =>
|
|
nextTick(() => {
|
|
if (single.value) return
|
|
nameInput.value?.focus()
|
|
nameInput.value?.select()
|
|
}),
|
|
)
|
|
|
|
const CROSSINGS: { value: CrossingStyle; label: string }[] = [
|
|
{ value: 'bow', label: 'Bow' },
|
|
{ value: 'triangle', label: 'Triangle' },
|
|
{ value: 'bridge', label: 'Bridge' },
|
|
{ value: 'gap', label: 'Gap' },
|
|
{ value: 'plain', label: 'Plain' },
|
|
]
|
|
const settings = computed(() => ed.project.value.settings)
|
|
function setCrossing(c: CrossingStyle) {
|
|
settings.value.crossing = c
|
|
ed.persist()
|
|
}
|
|
|
|
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 challenge = computed(() => CHALLENGES.find(c => c.id === def.value.challenge))
|
|
const solved = computed(() => !!challenge.value && ed.project.value.solved.includes(challenge.value.id))
|
|
|
|
const packName = ref('')
|
|
function pack() {
|
|
ed.packageSelected(packName.value || 'Part')
|
|
packName.value = ''
|
|
}
|
|
|
|
const showTable = ref(false)
|
|
const table = ref<TruthTable | { error: string } | null>(null)
|
|
function refreshTable() {
|
|
table.value = showTable.value ? ed.table() : null
|
|
}
|
|
watch([showTable, () => ed.sim.value, () => def.value.id], refreshTable)
|
|
|
|
const check = ref<TestResult | null>(null)
|
|
watch(() => def.value.id, () => (check.value = null))
|
|
watch(() => ed.sim.value, () => (check.value = null))
|
|
function runCheck() {
|
|
check.value = ed.checkChallenge()
|
|
}
|
|
|
|
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>
|
|
<aside class="inspector">
|
|
<div class="scroll">
|
|
<!-- Selection -->
|
|
<EditorNodeSettings v-if="single" :key="single.id" :node="single" />
|
|
|
|
<section v-else-if="ed.selection.value.length > 1 || (ed.selection.value.length && wires.length)">
|
|
<h3>
|
|
{{ ed.selection.value.length }} part{{ ed.selection.value.length === 1 ? '' : 's' }}<template v-if="wires.length">, {{ wires.length }} wire{{ wires.length === 1 ? '' : 's' }}</template> selected
|
|
</h3>
|
|
<p class="hint">Turn them into a reusable component. Wires that cross the selection border become its pins.</p>
|
|
<label class="field">
|
|
<span>Component name</span>
|
|
<input v-model="packName" placeholder="e.g. Half Adder" @keydown.enter="pack" />
|
|
</label>
|
|
<button class="btn btn-primary w-full" @click="pack"><Icon name="lucide:package" class="size-4" /> Make component</button>
|
|
<div class="mt-2 grid grid-cols-2 gap-2">
|
|
<button class="btn" title="Remove every wire attached to the selected parts" @click="ed.unlink(ed.selection.value)"><Icon name="lucide:unlink" class="size-4" /> Unlink all</button>
|
|
<button class="btn" @click="ed.duplicate()"><Icon name="lucide:copy" class="size-4" /> Duplicate</button>
|
|
</div>
|
|
<button class="btn btn-ghost mt-2 w-full" @click="ed.deleteSelected()"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
|
</section>
|
|
|
|
<section v-else-if="wires.length">
|
|
<h3>{{ wires.length === 1 ? 'Wire' : `${wires.length} wires` }}</h3>
|
|
<div class="field">
|
|
<span>Colour</span>
|
|
<div class="swatches">
|
|
<button class="swatch none" :class="{ active: wireColor === '' }" title="Default" @click="ed.setWireColor(undefined)" />
|
|
<button
|
|
v-for="c in WIRE_COLORS"
|
|
:key="c"
|
|
class="swatch"
|
|
:class="[`c-${c}`, { active: wireColor === c }]"
|
|
:title="c"
|
|
@click="ed.setWireColor(c)"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<ul class="tips">
|
|
<li><strong>Drag</strong> a segment to move it, a <strong>corner square</strong> to move a bend.</li>
|
|
<li><strong>Alt + drag</strong> pulls a new bend out of a segment.</li>
|
|
<li><strong>Ctrl + drag</strong> starts a new wire from this signal.</li>
|
|
<li><strong>Double-click</strong> selects the whole signal, <strong>Shift + click</strong> adds wires.</li>
|
|
<li><strong>Arrow keys</strong> shift the selected wires.</li>
|
|
</ul>
|
|
<div class="grid grid-cols-2 gap-2">
|
|
<button v-if="wires.length === 1" class="btn" @click="ed.selectNet(wires[0]!.id)"><Icon name="lucide:git-fork" class="size-4" /> Whole signal</button>
|
|
<button class="btn" :disabled="!wires.some(w => w.pts)" @click="ed.autoRoute()"><Icon name="lucide:wand-sparkles" class="size-4" /> Auto route</button>
|
|
</div>
|
|
<button class="btn btn-ghost mt-2 w-full" @click="ed.deleteSelected()"><Icon name="lucide:trash-2" class="size-4" /> Delete</button>
|
|
</section>
|
|
|
|
<!-- Challenge -->
|
|
<section v-if="challenge" class="challenge" :class="{ solved }">
|
|
<h3>
|
|
<Icon :name="solved ? 'lucide:badge-check' : 'lucide:flag'" class="size-4" />
|
|
Challenge: {{ challenge.name }}
|
|
</h3>
|
|
<p>{{ challenge.goal }}</p>
|
|
<p class="pins">Pins: {{ challenge.inputs.join(', ') }} → {{ challenge.outputs.join(', ') }}</p>
|
|
<button class="btn btn-primary w-full" @click="runCheck"><Icon name="lucide:play" class="size-4" /> Check</button>
|
|
<p v-if="check" class="result" :class="check.pass ? 'pass' : 'fail'">
|
|
<Icon :name="check.pass ? 'lucide:circle-check' : 'lucide:circle-x'" class="size-4 shrink-0" />
|
|
{{ check.message }}
|
|
</p>
|
|
</section>
|
|
|
|
<!-- Circuit -->
|
|
<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>{{ pins.inputs.length }} in, {{ pins.outputs.length }} out</dd>
|
|
<dt>NAND gates</dt>
|
|
<dd>{{ ed.circuitStats.value.nands.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 v-if="!single && !wires.length && !ed.selection.value.length">
|
|
<h3>Display</h3>
|
|
<div class="field">
|
|
<span>Gate symbols</span>
|
|
<div class="seg">
|
|
<button :class="{ active: settings.symbols === 'iec' }" @click="settings.symbols = 'iec'; ed.persist()">IEC (DIN)</button>
|
|
<button :class="{ active: settings.symbols === 'ansi' }" @click="settings.symbols = 'ansi'; ed.persist()">ANSI</button>
|
|
</div>
|
|
</div>
|
|
<div class="field">
|
|
<span>Where unconnected wires cross</span>
|
|
<div class="crossings">
|
|
<button
|
|
v-for="c in CROSSINGS"
|
|
:key="c.value"
|
|
class="crossing"
|
|
:class="{ active: (settings.crossing ?? 'bow') === c.value }"
|
|
:title="c.label"
|
|
@click="setCrossing(c.value)"
|
|
>
|
|
<EditorCrossingIcon :style="c.value" />
|
|
<span>{{ c.label }}</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<label class="check">
|
|
<input type="checkbox" :checked="settings.flow" @change="settings.flow = !settings.flow; ed.persist()" />
|
|
Animate current flow on wires that carry a 1
|
|
</label>
|
|
</section>
|
|
|
|
<section>
|
|
<button class="section-toggle" @click="showTable = !showTable">
|
|
<Icon :name="showTable ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
|
Truth table
|
|
</button>
|
|
<template v-if="showTable && table">
|
|
<p v-if="'error' in table" class="hint">{{ table.error }}</p>
|
|
<template v-else>
|
|
<p v-if="ed.circuitStats.value.sequential" class="hint">This circuit stores state, so the table depends on the order the rows are tried in.</p>
|
|
<div class="truth">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th v-for="l in table.inputs" :key="`i${l}`">{{ l }}</th>
|
|
<th class="sep" />
|
|
<th v-for="l in table.outputs" :key="`o${l}`" class="out">{{ l }}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="(r, i) in table.rows" :key="i" :class="{ unstable: !r.settled }">
|
|
<td v-for="(v, k) in r.ins" :key="`i${k}`" :class="{ one: v }">{{ v }}</td>
|
|
<td class="sep" />
|
|
<td v-for="(v, k) in r.outs" :key="`o${k}`" class="out" :class="{ one: v }">{{ r.settled ? v : '~' }}</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
</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>
|
|
</div>
|
|
</aside>
|
|
</template>
|