144 lines
6.7 KiB
Vue
144 lines
6.7 KiB
Vue
<script setup lang="ts">
|
||
import { CHALLENGES } from '~/lib/logic/challenges'
|
||
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
|
||
|
||
const ed = useEditor()
|
||
const tab = ref<'parts' | 'challenges'>('parts')
|
||
const query = ref('')
|
||
|
||
const BASICS: { kind: NodeKind; name: string; icon: string; key?: string; hint: string }[] = [
|
||
{ kind: 'nand', name: 'NAND', icon: 'lucide:square-function', key: 'N', hint: 'The one logic gate. Output is 0 only when both inputs are 1.' },
|
||
{ kind: 'input', name: 'Input', icon: 'lucide:toggle-left', key: 'I', hint: 'A switch. Inside a component it becomes an input pin.' },
|
||
{ kind: 'output', name: 'Output', icon: 'lucide:log-out', key: 'O', hint: 'Shows a value. Inside a component it becomes an output pin.' },
|
||
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.' },
|
||
{ kind: 'button', name: 'Push button', icon: 'lucide:circle-dot', key: 'B', hint: '1 while held down.' },
|
||
{ kind: 'clock', name: 'Clock', icon: 'lucide:audio-waveform', key: 'C', hint: 'Square wave. Speed and pause are in the top bar.' },
|
||
{ kind: 'high', name: 'Constant 1', icon: 'lucide:plus', hint: 'Always 1, like the supply rail.' },
|
||
{ kind: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.' },
|
||
{ kind: 'hex', name: 'Hex display', icon: 'lucide:binary', key: 'H', hint: 'Shows 4 bits as 0–F.' },
|
||
{ kind: 'note', name: 'Note', icon: 'lucide:sticky-note', key: 'T', hint: 'Text label for your drawing.' },
|
||
]
|
||
|
||
const GROUP_ORDER = ['', 'Gates', 'Routing', 'Arithmetic', 'Memory', 'Demos']
|
||
|
||
const groups = computed(() => {
|
||
const q = query.value.trim().toLowerCase()
|
||
const map = new Map<string, ComponentDef[]>()
|
||
for (const d of ed.project.value.components) {
|
||
if (q && !d.name.toLowerCase().includes(q)) continue
|
||
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 }))
|
||
})
|
||
|
||
function pick(kind: NodeKind, chip?: string) {
|
||
if (chip && !ed.canPlace(chip)) return
|
||
ed.placing.value = { kind, chip }
|
||
}
|
||
|
||
function onDragStart(e: DragEvent, kind: NodeKind, chip?: string) {
|
||
if (chip && !ed.canPlace(chip)) {
|
||
e.preventDefault()
|
||
return
|
||
}
|
||
e.dataTransfer?.setData('application/x-logigate', JSON.stringify({ kind, chip }))
|
||
if (e.dataTransfer) e.dataTransfer.effectAllowed = 'copy'
|
||
}
|
||
|
||
const solved = computed(() => new Set(ed.project.value.solved))
|
||
const started = computed(() => new Map(ed.project.value.components.filter(d => d.challenge).map(d => [d.challenge!, d])))
|
||
</script>
|
||
|
||
<template>
|
||
<aside class="palette">
|
||
<div class="tabs">
|
||
<button :class="{ active: tab === 'parts' }" @click="tab = 'parts'">Parts</button>
|
||
<button :class="{ active: tab === 'challenges' }" @click="tab = 'challenges'">
|
||
Challenges
|
||
<span class="count">{{ solved.size }}/{{ CHALLENGES.length }}</span>
|
||
</button>
|
||
</div>
|
||
|
||
<div v-if="tab === 'parts'" class="scroll">
|
||
<h3>Basics</h3>
|
||
<ul class="items">
|
||
<li v-for="item in BASICS" :key="item.kind">
|
||
<button
|
||
class="item"
|
||
:class="{ active: ed.placing.value?.kind === item.kind && !ed.placing.value?.chip }"
|
||
:title="item.hint"
|
||
draggable="true"
|
||
@click="pick(item.kind)"
|
||
@dragstart="onDragStart($event, item.kind)"
|
||
>
|
||
<span v-if="item.kind === 'nand'" class="nand-badge">&</span>
|
||
<Icon v-else :name="item.icon" class="size-4 shrink-0 text-(--muted)" />
|
||
<span class="grow truncate">{{ item.name }}</span>
|
||
<kbd v-if="item.key">{{ item.key }}</kbd>
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
|
||
<div class="search">
|
||
<Icon name="lucide:search" class="size-3.5 text-(--muted)" />
|
||
<input v-model="query" placeholder="Find component" />
|
||
</div>
|
||
|
||
<template v-for="g in groups" :key="g.name">
|
||
<h3>{{ g.name }}</h3>
|
||
<ul class="items">
|
||
<li v-for="d in g.defs" :key="d.id" class="comp-row">
|
||
<button
|
||
class="item"
|
||
:class="{ active: ed.placing.value?.chip === d.id, current: d.id === ed.def.value.id, blocked: !ed.canPlace(d.id) }"
|
||
:title="d.id === ed.def.value.id ? 'You are editing this one' : !ed.canPlace(d.id) ? 'Contains the circuit you are editing' : d.description || 'Click, then click on the canvas to place'"
|
||
:draggable="ed.canPlace(d.id)"
|
||
@click="pick('chip', d.id)"
|
||
@dragstart="onDragStart($event, 'chip', d.id)"
|
||
>
|
||
<Icon :name="d.challenge && solved.has(d.challenge) ? 'lucide:badge-check' : 'lucide:box'" class="size-4 shrink-0" :class="d.challenge && solved.has(d.challenge) ? 'text-(--on)' : 'text-(--muted)'" />
|
||
<span class="grow truncate">{{ d.name }}</span>
|
||
<span v-if="d.id === ed.def.value.id" class="editing">editing</span>
|
||
</button>
|
||
<button v-if="d.id !== ed.def.value.id" class="open" title="Open and edit" @click="ed.open(d.id)">
|
||
<Icon name="lucide:pencil" class="size-3.5" />
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
</template>
|
||
|
||
<div class="grid gap-2 px-3 pt-2 pb-4">
|
||
<button class="btn w-full" @click="ed.createComponent()">
|
||
<Icon name="lucide:plus" class="size-4" /> New component
|
||
</button>
|
||
<button class="btn btn-ghost w-full" title="NOT, AND, … up to a 4-bit counter, all built from NAND" @click="ed.loadExamples()">
|
||
<Icon name="lucide:library" class="size-4" /> Add example parts
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-else class="scroll">
|
||
<p class="intro">
|
||
Build each part from NAND and the parts you already made. Start a challenge, wire it up, then press
|
||
<strong>Check</strong> on the right.
|
||
</p>
|
||
<ul class="challenges">
|
||
<li v-for="c in CHALLENGES" :key="c.id" :class="{ solved: solved.has(c.id) }">
|
||
<div class="flex items-center gap-2">
|
||
<Icon :name="solved.has(c.id) ? 'lucide:circle-check' : 'lucide:circle'" class="size-4 shrink-0" :class="solved.has(c.id) ? 'text-(--on)' : 'text-(--muted)'" />
|
||
<span class="grow font-medium">{{ c.name }}</span>
|
||
<button class="btn-sm" @click="ed.beginChallenge(c)">{{ started.has(c.id) ? 'Open' : 'Start' }}</button>
|
||
</div>
|
||
<p>{{ c.goal }}</p>
|
||
<p class="pins">{{ c.inputs.join(', ') }} → {{ c.outputs.join(', ') }}</p>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</aside>
|
||
</template>
|