318 lines
14 KiB
Vue
318 lines
14 KiB
Vue
<script setup lang="ts">
|
|
import { shortCount } from '~/lib/logic/analyze'
|
|
import { CHALLENGES } from '~/lib/logic/challenges'
|
|
import { campaignProgress, challengeName, challengeOpen, defUsable, isCampaign, KIND_UNLOCK, NEEDS } from '~/lib/logic/progress'
|
|
import { weekKey, weeklyId } from '~/lib/logic/puzzles'
|
|
import { BASIC_GROUPS } from '~/composables/useParts'
|
|
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
|
|
|
|
const ed = useEditor()
|
|
const tab = ref<'parts' | 'challenges'>('parts')
|
|
const query = ref('')
|
|
|
|
const { componentGroups, search, recentParts } = useParts()
|
|
|
|
// ── Search: type to narrow everything down; arrows pick, Enter places ──
|
|
const searchInput = ref<HTMLInputElement>()
|
|
const hits = computed(() => (query.value.trim() ? search(query.value, 30) : []))
|
|
const at = ref(0)
|
|
watch(query, () => (at.value = 0))
|
|
watch(
|
|
() => ed.partSearch.value,
|
|
() => {
|
|
tab.value = 'parts'
|
|
nextTick(() => {
|
|
searchInput.value?.focus()
|
|
searchInput.value?.select()
|
|
})
|
|
},
|
|
)
|
|
|
|
function onSearchKey(e: KeyboardEvent) {
|
|
const n = hits.value.length
|
|
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
e.preventDefault()
|
|
if (n) at.value = (at.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
|
} else if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
const h = hits.value[at.value]
|
|
if (h) {
|
|
pick(h.kind, h.chip)
|
|
query.value = ''
|
|
searchInput.value?.blur()
|
|
}
|
|
} else if (e.key === 'Escape') {
|
|
query.value = ''
|
|
searchInput.value?.blur()
|
|
}
|
|
}
|
|
|
|
// ── Groups that can be folded away; remembered in this browser ──
|
|
const FOLD_KEY = 'logigate:palette-folded'
|
|
const folded = ref<Set<string>>(
|
|
(() => {
|
|
try {
|
|
return new Set<string>(JSON.parse(localStorage.getItem(FOLD_KEY) ?? '[]'))
|
|
} catch {
|
|
return new Set<string>()
|
|
}
|
|
})(),
|
|
)
|
|
function fold(name: string) {
|
|
const next = new Set(folded.value)
|
|
if (!next.delete(name)) next.add(name)
|
|
folded.value = next
|
|
try {
|
|
localStorage.setItem(FOLD_KEY, JSON.stringify([...next]))
|
|
} catch {}
|
|
}
|
|
|
|
function pick(kind: NodeKind, chip?: string) {
|
|
if (chip && !ed.canPlace(chip)) return
|
|
if (!ed.canPlaceKind(kind)) {
|
|
ed.play('error')
|
|
return ed.notify(lockedHint(kind), 'error')
|
|
}
|
|
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'
|
|
ed.dragPart.value = { kind, chip }
|
|
}
|
|
|
|
const solved = computed(() => new Set(ed.project.value.solved))
|
|
const best = computed(() => ed.project.value.best ?? {})
|
|
const solvedCount = computed(() => CHALLENGES.filter(c => solved.value.has(c.id)).length)
|
|
const campaign = computed(() => isCampaign(ed.project.value))
|
|
const progress = computed(() => campaignProgress(ed.project.value))
|
|
const open = (id: string) => challengeOpen(ed.project.value, id)
|
|
const needsText = (id: string) => `Solve ${(NEEDS[id] ?? []).filter(n => !solved.value.has(n)).map(challengeName).join(' and ')} first`
|
|
const puzzles = computed(() => (ed.project.value.puzzles ?? []).filter(p => !p.id.startsWith('week:') || p.id === weeklyId(weekKey())))
|
|
const weeklyStarted = computed(() => ed.project.value.components.some(d => d.challenge === weeklyId(weekKey())))
|
|
|
|
function lockedHint(kind: NodeKind) {
|
|
const by = KIND_UNLOCK[kind]
|
|
return by ? `Solve the ${challengeName(by)} challenge to unlock this part` : ''
|
|
}
|
|
|
|
function openWeekly() {
|
|
const spec = ed.project.value.puzzles?.find(p => p.id === weeklyId(weekKey()))
|
|
if (spec) ed.beginPuzzle(spec)
|
|
else void ed.openPuzzle('weekly')
|
|
}
|
|
|
|
function compTitle(d: ComponentDef) {
|
|
if (d.id === ed.def.value.id) return 'You are editing this one'
|
|
if (!defUsable(ed.project.value, d)) return 'Solve this challenge to use it as a part'
|
|
if (!ed.canPlace(d.id)) return 'Contains the circuit you are editing'
|
|
return d.description || 'Click, then click on the canvas to place'
|
|
}
|
|
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">{{ solvedCount }}/{{ CHALLENGES.length }}</span>
|
|
</button>
|
|
<button class="collapse-btn" title="Hide this panel ([)" @click="ed.toggleSidebar('left', false)">
|
|
<Icon name="lucide:panel-left-close" class="size-4" />
|
|
</button>
|
|
</div>
|
|
|
|
<div v-if="tab === 'parts'" class="scroll">
|
|
<div class="search part-search">
|
|
<Icon name="lucide:search" class="size-3.5 text-(--muted)" />
|
|
<input ref="searchInput" v-model="query" placeholder="Find a part" @keydown="onSearchKey" />
|
|
<kbd v-if="!query" title="Press / on the canvas to search, Shift+A to add at the mouse">/</kbd>
|
|
<button v-else class="icon-btn" title="Clear (Esc)" @click="query = ''"><Icon name="lucide:x" class="size-3.5" /></button>
|
|
</div>
|
|
|
|
<!-- Searching: one list of everything that matches -->
|
|
<template v-if="query.trim()">
|
|
<p v-if="!hits.length" class="hint px-3 pt-3">Nothing called "{{ query.trim() }}" fits here.</p>
|
|
<ul class="items mt-2">
|
|
<li v-for="(h, i) in hits" :key="h.chip ?? h.kind">
|
|
<button
|
|
class="item"
|
|
:class="{ active: i === at }"
|
|
:draggable="true"
|
|
@click="pick(h.kind, h.chip)"
|
|
@dragstart="onDragStart($event, h.kind, h.chip)"
|
|
@dragend="ed.dragPart.value = null"
|
|
@pointerenter="at = i"
|
|
>
|
|
<span v-if="h.kind === 'nand'" class="nand-badge">&</span>
|
|
<Icon v-else :name="h.icon" class="size-4 shrink-0 text-(--muted)" />
|
|
<span class="grow truncate">{{ h.name }}</span>
|
|
<span class="group-tag">{{ h.group }}</span>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
<p class="hint px-3 pt-2">↑ ↓ to choose, Enter to place, Esc to clear.</p>
|
|
</template>
|
|
|
|
<template v-else>
|
|
<template v-if="recentParts.length">
|
|
<h3>Recent</h3>
|
|
<div class="recent-parts">
|
|
<button
|
|
v-for="r in recentParts"
|
|
:key="r.chip ?? r.kind"
|
|
class="recent-chip"
|
|
:title="`Place ${r.name}`"
|
|
draggable="true"
|
|
@click="pick(r.kind, r.chip)"
|
|
@dragstart="onDragStart($event, r.kind, r.chip)"
|
|
@dragend="ed.dragPart.value = null"
|
|
>
|
|
<Icon :name="r.icon" class="size-3.5 shrink-0" />
|
|
<span class="truncate">{{ r.name }}</span>
|
|
</button>
|
|
</div>
|
|
</template>
|
|
|
|
<template v-for="g in BASIC_GROUPS" :key="g.name">
|
|
<button class="group-head" @click="fold(g.name)">
|
|
<Icon :name="folded.has(g.name) ? 'lucide:chevron-right' : 'lucide:chevron-down'" class="size-3.5" />
|
|
<span>{{ g.name }}</span>
|
|
</button>
|
|
<ul v-if="!folded.has(g.name)" class="items">
|
|
<li v-for="item in g.parts" :key="item.kind">
|
|
<button
|
|
class="item"
|
|
:class="{ active: ed.placing.value?.kind === item.kind && !ed.placing.value?.chip, blocked: !ed.canPlaceKind(item.kind) }"
|
|
:title="ed.canPlaceKind(item.kind) ? item.hint : lockedHint(item.kind)"
|
|
:draggable="ed.canPlaceKind(item.kind)"
|
|
@click="pick(item.kind)"
|
|
@dragstart="onDragStart($event, item.kind)"
|
|
@dragend="ed.dragPart.value = null"
|
|
>
|
|
<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>
|
|
<span v-if="item.kind === 'nand'" class="tcount" title="4 transistors: a CMOS NAND gate">4T</span>
|
|
<Icon v-if="!ed.canPlaceKind(item.kind)" name="lucide:lock" class="size-3.5 shrink-0 text-(--muted)" />
|
|
<kbd v-else-if="item.key">{{ item.key }}</kbd>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
|
|
<template v-for="g in componentGroups" :key="g.name">
|
|
<button class="group-head" @click="fold(`c:${g.name}`)">
|
|
<Icon :name="folded.has(`c:${g.name}`) ? 'lucide:chevron-right' : 'lucide:chevron-down'" class="size-3.5" />
|
|
<span>{{ g.name }}</span>
|
|
<span class="group-count">{{ g.defs.length }}</span>
|
|
</button>
|
|
<ul v-if="!folded.has(`c:${g.name}`)" 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="compTitle(d)"
|
|
:draggable="ed.canPlace(d.id)"
|
|
@click="pick('chip', d.id)"
|
|
@dragstart="onDragStart($event, 'chip', d.id)"
|
|
@dragend="ed.dragPart.value = null"
|
|
>
|
|
<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>
|
|
<span v-else-if="ed.transistors(d.id)" class="tcount" :title="`${ed.transistors(d.id).toLocaleString()} transistors`">{{ shortCount(ed.transistors(d.id)) }}T</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-3 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>
|
|
</template>
|
|
</div>
|
|
|
|
<div v-else class="scroll">
|
|
<div v-if="campaign" class="campaign">
|
|
<div class="flex items-center justify-between">
|
|
<span class="font-semibold">Campaign</span>
|
|
<span class="tabular-nums text-(--muted)">{{ solvedCount }} / {{ CHALLENGES.length }}</span>
|
|
</div>
|
|
<div class="bar"><span :style="{ width: `${progress * 100}%` }" /></div>
|
|
<p>Every part you solve becomes yours to use. New parts and challenges open as you go.</p>
|
|
</div>
|
|
<p v-else 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>
|
|
|
|
<div class="weekly">
|
|
<div class="flex items-center gap-2">
|
|
<Icon name="lucide:calendar-days" class="size-4 shrink-0 text-(--accent)" />
|
|
<span class="grow font-medium">Puzzle of the week</span>
|
|
<button class="btn-sm" @click="openWeekly">{{ weeklyStarted ? 'Open' : 'Play' }}</button>
|
|
</div>
|
|
<p>A new logic puzzle every Monday, the same for everyone. Fewest transistors wins.</p>
|
|
</div>
|
|
|
|
<template v-if="puzzles.length">
|
|
<h3>Puzzles</h3>
|
|
<ul class="challenges">
|
|
<li v-for="p in puzzles" :key="p.id" :class="{ solved: solved.has(p.id) }">
|
|
<div class="flex items-center gap-2">
|
|
<Icon :name="solved.has(p.id) ? 'lucide:circle-check' : 'lucide:puzzle'" class="size-4 shrink-0" :class="solved.has(p.id) ? 'text-(--on)' : 'text-(--muted)'" />
|
|
<span class="grow font-medium">{{ p.name }}</span>
|
|
<button class="btn-sm" @click="ed.beginPuzzle(p)">Open</button>
|
|
</div>
|
|
<p class="pins">
|
|
{{ p.inputs.join(', ') }} → {{ p.outputs.join(', ') }}
|
|
<span v-if="best[p.id] !== undefined" class="best"> · best {{ best[p.id]!.toLocaleString() }} transistors</span>
|
|
</p>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
|
|
<h3>Challenges</h3>
|
|
<ul class="challenges">
|
|
<li v-for="c in CHALLENGES" :key="c.id" :class="{ solved: solved.has(c.id), locked: !open(c.id) }">
|
|
<div class="flex items-center gap-2">
|
|
<Icon
|
|
:name="solved.has(c.id) ? 'lucide:circle-check' : open(c.id) ? 'lucide:circle' : 'lucide:lock'"
|
|
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" :disabled="!open(c.id) && !started.has(c.id)" :title="open(c.id) ? '' : needsText(c.id)" @click="ed.beginChallenge(c)">
|
|
{{ started.has(c.id) ? 'Open' : 'Start' }}
|
|
</button>
|
|
</div>
|
|
<p v-if="!open(c.id)" class="needs">{{ needsText(c.id) }}</p>
|
|
<p>{{ c.goal }}</p>
|
|
<p class="pins">
|
|
{{ c.inputs.join(', ') }} → {{ c.outputs.join(', ') }}
|
|
<span v-if="best[c.id] !== undefined" class="best" title="Fewest transistors in a passing solution so far">
|
|
· best {{ best[c.id]!.toLocaleString() }} transistors
|
|
</span>
|
|
</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</aside>
|
|
</template>
|