logigate/app/components/editor/FaceDialog.vue

793 lines
33 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
// Drawing a component's box by hand: place, size and turn its lamps,
// displays, screens and name; split a screen into pixels to place one by
// one; show displays of the components inside it; space and reorder its
// pins; draw lines, boxes, circles and text. Pick several things (Shift +
// click, or a box around them) to move them together. Everything shows live
// on the box, and on every copy placed.
import { faceCandidates, faceTransform, GRID as G, pixelKey, shapeTransform, type FaceItem } 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, size and turn' },
{ 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 box item's key, 'name', or 'pin:<key>'; the last one picked. */
const sel = ref<string | null>(null)
/** Everything picked, to move together (Shift + click, or a box around them). */
const group = ref<string[]>([])
const isSel = (id: string) => sel.value === id || group.value.includes(id)
function pick(id: string | null) {
sel.value = id
group.value = id ? [id] : []
}
watch(sel, v => {
if (v && !group.value.includes(v)) group.value = [v]
if (!v) group.value = []
})
const VIEW_W = 620
const VIEW_H = 440
const PAD = 34
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.key] = { x: it.x, y: it.y - s.body.y }
// A new design keeps the box as big as what is on it.
d.design = { w: s.body.w, h: s.body.h, place, shapes: [], fit: true }
}
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 ?? '')
const top = computed(() => body.value.y)
/** The box's items as placed, plus the name, to grab. */
const items = computed(() => shape.value.face ?? [])
const nameBox = computed(() => {
if (shape.value.nameAt === null) return 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
return { x: c.x - w / 2, y: c.y - 0.55, w, h: 1.1 }
})
/** Pins on both edges, cable sockets too, to drag up and down. */
const pins = computed(() => [
...shape.value.inputs.filter(p => p.dx === 0).map(p => ({ ...p, side: 'in' as const })),
...shape.value.outputs.filter(p => p.dx > 0).map(p => ({ ...p, side: 'out' as const })),
// Pins on the other edge than usual (lines of a socket that run the other way) stay with their edge.
...shape.value.inputs.filter(p => p.dx > 0).map(p => ({ ...p, side: 'out' as const })),
...shape.value.outputs.filter(p => p.dx === 0).map(p => ({ ...p, side: 'in' as const })),
])
const shapes = computed(() => def.value?.design?.shapes ?? [])
const selShape = computed(() => shapes.value.find(s => s.id === sel.value))
const selItem = computed(() => items.value.find(it => it.key === sel.value))
/** Everything the box can show, with whether it does. */
const candidates = computed(() => {
const d = def.value
if (!d) return []
const hide = new Set(d.design?.hide ?? [])
const show = new Set(d.design?.show ?? [])
return faceCandidates(d, ed.getDef.value).map(c => ({ ...c, shown: c.nested ? show.has(c.key) && !hide.has(c.key) : !hide.has(c.key) }))
})
const nameShown = computed(() => !(def.value?.design?.hide ?? []).includes('name'))
type Drag =
| { type: 'create'; id: string }
| { type: 'move'; start: { x: number; y: number }; origs: Map<string, { x: number; y: number }> }
| { type: 'resize'; id: string; handle: 'a' | 'b'; orig: FaceShape }
| { type: 'scale'; key: string }
| { type: 'box'; start: { x: number; y: number }; cur: { x: number; y: number }; add: boolean }
| { type: 'pin'; key: string; side: 'in' | 'out'; start: number; rows: { key: string; dy: number; set: boolean; port?: string }[]; index: number }
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) {
// Empty space: a box around things picks them (Shift: adds to what is picked).
if (!e.shiftKey) pick(null)
drag.value = { type: 'box', start: p, cur: p, add: e.shiftKey }
return
}
if (e.shiftKey && !id.startsWith('pin:')) {
// Shift + click: in or out of what is picked.
group.value = group.value.includes(id) ? group.value.filter(x => x !== id) : [...group.value, id]
sel.value = group.value.at(-1) ?? null
return
}
if (el!.getAttribute('data-handle') === 'scale') {
drag.value = { type: 'scale', key: id }
return
}
const many = group.value.length > 1 && group.value.includes(id)
if (!many) pick(id)
else sel.value = id
if (id.startsWith('pin:')) {
const key = id.slice(4)
const pin = pins.value.find(x => x.key === key)!
const side = pins.value.filter(x => x.side === pin.side).sort((a, b) => a.dy - b.dy)
const pinAt = def.value.design?.pinAt ?? {}
drag.value = { type: 'pin', key, side: pin.side, start: p.y, rows: side.map(x => ({ key: x.key, dy: x.dy, set: pinAt[x.key] !== undefined, ...(x.port ? { port: x.port } : {}) })), index: side.findIndex(x => x.key === key) }
return
}
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 origs = new Map<string, { x: number; y: number }>()
for (const g of many ? group.value : [id]) {
const o = origOf(g)
if (o) origs.set(g, o)
}
if (origs.size) drag.value = { type: 'move', start: p, origs }
}
/** Where a thing on the box is now, from the box's top left (a drawing's own corner, the name's box). */
function origOf(id: string): { x: number; y: number } | null {
const sh = shapes.value.find(s => s.id === id)
if (sh) return { x: sh.x, y: sh.y }
const it = items.value.find(x => x.key === id)
if (it) return { x: it.x, y: it.y - top.value }
if (id === 'name' && nameBox.value) return { x: nameBox.value.x, y: nameBox.value.y - top.value }
return null
}
/** Puts a thing on the box with its top left (as origOf gives it) at x, y. */
function putAt(id: string, x: number, y: number) {
const s = shapes.value.find(sh => sh.id === id)
if (s) {
s.x = x
s.y = y
} else if (id === 'name') {
const w = nameText.value.length * 0.5 + 0.6
design().place!.name = { x: x + w / 2, y: y + 0.55 }
} else if (!id.startsWith('pin:')) design().place![id] = { x, y }
}
/** The area a thing covers on the box, from its top left, sized and turned as shown. */
function areaOf(id: string): { x: number; y: number; w: number; h: number } | null {
const sh = shapes.value.find(s => s.id === id)
if (sh) {
const w = sh.kind === 'text' ? (sh.text?.length ?? 1) * 0.42 * (sh.size ?? 1) : Math.abs(sh.w)
const h = sh.kind === 'text' ? (sh.size ?? 1) : Math.abs(sh.h)
return { x: Math.min(sh.x, sh.x + sh.w), y: Math.min(sh.y, sh.y + sh.h), w, h }
}
const it = items.value.find(x => x.key === id)
if (it) {
const k = it.scale ?? 1
const turned = it.rotate === 90 || it.rotate === 270
const w = (turned ? it.h : it.w) * k
const h = (turned ? it.w : it.h) * k
return { x: it.x + (it.w * k) / 2 - w / 2, y: it.y - top.value + (it.h * k) / 2 - h / 2, w, h }
}
if (id === 'name' && nameBox.value) return { ...nameBox.value, y: nameBox.value.y - top.value }
return null
}
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 === 'pin') {
const row = d.rows[d.index]!
// A cable socket takes the lines split out of it along: they move as one block.
const block = d.rows.filter(r => r === row || (r.port && r.port === row.key))
const others = d.rows.filter(r => !block.includes(r))
const span = Math.max(...block.map(r => r.dy)) - row.dy
const want = Math.max(0, Math.round(row.dy + p.y - d.start))
const above = others.filter(r => r.dy < row.dy).at(-1)
const below = others.find(r => r.dy > row.dy + span)
const pinAt = (design().pinAt ??= {})
const putBlock = (top: number) => {
for (const r of block) pinAt[r.key] = top + (r.dy - row.dy)
}
if ((below && want + span >= below.dy) || (above && want <= above.dy)) {
// Onto or past a neighbour: the pins change order. The others stay where they are, this one goes where it is dragged.
const at = others.filter(r => r.dy < want || (r.dy === want && want > row.dy)).length
const order = [...others.slice(0, at), ...block, ...others.slice(at)].map(r => r.key)
const keep = (design().pinOrder ?? []).filter(k => !d.rows.some(r => r.key === k))
design().pinOrder = [...keep, ...order]
for (const r of others) pinAt[r.key] = r.dy
putBlock(want)
return
}
// Moved along its own gap: it and the pins below it on that edge go, so a gap opens above it.
const delta = Math.max(Math.round(p.y - d.start), (above ? above.dy + 1 : 0) - row.dy)
putBlock(row.dy + delta)
for (const r of others) if (r.dy > row.dy && r.set) pinAt[r.key] = Math.max(0, r.dy + delta)
return
}
if (d.type === 'move') {
const dx = p.x - d.start.x
const dy = p.y - d.start.y
for (const [id, o] of d.origs) putAt(id, snap(o.x + dx), snap(o.y + dy))
return
}
if (d.type === 'box') {
d.cur = p
return
}
if (d.type === 'scale') {
// Grown from its top left corner, as far as the pointer is pulled.
const it = items.value.find(x => x.key === d.key)
if (!it) return
const turned = it.rotate === 90 || it.rotate === 270
const kx = (p.x - it.x) / (turned ? it.h : it.w)
const ky = (p.y + top.value - it.y) / (turned ? it.w : it.h)
const k = Math.min(4, Math.max(0.25, Math.round(Math.max(kx, ky) * 20) / 20))
const des = design()
des.scale ??= {}
if (k === 1) delete des.scale[d.key]
else des.scale[d.key] = k
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 === 'box') {
// Everything the box touches is picked.
const x0 = Math.min(d.start.x, d.cur.x)
const x1 = Math.max(d.start.x, d.cur.x)
const y0 = Math.min(d.start.y, d.cur.y)
const y1 = Math.max(d.start.y, d.cur.y)
if (x1 - x0 < 0.2 && y1 - y0 < 0.2) return
const ids = [...shapes.value.map(sh => sh.id), ...items.value.map(it => it.key), ...(nameBox.value ? ['name'] : [])].filter(id => {
const a = areaOf(id)
return a && a.x < x1 && a.x + a.w > x0 && a.y < y1 && a.y + a.h > y0
})
group.value = d.add ? [...new Set([...group.value, ...ids])] : ids
sel.value = group.value.at(-1) ?? null
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() {
const gone = new Set(group.value.filter(id => shapes.value.some(s => s.id === id)))
if (selShape.value) gone.add(selShape.value.id)
if (!gone.size) return
design().shapes = shapes.value.filter(s => !gone.has(s.id))
pick(null)
ed.commit(false)
}
/** Moves everything picked a step (a quarter square; Shift: a whole one). */
function nudge(dx: number, dy: number) {
const ids = group.value.length ? group.value : sel.value ? [sel.value] : []
let moved = false
for (const id of ids) {
const o = origOf(id)
if (!o) continue
putAt(id, snap(o.x + dx), snap(o.y + dy))
moved = true
}
if (moved) ed.commit(false)
}
// ── Screens pixel by pixel ──────────────────────────────────────────
/** The screen of the selected item, when it is a whole screen (to split) or one of its pixels (to join). */
const selScreen = computed(() => {
const it = selItem.value
if (!it || it.kind !== 'screen') return null
if (it.screenOf) return { key: it.screenOf, split: true }
return (it.pixels?.length ?? 0) > 1 ? { key: it.key, split: false } : null
})
/** Splits a screen into pixels placed one by one (each where it sat), or joins them into one screen again. */
function toggleSplit(key: string, split: boolean) {
const d = design()
const set = new Set(d.split ?? [])
if (split) set.add(key)
else {
set.delete(key)
// The pixels' own places go; the screen keeps the place it had.
const it = items.value.find(x => x.screenOf === key)
for (const p of items.value.filter(x => x.screenOf === key)) delete d.place?.[p.key]
if (it && !d.place?.[key]) d.place![key] = { x: it.x, y: it.y - top.value }
}
if (set.size) d.split = [...set]
else delete d.split
pick(split ? pixelKey(items.value.find(x => x.key === key)?.pixels?.[0]?.id ?? '') : key)
ed.commit(false)
}
/** Fitted: the box is as big as what is on it. Off: it keeps the width and height set. */
function toggleFit() {
const d = design()
if (d.fit) {
delete d.fit
d.w = body.value.w
d.h = body.value.h
} else d.fit = true
ed.commit(false)
}
/** Shows or leaves off a thing: own items are shown unless left off, displays of parts inside only when shown. */
function setShown(key: string, nested: boolean, on: boolean) {
const d = design()
const hide = new Set(d.hide ?? [])
const show = new Set(d.show ?? [])
if (on) hide.delete(key)
else hide.add(key)
if (nested) {
if (on) show.add(key)
else show.delete(key)
}
d.hide = [...hide]
d.show = [...show]
if (!d.hide.length) delete d.hide
if (!d.show.length) delete d.show
if (!on && sel.value === key) pick(null)
ed.commit(false)
if (on && nested) {
nextTick(() => {
const it = items.value.find(x => x.key === key)
if (it && !d.place?.[key]) {
d.place![key] = { x: it.x, y: it.y - top.value }
pick(key)
ed.commit(false)
}
})
}
}
/** Grows or shrinks the selected item (steps of a quarter), or the size of a text. */
function grow(by: number) {
const s = selShape.value
if (s?.kind === 'text') {
s.size = Math.min(4, Math.max(0.5, Math.round(((s.size ?? 1) + by) * 4) / 4))
return ed.commit(false)
}
const key = sel.value
if (!key || !selItem.value) return
const d = design()
const next = Math.min(4, Math.max(0.25, Math.round(((d.scale?.[key] ?? selItem.value.scale ?? 1) + by) * 4) / 4))
d.scale ??= {}
if (next === 1) delete d.scale[key]
else d.scale[key] = next
ed.commit(false)
}
/** Turns the selected item or drawing a quarter. */
function turn(by: 90 | -90) {
const s = selShape.value
if (s) {
const r = (((s.rotate ?? 0) + by) % 360 + 360) % 360
if (r) s.rotate = r
else delete s.rotate
return ed.commit(false)
}
const key = sel.value
if (!key || !selItem.value) return
const d = design()
const r = (((d.rotate?.[key] ?? 0) + by) % 360 + 360) % 360
d.rotate ??= {}
if (r) d.rotate[key] = r
else delete d.rotate[key]
ed.commit(false)
}
function resetPins() {
if (!def.value?.design?.pinAt) return
delete def.value.design.pinAt
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
const d = design()
// A size typed in: the box keeps it (and what it shows can still make it bigger).
if (d.fit) {
delete d.fit
d.w = body.value.w
d.h = body.value.h
}
d[axis] = Math.min(120, 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>()
// Keys while the designer is open: R turns, + and − size, Delete removes a drawing.
onMounted(() => window.addEventListener('keydown', onKey))
onBeforeUnmount(() => window.removeEventListener('keydown', onKey))
const ARROW: Record<string, [number, number]> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
function onKey(e: KeyboardEvent) {
if ((e.target as HTMLElement).closest?.('input, textarea, select') || e.ctrlKey || e.metaKey || e.altKey) return
const arrow = ARROW[e.key]
if (arrow && (sel.value || group.value.length)) {
e.preventDefault()
const step = e.shiftKey ? 1 : 0.25
nudge(arrow[0] * step, arrow[1] * step)
} else if (e.key === 'Escape' && group.value.length) {
e.preventDefault()
e.stopPropagation()
pick(null)
} else if ((e.key === 'Delete' || e.key === 'Backspace') && (selShape.value || group.value.some(id => shapes.value.some(s => s.id === id)))) {
e.preventDefault()
remove()
} else if (e.key === 'r' || e.key === 'R') turn(e.shiftKey ? -90 : 90)
else if (e.key === '+' || e.key === '=') grow(0.25)
else if (e.key === '-') grow(-0.25)
}
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 },
]
})
const kindName = (it: FaceItem) => (it.screenOf ? 'Pixel' : it.kind === 'screen' ? 'Screen' : it.kind === 'hex' ? 'Display' : 'Lamp')
/** What a thing on the box is, and where from when it is a display of a part inside. */
const tipOf = (it: FaceItem) => `${it.label || kindName(it)}${it.from ? ` from ${it.from}` : ''}`
/** The size grip of the item picked alone: its bottom right corner, sized and turned with it. */
const scaleGrip = computed(() => {
const it = selItem.value
if (!it || group.value.length > 1) return null
return { it, x: (it.x + it.w) * G, y: (it.y + it.h) * G, r: 3.5 / (it.scale ?? 1) }
})
/** The box being dragged around things, in grid units from the box's top left. */
const pickBox = computed(() => {
const d = drag.value
if (d?.type !== 'box') return null
return { x: Math.min(d.start.x, d.cur.x), y: Math.min(d.start.y, d.cur.y), w: Math.abs(d.cur.x - d.start.x), h: Math.abs(d.cur.y - d.start.y) }
})
const selLabel = computed(() => {
if (selShape.value) return { line: 'Line', rect: 'Box', ellipse: 'Circle', text: 'Text' }[selShape.value.kind]
if (group.value.length > 1) return `${group.value.length} things`
if (selItem.value) return `${kindName(selItem.value)}${selItem.value.label ? ` ${selItem.value.label}` : ''}${selItem.value.from ? ` from ${selItem.value.from}` : ''}`
if (sel.value === 'name') return 'The name'
if (sel.value?.startsWith('pin:')) return `Pin ${pins.value.find(p => `pin:${p.key}` === sel.value)?.label ?? ''}`
return ''
})
const ICON: Record<string, string> = { lamp: 'lucide:lightbulb', hex: 'lucide:binary', screen: 'lucide:monitor' }
</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">
<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">Place, size and turn what it shows, drag pins apart, draw on 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>
<button class="btn-sm" :class="{ on: def.design?.fit }" title="The box is as big as what is on it (pins, name, displays, drawings). Off: it keeps the width and height set" @click="toggleFit">
<Icon name="lucide:scan" class="size-3.5" /> Fit
</button>
<label class="face-size">W <input type="number" min="2" max="120" step="0.5" :value="body.w" @change="setSize('w', $event)" /></label>
<label class="face-size">H <input type="number" min="2" max="120" 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>
<div class="face-body">
<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" />
<!-- Things on the box, grabbed where they are drawn (sized and turned alike) -->
<g v-for="it in items" :key="it.key" :transform="faceTransform(it)">
<rect
:x="it.x * G - 2"
:y="it.y * G - 2"
:width="it.w * G + 4"
:height="it.h * G + 4"
rx="3"
class="face-thing"
:class="{ sel: isSel(it.key), nested: !!it.from }"
:data-face="it.key"
>
<title>{{ tipOf(it) }}</title>
</rect>
<!-- Its size grip, picked alone: drag to make it bigger or smaller -->
<circle
v-if="scaleGrip?.it.key === it.key"
:cx="scaleGrip.x"
:cy="scaleGrip.y"
:r="scaleGrip.r"
class="face-grip scale"
:data-face="it.key"
data-handle="scale"
>
<title>Drag to size</title>
</circle>
</g>
<rect
v-if="nameBox"
:x="nameBox.x * G"
:y="nameBox.y * G"
:width="nameBox.w * G"
:height="nameBox.h * G"
rx="3"
class="face-thing"
:class="{ sel: isSel('name') }"
data-face="name"
/>
<rect
v-for="p in pins"
:key="`pin-${p.key}`"
:x="(p.dx - (p.side === 'in' ? 0.45 : 1.6)) * G"
:y="(p.dy - 0.42) * G"
:width="2.05 * G"
:height="0.84 * G"
rx="3"
class="face-thing face-pinbox"
:class="{ sel: sel === `pin:${p.key}` }"
:data-face="`pin:${p.key}`"
>
<title>Drag up or down; past another pin, they swap</title>
</rect>
<g :transform="`translate(0 ${body.y * G})`">
<g v-for="s in shapes" :key="s.id" :transform="shapeTransform(s)">
<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: isSel(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: isSel(s.id) }"
:data-face="s.id"
/>
</g>
<g v-if="selShape && group.length <= 1" :transform="shapeTransform(selShape)">
<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>
</g>
<rect v-if="pickBox" :x="PAD + pickBox.x * G * k" :y="PAD + pickBox.y * G * k" :width="pickBox.w * G * k" :height="pickBox.h * G * k" class="face-pickbox" />
</svg>
<aside class="face-side">
<h3>On the box</h3>
<ul class="face-list">
<li>
<label class="check">
<input type="checkbox" :checked="nameShown" @change="setShown('name', false, ($event.target as HTMLInputElement).checked)" />
<Icon name="lucide:type" class="size-3.5" /> Name
</label>
</li>
<li v-for="c in candidates.filter(x => !x.nested)" :key="c.key" :class="{ sel: sel === c.key }">
<label class="check">
<input type="checkbox" :checked="c.shown" @change="setShown(c.key, false, ($event.target as HTMLInputElement).checked)" />
<Icon :name="ICON[c.kind] ?? 'lucide:box'" class="size-3.5" />
<span class="truncate">{{ c.label }}</span>
</label>
</li>
</ul>
<template v-if="candidates.some(x => x.nested)">
<h3>From parts inside</h3>
<ul class="face-list">
<li v-for="c in candidates.filter(x => x.nested)" :key="c.key" :class="{ sel: sel === c.key }">
<label class="check">
<input type="checkbox" :checked="c.shown" @change="setShown(c.key, true, ($event.target as HTMLInputElement).checked)" />
<Icon :name="ICON[c.kind] ?? 'lucide:box'" class="size-3.5" />
<span class="truncate">{{ c.label }}</span>
</label>
</li>
</ul>
</template>
<p v-if="!candidates.length" class="hint">No lamps, displays or pixels inside yet.</p>
<h3>Pins</h3>
<p class="hint">Drag a pin up or down: the pins under it follow, so groups get gaps.</p>
<button class="btn-sm" :disabled="!def.design?.pinAt" @click="resetPins"><Icon name="lucide:rows-3" class="size-3.5" /> Pins evenly again</button>
</aside>
</div>
<div class="face-props">
<template v-if="sel && selLabel">
<span class="font-semibold">{{ selLabel }}</span>
<button v-if="selScreen && group.length <= 1" class="btn-sm" :title="selScreen.split ? 'Back into one screen' : 'Each pixel on its own, to place one by one'" @click="toggleSplit(selScreen.key, !selScreen.split)">
<Icon :name="selScreen.split ? 'lucide:grid-2x2' : 'lucide:grid-2x2-plus'" class="size-3.5" /> {{ selScreen.split ? 'Join pixels' : 'Split into pixels' }}
</button>
<template v-if="(selShape || selItem) && group.length <= 1">
<div class="flex gap-1" title="Turn (R, Shift+R)">
<button class="icon-btn" @click="turn(-90)"><Icon name="lucide:rotate-ccw" class="size-4" /></button>
<button class="icon-btn" @click="turn(90)"><Icon name="lucide:rotate-cw" class="size-4" /></button>
</div>
<div v-if="selItem || selShape?.kind === 'text'" class="flex items-center gap-1" title="Size (+, −)">
<button class="icon-btn" @click="grow(-0.25)"><Icon name="lucide:minus" class="size-4" /></button>
<span class="tabular-nums text-xs">{{ Math.round(((selShape ? selShape.size : (def.design?.scale?.[sel] ?? selItem?.scale)) ?? 1) * 100) }}%</span>
<button class="icon-btn" @click="grow(0.25)"><Icon name="lucide:plus" class="size-4" /></button>
</div>
</template>
<template v-if="selShape?.kind === 'text'">
<input ref="textInput" v-model="selShape.text" class="text-input" placeholder="Text" @change="ed.commit(false)" />
</template>
<span v-if="group.length > 1" class="hint">Drag one to move them all, or use the arrow keys (Shift: a whole square).</span>
<button v-if="selShape || group.some(id => shapes.some(x => x.id === id))" class="btn-sm danger ml-auto" @click="remove"><Icon name="lucide:trash-2" class="size-3.5" /> Delete</button>
<button v-else-if="selItem && group.length <= 1" class="btn-sm ml-auto" @click="setShown(selItem.key, selItem.key.includes('/'), false)">
<Icon name="lucide:eye-off" class="size-3.5" /> Leave off
</button>
<button v-else-if="sel === 'name'" class="btn-sm ml-auto" @click="setShown('name', false, false)"><Icon name="lucide:eye-off" class="size-3.5" /> Leave off</button>
</template>
<span v-else class="hint">Drag things on the box; Shift + click or a box around them picks several. The corner of a picked thing sizes it. R turns, + and − size, Delete removes a drawing.</span>
</div>
</div>
</div>
</template>