logigate/app/components/editor/FaceDialog.vue

400 lines
15 KiB
Vue
Raw Normal View History

<script setup lang="ts">
// Drawing a component's box by hand: drag its lamps, displays, screen and
// name where they belong, draw lines, boxes, circles and text on it, size
// it. Everything shows live on the box, and on every copy placed.
import { GRID as G } from '~/lib/logic/geometry'
import { uid } from '~/lib/logic/ops'
import { FACE_INKS, type CircuitNode, type FaceDesign, type FaceInk, type FaceShape } from '~/lib/logic/types'
const ed = useEditor()
const def = computed(() => (ed.faceFor.value ? ed.getDef.value(ed.faceFor.value) : undefined))
const preview = computed<CircuitNode>(() => ({ id: 'face-preview', kind: 'chip', chip: def.value?.id, x: 0, y: 0 }))
const shape = computed(() => ed.shape(preview.value))
const body = computed(() => shape.value.body)
type Tool = 'move' | 'line' | 'rect' | 'ellipse' | 'text'
const TOOLS: { id: Tool; icon: string; title: string }[] = [
{ id: 'move', icon: 'lucide:mouse-pointer-2', title: 'Move and resize' },
{ id: 'line', icon: 'lucide:minus', title: 'Line' },
{ id: 'rect', icon: 'lucide:square', title: 'Box' },
{ id: 'ellipse', icon: 'lucide:circle', title: 'Circle' },
{ id: 'text', icon: 'lucide:type', title: 'Text' },
]
const INK_HEX: Record<FaceInk, string> = { muted: '#6b778a', text: '#dbe2ea', red: '#ff5c5c', yellow: '#ffd34d', green: '#3ddc84', blue: '#4dabff' }
const tool = ref<Tool>('move')
const ink = ref<FaceInk>('muted')
const fill = ref(false)
/** What is selected: a drawing's id, a lamp or display node id, 'screen' or 'name'. */
const sel = ref<string | null>(null)
const VIEW_W = 700
const VIEW_H = 420
const PAD = 30
const k = computed(() => Math.min(3, (VIEW_W - 2 * PAD) / (Math.max(body.value.w, 4) * G), (VIEW_H - 2 * PAD) / (Math.max(body.value.h, 3) * G)))
const svg = ref<SVGSVGElement>()
/** The design being edited, made from the automatic layout on the first change, so nothing jumps. */
function design(): FaceDesign {
const d = def.value!
if (!d.design) {
const s = shape.value
const place: Record<string, { x: number; y: number }> = {}
for (const it of s.face ?? []) place[it.kind === 'screen' ? 'screen' : it.id] = { x: it.x, y: it.y - s.body.y }
d.design = { w: s.body.w, h: s.body.h, place, shapes: [] }
}
d.design.place ??= {}
d.design.shapes ??= []
return d.design
}
const snap = (v: number) => Math.round(v * 4) / 4
/** Pointer position in grid units from the box's top left corner. */
function at(e: PointerEvent) {
const r = svg.value!.getBoundingClientRect()
return { x: (e.clientX - r.left - PAD) / (G * k.value), y: (e.clientY - r.top - PAD) / (G * k.value) }
}
const nameText = computed(() => def.value?.name ?? '')
/** Everything that can be grabbed on the box, in grid units from its top left corner. */
const things = computed(() => {
const top = body.value.y
const out: { id: string; label: string; x: number; y: number; w: number; h: number }[] = []
for (const it of shape.value.face ?? []) {
out.push({ id: it.kind === 'screen' ? 'screen' : it.id, label: it.label || it.kind, x: it.x, y: it.y - top, w: it.w, h: it.h })
}
if (shape.value.nameAt !== null) {
const c = shape.value.nameAt ?? { x: body.value.w / 2, y: shape.value.nameY ?? 0 }
const w = nameText.value.length * 0.5 + 0.6
out.push({ id: 'name', label: 'name', x: c.x - w / 2, y: c.y - top - 0.55, w, h: 1.1 })
}
return out
})
const shapes = computed(() => def.value?.design?.shapes ?? [])
const selShape = computed(() => shapes.value.find(s => s.id === sel.value))
const selThing = computed(() => things.value.find(t => t.id === sel.value))
/** Things left off the box, to put back. */
const hidden = computed(() => {
const d = def.value
return (d?.design?.hide ?? []).map(id => ({
id,
label: id === 'name' ? 'Name' : id === 'screen' ? 'Screen' : d?.nodes.find(n => n.id === id)?.label?.trim() || (d?.nodes.find(n => n.id === id)?.kind ?? id),
}))
})
type Drag =
| { type: 'create'; id: string }
| { type: 'move'; id: string; start: { x: number; y: number }; orig: { x: number; y: number } }
| { type: 'resize'; id: string; handle: 'a' | 'b'; orig: FaceShape }
const drag = ref<Drag | null>(null)
function onDown(e: PointerEvent) {
if (!def.value || e.button !== 0) return
// No text selection: a later drag over selected text would turn into the browser's drag and drop.
e.preventDefault()
const p = at(e)
svg.value!.setPointerCapture(e.pointerId)
if (tool.value === 'text') {
const s: FaceShape = { id: uid('f'), kind: 'text', x: snap(p.x), y: snap(p.y - 0.5), w: 3, h: 1, text: 'Text', ink: ink.value === 'muted' ? 'text' : ink.value }
design().shapes!.push(s)
sel.value = s.id
tool.value = 'move'
ed.commit(false)
nextTick(() => textInput.value?.select())
return
}
if (tool.value !== 'move') {
const s: FaceShape = { id: uid('f'), kind: tool.value, x: snap(p.x), y: snap(p.y), w: 0, h: 0, ink: ink.value, ...(fill.value && tool.value !== 'line' ? { fill: true } : {}) }
design().shapes!.push(s)
sel.value = s.id
drag.value = { type: 'create', id: s.id }
return
}
const el = (e.target as Element).closest('[data-face]')
const id = el?.getAttribute('data-face')
if (!id) {
sel.value = null
return
}
sel.value = id
const handle = el!.getAttribute('data-handle') as 'a' | 'b' | null
const sh = shapes.value.find(s => s.id === id)
if (handle && sh) {
drag.value = { type: 'resize', id, handle, orig: { ...sh } }
return
}
const t = things.value.find(x => x.id === id)
const orig = sh ? { x: sh.x, y: sh.y } : t ? { x: t.x, y: t.y } : null
if (orig) drag.value = { type: 'move', id, start: p, orig }
}
function onMove(e: PointerEvent) {
const d = drag.value
if (!d) return
const p = at(e)
if (d.type === 'create') {
const s = shapes.value.find(x => x.id === d.id)
if (!s) return
s.w = snap(p.x) - s.x
s.h = snap(p.y) - s.y
if (e.shiftKey && s.kind === 'line') {
// Shift: straight across or straight down.
if (Math.abs(s.w) > Math.abs(s.h)) s.h = 0
else s.w = 0
}
return
}
if (d.type === 'move') {
const x = snap(d.orig.x + p.x - d.start.x)
const y = snap(d.orig.y + p.y - d.start.y)
const s = shapes.value.find(sh => sh.id === d.id)
if (s) {
s.x = x
s.y = y
} else if (d.id === 'name') {
const w = nameText.value.length * 0.5 + 0.6
design().place!.name = { x: x + w / 2, y: y + 0.55 }
} else design().place![d.id] = { x, y }
return
}
const s = shapes.value.find(sh => sh.id === d.id)
if (!s) return
const o = d.orig
if (d.handle === 'b') {
s.w = snap(p.x) - o.x
s.h = snap(p.y) - o.y
} else {
s.x = snap(p.x)
s.y = snap(p.y)
s.w = o.x + o.w - s.x
s.h = o.y + o.h - s.y
}
}
function onUp() {
const d = drag.value
drag.value = null
if (!d) return
if (d.type === 'create') {
const s = shapes.value.find(x => x.id === d.id)
// A click without dragging draws nothing.
if (s && Math.abs(s.w) < 0.25 && Math.abs(s.h) < 0.25) {
design().shapes = shapes.value.filter(x => x !== s)
sel.value = null
return
}
tool.value = 'move'
}
ed.commit(false)
}
function remove() {
if (!selShape.value) return
design().shapes = shapes.value.filter(s => s !== selShape.value)
sel.value = null
ed.commit(false)
}
function hide(id: string) {
const d = design()
d.hide = [...new Set([...(d.hide ?? []), id])]
sel.value = null
ed.commit(false)
}
function show(id: string) {
const d = design()
d.hide = (d.hide ?? []).filter(x => x !== id)
if (!d.hide.length) delete d.hide
ed.commit(false)
}
function setInk(i: FaceInk) {
ink.value = i
if (selShape.value) {
selShape.value.ink = i
ed.commit(false)
}
}
function toggleFill() {
fill.value = !fill.value
const s = selShape.value
if (s && s.kind !== 'line' && s.kind !== 'text') {
if (fill.value) s.fill = true
else delete s.fill
ed.commit(false)
}
}
function setSize(axis: 'w' | 'h', e: Event) {
const v = Number((e.target as HTMLInputElement).value)
if (!(v > 0)) return
design()[axis] = Math.min(80, Math.max(2, Math.round(v * 2) / 2))
ed.commit(false)
}
function reset() {
if (!def.value) return
delete def.value.design
sel.value = null
ed.commit(false)
}
const textInput = ref<HTMLInputElement>()
function onKey(e: KeyboardEvent) {
if ((e.target as HTMLElement).closest('input, textarea, select')) return
if ((e.key === 'Delete' || e.key === 'Backspace') && selShape.value) {
e.preventDefault()
remove()
}
}
function close() {
ed.dialog.value = null
ed.faceFor.value = null
}
/** Ends of a line, or opposite corners of anything else, to drag. */
const grips = computed(() => {
const s = selShape.value
if (!s || s.kind === 'text') return []
return [
{ h: 'a', x: s.x, y: s.y },
{ h: 'b', x: s.x + s.w, y: s.y + s.h },
]
})
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div v-if="def" class="modal face-modal" role="dialog" aria-labelledby="face-title" tabindex="-1" @keydown="onKey">
<div class="flex items-start justify-between gap-4">
<div>
<h2 id="face-title">The box of "{{ def.name }}"</h2>
<p class="hint mt-1">Drag lamps, displays and the name where you want them, draw on it, size it. Every placed copy shows it.</p>
</div>
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<div class="face-tools">
<div class="seg">
<button v-for="t in TOOLS" :key="t.id" :class="{ active: tool === t.id }" :title="t.title" @click="tool = t.id">
<Icon :name="t.icon" class="size-4" />
</button>
</div>
<div class="face-inks" title="Colour">
<button
v-for="i in FACE_INKS"
:key="i"
class="ctx-dot"
:class="{ active: (selShape?.ink ?? ink) === i }"
:style="{ background: INK_HEX[i] }"
:title="i"
@click="setInk(i)"
/>
</div>
<button class="btn-sm" :class="{ on: selShape?.fill ?? fill }" title="Fill boxes and circles lightly" @click="toggleFill">
<Icon name="lucide:paint-bucket" class="size-3.5" /> Fill
</button>
<label class="face-size">W <input type="number" min="2" max="80" step="0.5" :value="body.w" @change="setSize('w', $event)" /></label>
<label class="face-size">H <input type="number" min="2" max="80" step="0.5" :value="body.h" @change="setSize('h', $event)" /></label>
<button class="btn-sm ml-auto" :disabled="!def.design" title="Back to the automatic layout; drawings are removed" @click="reset">
<Icon name="lucide:rotate-ccw" class="size-3.5" /> Automatic
</button>
</div>
<svg
ref="svg"
class="face-canvas"
:class="{ drawing: tool !== 'move' }"
:width="VIEW_W"
:height="VIEW_H"
@pointerdown="onDown"
@pointermove="onMove"
@pointerup="onUp"
@pointercancel="onUp"
>
<defs>
<pattern id="face-grid" :width="G * k / 2" :height="G * k / 2" patternUnits="userSpaceOnUse" :x="PAD" :y="PAD">
<circle cx="0.6" cy="0.6" r="0.6" fill="#2a3442" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#face-grid)" />
<g :transform="`translate(${PAD} ${PAD - body.y * G * k}) scale(${k})`">
<EditorNodeView :node="preview" :shape="shape" :selected="false" />
<g :transform="`translate(0 ${body.y * G})`">
<rect
v-for="t in things"
:key="t.id"
:x="t.x * G - 2"
:y="t.y * G - 2"
:width="t.w * G + 4"
:height="t.h * G + 4"
rx="3"
class="face-thing"
:class="{ sel: sel === t.id }"
:data-face="t.id"
/>
<template v-for="s in shapes" :key="s.id">
<line
v-if="s.kind === 'line'"
:x1="s.x * G"
:y1="s.y * G"
:x2="(s.x + s.w) * G"
:y2="(s.y + s.h) * G"
class="face-hit"
:class="{ sel: sel === s.id }"
:data-face="s.id"
/>
<rect
v-else
:x="Math.min(s.x, s.x + s.w) * G - 2"
:y="Math.min(s.y, s.y + s.h) * G - 2"
:width="Math.max(Math.abs(s.w) * G, s.kind === 'text' ? (s.text?.length ?? 1) * 6.6 * (s.size ?? 1) : 0) + 4"
:height="(s.kind === 'text' ? 1 * (s.size ?? 1) : Math.abs(s.h)) * G + 4"
rx="2"
class="face-thing shape"
:class="{ sel: sel === s.id }"
:data-face="s.id"
/>
</template>
<circle v-for="g in grips" :key="g.h" :cx="g.x * G" :cy="g.y * G" r="3.5" class="face-grip" :data-face="sel!" :data-handle="g.h" />
</g>
</g>
</svg>
<div class="face-props">
<template v-if="selShape">
<template v-if="selShape.kind === 'text'">
<input ref="textInput" v-model="selShape.text" class="text-input" placeholder="Text" @change="ed.commit(false)" />
<select :value="selShape.size ?? 1" class="text-input w-28" @change="(selShape.size = Number(($event.target as HTMLSelectElement).value)), ed.commit(false)">
<option :value="0.75">Small</option>
<option :value="1">Normal</option>
<option :value="1.5">Large</option>
<option :value="2">Huge</option>
</select>
</template>
<span v-else class="hint">{{ { line: 'Line', rect: 'Box', ellipse: 'Circle' }[selShape.kind] }}: drag it, or its dots to resize. Shift keeps a new line straight.</span>
<button class="btn-sm danger ml-auto" @click="remove"><Icon name="lucide:trash-2" class="size-3.5" /> Delete</button>
</template>
<template v-else-if="selThing">
<span class="hint">{{ selThing.id === 'name' ? 'The name' : selThing.label }}: drag it where it belongs.</span>
<button class="btn-sm ml-auto" @click="hide(selThing.id)"><Icon name="lucide:eye-off" class="size-3.5" /> Leave off the box</button>
</template>
<span v-else class="hint">Pick a tool to draw, or drag things on the box. Delete removes a drawing.</span>
</div>
<div v-if="hidden.length" class="face-hidden">
<span class="hint">Left off:</span>
<button v-for="h in hidden" :key="h.id" class="btn-sm" title="Put it back on the box" @click="show(h.id)">
<Icon name="lucide:eye" class="size-3.5" /> {{ h.label }}
</button>
</div>
</div>
</div>
</template>