429 lines
17 KiB
Vue
429 lines
17 KiB
Vue
<script setup lang="ts">
|
|
// The parts dock at the bottom, like a city builder's: a row of categories;
|
|
// pointing at one shows its parts in a strip above (scroll it sideways with
|
|
// the wheel). Click a part, then the canvas, or drag it where it goes.
|
|
// Clicking a category keeps it open. Typing searches every part.
|
|
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 { componentGroups, search } = useParts()
|
|
|
|
interface Tile {
|
|
id: string
|
|
kind: NodeKind
|
|
chip?: string
|
|
name: string
|
|
hint: string
|
|
/** Small text under the name: its key, transistors, "editing". */
|
|
meta?: string
|
|
/** Why it cannot be placed right now. */
|
|
blocked?: string
|
|
/** A component that can be opened for editing. */
|
|
edit?: boolean
|
|
}
|
|
|
|
interface Task {
|
|
id: string
|
|
name: string
|
|
hint: string
|
|
state: 'solved' | 'open' | 'locked' | 'weekly' | 'puzzle'
|
|
meta?: string
|
|
run: () => void
|
|
}
|
|
|
|
type CatId = 'search' | 'logic' | 'inputs' | 'outputs' | 'wiring' | 'components' | 'challenges'
|
|
|
|
// ── What is in each category ──────────────────────────────────────
|
|
function lockedHint(kind: NodeKind) {
|
|
const by = KIND_UNLOCK[kind]
|
|
return by ? `Solve the ${challengeName(by)} challenge to unlock this part` : ''
|
|
}
|
|
|
|
const basic = (group: string): Tile[] =>
|
|
(BASIC_GROUPS.find(g => g.name === group)?.parts ?? []).map(p => ({
|
|
id: p.kind,
|
|
kind: p.kind,
|
|
name: p.name,
|
|
hint: p.hint,
|
|
meta: p.kind === 'nand' ? '4 transistors' : p.key ? `key ${p.key}` : undefined,
|
|
blocked: ed.canPlaceKind(p.kind) ? undefined : lockedHint(p.kind),
|
|
}))
|
|
|
|
function compTile(d: ComponentDef): Tile {
|
|
const editing = d.id === ed.def.value.id
|
|
const t = ed.transistors(d.id)
|
|
let blocked: string | undefined
|
|
if (editing) blocked = 'You are editing this one'
|
|
else if (!defUsable(ed.project.value, d)) blocked = 'Solve this challenge to use it as a part'
|
|
else if (!ed.canPlace(d.id)) blocked = 'It contains the circuit you are editing'
|
|
return {
|
|
id: d.id,
|
|
kind: 'chip',
|
|
chip: d.id,
|
|
name: d.name,
|
|
hint: d.description || 'Click, then click on the canvas to place',
|
|
meta: editing ? 'editing' : t ? `${shortCount(t)} transistors` : undefined,
|
|
blocked,
|
|
edit: !editing,
|
|
}
|
|
}
|
|
|
|
/** Components drawn like gates go with NAND under Logic; the rest under Components. */
|
|
const GATE_GROUP = 'Gates'
|
|
const compTabs = computed(() => componentGroups.value.filter(g => g.name !== GATE_GROUP))
|
|
const compTab = ref('')
|
|
const compTiles = computed(() => {
|
|
const tabs = compTabs.value
|
|
const g = tabs.find(t => t.name === compTab.value) ?? tabs[0]
|
|
return g ? g.defs.map(compTile) : []
|
|
})
|
|
|
|
const tiles = computed<Record<Exclude<CatId, 'challenges' | 'search'>, Tile[]>>(() => ({
|
|
logic: [...basic('Logic'), ...(componentGroups.value.find(g => g.name === GATE_GROUP)?.defs.map(compTile) ?? [])],
|
|
inputs: basic('Inputs'),
|
|
outputs: basic('Outputs'),
|
|
wiring: [...basic('Wiring'), ...basic('Notes')],
|
|
components: compTiles.value,
|
|
}))
|
|
|
|
const CATS: { id: Exclude<CatId, 'search'>; name: string; icon: string; key?: string }[] = [
|
|
{ id: 'logic', name: 'Logic', icon: 'lucide:square-function' },
|
|
{ id: 'inputs', name: 'Inputs', icon: 'lucide:toggle-left' },
|
|
{ id: 'outputs', name: 'Outputs', icon: 'lucide:lightbulb' },
|
|
{ id: 'wiring', name: 'Wiring', icon: 'lucide:cable' },
|
|
{ id: 'components', name: 'Components', icon: 'lucide:boxes' },
|
|
{ id: 'challenges', name: 'Challenges', icon: 'lucide:target' },
|
|
]
|
|
|
|
// ── Challenges and puzzles ────────────────────────────────────────
|
|
const solved = computed(() => new Set(ed.project.value.solved))
|
|
const solvedCount = computed(() => CHALLENGES.filter(c => solved.value.has(c.id)).length)
|
|
const campaign = computed(() => isCampaign(ed.project.value))
|
|
const started = computed(() => new Map(ed.project.value.components.filter(d => d.challenge).map(d => [d.challenge!, d])))
|
|
|
|
function openWeekly() {
|
|
const spec = ed.project.value.puzzles?.find(p => p.id === weeklyId(weekKey()))
|
|
if (spec) ed.beginPuzzle(spec)
|
|
else void ed.openPuzzle('weekly')
|
|
}
|
|
|
|
const tasks = computed<Task[]>(() => {
|
|
const best = ed.project.value.best ?? {}
|
|
const out: Task[] = [
|
|
{
|
|
id: 'weekly',
|
|
name: 'Puzzle of the week',
|
|
hint: 'A new logic puzzle every Monday, the same for everyone. Fewest transistors wins.',
|
|
state: 'weekly',
|
|
meta: started.value.has(weeklyId(weekKey())) ? 'open it' : 'new',
|
|
run: openWeekly,
|
|
},
|
|
]
|
|
const puzzles = (ed.project.value.puzzles ?? []).filter(p => !p.id.startsWith('week:'))
|
|
for (const p of puzzles)
|
|
out.push({ id: p.id, name: p.name, hint: p.goal, state: solved.value.has(p.id) ? 'solved' : 'puzzle', meta: best[p.id] !== undefined ? `best ${best[p.id]}` : 'puzzle', run: () => ed.beginPuzzle(p) })
|
|
for (const c of CHALLENGES) {
|
|
const open = challengeOpen(ed.project.value, c.id) || started.value.has(c.id)
|
|
const needs = `Solve ${(NEEDS[c.id] ?? []).filter(n => !solved.value.has(n)).map(challengeName).join(' and ')} first`
|
|
out.push({
|
|
id: c.id,
|
|
name: c.name,
|
|
hint: open ? `${c.goal}\n${c.inputs.join(', ')} → ${c.outputs.join(', ')}` : needs,
|
|
state: solved.value.has(c.id) ? 'solved' : open ? 'open' : 'locked',
|
|
meta: best[c.id] !== undefined ? `best ${best[c.id]}` : open ? (started.value.has(c.id) ? 'started' : undefined) : 'locked',
|
|
run: () => ed.beginChallenge(c),
|
|
})
|
|
}
|
|
return out
|
|
})
|
|
const TASK_ICON: Record<Task['state'], string> = {
|
|
solved: 'lucide:circle-check',
|
|
open: 'lucide:circle',
|
|
locked: 'lucide:lock',
|
|
weekly: 'lucide:calendar-days',
|
|
puzzle: 'lucide:puzzle',
|
|
}
|
|
|
|
// ── Opening and closing ───────────────────────────────────────────
|
|
const shown = ref<CatId | null>(null)
|
|
const pinned = ref(false)
|
|
let closeTimer: ReturnType<typeof setTimeout> | undefined
|
|
let openTimer: ReturnType<typeof setTimeout> | undefined
|
|
|
|
function hover(id: CatId) {
|
|
clearTimeout(closeTimer)
|
|
clearTimeout(openTimer)
|
|
if (pinned.value && shown.value !== id) return
|
|
// Already open: follow the pointer at once; otherwise wait a moment, so passing over does not flash.
|
|
if (shown.value) shown.value = id
|
|
else openTimer = setTimeout(() => (shown.value = id), 90)
|
|
}
|
|
|
|
function click(id: CatId) {
|
|
clearTimeout(openTimer)
|
|
if (shown.value === id && pinned.value) return close()
|
|
shown.value = id
|
|
pinned.value = true
|
|
}
|
|
|
|
function leave() {
|
|
clearTimeout(openTimer)
|
|
if (pinned.value || ed.dragPart.value || searching.value) return
|
|
closeTimer = setTimeout(close, 280)
|
|
}
|
|
|
|
function stay() {
|
|
clearTimeout(closeTimer)
|
|
}
|
|
|
|
function close() {
|
|
clearTimeout(closeTimer)
|
|
shown.value = null
|
|
pinned.value = false
|
|
}
|
|
|
|
const searchEl = ref<HTMLInputElement>()
|
|
const query = ref('')
|
|
const focused = ref(false)
|
|
const searching = computed(() => focused.value || !!query.value.trim())
|
|
const at = ref(0)
|
|
const hits = computed<Tile[]>(() =>
|
|
search(query.value, 40).map(h => ({ id: h.chip ?? h.kind, kind: h.kind, chip: h.chip, name: h.name, hint: h.group, meta: h.key ? `key ${h.key}` : h.group })),
|
|
)
|
|
watch(query, () => {
|
|
at.value = 0
|
|
if (query.value.trim()) shown.value = 'search'
|
|
})
|
|
watch(focused, f => {
|
|
if (f) shown.value = 'search'
|
|
else if (shown.value === 'search' && !query.value.trim()) close()
|
|
})
|
|
// "/" on the canvas, or a request from elsewhere, puts the cursor here.
|
|
watch(
|
|
() => ed.partSearch.value,
|
|
() => {
|
|
ed.toggleSidebar('left', true)
|
|
nextTick(() => {
|
|
searchEl.value?.focus()
|
|
searchEl.value?.select()
|
|
})
|
|
},
|
|
)
|
|
|
|
function onSearchKey(e: KeyboardEvent) {
|
|
e.stopPropagation()
|
|
const n = hits.value.length
|
|
if (['ArrowRight', 'ArrowDown', 'ArrowLeft', 'ArrowUp'].includes(e.key)) {
|
|
e.preventDefault()
|
|
if (n) at.value = (at.value + (e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
|
nextTick(() => strip.value?.querySelector('.tile.active')?.scrollIntoView({ inline: 'nearest', block: 'nearest' }))
|
|
} else if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
const h = hits.value[at.value]
|
|
if (h) pick(h)
|
|
} else if (e.key === 'Escape') {
|
|
query.value = ''
|
|
searchEl.value?.blur()
|
|
}
|
|
}
|
|
|
|
const list = computed<Tile[]>(() => {
|
|
const s = shown.value
|
|
if (!s || s === 'challenges') return []
|
|
return s === 'search' ? hits.value : tiles.value[s]
|
|
})
|
|
|
|
// ── Using a tile ──────────────────────────────────────────────────
|
|
function pick(t: Tile) {
|
|
if (t.blocked) {
|
|
ed.play('error')
|
|
return ed.notify(t.blocked, 'error')
|
|
}
|
|
ed.placing.value = { kind: t.kind, chip: t.chip }
|
|
query.value = ''
|
|
searchEl.value?.blur()
|
|
close()
|
|
}
|
|
|
|
function onDragStart(e: DragEvent, t: Tile) {
|
|
if (t.blocked) return e.preventDefault()
|
|
e.dataTransfer?.setData('application/x-logigate', JSON.stringify({ kind: t.kind, chip: t.chip }))
|
|
if (e.dataTransfer) e.dataTransfer.effectAllowed = 'copy'
|
|
ed.dragPart.value = { kind: t.kind, chip: t.chip }
|
|
}
|
|
|
|
function onDragEnd() {
|
|
ed.dragPart.value = null
|
|
if (!pinned.value) close()
|
|
}
|
|
|
|
function runTask(t: Task) {
|
|
if (t.state === 'locked') {
|
|
ed.play('error')
|
|
return ed.notify(t.hint, 'error')
|
|
}
|
|
t.run()
|
|
close()
|
|
}
|
|
|
|
// ── Sideways scrolling ────────────────────────────────────────────
|
|
const strip = ref<HTMLElement>()
|
|
const edges = ref({ left: false, right: false })
|
|
function measure() {
|
|
const el = strip.value
|
|
if (!el) return
|
|
edges.value = { left: el.scrollLeft > 4, right: el.scrollLeft + el.clientWidth < el.scrollWidth - 4 }
|
|
}
|
|
function onWheel(e: WheelEvent) {
|
|
const el = strip.value
|
|
if (!el || Math.abs(e.deltaX) > Math.abs(e.deltaY)) return
|
|
e.preventDefault()
|
|
el.scrollLeft += e.deltaY
|
|
}
|
|
function page(dir: number) {
|
|
strip.value?.scrollBy({ left: dir * strip.value.clientWidth * 0.8, behavior: 'smooth' })
|
|
}
|
|
watch([shown, compTab, () => list.value.length], () => nextTick(measure))
|
|
|
|
const title = computed(() => {
|
|
const s = shown.value
|
|
if (s === 'search') return query.value.trim() ? `Parts matching "${query.value.trim()}"` : 'Recent and basic parts'
|
|
return CATS.find(c => c.id === s)?.name ?? ''
|
|
})
|
|
|
|
// Escape closes a strip that was clicked open.
|
|
function onKey(e: KeyboardEvent) {
|
|
if (e.key === 'Escape' && shown.value && !searching.value) close()
|
|
}
|
|
onMounted(() => window.addEventListener('keydown', onKey))
|
|
onBeforeUnmount(() => {
|
|
window.removeEventListener('keydown', onKey)
|
|
clearTimeout(closeTimer)
|
|
clearTimeout(openTimer)
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div class="dock" @pointerleave="leave" @pointerenter="stay">
|
|
<Transition name="flyout">
|
|
<section v-if="shown" class="dock-flyout" :class="{ pinned }" :aria-label="title">
|
|
<header class="flyout-head">
|
|
<span class="flyout-title">{{ title }}</span>
|
|
<template v-if="shown === 'components' && compTabs.length > 1">
|
|
<div class="flyout-tabs" role="tablist">
|
|
<button
|
|
v-for="(g, i) in compTabs"
|
|
:key="g.name"
|
|
role="tab"
|
|
:aria-selected="(compTab || compTabs[0]!.name) === g.name"
|
|
:class="{ active: (compTab || compTabs[0]!.name) === g.name || (!compTab && i === 0) }"
|
|
@click="compTab = g.name"
|
|
>
|
|
{{ g.name }} <span class="count">{{ g.defs.length }}</span>
|
|
</button>
|
|
</div>
|
|
</template>
|
|
<template v-if="shown === 'challenges'">
|
|
<span v-if="campaign" class="flyout-progress" :title="`${solvedCount} of ${CHALLENGES.length} solved`">
|
|
<span class="bar"><span :style="{ width: `${campaignProgress(ed.project.value) * 100}%` }" /></span>
|
|
{{ solvedCount }}/{{ CHALLENGES.length }}
|
|
</span>
|
|
<span v-else class="flyout-note">{{ solvedCount }}/{{ CHALLENGES.length }} solved</span>
|
|
</template>
|
|
<span v-else-if="shown === 'search'" class="flyout-note">← → choose · Enter place</span>
|
|
<span v-else class="flyout-note">Click, then click the canvas · or drag</span>
|
|
<button v-if="pinned" class="icon-btn ml-auto" title="Close (Esc)" @click="close"><Icon name="lucide:x" class="size-3.5" /></button>
|
|
</header>
|
|
|
|
<div class="strip-wrap">
|
|
<button v-if="edges.left" class="strip-arrow left" title="Scroll left" @click="page(-1)"><Icon name="lucide:chevron-left" class="size-4" /></button>
|
|
<div ref="strip" class="strip" @wheel="onWheel" @scroll="measure">
|
|
<template v-if="shown === 'challenges'">
|
|
<button
|
|
v-for="t in tasks"
|
|
:key="t.id"
|
|
class="tile task"
|
|
:class="[t.state, { current: ed.def.value.challenge === t.id || (t.id === 'weekly' && ed.def.value.challenge === weeklyId(weekKey())) }]"
|
|
:title="t.hint"
|
|
@click="runTask(t)"
|
|
>
|
|
<span class="tile-art"><Icon :name="TASK_ICON[t.state]" class="size-6" /></span>
|
|
<span class="tile-name">{{ t.name }}</span>
|
|
<span v-if="t.meta" class="tile-meta">{{ t.meta }}</span>
|
|
</button>
|
|
</template>
|
|
<template v-else>
|
|
<div v-for="(t, i) in list" :key="t.id" class="tile-slot">
|
|
<button
|
|
class="tile"
|
|
:class="{ active: shown === 'search' && i === at, blocked: !!t.blocked, placing: ed.placing.value?.kind === t.kind && ed.placing.value?.chip === t.chip }"
|
|
:title="t.blocked || t.hint"
|
|
:draggable="!t.blocked"
|
|
@click="pick(t)"
|
|
@dragstart="onDragStart($event, t)"
|
|
@dragend="onDragEnd"
|
|
@pointerenter="shown === 'search' && (at = i)"
|
|
>
|
|
<span class="tile-art"><EditorPartThumb :kind="t.kind" :chip="t.chip" /></span>
|
|
<span class="tile-name">{{ t.name }}</span>
|
|
<span v-if="t.meta" class="tile-meta">{{ t.meta }}</span>
|
|
<Icon v-if="t.blocked && !t.meta?.startsWith('editing')" name="lucide:lock" class="tile-lock size-3.5" />
|
|
</button>
|
|
<button v-if="t.edit && t.chip" class="tile-edit" title="Open and edit" @click="ed.open(t.chip!); close()">
|
|
<Icon name="lucide:pencil" class="size-3" />
|
|
</button>
|
|
</div>
|
|
<template v-if="shown === 'components'">
|
|
<button class="tile add" title="An empty component to build; its inputs and outputs become its pins" @click="ed.createComponent(); close()">
|
|
<span class="tile-art"><Icon name="lucide:plus" class="size-6" /></span>
|
|
<span class="tile-name">New component</span>
|
|
</button>
|
|
<button class="tile add" title="NOT, AND, … up to a 4-bit counter, all built from NAND" @click="ed.loadExamples(); close()">
|
|
<span class="tile-art"><Icon name="lucide:library" class="size-6" /></span>
|
|
<span class="tile-name">Example parts</span>
|
|
</button>
|
|
</template>
|
|
<p v-if="!list.length && shown === 'search'" class="strip-empty">Nothing called "{{ query.trim() }}" fits here.</p>
|
|
</template>
|
|
</div>
|
|
<button v-if="edges.right" class="strip-arrow right" title="Scroll right" @click="page(1)"><Icon name="lucide:chevron-right" class="size-4" /></button>
|
|
</div>
|
|
</section>
|
|
</Transition>
|
|
|
|
<nav class="dock-bar" aria-label="Parts">
|
|
<label class="dock-search" :class="{ open: searching }">
|
|
<Icon name="lucide:search" class="size-4 shrink-0" />
|
|
<input
|
|
ref="searchEl"
|
|
v-model="query"
|
|
placeholder="Find a part"
|
|
@focus="focused = true"
|
|
@blur="focused = false"
|
|
@keydown="onSearchKey"
|
|
/>
|
|
<kbd v-if="!searching">/</kbd>
|
|
</label>
|
|
<span class="dock-sep" />
|
|
<button
|
|
v-for="c in CATS"
|
|
:key="c.id"
|
|
class="dock-cat"
|
|
:class="{ active: shown === c.id, task: c.id === 'challenges' && !!ed.task.value }"
|
|
:aria-expanded="shown === c.id"
|
|
@pointerenter="hover(c.id)"
|
|
@click="click(c.id)"
|
|
>
|
|
<Icon :name="c.icon" class="size-5" />
|
|
<span>{{ c.name }}</span>
|
|
<span v-if="c.id === 'challenges'" class="dock-count">{{ solvedCount }}/{{ CHALLENGES.length }}</span>
|
|
</button>
|
|
</nav>
|
|
</div>
|
|
</template>
|