logigate/app/components/editor/NodeView.vue

141 lines
6.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { gateDrawing, sevenSegment } from '~/lib/draw'
import { GRID as G, noteLines, type Shape } from '~/lib/logic/geometry'
import type { CircuitNode } from '~/lib/logic/types'
const props = defineProps<{
node: CircuitNode
shape: Shape
selected: boolean
ghost?: boolean
}>()
const ed = useEditor()
const style = computed(() => ed.project.value.settings.symbols)
const gate = computed(() => (props.shape.gate ? gateDrawing(props.shape, style.value) : null))
const chipDef = computed(() => (props.node.chip ? ed.getDef.value(props.node.chip) : undefined))
const b = computed(() => props.shape.body)
const v = (pin: string) => (props.ghost ? 0 : ed.val(props.node.id, pin))
const hexValue = computed(() => [0, 1, 2, 3].reduce((n, i) => n + (v(`b${i}`) << i), 0))
const segments = computed(() => sevenSegment(hexValue.value))
const lit = computed(() => v(props.node.kind === 'input' || props.node.kind === 'button' || props.node.kind === 'clock' ? 'q' : 'a') === 1)
const title = computed(() => {
if (props.node.kind === 'chip') return chipDef.value ? `${chipDef.value.name}${chipDef.value.description ? `: ${chipDef.value.description}` : ''}\nDouble-click to open` : 'Missing component'
return undefined
})
</script>
<template>
<g
:transform="`translate(${node.x * G} ${node.y * G})`"
:data-node="node.id"
class="node"
:class="{ selected, ghost, [`kind-${node.kind}`]: true }"
>
<title v-if="title">{{ title }}</title>
<!-- Logic gate symbol (NAND, or a component drawn as a gate) -->
<template v-if="gate">
<path :d="gate.stubs" class="stub" />
<path :d="gate.body" class="body" :class="{ prim: node.kind === 'nand' }" />
<path v-if="gate.extra" :d="gate.extra" class="stroke-only" />
<circle v-if="gate.bubble" v-bind="gate.bubble" class="body" />
<text v-if="gate.label" :x="gate.label.x" :y="gate.label.y" class="gate-label" text-anchor="middle">{{ gate.label.text }}</text>
</template>
<!-- Component box -->
<template v-else-if="node.kind === 'chip'">
<rect :x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="3" class="body chip" :class="{ missing: !chipDef }" />
<text :x="(b.w * G) / 2" :y="(b.y + b.h / 2) * G + 4" text-anchor="middle" class="chip-name">{{ chipDef?.name ?? 'Missing' }}</text>
<text v-for="p in shape.inputs" :key="p.key" :x="5" :y="p.dy * G + 3.5" class="pin-label">{{ p.label }}</text>
<text v-for="p in shape.outputs" :key="p.key" :x="b.w * G - 5" :y="p.dy * G + 3.5" text-anchor="end" class="pin-label">{{ p.label }}</text>
</template>
<!-- Switch (input pin) -->
<template v-else-if="node.kind === 'input'">
<text :x="-6" :y="4" text-anchor="end" class="io-label">{{ node.label }}</text>
<rect x="0" :y="-0.6 * G" :width="2 * G" :height="1.2 * G" :rx="0.6 * G" class="switch" :class="{ on: lit }" />
<circle :cx="lit ? 1.4 * G : 0.6 * G" cy="0" :r="0.42 * G" class="knob" />
</template>
<!-- Output pin -->
<template v-else-if="node.kind === 'output'">
<path :d="`M0 0H${0.4 * G}`" class="stub" />
<path :d="`M${0.4 * G} ${-0.6 * G}H${1.5 * G}L${2 * G} 0L${1.5 * G} ${0.6 * G}H${0.4 * G}Z`" class="terminal" :class="{ on: lit }" />
<text :x="2 * G + 6" :y="4" class="io-label">{{ node.label }}</text>
</template>
<!-- Lamp: IEC symbol, circle with a cross -->
<template v-else-if="node.kind === 'lamp'">
<path :d="`M0 0H${0.35 * G}`" class="stub" />
<circle :cx="G" cy="0" :r="0.65 * G" class="lamp" :class="{ on: lit }" />
<path :d="`M${G - 0.46 * G} ${-0.46 * G}L${G + 0.46 * G} ${0.46 * G}M${G + 0.46 * G} ${-0.46 * G}L${G - 0.46 * G} ${0.46 * G}`" class="lamp-cross" :class="{ on: lit }" />
<text v-if="node.label" :x="2 * G + 4" :y="4" class="io-label">{{ node.label }}</text>
</template>
<!-- Constant 1 -->
<template v-else-if="node.kind === 'high'">
<rect x="0" :y="-0.6 * G" :width="1.5 * G" :height="1.2 * G" rx="2" class="body" />
<text :x="0.75 * G" y="4" text-anchor="middle" class="const on">1</text>
<path :d="`M${1.5 * G} 0H${2 * G}`" class="stub" />
</template>
<!-- Constant 0: ground symbol -->
<template v-else-if="node.kind === 'low'">
<path :d="`M${2 * G} 0H${1.1 * G}M${1.1 * G} ${-0.6 * G}V${0.6 * G}M${0.75 * G} ${-0.4 * G}V${0.4 * G}M${0.4 * G} ${-0.2 * G}V${0.2 * G}`" class="ground" />
<rect x="0" :y="-0.7 * G" :width="2 * G" :height="1.4 * G" fill="transparent" />
</template>
<!-- Clock -->
<template v-else-if="node.kind === 'clock'">
<rect x="0" :y="-0.7 * G" :width="2.5 * G" :height="1.4 * G" rx="2" class="body" />
<path :d="`M${0.35 * G} ${0.35 * G}H${0.8 * G}V${-0.35 * G}H${1.3 * G}V${0.35 * G}H${1.8 * G}V${-0.35 * G}H${2.15 * G}`" class="wave" :class="{ on: lit }" />
<path :d="`M${2.5 * G} 0H${3 * G}`" class="stub" />
</template>
<!-- Push button -->
<template v-else-if="node.kind === 'button'">
<text v-if="node.label" :x="-6" :y="4" text-anchor="end" class="io-label">{{ node.label }}</text>
<rect x="0" :y="-0.7 * G" :width="1.6 * G" :height="1.4 * G" rx="3" class="body" />
<circle :cx="0.8 * G" cy="0" :r="0.45 * G" class="button" :class="{ on: lit }" />
<path :d="`M${1.6 * G} 0H${2 * G}`" class="stub" />
</template>
<!-- Hex display -->
<template v-else-if="node.kind === 'hex'">
<rect x="0" :y="b.y * G" :width="b.w * G" :height="b.h * G" rx="3" class="body display" />
<path v-for="(s, i) in segments" :key="i" :d="s.d" class="seg" :class="{ on: s.on }" />
<text v-for="p in shape.inputs" :key="p.key" :x="4" :y="p.dy * G + 3" class="pin-label tiny">{{ p.label }}</text>
</template>
<!-- Note -->
<template v-else-if="node.kind === 'note'">
<rect :x="-4" :y="b.y * G" :width="b.w * G + 8" :height="b.h * G" rx="3" class="note-bg" />
<text v-for="(line, i) in noteLines(node)" :key="i" x="0" :y="i * G + 4" class="note">{{ line }}</text>
</template>
<rect
v-if="selected"
:x="b.x * G - 4"
:y="b.y * G - 4"
:width="b.w * G + 8"
:height="b.h * G + 8"
rx="5"
class="sel-outline"
/>
<template v-if="!ghost">
<g v-for="p in shape.inputs" :key="`i-${p.key}`">
<circle :cx="p.dx * G" :cy="p.dy * G" r="7" class="pin-hit" :data-node="node.id" :data-pin="p.key" data-dir="in" />
<circle :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
</g>
<g v-for="p in shape.outputs" :key="`o-${p.key}`">
<circle :cx="p.dx * G" :cy="p.dy * G" r="7" class="pin-hit" :data-node="node.id" :data-pin="p.key" data-dir="out" />
<circle :cx="p.dx * G" :cy="p.dy * G" r="2.6" class="pin" :class="{ on: v(p.key) === 1 }" />
</g>
</template>
</g>
</template>