Procedural, game-ready vehicles for three.js: 13 body styles, blending, seeded random vehicles, three LODs, colliders, 13 parametric mods, paint and livery, a rig that drives, and GLB export that a game can load and drive. Ships with a studio (browser or Tauri desktop app) and a Play mode.
110 lines
6.7 KiB
TypeScript
110 lines
6.7 KiB
TypeScript
/** The top bar: menus, command search, Drive and the Export button. */
|
|
import { showAbout } from './about.ts'
|
|
import mark from './assets/mark.png'
|
|
import { stage, state } from './context.ts'
|
|
import { edit, future, nextSeed, past, randomise, redo, resetToPreset, undo } from './editing.ts'
|
|
import { exportButton, exportTarget, openExport, runExport, TARGETS } from './exporting.ts'
|
|
import { deleteEntry, deleteLibrary, duplicate, exportLibrary, importLibrary, newLibrary, renameLibrary, renderGarage } from './garage.ts'
|
|
import { icon, ICON } from './icons.ts'
|
|
import { menubar } from './layout.ts'
|
|
import { openPalette } from './palette.ts'
|
|
import { enterDrive } from './play.ts'
|
|
import { openNew } from './popovers.ts'
|
|
import { renderAll } from './render.ts'
|
|
import { setCollider, setLod, setWireframe, viewLabel } from './toolbar.ts'
|
|
import { dialog, el, type MenuEntry, showMenu } from './ui.ts'
|
|
import { decorateMenubar, isDesktop, quitApp } from './windowbar.ts'
|
|
|
|
export function menus(): [string, () => MenuEntry[]][] {
|
|
return [
|
|
['File', () => [
|
|
{ label: 'New vehicle…', kbd: 'N', run: () => openNew() },
|
|
{ label: 'Duplicate vehicle', kbd: 'Ctrl+D', disabled: !state.entryId, run: () => void duplicate(state.entryId) },
|
|
'separator',
|
|
{ label: 'Export…', kbd: 'Ctrl+Shift+E', run: () => openExport() },
|
|
{ label: `Export again (${TARGETS[exportTarget].label})`, kbd: 'Ctrl+E', run: () => void runExport(exportTarget) },
|
|
'separator',
|
|
{ label: 'New collection…', run: () => void newLibrary() },
|
|
{ label: 'Rename collection…', run: () => void renameLibrary() },
|
|
{ label: 'Import collection or spec JSON…', run: () => void importLibrary() },
|
|
{ label: 'Export collection JSON', run: () => exportLibrary() },
|
|
'separator',
|
|
{ label: 'Delete collection…', danger: true, disabled: state.libraries.length <= 1, run: () => deleteLibrary() },
|
|
...(isDesktop() ? ['separator' as const, { label: 'Quit', kbd: 'Alt+F4', run: () => void quitApp() }] : []),
|
|
]],
|
|
['Edit', () => [
|
|
{ label: 'Undo', kbd: 'Ctrl+Z', disabled: past.length === 0, run: () => undo() },
|
|
{ label: 'Redo', kbd: 'Ctrl+Shift+Z', disabled: future.length === 0, run: () => redo() },
|
|
'separator',
|
|
{ label: 'Rename vehicle', kbd: 'F2', disabled: !state.entryId, run: () => { state.renaming = state.entryId; renderGarage() } },
|
|
{ label: 'Delete vehicle', kbd: 'Del', danger: true, disabled: !state.entryId, run: () => void deleteEntry(state.entryId) },
|
|
]],
|
|
['Vehicle', () => [
|
|
{ label: 'Randomise', kbd: 'R', run: () => randomise(state.randomStyle ? undefined : state.spec.style) },
|
|
{ label: 'Randomise any style', kbd: 'Shift+R', run: () => randomise() },
|
|
{ label: 'Next seed', kbd: '.', run: () => nextSeed() },
|
|
'separator',
|
|
{ label: 'Reset to preset', run: () => resetToPreset() },
|
|
{ label: 'Remove all mods', run: () => { edit((s) => ({ ...s, mods: [] })); renderAll() } },
|
|
'separator',
|
|
{ label: 'Drive on the circuit', kbd: 'P', run: () => void enterDrive('circuit') },
|
|
{ label: 'Drive in traffic', run: () => void enterDrive('traffic') },
|
|
]],
|
|
['View', () => [
|
|
{ label: 'Wireframe', kbd: 'W', checked: stage.wireframe, run: () => setWireframe(!stage.wireframe) },
|
|
{ label: 'Collider hull', kbd: 'C', checked: stage.showCollider, run: () => setCollider(!stage.showCollider) },
|
|
{ label: 'Ground grid', kbd: 'G', checked: stage.grid, run: () => { stage.grid = !stage.grid } },
|
|
{ label: 'Auto-rotate', kbd: 'O', checked: stage.controls.autoRotate, run: () => { stage.controls.autoRotate = !stage.controls.autoRotate } },
|
|
'separator',
|
|
...(['auto', 0, 1, 2] as const).map((mode, i): MenuEntry => ({
|
|
label: mode === 'auto' ? 'LOD: automatic' : `LOD${mode}`, kbd: String(i === 0 ? '`' : i), checked: stage.lodMode === mode, run: () => setLod(mode),
|
|
})),
|
|
'separator',
|
|
{ label: 'Frame vehicle', kbd: 'F', run: () => stage.frame() },
|
|
...(['side', 'front34', 'rear34', 'front', 'rear', 'top'] as const).map((v): MenuEntry => ({ label: `Camera: ${viewLabel(v)}`, run: () => stage.look(v) })),
|
|
]],
|
|
['Help', () => [
|
|
{ label: 'Search commands', kbd: 'F3', run: () => openPalette() },
|
|
{ label: 'Keyboard shortcuts', kbd: '?', run: () => showShortcuts() },
|
|
'separator',
|
|
{ label: 'About Vehicle Generator', run: () => showAbout() },
|
|
]],
|
|
]
|
|
}
|
|
|
|
export function renderMenubar(): void {
|
|
menubar.replaceChildren(el('div', { class: 'logo' }, el('img', { src: mark, alt: '', width: 22, height: 22 }), el('b', {}, 'VEHICLE', el('em', {}, 'GEN'))))
|
|
for (const [label, entries] of menus()) {
|
|
const b = el('button', { type: 'button', class: 'menu-button' }, label)
|
|
b.addEventListener('click', () => {
|
|
const r = b.getBoundingClientRect()
|
|
b.classList.add('open')
|
|
showMenu(r.left, r.bottom + 4, entries())
|
|
const off = (): void => { b.classList.remove('open'); window.removeEventListener('pointerdown', off, true) }
|
|
setTimeout(() => window.addEventListener('pointerdown', off, true))
|
|
})
|
|
menubar.append(b)
|
|
}
|
|
const search = el('button', { type: 'button', class: 'search' }, icon(ICON.search), el('span', {}, 'Search anything…'), el('kbd', {}, 'F3'))
|
|
search.addEventListener('click', () => openPalette())
|
|
const drive = el('button', { type: 'button', class: 'ghost' }, icon(ICON.play, 12), 'Drive')
|
|
drive.addEventListener('click', () => void enterDrive('circuit'))
|
|
menubar.append(el('div', { class: 'spacer' }), search, el('div', { class: 'spacer' }), drive, exportButton())
|
|
// On the desktop the bar is also the title bar: put its drag layer and caption buttons back.
|
|
if (windowReady) decorateMenubar(menubar)
|
|
}
|
|
export let windowReady = false
|
|
|
|
export function showShortcuts(): void {
|
|
const rows: [string, string][] = [
|
|
['Search anything', 'F3'], ['New vehicle', 'N'], ['Randomise (same style)', 'R'], ['Randomise any style', 'Shift+R'],
|
|
['Next seed', '.'], ['Undo / redo', 'Ctrl+Z / Ctrl+Shift+Z'], ['Duplicate', 'Ctrl+D'], ['Rename / delete', 'F2 / Del'],
|
|
['Export again', 'Ctrl+E'], ['Export options', 'Ctrl+Shift+E'], ['Part up / down', '↑ ↓'], ['Variant', '← →'],
|
|
['Wireframe / collider / grid', 'W / C / G'], ['Auto-rotate', 'O'], ['LOD auto / 0 / 1 / 2', '` 1 2 3'], ['Frame vehicle', 'F'],
|
|
['Headlights / brake', 'L / B'], ['Roll the wheels', 'Space'], ['Drive', 'P'],
|
|
]
|
|
dialog('Keyboard shortcuts', el('div', { class: 'shortcuts' }, ...rows.map(([a, k]) => el('div', {}, el('span', {}, a), el('kbd', {}, k)))), [{ label: 'Close', primary: true }])
|
|
}
|
|
|
|
/** The desktop title bar is mounted: later rebuilds of the bar decorate it again. */
|
|
export function markWindowReady(): void { windowReady = true }
|