383 lines
18 KiB
Vue
383 lines
18 KiB
Vue
<script setup lang="ts">
|
|
import type { ChallengeCheck } from '~/composables/useEditor'
|
|
import { tableMarkdown, type TruthTable } from '~/lib/logic/analyze'
|
|
import { cableOf } from '~/lib/logic/cables'
|
|
import { apiId, isTable } from '~/lib/logic/puzzles'
|
|
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' || n.kind === 'pixel'))
|
|
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))])
|
|
// The challenge or puzzle this circuit is for.
|
|
const challenge = computed(() => {
|
|
const t = ed.task.value
|
|
const c = t?.challenge ?? t?.puzzle
|
|
return t && c ? { id: t.id, name: c.name, goal: c.goal, inputs: c.inputs, outputs: c.outputs, puzzle: t.puzzle } : undefined
|
|
})
|
|
const solved = computed(() => !!challenge.value && ed.project.value.solved.includes(challenge.value.id))
|
|
const board = computed(() => (challenge.value?.puzzle ? ed.boards[challenge.value.id] : undefined))
|
|
// What the puzzle asks for, as a table to read.
|
|
const target = computed<TruthTable | null>(() => {
|
|
const p = challenge.value?.puzzle
|
|
if (!p) return null
|
|
return { inputs: p.inputs, outputs: p.outputs, rows: p.steps.map(s => ({ ins: p.inputs.map(l => s.set[l] ?? 0), outs: p.outputs.map(l => s.expect[l] ?? 0), settled: true })) }
|
|
})
|
|
watch(
|
|
() => challenge.value?.puzzle,
|
|
p => {
|
|
if (p && !ed.boards[p.id]) void ed.loadBoard(p)
|
|
},
|
|
{ immediate: true },
|
|
)
|
|
const showTarget = ref(true)
|
|
const puzzleLink = computed(() => (challenge.value?.puzzle ? `${location.origin}/puzzle/${apiId(challenge.value.puzzle.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<ChallengeCheck | null>(null)
|
|
const bestHere = computed(() => (challenge.value ? ed.project.value.best?.[challenge.value.id] : undefined))
|
|
const cable = computed(() => (wires.value.length === 1 ? cableOf(def.value, wires.value[0]!) : undefined))
|
|
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>
|
|
<button class="btn mt-2 w-full" title="Their pins in order, top to bottom: click where each one goes (W)" @click="ed.startLinking()">
|
|
<Icon name="lucide:cable" class="size-4" /> Connect one by one
|
|
</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
|
|
v-if="wires.length >= 2"
|
|
class="btn mt-2 w-full"
|
|
title="Replace these wires with one cable: a bundle after their sources, a split before their targets"
|
|
@click="ed.bundleSelected()"
|
|
>
|
|
<Icon name="lucide:merge" class="size-4" /> Bundle into cable
|
|
</button>
|
|
<button v-if="cable" class="btn mt-2 w-full" title="Take the cable apart again into single wires" @click="ed.removeCable(cable.id)">
|
|
<Icon name="lucide:split" class="size-4" /> Remove cable
|
|
</button>
|
|
<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.puzzle ? 'Puzzle' : 'Challenge' }}: {{ challenge.name }}
|
|
</h3>
|
|
<p>{{ challenge.goal }}</p>
|
|
<p class="pins">Pins: {{ challenge.inputs.join(', ') }} → {{ challenge.outputs.join(', ') }}</p>
|
|
<template v-if="target">
|
|
<button class="section-toggle mt-2" @click="showTarget = !showTarget">
|
|
<Icon :name="showTarget ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
|
{{ isTable(challenge.puzzle!) ? 'Target truth table' : `Target: ${target.rows.length} steps from power-on` }}
|
|
</button>
|
|
<div v-if="showTarget" class="target-table">
|
|
<EditorTruthTable :table="target" />
|
|
</div>
|
|
</template>
|
|
<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>
|
|
<p class="score">
|
|
<Icon name="lucide:cpu" class="size-4 shrink-0" />
|
|
<span>
|
|
{{ ed.transistors(def.id).toLocaleString() }} transistors now<template v-if="bestHere !== undefined">, best <strong>{{ bestHere.toLocaleString() }}</strong></template>
|
|
</span>
|
|
</p>
|
|
<p v-if="check?.pass && check.record !== undefined" class="result pass">
|
|
<Icon name="lucide:trophy" class="size-4 shrink-0" /> New record! {{ check.record - check.transistors }} fewer than before ({{ check.record }}).
|
|
</p>
|
|
<p v-else-if="check?.pass && check.best === check.transistors" class="hint">Fewer transistors is better: 4 per NAND gate. Can you make it smaller?</p>
|
|
<div v-if="check?.pass && (check.unlocked?.length || check.nextUp?.length)" class="unlock">
|
|
<Icon name="lucide:sparkles" class="size-4 shrink-0" />
|
|
<div>
|
|
<p v-if="check.unlocked?.length"><strong>Unlocked:</strong> {{ check.unlocked.join(', ') }}</p>
|
|
<p v-if="check.nextUp?.length"><strong>New challenges:</strong> {{ check.nextUp.join(', ') }}</p>
|
|
</div>
|
|
</div>
|
|
<template v-if="challenge.puzzle">
|
|
<p v-if="challenge.puzzle.author !== undefined" class="hint mt-2">The author's solution uses {{ challenge.puzzle.author.toLocaleString() }} transistors.</p>
|
|
<EditorScoreChart v-if="board?.players" :summary="board" :mine="bestHere" />
|
|
<p v-else class="hint mt-2">No one has solved it yet. Be the first.</p>
|
|
<button class="btn btn-ghost mt-2 w-full" title="Copy the link to this puzzle" @click="ed.copyText(puzzleLink, 'Puzzle link copied')">
|
|
<Icon name="lucide:link" class="size-4" /> Copy puzzle link
|
|
</button>
|
|
</template>
|
|
</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 title="CMOS: 4 transistors per NAND gate">Transistors</dt>
|
|
<dd>{{ ed.transistors(def.id).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>
|
|
<button
|
|
v-if="!challenge"
|
|
class="btn mt-1 mb-2 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>
|
|
<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>
|
|
<label class="check">
|
|
<input type="checkbox" :checked="!!settings.tidy" @change="ed.setTidy(!settings.tidy)" />
|
|
Tidy wiring: sorted parallel runs, one trunk per signal, fewer crossings
|
|
</label>
|
|
<div class="field mt-3">
|
|
<span>Sound</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="icon-btn" :title="ed.sound.value.on ? 'Turn sound off' : 'Turn sound on'" @click="ed.setSound({ on: !ed.sound.value.on })">
|
|
<Icon :name="ed.sound.value.on ? 'lucide:volume-2' : 'lucide:volume-x'" class="size-4" />
|
|
</button>
|
|
<input
|
|
type="range"
|
|
min="0"
|
|
max="1"
|
|
step="0.05"
|
|
class="grow"
|
|
:value="ed.sound.value.volume"
|
|
:disabled="!ed.sound.value.on"
|
|
aria-label="Volume"
|
|
@change="ed.setSound({ volume: Number(($event.target as HTMLInputElement).value) })"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<label class="check">
|
|
<input type="checkbox" :checked="ed.sound.value.ticks" :disabled="!ed.sound.value.on" @change="ed.setSound({ ticks: !ed.sound.value.ticks })" />
|
|
Tick on every clock cycle (up to 8 Hz)
|
|
</label>
|
|
<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>
|
|
</section>
|
|
|
|
<section>
|
|
<div class="flex items-center gap-1">
|
|
<button class="section-toggle grow" @click="showTable = !showTable">
|
|
<Icon :name="showTable ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
|
Truth table
|
|
</button>
|
|
<template v-if="showTable && table && !('error' in table)">
|
|
<button class="icon-btn" title="Download as CSV" @click="ed.exportTable(table)"><Icon name="lucide:download" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Copy as a Markdown table" @click="ed.copyText(tableMarkdown(table), 'Truth table copied')">
|
|
<Icon name="lucide:clipboard-copy" class="size-3.5" />
|
|
</button>
|
|
</template>
|
|
<button class="icon-btn" title="Full screen" @click="ed.dialog.value = 'table'"><Icon name="lucide:maximize-2" class="size-3.5" /></button>
|
|
</div>
|
|
<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>
|
|
<EditorTruthTable :table="table" />
|
|
</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>
|