logigate/app/components/editor/Toolbar.vue

149 lines
7.4 KiB
Vue
Raw Normal View History

<script setup lang="ts">
// The bar along the top, floating like the parts bar below: menu and where
// you are on the left, the clock in the middle, the rest on the right.
import { CHALLENGES } from '~/lib/logic/challenges'
const ed = useEditor()
const SPEEDS = [
{ value: 0.5, label: '0.5 Hz' },
{ value: 1, label: '1 Hz' },
{ value: 2, label: '2 Hz' },
{ value: 5, label: '5 Hz' },
{ value: 10, label: '10 Hz' },
{ value: 30, label: '30 Hz' },
{ value: 0, label: 'Max' },
]
const hasClock = computed(() => (ed.sim.value?.nl.clocks.length ?? 0) > 0)
const statusText = computed(() => ({ stable: 'Stable', busy: 'Settling', oscillating: 'Oscillating' })[ed.status.value])
// The way into the open circuit: Main, then each component entered on the way.
const path = computed(() =>
[...ed.nav.value.map((id, i) => ({ id, i })), { id: ed.def.value.id, i: -1 }]
.filter(p => p.id !== ed.mainId.value)
.map(p => ({ ...p, name: ed.getDef.value(p.id)?.name ?? '?' })),
)
const atMain = computed(() => ed.def.value.id === ed.mainId.value)
// Challenges and puzzles open below their button.
const showChallenges = ed.challengesOpen
const challengesEl = ref<HTMLElement>()
const solvedCount = computed(() => CHALLENGES.filter(c => ed.project.value.solved.includes(c.id)).length)
function outside(e: PointerEvent) {
if (showChallenges.value && !challengesEl.value?.contains(e.target as Node)) showChallenges.value = false
}
onMounted(() => window.addEventListener('pointerdown', outside, true))
onBeforeUnmount(() => window.removeEventListener('pointerdown', outside, true))
const cloudText = computed(() => ({ off: '', syncing: 'syncing', synced: 'all saved', offline: 'offline', error: 'sync failed' })[ed.cloudState.value])
const linkIcon = computed(() => {
switch (ed.linkState.value) {
case 'saved':
return 'lucide:cloud-check'
case 'saving':
case 'dirty':
return 'lucide:cloud-upload'
case 'error':
return 'lucide:cloud-alert'
default:
return 'lucide:share-2'
}
})
</script>
<template>
<div class="top-dock">
<header class="top-bar">
<div class="tb-group min-w-0">
<button class="bar-btn brand-btn" title="Menu: save, load, settings, main menu (Esc)" @click="ed.pause()">
<svg viewBox="0 0 32 32" class="size-5" aria-hidden="true">
<path d="M6 7h10a9 9 0 0 1 0 18H6Z" fill="none" stroke="currentColor" stroke-width="2.5" />
<circle cx="27" cy="16" r="2.6" fill="none" stroke="currentColor" stroke-width="2.2" />
</svg>
<span class="project-name truncate">{{ ed.project.value.name }}</span>
</button>
<span class="bar-sep" />
<nav class="crumbs" aria-label="Where you are">
<button class="bar-btn" :class="{ here: atMain }" :disabled="atMain" title="Go to Main (components open with a double-click)" @click="ed.goMain()">
<Icon name="lucide:house" class="size-4" />
<span>Main</span>
</button>
<template v-for="p in path" :key="`${p.id}-${p.i}`">
<Icon name="lucide:chevron-right" class="crumb-sep size-3.5" />
<button class="bar-btn crumb" :class="{ here: p.i < 0 }" :disabled="p.i < 0" :title="p.i < 0 ? 'Editing this one' : `Back to ${p.name}`" @click="ed.goTo(p.i)">
<span class="truncate">{{ p.name }}</span>
</button>
</template>
</nav>
</div>
<div class="tb-group">
<div ref="challengesEl" class="relative">
<button class="bar-btn" :class="{ open: showChallenges }" title="Challenges, puzzles and the puzzle of the week" @click="showChallenges = !showChallenges">
<Icon name="lucide:flag" class="size-4" />
<span class="bar-text">Challenges</span>
<span class="bar-count">{{ solvedCount }}/{{ CHALLENGES.length }}</span>
</button>
<Transition name="drop">
<div v-if="showChallenges" class="top-pop">
<EditorChallengesPanel @close="showChallenges = false" />
</div>
</Transition>
</div>
<span class="bar-sep" />
<button
class="bar-btn"
:class="{ active: ed.running.value && hasClock }"
:title="ed.running.value ? 'Pause the clock' : 'Run the clock'"
:disabled="!hasClock"
@click="ed.running.value = !ed.running.value"
>
<Icon :name="ed.running.value ? 'lucide:pause' : 'lucide:play'" class="size-4" />
</button>
<button class="bar-btn" title="One clock cycle" :disabled="!hasClock" @click="ed.running.value = false; ed.step()">
<Icon name="lucide:step-forward" class="size-4" />
</button>
<select v-model.number="ed.hz.value" class="tb-select" title="Clock speed" :disabled="!hasClock">
<option v-for="s in SPEEDS" :key="s.value" :value="s.value">{{ s.label }}</option>
</select>
<span v-if="hasClock" class="cycles tabular-nums" title="Clock cycles since this circuit was opened">{{ ed.cycles.value.toLocaleString() }} ticks</span>
<span class="status" :class="ed.status.value" :title="ed.status.value === 'oscillating' ? 'A loop keeps flipping and never settles (e.g. a NOT feeding itself)' : 'Simulation state'">
<span class="dot" />{{ statusText }}
</span>
</div>
<div class="tb-group">
<button class="bar-btn" title="Find anything in the project (Ctrl+F)" @click="ed.dialog.value = 'search'"><Icon name="lucide:search" class="size-4" /></button>
<button class="bar-btn" title="Undo (Ctrl+Z)" :disabled="!ed.canUndo.value" @click="ed.undo()"><Icon name="lucide:undo-2" class="size-4" /></button>
<button class="bar-btn" title="Redo (Ctrl+Shift+Z)" :disabled="!ed.canRedo.value" @click="ed.redo()"><Icon name="lucide:redo-2" class="size-4" /></button>
<button class="bar-btn" :title="ed.sound.value.on ? 'Sound on (volume and clock ticks in Settings)' : 'Sound off'" @click="ed.setSound({ on: !ed.sound.value.on })">
<Icon :name="ed.sound.value.on ? 'lucide:volume-2' : 'lucide:volume-x'" class="size-4" />
</button>
<button class="bar-btn" title="Settings: gate symbols, wire crossings, current flow, tidy wiring, sound" @click="ed.dialog.value = 'settings'">
<Icon name="lucide:settings" class="size-4" />
</button>
<button class="bar-btn" title="Help (?)" @click="ed.showHelp.value = true"><Icon name="lucide:circle-help" class="size-4" /></button>
<span class="bar-sep" />
<button class="bar-btn share-btn" :class="ed.linkState.value" title="Save online, share a link, export or import a file" @click="ed.dialog.value = 'share'">
<Icon :name="linkIcon" class="size-4" />
<span class="bar-text">Share</span>
</button>
<button
class="bar-btn account-btn"
:class="ed.cloudState.value"
:title="ed.account.value ? `${ed.account.value.email}: ${cloudText}` : 'Sign in to keep your projects on every computer'"
@click="ed.dialog.value = 'account'"
>
<template v-if="ed.account.value">
<span class="avatar">{{ ed.account.value.email[0]!.toUpperCase() }}</span>
<span class="sync-dot" />
</template>
<template v-else>
<Icon name="lucide:user" class="size-4" />
<span class="bar-text">Sign in</span>
</template>
</button>
</div>
</header>
</div>
</template>